14
14
import fi .helsinki .cs .tmc .utilities .BgTaskListenerList ;
15
15
import fi .helsinki .cs .tmc .utilities .CancellableCallable ;
16
16
import hy .tmc .core .TmcCore ;
17
- import hy .tmc .core .exceptions .TmcCoreException ;
18
17
import java .util .ArrayList ;
19
18
import java .util .List ;
20
- import java .util .concurrent .ExecutionException ;
21
- import java .util .concurrent .ExecutorService ;
22
- import java .util .concurrent .Executors ;
23
19
import java .util .logging .Level ;
24
20
import java .util .logging .Logger ;
25
21
import org .openide .util .Exceptions ;
@@ -35,18 +31,22 @@ public final class RefreshCoursesAction {
35
31
private ConvenientDialogDisplayer dialogs ;
36
32
37
33
private BgTaskListenerList <List <Course >> listeners ;
34
+ private final TmcCore tmcCore ;
35
+ private final TmcSettings tmcSettings ;
38
36
39
37
public RefreshCoursesAction () {
40
38
this (TmcSettings .getDefault ());
41
39
}
42
40
43
41
public RefreshCoursesAction (TmcSettings settings ) {
42
+ this .tmcSettings = settings ;
44
43
this .serverAccess = new ServerAccess (settings );
45
44
this .serverAccess .setSettings (settings );
46
45
this .courseDb = CourseDb .getInstance ();
47
46
this .dialogs = ConvenientDialogDisplayer .getDefault ();
48
47
49
48
this .listeners = new BgTaskListenerList <List <Course >>();
49
+ this .tmcCore = new TmcCore ();
50
50
}
51
51
52
52
public RefreshCoursesAction addDefaultListener (boolean showDialogOnError , boolean updateCourseDb ) {
@@ -58,64 +58,42 @@ public RefreshCoursesAction addListener(BgTaskListener<List<Course>> listener) {
58
58
this .listeners .addListener (listener );
59
59
return this ;
60
60
}
61
-
62
- public void run () throws TmcCoreException {
63
- CancellableCallable <List <Course >> courseListTask = serverAccess .getDownloadingCourseListTask ();
64
- TmcCore c = new TmcCore ();
65
- final ListenableFuture <List <Course >> courses = c .listCourses ();
66
- /*
67
-
68
- courses.addListener(new Runnable() {
69
-
61
+
62
+ public void run () {
63
+ Credentials credentials = new Credentials (this .tmcSettings .getUsername (),
64
+ this .tmcSettings .getPassword ());
65
+ System .out .println (credentials );
66
+ ListenableFuture <Boolean > login = this .tmcCore .login (credentials , tmcSettings .getServerBaseUrl ());
67
+ Futures .addCallback (login , new FutureCallback <Boolean >() {
70
68
@ Override
71
- public void run() {
72
- System.out.println("Jeee, kurssit haettu!");
73
-
74
- try {
75
- for (Course get : courses.get()) {
76
- System.out.println(get.getName());
77
- }
78
- } catch (InterruptedException ex) {
79
- Exceptions.printStackTrace(ex);
80
- } catch (ExecutionException ex) {
81
- Exceptions.printStackTrace(ex);
82
- }
83
-
69
+ public void onSuccess (Boolean v ) {
70
+ System .out .println ("TOIMIIIII: " + v );
84
71
}
85
-
86
- }, Executors.newFixedThreadPool(1));
87
-
88
- if(true) {
89
- return;
90
- }*/
91
-
92
- /*Futures.addCallback(courses, new FutureCallback<List<Course>>(){
72
+
93
73
@ Override
94
- public void onSuccess(List<Course> v) {
95
- for(Course c: v){
96
- System.out.println(c.getName());
97
- }
74
+ public void onFailure (Throwable thrwbl ) {
75
+ System .out .println ("LOGIN LATAUS FEILASI1: " );
76
+ Exceptions .printStackTrace (thrwbl );
98
77
}
78
+ });
79
+ }
99
80
100
- @Override
101
- public void onFailure(Throwable thrwbl) {
102
-
103
- }
104
- });*/
81
+ public void oldRun () {
105
82
83
+ CancellableCallable <List <Course >> courseListTask = serverAccess .getDownloadingCourseListTask ();
106
84
BgTask .start ("Refreshing course list" , courseListTask , new BgTaskListener <List <Course >>() {
107
-
85
+
108
86
@ Override
109
87
public void bgTaskReady (final List <Course > courses ) {
110
88
Course currentCourseStub = CourseListUtils .getCourseByName (courses , courseDb .getCurrentCourseName ());
111
89
if (currentCourseStub != null ) {
112
90
CancellableCallable <Course > currentCourseTask = serverAccess .getFullCourseInfoTask (currentCourseStub );
113
-
91
+
114
92
BgTask .start ("Loading course" , currentCourseTask , new BgTaskListener <Course >() {
115
93
@ Override
116
94
public void bgTaskReady (Course currentCourse ) {
117
95
currentCourse .setExercisesLoaded (true );
118
-
96
+
119
97
ArrayList <Course > finalCourses = new ArrayList <Course >();
120
98
for (Course course : courses ) {
121
99
if (course .getName ().equals (currentCourse .getName ())) {
@@ -126,12 +104,12 @@ public void bgTaskReady(Course currentCourse) {
126
104
}
127
105
listeners .bgTaskReady (finalCourses );
128
106
}
129
-
107
+
130
108
@ Override
131
109
public void bgTaskCancelled () {
132
110
listeners .bgTaskCancelled ();
133
111
}
134
-
112
+
135
113
@ Override
136
114
public void bgTaskFailed (Throwable ex ) {
137
115
log .log (Level .INFO , "Failed to download current course info." , ex );
@@ -143,12 +121,12 @@ public void bgTaskFailed(Throwable ex) {
143
121
listeners .bgTaskReady (courses );
144
122
}
145
123
}
146
-
124
+
147
125
@ Override
148
126
public void bgTaskCancelled () {
149
127
listeners .bgTaskCancelled ();
150
128
}
151
-
129
+
152
130
@ Override
153
131
public void bgTaskFailed (Throwable ex ) {
154
132
log .log (Level .INFO , "Failed to download course list." , ex );
0 commit comments