9
9
import fi .helsinki .cs .tmc .model .ServerAccess ;
10
10
import fi .helsinki .cs .tmc .model .TmcCoreSingleton ;
11
11
import fi .helsinki .cs .tmc .model .NBTmcSettings ;
12
- import fi .helsinki .cs .tmc .utilities .BgTaskListener ;
13
12
import fi .helsinki .cs .tmc .ui .ConvenientDialogDisplayer ;
14
- import fi .helsinki .cs .tmc .utilities .BgTask ;
15
- import fi .helsinki .cs .tmc .utilities .BgTaskListenerList ;
16
- import fi .helsinki .cs .tmc .utilities .CancellableCallable ;
13
+ import fi .helsinki .cs .tmc .utilities .FutureCallbackList ;
17
14
import hy .tmc .core .TmcCore ;
18
- import hy .tmc .core .domain .Credentials ;
19
15
import hy .tmc .core .exceptions .TmcCoreException ;
20
16
import java .util .ArrayList ;
21
17
import java .util .List ;
27
23
* Refreshes the course list in the background.
28
24
*/
29
25
public final class RefreshCoursesAction {
26
+
30
27
private final static Logger log = Logger .getLogger (RefreshCoursesAction .class .getName ());
31
28
32
29
private ServerAccess serverAccess ;
33
30
private CourseDb courseDb ;
34
31
private ConvenientDialogDisplayer dialogs ;
35
-
36
- private BgTaskListenerList <List <Course >> listeners ;
32
+
33
+ private FutureCallbackList <List <Course >> callbacks ;
37
34
private final TmcCore tmcCore ;
38
35
private final NBTmcSettings tmcSettings ;
39
36
40
37
public RefreshCoursesAction () {
41
38
this (NBTmcSettings .getDefault ());
42
39
}
43
-
40
+
44
41
public RefreshCoursesAction (NBTmcSettings settings ) {
45
42
this .tmcSettings = settings ;
46
43
this .serverAccess = new ServerAccess (settings );
47
44
this .serverAccess .setSettings (settings );
48
45
this .courseDb = CourseDb .getInstance ();
49
46
this .dialogs = ConvenientDialogDisplayer .getDefault ();
50
47
51
- this .listeners = new BgTaskListenerList <List <Course >>();
48
+ //this.listeners = new BgTaskListenerList<List<Course>>();
49
+ this .callbacks = new FutureCallbackList <List <Course >>();
52
50
this .tmcCore = TmcCoreSingleton .getInstance ();
53
51
}
54
52
55
53
public RefreshCoursesAction addDefaultListener (boolean showDialogOnError , boolean updateCourseDb ) {
56
- this .listeners .addListener (new DefaultListener (showDialogOnError , updateCourseDb ));
54
+ this .callbacks .addListener (new DefaultListener (showDialogOnError , updateCourseDb ));
57
55
return this ;
58
56
}
59
57
60
- public RefreshCoursesAction addListener (BgTaskListener <List <Course >> listener ) {
61
- this .listeners .addListener (listener );
58
+ public RefreshCoursesAction addListener (FutureCallback <List <Course >> callbacks ) {
59
+ this .callbacks .addListener (callbacks );
62
60
return this ;
63
61
}
64
-
65
- /*public void run() {
62
+
63
+ /**
64
+ * Starts downloading course-jsons from TMC-server.
65
+ * Url of TMC-server is defined in TmcSettings object.
66
+ * TmcCore includes all logic, callbacks here are run after core-futures are ready.
67
+ */
68
+ public void run () {
66
69
try {
67
- Credentials credentials = new Credentials(this.tmcSettings.getUsername(),
68
- this.tmcSettings.getPassword()) {};
69
- System.out.println(credentials);
70
- ListenableFuture<Boolean> login = this.tmcCore.login(credentials, tmcSettings.getServerBaseUrl() + "/user");
71
- Futures.addCallback(login, new FutureCallback<Boolean>() {
72
- @Override
73
- public void onSuccess(Boolean v) {
74
- System.out.println("TOIMIIIII: " + v);
75
- }
76
-
77
- @Override
78
- public void onFailure(Throwable thrwbl) {
79
- System.out.println("LOGIN LATAUS FEILASI1: " );
80
- Exceptions.printStackTrace(thrwbl);
81
- }
82
- });
70
+ ListenableFuture <List <Course >> listCourses = this .tmcCore .listCourses (tmcSettings );
71
+ Futures .addCallback (listCourses , new LoadCourses ());
83
72
} catch (TmcCoreException ex ) {
84
73
Exceptions .printStackTrace (ex );
74
+ callbacks .onFailure (ex );
75
+ }
76
+ }
77
+
78
+ /**
79
+ * This callBack is run when ListenableFuture (to witch this is attached) is done.
80
+ * On success method takes list of Course-objects, searches the current course and starts uploading
81
+ * the details of the course.
82
+ * If no currentCourse found, no need to update details.
83
+ */
84
+ class LoadCourses implements FutureCallback <List <Course >> {
85
+ @ Override
86
+ public void onSuccess (final List <Course > courses ) {
87
+ Course currentCourse = CourseListUtils .getCourseByName (courses , courseDb .getCurrentCourseName ());
88
+ if (currentCourse != null ) {
89
+ try {
90
+ ListenableFuture <Course > courseFuture = tmcCore .getCourse (tmcSettings , currentCourse .getDetailsUrl ());
91
+ Futures .addCallback (courseFuture , new UpdateCourse (courses ));
92
+ } catch (TmcCoreException ex ) {
93
+ Exceptions .printStackTrace (ex );
94
+ callbacks .onFailure (ex );
95
+ }
96
+ } else {
97
+ callbacks .onSuccess (courses );
98
+ }
85
99
}
86
- }*/
87
100
88
- public void run () {
101
+ @ Override
102
+ public void onFailure (Throwable ex ) {
103
+ log .log (Level .INFO , "Failed to download current course info." , ex );
104
+ callbacks .onFailure (ex );
105
+ }
106
+ }
107
+
108
+ /**
109
+ * When detailed current course is present, courses will be given to FutureCallbackList,
110
+ * that shares the result to every callback that is attached to that list.
111
+ */
112
+ class UpdateCourse implements FutureCallback <Course > {
113
+ List <Course > courses ;
89
114
90
- CancellableCallable <List <Course >> courseListTask = serverAccess .getDownloadingCourseListTask ();
91
- BgTask .start ("Refreshing course list" , courseListTask , new BgTaskListener <List <Course >>() {
92
-
93
- @ Override
94
- public void bgTaskReady (final List <Course > courses ) {
95
- Course currentCourseStub = CourseListUtils .getCourseByName (courses , courseDb .getCurrentCourseName ());
96
- if (currentCourseStub != null ) {
97
- CancellableCallable <Course > currentCourseTask = serverAccess .getFullCourseInfoTask (currentCourseStub );
98
-
99
- BgTask .start ("Loading course" , currentCourseTask , new BgTaskListener <Course >() {
100
- @ Override
101
- public void bgTaskReady (Course currentCourse ) {
102
- currentCourse .setExercisesLoaded (true );
103
-
104
- ArrayList <Course > finalCourses = new ArrayList <Course >();
105
- for (Course course : courses ) {
106
- if (course .getName ().equals (currentCourse .getName ())) {
107
- finalCourses .add (currentCourse );
108
- } else {
109
- finalCourses .add (course );
110
- }
111
- }
112
- listeners .bgTaskReady (finalCourses );
113
- }
114
-
115
- @ Override
116
- public void bgTaskCancelled () {
117
- listeners .bgTaskCancelled ();
118
- }
119
-
120
- @ Override
121
- public void bgTaskFailed (Throwable ex ) {
122
- log .log (Level .INFO , "Failed to download current course info." , ex );
123
- listeners .bgTaskFailed (ex );
124
- }
125
- });
126
-
115
+ public UpdateCourse (List <Course > courses ) {
116
+ this .courses = courses ;
117
+ }
118
+
119
+ @ Override
120
+ public void onSuccess (Course detailedCourse ) {
121
+ detailedCourse .setExercisesLoaded (true );
122
+ ArrayList <Course > finalCourses = new ArrayList <Course >();
123
+ for (Course course : courses ) {
124
+ if (course .getName ().equals (detailedCourse .getName ())) {
125
+ finalCourses .add (detailedCourse );
127
126
} else {
128
- listeners . bgTaskReady ( courses );
127
+ finalCourses . add ( course );
129
128
}
130
129
}
131
-
132
- @ Override
133
- public void bgTaskCancelled () {
134
- listeners .bgTaskCancelled ();
135
- }
136
-
137
- @ Override
138
- public void bgTaskFailed (Throwable ex ) {
139
- log .log (Level .INFO , "Failed to download course list." , ex );
140
- listeners .bgTaskFailed (ex );
141
- }
142
- });
130
+ callbacks .onSuccess (finalCourses );
131
+ }
132
+
133
+ @ Override
134
+ public void onFailure (Throwable ex ) {
135
+ log .log (Level .INFO , "Failed to download current course info." , ex );
136
+ callbacks .onFailure (ex );
137
+ }
143
138
}
144
139
145
- private class DefaultListener implements BgTaskListener <List <Course >> {
140
+ /**
141
+ * Updates the courseDb after all course-jsons are downloaded.
142
+ */
143
+ private class DefaultListener implements FutureCallback <List <Course >> {
144
+
146
145
private final boolean showDialogOnError ;
147
146
private final boolean updateCourseDb ;
148
147
@@ -152,18 +151,14 @@ public DefaultListener(boolean showDialogOnError, boolean updateCourseDb) {
152
151
}
153
152
154
153
@ Override
155
- public void bgTaskReady (List <Course > result ) {
154
+ public void onSuccess (List <Course > result ) {
156
155
if (updateCourseDb ) {
157
156
courseDb .setAvailableCourses (result );
158
157
}
159
158
}
160
159
161
160
@ Override
162
- public void bgTaskCancelled () {
163
- }
164
-
165
- @ Override
166
- public void bgTaskFailed (Throwable ex ) {
161
+ public void onFailure (Throwable ex ) {
167
162
if (showDialogOnError ) {
168
163
dialogs .displayError ("Course refresh failed.\n " + ServerErrorHelper .getServerExceptionMsg (ex ));
169
164
}
0 commit comments