From 9bda11fc55deaef71a3db94ffdec23adce3f0709 Mon Sep 17 00:00:00 2001 From: "liana.bakradze" Date: Thu, 28 Jul 2016 16:09:30 +0300 Subject: [PATCH] EDU-662 2.04 course not recognized as a course in 3.0 preview --- .../edu/coursecreator/CCProjectComponent.java | 30 +++++++++------- .../edu/coursecreator/CCProjectService.java | 12 +++---- .../edu/learning/StudySerializationUtils.java | 35 +++++++++++++++---- 3 files changed, 52 insertions(+), 25 deletions(-) diff --git a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCProjectComponent.java b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCProjectComponent.java index de9b472fd0ce..45f89c83db2b 100644 --- a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCProjectComponent.java +++ b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCProjectComponent.java @@ -60,17 +60,23 @@ public class CCProjectComponent extends AbstractProjectComponent { for (VirtualFile answerFile : files) { ApplicationManager.getApplication().runWriteAction(() -> { String answerName = answerFile.getName(); - String name = FileUtil.getNameWithoutExtension(FileUtil.getNameWithoutExtension(answerName)) + "." + FileUtilRt.getExtension(answerName); - VirtualFile file = answerFile.getParent().findChild(name); - try { - if (file != null) { - file.delete(CCProjectComponent.class); - } - answerFile.rename(CCProjectComponent.class, name); - } - catch (IOException e) { - LOG.error(e); - } + String nameWithoutExtension = FileUtil.getNameWithoutExtension(answerName); + String name = FileUtil.getNameWithoutExtension(nameWithoutExtension) + "." + FileUtilRt.getExtension(answerName); + VirtualFile parent = answerFile.getParent(); + VirtualFile file = parent.findChild(name); + try { + if (file != null) { + file.delete(CCProjectComponent.class); + } + VirtualFile windowsDescrFile = parent.findChild(FileUtil.getNameWithoutExtension(name) + EduNames.WINDOWS_POSTFIX); + if (windowsDescrFile != null) { + windowsDescrFile.delete(CCProjectComponent.class); + } + answerFile.rename(CCProjectComponent.class, name); + } + catch (IOException e) { + LOG.error(e); + } }); } } @@ -88,7 +94,7 @@ public class CCProjectComponent extends AbstractProjectComponent { if (taskFile == null) { taskFile = LocalFileSystem.getInstance().findFileByPath(FileUtil.join(taskPath, EduNames.SRC, answerName)); } - if (taskFile!= null) { + if (taskFile != null) { result.add(taskFile); } } diff --git a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCProjectService.java b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCProjectService.java index 07d92c2c9ef1..43691ae56c35 100644 --- a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCProjectService.java +++ b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCProjectService.java @@ -73,11 +73,11 @@ public class CCProjectService implements PersistentStateComponent { public void loadState(Element state) { try { Element courseElement = getChildWithName(state, COURSE).getChild(COURSE_TITLED); - for (Element lesson : getChildList(courseElement, LESSONS)) { + for (Element lesson : getChildList(courseElement, LESSONS, true)) { int lessonIndex = getAsInt(lesson, INDEX); - for (Element task : getChildList(lesson, TASK_LIST)) { + for (Element task : getChildList(lesson, TASK_LIST, true)) { int taskIndex = getAsInt(task, INDEX); - Map taskFiles = getChildMap(task, TASK_FILES); + Map taskFiles = getChildMap(task, TASK_FILES, true); for (Map.Entry entry : taskFiles.entrySet()) { Element taskFileElement = entry.getValue(); String name = entry.getKey(); @@ -86,10 +86,10 @@ public class CCProjectService implements PersistentStateComponent { if (document == null) { continue; } - for (Element placeholder : getChildList(taskFileElement, ANSWER_PLACEHOLDERS)) { - Element lineElement = getChildWithName(placeholder, LINE); + for (Element placeholder : getChildList(taskFileElement, ANSWER_PLACEHOLDERS, true)) { + Element lineElement = getChildWithName(placeholder, LINE, true); int line = lineElement != null ? Integer.valueOf(lineElement.getAttributeValue(VALUE)) : 0; - Element startElement = getChildWithName(placeholder, START); + Element startElement = getChildWithName(placeholder, START, true); int start = startElement != null ? Integer.valueOf(startElement.getAttributeValue(VALUE)) : 0; int offset = document.getLineStartOffset(line) + start; addChildWithName(placeholder, OFFSET, offset); 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 4424fc76744a..43c84a1d7b10 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 @@ -48,7 +48,8 @@ public class StudySerializationUtils { private StudySerializationUtils() { } - public static class StudyUnrecognizedFormatException extends Exception {} + public static class StudyUnrecognizedFormatException extends Exception { + } public static class Xml { public final static String COURSE_ELEMENT = "courseElement"; @@ -139,7 +140,6 @@ public class StudySerializationUtils { addChildWithName(initialState, MY_LENGTH, getChildWithName(placeholder, MY_INITIAL_LENGTH).getAttributeValue(VALUE)); } } - } } element.removeContent(); @@ -233,7 +233,11 @@ public class StudySerializationUtils { } public static void addHints(@NotNull Element placeholder) throws StudyUnrecognizedFormatException { - final String hint = getChildWithName(placeholder, HINT).getAttribute(VALUE).getValue(); + Element element = getChildWithName(placeholder, HINT, true); + if (element == null) { + return; + } + final String hint = element.getAttribute(VALUE).getValue(); Element listElement = new Element(LIST); final Element hintElement = new Element(OPTION); hintElement.setAttribute(VALUE, hint); @@ -284,7 +288,11 @@ public class StudySerializationUtils { } public static List getChildList(Element parent, String name) throws StudyUnrecognizedFormatException { - Element listParent = getChildWithName(parent, name); + return getChildList(parent, name, false); + } + + public static List getChildList(Element parent, String name, boolean optional) throws StudyUnrecognizedFormatException { + Element listParent = getChildWithName(parent, name, optional); if (listParent != null) { Element list = listParent.getChild(LIST); if (list != null) { @@ -295,6 +303,10 @@ public class StudySerializationUtils { } public static Element getChildWithName(Element parent, String name) throws StudyUnrecognizedFormatException { + return getChildWithName(parent, name, false); + } + + public static Element getChildWithName(Element parent, String name, boolean optional) throws StudyUnrecognizedFormatException { for (Element child : parent.getChildren()) { Attribute attribute = child.getAttribute(NAME); if (attribute == null) { @@ -304,11 +316,18 @@ public class StudySerializationUtils { return child; } } + if (optional) { + return null; + } throw new StudyUnrecognizedFormatException(); } public static Map getChildMap(Element element, String name) throws StudyUnrecognizedFormatException { - Element mapParent = getChildWithName(element, name); + return getChildMap(element, name, false); + } + + public static Map getChildMap(Element element, String name, boolean optional) throws StudyUnrecognizedFormatException { + Element mapParent = getChildWithName(element, name, optional); if (mapParent != null) { Element map = mapParent.getChild(MAP); if (map != null) { @@ -393,7 +412,8 @@ public class StudySerializationUtils { int start = placeholderObject.getAsJsonPrimitive(START).getAsInt(); if (line == -1) { placeholderObject.addProperty(OFFSET, start); - } else { + } + else { Document document = EditorFactory.getInstance().createDocument(taskFileObject.getAsJsonPrimitive(TEXT).getAsString()); placeholderObject.addProperty(OFFSET, document.getLineStartOffset(line) + start); } @@ -401,7 +421,8 @@ public class StudySerializationUtils { final JsonArray hintsArray = new JsonArray(); try { - final Type listType = new TypeToken>() {}.getType(); + final Type listType = new TypeToken>() { + }.getType(); final List hints = gson.fromJson(hintString, listType); for (String hint : hints) { hintsArray.add(hint);