EDU-277 Implement separate feature usage statistics for PyCharm Educational Edition
[idea/community.git] / python / educational-core / course-creator / src / com / jetbrains / edu / coursecreator / actions / CCCreateCourseArchive.java
1 package com.jetbrains.edu.coursecreator.actions;
2
3 import com.google.gson.Gson;
4 import com.google.gson.GsonBuilder;
5 import com.intellij.ide.projectView.ProjectView;
6 import com.intellij.openapi.actionSystem.AnActionEvent;
7 import com.intellij.openapi.actionSystem.CommonDataKeys;
8 import com.intellij.openapi.actionSystem.LangDataKeys;
9 import com.intellij.openapi.actionSystem.Presentation;
10 import com.intellij.openapi.application.ApplicationManager;
11 import com.intellij.openapi.diagnostic.Logger;
12 import com.intellij.openapi.module.Module;
13 import com.intellij.openapi.project.DumbAwareAction;
14 import com.intellij.openapi.project.Project;
15 import com.intellij.openapi.ui.DialogWrapper;
16 import com.intellij.openapi.ui.Messages;
17 import com.intellij.openapi.util.io.FileUtil;
18 import com.intellij.openapi.vfs.VfsUtil;
19 import com.intellij.openapi.vfs.VirtualFile;
20 import com.intellij.openapi.vfs.VirtualFileManager;
21 import com.intellij.util.containers.HashMap;
22 import com.intellij.util.io.ZipUtil;
23 import com.jetbrains.edu.coursecreator.CCLanguageManager;
24 import com.jetbrains.edu.coursecreator.CCUtils;
25 import com.jetbrains.edu.coursecreator.ui.CreateCourseArchiveDialog;
26 import com.jetbrains.edu.learning.StudyTaskManager;
27 import com.jetbrains.edu.learning.core.EduNames;
28 import com.jetbrains.edu.learning.core.EduUtils;
29 import com.jetbrains.edu.learning.courseFormat.*;
30 import com.jetbrains.edu.learning.statistics.EduUsagesCollector;
31 import org.jetbrains.annotations.NotNull;
32
33 import java.io.*;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.zip.ZipOutputStream;
37
38 public class CCCreateCourseArchive extends DumbAwareAction {
39   private static final Logger LOG = Logger.getInstance(CCCreateCourseArchive.class.getName());
40   private String myZipName;
41   private String myLocationDir;
42
43   public void setZipName(String zipName) {
44     myZipName = zipName;
45   }
46
47   public void setLocationDir(String locationDir) {
48     myLocationDir = locationDir;
49   }
50
51   public CCCreateCourseArchive() {
52     super("Generate Course Archive", "Generate Course Archive", null);
53   }
54
55   @Override
56   public void update(@NotNull AnActionEvent e) {
57     Presentation presentation = e.getPresentation();
58     Project project = e.getProject();
59     presentation.setEnabledAndVisible(project != null && CCUtils.isCourseCreator(project));
60   }
61
62   @Override
63   public void actionPerformed(@NotNull AnActionEvent e) {
64     final Project project = e.getData(CommonDataKeys.PROJECT);
65     final Module module = e.getData(LangDataKeys.MODULE);
66     if (project == null || module == null) {
67       return;
68     }
69     CreateCourseArchiveDialog dlg = new CreateCourseArchiveDialog(project, this);
70     dlg.show();
71     if (dlg.getExitCode() != DialogWrapper.OK_EXIT_CODE) {
72       return;
73     }
74     createCourseArchive(project, module, myZipName, myLocationDir, true);
75     EduUsagesCollector.createdCourseArchive();
76   }
77
78   public static void createCourseArchive(final Project project, Module module, String zipName, String locationDir, boolean showMessage) {
79     final Course course = StudyTaskManager.getInstance(project).getCourse();
80     if (course == null) return;
81     final VirtualFile baseDir = project.getBaseDir();
82     VirtualFile archiveFolder = CCUtils.generateFolder(project, module, zipName);
83     if (archiveFolder == null) {
84       return;
85     }
86
87     CCLanguageManager manager = CCUtils.getStudyLanguageManager(course);
88     if (manager == null) {
89       return;
90     }
91     FileFilter filter = pathname -> !manager.doNotPackFile(pathname);
92
93     for (VirtualFile child : baseDir.getChildren()) {
94       String name = child.getName();
95       File fromFile = new File(child.getPath());
96       if (CCUtils.GENERATED_FILES_FOLDER.equals(name) || ".idea".equals(name)
97           || name.contains("iml") || manager.doNotPackFile(fromFile)) {
98         continue;
99       }
100       copyChild(archiveFolder, filter, child, fromFile);
101     }
102
103     final List<Lesson> lessons = course.getLessons();
104
105     ApplicationManager.getApplication().runWriteAction(new Runnable() {
106       @Override
107       public void run() {
108         final Map<TaskFile, TaskFile> savedTaskFiles = new HashMap<TaskFile, TaskFile>();
109         replaceAnswerFilesWithTaskFiles(savedTaskFiles);
110         generateJson(project, archiveFolder);
111         resetTaskFiles(savedTaskFiles);
112         VirtualFileManager.getInstance().refreshWithoutFileWatcher(false);
113         packCourse(archiveFolder, locationDir, zipName, showMessage);
114         synchronize(project);
115       }
116
117       private void replaceAnswerFilesWithTaskFiles(Map<TaskFile, TaskFile> savedTaskFiles) {
118         for (Lesson lesson : lessons) {
119           final VirtualFile lessonDir = baseDir.findChild(EduNames.LESSON + String.valueOf(lesson.getIndex()));
120           if (lessonDir == null) continue;
121           for (Task task : lesson.getTaskList()) {
122             final VirtualFile taskDir = lessonDir.findChild(EduNames.TASK + String.valueOf(task.getIndex()));
123             if (taskDir == null) continue;
124             for (final Map.Entry<String, TaskFile> entry : task.getTaskFiles().entrySet()) {
125               TaskFile taskFileCopy = new TaskFile();
126               TaskFile taskFile = entry.getValue();
127               TaskFile.copy(taskFile, taskFileCopy);
128               savedTaskFiles.put(taskFile, taskFileCopy);
129
130               VirtualFile userFileDir = VfsUtil.findRelativeFile(archiveFolder, lessonDir.getName(), taskDir.getName());
131               if (userFileDir == null) {
132                 continue;
133               }
134               String taskFileName = entry.getKey();
135               EduUtils.createStudentFileFromAnswer(project, userFileDir, taskDir, taskFileName, taskFile);
136             }
137           }
138         }
139       }
140     });
141
142
143   }
144
145   private static void copyChild(VirtualFile archiveFolder, FileFilter filter, VirtualFile child, File fromFile) {
146     File toFile = new File(archiveFolder.getPath(), child.getName());
147
148     try {
149       if (child.isDirectory()) {
150         FileUtil.copyDir(fromFile, toFile, filter);
151       }
152       else {
153         if (filter.accept(fromFile)) {
154           FileUtil.copy(fromFile, toFile);
155         }
156       }
157     }
158     catch (IOException e) {
159       LOG.info("Failed to copy" + fromFile.getPath(), e);
160     }
161   }
162
163   private static void resetTaskFiles(Map<TaskFile, TaskFile> savedTaskFiles) {
164     for (Map.Entry<TaskFile, TaskFile> entry : savedTaskFiles.entrySet()) {
165       List<AnswerPlaceholder> placeholders = entry.getValue().getAnswerPlaceholders();
166       for (AnswerPlaceholder placeholder : placeholders) {
167         placeholder.setUseLength(false);
168       }
169       entry.getKey().setAnswerPlaceholders(placeholders);
170     }
171   }
172
173   private static void synchronize(@NotNull final Project project) {
174     VirtualFileManager.getInstance().refreshWithoutFileWatcher(true);
175     ProjectView.getInstance(project).refresh();
176   }
177
178   private static void packCourse(@NotNull final VirtualFile baseDir, String locationDir, String zipName, boolean showMessage) {
179     try {
180       final File zipFile = new File(locationDir, zipName + ".zip");
181       ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
182       VirtualFile[] courseFiles = baseDir.getChildren();
183       for (VirtualFile file : courseFiles) {
184         ZipUtil.addFileOrDirRecursively(zos, null, new File(file.getPath()), file.getName(), null, null);
185       }
186       zos.close();
187       if (showMessage) {
188         Messages.showInfoMessage("Course archive was saved to " + zipFile.getPath(), "Course Archive Was Created Successfully");
189       }
190     }
191     catch (IOException e1) {
192       LOG.error(e1);
193     }
194   }
195
196   @SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
197   private static void generateJson(@NotNull final Project project, VirtualFile parentDir) {
198     final Course course = StudyTaskManager.getInstance(project).getCourse();
199     final Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
200     final String json = gson.toJson(course);
201     final File courseJson = new File(parentDir.getPath(), EduNames.COURSE_META_FILE);
202     OutputStreamWriter outputStreamWriter = null;
203     try {
204       outputStreamWriter = new OutputStreamWriter(new FileOutputStream(courseJson), "UTF-8");
205       outputStreamWriter.write(json);
206     }
207     catch (Exception e) {
208       Messages.showErrorDialog(e.getMessage(), "Failed to Generate Json");
209       LOG.info(e);
210     }
211     finally {
212       try {
213         if (outputStreamWriter != null) {
214           outputStreamWriter.close();
215         }
216       }
217       catch (IOException e1) {
218         //close silently
219       }
220     }
221   }
222 }