-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathSendFeedbackAction.java
More file actions
81 lines (67 loc) · 2.85 KB
/
SendFeedbackAction.java
File metadata and controls
81 lines (67 loc) · 2.85 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
package fi.helsinki.cs.tmc.actions;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import fi.helsinki.cs.tmc.data.FeedbackAnswer;
import fi.helsinki.cs.tmc.model.NbTmcSettings;
import fi.helsinki.cs.tmc.model.TmcCoreSingleton;
import fi.helsinki.cs.tmc.ui.ConvenientDialogDisplayer;
import fi.helsinki.cs.tmc.utilities.ExceptionUtils;
import fi.helsinki.cs.tmc.core.TmcCore;
import fi.helsinki.cs.tmc.core.communication.HttpResult;
import fi.helsinki.cs.tmc.core.domain.submission.SubmissionResult;
import fi.helsinki.cs.tmc.core.exceptions.TmcCoreException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openide.util.Exceptions;
public class SendFeedbackAction {
private static final Logger log = Logger.getLogger(SendFeedbackAction.class.getName());
private final SubmissionResult result;
private List<FeedbackAnswer> answers;
private TmcCore core;
private ConvenientDialogDisplayer dialogs;
private NbTmcSettings settings = NbTmcSettings.getDefault();
public SendFeedbackAction(List<FeedbackAnswer> answers, SubmissionResult result) {
this.answers = answers;
this.core = TmcCoreSingleton.getInstance();
this.dialogs = ConvenientDialogDisplayer.getDefault();
this.result = result;
}
public void run() {
try {
ListenableFuture<HttpResult> feedbackFuture;
feedbackFuture =
core.sendFeedback(getFeedbackAnswers(), new URI(result.getFeedbackAnswerUrl()));
Futures.addCallback(feedbackFuture, new FeedbackReplyCallback());
} catch (TmcCoreException ex) {
Exceptions.printStackTrace(ex);
} catch (URISyntaxException ex) {
Exceptions.printStackTrace(ex);
}
}
private Map<String, String> getFeedbackAnswers() {
Map<String, String> answerMap = new HashMap<String, String>();
for (FeedbackAnswer answer : answers) {
answerMap.put("" + answer.getQuestion().getId(), answer.getAnswer());
}
return answerMap;
}
private class FeedbackReplyCallback implements FutureCallback<HttpResult> {
@Override
public void onSuccess(HttpResult v) {
System.out.println(v.getData() + " " + v.getStatusCode());
}
@Override
public void onFailure(Throwable ex) {
String msg = "Failed to send feedback :-(\n" + ServerErrorHelper.getServerExceptionMsg(ex);
String msgWithBacktrace = msg + "\n" + ExceptionUtils.backtraceToString(ex);
log.log(Level.INFO, msgWithBacktrace);
dialogs.displayError(msg);
}
}
}