EDU-662 2.04 course not recognized as a course in 3.0 preview

This commit is contained in:
liana.bakradze
2016-07-28 16:09:30 +03:00
parent 63a93e68ed
commit 9bda11fc55
3 changed files with 52 additions and 25 deletions
@@ -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);
}
}
@@ -73,11 +73,11 @@ public class CCProjectService implements PersistentStateComponent<Element> {
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<String, Element> taskFiles = getChildMap(task, TASK_FILES);
Map<String, Element> taskFiles = getChildMap(task, TASK_FILES, true);
for (Map.Entry<String, Element> entry : taskFiles.entrySet()) {
Element taskFileElement = entry.getValue();
String name = entry.getKey();
@@ -86,10 +86,10 @@ public class CCProjectService implements PersistentStateComponent<Element> {
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);
@@ -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<Element> getChildList(Element parent, String name) throws StudyUnrecognizedFormatException {
Element listParent = getChildWithName(parent, name);
return getChildList(parent, name, false);
}
public static List<Element> 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 <K, V> Map<K, V> getChildMap(Element element, String name) throws StudyUnrecognizedFormatException {
Element mapParent = getChildWithName(element, name);
return getChildMap(element, name, false);
}
public static <K, V> Map<K, V> 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<List<String>>() {}.getType();
final Type listType = new TypeToken<List<String>>() {
}.getType();
final List<String> hints = gson.fromJson(hintString, listType);
for (String hint : hints) {
hintsArray.add(hint);