Skip to content

Commit 3e1a64d

Browse files
committed
DownloadExerciseAction now saves all checksums to courseDb. Then new exercises or updates are not offered if no reason.
1 parent fa6652f commit 3e1a64d

File tree

6 files changed

+65
-20
lines changed

6 files changed

+65
-20
lines changed

tmc-plugin/src/fi/helsinki/cs/tmc/actions/CheckForNewExercisesOrUpdates.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
import fi.helsinki.cs.tmc.model.CourseDb;
88
import fi.helsinki.cs.tmc.model.LocalExerciseStatus;
99
import fi.helsinki.cs.tmc.model.ObsoleteClientException;
10-
import fi.helsinki.cs.tmc.model.ServerAccess;
1110
import fi.helsinki.cs.tmc.model.NBTmcSettings;
11+
import fi.helsinki.cs.tmc.model.ServerAccess;
1212
import fi.helsinki.cs.tmc.model.TmcCoreSingleton;
1313
import fi.helsinki.cs.tmc.ui.DownloadOrUpdateExercisesDialog;
1414
import fi.helsinki.cs.tmc.ui.ConvenientDialogDisplayer;
1515
import fi.helsinki.cs.tmc.ui.TmcNotificationDisplayer;
1616
import fi.helsinki.cs.tmc.utilities.Inflector;
1717
import fi.helsinki.cs.tmc.utilities.TmcStringUtils;
1818
import hy.tmc.core.TmcCore;
19+
import hy.tmc.core.domain.Exercise;
1920
import hy.tmc.core.exceptions.TmcCoreException;
2021
import java.awt.event.ActionEvent;
2122
import java.awt.event.ActionListener;
@@ -78,22 +79,28 @@ public void actionPerformed(ActionEvent e) {
7879

7980
public void run() {
8081
try {
81-
ProgressHandle exerciseRefresh = ProgressHandleFactory.createSystemHandle(
82-
"Checking for new exercises");
83-
exerciseRefresh.start();
8482
final Course currentCourseBeforeUpdate = courseDb.getCurrentCourse();
8583
if (backgroundProcessingOrNoCurrentCourse(currentCourseBeforeUpdate)) {
8684
return;
8785
}
86+
ProgressHandle exerciseRefresh = ProgressHandleFactory.createSystemHandle(
87+
"Checking for new exercises");
88+
exerciseRefresh.start();
8889
ListenableFuture<Course> currentCourseFuture = this.tmcCore.getCourse(
89-
NBTmcSettings.getDefault(), currentCourseBeforeUpdate.getDetailsUrl()
90+
NBTmcSettings.getDefault(), detailUrl(currentCourseBeforeUpdate)
9091
);
9192
Futures.addCallback(currentCourseFuture, new UpdateCourseForExerciseUpdate(exerciseRefresh));
9293
} catch (TmcCoreException ex) {
9394
Exceptions.printStackTrace(ex);
9495
}
9596
}
9697

98+
private String detailUrl(final Course currentCourseBeforeUpdate) {
99+
return ServerAccess.addApiCallQueryParameters(
100+
currentCourseBeforeUpdate.getDetailsUrl()
101+
);
102+
}
103+
97104
/**
98105
* If there is something at background or no current course is chosen,
99106
* return true.
@@ -127,15 +134,21 @@ public UpdateCourseForExerciseUpdate(ProgressHandle lastAction) {
127134
public void onSuccess(Course receivedCourse) {
128135
lastAction.finish();
129136
if (receivedCourse != null) {
137+
setCourseNameToAllExercises(receivedCourse);
130138
courseDb.putDetailedCourse(receivedCourse);
131139
final LocalExerciseStatus status = LocalExerciseStatus.get(receivedCourse.getExercises());
132140
updateGUI(status);
133141
}
134142
}
135143

144+
private void setCourseNameToAllExercises(Course receivedCourse) {
145+
for (Exercise exercise : receivedCourse.getExercises()) {
146+
exercise.setCourseName(receivedCourse.getName());
147+
}
148+
}
149+
136150
private void updateGUI(final LocalExerciseStatus status) {
137151
boolean thereIsSomethingToDownload = status.thereIsSomethingToDownload(false);
138-
System.out.println("on ladattavaa: " + thereIsSomethingToDownload);
139152
if (thereIsSomethingToDownload) {
140153
if (beQuiet) {
141154
displayNotification(status, new ActionListener() {

tmc-plugin/src/fi/helsinki/cs/tmc/actions/DownloadExercisesAction.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.common.util.concurrent.FutureCallback;
44
import com.google.common.util.concurrent.Futures;
55
import com.google.common.util.concurrent.ListenableFuture;
6+
import fi.helsinki.cs.tmc.model.CourseDb;
67
import hy.tmc.core.domain.Exercise;
78
import fi.helsinki.cs.tmc.model.NBTmcSettings;
89
import fi.helsinki.cs.tmc.model.ProjectMediator;
@@ -11,12 +12,15 @@
1112
import fi.helsinki.cs.tmc.ui.ConvenientDialogDisplayer;
1213
import hy.tmc.core.TmcCore;
1314
import hy.tmc.core.exceptions.TmcCoreException;
15+
import java.lang.reflect.InvocationTargetException;
1416
import java.util.ArrayList;
1517
import java.util.List;
1618
import java.util.logging.Level;
1719
import java.util.logging.Logger;
20+
import javax.swing.SwingUtilities;
1821
import org.netbeans.api.progress.ProgressHandle;
1922
import org.netbeans.api.progress.ProgressHandleFactory;
23+
import org.openide.util.Exceptions;
2024

2125
/**
2226
* Downloads and opens the given exercises in the background.
@@ -46,20 +50,26 @@ public void run() throws TmcCoreException {
4650
// = new AggregatingBgTaskListener<TmcProjectInfo>(exercisesToDownload.size(), whenAllDownloadsFinished);
4751

4852
ProgressHandle exerciseDownload = ProgressHandleFactory.createSystemHandle(
49-
"Downloading " + exercisesToDownload.size() + " exercises.");
53+
"Downloading " + exercisesToDownload.size() + " exercises.");
5054
exerciseDownload.start();
5155
ListenableFuture<List<Exercise>> dlFuture = tmcCore.downloadExercises(exercisesToDownload, settings);
5256

5357
Futures.addCallback(dlFuture, new ProjectOpener(exerciseDownload));
5458
}
5559

5660
private class ProjectOpener implements FutureCallback<List<Exercise>> {
57-
private ProgressHandle lastAction;
5861

62+
private ProgressHandle lastAction;
63+
64+
/**
65+
* Converts Exercise objects to TmcProjectInfo objects.
66+
* Saves them to CourseDb and opens them.
67+
* @param lastAction
68+
*/
5969
public ProjectOpener(ProgressHandle lastAction) {
6070
this.lastAction = lastAction;
6171
}
62-
72+
6373
@Override
6474
public void onSuccess(List<Exercise> downloadedExercises) {
6575
lastAction.finish();
@@ -71,9 +81,25 @@ public void onSuccess(List<Exercise> downloadedExercises) {
7181
}
7282
projects.add(info);
7383
}
84+
saveDownloadedExercisesToCourseDb(downloadedExercises);
7485
projectMediator.openProjects(projects);
7586
}
7687

