View Javadoc
1   package org.itracker.web.util;
2   
3   import org.apache.commons.beanutils.PropertyUtils;
4   import org.apache.log4j.Logger;
5   import org.apache.struts.action.ActionForm;
6   import org.apache.struts.action.ActionMessages;
7   import org.itracker.model.*;
8   import org.itracker.model.util.UserUtilities;
9   import org.itracker.services.ProjectService;
10  import org.itracker.services.UserService;
11  
12  import java.lang.reflect.InvocationTargetException;
13  import java.util.*;
14  
15  /**
16   * Helper utils for admin project actions.
17   */
18  public class AdminProjectUtilities {
19      private static final Logger log = Logger.getLogger(AdminProjectUtilities.class);
20  
21      /**
22       * When creating project, initial set of users with specific set of rights
23       * can be defined.
24       *
25       * @param project        the project
26       * @param userIds        the user IDs
27       * @param permissions    the permissions
28       * @param projectService the project service
29       * @param userService    the user service
30       */
31      public static void handleInitialProjectMembers(Project project,
32                                                     Set<Integer> userIds, Set<Integer> permissions,
33                                                     ProjectService projectService, UserService userService) {
34          List<Permission> userPermissionModels;
35          if (userIds != null && permissions != null && userIds.size() > 0
36                  && permissions.size() > 0) {
37  
38              Set<User> users = new HashSet<>(userIds.size());
39              for (Integer userId : userIds)
40                  users.add(userService.getUser(userId));
41  
42              // process member-users
43              for (User user : users) {
44                  userPermissionModels = userService.getUserPermissionsLocal(user);
45  
46                  for (Integer type : permissions)
47                      userPermissionModels.add(new Permission(PermissionType.valueOf(type), user, project));
48  
49                  // save the permissions
50                  userService.setUserPermissions(user.getId(), userPermissionModels);
51                  userService.updateAuthenticator(user.getId(), userPermissionModels);
52              }
53          }
54  
55      }
56  
57  
58      /**
59       * Setup permissions for updated project-owners.
60       *
61       * @param project     the project
62       * @param userIds     the user IDs
63       * @param userService the user service
64       */
65      public static void updateProjectOwners(Project project,
66                                                   Set<Integer> userIds, ProjectService projectService,
67                                                   UserService userService) {
68          Set<Permission> userPermissionModels;
69  
70          if (log.isDebugEnabled()) {
71              log.debug("updateProjectOwners: setting new owners: " + userIds);
72          }
73  
74          // add all defined owners to project
75          for (Integer userId : userIds) {
76              User usermodel = userService.getUser(userId);
77              boolean newPermissions = false;
78              userPermissionModels = new HashSet<>(userService.getUserPermissionsLocal(usermodel));
79              if (log.isDebugEnabled()) {
80                  log.debug("updateProjectOwners: setting owner " + usermodel + " to " + project);
81              }
82              for (Integer permission : UserUtilities.ALL_PERMISSIONS_SET) {
83                  if (userPermissionModels.add(new Permission(PermissionType.valueOf(permission),
84                          usermodel, project))) {
85                      newPermissions = true;
86                  }
87              }
88              if (newPermissions) {
89                  userService.addUserPermissions(usermodel.getId(),
90                          new ArrayList<>(userPermissionModels));
91                  if (log.isDebugEnabled()) {
92                      log.debug("updateProjectOwners: updated permissions for " + usermodel + " to " + userPermissionModels);
93                  }
94              }
95          }
96  
97          projectService.setProjectOwners(project, userIds);
98      }
99  
100     public static void setFormProperties(Project project, ProjectService projectService,
101                                                ActionForm form, ActionMessages errors)
102             throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
103         project.setDescription((String) PropertyUtils.getSimpleProperty(
104                 form, "description"));
105         project.setName((String) PropertyUtils.getSimpleProperty(form,
106                 "name"));
107         Integer projectStatus = (Integer) PropertyUtils.getSimpleProperty(
108                 form, "status");
109 
110         String projectName = (String) PropertyUtils.getSimpleProperty(form, "name");
111 
112         project.setName(projectName);
113 
114 
115         if (errors.isEmpty()) {
116             if (projectStatus != null) {
117                 project.setStatus(Status.valueOf(projectStatus));
118             } else {
119                 project.setStatus(Status.ACTIVE);
120             }
121 
122             Integer[] optionValues = (Integer[]) PropertyUtils
123                     .getSimpleProperty(form, "options");
124             int optionmask = 0;
125             if (optionValues != null) {
126                 for (Integer optionValue : optionValues) {
127                     optionmask += optionValue;
128                 }
129             }
130             project.setOptions(optionmask);
131 
132             Integer[] fieldsArray = (Integer[]) PropertyUtils.getSimpleProperty(form, "fields");
133             Set<Integer> fields = null == fieldsArray ? new HashSet<Integer>(0) :
134                     new HashSet<>(Arrays.asList(fieldsArray));
135 
136             projectService.setProjectFields(project, fields);
137 
138         }
139     }
140 }