Project

General

Profile

Download (8.54 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
4
* http://www.e-taxonomy.eu
5
* 
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.cdm.persistence.dao.hibernate.common;
11

    
12
import java.util.List;
13

    
14
import org.apache.log4j.Logger;
15
import org.hibernate.Query;
16

    
17
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
18
import eu.etaxonomy.cdm.model.common.Annotation;
19
import eu.etaxonomy.cdm.model.common.Marker;
20
import eu.etaxonomy.cdm.model.common.MarkerType;
21
import eu.etaxonomy.cdm.persistence.dao.common.IAnnotatableDao;
22
import eu.etaxonomy.cdm.persistence.query.OrderHint;
23
import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
24

    
25
/**
26
 * @author n.hoffmann
27
 * @created 24.09.2008
28
 * @version 1.0
29
 */
30
public abstract class AnnotatableDaoImpl<T extends AnnotatableEntity> extends VersionableDaoBase<T> implements IAnnotatableDao<T> {
31
	@SuppressWarnings("unused")
32
	private static Logger logger = Logger.getLogger(AnnotatableDaoImpl.class);
33
	
34
	/**
35
	 * @param type
36
	 */
37
	public AnnotatableDaoImpl(Class<T> type) {
38
		super(type);
39
	}
40
	
41
	public int countAnnotations(T annotatableEntity, MarkerType status) {
42
		checkNotInPriorView("AnnotatableDaoImpl.countAnnotations(T annotatableEntity, MarkerType status)");
43
		Query query = null;
44
		
45
		if(status == null) {
46
			query = getSession().createQuery("select count(annotation) from Annotation annotation where annotation.annotatedObj.id = :id and annotation.annotatedObj.class = :class");
47
		} else {
48
			query = getSession().createQuery("select count(annotation) from Annotation annotation join annotation.markers marker where annotation.annotatedObj.id = :id and annotation.annotatedObj.class = :class and marker.markerType = :status");
49
			query.setParameter("status",status);
50
		}
51
		
52
		query.setParameter("id",annotatableEntity.getId());
53
		query.setParameter("class", annotatableEntity.getClass().getName());
54
		
55
		return ((Long)query.uniqueResult()).intValue();
56
	}
57
	
58
	public List<Annotation> getAnnotations(T annotatableEntity,	MarkerType status, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
59
		checkNotInPriorView("AnnotatableDaoImpl.getAnnotations(T annotatableEntity, MarkerType status, Integer pageSize, Integer pageNumber)");
60
        Query query = null;
61
        
62
        StringBuffer orderString = new StringBuffer();
63
        
64
        if(orderHints != null && !orderHints.isEmpty()) {
65
		    orderString.append(" order by");
66
		    for(OrderHint orderHint : orderHints) {
67
		    	orderString.append(" annotation." + orderHint.getPropertyName() + " ");
68
		    	
69
		    	if(orderHint.getSortOrder() == SortOrder.ASCENDING) {
70
		    		orderString.append("asc");
71
		    	} else {
72
		    		orderString.append("desc");
73
		    	}
74
		    }
75
		}
76
        
77
		
78
		if(status == null) {
79
			query = getSession().createQuery("select annotation from Annotation annotation where annotation.annotatedObj.id = :id and annotation.annotatedObj.class = :class" + orderString.toString());
80
		} else {
81
			query = getSession().createQuery("select annotation from Annotation annotation join annotation.markers marker where annotation.annotatedObj.id = :id and annotation.annotatedObj.class = :class and marker.markerType = :status" + orderString.toString());
82
			query.setParameter("status",status);
83
		}
84
		
85
		query.setParameter("id",annotatableEntity.getId());
86
		query.setParameter("class", annotatableEntity.getClass().getName());
87
		
88
		if(pageSize != null) {
89
			query.setMaxResults(pageSize);
90
		    if(pageNumber != null) {
91
		    	query.setFirstResult(pageNumber * pageSize);
92
		    }
93
		}
94
		
95
		List<Annotation> results = (List<Annotation>)query.list();
96
		defaultBeanInitializer.initializeAll(results, propertyPaths);
97
		return results;
98
	}
99
	
100
	public int countMarkers(T annotatableEntity, Boolean technical) {
101
		checkNotInPriorView("AnnotatableDaoImpl.countMarkers(T annotatableEntity, Boolean technical");
102
        Query query = null;
103
		
104
		if(technical == null) {
105
			query = getSession().createQuery("select count(marker) from Marker marker where marker.markedObj.id = :id and marker.markedObj.class = :class");
106
		} else {
107
			query = getSession().createQuery("select count(marker) from Marker marker join marker.markerType type where marker.markedObj.id = :id and marker.markedObj.class = :class and type.isTechnical = :technical");
108
			query.setParameter("technical",technical);
109
		}
110
		
111
		query.setParameter("id",annotatableEntity.getId());
112
		query.setParameter("class", annotatableEntity.getClass().getName());
113
		
114
		return ((Long)query.uniqueResult()).intValue();
115
	}
116
	
117
    public List<Marker> getMarkers(T annotatableEntity, Boolean technical, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
118
    	checkNotInPriorView("AnnotatableDaoImpl.getMarkers(T annotatableEntity, Boolean technical, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)");
119
        Query query = null;
120
        
121
        StringBuffer orderString = new StringBuffer();
122
        
123
        if(orderHints != null && !orderHints.isEmpty()) {
124
		    orderString.append(" order by");
125
		    for(OrderHint orderHint : orderHints) {
126
		    	orderString.append(" marker." + orderHint.getPropertyName() + " ");
127
		    	
128
		    	if(orderHint.getSortOrder() == SortOrder.ASCENDING) {
129
		    		orderString.append("asc");
130
		    	} else {
131
		    		orderString.append("desc");
132
		    	}
133
		    }
134
		}
135
        
136
		
137
		if(technical == null) {
138
			query = getSession().createQuery("select marker from Marker marker where marker.markedObj.id = :id and marker.markedObj.class = :class" + orderString.toString());
139
		} else {
140
			query = getSession().createQuery("select marker from Marker marker join marker.markerType type where marker.markedObj.id = :id and marker.markedObj.class = :class and type.isTechnical = :technical" + orderString.toString());
141
			query.setParameter("technical",technical);
142
		}
143
		
144
		query.setParameter("id",annotatableEntity.getId());
145
		query.setParameter("class", annotatableEntity.getClass().getName());
146
		
147
		if(pageSize != null) {
148
			query.setMaxResults(pageSize);
149
		    if(pageNumber != null) {
150
		    	query.setFirstResult(pageNumber * pageSize);
151
		    }
152
		}
153
		
154
		List<Marker> results = (List<Marker>)query.list();
155
		defaultBeanInitializer.initializeAll(results, propertyPaths);
156
		return results;
157
    }
158
	
159
    public int countMarkers(Class<? extends T> clazz, Boolean technical) {
160
		checkNotInPriorView("AnnotatableDaoImpl.countMarkers(Class<? extends T> clazz, Boolean technical, Integer pageSize, Integer pageNumber, List<String> propertyPaths)");
161
		Query query = null;
162
		if(technical == null) {
163
			query = getSession().createQuery("select count(marker) from Marker marker join marker.markerType type where marker.markedObj.class = :class");
164
		} else {
165
			query = getSession().createQuery("select count(marker) from Marker marker join marker.markerType type where marker.markedObj.class = :class and type.technical = :technical");
166
			query.setParameter("technical",technical);
167
		}
168
		
169
		if(clazz == null) {
170
		  query.setParameter("class", type.getName());
171
		} else {
172
	      query.setParameter("class", clazz.getName());
173
		}
174
		
175
		return ((Long)query.uniqueResult()).intValue();
176
	}
177
	
178
	public List<Object[]> groupMarkers(Class<? extends T> clazz, Boolean technical, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
179
		checkNotInPriorView("AnnotatableDaoImpl.groupMarkers(Class<? extends T> clazz, Boolean technical, Integer pageSize, Integer pageNumber, List<String> propertyPaths)");
180
		Query query = null;
181
		if(technical == null) {
182
			query = getSession().createQuery("select type, count(marker) from Marker marker join marker.markerType type where marker.markedObj.class = :class group by type order by type.titleCache asc");
183
		} else {
184
			query = getSession().createQuery("select type, count(marker) from Marker marker join marker.markerType type where marker.markedObj.class = :class and type.technical = :technical group by type order by type.titleCache asc");
185
			query.setParameter("technical",technical);
186
		}
187
		
188
		if(clazz == null) {
189
			  query.setParameter("class", type.getName());
190
		} else {
191
		      query.setParameter("class", clazz.getName());
192
		}
193
		
194
		if(pageSize != null) {
195
			query.setMaxResults(pageSize);
196
		    if(pageNumber != null) {
197
		    	query.setFirstResult(pageNumber * pageSize);
198
		    }
199
		}
200
		
201
		List<Object[]> result = (List<Object[]>)query.list();
202
		
203
		if(propertyPaths != null && !propertyPaths.isEmpty()) {
204
		  for(Object[] objects : result) {
205
			defaultBeanInitializer.initialize(objects[0], propertyPaths);
206
		  }
207
		}
208
		
209
		return result;
210
	}
211
	
212
}
(1-1/23)