View Javadoc
1   package org.itracker.web.util;
2   
3   import org.apache.log4j.Logger;
4   import org.apache.struts.action.ActionForward;
5   import org.apache.struts.action.ActionMapping;
6   import org.itracker.core.resources.ITrackerResources;
7   import org.itracker.model.*;
8   import org.itracker.model.Notification.Type;
9   import org.itracker.model.util.*;
10  import org.itracker.services.NotificationService;
11  import org.itracker.services.UserService;
12  
13  import javax.servlet.http.HttpServletRequest;
14  import javax.servlet.http.HttpSession;
15  import java.util.*;
16  
17  public class EditIssueActionUtil {
18      private static final Logger log = Logger.getLogger(EditIssueActionUtil.class);
19  
20      public static void sendNotification(Integer issueId, int previousStatus,
21                                                String baseURL, NotificationService notificationService) {
22          Type notificationType = Type.UPDATED;
23  
24          Issue issue = ServletContextUtils.getItrackerServices().getIssueService().getIssue(issueId);
25  
26          if ((previousStatus < IssueUtilities.STATUS_CLOSED)
27                  && issue.getStatus() >= IssueUtilities.STATUS_CLOSED) {
28              notificationType = Type.CLOSED;
29          }
30  
31          if (log.isDebugEnabled()) {
32              log.debug("notificationService: before send");
33          }
34          notificationService.sendNotification(issue, notificationType, baseURL);
35  
36          if (log.isDebugEnabled()) {
37              log.debug("notificationService: after send");
38          }
39      }
40  
41      public static ActionForward getReturnForward(Issue issue, Project project,
42                                                         String caller, ActionMapping mapping) throws Exception {
43          if ("index".equals(caller)) {
44              log.info("EditIssueAction: Forward: index");
45              return mapping.findForward("index");
46          } else if ("viewissue".equals(caller) && issue.getStatus() >= IssueUtilities.STATUS_CLOSED) {
47              log.info("EditIssueAction: Forward: viewissue");
48              return new ActionForward(mapping.findForward("viewissue").getPath()
49                      + "?id=" + issue.getId());
50          } else {
51              log.info("EditIssueAction: Forward: listissues");
52              return new ActionForward(mapping.findForward("listissues")
53                      .getPath()
54                      + "?projectId=" + project.getId());
55          }
56      }
57  
58  
59      public static Map<Integer, List<NameValuePair>> getListOptions(
60              HttpServletRequest request, Issue issue,
61              List<NameValuePair> ownersList,
62              Map<Integer, Set<PermissionType>> userPermissions, Project project,
63              User currUser) {
64          Map<Integer, List<NameValuePair>> listOptions = new HashMap<Integer, List<NameValuePair>>();
65  
66          Locale locale = (Locale) request.getSession().getAttribute(
67                  Constants.LOCALE_KEY);
68  
69          if (ownersList != null && ownersList.size() > 0) {
70              listOptions.put(IssueUtilities.FIELD_OWNER, ownersList);
71          }
72  
73          boolean hasFullEdit = UserUtilities.hasPermission(userPermissions,
74                  project.getId(), UserUtilities.PERMISSION_EDIT_FULL);
75  
76          List<NameValuePair> allStatuses = IssueUtilities.getStatuses(locale);
77          List<NameValuePair> statusList = new ArrayList<NameValuePair>();
78  
79          if (!hasFullEdit) {
80  
81              if (issue.getStatus() >= IssueUtilities.STATUS_RESOLVED
82                      && UserUtilities.hasPermission(userPermissions, project
83                      .getId(), UserUtilities.PERMISSION_CLOSE)) {
84                  for (NameValuePair allStatuse : allStatuses) {
85                      int statusNumber = Integer.parseInt(allStatuse
86                              .getValue());
87                      if (issue.getStatus() >= IssueUtilities.STATUS_CLOSED
88                              && statusNumber >= IssueUtilities.STATUS_CLOSED) {
89                          statusList.add(allStatuse);
90                      } else if (issue.getStatus() >= IssueUtilities.STATUS_RESOLVED
91                              && statusNumber >= IssueUtilities.STATUS_RESOLVED) {
92                          statusList.add(allStatuse);
93                      }
94                  }
95              }
96  
97          } else {
98  
99              if (currUser.isSuperUser()) {
100                 for (NameValuePair allStatuse : allStatuses) {
101                     statusList.add(allStatuse);
102                 }
103             } else if (issue.getStatus() >= IssueUtilities.STATUS_ASSIGNED
104                     && issue.getStatus() < IssueUtilities.STATUS_RESOLVED) {
105                 for (NameValuePair allStatuse : allStatuses) {
106                     int statusNumber = Integer.parseInt(allStatuse
107                             .getValue());
108                     if (statusNumber >= IssueUtilities.STATUS_ASSIGNED
109                             && statusNumber < IssueUtilities.STATUS_CLOSED) {
110                         statusList.add(allStatuse);
111                     } else if (statusNumber >= IssueUtilities.STATUS_CLOSED
112                             && ProjectUtilities
113                             .hasOption(
114                                     ProjectUtilities.OPTION_ALLOW_ASSIGN_TO_CLOSE,
115                                     project.getOptions())
116                             && UserUtilities.hasPermission(userPermissions,
117                             project.getId(),
118                             UserUtilities.PERMISSION_CLOSE)) {
119                         statusList.add(allStatuse);
120                     }
121                 }
122             } else if (issue.getStatus() >= IssueUtilities.STATUS_RESOLVED
123                     && issue.getStatus() < IssueUtilities.STATUS_CLOSED) {
124                 for (NameValuePair allStatuse : allStatuses) {
125                     int statusNumber = Integer.parseInt(allStatuse
126                             .getValue());
127                     if (statusNumber >= IssueUtilities.STATUS_ASSIGNED
128                             && statusNumber < IssueUtilities.STATUS_CLOSED) {
129                         statusList.add(allStatuse);
130                     } else if (statusNumber >= IssueUtilities.STATUS_CLOSED
131                             && UserUtilities.hasPermission(userPermissions,
132                             project.getId(),
133                             UserUtilities.PERMISSION_CLOSE)) {
134                         statusList.add(allStatuse);
135                     }
136                 }
137             } else if (issue.getStatus() >= IssueUtilities.STATUS_CLOSED) {
138                 for (NameValuePair allStatuse : allStatuses) {
139                     int statusNumber = Integer.parseInt(allStatuse
140                             .getValue());
141                     if ((statusNumber >= IssueUtilities.STATUS_ASSIGNED && statusNumber < IssueUtilities.STATUS_RESOLVED)
142                             || statusNumber >= IssueUtilities.STATUS_CLOSED) {
143                         statusList.add(allStatuse);
144                     }
145                 }
146             }
147 
148         }
149         // sort by status code so it will be ascending output.
150         Collections.sort(statusList, NameValuePair.VALUE_COMPARATOR);
151         listOptions.put(IssueUtilities.FIELD_STATUS, statusList);
152 
153         List<NameValuePair> severities = IssueUtilities.getSeverities(locale);
154         // sort by severity code so it will be ascending output.
155         Collections.sort(severities, NameValuePair.VALUE_COMPARATOR);
156         listOptions.put(IssueUtilities.FIELD_SEVERITY, severities);
157 
158         List<NameValuePair> resolutions = IssueUtilities.getResolutions(locale);
159         listOptions.put(IssueUtilities.FIELD_RESOLUTION, resolutions);
160 
161         List<Component> components = project.getComponents();
162         Collections.sort(components, Component.NAME_COMPARATOR);
163         listOptions.put(IssueUtilities.FIELD_COMPONENTS, Convert
164                 .componentsToNameValuePairs(components));
165 
166         List<Version> versions = project.getVersions();
167         listOptions.put(IssueUtilities.FIELD_VERSIONS, Convert
168                 .versionsToNameValuePairs(versions));
169         listOptions.put(IssueUtilities.FIELD_TARGET_VERSION, Convert
170                 .versionsToNameValuePairs(versions));
171 
172         List<CustomField> projectFields = project.getCustomFields();
173         for (CustomField projectField : projectFields) {
174             if (projectField.getFieldType() == CustomField.Type.LIST) {
175                 listOptions.put(projectField.getId(), Convert
176                         .customFieldOptionsToNameValuePairs(projectField, locale));
177             }
178         }
179 
180         return listOptions;
181     }
182 
183     private static List<NameValuePair> fieldOptions(CustomField field) {
184         return Convert.customFieldOptionsToNameValuePairs(field.getOptions());
185     }
186 
187     public static Map<Integer, List<NameValuePair>> mappedFieldOptions(List<CustomField> fields ) {
188         Map<Integer, List<NameValuePair>> options = new HashMap<Integer, List<NameValuePair>>(fields.size());
189         for (CustomField field: fields) {
190 
191             List<NameValuePair> listOptions = fieldOptions(field);
192             if (field.isSortOptionsByName()) {
193                 Collections.sort(listOptions, NameValuePair.KEY_COMPARATOR);
194             }
195 
196             options.put(field.getId(), listOptions);
197         }
198 
199 
200 
201         return options;
202     }
203 
204 
205     /**
206      * This method will obtain and build a list of possible owners for the
207      * webpages to display and the operator to choose from.
208      */
209     public static List<NameValuePair> getAssignableIssueOwnersList(Issue issue,
210                                                                    Project project, User currUser, Locale locale,
211                                                                    UserService userService,
212                                                                    Map<Integer, Set<PermissionType>> userPermissions) {
213 
214         List<NameValuePair> ownersList = new ArrayList<NameValuePair>();
215 
216         if (UserUtilities.hasPermission(userPermissions, project.getId(),
217                 UserUtilities.PERMISSION_ASSIGN_OTHERS)) {
218             if (issue.getOwner() == null) {
219                 ownersList.add(new NameValuePair(ITrackerResources.getString(
220                         "itracker.web.generic.unassigned", locale), "-1"));
221             } else {
222                 ownersList.add(new NameValuePair(ITrackerResources.getString(
223                         "itracker.web.generic.unassign", locale), "-1"));
224             }
225             List<User> possibleOwners = userService.getPossibleOwners(issue,
226                     project.getId(), currUser.getId());
227             Collections.sort(possibleOwners, User.NAME_COMPARATOR);
228             List<NameValuePair> ownerNames = Convert
229                     .usersToNameValuePairs(possibleOwners);
230             for (NameValuePair ownerName : ownerNames) {
231                 ownersList.add(ownerName);
232             }
233         } else if (UserUtilities.hasPermission(userPermissions,
234                 project.getId(), UserUtilities.PERMISSION_ASSIGN_SELF)) {
235             if (issue.getOwner() != null) {
236                 if (IssueUtilities.canUnassignIssue(issue, currUser.getId(),
237                         userPermissions)) {
238                     ownersList.add(new NameValuePair(ITrackerResources
239                             .getString("itracker.web.generic.unassign",
240                                     locale), "-1"));
241                 }
242                 if (!issue.getOwner().getId().equals(currUser.getId())) {
243                     ownersList.add(new NameValuePair(issue.getOwner()
244                             .getFirstName()
245                             + " " + issue.getOwner().getLastName(), issue
246                             .getOwner().getId().toString()));
247                     ownersList.add(new NameValuePair(currUser.getFirstName()
248                             + " " + currUser.getLastName(), currUser.getId()
249                             .toString()));
250                 } else {
251                     ownersList.add(new NameValuePair(currUser.getFirstName()
252                             + " " + currUser.getLastName(), currUser.getId()
253                             .toString()));
254                 }
255             }
256         } else if (issue.getOwner() != null
257                 && IssueUtilities.canUnassignIssue(issue, currUser.getId(),
258                 userPermissions)) {
259             ownersList.add(new NameValuePair(ITrackerResources.getString(
260                     "itracker.web.generic.unassign", locale), "-1"));
261             ownersList.add(new NameValuePair(issue.getOwner().getFirstName()
262                     + " " + issue.getOwner().getLastName(), issue.getOwner()
263                     .getId().toString()));
264         }
265 
266         return ownersList;
267     }
268 
269     @SuppressWarnings("unchecked")
270     public static void setupCreateIssue(HttpServletRequest req) {
271         HttpSession session = req.getSession();
272         Project project = (Project) session.getAttribute(Constants.PROJECT_KEY);
273         Map<Integer, List<NameValuePair>> listOptions = (Map<Integer, List<NameValuePair>>) session
274                 .getAttribute(Constants.LIST_OPTIONS_KEY);
275         List<NameValuePair> possibleOwners = WorkflowUtilities.getListOptions(
276                 listOptions, IssueUtilities.FIELD_OWNER);
277         List<NameValuePair> severities = WorkflowUtilities.getListOptions(
278                 listOptions, IssueUtilities.FIELD_SEVERITY);
279         List<NameValuePair> possibleCreators = WorkflowUtilities
280                 .getListOptions(listOptions, IssueUtilities.FIELD_CREATOR);
281         List<NameValuePair> components = WorkflowUtilities.getListOptions(
282                 listOptions, IssueUtilities.FIELD_COMPONENTS);
283         List<NameValuePair> versions = WorkflowUtilities.getListOptions(
284                 listOptions, IssueUtilities.FIELD_VERSIONS);
285         List<CustomField> projectFields = project.getCustomFields();
286         if (projectFields != null && projectFields.size() > 0) {
287             Collections.sort(projectFields, CustomField.ID_COMPARATOR);
288         }
289         String wrap = "soft";
290         if (ProjectUtilities.hasOption(ProjectUtilities.OPTION_SURPRESS_HISTORY_HTML, project.getOptions())) {
291             wrap = "hard";
292         }
293         /*
294            * Get the status name for the current locale and set in request
295            */
296         String pageTitleKey = "itracker.web.createissue.title";
297         String pageTitleArg = project.getName();
298         req.setAttribute("pageTitleKey", pageTitleKey);
299         req.setAttribute("pageTitleArg", pageTitleArg);
300 
301         req.setAttribute("statusName", IssueUtilities.getStatusName(
302                 IssueUtilities.STATUS_NEW,
303                 LoginUtilities.getCurrentLocale(req)));
304         req.setAttribute("hasAttachmentOption", !ProjectUtilities.hasOption(
305                 ProjectUtilities.OPTION_NO_ATTACHMENTS, project.getOptions()));
306         req.setAttribute("possibleOwners", possibleOwners);
307         req.setAttribute("severities", severities);
308         req.setAttribute("possibleCreators", possibleCreators);
309         req.setAttribute("components", components);
310         req.setAttribute("versions", versions);
311         req.setAttribute("projectFields", projectFields);
312         req.setAttribute("listOptions", listOptions);
313         req.setAttribute("wrap", wrap);
314     }
315 }