Use RemoteCourse class

This commit is contained in:
meanmail
2017-04-07 02:54:27 +07:00
parent 8e6b8a1a95
commit 452860a697
2 changed files with 51 additions and 34 deletions
@@ -46,9 +46,9 @@ public class StudyTaskManager implements PersistentStateComponent<Element>, Dumb
private static final Logger LOG = Logger.getInstance(StudyTaskManager.class);
public static final int CURRENT_VERSION = 5;
private Course myCourse;
public int VERSION = 5;
public int VERSION = CURRENT_VERSION;
public Map<Task, List<UserTest>> myUserTests = new HashMap<>();
public final Map<Task, List<UserTest>> myUserTests = new HashMap<>();
public boolean myShouldUseJavaFx = StudyUtils.hasJavaFx();
private StudyToolWindow.StudyToolWindowMode myToolWindowMode = StudyToolWindow.StudyToolWindowMode.TEXT;
@@ -191,14 +191,18 @@ public class StudyTaskManager implements PersistentStateComponent<Element>, Dumb
private void updateTestHelper() {
StartupManager.getInstance(myProject).runWhenProjectIsInitialized(() -> ApplicationManager.getApplication().runWriteAction(() -> {
final VirtualFile testHelper = myProject.getBaseDir().findChild(EduNames.TEST_HELPER);
final VirtualFile baseDir = myProject.getBaseDir();
if (baseDir == null) {
return;
}
final VirtualFile testHelper = baseDir.findChild(EduNames.TEST_HELPER);
if (testHelper != null) {
StudyUtils.deleteFile(testHelper);
}
final FileTemplate template =
FileTemplateManager.getInstance(myProject).getInternalTemplate(FileUtil.getNameWithoutExtension(EduNames.TEST_HELPER));
try {
final PsiDirectory projectDir = PsiManager.getInstance(myProject).findDirectory(myProject.getBaseDir());
final PsiDirectory projectDir = PsiManager.getInstance(myProject).findDirectory(baseDir);
if (projectDir != null) {
FileTemplateUtil.createFromTemplate(template, EduNames.TEST_HELPER, null, projectDir);
}
@@ -211,8 +215,9 @@ public class StudyTaskManager implements PersistentStateComponent<Element>, Dumb
private void deserialize(Element state) throws StudySerializationUtils.StudyUnrecognizedFormatException {
final Element taskManagerElement = state.getChild(StudySerializationUtils.Xml.MAIN_ELEMENT);
if (taskManagerElement == null)
if (taskManagerElement == null) {
throw new StudySerializationUtils.StudyUnrecognizedFormatException();
}
XmlSerializer.deserializeInto(this, taskManagerElement);
final Element xmlCourse = StudySerializationUtils.Xml.getChildWithName(taskManagerElement, StudySerializationUtils.COURSE);
final Element remoteCourseElement = xmlCourse.getChild(REMOTE_COURSE);
@@ -250,5 +255,4 @@ public class StudyTaskManager implements PersistentStateComponent<Element>, Dumb
public void setTurnEditingMode(boolean turnEditingMode) {
myTurnEditingMode = turnEditingMode;
}
}
@@ -26,6 +26,7 @@ import com.intellij.openapi.project.ProjectManager;
import com.intellij.platform.ProjectSetReader;
import com.jetbrains.edu.learning.StudyTaskManager;
import com.jetbrains.edu.learning.courseFormat.Course;
import com.jetbrains.edu.learning.courseFormat.RemoteCourse;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpMethod;
@@ -89,8 +90,8 @@ public class StepikRestService extends RestService {
String path = urlDecoder.path();
Matcher matcher = OPEN_COURSE.matcher(path);
if (matcher.matches()) {
int course_id = Integer.parseInt(matcher.group(1));
LOG.info("Open course: " + course_id);
int targetCourseId = Integer.parseInt(matcher.group(1));
LOG.info("Open course: " + targetCourseId);
RecentProjectsManagerBase recentProjectsManager = (RecentProjectsManagerBase)RecentProjectsManager.getInstance();
@@ -106,38 +107,21 @@ public class StepikRestService extends RestService {
List<String> recentPaths = state.recentPaths;
LOG.info(recentPaths.toString());
Project project = ProjectManager.getInstance().getDefaultProject();
StudyTaskManager taskManager = new StudyTaskManager(project);
SAXBuilder parser = new SAXBuilder();
Document xmlDoc;
for (String projectPath : recentPaths) {
try {
String studyProjectXML = projectPath + STUDY_PROJECT_XML_PATH;
xmlDoc = parser.build(new File(studyProjectXML));
Element root = xmlDoc.getRootElement();
Element component = root.getChild("component");
if (component == null) {
continue;
}
taskManager.loadState(component);
Course course = taskManager.getCourse();
if (course == null) {
continue;
}
if (course.getId() == course_id) {
openProject(projectPath);
RestService.sendOk(request, context);
return null;
}
Element component = readComponent(parser, projectPath);
if (component == null) {
continue;
}
catch (JDOMException | IllegalStateException | IOException ignored) {
int courseId = getCourseId(taskManager, component);
if (courseId == targetCourseId) {
openProject(projectPath);
RestService.sendOk(request, context);
return null;
}
}
}
@@ -145,4 +129,33 @@ public class StepikRestService extends RestService {
RestService.sendStatus(HttpResponseStatus.NOT_FOUND, false, context.channel());
return "Unknown command";
}
@Nullable
private static Element readComponent(@NotNull SAXBuilder parser, @NotNull String projectPath) {
Element component = null;
try {
String studyProjectXML = projectPath + STUDY_PROJECT_XML_PATH;
Document xmlDoc = parser.build(new File(studyProjectXML));
Element root = xmlDoc.getRootElement();
component = root.getChild("component");
}
catch (JDOMException | IOException ignored) {
}
return component;
}
private static int getCourseId(@NotNull StudyTaskManager taskManager, @NotNull Element component) {
try {
taskManager.loadState(component);
Course course = taskManager.getCourse();
if ((course instanceof RemoteCourse)) {
return ((RemoteCourse)course).getId();
}
}
catch (IllegalStateException ignored) {
}
return 0;
}
}