Committing large number of changes relating to versioning implementation (#108)
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / persistence / dao / hibernate / media / MediaDaoHibernateImpl.java
1 /**
2 * Copyright (C) 2008 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 */
6
7 package eu.etaxonomy.cdm.persistence.dao.hibernate.media;
8
9 import java.util.HashSet;
10 import java.util.List;
11 import java.util.Set;
12
13 import org.hibernate.Criteria;
14 import org.hibernate.criterion.Projections;
15 import org.hibernate.criterion.Restrictions;
16 import org.springframework.stereotype.Repository;
17
18 import eu.etaxonomy.cdm.model.description.IdentificationKey;
19 import eu.etaxonomy.cdm.model.location.NamedArea;
20 import eu.etaxonomy.cdm.model.media.Media;
21 import eu.etaxonomy.cdm.model.taxon.Taxon;
22 import eu.etaxonomy.cdm.persistence.dao.hibernate.common.AnnotatableDaoImpl;
23 import eu.etaxonomy.cdm.persistence.dao.media.IMediaDao;
24
25 /**
26 * @author a.babadshanjan
27 * @created 08.09.2008
28 */
29 @Repository
30 public class MediaDaoHibernateImpl extends AnnotatableDaoImpl<Media>
31 implements IMediaDao {
32
33 public MediaDaoHibernateImpl() {
34 super(Media.class);
35 }
36
37 public int countIdentificationKeys(Set<Taxon> taxonomicScope, Set<NamedArea> geoScopes) {
38 checkNotInPriorView("MediaDaoHibernateImpl.countIdentificationKeys(Set<Taxon> taxonomicScope, Set<NamedArea> geoScopes)");
39 Criteria criteria = getSession().createCriteria(IdentificationKey.class);
40
41 if(taxonomicScope != null && !taxonomicScope.isEmpty()) {
42 Set<Integer> taxonomicScopeIds = new HashSet<Integer>();
43 for(Taxon n : taxonomicScope) {
44 taxonomicScopeIds.add(n.getId());
45 }
46 criteria.createCriteria("taxonomicScope").add(Restrictions.in("id", taxonomicScopeIds));
47 }
48
49 if(geoScopes != null && !geoScopes.isEmpty()) {
50 Set<Integer> geoScopeIds = new HashSet<Integer>();
51 for(NamedArea n : geoScopes) {
52 geoScopeIds.add(n.getId());
53 }
54 criteria.createCriteria("geoScopes").add(Restrictions.in("id", geoScopeIds));
55 }
56
57 criteria.setProjection(Projections.countDistinct("id"));
58
59 return (Integer)criteria.uniqueResult();
60 }
61
62 public List<IdentificationKey> getIdentificationKeys(Set<Taxon> taxonomicScope, Set<NamedArea> geoScopes, Integer pageSize, Integer pageNumber) {
63 checkNotInPriorView("MediaDaoHibernateImpl.getIdentificationKeys(Set<Taxon> taxonomicScope, Set<NamedArea> geoScopes, Integer pageSize, Integer pageNumber)");
64 Criteria inner = getSession().createCriteria(IdentificationKey.class);
65
66 if(taxonomicScope != null && !taxonomicScope.isEmpty()) {
67 Set<Integer> taxonomicScopeIds = new HashSet<Integer>();
68 for(Taxon n : taxonomicScope) {
69 taxonomicScopeIds.add(n.getId());
70 }
71 inner.createCriteria("taxonomicScope").add(Restrictions.in("id", taxonomicScopeIds));
72 }
73
74 if(geoScopes != null && !geoScopes.isEmpty()) {
75 Set<Integer> geoScopeIds = new HashSet<Integer>();
76 for(NamedArea n : geoScopes) {
77 geoScopeIds.add(n.getId());
78 }
79 inner.createCriteria("geoScopes").add(Restrictions.in("id", geoScopeIds));
80 }
81
82 inner.setProjection(Projections.distinct(Projections.id()));
83
84 Criteria criteria = getSession().createCriteria(IdentificationKey.class);
85 criteria.add(Restrictions.in("id", (List<Integer>)inner.list()));
86
87 if(pageSize != null) {
88 criteria.setMaxResults(pageSize);
89 if(pageNumber != null) {
90 criteria.setFirstResult(pageNumber * pageSize);
91 }
92 }
93
94 return (List<IdentificationKey>)criteria.list();
95 }
96 }