|
1 | 1 | package fi.helsinki.cs.tmc.actions;
|
2 | 2 |
|
| 3 | +import com.google.common.util.concurrent.FutureCallback; |
| 4 | +import com.google.common.util.concurrent.Futures; |
| 5 | +import com.google.common.util.concurrent.ListenableFuture; |
3 | 6 | import hy.tmc.core.domain.Exercise;
|
4 | 7 | import fi.helsinki.cs.tmc.model.CourseDb;
|
5 |
| -import fi.helsinki.cs.tmc.model.ExerciseUpdateOverwritingDecider; |
| 8 | +import fi.helsinki.cs.tmc.model.NBTmcSettings; |
6 | 9 | import fi.helsinki.cs.tmc.model.ProjectMediator;
|
7 | 10 | import fi.helsinki.cs.tmc.model.ServerAccess;
|
| 11 | +import fi.helsinki.cs.tmc.model.TmcCoreSingleton; |
8 | 12 | import fi.helsinki.cs.tmc.model.TmcProjectInfo;
|
9 | 13 | import fi.helsinki.cs.tmc.ui.ConvenientDialogDisplayer;
|
10 |
| -import fi.helsinki.cs.tmc.utilities.AggregatingBgTaskListener; |
11 |
| -import fi.helsinki.cs.tmc.utilities.BgTask; |
12 |
| -import fi.helsinki.cs.tmc.utilities.BgTaskListener; |
13 |
| -import fi.helsinki.cs.tmc.utilities.zip.NbProjectUnzipper; |
| 14 | +import hy.tmc.core.TmcCore; |
| 15 | +import hy.tmc.core.exceptions.TmcCoreException; |
14 | 16 | import java.awt.event.ActionEvent;
|
15 | 17 | import java.awt.event.ActionListener;
|
16 |
| -import java.io.File; |
17 |
| -import java.io.IOException; |
18 | 18 | import java.util.ArrayList;
|
19 |
| -import java.util.Collection; |
20 | 19 | import java.util.List;
|
21 | 20 | import java.util.logging.Logger;
|
| 21 | +import org.openide.util.Exceptions; |
22 | 22 |
|
23 | 23 | public class UpdateExercisesAction implements ActionListener {
|
24 |
| - |
| 24 | + |
25 | 25 | private static final Logger log = Logger.getLogger(UpdateExercisesAction.class.getName());
|
26 | 26 |
|
27 | 27 | private List<Exercise> exercisesToUpdate;
|
28 | 28 | private CourseDb courseDb;
|
29 | 29 | private ProjectMediator projectMediator;
|
30 | 30 | private ServerAccess serverAccess;
|
31 | 31 | private ConvenientDialogDisplayer dialogDisplayer;
|
32 |
| - |
| 32 | + private TmcCore core; |
| 33 | + |
33 | 34 | public UpdateExercisesAction(List<Exercise> exercisesToUpdate) {
|
34 | 35 | this.exercisesToUpdate = exercisesToUpdate;
|
35 | 36 | this.courseDb = CourseDb.getInstance();
|
36 | 37 | this.projectMediator = ProjectMediator.getInstance();
|
37 | 38 | this.serverAccess = new ServerAccess();
|
38 | 39 | this.dialogDisplayer = ConvenientDialogDisplayer.getDefault();
|
| 40 | + this.core = TmcCoreSingleton.getInstance(); |
39 | 41 | }
|
40 | 42 |
|
41 | 43 | @Override
|
42 | 44 | public void actionPerformed(ActionEvent e) {
|
43 | 45 | run();
|
44 | 46 | }
|
45 |
| - |
| 47 | + |
46 | 48 | public void run() {
|
47 |
| - final AggregatingBgTaskListener<TmcProjectInfo> projectOpener = new AggregatingBgTaskListener<TmcProjectInfo>(exercisesToUpdate.size(), new BgTaskListener<Collection<TmcProjectInfo>>() { |
48 |
| - @Override |
49 |
| - public void bgTaskReady(Collection<TmcProjectInfo> result) { |
50 |
| - result = new ArrayList<TmcProjectInfo>(result); |
51 |
| - |
52 |
| - // result may contain nulls since some downloads might fail |
53 |
| - while (result.remove(null)) { |
54 |
| - } |
55 |
| - |
56 |
| - projectMediator.scanForExternalChanges(result); |
57 |
| - |
58 |
| - // Open all at once. This is much faster. |
59 |
| - projectMediator.openProjects(result); |
60 |
| - } |
61 |
| - |
62 |
| - // Cancelled and failed are never called since we only call bgTaskReady below manually |
63 |
| - @Override |
64 |
| - public void bgTaskCancelled() { |
65 |
| - } |
66 |
| - @Override |
67 |
| - public void bgTaskFailed(Throwable ex) { |
68 |
| - } |
69 |
| - }); |
70 |
| - |
71 |
| - |
72 |
| - for (final Exercise exercise : exercisesToUpdate) { |
73 |
| - final File projectDir = projectMediator.getProjectDirForExercise(exercise); |
| 49 | + try { |
| 50 | + ListenableFuture<List<Exercise>> downloadFuture = core.downloadExercises(exercisesToUpdate, |
| 51 | + NBTmcSettings.getDefault()); |
| 52 | + Futures.addCallback(downloadFuture, new ProjectOpenerCallback()); |
74 | 53 |
|
75 |
| - BgTask.start("Downloading " + exercise.getName(), serverAccess.getDownloadingExerciseZipTask(exercise), new BgTaskListener<byte[]>() { |
| 54 | + } catch (TmcCoreException ex) { |
| 55 | + Exceptions.printStackTrace(ex); |
| 56 | + dialogDisplayer.displayError("Error occured while downloading updates", ex); |
| 57 | + } |
| 58 | + } |
76 | 59 |
|
77 |
| - @Override |
78 |
| - public void bgTaskReady(byte[] data) { |
79 |
| - TmcProjectInfo project = null; |
80 |
| - try { |
81 |
| - try { |
82 |
| - ExerciseUpdateOverwritingDecider overwriter = new ExerciseUpdateOverwritingDecider(projectDir); |
83 |
| - NbProjectUnzipper unzipper = new NbProjectUnzipper(overwriter); |
84 |
| - NbProjectUnzipper.Result result = unzipper.unzipProject(data, projectDir); |
85 |
| - log.info("== Exercise unzip result ==\n" + result); |
86 |
| - } catch (IOException ex) { |
87 |
| - dialogDisplayer.displayError("Failed to update project.", ex); |
88 |
| - return; |
89 |
| - } |
90 |
| - courseDb.exerciseDownloaded(exercise); |
91 |
| - |
92 |
| - project = projectMediator.tryGetProjectForExercise(exercise); |
93 |
| - } finally { |
94 |
| - projectOpener.bgTaskReady(project); |
95 |
| - } |
96 |
| - } |
| 60 | + private class ProjectOpenerCallback implements FutureCallback<List<Exercise>> { |
97 | 61 |
|
98 |
| - @Override |
99 |
| - public void bgTaskCancelled() { |
100 |
| - projectOpener.bgTaskReady(null); |
101 |
| - } |
| 62 | + @Override |
| 63 | + public void onSuccess(List<Exercise> downloadedExercises) { |
| 64 | + ArrayList<TmcProjectInfo> projects = new ArrayList<TmcProjectInfo>(); |
102 | 65 |
|
103 |
| - @Override |
104 |
| - public void bgTaskFailed(Throwable ex) { |
105 |
| - projectOpener.bgTaskReady(null); |
106 |
| - String msg = ServerErrorHelper.getServerExceptionMsg(ex); |
107 |
| - dialogDisplayer.displayError("Failed to download updated exercises.\n" + msg, ex); |
| 66 | + for (Exercise exercise : downloadedExercises) { |
| 67 | + courseDb.exerciseDownloaded(exercise); |
| 68 | + TmcProjectInfo project = projectMediator.tryGetProjectForExercise(exercise); |
| 69 | + if (project != null) { |
| 70 | + projects.add(project); |
108 | 71 | }
|
109 |
| - }); |
| 72 | + } |
| 73 | + projectMediator.scanForExternalChanges(projects); |
| 74 | + |
| 75 | + projectMediator.openProjects(projects); |
110 | 76 | }
|
| 77 | + |
| 78 | + @Override |
| 79 | + public void onFailure(Throwable ex) { |
| 80 | + Exceptions.printStackTrace(ex); |
| 81 | + dialogDisplayer.displayError("Error occured while downloading updates", ex); |
| 82 | + } |
| 83 | + |
111 | 84 | }
|
112 | 85 | }
|
0 commit comments