88+
private void saveDownloadedExercisesToCourseDb(final List<Exercise> downloadedExercises) {
89+
try {
90+
SwingUtilities.invokeAndWait(new Runnable() {
91+
@Override
92+
public void run() {
93+
CourseDb.getInstance().multipleExerciseDownloaded(downloadedExercises);
94+
}
95+
});
96+
} catch (InterruptedException ex) {
97+
Exceptions.printStackTrace(ex);
98+
} catch (InvocationTargetException ex) {
99+
Exceptions.printStackTrace(ex);
100+
}
101+
}
102+
77103
@Override
78104
public void onFailure(Throwable thrwbl) {
79105
lastAction.finish();

tmc-plugin/src/fi/helsinki/cs/tmc/model/CourseDb.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,21 @@ public String getDownloadedExerciseChecksum(ExerciseKey ex) {
166166
* Sets the downloaded checksum of the exercise to be the one reported by the server.
167167
*/
168168
public void exerciseDownloaded(Exercise ex) {
169-
downloadedExerciseChecksums.put(ex.getKey(), ex.getChecksum());
169+
List<Exercise> exercises = new ArrayList<Exercise>();
170+
exercises.add(ex);
171+
this.multipleExerciseDownloaded(exercises);
172+
}
173+
174+
/**
175+
* Informs the course database that the exercises are considered downloaded.
176+
*
177+
* <p>
178+
* Sets the downloaded checksums of the exercises to be the ones reported by the server.
179+
*/
180+
public void multipleExerciseDownloaded(List<Exercise> exercises) {
181+
for (Exercise ex : exercises) {
182+
downloadedExerciseChecksums.put(ex.getKey(), ex.getChecksum());
183+
}
170184
save();
171185
}
172186

tmc-plugin/src/fi/helsinki/cs/tmc/model/LocalExerciseStatus.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ private LocalExerciseStatus(CourseDb courseDb, ProjectMediator projectMediator,
3131

3232
for (Exercise ex : allExercises) {
3333
if (!ex.hasDeadlinePassed()) {
34-
System.out.println("Exercise: " + ex);
3534
TmcProjectInfo proj = projectMediator.tryGetProjectForExercise(ex);
3635
boolean isDownloaded = proj != null;
3736
if (courseDb.isUnlockable(ex)) {

tmc-plugin/src/fi/helsinki/cs/tmc/model/ProjectMediator.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,12 @@ public TmcProjectInfo tryGetProjectOwningFile(FileObject fo) {
161161
*/
162162
public TmcProjectInfo tryGetProjectForExercise(Exercise exercise) {
163163
projectManager.clearNonProjectCache(); // Just to be sure.
164-
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
165164

166165
File path = getProjectDirForExercise(exercise);
167166
FileObject fo = FileUtil.toFileObject(path);
168-
System.out.println("PATH: " + path);
169-
System.out.println("FIlEOBJECT: " + fo);
170167
if (fo != null) {
171168
try {
172169
Project project = projectManager.findProject(fo);
173-
System.out.println("PROJECT: " + project);
174170
if (project != null) {
175171
return wrapProject(project);
176172
} else {
@@ -185,9 +181,6 @@ public TmcProjectInfo tryGetProjectForExercise(Exercise exercise) {
185181
return null;
186182
}
187183
} else {
188-
logger.log(Level.WARNING, "ProjectMediator failed to convert "
189-
+ "File to org.openide.filesystems.FileObject. Check "
190-
+ "http://bits.netbeans.org/7.4/javadoc/org-openide-filesystems/org/openide/filesystems/FileUtil.html#toFileObject(java.io.File)");
191184
return null;
192185
}
193186
}

tmc-plugin/src/fi/helsinki/cs/tmc/model/ServerAccess.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ private String getCourseListUrl() {
8484
return addApiCallQueryParameters(settings.getServerAddress() + "/courses.json");
8585
}
8686

87-
private String addApiCallQueryParameters(String url) {
87+
public static String addApiCallQueryParameters(String url) {
8888
url = UriUtils.withQueryParam(url, "api_version", ""+API_VERSION);
8989
url = UriUtils.withQueryParam(url, "client", "netbeans_plugin");
90-
url = UriUtils.withQueryParam(url, "client_version", clientVersion);
90+
url = UriUtils.withQueryParam(url, "client_version", getClientVersion());
9191
return url;
9292
}
9393

0 commit comments

Comments
 (0)