1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.itracker.services.implementations;
20
21 import org.apache.log4j.Logger;
22 import org.itracker.model.*;
23 import org.itracker.model.util.IssueUtilities;
24 import org.itracker.persistence.dao.*;
25 import org.itracker.services.ProjectService;
26
27 import java.util.*;
28
29 public class ProjectServiceImpl implements ProjectService {
30
31
32
33
34 private static final Logger logger = Logger.getLogger(ProjectService.class);
35
36 private ComponentDAO componentDAO;
37 private CustomFieldDAO customFieldDAO;
38 private ProjectDAO projectDAO;
39 private ProjectScriptDAO projectScriptDAO;
40 private UserDAO userDAO;
41 private VersionDAO versionDAO;
42 private IssueDAO issueDAO;
43
44 public ProjectServiceImpl(ComponentDAO componentDAO,
45 CustomFieldDAO customFieldDAO, ProjectDAO projectDAO,
46 ProjectScriptDAO projectScriptDAO, UserDAO userDAO,
47 VersionDAO versionDAO, IssueDAO issueDAO) {
48 this.componentDAO = componentDAO;
49 this.customFieldDAO = customFieldDAO;
50 this.projectDAO = projectDAO;
51 this.projectScriptDAO = projectScriptDAO;
52 this.userDAO = userDAO;
53 this.versionDAO = versionDAO;
54 this.issueDAO = issueDAO;
55 }
56
57 public Project getProject(Integer projectId) {
58 Project project = getProjectDAO().findByPrimaryKey(projectId);
59 return project;
60 }
61
62 public List<Project> getAllProjects() {
63 return getProjectDAO().findAll();
64 }
65
66 public List<Project> getAllAvailableProjects() {
67 List<Project> projects = getProjectDAO().findAllAvailable();
68 return projects;
69 }
70
71 public List<Project> getListOfAllAvailableProjects() {
72 return getAllAvailableProjects();
73 }
74
75 public Component updateProjectComponent(Component component) {
76 getComponentDAO().saveOrUpdate(component);
77 return component;
78 }
79
80 public Component addProjectComponent(Integer projectId, Component component) {
81 Project project = getProjectDAO().findByPrimaryKey(projectId);
82
83 component.setProject(project);
84 project.getComponents().add(component);
85 getComponentDAO().save(component);
86 getProjectDAO().save(project);
87
88 return component;
89 }
90
91 public boolean removeProjectComponent(Integer projectId, Integer componentId) {
92
93 Component component = getComponentDAO().findById(componentId);
94 if (component == null) {
95 return false;
96 }
97
98 if (!component.getProject().getId().equals(projectId)) {
99 return false;
100 }
101
102 getComponentDAO().delete(component);
103
104 return true;
105
106 }
107
108 public Component getProjectComponent(Integer componentId) {
109
110 Component component = getComponentDAO().findById(componentId);
111
112 return component;
113
114 }
115
116 public Version addProjectVersion(Integer projectId, Version version) {
117 version.setCreateDate(new Date());
118
119 Project project = getProjectDAO().findByPrimaryKey(projectId);
120 version.setProject(project);
121
122 Collection<Version> versions = project.getVersions();
123 versions.add(version);
124 getVersionDAO().save(version);
125
126 return version;
127
128 }
129
130 public boolean removeProjectVersion(Integer projectId, Integer versionId) {
131
132 Version version = getVersionDAO().findByPrimaryKey(versionId);
133 if (version == null) {
134 return false;
135 }
136
137 if (!version.getProject().getId().equals(projectId)) {
138 return false;
139 }
140
141 List<Issue> issues = getIssueDAO().findByTargetVersion(version.getId());
142 Iterator<Issue> iterator = issues.iterator();
143 while (iterator.hasNext()) {
144 Issue issue = (Issue) iterator.next();
145 issue.setTargetVersion(null);
146 getIssueDAO().save(issue);
147 }
148
149 issues = getIssueDAO().findByVersion(version.getId());
150 iterator = issues.iterator();
151 while (iterator.hasNext()) {
152 Issue issue = (Issue) iterator.next();
153 if (issue.getVersions().remove(version)) {
154 getIssueDAO().save(issue);
155 }
156 }
157
158 getVersionDAO().delete(version);
159 return true;
160 }
161
162 public Version updateProjectVersion(Version version) {
163 getVersionDAO().saveOrUpdate(version);
164 return version;
165 }
166
167 public Version getProjectVersion(Integer versionId) {
168 Version version = getVersionDAO().findByPrimaryKey(versionId);
169
170 return version;
171
172 }
173
174 public List<User> getProjectOwners(Integer projectId) {
175 Project project = getProjectDAO().findByPrimaryKey(projectId);
176 List<User> users = project.getOwners();
177 return users;
178 }
179
180 public boolean setProjectOwners(Project project,
181 Set<Integer> setOfNewOwnerIds) {
182 if (null == project) {
183 logger.warn("setProjectOwners, project was null");
184 throw new IllegalArgumentException("Project must not be null.");
185 }
186 if (null == setOfNewOwnerIds) {
187 setOfNewOwnerIds = new HashSet<Integer>(0);
188 }
189
190 List<User> owners = new ArrayList<User>(setOfNewOwnerIds.size());
191 if (!setOfNewOwnerIds.isEmpty()) {
192
193 for (Iterator<Integer> iterator = setOfNewOwnerIds.iterator(); iterator
194 .hasNext(); ) {
195 Integer ownerId = iterator.next();
196 User owner = getUserDAO().findByPrimaryKey(ownerId);
197 owners.add(owner);
198 }
199 }
200 project.setOwners(owners);
201
202 return true;
203 }
204
205 public List<CustomField> getProjectFields(Integer projectId) {
206 return getProjectFields(projectId, null);
207 }
208
209
210
211
212 public List<CustomField> getProjectFields(Integer projectId, Locale locale) {
213
214 Project project = projectDAO.findByPrimaryKey(projectId);
215 List<CustomField> fields = project.getCustomFields();
216
217 return fields;
218 }
219
220 public boolean setProjectFields(Project project,
221 Set<Integer> setOfNewsFieldIds) {
222 List<CustomField> fields;
223 fields = project.getCustomFields();
224 fields.clear();
225
226 if (setOfNewsFieldIds != null && !setOfNewsFieldIds.isEmpty()) {
227 for (Iterator<Integer> iterator = setOfNewsFieldIds.iterator(); iterator
228 .hasNext(); ) {
229 Integer fieldId = iterator.next();
230 CustomField field = customFieldDAO.findByPrimaryKey(fieldId);
231 fields.add(field);
232 }
233 }
234 return true;
235 }
236
237 public ProjectScript getProjectScript(Integer scriptId) {
238 ProjectScript projectScript = this.projectScriptDAO
239 .findByPrimaryKey(scriptId);
240 return projectScript;
241
242 }
243
244 public List<ProjectScript> getProjectScripts() {
245 List<ProjectScript> projectScripts = this.projectScriptDAO.findAll();
246 return projectScripts;
247 }
248
249 public ProjectScript addProjectScript(Integer projectId,
250 ProjectScript projectScript) {
251 ProjectScript addprojectScript = new ProjectScript();
252 addprojectScript.setId(projectScript.getId());
253 addprojectScript.setFieldId(projectScript.getFieldId());
254 addprojectScript.setFieldType(projectScript.getFieldType());
255 addprojectScript.setPriority(projectScript.getPriority());
256 addprojectScript.setProject(projectScript.getProject());
257 addprojectScript.setScript(projectScript.getScript());
258 this.projectScriptDAO.save(addprojectScript);
259
260 return addprojectScript;
261
262 }
263
264 public boolean removeProjectScript(Integer projectId, Integer scriptId) {
265 ProjectScript script = projectScriptDAO.findByPrimaryKey(scriptId);
266 this.projectScriptDAO.delete(script);
267
268
269 return true;
270
271 }
272
273 public ProjectScript updateProjectScript(ProjectScript projectScript) {
274 ProjectScript editprojectScript = projectScriptDAO
275 .findByPrimaryKey(projectScript.getId());
276 editprojectScript.setId(projectScript.getId());
277 editprojectScript.setFieldId(projectScript.getFieldId());
278 editprojectScript.setPriority(projectScript.getPriority());
279 editprojectScript.setProject(projectScript.getProject());
280 editprojectScript.setScript(projectScript.getScript());
281
282 this.projectScriptDAO.saveOrUpdate(editprojectScript);
283
284 return editprojectScript;
285 }
286
287 public Long getTotalNumberIssuesByProject(Integer projectId) {
288 return issueDAO.countByProject(projectId);
289 }
290
291 public Long countIssuesByVersion(Integer versionId) {
292 return issueDAO.countByVersion(versionId);
293
294 }
295
296 public Long countIssuesByComponent(Integer componentId) {
297 return issueDAO.countByComponent(componentId);
298 }
299
300 public Long[] getProjectStats(Integer projectId) {
301 final Long[] issueStats = new Long[2];
302
303 Long openIssuesCount = issueDAO.countByProjectAndLowerStatus(projectId,
304 IssueUtilities.STATUS_RESOLVED);
305
306 issueStats[0] = openIssuesCount;
307
308 Long resolvedIssuesCount = issueDAO.countByProjectAndHigherStatus(
309 projectId, IssueUtilities.STATUS_RESOLVED);
310 issueStats[1] = resolvedIssuesCount;
311
312 return issueStats;
313 }
314
315
316 @SuppressWarnings("unused")
317 private IssueDAO getIssueDAO() {
318 return issueDAO;
319 }
320
321 private ProjectDAO getProjectDAO() {
322 return projectDAO;
323 }
324
325 private ComponentDAO getComponentDAO() {
326 return componentDAO;
327 }
328
329 private CustomFieldDAO getCustomFieldDAO() {
330 return this.customFieldDAO;
331 }
332
333 private ProjectScriptDAO getProjectScriptDAO() {
334 return this.projectScriptDAO;
335 }
336
337 private VersionDAO getVersionDAO() {
338 return this.versionDAO;
339 }
340
341 private UserDAO getUserDAO() {
342 return this.userDAO;
343 }
344
345 public List<CustomField> getListOfProjectFields(Integer projectId) {
346 throw new UnsupportedOperationException();
347 }
348
349 public List<User> getListOfProjectOwners(Integer projectId) {
350 throw new UnsupportedOperationException();
351 }
352
353 public Long getTotalNumberOpenIssuesByProject(Integer projectId) {
354 return issueDAO.countByProjectAndLowerStatus(projectId,
355 IssueUtilities.STATUS_RESOLVED);
356 }
357
358 public Long getTotalNumberResolvedIssuesByProject(Integer projectId) {
359 return issueDAO.countByProjectAndHigherStatus(projectId,
360 IssueUtilities.STATUS_RESOLVED);
361 }
362
363 public Date getLatestIssueUpdatedDateByProjectId(Integer projectId) {
364 return issueDAO.latestModificationDate(projectId);
365 }
366
367 public Project createProject(Project project, Integer userId) {
368 project.setId(null);
369 User user = getUserDAO().findByPrimaryKey(userId);
370 project.setOwners(Arrays.asList(new User[]{user}));
371 getProjectDAO().save(project);
372
373 return project;
374 }
375
376 public Project updateProject(Project project, Integer userId) {
377 User user = getUserDAO().findByPrimaryKey(userId);
378
379 getProjectDAO().saveOrUpdate(project);
380
381 return project;
382 }
383
384 public Boolean isUniqueProjectName(String projectName,
385 Integer updatedProjectId) {
386 Project project = getProjectDAO().findByName(projectName);
387 if (project != null) {
388
389 if (!project.getId().equals(updatedProjectId)) {
390 return false;
391 }
392 }
393 return true;
394 }
395 }