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 ;
8
+ import fi .helsinki .cs .tmc .model .NBTmcSettings ;
5
9
import fi .helsinki .cs .tmc .model .ProjectMediator ;
6
10
import fi .helsinki .cs .tmc .model .ServerAccess ;
11
+ import fi .helsinki .cs .tmc .model .TmcCoreSingleton ;
7
12
import fi .helsinki .cs .tmc .model .TmcProjectInfo ;
8
13
import fi .helsinki .cs .tmc .ui .ConvenientDialogDisplayer ;
9
14
import fi .helsinki .cs .tmc .utilities .AggregatingBgTaskListener ;
10
15
import fi .helsinki .cs .tmc .utilities .BgTask ;
11
16
import fi .helsinki .cs .tmc .utilities .BgTaskListener ;
12
17
import fi .helsinki .cs .tmc .utilities .zip .NbProjectUnzipper ;
18
+ import hy .tmc .core .TmcCore ;
19
+ import hy .tmc .core .exceptions .TmcCoreException ;
20
+ import java .util .ArrayList ;
13
21
import java .util .Collection ;
14
22
import java .util .List ;
15
23
import java .util .concurrent .Callable ;
21
29
* Downloads and opens the given exercises in the background.
22
30
*/
23
31
public class DownloadExercisesAction {
32
+
24
33
private static final Logger logger = Logger .getLogger (DownloadExercisesAction .class .getName ());
25
34
26
35
private ServerAccess serverAccess ;
@@ -29,6 +38,8 @@ public class DownloadExercisesAction {
29
38
private ConvenientDialogDisplayer dialogs ;
30
39
31
40
private List <Exercise > exercisesToDownload ;
41
+ private TmcCore tmcCore ;
42
+ private NBTmcSettings settings ;
32
43
33
44
public DownloadExercisesAction (List <Exercise > exercisesToOpen ) {
34
45
this .serverAccess = new ServerAccess ();
@@ -37,17 +48,43 @@ public DownloadExercisesAction(List<Exercise> exercisesToOpen) {
37
48
this .dialogs = ConvenientDialogDisplayer .getDefault ();
38
49
39
50
this .exercisesToDownload = exercisesToOpen ;
51
+ this .tmcCore = TmcCoreSingleton .getInstance ();
52
+ this .settings = NBTmcSettings .getDefault ();
40
53
}
41
54
42
- public void run () {
43
- final AggregatingBgTaskListener <TmcProjectInfo > aggregator =
44
- new AggregatingBgTaskListener <TmcProjectInfo >(exercisesToDownload .size (), whenAllDownloadsFinished );
55
+ public void run () throws TmcCoreException {
56
+ // final AggregatingBgTaskListener<TmcProjectInfo> aggregator
57
+ // = new AggregatingBgTaskListener<TmcProjectInfo>(exercisesToDownload.size(), whenAllDownloadsFinished);
45
58
46
- for (final Exercise exercise : exercisesToDownload ) {
47
- startDownloading (exercise , aggregator );
48
- }
59
+ ListenableFuture <List <Exercise >> dlFuture = tmcCore .donwloadExercises (exercisesToDownload , settings );
60
+
61
+ Futures .addCallback (dlFuture ,
62
+ new FutureCallback <List <Exercise >>() {
63
+
64
+ @ Override
65
+ public void onSuccess (List <Exercise > downloadedExercises ) {
66
+ List <TmcProjectInfo > projects = new ArrayList <TmcProjectInfo >();
67
+ for (Exercise exercise : downloadedExercises ) {
68
+ projects .add (projectMediator .tryGetProjectForExercise (exercise ));
69
+ }
70
+ projectMediator .openProjects (projects );
71
+ }
72
+
73
+ @ Override
74
+ public void onFailure (Throwable thrwbl ) {
75
+ logger .log (Level .INFO , "Failed to download exercise file." , thrwbl );
76
+ dialogs .displayError ("Failed to download exercises.\n " + ServerErrorHelper .getServerExceptionMsg (thrwbl ));
77
+
78
+ }
79
+ }
80
+ );
81
+
82
+ //for (final Exercise exercise : exercisesToDownload) {
83
+ // startDownloading(exercise, aggregator);
84
+ //}
49
85
}
50
86
87
+ /*
51
88
private void startDownloading(final Exercise exercise, final BgTaskListener<TmcProjectInfo> listener) {
52
89
BgTask.start("Downloading " + exercise.getName(), serverAccess.getDownloadingExerciseZipTask(exercise), new BgTaskListener<byte[]>() {
53
90
@Override
@@ -75,7 +112,7 @@ public void run() {
75
112
}
76
113
}, listener);
77
114
}
78
-
115
+
79
116
@Override
80
117
public void bgTaskCancelled() {
81
118
listener.bgTaskCancelled();
@@ -87,7 +124,7 @@ public void bgTaskFailed(Throwable ex) {
87
124
}
88
125
});
89
126
}
90
-
127
+ /*
91
128
private BgTaskListener<Collection<TmcProjectInfo>> whenAllDownloadsFinished = new BgTaskListener<Collection<TmcProjectInfo>>() {
92
129
@Override
93
130
public void bgTaskReady(Collection<TmcProjectInfo> projects) {
@@ -104,4 +141,6 @@ public void bgTaskFailed(Throwable ex) {
104
141
dialogs.displayError("Failed to download exercises.\n" + ServerErrorHelper.getServerExceptionMsg(ex));
105
142
}
106
143
};
144
+ */
145
+
107
146
}
0 commit comments