View Javadoc

1   package org.itracker.web.actions;
2   
3   import java.util.ArrayList;
4   import java.util.Collections;
5   import java.util.Comparator;
6   import java.util.Iterator;
7   import java.util.List;
8   import java.util.Locale;
9   import java.util.Map;
10  import java.util.Set;
11  
12  import javax.servlet.http.HttpServletRequest;
13  import javax.servlet.http.HttpServletResponse;
14  import javax.servlet.http.HttpSession;
15  
16  import org.apache.log4j.Logger;
17  import org.apache.struts.action.ActionForm;
18  import org.apache.struts.action.ActionForward;
19  import org.apache.struts.action.ActionMapping;
20  import org.itracker.core.resources.ITrackerResources;
21  import org.itracker.model.Issue;
22  import org.itracker.model.NameValuePair;
23  import org.itracker.model.Permission;
24  import org.itracker.model.PermissionType;
25  import org.itracker.model.Project;
26  import org.itracker.model.User;
27  import org.itracker.model.UserPreferences;
28  import org.itracker.services.IssueService;
29  import org.itracker.services.ProjectService;
30  import org.itracker.services.UserService;
31  import org.itracker.services.util.IssueUtilities;
32  import org.itracker.services.util.UserUtilities;
33  import org.itracker.web.actions.base.ItrackerBaseAction;
34  import org.itracker.web.ptos.IssuePTO;
35  import org.itracker.web.util.LoginUtilities;
36  
37  //  TODO: Action Cleanup
38  
39  public class PortalHomeAction extends ItrackerBaseAction {
40      
41      static final Logger LOGGER = Logger.getLogger(PortalHomeAction.class);
42      
43      @SuppressWarnings("unchecked")
44      public ActionForward execute(ActionMapping mapping, ActionForm form,
45              HttpServletRequest request, HttpServletResponse response)
46              throws Exception {
47          LOGGER.info("Stepping up into the loginRouter method");
48  
49  		// maybe wrong the next line... setting a default forward...
50  		ActionForward forward = mapping.findForward("portalhome");
51  
52  		if (forward == null) {
53  			return null;
54  		} else {
55              
56              LOGGER.info("Found forward, let's go and check if this forward is portalhome...");
57  //            super.executeAlways(mapping,form,request,response);
58              
59              if (forward.getName().equals("portalhome")
60  					|| forward.getName().equals("index")) {
61                  
62                  IssueService issueService = this.getITrackerServices().getIssueService();
63                  ProjectService projectService = this.getITrackerServices().getProjectService();
64                  UserService userService = this.getITrackerServices().getUserService();
65                  User currUser = (User)request.getSession().getAttribute("currUser");
66                  Locale locale = super.getLocale(request);
67  //                Integer userId = currUser.getId();
68  //				Map<Integer, Set<PermissionType>> permissions =
69  //                        (Map<Integer, Set<PermissionType>>)request.getSession().getAttribute("permissions");
70                  
71                  // GETTING AND SETTING USER PREFS AND HIDDEN SECTIONS ACCORDINGLY
72                  UserPreferences userPrefs = currUser.getPreferences();
73                  if(userPrefs == null) userPrefs = new UserPreferences();
74                  
75                  int hiddenSections = 0;
76                  Boolean allSections = null == request.getSession().getAttribute("allSections") ? false: Boolean.valueOf(request.getSession().getAttribute("allSections").toString());
77                  if (null != request.getParameter("allSections"))
78                  {
79                  	allSections = Boolean.valueOf(request.getParameter("allSections"));
80                  }
81                  
82                  if(!allSections) {
83                      hiddenSections = userPrefs.getHiddenIndexSections();
84                  }
85                  
86                  final List<IssuePTO> createdIssuePTOs;
87                  final List<IssuePTO> ownedIssuePTOs;
88                  final List<IssuePTO> unassignedIssuePTOs;
89                  final List<IssuePTO> watchedIssuePTOs;
90                  
91                  // POPULATING ISSUE MODELS
92                  final List<Issue> createdIssues;
93                  final List<Issue> ownedIssues;
94                  final List<Issue> unassignedIssues;
95                  final List<Issue> watchedIssues;
96                  
97                  // PUTTING PREFERENCES INTO THE REQUEST SCOPE
98                  
99                  if(UserUtilities.hideIndexSection(UserUtilities.PREF_HIDE_CREATED, hiddenSections)) {
100                     createdIssues  = new ArrayList<Issue>();
101                     request.setAttribute("UserUtilities_PREF_HIDE_CREATED", Boolean.TRUE);
102                 } else {
103                     createdIssues = issueService.getIssuesCreatedByUser(currUser.getId());
104                     request.setAttribute("UserUtilities_PREF_HIDE_CREATED", Boolean.FALSE);
105                 }
106                 
107                 if(UserUtilities.hideIndexSection(UserUtilities.PREF_HIDE_ASSIGNED, hiddenSections)) {
108                     ownedIssues = new ArrayList<Issue>();
109                     request.setAttribute("UserUtilities_PREF_HIDE_ASSIGNED", Boolean.TRUE);
110                 } else {
111                     ownedIssues = issueService.getIssuesOwnedByUser(currUser.getId());
112                     request.setAttribute("UserUtilities_PREF_HIDE_ASSIGNED", Boolean.FALSE);
113                 }
114                 
115                 if(UserUtilities.hideIndexSection(UserUtilities.PREF_HIDE_UNASSIGNED, hiddenSections)) {
116                     unassignedIssues = new ArrayList<Issue>();
117                     request.setAttribute("UserUtilities_PREF_HIDE_UNASSIGNED", Boolean.TRUE);
118                 } else {
119                     unassignedIssues = issueService.getUnassignedIssues();
120                     request.setAttribute("UserUtilities_PREF_HIDE_UNASSIGNED", Boolean.FALSE);
121                 }
122                 
123                 if(UserUtilities.hideIndexSection(UserUtilities.PREF_HIDE_WATCHED, hiddenSections)) {
124                     watchedIssues = new ArrayList<Issue>();
125                     request.setAttribute("UserUtilities_PREF_HIDE_WATCHED", Boolean.TRUE);
126                 } else {
127                     watchedIssues = issueService.getIssuesWatchedByUser(currUser.getId());
128                     request.setAttribute("UserUtilities_PREF_HIDE_WATCHED", Boolean.FALSE);
129                 }
130                 
131                 // SORTING ISSUES ACCORDING TO USER PREFS
132                 if (null!=userPrefs) {
133                     String order = userPrefs.getSortColumnOnIssueList();
134                     Comparator sort_id = Issue.STATUS_COMPARATOR;
135                  //TODO: since repeating code, set a common Comparator variable to contain the Comparator to use and
136                  //      execute the sort pre issue type only once.
137                     if("id".equals(order)) {
138                         sort_id = Issue.ID_COMPARATOR;
139                     } else if("sev".equals(order)) {
140                         sort_id = Issue.SEVERITY_COMPARATOR;
141                     } else if("stat".equals(order)) {
142                         sort_id = Issue.STATUS_COMPARATOR;
143                     } else if("lm".equals(order)) {
144                         sort_id = Issue.LAST_MODIFIED_DATE_COMPARATOR;
145                     } else if("own".equals(order)) {
146                         sort_id = Issue.OWNER_AND_STATUS_COMPARATOR;
147                     }
148                     
149                     Collections.sort(createdIssues, sort_id);
150                     Collections.sort(ownedIssues, sort_id);
151                     Collections.sort(unassignedIssues, sort_id);
152                     Collections.sort(watchedIssues, sort_id);
153                 }
154                 
155                 // COPYING MODELS INTO PTOS
156                 
157                 // SETTING USER PERMISSIONS ON THE ISSUES
158 // Marky:  Made function that would built the PTOs
159                 ownedIssuePTOs = buildIssueList( ownedIssues, request );
160                 unassignedIssuePTOs = buildIssueList( unassignedIssues, request );
161                 createdIssuePTOs = buildIssueList( createdIssues, request );
162                 watchedIssuePTOs = buildIssueList( watchedIssues, request );
163                 if ( watchedIssuePTOs != null && watchedIssuePTOs.size() > 0 && unassignedIssuePTOs != null && unassignedIssuePTOs.size() > 0 ) {
164                     for ( Iterator<IssuePTO> witerator = watchedIssuePTOs.iterator(); witerator.hasNext(); ) {
165                         IssuePTO watchedIssue = (IssuePTO) witerator.next();
166                         for ( int i = 0; i < unassignedIssuePTOs.size(); i++ ) {
167                             if ( watchedIssue.getIssue().getId() == unassignedIssuePTOs.get(i).getIssue().getId() ) {
168                                 unassignedIssuePTOs.get(i).setUserHasIssueNotification(false);
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 // Marky:  moved these out of for loop so they can be referenced after loop is over to set attributes.
182                 
183 //                List<Boolean> creatorsPresent = new ArrayList<Boolean>();
184 //                HashMap<Integer,List<User>> usersWithEditOwnMap = new HashMap<Integer,List<User>>();
185                 HttpSession session = request.getSession(true);
186                 Map<Integer, Set<PermissionType>> userPermissions = getUserPermissions(session);
187                 
188                 Iterator<IssuePTO> unassignedIssuePTOIt = unassignedIssuePTOs.iterator();
189                 while (unassignedIssuePTOIt.hasNext()) {
190 					IssuePTO issuePTO = unassignedIssuePTOIt.next();
191 
192                     List<User> possibleIssueOwners = new ArrayList<User>();
193                     boolean creatorPresent = false;
194                     final Issue issue = issuePTO.getIssue();
195                     final Project project = issueService.getIssueProject(issue.getId());
196                     
197                     final List<NameValuePair> ownersList;
198                     
199                     ownersList = UserUtilities.getAssignableIssueOwnersList(issue, project, currUser, locale, userService, userPermissions);
200                     
201                     for ( Iterator idIterator = ownersList.iterator(); idIterator.hasNext(); ) {
202                         NameValuePair owner = (NameValuePair) idIterator.next();
203                         possibleIssueOwners.add(userService.getUser(Integer.parseInt(owner.getValue())));
204                         if ( owner.getValue().equals(String.valueOf(issue.getCreator().getId()) )) {
205                             creatorPresent = true;
206                         }
207                     }
208                     
209                     if(! creatorPresent) {
210                         Iterator premIterator = issue.getCreator().getPermissions().iterator();
211                         while (premIterator.hasNext()) {
212                             Permission creatorPermission = (Permission) premIterator.next();
213                             if ( creatorPermission.getPermissionType() == UserUtilities.PERMISSION_EDIT_USERS ) {
214                                 possibleIssueOwners.add(userService.getUser(issue.getCreator().getId()));
215                                 break;
216                             }
217                         }
218                     }
219                     issuePTO.setPossibleOwners(possibleIssueOwners);
220                     
221                 }
222                 
223 
224                 
225                 request.setAttribute("itracker_web_generic_unassigned", ITrackerResources.getString("itracker.web.generic.unassigned", locale));
226                 
227                 // PUTTING ISSUES INTO THE REQUEST SCOPE
228 				LOGGER.info("ownedIssues Size: " + ownedIssuePTOs.size());
229 				request.setAttribute("ownedIssues", ownedIssuePTOs);
230 
231 				LOGGER.info("unassignedIssues Size:  " + unassignedIssuePTOs.size());
232 				request.setAttribute("unassignedIssues", unassignedIssuePTOs);
233 
234 				LOGGER.info("createdIssues Size: " + createdIssuePTOs.size());
235 				request.setAttribute("createdIssues", createdIssuePTOs);
236 
237 				LOGGER.info("watchedIssues Size: " + watchedIssuePTOs.size());
238 				request.setAttribute("watchedIssues", watchedIssuePTOs);
239                 
240                 
241                 
242                 LOGGER.info("Found forward: "+forward.getName()+" and stepped into action method that's populating portalhome");
243 
244                 
245                 request.setAttribute("ih",issueService);
246                 request.setAttribute("ph",projectService);
247                 request.setAttribute("uh",userService);
248                 request.setAttribute("userPrefs",userPrefs);
249                 //TODO: set the next value based on the request attribute!
250                 Boolean showall = null == request.getSession().getAttribute("showAll") ? false : Boolean.valueOf(request.getSession().getAttribute("showAll").toString());
251                 
252                 if (null != request.getParameter("showAll")) {
253                 	showall = Boolean.valueOf(request.getParameter("showAll"));
254                 }
255                 if (!showall && userPrefs.getNumItemsOnIndex() < 1) {
256                 	showall=true;
257                 }
258                 
259                 LOGGER.info("userPrefs.getNumItemsOnIndex(): " + userPrefs.getNumItemsOnIndex() + ", showAll: " + showall);
260                 
261                 //request.setAttribute("showAll", Boolean.valueOf(showall));
262                 request.getSession().setAttribute("showAll", showall);
263                 
264 
265                 
266                 request.getSession().setAttribute("allSections", allSections);
267                 
268                 
269                 LOGGER.info("Action is trying to forward portalhome");
270             }
271             return forward;
272         }
273     }
274     
275     // this function is used to load the issue type PTOs List with issue/owner/project data.  It will return this the the main
276     // function for further processing.
277     
278     @SuppressWarnings("unchecked")
279 	public List<IssuePTO> buildIssueList( List<Issue> issues, HttpServletRequest request ) {
280         User currUser = LoginUtilities.getCurrentUser(request);
281         Locale locale = getLocale(request);
282         //Integer userId = currUser.getId();
283         Map<Integer, Set<PermissionType>> permissions =
284                 (Map<Integer, Set<PermissionType>>)request.getSession().getAttribute("permissions");
285         
286         List<IssuePTO> issuePTOs = new ArrayList<IssuePTO>();
287         
288         for (int i=0;i<issues.size();i++) {
289             Issue issue = issues.get(i);
290             IssuePTO issuePTO = new IssuePTO(issue);
291             issuePTO.setSeverityLocalizedString(IssueUtilities.getSeverityName(issue.getSeverity(), locale));
292             issuePTO.setStatusLocalizedString(IssueUtilities.getStatusName(issue.getStatus(), locale));
293             issuePTO.setUnassigned((issuePTO.getIssue().getOwner() == null ? true : false));
294             issuePTO.setUserCanEdit(IssueUtilities.canEditIssue(issue, currUser.getId(), permissions));
295             issuePTO.setUserCanViewIssue(IssueUtilities.canViewIssue(issue, currUser.getId(), permissions));
296             issuePTO.setUserHasPermission_PERMISSION_ASSIGN_SELF(UserUtilities.hasPermission(permissions, issue.getProject().getId(), UserUtilities.PERMISSION_ASSIGN_SELF));
297             issuePTO.setUserHasPermission_PERMISSION_ASSIGN_OTHERS(UserUtilities.hasPermission(permissions, issue.getProject().getId(), UserUtilities.PERMISSION_ASSIGN_OTHERS));
298             issuePTO.setUserHasIssueNotification(IssueUtilities.hasIssueNotification(issue, currUser.getId()));
299             
300             issuePTOs.add(issuePTO);
301         }
302         return issuePTOs;
303     }
304     
305     
306     public PortalHomeAction() {
307         super();
308   
309     }
310     
311 }