View Javadoc

1   /**
2    *
3    */
4   package org.appfuse.webapp.client.ui.users.updatePassword;
5   
6   import java.util.Set;
7   
8   import javax.validation.ConstraintViolation;
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.proxies.UserProxy;
13  import org.appfuse.webapp.client.ui.home.HomePlace;
14  import org.appfuse.webapp.client.ui.login.LoginPlace;
15  import org.appfuse.webapp.client.ui.users.updatePassword.UpdatePasswordView.UserCredentials;
16  
17  import com.github.gwtbootstrap.client.ui.constants.AlertType;
18  import com.google.gwt.editor.client.EditorDriver;
19  import com.google.gwt.event.shared.EventBus;
20  import com.google.gwt.user.client.ui.AcceptsOneWidget;
21  import com.google.inject.Inject;
22  import com.google.web.bindery.requestfactory.shared.Receiver;
23  
24  /**
25   * @author ivangsa
26   *
27   */
28  public class UpdatePasswordActivity extends AbstractBaseActivity implements UpdatePasswordView.Delegate {
29  
30      private final UpdatePasswordView view;
31  
32      @Inject
33      public UpdatePasswordActivity(final Application application, final UpdatePasswordView view) {
34          super(application);
35          this.view = view;
36          setTitle(i18n.updatePassword_title());
37      }
38  
39      /**
40       * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
41       *      com.google.gwt.event.shared.EventBus)
42       */
43      @Override
44      public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
45          view.setDelegate(this);
46          view.setWaiting(false);
47          view.setUserCredentials(((UpdatePasswordPlace) currentPlace).getUserCredentials());
48          panel.setWidget(view);
49          setDocumentTitleAndBodyAttributtes();
50      }
51  
52      @Override
53      public void onUpdatePasswordClick() {
54          final EditorDriver<UserCredentials> editorDriver = view.getEditorDriver();
55          final UserCredentials userCredentials = editorDriver.flush();
56          final Set<ConstraintViolation<UserCredentials>> violations = getValidator().validate(userCredentials);
57          if (!violations.isEmpty()) {
58              editorDriver.setConstraintViolations((Set) violations);
59              return;
60          }
61  
62          final boolean isUsingToken = userCredentials.getToken() != null && !"".equals(userCredentials.getToken().trim());
63  
64          requests.userRequest().updatePassword(
65                  userCredentials.getUsername(),
66                  userCredentials.getToken(),
67                  userCredentials.getCurrentPassword(),
68                  userCredentials.getPassword())
69                  .fire(new Receiver<UserProxy>() {
70  
71                      @Override
72                      public void onSuccess(final UserProxy response) {
73                          if (response == null) {
74                              if (isUsingToken) {
75                                  placeController.goTo(new LoginPlace());
76                                  shell.addMessage(i18n.updatePassword_invalidToken(), AlertType.ERROR);
77                              } else {
78                                  shell.addMessage(i18n.updatePassword_invalidPassword(), AlertType.ERROR);
79                              }
80                          } else {
81                              placeController.goTo(new HomePlace());
82                              shell.addMessage(i18n.updatePassword_success(), AlertType.SUCCESS);
83                          }
84                      }
85                  });
86      }
87  
88      @Override
89      public void onCancelClick() {
90          placeController.goTo(getBackButtonPlace());
91      }
92  }