View Javadoc

1   /**
2    * 
3    */
4   package org.appfuse.webapp.client.ui.upload;
5   
6   import java.util.ArrayList;
7   import java.util.List;
8   import java.util.Set;
9   
10  import org.appfuse.webapp.client.application.Application;
11  import org.appfuse.webapp.client.application.base.activity.AbstractBaseActivity;
12  import org.appfuse.webapp.client.ui.home.HomePlace;
13  import org.appfuse.webapp.client.ui.upload.views.FileUploadView;
14  import org.appfuse.webapp.client.ui.upload.views.UploadedFileView;
15  
16  import com.github.gwtbootstrap.client.ui.Form.SubmitCompleteEvent;
17  import com.github.gwtbootstrap.client.ui.Form.SubmitEvent;
18  import com.github.gwtbootstrap.client.ui.constants.AlertType;
19  import com.google.gwt.event.shared.EventBus;
20  import com.google.gwt.user.client.ui.AcceptsOneWidget;
21  import com.google.inject.Inject;
22  
23  /**
24   * @author ivangsa
25   *
26   */
27  public class FileUploadActivity extends AbstractBaseActivity implements FileUploadView.Delegate, UploadedFileView.Delegate {
28  
29      private final FileUploadView formView;
30      private final UploadedFileView resultsView;
31      private AcceptsOneWidget panel;
32  
33      @Inject
34      public FileUploadActivity(final Application application, final FileUploadView formView, final UploadedFileView resultsView) {
35          super(application);
36          this.formView = formView;
37          this.resultsView = resultsView;
38          setTitle(i18n.upload_title());
39      }
40  
41      /*
42       * (non-Javadoc)
43       * 
44       * @see
45       * com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client
46       * .ui.AcceptsOneWidget, com.google.gwt.event.shared.EventBus)
47       */
48      @Override
49      public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
50          this.panel = panel;
51          showForm();
52          setDocumentTitleAndBodyAttributtes();
53      }
54  
55      private void showForm() {
56          formView.setDelegate(this);
57          formView.edit(new FileUploadBean());
58          panel.setWidget(formView);
59      }
60  
61      @Override
62      public void onSubmit(final SubmitEvent event) {
63          if (formView != null) {
64              final FileUploadBean fileUpload = formView.getEditorDriver().flush();
65              final Set violations = getValidator().validate(fileUpload);
66              formView.getEditorDriver().setConstraintViolations(violations);
67              if (!violations.isEmpty()) {
68                  event.cancel();
69              }
70          }
71      }
72  
73      @Override
74      public void onSubmitComplete(final SubmitCompleteEvent event) {
75          final UploadedFileBean uploadedFile = parseResponse(event.getResults());
76          if (uploadedFile.getErrorMessages() != null && uploadedFile.getErrorMessages().length() > 0) {
77              final List<String> errorMessages = new ArrayList<String>();
78              for (int i = 0; i < uploadedFile.getErrorMessages().length(); i++) {
79                  errorMessages.add(uploadedFile.getErrorMessages().get(i));
80              }
81              formView.showErrorsMessages(errorMessages);
82          } else {
83              shell.addMessage(uploadedFile.getLocation(), AlertType.SUCCESS);
84              showResults(uploadedFile);
85          }
86      }
87  
88      private void showResults(final UploadedFileBean uploadedFile) {
89          resultsView.setDelegate(this);
90          resultsView.display(uploadedFile);
91          panel.setWidget(resultsView);
92      }
93  
94      @Override
95      public void onDoneClick() {
96          placeController.goTo(new HomePlace());
97      }
98  
99      @Override
100     public void onCancelClick() {
101         placeController.goTo(new HomePlace());
102     }
103 
104     @Override
105     public void onUploadAnotherFileClick() {
106         showForm();
107     }
108 
109     private final native UploadedFileBean parseResponse(String json) /*-{
110                                                                      return eval('(' + json + ')');
111                                                                      }-*/;
112 }