-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathRequestReviewAction.java
More file actions
224 lines (195 loc) · 8.73 KB
/
Copy pathRequestReviewAction.java
File metadata and controls
224 lines (195 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package fi.helsinki.cs.tmc.actions;
import fi.helsinki.cs.tmc.core.domain.Exercise;
import fi.helsinki.cs.tmc.events.TmcEventBus;
import fi.helsinki.cs.tmc.model.CourseDb;
import fi.helsinki.cs.tmc.model.ProjectMediator;
import fi.helsinki.cs.tmc.model.ServerAccess;
import fi.helsinki.cs.tmc.model.TmcProjectInfo;
import fi.helsinki.cs.tmc.model.NbTmcSettings;
import fi.helsinki.cs.tmc.spyware.LoggableEvent;
import fi.helsinki.cs.tmc.ui.CodeReviewRequestDialog;
import fi.helsinki.cs.tmc.ui.ConvenientDialogDisplayer;
import fi.helsinki.cs.tmc.utilities.BgTask;
import fi.helsinki.cs.tmc.utilities.BgTaskListener;
import fi.helsinki.cs.tmc.utilities.CancellableCallable;
import fi.helsinki.cs.tmc.utilities.zip.RecursiveZipper;
import com.google.gson.Gson;
import org.netbeans.api.project.Project;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.nodes.Node;
import org.openide.util.NbBundle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;
@ActionID(category = "TMC", id = "fi.helsinki.cs.tmc.actions.RequestReviewAction")
@ActionRegistration(displayName = "#CTL_RequestReviewAction", lazy = false)
@ActionReferences({
@ActionReference(path = "Menu/TM&C", position = -5, separatorAfter = 0),
@ActionReference(
path = "Projects/Actions",
position = 1350,
separatorBefore = 1330,
separatorAfter = 1360
) // Positioning y u no work?
})
@NbBundle.Messages("CTL_RequestReviewAction=Request code review")
public class RequestReviewAction extends AbstractExerciseSensitiveAction {
private static final Logger log = Logger.getLogger(RequestReviewAction.class.getName());
private NbTmcSettings settings;
private CourseDb courseDb;
private ProjectMediator projectMediator;
private ConvenientDialogDisplayer dialogs;
private TmcEventBus eventBus;
public RequestReviewAction() {
this.settings = NbTmcSettings.getDefault();
this.courseDb = CourseDb.getInstance();
this.projectMediator = ProjectMediator.getInstance();
this.dialogs = ConvenientDialogDisplayer.getDefault();
this.eventBus = TmcEventBus.getDefault();
}
@Override
protected ProjectMediator getProjectMediator() {
return projectMediator;
}
@Override
protected CourseDb getCourseDb() {
return courseDb;
}
@Override
protected boolean enabledFor(Exercise exercise) {
return exercise.isCodeReviewRequestsEnabled() && super.enabledFor(exercise);
}
@Override
protected void performAction(Node[] nodes) {
// TODO(jamo): use bg task
List<Project> project = projectsFromNodes(nodes);
if (project.size() == 1) {
TmcProjectInfo projectInfo = projectMediator.wrapProject(project.get(0));
Exercise exercise = projectMediator.tryGetExerciseForProject(projectInfo, courseDb);
if (exercise != null) {
showReviewRequestDialog(projectInfo, exercise);
} else {
log.log(
Level.WARNING,
"RequestReviewAction called in a context without a valid TMC project.");
}
} else {
log.log(
Level.WARNING,
"RequestReviewAction called in a context with {0} projects",
project.size());
}
}
private void showReviewRequestDialog(
final TmcProjectInfo projectInfo, final Exercise exercise) {
final CodeReviewRequestDialog dialog = new CodeReviewRequestDialog(exercise);
dialog.setOkListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String message = dialog.getMessageForReviewer().trim();
requestCodeReviewFor(projectInfo, exercise, message);
}
});
dialog.setVisible(true);
}
private void requestCodeReviewFor(
final TmcProjectInfo projectInfo,
final Exercise exercise,
final String messageForReviewer) {
projectMediator.saveAllFiles();
final String errorMsgLocale = settings.getErrorMsgLocale().toString();
BgTask.start(
"Zipping up " + exercise.getName(),
new Callable<byte[]>() {
@Override
public byte[] call() throws Exception {
RecursiveZipper zipper =
new RecursiveZipper(
projectInfo.getProjectDirAsFile(),
projectInfo.getZippingDecider());
return zipper.zipProjectSources();
}
},
new BgTaskListener<byte[]>() {
@Override
public void bgTaskReady(byte[] zipData) {
Map<String, String> extraParams = new HashMap<String, String>();
extraParams.put("error_msg_locale", errorMsgLocale);
extraParams.put("request_review", "1");
if (!messageForReviewer.isEmpty()) {
extraParams.put("message_for_reviewer", messageForReviewer);
}
final ServerAccess sa = new ServerAccess();
CancellableCallable<ServerAccess.SubmissionResponse> submitTask =
sa.getSubmittingExerciseTask(exercise, zipData, extraParams);
BgTask.start(
"Sending " + exercise.getName(),
submitTask,
new BgTaskListener<ServerAccess.SubmissionResponse>() {
@Override
public void bgTaskReady(
ServerAccess.SubmissionResponse result) {
sendLoggableEvent(exercise, result.submissionUrl);
dialogs.displayMessage(
"Code submitted for review.\n"
+ "You will be notified when an instructor has reviewed your code.");
}
@Override
public void bgTaskCancelled() {}
@Override
public void bgTaskFailed(Throwable ex) {
dialogs.displayError(
"Failed to submit exercise for code review \n"
+ ServerErrorHelper.getServerExceptionMsg(ex));
}
});
}
@Override
public void bgTaskCancelled() {}
@Override
public void bgTaskFailed(Throwable ex) {
dialogs.displayError("Failed to zip up exercise", ex);
}
});
}
@Override
public String getName() {
return "Request code review";
}
private void sendLoggableEvent(Exercise exercise, URI submissionUrl) {
String courseName = courseDb.getCurrentCourseName();
if (courseName != null) {
ReviewRequested dataObject = new ReviewRequested(exercise, submissionUrl);
String json = new Gson().toJson(dataObject);
byte[] jsonBytes = json.getBytes(Charset.forName("UTF-8"));
LoggableEvent event =
new LoggableEvent(
exercise.getCourseName(),
exercise.getName(),
"review_requested",
jsonBytes);
eventBus.post(event);
}
}
private static class ReviewRequested {
public final String courseName;
public final String exerciseName;
public final String submissionUrl;
private ReviewRequested(Exercise exercise, URI submissionUrl) {
this.courseName = exercise.getCourseName();
this.exerciseName = exercise.getName();
this.submissionUrl = submissionUrl.toString();
}
}
}