From 7a1e6f507c705e395150129047642059d41a1de1 Mon Sep 17 00:00:00 2001
From: Daniel Gerhardt <code@dgerhardt.net>
Date: Sun, 16 Apr 2017 18:01:53 +0200
Subject: [PATCH] Add and setup Ektorp dependency

Ektorp is introduced as a replacement for CouchDB4J.
---
 pom.xml                                       | 10 ++++++
 .../java/de/thm/arsnova/config/AppConfig.java | 27 ++++++++++++++++
 .../java/de/thm/arsnova/entities/Entity.java  | 31 ++++++++++++++++++
 .../serialization/CouchDbDocumentMixIn.java   | 29 +++++++++++++++++
 .../serialization/CouchDbDocumentModule.java  | 32 +++++++++++++++++++
 .../CouchDbObjectMapperFactory.java           | 31 ++++++++++++++++++
 .../arsnova/entities/serialization/View.java  | 23 +++++++++++++
 7 files changed, 183 insertions(+)
 create mode 100644 src/main/java/de/thm/arsnova/entities/Entity.java
 create mode 100644 src/main/java/de/thm/arsnova/entities/serialization/CouchDbDocumentMixIn.java
 create mode 100644 src/main/java/de/thm/arsnova/entities/serialization/CouchDbDocumentModule.java
 create mode 100644 src/main/java/de/thm/arsnova/entities/serialization/CouchDbObjectMapperFactory.java
 create mode 100644 src/main/java/de/thm/arsnova/entities/serialization/View.java

diff --git a/pom.xml b/pom.xml
index 667ccecef..2bf9e1a8d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -190,6 +190,16 @@
 			<artifactId>couchdb4j</artifactId>
 			<version>0.8-SNAPSHOT</version>
 		</dependency>
+		<dependency>
+			<groupId>org.ektorp</groupId>
+			<artifactId>org.ektorp</artifactId>
+			<version>1.4.4</version>
+		</dependency>
+		<dependency>
+			<groupId>org.ektorp</groupId>
+			<artifactId>org.ektorp.spring</artifactId>
+			<version>1.4.4</version>
+		</dependency>
 		<dependency>
 			<groupId>javax.servlet</groupId>
 			<artifactId>javax.servlet-api</artifactId>
diff --git a/src/main/java/de/thm/arsnova/config/AppConfig.java b/src/main/java/de/thm/arsnova/config/AppConfig.java
index 00b5d0c0f..77cf95fc1 100644
--- a/src/main/java/de/thm/arsnova/config/AppConfig.java
+++ b/src/main/java/de/thm/arsnova/config/AppConfig.java
@@ -22,12 +22,16 @@ import com.fasterxml.jackson.databind.SerializationFeature;
 import de.thm.arsnova.ImageUtils;
 import de.thm.arsnova.connector.client.ConnectorClient;
 import de.thm.arsnova.connector.client.ConnectorClientImpl;
+import de.thm.arsnova.entities.serialization.CouchDbObjectMapperFactory;
 import de.thm.arsnova.socket.ARSnovaSocket;
 import de.thm.arsnova.socket.ARSnovaSocketIOServer;
 import de.thm.arsnova.web.CacheControlInterceptorHandler;
 import de.thm.arsnova.web.CorsFilter;
 import de.thm.arsnova.web.DeprecatedApiInterceptorHandler;
 import de.thm.arsnova.web.ResponseInterceptorHandler;
