View Javadoc

1   package org.appfuse.webapp.pages;
2   
3   import de.agilecoders.wicket.core.markup.html.bootstrap.common.NotificationMessage;
4   import org.apache.wicket.Page;
5   import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
6   import org.apache.wicket.model.IModel;
7   import org.apache.wicket.model.Model;
8   import org.apache.wicket.model.StringResourceModel;
9   import org.appfuse.model.User;
10  import org.appfuse.service.UserExistsException;
11  
12  /**
13   * Page for editing a current user from list and an administrative addition of a new user.
14   *
15   * @author Marcin ZajÄ…czkowski, 2011-03-12
16   */
17  @AuthorizeInstantiation({"ROLE_ADMIN"})
18  public class FromListUserEdit extends AbstractUserEdit {
19  
20      private static final String USER_PROFILE_PROPERTY_PREFIX = "userProfile.admin";
21  
22      /**
23       * Constructor for adding a new user.
24       *
25       * @param responsePage page to come back
26       */
27      @Deprecated //rather user and proper model should be created in the place which calls that action
28      public FromListUserEdit(Page responsePage) {
29          super(responsePage, USER_PROFILE_PROPERTY_PREFIX, new Model<User>());
30          //TODO: MZA: Here or in onInitialize?
31          setUser(new User());
32      }
33  
34      /**
35       * Constructor for editing an existing user.
36       *
37       * @param responsePage page to come back
38       * @param userModel model for editing user
39       */
40      public FromListUserEdit(Page responsePage, IModel<User> userModel) {
41          super(responsePage, USER_PROFILE_PROPERTY_PREFIX, userModel);
42      }
43  
44      protected void onSaveButtonSubmit() {
45          User user = getUser();
46  
47          log.info("onSubmit: {}", user);
48          log.info("onSubmit (address): {}", user.getAddress());
49  
50          try {
51              getUserManager().saveUser(user);
52              getSession().info(createDefaultInfoNotificationMessage(
53                      new StringResourceModel("user.added", this, null, new Object[]{user.getFullName()})));
54              resolveAndSetResponsePage();
55          } catch (UserExistsException e) {
56              log.warn("User already exists", e);
57              error(new NotificationMessage(new StringResourceModel("errors.existing.user", this, null, new Object[] {
58                      user.getUsername(), user.getEmail()})));
59          }
60      }
61  
62      @Override
63      protected void onDeleteButtonSubmit() {
64          User editedUser = getUser();
65          String fullName = editedUser.getFullName();
66          log.debug("deleting user: {}, {}", editedUser.getId(), fullName);
67          getUserManager().removeUser(editedUser.getId().toString());
68          getSession().info(new StringResourceModel(
69                  "user.deleted", this, null, new Object[] {fullName}).getString());
70          resolveAndSetResponsePage();
71      }
72  
73      @Override
74      protected boolean getDisplayRolesGroupVisibility() {
75          return false;
76      }
77  
78      @Override
79      protected boolean getAccountSettingsGroupVisibility() {
80          return true;
81      }
82  
83      @Override
84      protected boolean getDeleteButtonVisibility() {
85          return isExistingUserEdited();
86      }
87  
88      private boolean isExistingUserEdited() {
89          //Quite not elegant, but it seems there is not better way to determine if User is a new user (after add button)
90          return getUser().getFirstName() != null;
91      }
92  }