11package fi .helsinki .cs .tmc .exerciseSubmitter ;
22
3+ import com .google .common .base .Strings ;
4+ import com .google .common .util .concurrent .ListenableFuture ;
35import fi .helsinki .cs .tmc .actions .CheckForNewExercisesOrUpdates ;
46import fi .helsinki .cs .tmc .actions .ServerErrorHelper ;
57import fi .helsinki .cs .tmc .actions .SubmitExerciseAction ;
1113import fi .helsinki .cs .tmc .model .CourseDb ;
1214import fi .helsinki .cs .tmc .model .ProjectMediator ;
1315import fi .helsinki .cs .tmc .model .ServerAccess ;
14- import fi .helsinki .cs .tmc .model .SubmissionResultWaiter ;
1516import fi .helsinki .cs .tmc .model .TmcProjectInfo ;
1617import fi .helsinki .cs .tmc .model .NbTmcSettings ;
18+ import fi .helsinki .cs .tmc .model .TmcCoreSingleton ;
1719import fi .helsinki .cs .tmc .ui .ConvenientDialogDisplayer ;
1820import fi .helsinki .cs .tmc .ui .SubmissionResultWaitingDialog ;
1921import fi .helsinki .cs .tmc .ui .TestResultDisplayer ;
2022import fi .helsinki .cs .tmc .utilities .BgTask ;
2123import fi .helsinki .cs .tmc .utilities .BgTaskListener ;
2224import fi .helsinki .cs .tmc .utilities .CancellableCallable ;
23- import fi .helsinki .cs .tmc .utilities .zip .RecursiveZipper ;
24-
25- import java .util .HashMap ;
26- import java .util .Map ;
27- import java .util .concurrent .Callable ;
2825import java .util .logging .Level ;
2926import java .util .logging .Logger ;
3027
@@ -35,7 +32,9 @@ public class ExerciseSubmitter {
3532 private static final Logger log = Logger .getLogger (SubmitExerciseAction .class .getName ());
3633
3734 public static class InvokedEvent implements TmcEvent {
35+
3836 public final TmcProjectInfo projectInfo ;
37+
3938 public InvokedEvent (TmcProjectInfo projectInfo ) {
4039 this .projectInfo = projectInfo ;
4140 }
@@ -59,8 +58,7 @@ public ExerciseSubmitter() {
5958 this .eventBus = TmcEventBus .getDefault ();
6059 }
6160
62-
63- public void performAction (Project ... projects ) {
61+ public void performAction (Project ... projects ) {
6462 for (Project nbProject : projects ) {
6563 TmcProjectInfo tmcProject = projectMediator .wrapProject (nbProject );
6664 eventBus .post (new InvokedEvent (tmcProject ));
@@ -77,53 +75,48 @@ private void submitProject(final TmcProjectInfo project) {
7775 projectMediator .saveAllFiles ();
7876
7977 // Oh what a mess :/
80-
8178 final SubmissionResultWaitingDialog dialog = SubmissionResultWaitingDialog .createAndShow ();
8279
83- final BgTaskListener <ServerAccess .SubmissionResponse > submissionListener = new BgTaskListener <ServerAccess .SubmissionResponse >() {
84- @ Override
85- public void bgTaskReady (ServerAccess .SubmissionResponse response ) {
86- final SubmissionResultWaiter waitingTask = new SubmissionResultWaiter (response .submissionUrl .toString (), dialog );
87- dialog .setTask (waitingTask );
80+ CancellableCallable <SubmissionResult > submitAndPollTask = new CancellableCallable <SubmissionResult >() {
8881
89- BgTask . start ( "Waiting for results from server." , waitingTask , new BgTaskListener <SubmissionResult >() {
82+ ListenableFuture <SubmissionResult > lf ;
9083
91- @ Override
92- public void bgTaskReady (SubmissionResult result ) {
84+ @ Override
85+ public SubmissionResult call () throws Exception {
86+ lf = TmcCoreSingleton .getInstance ().submit (project .getProjectDirAsPath ());
87+ return lf .get ();
88+ }
9389
94- dialog .close ();
90+ @ Override
91+ public boolean cancel () {
92+ return lf .cancel (true );
93+ }
94+ };
9595
96- final ResultCollector resultCollector = new ResultCollector (exercise );
97- resultCollector .setValidationResult (result .getValidationResult ());
98- resultDisplayer .showSubmissionResult (exercise , result , resultCollector );
96+ dialog .setTask (submitAndPollTask );
9997
100- // We change exercise state as a first approximation,
101- // then refresh from the server and potentially notify the user
102- // as we might have unlocked new exercises.
103- exercise .setAttempted (true );
98+ BgTask .start ("Submitting and waiting for results from server." , submitAndPollTask , new BgTaskListener <SubmissionResult >() {
10499
105- if (result .getStatus () == SubmissionResult .Status .OK ) {
106- exercise .setCompleted (true );
107- }
100+ @ Override
101+ public void bgTaskReady (SubmissionResult result ) {
102+ dialog .close ();
103+
104+ final ResultCollector resultCollector = new ResultCollector (exercise );
105+ resultCollector .setValidationResult (result .getValidationResult ());
106+ resultDisplayer .showSubmissionResult (exercise , result , resultCollector );
108107
109- courseDb .save ();
108+ // We change exercise state as a first approximation,
109+ // then refresh from the server and potentially notify the user
110+ // as we might have unlocked new exercises.
111+ exercise .setAttempted (true );
110112
111- new CheckForNewExercisesOrUpdates (true , false ).run ();
112- }
113+ if (result .getStatus () == SubmissionResult .Status .OK ) {
114+ exercise .setCompleted (true );
115+ }
113116
114- @ Override
115- public void bgTaskCancelled () {
116- dialog .close ();
117- }
117+ courseDb .save ();
118118
119- @ Override
120- public void bgTaskFailed (Throwable ex ) {
121- log .log (Level .INFO , "Error waiting for results from server." , ex );
122- String msg = ServerErrorHelper .getServerExceptionMsg (ex );
123- dialogDisplayer .displayError ("Error trying to get test results." , ex );
124- dialog .close ();
125- }
126- });
119+ new CheckForNewExercisesOrUpdates (true , false ).run ();
127120 }
128121
129122 @ Override
@@ -133,41 +126,13 @@ public void bgTaskCancelled() {
133126
134127 @ Override
135128 public void bgTaskFailed (Throwable ex ) {
136- log .log (Level .INFO , "Error submitting exercise ." , ex );
129+ log .log (Level .INFO , "Error waiting for results from server ." , ex );
137130 String msg = ServerErrorHelper .getServerExceptionMsg (ex );
138- dialogDisplayer .displayError ("Error submitting exercise." , ex );
131+ if (!Strings .isNullOrEmpty (msg )) {
132+ dialogDisplayer .displayError ("Error trying to get test results." , ex );
133+ }
139134 dialog .close ();
140135 }
141- };
142-
143- final String errorMsgLocale = settings .getErrorMsgLocale ().toString ();
144-
145- BgTask .start ("Zipping up " + exercise .getName (), new Callable <byte []>() {
146- @ Override
147- public byte [] call () throws Exception {
148- RecursiveZipper zipper = new RecursiveZipper (project .getProjectDirAsFile (), project .getZippingDecider ());
149- return zipper .zipProjectSources ();
150- }
151- }, new BgTaskListener <byte []>() {
152- @ Override
153- public void bgTaskReady (byte [] zipData ) {
154- Map <String , String > extraParams = new HashMap <String , String >();
155- extraParams .put ("error_msg_locale" , errorMsgLocale );
156-
157- CancellableCallable <ServerAccess .SubmissionResponse > submitTask = serverAccess .getSubmittingExerciseTask (exercise , zipData , extraParams );
158- dialog .setTask (submitTask );
159- BgTask .start ("Sending " + exercise .getName (), submitTask , submissionListener );
160- }
161-
162- @ Override
163- public void bgTaskCancelled () {
164- submissionListener .bgTaskCancelled ();
165- }
166-
167- @ Override
168- public void bgTaskFailed (Throwable ex ) {
169- submissionListener .bgTaskFailed (ex );
170- }
171136 });
172137 }
173- }
138+ }
0 commit comments