+import org.ektorp.impl.StdCouchDbConnector;
+import org.ektorp.impl.StdCouchDbInstance;
+import org.ektorp.spring.HttpClientFactoryBean;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.beans.factory.config.PropertiesFactoryBean;
@@ -98,6 +102,10 @@ public class AppConfig extends WebMvcConfigurerAdapter {
 	@Autowired
 	private Environment env;
 
+	@Value("${couchdb.name}") private String couchDbName;
+	@Value("${couchdb.host}") private String couchDbHost;
+	@Value("${couchdb.port}") private int couchDbPort;
+
 	@Value(value = "${connector.enable}") private boolean connectorEnable;
 	@Value(value = "${connector.uri}") private String connectorUri;
 	@Value(value = "${connector.username}") private String connectorUsername;
@@ -226,6 +234,25 @@ public class AppConfig extends WebMvcConfigurerAdapter {
 		return new CorsFilter(Arrays.asList(corsOrigins));
 	}
 
+	@Bean
+	public StdCouchDbConnector couchDbConnector() throws Exception {
+		return new StdCouchDbConnector(couchDbName, couchDbInstance(), new CouchDbObjectMapperFactory());
+	}
+
+	@Bean
+	public StdCouchDbInstance couchDbInstance() throws Exception {
+		return new StdCouchDbInstance(couchDbHttpClientFactory().getObject());
+	}
+
+	@Bean
+	public HttpClientFactoryBean couchDbHttpClientFactory() throws Exception {
+		final HttpClientFactoryBean factory = new HttpClientFactoryBean();
+		factory.setHost(couchDbHost);
+		factory.setPort(couchDbPort);
+
+		return factory;
+	}
+
 	@Bean(name = "connectorClient")
 	public ConnectorClient connectorClient() {
 		if (!connectorEnable) {
diff --git a/src/main/java/de/thm/arsnova/entities/Entity.java b/src/main/java/de/thm/arsnova/entities/Entity.java
new file mode 100644
index 000000000..23c3103ae
--- /dev/null
+++ b/src/main/java/de/thm/arsnova/entities/Entity.java
@@ -0,0 +1,31 @@
+/*
+ * This file is part of ARSnova Backend.
+ * Copyright (C) 2012-2017 The ARSnova Team
+ *
+ * ARSnova Backend is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * ARSnova Backend is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package de.thm.arsnova.entities;
+
+import com.fasterxml.jackson.annotation.JsonView;
+import de.thm.arsnova.entities.serialization.View;
+
+public interface Entity {
+	String getId();
+	void setId(String id);
+
+	@JsonView(View.Persistence.class)
+	default String getType() {
+		return getClass().getSimpleName();
+	}
+}
diff --git a/src/main/java/de/thm/arsnova/entities/serialization/CouchDbDocumentMixIn.java b/src/main/java/de/thm/arsnova/entities/serialization/CouchDbDocumentMixIn.java
new file mode 100644
index 000000000..84a77f049
--- /dev/null
+++ b/src/main/java/de/thm/arsnova/entities/serialization/CouchDbDocumentMixIn.java
@@ -0,0 +1,29 @@
+/*
+ * This file is part of ARSnova Backend.
+ * Copyright (C) 2012-2017 The ARSnova Team
+ *
+ * ARSnova Backend is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * ARSnova Backend is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package de.thm.arsnova.entities.serialization;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+@JsonIgnoreProperties(value = {"type"}, allowGetters = true)
+public abstract class CouchDbDocumentMixIn {
+	@JsonProperty("_id") abstract String getId();
+	@JsonProperty("_id") abstract void setId(String id);
+	@JsonProperty("_rev") abstract String getRevision();
+	@JsonProperty("_rev") abstract String setRevision(String rev);
+}
diff --git a/src/main/java/de/thm/arsnova/entities/serialization/CouchDbDocumentModule.java b/src/main/java/de/thm/arsnova/entities/serialization/CouchDbDocumentModule.java
new file mode 100644
index 000000000..1579458a2
--- /dev/null
+++ b/src/main/java/de/thm/arsnova/entities/serialization/CouchDbDocumentModule.java
@@ -0,0 +1,32 @@
+/*
+ * This file is part of ARSnova Backend.
+ * Copyright (C) 2012-2017 The ARSnova Team
+ *
+ * ARSnova Backend is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * ARSnova Backend is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package de.thm.arsnova.entities.serialization;
+
+import com.fasterxml.jackson.databind.module.SimpleModule;
+import de.thm.arsnova.entities.Entity;
+
+public class CouchDbDocumentModule extends SimpleModule {
+	public CouchDbDocumentModule() {
+		super("CouchDbDocumentModule");
+	}
+
+	@Override
+	public void setupModule(SetupContext context) {
+		context.setMixInAnnotations(Entity.class, CouchDbDocumentMixIn.class);
+	}
+}
diff --git a/src/main/java/de/thm/arsnova/entities/serialization/CouchDbObjectMapperFactory.java b/src/main/java/de/thm/arsnova/entities/serialization/CouchDbObjectMapperFactory.java
new file mode 100644
index 000000000..1074cc816
--- /dev/null
+++ b/src/main/java/de/thm/arsnova/entities/serialization/CouchDbObjectMapperFactory.java
@@ -0,0 +1,31 @@
+/*
+ * This file is part of ARSnova Backend.
+ * Copyright (C) 2012-2017 The ARSnova Team
+ *
+ * ARSnova Backend is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * ARSnova Backend is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package de.thm.arsnova.entities.serialization;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.ektorp.CouchDbConnector;
+import org.ektorp.impl.StdObjectMapperFactory;
+
+public class CouchDbObjectMapperFactory extends StdObjectMapperFactory {
+	public ObjectMapper createObjectMapper(CouchDbConnector connector) {
+		ObjectMapper om = super.createObjectMapper(connector);
+		om.registerModule(new CouchDbDocumentModule());
+
+		return om;
+	}
+}
diff --git a/src/main/java/de/thm/arsnova/entities/serialization/View.java b/src/main/java/de/thm/arsnova/entities/serialization/View.java
new file mode 100644
index 000000000..b2bcb7a60
--- /dev/null
+++ b/src/main/java/de/thm/arsnova/entities/serialization/View.java
@@ -0,0 +1,23 @@
+/*
+ * This file is part of ARSnova Backend.
+ * Copyright (C) 2012-2017 The ARSnova Team
+ *
+ * ARSnova Backend is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * ARSnova Backend is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package de.thm.arsnova.entities.serialization;
+
+public class View {
+	public interface Public {}
+	public interface Persistence {}
+}
-- 
GitLab