|
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 |
| -import fi.helsinki.cs.tmc.model.CourseDb; |
| 7 | +import fi.helsinki.cs.tmc.model.NBTmcSettings; |
5 | 8 | import fi.helsinki.cs.tmc.model.ProjectMediator;
|
6 |
| -import fi.helsinki.cs.tmc.model.ServerAccess; |
| 9 | +import fi.helsinki.cs.tmc.model.TmcCoreSingleton; |
7 | 10 | import fi.helsinki.cs.tmc.model.TmcProjectInfo;
|
8 | 11 | import fi.helsinki.cs.tmc.ui.ConvenientDialogDisplayer;
|
9 |
| -import fi.helsinki.cs.tmc.utilities.AggregatingBgTaskListener; |
10 |
| -import fi.helsinki.cs.tmc.utilities.BgTask; |
11 |
| -import fi.helsinki.cs.tmc.utilities.BgTaskListener; |
12 |
| -import fi.helsinki.cs.tmc.utilities.zip.NbProjectUnzipper; |
13 |
| -import java.util.Collection; |
| 12 | +import hy.tmc.core.TmcCore; |
| 13 | +import hy.tmc.core.exceptions.TmcCoreException; |
| 14 | +import java.util.ArrayList; |
14 | 15 | import java.util.List;
|
15 |
| -import java.util.concurrent.Callable; |
16 | 16 | import java.util.logging.Level;
|
17 | 17 | import java.util.logging.Logger;
|
18 |
| -import javax.swing.SwingUtilities; |
19 | 18 |
|
20 | 19 | /**
|
21 | 20 | * Downloads and opens the given exercises in the background.
|
22 | 21 | */
|
23 | 22 | public class DownloadExercisesAction {
|
| 23 | + |
24 | 24 | private static final Logger logger = Logger.getLogger(DownloadExercisesAction.class.getName());
|
25 | 25 |
|
26 |
| - private ServerAccess serverAccess; |
27 |
| - private CourseDb courseDb; |
28 | 26 | private ProjectMediator projectMediator;
|
29 | 27 | private ConvenientDialogDisplayer dialogs;
|
30 | 28 |
|
31 | 29 | private List<Exercise> exercisesToDownload;
|
| 30 | + private TmcCore tmcCore; |
| 31 | + private NBTmcSettings settings; |
32 | 32 |
|
33 | 33 | public DownloadExercisesAction(List<Exercise> exercisesToOpen) {
|
34 |
| - this.serverAccess = new ServerAccess(); |
35 |
| - this.courseDb = CourseDb.getInstance(); |
36 | 34 | this.projectMediator = ProjectMediator.getInstance();
|
37 | 35 | this.dialogs = ConvenientDialogDisplayer.getDefault();
|
38 | 36 |
|
39 | 37 | this.exercisesToDownload = exercisesToOpen;
|
| 38 | + this.tmcCore = TmcCoreSingleton.getInstance(); |
| 39 | + this.settings = NBTmcSettings.getDefault(); |
40 | 40 | }
|
41 | 41 |
|
42 |
| - public void run() { |
43 |
| - final AggregatingBgTaskListener<TmcProjectInfo> aggregator = |
44 |
| - new AggregatingBgTaskListener<TmcProjectInfo>(exercisesToDownload.size(), whenAllDownloadsFinished); |
45 |
| - |
46 |
| - for (final Exercise exercise : exercisesToDownload) { |
47 |
| - startDownloading(exercise, aggregator); |
48 |
| - } |
49 |
| - } |
50 |
| - |
51 |
| - private void startDownloading(final Exercise exercise, final BgTaskListener<TmcProjectInfo> listener) { |
52 |
| - BgTask.start("Downloading " + exercise.getName(), serverAccess.getDownloadingExerciseZipTask(exercise), new BgTaskListener<byte[]>() { |
53 |
| - @Override |
54 |
| - public void bgTaskReady(final byte[] zipData) { |
55 |
| - BgTask.start("Extracting project", new Callable<TmcProjectInfo>() { |
56 |
| - @Override |
57 |
| - public TmcProjectInfo call() throws Exception { |
58 |
| - NbProjectUnzipper unzipper = new NbProjectUnzipper(); |
59 |
| - unzipper.unzipProject(zipData, projectMediator.getProjectDirForExercise(exercise)); |
60 |
| - TmcProjectInfo proj = projectMediator.tryGetProjectForExercise(exercise); |
61 |
| - |
62 |
| - if (proj == null) { |
63 |
| - throw new RuntimeException("Failed to open project for exercise " + exercise.getName()); |
64 |
| - } |
| 42 | + public void run() throws TmcCoreException { |
| 43 | + // final AggregatingBgTaskListener<TmcProjectInfo> aggregator |
| 44 | + // = new AggregatingBgTaskListener<TmcProjectInfo>(exercisesToDownload.size(), whenAllDownloadsFinished); |
65 | 45 |
|
66 |
| - // Need to invoke courseDb in swing thread to avoid races |
67 |
| - SwingUtilities.invokeAndWait(new Runnable() { |
68 |
| - @Override |
69 |
| - public void run() { |
70 |
| - courseDb.exerciseDownloaded(exercise); |
71 |
| - } |
72 |
| - }); |
| 46 | + ListenableFuture<List<Exercise>> dlFuture = tmcCore.downloadExercises(exercisesToDownload, settings); |
73 | 47 |
|
74 |
| - return proj; |
75 |
| - } |
76 |
| - }, listener); |
77 |
| - } |
78 |
| - |
79 |
| - @Override |
80 |
| - public void bgTaskCancelled() { |
81 |
| - listener.bgTaskCancelled(); |
82 |
| - } |
83 |
| - |
84 |
| - @Override |
85 |
| - public void bgTaskFailed(Throwable ex) { |
86 |
| - listener.bgTaskFailed(ex); |
87 |
| - } |
88 |
| - }); |
| 48 | + Futures.addCallback(dlFuture, new ProjectOpener()); |
89 | 49 | }
|
90 | 50 |
|
91 |
| - private BgTaskListener<Collection<TmcProjectInfo>> whenAllDownloadsFinished = new BgTaskListener<Collection<TmcProjectInfo>>() { |
| 51 | + private class ProjectOpener implements FutureCallback<List<Exercise>> { |
| 52 | + |
92 | 53 | @Override
|
93 |
| - public void bgTaskReady(Collection<TmcProjectInfo> projects) { |
| 54 | + public void onSuccess(List<Exercise> downloadedExercises) { |
| 55 | + List<TmcProjectInfo> projects = new ArrayList<TmcProjectInfo>(); |
| 56 | + for (Exercise exercise : downloadedExercises) { |
| 57 | + TmcProjectInfo info = projectMediator.tryGetProjectForExercise(exercise); |
| 58 | + if (info == null) { |
| 59 | + dialogs.displayError( |
| 60 | + "Could not find exercise " + exercise.getName() + " from the filesystem"); |
| 61 | + continue; |
| 62 | + } |
| 63 | + projects.add(info); |
| 64 | + } |
94 | 65 | projectMediator.openProjects(projects);
|
95 | 66 | }
|
96 | 67 |
|
97 | 68 | @Override
|
98 |
| - public void bgTaskCancelled() { |
99 |
| - } |
| 69 | + public void onFailure(Throwable thrwbl) { |
| 70 | + logger.log(Level.INFO, "Failed to download exercise file.", thrwbl); |
| 71 | + dialogs.displayError("Failed to download exercises.\n" + ServerErrorHelper.getServerExceptionMsg(thrwbl)); |
100 | 72 |
|
101 |
| - @Override |
102 |
| - public void bgTaskFailed(Throwable ex) { |
103 |
| - logger.log(Level.INFO, "Failed to download exercise file.", ex); |
104 |
| - dialogs.displayError("Failed to download exercises.\n" + ServerErrorHelper.getServerExceptionMsg(ex)); |
105 | 73 | }
|
106 |
| - }; |
| 74 | + } |
107 | 75 | }
|
0 commit comments