7
7
import fi .helsinki .cs .tmc .data .CourseListUtils ;
8
8
import fi .helsinki .cs .tmc .model .CourseDb ;
9
9
import fi .helsinki .cs .tmc .model .ServerAccess ;
10
- import fi .helsinki .cs .tmc .model .TmcSettings ;
10
+ import fi .helsinki .cs .tmc .model .TmcCoreSingleton ;
11
+ import fi .helsinki .cs .tmc .model .NBTmcSettings ;
11
12
import fi .helsinki .cs .tmc .utilities .BgTaskListener ;
12
13
import fi .helsinki .cs .tmc .ui .ConvenientDialogDisplayer ;
13
14
import fi .helsinki .cs .tmc .utilities .BgTask ;
25
26
* Refreshes the course list in the background.
26
27
*/
27
28
public final class RefreshCoursesAction {
29
+
28
30
private final static Logger log = Logger .getLogger (RefreshCoursesAction .class .getName ());
29
31
30
32
private ServerAccess serverAccess ;
31
33
private CourseDb courseDb ;
32
34
private ConvenientDialogDisplayer dialogs ;
33
-
35
+
34
36
private BgTaskListenerList <List <Course >> listeners ;
35
37
private final TmcCore tmcCore ;
36
- private final TmcSettings tmcSettings ;
38
+ private final NBTmcSettings tmcSettings ;
37
39
38
40
public RefreshCoursesAction () {
39
- this (TmcSettings .getDefault ());
41
+ this (NBTmcSettings .getDefault ());
40
42
}
41
-
42
- public RefreshCoursesAction (TmcSettings settings ) {
43
+
44
+ public RefreshCoursesAction (NBTmcSettings settings ) {
43
45
this .tmcSettings = settings ;
44
46
this .serverAccess = new ServerAccess (settings );
45
47
this .serverAccess .setSettings (settings );
46
48
this .courseDb = CourseDb .getInstance ();
47
49
this .dialogs = ConvenientDialogDisplayer .getDefault ();
48
50
49
51
this .listeners = new BgTaskListenerList <List <Course >>();
50
- this .tmcCore = new TmcCore ();
52
+ this .tmcCore = TmcCoreSingleton . getInstance ();
51
53
}
52
54
53
55
public RefreshCoursesAction addDefaultListener (boolean showDialogOnError , boolean updateCourseDb ) {
@@ -59,46 +61,70 @@ public RefreshCoursesAction addListener(BgTaskListener<List<Course>> listener) {
59
61
this .listeners .addListener (listener );
60
62
return this ;
61
63
}
62
-
63
- public void run () {
64
- // try {
65
- // Credentials credentials = new Credentials(this.tmcSettings.getUsername(),
66
- // this.tmcSettings.getPassword()) {};
67
- // System.out.println(credentials);
68
- // ListenableFuture<Boolean> login = this.tmcCore.login(credentials, tmcSettings.getServerBaseUrl()+"/user");
69
- // Futures.addCallback(login, new FutureCallback<Boolean>() {
70
- // @Override
71
- // public void onSuccess(Boolean loginSuccess) {
72
- //
73
- // }
74
- //
75
- // @Override
76
- // public void onFailure(Throwable thrwbl) {
77
- // System.out.println("LOGIN LATAUS FEILASI1: " );
78
- // Exceptions.printStackTrace(thrwbl);
79
- // }
80
- // });
81
- // } catch (TmcCoreException ex) {
82
- // Exceptions.printStackTrace(ex);
83
- // }
64
+
65
+ //HUOM metodissa oldRun on vanha toteutus. Se hakee palvelmelta nykyisen kurssin "fullcourse info".
66
+ public void newrun () {
67
+ try {
68
+ ListenableFuture <List <Course >> listCourses = this .tmcCore .listCourses (tmcSettings );
69
+ Futures .addCallback (listCourses , new FutureCallback <List <Course >>() {
70
+ @ Override
71
+ public void onSuccess (List <Course > courses ) {
72
+ Course currentCourse = CourseListUtils .getCourseByName (courses , courseDb .getCurrentCourseName ());
73
+ if (currentCourse != null ) {
74
+ ListenableFuture <Course > courseFuture = tmcCore .getCourse (tmcSettings , currentCourse .getDetailsUrl ());
75
+ Futures .addCallback (courseFuture , new FutureCallback <Course >()
76
+ @ Override
77
+ public void onSuccess (Course detailedCourse ) {
78
+ detailedCourse .setExercisesLoaded (true );
79
+ ArrayList <Course > finalCourses = new ArrayList <Course >();
80
+ for (Course course : courses ){
81
+ if (course .getName ().equals (detailedCourse .getName ())){
82
+ finalCourses .add (detailedCourses );
83
+ } else {
84
+ finalCourses .add (course );
85
+ }
86
+ }
87
+ listeners .bgTaskReady (finalCourses );
88
+ }
89
+ @ Override
90
+ public void onFailure () {
91
+
92
+ }
93
+ );
94
+ } else {
95
+ listeners .bgTaskReady (courses );
96
+ }
97
+ }
98
+
99
+ @ Override
100
+ public void onFailure (Throwable ex ) {
101
+ log .log (Level .INFO , "Failed to download current course info." , ex );
102
+ listeners .bgTaskFailed (ex );
103
+ }
104
+ }
105
+ );
106
+ } catch (TmcCoreException ex ) {
107
+ Exceptions .printStackTrace (ex );
108
+ }
84
109
}
85
110
86
- public void oldRun () {
87
-
111
+ public void run () {
88
112
CancellableCallable <List <Course >> courseListTask = serverAccess .getDownloadingCourseListTask ();
89
113
BgTask .start ("Refreshing course list" , courseListTask , new BgTaskListener <List <Course >>() {
90
-
114
+
91
115
@ Override
92
116
public void bgTaskReady (final List <Course > courses ) {
93
117
Course currentCourseStub = CourseListUtils .getCourseByName (courses , courseDb .getCurrentCourseName ());
118
+ // CurrentCourseStub on null jos on vanhentunut/ ei enää olemassa servulla
94
119
if (currentCourseStub != null ) {
120
+ // Jos on olemassa servulla, päivitetään lokaali kurssi servun kurssia vastaavaksi
95
121
CancellableCallable <Course > currentCourseTask = serverAccess .getFullCourseInfoTask (currentCourseStub );
96
-
122
+
97
123
BgTask .start ("Loading course" , currentCourseTask , new BgTaskListener <Course >() {
98
124
@ Override
99
125
public void bgTaskReady (Course currentCourse ) {
100
126
currentCourse .setExercisesLoaded (true );
101
-
127
+
102
128
ArrayList <Course > finalCourses = new ArrayList <Course >();
103
129
for (Course course : courses ) {
104
130
if (course .getName ().equals (currentCourse .getName ())) {
@@ -109,29 +135,28 @@ public void bgTaskReady(Course currentCourse) {
109
135
}
110
136
listeners .bgTaskReady (finalCourses );
111
137
}
112
-
138
+
113
139
@ Override
114
140
public void bgTaskCancelled () {
115
141
listeners .bgTaskCancelled ();
116
142
}
117
-
143
+
118
144
@ Override
119
145
public void bgTaskFailed (Throwable ex ) {
120
146
log .log (Level .INFO , "Failed to download current course info." , ex );
121
147
listeners .bgTaskFailed (ex );
122
148
}
123
149
});
124
-
125
150
} else {
126
151
listeners .bgTaskReady (courses );
127
152
}
128
153
}
129
-
154
+
130
155
@ Override
131
156
public void bgTaskCancelled () {
132
157
listeners .bgTaskCancelled ();
133
158
}
134
-
159
+
135
160
@ Override
136
161
public void bgTaskFailed (Throwable ex ) {
137
162
log .log (Level .INFO , "Failed to download course list." , ex );
@@ -141,6 +166,7 @@ public void bgTaskFailed(Throwable ex) {
141
166
}
142
167
143
168
private class DefaultListener implements BgTaskListener <List <Course >> {
169
+
144
170
private final boolean showDialogOnError ;
145
171
private final boolean updateCourseDb ;
146
172
0 commit comments