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