From ef6a45017bd3284f78c7d1292dfb3f156661b7fd Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Mon, 20 Mar 2017 19:16:27 +0300 Subject: [PATCH] added migration to the new version with tasks --- .../edu/learning/StudySerializationUtils.java | 46 +++++ .../edu/learning/StudyTaskManager.java | 12 +- .../student/testData/migration/adaptive4.xml | 175 ++++++++++++++++++ .../student/testData/migration/adaptive5.xml | 154 +++++++++++++++ .../student/testData/migration/subtasks4.xml | 158 ++++++++++++++++ .../student/testData/migration/subtasks5.xml | 145 +++++++++++++++ .../edu/learning/StudyMigrationTest.java | 12 ++ 7 files changed, 697 insertions(+), 5 deletions(-) create mode 100644 python/educational-core/student/testData/migration/adaptive4.xml create mode 100644 python/educational-core/student/testData/migration/adaptive5.xml create mode 100644 python/educational-core/student/testData/migration/subtasks4.xml create mode 100644 python/educational-core/student/testData/migration/subtasks5.xml diff --git a/python/educational-core/student/src/com/jetbrains/edu/learning/StudySerializationUtils.java b/python/educational-core/student/src/com/jetbrains/edu/learning/StudySerializationUtils.java index 0fa92a103da5..3f528eb32d84 100644 --- a/python/educational-core/student/src/com/jetbrains/edu/learning/StudySerializationUtils.java +++ b/python/educational-core/student/src/com/jetbrains/edu/learning/StudySerializationUtils.java @@ -20,6 +20,7 @@ import com.jetbrains.edu.learning.courseFormat.tasks.*; import com.jetbrains.edu.learning.stepic.EduStepicConnector; import com.jetbrains.edu.learning.stepic.StepicWrappers; import org.jdom.Attribute; +import org.jdom.Content; import org.jdom.Element; import org.jdom.output.XMLOutputter; import org.jetbrains.annotations.NotNull; @@ -95,6 +96,14 @@ public class StudySerializationUtils { public static final String SELECTED = "selected"; public static final String TASK_TEXT = "taskText"; public static final String PLACEHOLDER_TEXT = "placeholderText"; + private static String LAST_SUBTASK_INDEX = "lastSubtaskIndex"; + private static String THEORY_TAG = "theoryTask"; + private static String ADAPTIVE_TASK_PARAMETERS = "adaptiveTaskParameters"; + private static String ADAPTIVE = "adaptive"; + private static String TASK_WITH_SUBTASKS = "TaskWithSubtasks"; + private static String THEORY_TASK = "TheoryTask"; + private static String CHOICE_TASK = "ChoiceTask"; + private static String CODE_TASK = "CodeTask"; private Xml() { } @@ -246,6 +255,43 @@ public class StudySerializationUtils { return state; } + public static Element convertToFifthVersion(Element state) throws StudyUnrecognizedFormatException { + Element taskManagerElement = state.getChild(MAIN_ELEMENT); + Element courseElement = getChildWithName(taskManagerElement, COURSE).getChild(COURSE_TITLED); + final Element adaptive = getChildWithName(courseElement, ADAPTIVE); + for (Element lesson : getChildList(courseElement, LESSONS)) { + for (Element task : getChildList(lesson, TASK_LIST)) { + final Element lastSubtaskIndex = getChildWithName(task, LAST_SUBTASK_INDEX); + final Element theoryTask = getChildWithName(task, THEORY_TAG); + final Element adaptiveParams = getChildWithName(task, ADAPTIVE_TASK_PARAMETERS); + final boolean hasAdaptiveParams = !adaptiveParams.getChildren().isEmpty(); + if (Integer.valueOf(lastSubtaskIndex.getAttributeValue(VALUE)) != 0) { + task.setName(TASK_WITH_SUBTASKS); + } + else if (Boolean.valueOf(theoryTask.getAttributeValue(VALUE))) { + task.setName(THEORY_TASK); + } + else if (hasAdaptiveParams) { + task.setName(CHOICE_TASK); + final Element adaptiveParameters = adaptiveParams.getChildren().get(0); + for (Element element : adaptiveParameters.getChildren()) { + final Attribute name = element.getAttribute(NAME); + if (name != null && !THEORY_TAG.equals(name.getValue())) { + final Content elementCopy = element.clone(); + task.addContent(elementCopy); + } + } + } + else if (Boolean.valueOf(adaptive.getAttributeValue(VALUE))) { + task.setName(CODE_TASK); + } + task.removeContent(adaptiveParams); + task.removeContent(theoryTask); + } + } + return state; + } + public static String addStatus(XMLOutputter outputter, Map placeholderTextToStatus, String taskStatus, diff --git a/python/educational-core/student/src/com/jetbrains/edu/learning/StudyTaskManager.java b/python/educational-core/student/src/com/jetbrains/edu/learning/StudyTaskManager.java index b5278c696ba7..9d74279928a7 100644 --- a/python/educational-core/student/src/com/jetbrains/edu/learning/StudyTaskManager.java +++ b/python/educational-core/student/src/com/jetbrains/edu/learning/StudyTaskManager.java @@ -34,9 +34,9 @@ import java.util.Map; @State(name = "StudySettings", storages = @Storage("study_project.xml")) public class StudyTaskManager implements PersistentStateComponent, DumbAware { private static final Logger LOG = Logger.getInstance(StudyTaskManager.class); - public static final int CURRENT_VERSION = 4; + public static final int CURRENT_VERSION = 5; private Course myCourse; - public int VERSION = 4; + public int VERSION = 5; public Map> myUserTests = new HashMap<>(); public List myInvisibleFiles = new ArrayList<>(); @@ -140,9 +140,11 @@ public class StudyTaskManager implements PersistentStateComponent, Dumb state = StudySerializationUtils.Xml.convertToThirdVersion(state, myProject); case 3: state = StudySerializationUtils.Xml.convertToForthVersion(state); - //uncomment for future versions - //case 4: - //state = StudySerializationUtils.Xml.convertToForthVersion(state, myProject); + case 4: + state = StudySerializationUtils.Xml.convertToFifthVersion(state); + //uncomment for future versions + //case 5: + // state = StudySerializationUtils.Xml.convertToSixthVersion(state, myProject); } XmlSerializer.deserializeInto(this, state.getChild(StudySerializationUtils.Xml.MAIN_ELEMENT)); VERSION = CURRENT_VERSION; diff --git a/python/educational-core/student/testData/migration/adaptive4.xml b/python/educational-core/student/testData/migration/adaptive4.xml new file mode 100644 index 000000000000..e18194f223fc --- /dev/null +++ b/python/educational-core/student/testData/migration/adaptive4.xml @@ -0,0 +1,175 @@ + + + + + + + diff --git a/python/educational-core/student/testData/migration/adaptive5.xml b/python/educational-core/student/testData/migration/adaptive5.xml new file mode 100644 index 000000000000..cdb97a37db83 --- /dev/null +++ b/python/educational-core/student/testData/migration/adaptive5.xml @@ -0,0 +1,154 @@ + + + + + + + \ No newline at end of file diff --git a/python/educational-core/student/testData/migration/subtasks4.xml b/python/educational-core/student/testData/migration/subtasks4.xml new file mode 100644 index 000000000000..b8a4ca5b482d --- /dev/null +++ b/python/educational-core/student/testData/migration/subtasks4.xml @@ -0,0 +1,158 @@ + + + + + + + \ No newline at end of file diff --git a/python/educational-core/student/testData/migration/subtasks5.xml b/python/educational-core/student/testData/migration/subtasks5.xml new file mode 100644 index 000000000000..e11207ce1d0c --- /dev/null +++ b/python/educational-core/student/testData/migration/subtasks5.xml @@ -0,0 +1,145 @@ + + + + + + + \ No newline at end of file diff --git a/python/educational-core/student/testSrc/com/jetbrains/edu/learning/StudyMigrationTest.java b/python/educational-core/student/testSrc/com/jetbrains/edu/learning/StudyMigrationTest.java index 34f7846b804d..cac4b36e44ec 100644 --- a/python/educational-core/student/testSrc/com/jetbrains/edu/learning/StudyMigrationTest.java +++ b/python/educational-core/student/testSrc/com/jetbrains/edu/learning/StudyMigrationTest.java @@ -20,6 +20,18 @@ public class StudyMigrationTest { assertThat(StudySerializationUtils.Xml.convertToForthVersion(element)).isEqualTo(getTestDataPath().resolve("4.xml")); } + @Test + public void testAdaptive45() throws JDOMException, IOException, StudySerializationUtils.StudyUnrecognizedFormatException { + Element element = JdomKt.loadElement(getTestDataPath().resolve("adaptive4.xml")); + assertThat(StudySerializationUtils.Xml.convertToFifthVersion(element)).isEqualTo(getTestDataPath().resolve("adaptive5.xml")); + } + + @Test + public void testSubtasks45() throws JDOMException, IOException, StudySerializationUtils.StudyUnrecognizedFormatException { + Element element = JdomKt.loadElement(getTestDataPath().resolve("subtasks4.xml")); + assertThat(StudySerializationUtils.Xml.convertToFifthVersion(element)).isEqualTo(getTestDataPath().resolve("subtasks5.xml")); + } + @NotNull protected Path getTestDataPath() { return Paths.get(PlatformTestUtil.getCommunityPath(), "python/educational-core/student/testData/migration");