From 41c79ec61d6d66184dd7dfe9a4741e9c16f82cec Mon Sep 17 00:00:00 2001
From: Daniel Gerhardt <code@dgerhardt.net>
Date: Thu, 7 Sep 2017 16:39:20 +0200
Subject: [PATCH] Add reverse migrations (to v2) for entities

---
 .../java/de/thm/arsnova/config/AppConfig.java |  12 +-
 .../{V2Migrator.java => FromV2Migrator.java}  |  24 ++-
 .../entities/migration/ToV2Migrator.java      | 146 ++++++++++++++++++
 3 files changed, 178 insertions(+), 4 deletions(-)
 rename src/main/java/de/thm/arsnova/entities/migration/{V2Migrator.java => FromV2Migrator.java} (86%)
 create mode 100644 src/main/java/de/thm/arsnova/entities/migration/ToV2Migrator.java

diff --git a/src/main/java/de/thm/arsnova/config/AppConfig.java b/src/main/java/de/thm/arsnova/config/AppConfig.java
index 7fba9014a..287c5772c 100644
--- a/src/main/java/de/thm/arsnova/config/AppConfig.java
+++ b/src/main/java/de/thm/arsnova/config/AppConfig.java
@@ -20,7 +20,8 @@ package de.thm.arsnova.config;
 import com.fasterxml.jackson.annotation.JsonInclude;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.SerializationFeature;
-import de.thm.arsnova.entities.migration.V2Migrator;
+import de.thm.arsnova.entities.migration.FromV2Migrator;
+import de.thm.arsnova.entities.migration.ToV2Migrator;
 import de.thm.arsnova.util.ImageUtils;
 import de.thm.arsnova.connector.client.ConnectorClient;
 import de.thm.arsnova.connector.client.ConnectorClientImpl;
@@ -287,7 +288,12 @@ public class AppConfig extends WebMvcConfigurerAdapter {
 	}
 
 	@Bean
-	public V2Migrator v2Migrator() {
-		return new V2Migrator();
+	public FromV2Migrator fromV2Migrator() {
+		return new FromV2Migrator();
+	}
+
+	@Bean
+	public ToV2Migrator toV2Migrator() {
+		return new ToV2Migrator();
 	}
 }
