View Javadoc
1   package org.itracker.web.actions;
2   
3   import org.apache.commons.collections.CollectionUtils;
4   import org.apache.commons.collections.Predicate;
5   import org.apache.struts.action.ActionForm;
6   import org.apache.struts.action.ActionForward;
7   import org.apache.struts.action.ActionMapping;
8   import org.itracker.model.*;
9   import org.itracker.model.util.IssueUtilities;
10  import org.itracker.model.util.UserUtilities;
11  import org.itracker.services.IssueService;
12  import org.itracker.services.ProjectService;
13  import org.itracker.services.UserService;
14  import org.itracker.web.actions.base.ItrackerBaseAction;
15  import org.itracker.web.ptos.IssuePTO;
16  import org.itracker.web.util.*;
17  import org.slf4j.Logger;
18  import org.slf4j.LoggerFactory;
19  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  import javax.servlet.http.HttpSession;
23  import java.util.*;
24  
25  //  TODO: Action Cleanup
26  
27  public class PortalHomeAction extends ItrackerBaseAction {
28  
29      static final Logger log = LoggerFactory.getLogger(PortalHomeAction.class);
30      
31      @SuppressWarnings("unchecked")
32      public ActionForward execute(ActionMapping mapping, ActionForm form,
33              HttpServletRequest request, HttpServletResponse response)
34              throws Exception {
35          log.debug("Stepping up into the loginRouter method");
36  
37  		// maybe wrong the next line... setting a default forward...
38  		ActionForward forward = mapping.findForward("portalhome");
39  
40  		if (forward == null) {
41  			return null;
42  		} else {
43              
44              log.debug("Found forward, let's go and check if this forward is portalhome...");
45  
46              if (forward.getName().equals("portalhome")
47  					|| forward.getName().equals("index")) {
48                  
49                  final IssueService issueService = ServletContextUtils.getItrackerServices().getIssueService();
50                  final ProjectService projectService = ServletContextUtils.getItrackerServices().getProjectService();
51                  final UserService userService = ServletContextUtils.getItrackerServices().getUserService();
52                  final User currUser = (User)request.getSession().getAttribute("currUser");
53                  final Locale locale = super.getLocale(request);
54  
55                  final Map<Integer, Set<PermissionType>> permissions =
56                          (Map<Integer, Set<PermissionType>>)request.getSession().getAttribute("permissions");
57  
58                  // GETTING AND SETTING USER PREFS AND HIDDEN SECTIONS ACCORDINGLY
59                  UserPreferences userPrefs = currUser.getPreferences();
60                  if(userPrefs == null) userPrefs = new UserPreferences();
61                  
62                  int hiddenSections = 0;
63                  Boolean allSections = null == request.getSession().getAttribute("allSections") ? false: Boolean.valueOf(request.getSession().getAttribute("allSections").toString());
64                  if (null != request.getParameter("allSections"))
65                  {
66                  	allSections = Boolean.valueOf(request.getParameter("allSections"));
67                  }
68                  
69                  if(!allSections) {
70                      hiddenSections = userPrefs.getHiddenIndexSections();
71                  }
72                  
73                  final List<IssuePTO> createdIssuePTOs;
74                  final List<IssuePTO> ownedIssuePTOs;
75                  final List<IssuePTO> unassignedIssuePTOs;
76                  final List<IssuePTO> watchedIssuePTOs;
77                  
78                  // POPULATING ISSUE MODELS
79                  final List<Issue> createdIssues;
80                  final List<Issue> ownedIssues;
81                  final List<Issue> unassignedIssues;
82                  final List<Issue> watchedIssues;
83                  
84                  // PUTTING PREFERENCES INTO THE REQUEST SCOPE
85                  
86                  if(UserUtilities.hideIndexSection(UserUtilities.PREF_HIDE_CREATED, hiddenSections)) {
87                      createdIssues  = new ArrayList<Issue>();
88                      request.setAttribute("UserUtilities_PREF_HIDE_CREATED", Boolean.TRUE);
89                  } else {
90                      createdIssues = issueService.getIssuesCreatedByUser(currUser.getId());
91                      request.setAttribute("UserUtilities_PREF_HIDE_CREATED", Boolean.FALSE);
92                  }
93                  
94                  if(UserUtilities.hideIndexSection(UserUtilities.PREF_HIDE_ASSIGNED, hiddenSections)) {
95                      ownedIssues = new ArrayList<Issue>();
96                      request.setAttribute("UserUtilities_PREF_HIDE_ASSIGNED", Boolean.TRUE);
97                  } else {
98                      ownedIssues = issueService.getIssuesOwnedByUser(currUser.getId());
99                      request.setAttribute("UserUtilities_PREF_HIDE_ASSIGNED", Boolean.FALSE);
100                 }
101                 
102                 if(UserUtilities.hideIndexSection(UserUtilities.PREF_HIDE_UNASSIGNED, hiddenSections)) {
103                     unassignedIssues = new ArrayList<Issue>();
104                     request.setAttribute("UserUtilities_PREF_HIDE_UNASSIGNED", Boolean.TRUE);
105                 } else {
106                     unassignedIssues = issueService.getUnassignedIssues();
107                     request.setAttribute("UserUtilities_PREF_HIDE_UNASSIGNED", Boolean.FALSE);
108                 }
109                 
110                 if(UserUtilities.hideIndexSection(UserUtilities.PREF_HIDE_WATCHED, hiddenSections)) {
111                     watchedIssues = new ArrayList<Issue>();
112                     request.setAttribute("UserUtilities_PREF_HIDE_WATCHED", Boolean.TRUE);
113                 } else {
114                     watchedIssues = issueService.getIssuesWatchedByUser(currUser.getId());
115                     request.setAttribute("UserUtilities_PREF_HIDE_WATCHED", Boolean.FALSE);
116                 }
117                 
118                 // SORTING ISSUES ACCORDING TO USER PREFS
119                 String order;
120                 if (null != request.getParameter("sortKey")) {
121                     order = request.getParameter("sortKey");
122                 } else {
123                     order = userPrefs.getSortColumnOnIssueList();
124                 }
125                 Comparator sort_id = Issue.ID_COMPARATOR;
126                  //TODO: since repeating code, set a common Comparator variable to contain the Comparator to use and
127                  //      execute the sort pre issue type only once.
128 
129                 if("id".equals(order)) {
130                     sort_id = Issue.ID_COMPARATOR;
131                 } else if("sev".equals(order)) {
132                     sort_id = Issue.SEVERITY_COMPARATOR;
133                 } else if("stat".equals(order)) {
134                     sort_id = Issue.STATUS_COMPARATOR;
135                 } else if("lm".equals(order)) {
136                     sort_id = Issue.LAST_MODIFIED_DATE_COMPARATOR;
137                 } else if("own".equals(order)) {
138                     sort_id = Issue.OWNER_AND_STATUS_COMPARATOR;
139                 }
140 
141                 Collections.sort(createdIssues, sort_id);
142                 Collections.sort(ownedIssues, sort_id);
143                 unassignedIssues.removeAll(CollectionUtils.select(unassignedIssues,
144                     new Predicate() {
145                         @Override
146                         public boolean evaluate(Object object) {
147                             return object instanceof Issue
148                                     && !IssueUtilities.canViewIssue((Issue) object, currUser.getId(), permissions);
149                         }
150                     }
151                 ));
152                 Collections.sort(unassignedIssues, sort_id);
153                 Collections.sort(watchedIssues, sort_id);
154 
155                 
156                 // COPYING MODELS INTO PTOS
157                 
158                 // SETTING USER PERMISSIONS ON THE ISSUES
159                 ownedIssuePTOs = buildIssueList( ownedIssues, request, permissions );
160                 unassignedIssuePTOs = buildIssueList( unassignedIssues, request, permissions );
161                 createdIssuePTOs = buildIssueList( createdIssues, request, permissions );
162                 watchedIssuePTOs = buildIssueList( watchedIssues, request, permissions );
163                 // setup unassigned watched issues has notification
164                 if ( watchedIssuePTOs != null && watchedIssuePTOs.size() > 0 && unassignedIssuePTOs != null && unassignedIssuePTOs.size() > 0 ) {
165                     for (IssuePTO watchedIssue : watchedIssuePTOs) {
166                         for (IssuePTO unassignedIssuePTO : unassignedIssuePTOs) {
167                             if (watchedIssue.getIssue().getId() == unassignedIssuePTO.getIssue().getId()) {
168                                 unassignedIssuePTO.setUserHasIssueNotification(true);
169                             }
170                         }
171                     }
172                 }
173                 // POSSIBLE OWNERS CODE...
174                 
175                 // Because of the potentially large number of issues, and a multitude of projects, the
176                 // possible owners for a project are stored in a Map.  This doesn't take into account the
177                 // creator of the issue though since they may only have EDIT_USERS permission.  So if the
178                 // creator isn't already in the project list, check to see if the creator has EDIT_USERS
179                 // permissions, if so then add them to the list of owners and resort.
180 
181                 HttpSession session = request.getSession(true);
182                 Map<Integer, Set<PermissionType>> userPermissions = RequestHelper.getUserPermissions(session);
183 
184                 for (IssuePTO issuePTO : unassignedIssuePTOs) {
185                     List<User> possibleIssueOwners = new ArrayList<User>();
186                     boolean creatorPresent = false;
187                     final Issue issue = issuePTO.getIssue();
188                     final Project project = issueService.getIssueProject(issue.getId());
189 
190                     final List<NameValuePair> ownersList;
191 
192                     ownersList = EditIssueActionUtil.getAssignableIssueOwnersList(issue, project, currUser, locale, userService, userPermissions);
193 
194                     for (NameValuePair owner : ownersList) {
195                         possibleIssueOwners.add(userService.getUser(Integer.parseInt(owner.getValue())));
196                         if (owner.getValue().equals(String.valueOf(issue.getCreator().getId()))) {
197                             creatorPresent = true;
198                         }
199                     }
200 
201                     if (!creatorPresent) {
202                         for (Permission creatorPermission : issue.getCreator().getPermissions()) {
203                             if (creatorPermission.getPermissionType() == PermissionType.ISSUE_EDIT_USERS) {
204                                 possibleIssueOwners.add(userService.getUser(issue.getCreator().getId()));
205                                 break;
206                             }
207                         }
208                     }
209                     issuePTO.setPossibleOwners(possibleIssueOwners);
210 
211                 }
212                 
213 
214 
215                 // PUTTING ISSUES INTO THE REQUEST SCOPE
216 				log.debug("ownedIssues Size: {}", ownedIssuePTOs.size());
217 				request.setAttribute("ownedIssues", ownedIssuePTOs);
218 
219 				log.debug("unassignedIssues Size: {}", unassignedIssuePTOs.size());
220 				request.setAttribute("unassignedIssues", unassignedIssuePTOs);
221 
222 				log.debug("createdIssues Size: {}", createdIssuePTOs.size());
223 				request.setAttribute("createdIssues", createdIssuePTOs);
224 
225 				log.debug("watchedIssues Size: {}", watchedIssuePTOs.size());
226 				request.setAttribute("watchedIssues", watchedIssuePTOs);
227                 
228                 
229                 
230                 log.debug("Found forward: {} and stepped into action method that's populating portalhome",
231                         forward.getName());
232 
233                 request.setAttribute("ih", issueService);
234                 request.setAttribute("ph",projectService);
235                 request.setAttribute("uh",userService);
236                 request.setAttribute("userPrefs",userPrefs);
237                 //TODO: set the next value based on the request attribute!
238                 String showAllAtt = String.valueOf(request.getSession().getAttribute(Constants.SHOW_ALL_KEY));
239                 Boolean showAll = null == showAllAtt? false
240                         : Boolean.valueOf(showAllAtt);
241                 showAllAtt = request.getParameter(Constants.SHOW_ALL_KEY);
242                 if (null != showAllAtt) {
243                 	showAll = Boolean.valueOf(showAllAtt);
244                 }
245                 if (!showAll && userPrefs.getNumItemsOnIndex() < 1) {
246                 	showAll=true;
247                 }
248                 
249                 log.debug("userPrefs.getNumItemsOnIndex(): {}, showAll: {}", userPrefs.getNumItemsOnIndex(), showAll);
250 
251                 request.getSession().setAttribute(Constants.SHOW_ALL_KEY, showAll);
252 
253                 request.getSession().setAttribute(Constants.ALL_SECTIONS_KEY, allSections);
254 
255                 log.debug("Action is trying to forward portalhome");
256             }
257             return forward;
258         }
259     }
260     
261     // this function is used to load the issue type PTOs List with issue/owner/project data.  It will return this the the main
262     // function for further processing.
263     
264     @SuppressWarnings("unchecked")
265 	public List<IssuePTO> buildIssueList(List<Issue> issues, HttpServletRequest request, Map<Integer, Set<PermissionType>> permissions) {
266         User currUser = LoginUtilities.getCurrentUser(request);
267         Locale locale = getLocale(request);
268         
269         List<IssuePTO> issuePTOs = new ArrayList<IssuePTO>();
270 
271         for (Issue issue : issues) {
272             IssuePTOtml#IssuePTO">IssuePTO issuePTO = new IssuePTO(issue);
273             issuePTO.setSeverityLocalizedString(IssueUtilities.getSeverityName(issue.getSeverity(), locale));
274             issuePTO.setStatusLocalizedString(IssueUtilities.getStatusName(issue.getStatus(), locale));
275             issuePTO.setUnassigned((issuePTO.getIssue().getOwner() == null));
276             issuePTO.setUserCanEdit(IssueUtilities.canEditIssue(issue, currUser.getId(), permissions));
277             issuePTO.setUserCanViewIssue(IssueUtilities.canViewIssue(issue, currUser.getId(), permissions));
278             issuePTO.setUserHasPermission_PERMISSION_ASSIGN_SELF(UserUtilities.hasPermission(permissions, issue.getProject().getId(), UserUtilities.PERMISSION_ASSIGN_SELF));
279             issuePTO.setUserHasPermission_PERMISSION_ASSIGN_OTHERS(UserUtilities.hasPermission(permissions, issue.getProject().getId(), UserUtilities.PERMISSION_ASSIGN_OTHERS));
280             issuePTO.setUserHasIssueNotification(IssueUtilities.hasIssueNotification(issue, currUser.getId()));
281             issuePTOs.add(issuePTO);
282         }
283         return issuePTOs;
284     }
285     
286     
287     public PortalHomeAction() {
288         super();
289   
290     }
291     
292 }