diff --git a/src/main/java/de/thm/arsnova/config/AppConfig.java b/src/main/java/de/thm/arsnova/config/AppConfig.java index b96dd020aa0c327ca3c60ca09f88e0947c952cbe..75f6caa391fc1aadd119ca8c93cbb37a793272af 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 99% 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 f37fd437c7ac12f7346248e20350b83cdd30d56d..afbc44172a0ca421f062a9e37e7a7c18a6c63a44 100644 --- a/src/main/java/de/thm/arsnova/entities/migration/V2Migrator.java +++ b/src/main/java/de/thm/arsnova/entities/migration/FromV2Migrator.java @@ -19,7 +19,7 @@ import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; -public class V2Migrator { +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 0000000000000000000000000000000000000000..b0ca6c2224a37d38b44162c026b5b8a6fb138ede --- /dev/null +++ b/src/main/java/de/thm/arsnova/entities/migration/ToV2Migrator.java @@ -0,0 +1,124 @@ +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; + +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; + } +}