View Javadoc
1   package org.itracker.web.actions.project;
2   
3   import org.apache.log4j.Logger;
4   import org.apache.struts.action.ActionForm;
5   import org.apache.struts.action.ActionForward;
6   import org.apache.struts.action.ActionMapping;
7   import org.itracker.model.PermissionType;
8   import org.itracker.model.Project;
9   import org.itracker.model.util.UserUtilities;
10  import org.itracker.services.ProjectService;
11  import org.itracker.web.actions.base.ItrackerBaseAction;
12  import org.itracker.web.ptos.ProjectPTO;
13  import org.itracker.web.util.RequestHelper;
14  import org.itracker.web.util.ServletContextUtils;
15  
16  import javax.servlet.http.HttpServletRequest;
17  import javax.servlet.http.HttpServletResponse;
18  import java.util.*;
19  
20  public class ListProjectsAction extends ItrackerBaseAction {
21  	private static final Logger log = Logger
22  			.getLogger(ListProjectsAction.class);
23  
24  	/**
25  	 * returns listing of ALL projects with given permissions. Also locked and
26  	 * view-only projects are selected.
27  	 * 
28  	 * @param projectService
29  	 * @param permissionFlags
30  	 * @param permissions
31  	 * @return
32  	 */
33  	protected static List<ProjectPTO> getAllPTOs(ProjectService projectService,
34  			PermissionType[] permissionFlags,
35  			final Map<Integer, Set<PermissionType>> permissions) {
36  		List<Project> projects = projectService.getAllProjects();
37  
38  		ArrayList<Project> projects_tmp = new ArrayList<Project>(projects);
39  		Iterator<Project> projectIt = projects.iterator();
40  		while (projectIt.hasNext()) {
41  			Project project = projectIt.next();
42  			if (!UserUtilities.hasPermission(permissions, project.getId(),
43  					permissionFlags)) {
44  				projects_tmp.remove(project);
45  			}
46  		}
47  
48  		projects = projects_tmp;
49  		Collections.sort(projects, new Project.ProjectComparator());
50  
51  		ArrayList<ProjectPTO> ptos = new ArrayList<>(projects_tmp
52  				.size());
53  
54  		projectIt = projects.iterator();
55  
56  		while (projectIt.hasNext()) {
57  			Project project = projectIt.next();
58  			ptos.add(createProjectPTO(project, projectService, permissions));
59  		}
60  
61  		return ptos;
62  	}
63  
64  	protected static List<ProjectPTO> getPTOs(ProjectService projectService,
65  			PermissionType[] permissionFlags,
66  			final Map<Integer, Set<PermissionType>> permissions) {
67  		List<Project> projects = projectService.getAllAvailableProjects();
68  
69  		ArrayList<Project> projects_tmp = new ArrayList<Project>(projects);
70  		Iterator<Project> projectIt = projects.iterator();
71  		while (projectIt.hasNext()) {
72  			Project project = (Project) projectIt.next();
73  			if (!UserUtilities.hasPermission(permissions, project.getId(),
74  					permissionFlags)) {
75  				projects_tmp.remove(project);
76  			}
77  		}
78  
79  		projects = projects_tmp;
80  		Collections.sort(projects, new Project.ProjectComparator());
81  
82  		ArrayList<ProjectPTO> ptos = new ArrayList<ProjectPTO>(projects_tmp
83  				.size());
84  
85  		projectIt = projects.iterator();
86  
87  		while (projectIt.hasNext()) {
88  			Project project = projectIt.next();
89  			ptos.add(createProjectPTO(project, projectService, permissions));
90  		}
91  
92  		return ptos;
93  	}
94  
95  	public ActionForward execute(ActionMapping mapping, ActionForm form,
96  			HttpServletRequest request, HttpServletResponse response)
97  			throws Exception {
98  
99  		final Map<Integer, Set<PermissionType>> permissions = RequestHelper
100 				.getUserPermissions(request.getSession());
101 
102 		ProjectService projectService = ServletContextUtils.getItrackerServices()
103 				.getProjectService();
104 
105 		request.setAttribute("projects", getPTOs(projectService, new PermissionType[] {
106 				PermissionType.ISSUE_VIEW_ALL,
107 				PermissionType.ISSUE_VIEW_USERS }, permissions));
108 
109 		// TODO add a secure token to feed-URL
110         request.setAttribute("rssFeed", "/servlets/issues/");
111 
112 		String pageTitleKey = "itracker.web.listprojects.title";
113 		String pageTitleArg = "";
114 
115 		request.setAttribute("pageTitleKey", pageTitleKey);
116 		request.setAttribute("pageTitleArg", pageTitleArg);
117 
118 		log.info("ListProjectsAction: Forward: listprojects");
119 		return mapping.findForward("list_projects");
120 	}
121 
122 	private static void setupNumberOfIssues(ProjectPTO pto,
123 			ProjectService service) {
124 		pto.setTotalNumberIssues(service.getTotalNumberIssuesByProject(pto
125 				.getId()));
126 	}
127 
128 	private static void setupNumberOfOpenIssues(ProjectPTO pto,
129 			ProjectService service) {
130 		pto.setTotalOpenIssues(service.getTotalNumberOpenIssuesByProject(pto
131 				.getId()));
132 	}
133 
134 	private static void setupNumberOfResolvedIssues(ProjectPTO pto,
135 			ProjectService service) {
136 		pto.setTotalResolvedIssues(service
137 				.getTotalNumberResolvedIssuesByProject(pto.getId()));
138 	}
139 
140 	private static void setupCanCreate(ProjectPTO pto,
141 			final Map<Integer, Set<PermissionType>> permissions) {
142 		pto.setCanCreate(UserUtilities.hasPermission(permissions, pto.getId(),
143 				PermissionType.ISSUE_CREATE));
144 	}
145 
146 	private static void setupLastIssueUpdateDate(ProjectPTO pto,
147 			ProjectService service) {
148 		pto.setLastUpdatedIssueDate(service
149 				.getLatestIssueUpdatedDateByProjectId(pto.getId()));
150 	}
151 
152 	/**
153 	 * 
154 	 * Makes a page transfer object for the project in first argument.
155 	 * 
156 	 * @param project
157 	 *            - wrapped project for the pto
158 	 * @param projectService
159 	 *            - project-service
160 	 * @param permissions
161 	 *            - users permissions
162 
163 	 * @return
164 	 */
165 	private static ProjectPTO createProjectPTO(Project project,
166 			ProjectService projectService,
167 			final Map<Integer, Set<PermissionType>> permissions) {
168 		ProjectPTO pto = new ProjectPTO();
169 		pto.setCreateDate(project.getCreateDate());
170 		pto.setModifiedDate(project.getLastModifiedDate());
171 		pto.setId(project.getId());
172 		pto.setDescription(project.getDescription());
173 		pto.setName(project.getName());
174 		pto.setStatus(project.getStatus());
175 		setupCanCreate(pto, permissions);
176 		setupLastIssueUpdateDate(pto, projectService);
177 		setupNumberOfIssues(pto, projectService);
178 		setupNumberOfOpenIssues(pto, projectService);
179 		setupNumberOfResolvedIssues(pto, projectService);
180 		return pto;
181 	}
182 }