separate course-creator stepic connector from student one
[idea/community.git] / python / educational-core / course-creator / src / com / jetbrains / edu / coursecreator / actions / CCGetCourseFromStepic.java
1 package com.jetbrains.edu.coursecreator.actions;
2
3 import com.intellij.ide.IdeView;
4 import com.intellij.ide.projectView.ProjectView;
5 import com.intellij.openapi.actionSystem.AnActionEvent;
6 import com.intellij.openapi.actionSystem.CommonDataKeys;
7 import com.intellij.openapi.actionSystem.LangDataKeys;
8 import com.intellij.openapi.application.ApplicationManager;
9 import com.intellij.openapi.application.ModalityState;
10 import com.intellij.openapi.progress.ProgressIndicator;
11 import com.intellij.openapi.progress.ProgressManager;
12 import com.intellij.openapi.project.DumbAwareAction;
13 import com.intellij.openapi.project.Project;
14 import com.intellij.openapi.ui.Messages;
15 import com.intellij.openapi.util.text.StringUtil;
16 import com.intellij.openapi.vfs.VirtualFile;
17 import com.intellij.openapi.vfs.VirtualFileManager;
18 import com.jetbrains.edu.coursecreator.CCUtils;
19 import com.jetbrains.edu.learning.StudyTaskManager;
20 import com.jetbrains.edu.learning.StudyUtils;
21 import com.jetbrains.edu.learning.core.EduNames;
22 import com.jetbrains.edu.learning.courseFormat.Course;
23 import com.jetbrains.edu.learning.courseFormat.Lesson;
24 import com.jetbrains.edu.learning.courseFormat.Task;
25 import com.jetbrains.edu.learning.courseFormat.TaskFile;
26 import com.jetbrains.edu.learning.courseGeneration.StudyGenerator;
27 import com.jetbrains.edu.learning.stepic.CCStepicConnector;
28 import com.jetbrains.edu.learning.stepic.CourseInfo;
29 import com.jetbrains.edu.learning.stepic.EduStepicConnector;
30 import org.jetbrains.annotations.NotNull;
31
32 import java.io.File;
33 import java.util.Map;
34
35 import static com.jetbrains.edu.coursecreator.actions.CCFromCourseArchive.createAnswerFile;
36 import static com.jetbrains.edu.learning.courseGeneration.StudyProjectGenerator.OUR_COURSES_DIR;
37 import static com.jetbrains.edu.learning.courseGeneration.StudyProjectGenerator.flushCourse;
38
39 public class CCGetCourseFromStepic extends DumbAwareAction {
40
41   public CCGetCourseFromStepic() {
42     super("Get Course From Stepik", "Get Course From Stepik", null);
43   }
44
45   @Override
46   public void actionPerformed(@NotNull AnActionEvent e) {
47     final IdeView view = e.getData(LangDataKeys.IDE_VIEW);
48     final Project project = e.getData(CommonDataKeys.PROJECT);
49     if (view == null || project == null) {
50       return;
51     }
52     final String courseId = Messages.showInputDialog("Please, enter course id", "Get Course From Stepik", null);
53     if (StringUtil.isNotEmpty(courseId)) {
54       ProgressManager.getInstance().run(new com.intellij.openapi.progress.Task.Modal(project, "Creating Course", true) {
55         @Override
56         public void run(@NotNull final ProgressIndicator indicator) {
57           createCourse(project, courseId);
58         }
59       });
60     }
61   }
62
63   private static void createCourse(Project project, String courseId) {
64     final VirtualFile baseDir = project.getBaseDir();
65     final CourseInfo info = CCStepicConnector.getCourseInfo(project, courseId);
66     if (info == null) return;
67
68     final Course course = EduStepicConnector.getCourse(project, info);
69     if (course != null) {
70       flushCourse(project, course);
71
72       final File courseDirectory = StudyUtils.getCourseDirectory(project, course);
73       ApplicationManager.getApplication().invokeAndWait(() -> ApplicationManager.getApplication().runWriteAction(() -> {
74         final VirtualFile[] children = baseDir.getChildren();
75         for (VirtualFile child : children) {
76           StudyUtils.deleteFile(child);
77         }
78         StudyGenerator.createCourse(course, baseDir, courseDirectory, project);
79       }), ModalityState.current());
80
81
82       StudyTaskManager.getInstance(project).setCourse(course);
83       File courseDir = new File(OUR_COURSES_DIR, course.getName() + "-" + project.getName());
84       course.setCourseDirectory(courseDir.getPath());
85       course.setCourseMode(CCUtils.COURSE_MODE);
86       project.getBaseDir().refresh(false, true);
87       int index = 1;
88       int taskIndex = 1;
89       for (Lesson lesson : course.getLessons()) {
90         final VirtualFile lessonDir = project.getBaseDir().findChild(EduNames.LESSON + String.valueOf(index));
91         lesson.setIndex(index);
92         if (lessonDir == null) continue;
93         for (Task task : lesson.getTaskList()) {
94           final VirtualFile taskDir = lessonDir.findChild(EduNames.TASK + String.valueOf(taskIndex));
95           task.setIndex(taskIndex);
96           task.setLesson(lesson);
97           if (taskDir == null) continue;
98           for (final Map.Entry<String, TaskFile> entry : task.getTaskFiles().entrySet()) {
99             ApplicationManager.getApplication()
100               .invokeAndWait(() -> ApplicationManager.getApplication().runWriteAction(() -> createAnswerFile(project, taskDir, entry)),
101                              ModalityState.current());
102           }
103           taskIndex += 1;
104         }
105         index += 1;
106         taskIndex = 1;
107       }
108       course.initCourse(true);
109       ApplicationManager.getApplication()
110         .invokeAndWait(() -> StudyUtils.registerStudyToolWindow(course, project), ModalityState.current());
111     }
112     VirtualFileManager.getInstance().refreshWithoutFileWatcher(true);
113     ProjectView.getInstance(project).refresh();
114   }
115 }