diff --git a/src/main/java/de/thm/arsnova/entities/migration/V2Migrator.java b/src/main/java/de/thm/arsnova/entities/migration/FromV2Migrator.java
similarity index 86%
rename from src/main/java/de/thm/arsnova/entities/migration/V2Migrator.java
rename to src/main/java/de/thm/arsnova/entities/migration/FromV2Migrator.java
index f37fd437c..deb6e3981 100644
--- a/src/main/java/de/thm/arsnova/entities/migration/V2Migrator.java
+++ b/src/main/java/de/thm/arsnova/entities/migration/FromV2Migrator.java
@@ -1,3 +1,20 @@
+/*
+ * This file is part of ARSnova Backend.
+ * Copyright (C) 2012-2018 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.migration;
 
 import de.thm.arsnova.entities.ChoiceAnswer;
@@ -19,7 +36,12 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.stream.Collectors;
 
-public class V2Migrator {
+/**
+ * Converts legacy entities from version 2 to current model version.
+ *
+ * @author Daniel Gerhardt
+ */
+public class FromV2Migrator {
 	private void copyCommonProperties(final Entity from, final Entity to) {
 		to.setId(from.getId());
 		to.setRevision(from.getRevision());
diff --git a/src/main/java/de/thm/arsnova/entities/migration/ToV2Migrator.java b/src/main/java/de/thm/arsnova/entities/migration/ToV2Migrator.java
new file mode 100644
index 000000000..1b3c4097b
--- /dev/null
+++ b/src/main/java/de/thm/arsnova/entities/migration/ToV2Migrator.java
@@ -0,0 +1,146 @@
+/*
+ * This file is part of ARSnova Backend.
+ * Copyright (C) 2012-2018 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.migration;
+
+import de.thm.arsnova.entities.ChoiceAnswer;
+import de.thm.arsnova.entities.ChoiceQuestionContent;
+import de.thm.arsnova.entities.Entity;
+import de.thm.arsnova.entities.TextAnswer;
+import de.thm.arsnova.entities.UserProfile;
+import de.thm.arsnova.entities.migration.v2.Answer;
+import de.thm.arsnova.entities.migration.v2.AnswerOption;
+import de.thm.arsnova.entities.migration.v2.Comment;
+import de.thm.arsnova.entities.migration.v2.Content;
+import de.thm.arsnova.entities.migration.v2.LoggedIn;
+import de.thm.arsnova.entities.migration.v2.MotdList;
+import de.thm.arsnova.entities.migration.v2.Session;
+import de.thm.arsnova.entities.migration.v2.VisitedSession;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Converts entities from current model version to legacy version 2.
+ *
+ * @author Daniel Gerhardt
+ */
+public class ToV2Migrator {
+	private void copyCommonProperties(final Entity from, final Entity to) {
+		to.setId(from.getId());
+		to.setRevision(from.getRevision());
+	}
+
+	public LoggedIn migrateLoggedIn(final UserProfile from) {
+		final LoggedIn to = new LoggedIn();
+		copyCommonProperties(from, to);
+		to.setUser(from.getLoginId());
+		to.setTimestamp(from.getLastLogin());
+		to.setVisitedSessions(from.getSessionHistory().stream()
+				.map(entry -> new VisitedSession())
+				.collect(Collectors.toList()));
+
+		return to;
+	}
+
+	public MotdList migrateMotdList(final UserProfile from) {
+		final MotdList to = new MotdList();
+		copyCommonProperties(from, to);
+		to.setUsername(from.getLoginId());
+		to.setMotdkeys(String.join(",", from.getAcknowledgedMotds()));
+
+		return to;
+	}
+
+	public Session migrate(final de.thm.arsnova.entities.Session from, final UserProfile owner) {
+		final Session to = new Session();
+		copyCommonProperties(from, to);
+		to.setKeyword(from.getShortId());
+		to.setCreator(owner.getLoginId());
+		to.setName(from.getName());
+		to.setShortName(from.getAbbreviation());
+		to.setActive(!from.isClosed());
+
+		return to;
+	}
+
+	public Content migrate(final de.thm.arsnova.entities.Content from) {
+		final Content to = new Content();
+		copyCommonProperties(from, to);
+		to.setSessionId(from.getSessionId());
+		to.setSubject(from.getSubject());
+		to.setText(from.getBody());
+		to.setQuestionType(from.getFormat());
+		to.setQuestionVariant(from.getGroup());
+
+		if (from instanceof ChoiceQuestionContent) {
+			final ChoiceQuestionContent fromChoiceQuestionContent = (ChoiceQuestionContent) from;
+			final List<AnswerOption> toOptions = new ArrayList<>();
+			to.setPossibleAnswers(toOptions);
+			for (int i = 0; i < fromChoiceQuestionContent.getOptions().size(); i++) {
+				AnswerOption option = new AnswerOption();
+				option.setText(fromChoiceQuestionContent.getOptions().get(1).getLabel());
+				option.setValue(fromChoiceQuestionContent.getOptions().get(1).getPoints());
+				option.setCorrect(fromChoiceQuestionContent.getCorrectOptionIndexes().contains(i));
+				toOptions.add(option);
+			}
+		}
+
+		return to;
+	}
+
+	public Answer migrate(final de.thm.arsnova.entities.ChoiceAnswer from, final de.thm.arsnova.entities.ChoiceQuestionContent content, final UserProfile creator) {
+		final Answer to = new Answer();
+		copyCommonProperties(from, to);
+		to.setQuestionId(from.getContentId());
+		to.setUser(creator.getLoginId());
+
+		List<String> answers = new ArrayList<>();
+		for (int i = 0; i < content.getOptions().size(); i++) {
+			answers.add(from.getSelectedChoiceIndexes().contains(i) ? "1" : "0");
+		}
+		to.setAnswerText(answers.stream().collect(Collectors.joining()));
+
+		return to;
+	}
+
+	public Answer migrate(final de.thm.arsnova.entities.TextAnswer from, final de.thm.arsnova.entities.Content content, final UserProfile creator) {
+		final Answer to = new Answer();
+		copyCommonProperties(from, to);
+		to.setQuestionId(from.getContentId());
+		to.setUser(creator.getLoginId());
+
+		to.setAnswerSubject(from.getSubject());
+		to.setAnswerText(from.getBody());
+
+		return to;
+	}
+
+	public Comment migrate(final de.thm.arsnova.entities.Comment from, final UserProfile creator) {
+		final Comment to = new Comment();
+		copyCommonProperties(from, to);
+		to.setSessionId(from.getSessionId());
+		to.setCreator(creator.getLoginId());
+		to.setSubject(from.getSubject());
+		to.setText(from.getBody());
+		to.setTimestamp(from.getTimestamp());
+		to.setRead(from.isRead());
+
+		return to;
+	}
+}
-- 
GitLab