View Javadoc
1   package org.itracker.persistence.dao;
2   
3   import org.apache.commons.collections.CollectionUtils;
4   import org.apache.commons.collections.Predicate;
5   import org.apache.commons.collections.Transformer;
6   import org.hibernate.Criteria;
7   import org.hibernate.HibernateException;
8   import org.hibernate.Query;
9   import org.hibernate.criterion.Order;
10  import org.hibernate.criterion.Restrictions;
11  import org.itracker.model.*;
12  import org.itracker.model.util.IssueUtilities;
13  
14  import java.util.*;
15  
16  /**
17   * Default implementation of <code>IssueDAO</code> using Hibernate.
18   *
19   * @author ready
20   */
21  public class IssueDAOImpl extends BaseHibernateDAOImpl<Issue> implements IssueDAO {
22  
23      private ProjectDAO projectDAO;
24  
25      public Issue findByPrimaryKey(Integer issueId) {
26  
27          try {
28              Issue="../../../../org/itracker/model/Issue.html#Issue">Issue issue = (Issue) getSession().get(Issue.class, issueId);
29              if (null != issue) {
30                  getSession().refresh(issue);
31              } else {
32                  throw new NoSuchEntityException("Issue " + issueId);
33              }
34              return issue;
35          } catch (HibernateException ex) {
36              throw convertHibernateAccessException(ex);
37          }
38      }
39  
40      public Long countAllIssues() {
41  
42          final Long count;
43  
44          try {
45              final Query query = getSession().getNamedQuery("IssueCountAll");
46              count = (Long) query.uniqueResult();
47          } catch (HibernateException ex) {
48              throw convertHibernateAccessException(ex);
49          }
50  
51          return count;
52  
53      }
54  
55      @SuppressWarnings("unchecked")
56      public List<Issue> findAll() {
57  
58          final List<Issue> issues;
59  
60          try {
61              issues = getSession().getNamedQuery("IssuesAllQuery").list();
62          } catch (HibernateException ex) {
63              throw convertHibernateAccessException(ex);
64          }
65  
66          return issues;
67  
68      }
69  
70      @SuppressWarnings("unchecked")
71      public List<Issue> findByStatus(int status) {
72  
73          try {
74              Query query = getSession().getNamedQuery("IssuesByStatusQuery");
75              query.setInteger("issueStatus", status);
76              return query.list();
77          } catch (HibernateException ex) {
78              throw convertHibernateAccessException(ex);
79          }
80  
81      }
82  
83      @SuppressWarnings("unchecked")
84      public List<Issue> findByStatusLessThan(int maxExclusiveStatus) {
85  
86          final List<Issue> issues;
87  
88          try {
89              Query query = getSession().getNamedQuery("IssuesByStatusLessThanQuery");
90              query.setInteger("maxExclusiveStatus", maxExclusiveStatus);
91              issues = query.list();
92          } catch (HibernateException ex) {
93              throw convertHibernateAccessException(ex);
94          }
95  
96          return issues;
97  
98      }
99  
100     @SuppressWarnings("unchecked")
101     public List<Issue> findByStatusLessThanEqualTo(int maxStatus) {
102 
103         final List<Issue> issues;
104 
105         try {
106             Query query = getSession().getNamedQuery("IssuesByStatusLessThanEqualToQuery");
107             query.setInteger("maxStatus", maxStatus);
108             issues = query.list();
109         } catch (HibernateException ex) {
110             throw convertHibernateAccessException(ex);
111         }
112 
113         return issues;
114 
115     }
116 
117     @SuppressWarnings("unchecked")
118     public List<Issue> findByStatusLessThanEqualToInAvailableProjects(int maxStatus) {
119 
120         final List<Issue> issues;
121 
122         try {
123             Query query = getSession().getNamedQuery(
124                     "IssuesByStatusLessThanEqualToInAvailableProjectsQuery");
125             query.setInteger("maxStatus", maxStatus);
126             issues = query.list();
127         } catch (HibernateException ex) {
128             throw convertHibernateAccessException(ex);
129         }
130 
131         return issues;
132 
133     }
134 
135     @SuppressWarnings("unchecked")
136     public List<Issue> findBySeverity(int severity) {
137 
138         final List<Issue> issues;
139 
140         try {
141             Query query = getSession().getNamedQuery("IssuesBySeverityQuery");
142             query.setInteger("severity", severity);
143             issues = query.list();
144         } catch (HibernateException ex) {
145             throw convertHibernateAccessException(ex);
146         }
147 
148         return issues;
149 
150     }
151 
152     @SuppressWarnings("unchecked")
153     public List<Issue> findByProject(Integer projectId) {
154 
155         final List<Issue> issues;
156 
157         try {
158             Query query = getSession().getNamedQuery("IssuesByProjectQuery");
159             query.setInteger("projectId", projectId);
160             issues = query.list();
161         } catch (HibernateException ex) {
162             throw convertHibernateAccessException(ex);
163         }
164 
165         return issues;
166 
167     }
168 
169     public Long countByProject(Integer projectId) {
170 
171         final Long count;
172 
173         try {
174             final Query query = getSession().getNamedQuery(
175                     "IssueCountByProjectQuery");
176             query.setInteger("projectId", projectId);
177             count = (Long) query.uniqueResult();
178         } catch (HibernateException ex) {
179             throw convertHibernateAccessException(ex);
180         }
181 
182         return count;
183 
184     }
185 
186     @SuppressWarnings("unchecked")
187     public List<Issue> findByProjectAndLowerStatus(Integer projectId,
188                                                    int maxExclusiveStatus) {
189 
190         final List<Issue> issues;
191 
192         try {
193             Query query = getSession().getNamedQuery(
194                     "IssuesByProjectAndLowerStatusQuery");
195             query.setInteger("projectId", projectId);
196             query.setInteger("maxExclusiveStatus", maxExclusiveStatus);
197             issues = query.list();
198         } catch (HibernateException ex) {
199             throw convertHibernateAccessException(ex);
200         }
201 
202         return issues;
203 
204     }
205 
206     public Long countByProjectAndLowerStatus(Integer projectId,
207                                              int maxExclusiveStatus) {
208 
209         final Long count;
210 
211         try {
212             final Query query = getSession().getNamedQuery(
213                     "IssueCountByProjectAndLowerStatusQuery");
214             query.setInteger("projectId", projectId);
215             query.setInteger("maxExclusiveStatus", maxExclusiveStatus);
216             count = (Long) query.uniqueResult();
217         } catch (HibernateException ex) {
218             throw convertHibernateAccessException(ex);
219         }
220 
221         return count;
222 
223     }
224 
225     @SuppressWarnings("unchecked")
226     public List<Issue> findByProjectAndHigherStatus(Integer projectId,
227                                                     int status) {
228 
229         final List<Issue> issues;
230 
231         try {
232             Query query = getSession().getNamedQuery(
233                     "IssuesByProjectAndHigherStatusQuery");
234             query.setInteger("projectId", projectId);
235             query.setInteger("minStatus", status);
236             issues = query.list();
237         } catch (HibernateException ex) {
238             throw convertHibernateAccessException(ex);
239         }
240 
241         return issues;
242 
243     }
244 
245     public Long countByProjectAndHigherStatus(Integer projectId, int minStatus) {
246 
247         final Long count;
248 
249         try {
250             final Query query = getSession().getNamedQuery(
251                     "IssueCountByProjectAndHigherStatusQuery");
252             query.setInteger("projectId", projectId);
253             query.setInteger("minStatus", minStatus);
254             count = (Long) query.uniqueResult();
255         } catch (HibernateException ex) {
256             throw convertHibernateAccessException(ex);
257         }
258 
259         return count;
260 
261     }
262 
263     @SuppressWarnings("unchecked")
264     public List<Issue> findByOwner(Integer ownerId, int maxExclusiveStatus) {
265 
266         final List<Issue> issues;
267 
268         try {
269             Query query = getSession().getNamedQuery("IssuesByOwnerQuery");
270             query.setInteger("ownerId", ownerId);
271             query.setInteger("maxExclusiveStatus", maxExclusiveStatus);
272             issues = query.list();
273         } catch (HibernateException ex) {
274             throw convertHibernateAccessException(ex);
275         }
276 
277         return issues;
278 
279     }
280 
281     @SuppressWarnings("unchecked")
282     public List<Issue> findByOwnerInAvailableProjects(Integer ownerId,
283                                                       int maxExclusiveStatus) {
284 
285         final List<Issue> issues;
286 
287         try {
288             Query query = getSession().getNamedQuery(
289                     "IssuesByOwnerInAvailableProjectsQuery");
290             query.setInteger("ownerId", ownerId);
291             query.setInteger("maxExclusiveStatus", maxExclusiveStatus);
292 
293             issues = query.list();
294         } catch (HibernateException ex) {
295             throw convertHibernateAccessException(ex);
296         }
297 
298         return issues;
299 
300     }
301 
302     @SuppressWarnings("unchecked")
303     public List<Issue> findUnassignedIssues(int maxStatus) {
304 
305         final List<Issue> issues;
306 
307         try {
308             Query query = getSession().getNamedQuery("IssuesUnassignedQuery");
309             query.setInteger("maxStatus", maxStatus);
310 
311             issues = query.list();
312         } catch (HibernateException ex) {
313             throw convertHibernateAccessException(ex);
314         }
315 
316         return issues;
317 
318     }
319 
320     @SuppressWarnings("unchecked")
321     public List<Issue> findByCreator(Integer creatorId,
322                                      int maxExclusiveStatus) {
323 
324         final List<Issue> issues;
325 
326         try {
327             Query query = getSession().getNamedQuery("IssuesByCreatorQuery");
328             query.setInteger("creatorId", creatorId);
329             query.setInteger("maxExclusiveStatus", maxExclusiveStatus);
330             issues = query.list();
331         } catch (HibernateException ex) {
332             throw convertHibernateAccessException(ex);
333         }
334 
335         return issues;
336 
337     }
338 
339     @SuppressWarnings("unchecked")
340     public List<Issue> findByCreatorInAvailableProjects(Integer creatorId,
341                                                         int maxExclusiveStatus) {
342 
343         final List<Issue> issues;
344 
345         try {
346             Query query = getSession().getNamedQuery(
347                     "IssuesByCreatorInAvailableProjectsQuery");
348             query.setInteger("creatorId", creatorId);
349             query.setInteger("maxExclusiveStatus", maxExclusiveStatus);
350             issues = query.list();
351         } catch (HibernateException ex) {
352             throw convertHibernateAccessException(ex);
353         }
354 
355         return issues;
356 
357     }
358 
359     @SuppressWarnings("unchecked")
360     public List<Issue> findByNotification(Integer userId,
361                                           int maxExclusiveStatus) {
362 
363         final List<Issue> issues;
364 
365         try {
366             final Query query = getSession().getNamedQuery(
367                     "IssuesByNotificationQuery");
368             query.setInteger("userId", userId);
369             query.setInteger("maxExclusiveStatus", maxExclusiveStatus);
370             issues = query.list();
371         } catch (HibernateException ex) {
372             throw convertHibernateAccessException(ex);
373         }
374 
375         return issues;
376 
377     }
378 
379     @SuppressWarnings("unchecked")
380     public List<Issue> findByNotificationInAvailableProjects(Integer userId,
381                                                              int maxExclusiveStatus) {
382 
383         final List<Issue> issues;
384 
385         try {
386             final Query query = getSession().getNamedQuery(
387                     "IssuesByNotificationInAvailableProjectsQuery");
388             query.setInteger("userId", userId);
389             //@ToDo Check this query.
390             query.setInteger("maxExclusiveStatus", maxExclusiveStatus);
391             issues = query.list();
392         } catch (HibernateException ex) {
393             throw convertHibernateAccessException(ex);
394         }
395 
396         return issues;
397 
398     }
399 
400     @SuppressWarnings("unchecked")
401     public List<Issue> findByComponent(Integer componentId) {
402 
403         final List<Issue> issues;
404 
405         try {
406             final Query query = getSession().getNamedQuery(
407                     "IssuesByComponentQuery");
408             query.setInteger("componentId", componentId);
409             issues = query.list();
410         } catch (HibernateException ex) {
411             throw convertHibernateAccessException(ex);
412         }
413 
414         return issues;
415 
416     }
417 
418     public Long countByComponent(Integer componentId) {
419 
420         final Long count;
421 
422         try {
423             final Query query = getSession().getNamedQuery(
424                     "IssueCountByComponentQuery");
425             query.setInteger("componentId", componentId);
426             count = (Long) query.uniqueResult();
427         } catch (HibernateException ex) {
428             throw convertHibernateAccessException(ex);
429         }
430 
431         return count;
432 
433     }
434 
435     @SuppressWarnings("unchecked")
436     public List<Issue> findByVersion(Integer versionId) {
437 
438         final List<Issue> issues;
439 
440         try {
441             final Query query = getSession().getNamedQuery(
442                     "IssuesByVersionQuery");
443             query.setInteger("versionId", versionId);
444             issues = query.list();
445         } catch (HibernateException ex) {
446             throw convertHibernateAccessException(ex);
447         }
448 
449         return issues;
450 
451     }
452 
453     public Long countByVersion(Integer versionId) {
454 
455         final Long count;
456 
457         try {
458             final Query query = getSession().getNamedQuery(
459                     "IssueCountByVersionQuery");
460             query.setInteger("versionId", versionId);
461             count = (Long) query.uniqueResult();
462         } catch (HibernateException ex) {
463             throw convertHibernateAccessException(ex);
464         }
465 
466         return count;
467 
468     }
469 
470     public Date latestModificationDate(Integer projectId) {
471 
472         final Date lastModifiedDate;
473 
474         try {
475             final Query query = getSession().getNamedQuery(
476                     "MaxIssueModificationDateQuery");
477             query.setInteger("projectId", projectId);
478             lastModifiedDate = (Date) query.uniqueResult();
479         } catch (HibernateException ex) {
480             throw convertHibernateAccessException(ex);
481         }
482 
483         return lastModifiedDate;
484 
485     }
486 
487     public List<Issue> findNextIssues(Integer issueId) {
488         List<Issue> issue;
489         try {
490             final Query query = getSession().getNamedQuery(
491                     "FindNextIssues");
492             query.setInteger("issueId", issueId);
493             issue = query.list();
494         } catch (HibernateException ex) {
495             throw convertHibernateAccessException(ex);
496         }
497         return issue;
498     }
499     public List<Issue> findPreviousIssues(Integer issueId) {
500         List<Issue> issue;
501         try {
502             final Query query = getSession().getNamedQuery(
503                     "FindPreviousIssues");
504             query.setInteger("issueId", issueId);
505             issue = query.list();
506         } catch (HibernateException ex) {
507             throw convertHibernateAccessException(ex);
508         }
509         return issue;
510     }
511 
512     /**
513      * It doens't really make sense for this method to receive projectDAO, it's just a quick
514      * fix for the fact that IssueSearchQuery handles ids and not objects
515      */
516     @SuppressWarnings("unchecked")
517     public List<Issue> query(
518             IssueSearchQuery searchQuery,
519             final User user,
520             final Map<Integer, Set<PermissionType>> userPermissions) {
521 
522         Criteria criteria = getSession().createCriteria(Issue.class);
523 
524         // projects
525         Collection<Project> projects = Collections.checkedCollection((Collection<Project>) getProjectsObjects(searchQuery), Project.class);
526 
527         if (projects.size() > 0) {
528             criteria.add(Restrictions.in("project", projects));
529         }
530 
531         // severities
532         if (searchQuery.getSeverities().size() > 0) {
533             criteria.add(Restrictions.in("severity", searchQuery.getSeverities()));
534         }
535 
536         // status
537         if (searchQuery.getStatuses().size() > 0) {
538             criteria.add(Restrictions.in("status", searchQuery.getStatuses()));
539         }
540 
541         // componentes
542         if (searchQuery.getComponents().size() > 0) {
543             criteria.createCriteria("components").add(Restrictions.in("id", searchQuery.getComponents()));
544         }
545 
546         // versions
547         if (searchQuery.getVersions().size() > 0) {
548             criteria.createCriteria("versions").add(Restrictions.in("id", searchQuery.getVersions()));
549         }
550 
551         // creator
552         if (searchQuery.getCreator() != null) {
553             criteria.add(Restrictions.eq("creator", searchQuery.getCreator()));
554         }
555 
556         // owner
557         if (searchQuery.getOwner() != null) {
558             criteria.add(Restrictions.eq("owner", searchQuery.getOwner()));
559         }
560 
561         // description and history
562         if (searchQuery.getText() != null && !searchQuery.getText().equals("")) {
563             criteria.createAlias("history", "history").
564                     add(Restrictions.or(
565                             Restrictions.ilike("description", "%" + searchQuery.getText() + "%"),
566                             Restrictions.ilike("history.description", "%" + searchQuery.getText() + "%")
567                     ));
568         }
569 
570         // resolution
571         if (searchQuery.getResolution() != null) {
572             criteria.add(Restrictions.eq("resolution", searchQuery.getResolution() + "%"));
573         }
574 
575         // resolution
576         if (searchQuery.getTargetVersion() != null) {
577             criteria.add(Restrictions.eq("targetVersion.id", searchQuery.getTargetVersion()));
578         }
579 
580 
581         // sort
582         String order = searchQuery.getOrderBy();
583         if (!"id".equals(order)) {
584             if ("sev".equals(order)) {
585                 criteria.addOrder(order("severity", true));
586             } else if ("proj".equals(order)) {
587                 criteria.addOrder(order("project", true)).addOrder(order("status", false));
588             } else if ("owner".equals(order)) {
589                 criteria.addOrder(order("owner", true)).addOrder(order("status", false));
590             } else if ("lm".equals(order)) {
591                 criteria.addOrder(order("lastModifiedDate", true));
592             } else {
593                 criteria.addOrder(order("status", true));
594             }
595         }
596         criteria.addOrder(order("id", true));
597 
598         List<Issue> list = Collections.checkedList(Criteria.DISTINCT_ROOT_ENTITY.transformList(criteria.list()), Issue.class);
599 
600         // filter for permission
601         list = new LinkedList<Issue>(Collections.checkedCollection(CollectionUtils.select(list, new Predicate() {
602             public boolean evaluate(Object arg0) {
603                 return IssueUtilities.canViewIssue((Issue) arg0, user, userPermissions);
604             }
605         }), Issue.class));
606 
607 
608         return list;
609 
610     }
611 
612     // from the list of project ids this objects has, return a list of
613     // projects
614     public Collection<?> getProjectsObjects(final IssueSearchQuery query) {
615         return CollectionUtils.collect(query.getProjects(), new Transformer() {
616             public Object transform(Object arg0) {
617                 return projectDAO.findByPrimaryKey((Integer) arg0);
618             }
619         });
620     }
621     Order order(String propertyName, boolean asc) {
622         return asc ? Order.asc(propertyName) : Order.desc(propertyName);
623     }
624 
625     public ProjectDAO getProjectDAO() {
626         return projectDAO;
627     }
628 
629     public void setProjectDAO(ProjectDAO projectDAO) {
630         this.projectDAO = projectDAO;
631     }
632 
633     /**
634      * {@inheritDoc}
635      */
636     @SuppressWarnings("unchecked")
637     public List<Issue> findByTargetVersion(Integer versionId) {
638 
639         try {
640             final Query query = getSession().getNamedQuery("FindByTargetVersion");
641             query.setInteger("versionId", versionId);
642             return query.list();
643         } catch (HibernateException ex) {
644             throw convertHibernateAccessException(ex);
645         }
646 
647 
648     }
649 
650 
651 }