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.Hibernate;
|
15
|
import org.hibernate.Query;
|
16
|
import org.hibernate.criterion.Projections;
|
17
|
import org.hibernate.criterion.Restrictions;
|
18
|
import org.hibernate.envers.query.AuditEntity;
|
19
|
import org.hibernate.envers.query.AuditQuery;
|
20
|
import org.hibernate.search.FullTextSession;
|
21
|
import org.hibernate.search.Search;
|
22
|
import org.springframework.stereotype.Repository;
|
23
|
|
24
|
import eu.etaxonomy.cdm.model.description.MediaKey;
|
25
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
26
|
import eu.etaxonomy.cdm.model.media.Media;
|
27
|
import eu.etaxonomy.cdm.model.media.Rights;
|
28
|
import eu.etaxonomy.cdm.model.molecular.PhylogeneticTree;
|
29
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
30
|
import eu.etaxonomy.cdm.model.view.AuditEvent;
|
31
|
import eu.etaxonomy.cdm.persistence.dao.common.OperationNotSupportedInPriorViewException;
|
32
|
import eu.etaxonomy.cdm.persistence.dao.hibernate.common.IdentifiableDaoBase;
|
33
|
import eu.etaxonomy.cdm.persistence.dao.media.IMediaDao;
|
34
|
|
35
|
/**
|
36
|
* @author a.babadshanjan
|
37
|
* @since 08.09.2008
|
38
|
*/
|
39
|
@Repository
|
40
|
public class MediaDaoHibernateImpl extends IdentifiableDaoBase<Media> implements IMediaDao {
|
41
|
|
42
|
protected String getDefaultField() {
|
43
|
return "title.text";
|
44
|
}
|
45
|
|
46
|
public MediaDaoHibernateImpl() {
|
47
|
super(Media.class);
|
48
|
indexedClasses = new Class[3];
|
49
|
indexedClasses[0] = Media.class;
|
50
|
indexedClasses[1] = MediaKey.class;
|
51
|
indexedClasses[2] = PhylogeneticTree.class;
|
52
|
}
|
53
|
|
54
|
@Override
|
55
|
public int countMediaKeys(Set<Taxon> taxonomicScope, Set<NamedArea> geoScopes) {
|
56
|
AuditEvent auditEvent = getAuditEventFromContext();
|
57
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
58
|
Criteria criteria = getSession().createCriteria(MediaKey.class);
|
59
|
|
60
|
if(taxonomicScope != null && !taxonomicScope.isEmpty()) {
|
61
|
Set<Integer> taxonomicScopeIds = new HashSet<Integer>();
|
62
|
for(Taxon n : taxonomicScope) {
|
63
|
taxonomicScopeIds.add(n.getId());
|
64
|
}
|
65
|
criteria.createCriteria("taxonomicScope").add(Restrictions.in("id", taxonomicScopeIds));
|
66
|
}
|
67
|
|
68
|
if(geoScopes != null && !geoScopes.isEmpty()) {
|
69
|
Set<Integer> geoScopeIds = new HashSet<Integer>();
|
70
|
for(NamedArea n : geoScopes) {
|
71
|
geoScopeIds.add(n.getId());
|
72
|
}
|
73
|
criteria.createCriteria("geographicalScope").add(Restrictions.in("id", geoScopeIds));
|
74
|
}
|
75
|
|
76
|
criteria.setProjection(Projections.countDistinct("id"));
|
77
|
|
78
|
return ((Number)criteria.uniqueResult()).intValue();
|
79
|
} else {
|
80
|
if((taxonomicScope == null || taxonomicScope.isEmpty()) && (geoScopes == null || geoScopes.isEmpty())) {
|
81
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(MediaKey.class,auditEvent.getRevisionNumber());
|
82
|
query.addProjection(AuditEntity.id().countDistinct());
|
83
|
return ((Long)query.getSingleResult()).intValue();
|
84
|
} else {
|
85
|
throw new OperationNotSupportedInPriorViewException("countMediaKeys(Set<Taxon> taxonomicScope, Set<NamedArea> geoScopes)");
|
86
|
}
|
87
|
}
|
88
|
}
|
89
|
|
90
|
@Override
|
91
|
public List<MediaKey> getMediaKeys(Set<Taxon> taxonomicScope, Set<NamedArea> geoScopes, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
92
|
AuditEvent auditEvent = getAuditEventFromContext();
|
93
|
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
|
94
|
Criteria inner = getSession().createCriteria(MediaKey.class);
|
95
|
|
96
|
if(taxonomicScope != null && !taxonomicScope.isEmpty()) {
|
97
|
Set<Integer> taxonomicScopeIds = new HashSet<Integer>();
|
98
|
for(Taxon n : taxonomicScope) {
|
99
|
taxonomicScopeIds.add(n.getId());
|
100
|
}
|
101
|
inner.createCriteria("taxonomicScope").add(Restrictions.in("id", taxonomicScopeIds));
|
102
|
}
|
103
|
|
104
|
if(geoScopes != null && !geoScopes.isEmpty()) {
|
105
|
Set<Integer> geoScopeIds = new HashSet<Integer>();
|
106
|
for(NamedArea n : geoScopes) {
|
107
|
geoScopeIds.add(n.getId());
|
108
|
}
|
109
|
inner.createCriteria("geographicalScope").add(Restrictions.in("id", geoScopeIds));
|
110
|
}
|
111
|
|
112
|
inner.setProjection(Projections.distinct(Projections.id()));
|
113
|
|
114
|
Criteria criteria = getSession().createCriteria(MediaKey.class);
|
115
|
criteria.add(Restrictions.in("id", inner.list()));
|
116
|
|
117
|
if(pageSize != null) {
|
118
|
criteria.setMaxResults(pageSize);
|
119
|
if(pageNumber != null) {
|
120
|
criteria.setFirstResult(pageNumber * pageSize);
|
121
|
}
|
122
|
}
|
123
|
|
124
|
List<MediaKey> results = criteria.list();
|
125
|
|
126
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
127
|
|
128
|
return results;
|
129
|
} else {
|
130
|
if((taxonomicScope == null || taxonomicScope.isEmpty()) && (geoScopes == null || geoScopes.isEmpty())) {
|
131
|
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(MediaKey.class,auditEvent.getRevisionNumber());
|
132
|
|
133
|
if(pageSize != null) {
|
134
|
query.setMaxResults(pageSize);
|
135
|
if(pageNumber != null) {
|
136
|
query.setFirstResult(pageNumber * pageSize);
|
137
|
} else {
|
138
|
query.setFirstResult(0);
|
139
|
}
|
140
|
}
|
141
|
List<MediaKey> results = query.getResultList();
|
142
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
143
|
return results;
|
144
|
} else {
|
145
|
throw new OperationNotSupportedInPriorViewException("getMediaKeys(Set<Taxon> taxonomicScope, Set<NamedArea> geoScopes, Integer pageSize, Integer pageNumber, List<String> propertyPaths)");
|
146
|
}
|
147
|
}
|
148
|
}
|
149
|
|
150
|
@Override
|
151
|
public List<Rights> getRights(Media media, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
152
|
checkNotInPriorView("MediaDaoHibernateImpl.getRights(Media t, Integer pageSize, Integer pageNumber, List<String> propertyPaths)");
|
153
|
Query query = getSession().createQuery("select rights from Media media join media.rights rights where media = :media");
|
154
|
query.setParameter("media",media);
|
155
|
setPagingParameter(query, pageSize, pageNumber);
|
156
|
List<Rights> results = query.list();
|
157
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
158
|
return results;
|
159
|
}
|
160
|
|
161
|
@Override
|
162
|
public int countRights(Media media) {
|
163
|
checkNotInPriorView("MediaDaoHibernateImpl.countRights(Media t)");
|
164
|
Query query = getSession().createQuery("select count(rights) from Media media join media.rights rights where media = :media");
|
165
|
query.setParameter("media",media);
|
166
|
return ((Long)query.uniqueResult()).intValue();
|
167
|
}
|
168
|
|
169
|
|
170
|
|
171
|
@Override
|
172
|
public void rebuildIndex() {
|
173
|
FullTextSession fullTextSession = Search.getFullTextSession(getSession());
|
174
|
|
175
|
for(Media media : list(null,null)) { // re-index all media
|
176
|
Hibernate.initialize(media.getTitle());
|
177
|
Hibernate.initialize(media.getAllDescriptions());
|
178
|
Hibernate.initialize(media.getArtist());
|
179
|
fullTextSession.index(media);
|
180
|
}
|
181
|
fullTextSession.flushToIndexes();
|
182
|
}
|
183
|
|
184
|
}
|