move course to another service for old projects
authorliana.bakradze <liana.bakradze@jetbrains.com>
Thu, 21 Apr 2016 11:31:02 +0000 (14:31 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Thu, 21 Apr 2016 11:31:02 +0000 (14:31 +0300)
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCProjectComponent.java

index 2b91b9509dc288009c80607bf8d9e4eecbc21d2f..b5a23ba917ed33b3a2e9835e9199f1a5a4e164f4 100644 (file)
@@ -1,17 +1,40 @@
 package com.jetbrains.edu.coursecreator;
 
-import com.intellij.openapi.components.ProjectComponent;
+import com.intellij.openapi.application.PathManager;
+import com.intellij.openapi.components.AbstractProjectComponent;
+import com.intellij.openapi.project.Project;
 import com.intellij.openapi.vfs.VirtualFileManager;
+import com.jetbrains.edu.learning.StudyProjectComponent;
+import com.jetbrains.edu.learning.StudyTaskManager;
+import com.jetbrains.edu.learning.courseFormat.Course;
 import org.jetbrains.annotations.NotNull;
 
-public class CCProjectComponent implements ProjectComponent {
-  private final  CCVirtualFileListener myTaskFileLifeListener = new CCVirtualFileListener();
+import java.io.File;
+
+public class CCProjectComponent extends AbstractProjectComponent {
+  private final CCVirtualFileListener myTaskFileLifeListener = new CCVirtualFileListener();
+  private final Project myProject;
+
+  protected CCProjectComponent(Project project) {
+    super(project);
+    myProject = project;
+  }
 
   public void initComponent() {
     VirtualFileManager.getInstance().addVirtualFileListener(myTaskFileLifeListener);
   }
 
-  public void disposeComponent() {
+  public void migrateIfNeeded() {
+    Course studyCourse = StudyTaskManager.getInstance(myProject).getCourse();
+    Course course = CCProjectService.getInstance(myProject).getCourse();
+    if (studyCourse == null && course != null) {
+      course.setCourseMode(CCUtils.COURSE_MODE);
+      File coursesDir = new File(PathManager.getConfigPath(), "courses");
+      File courseDir = new File(coursesDir, course.getName() + "-" + myProject.getName());
+      course.setCourseDirectory(courseDir.getPath());
+      StudyTaskManager.getInstance(myProject).setCourse(course);
+      StudyProjectComponent.getInstance(myProject).registerStudyToolWindow(course);
+    }
   }
 
   @NotNull
@@ -20,6 +43,7 @@ public class CCProjectComponent implements ProjectComponent {
   }
 
   public void projectOpened() {
+    migrateIfNeeded();
     VirtualFileManager.getInstance().addVirtualFileListener(myTaskFileLifeListener);
   }