Project

General

Profile

Download (8.91 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
 * @since 24.09.2008
28
 */
29
public abstract class AnnotatableDaoImpl<T extends AnnotatableEntity>
30
        extends VersionableDaoBase<T>
31
        implements IAnnotatableDao<T> {
32

    
33
    @SuppressWarnings("unused")
34
	private static Logger logger = Logger.getLogger(AnnotatableDaoImpl.class);
35

    
36
	/**
37
	 * @param type
38
	 */
39
	public AnnotatableDaoImpl(Class<T> type) {
40
		super(type);
41
	}
42

    
43
	@Override
44
    public int countAnnotations(T annotatableEntity, MarkerType status) {
45
		checkNotInPriorView("AnnotatableDaoImpl.countAnnotations(T annotatableEntity, MarkerType status)");
46
		Query query = null;
47

    
48
		String className = annotatableEntity.getClass().getName();
49
        if(status == null) {
50
           //AND annoEnt.class = :class" does not work for some reason
51
        	query = getSession().createQuery("SELECT COUNT(annotation) FROM " + className + " annoEnt JOIN annoEnt.annotations annotation WHERE annoEnt.id = :id" );
52
        } else {
53
        	query = getSession().createQuery("SELECT COUNT(annotation) FROM " + className + " annoEnt JOIN annoEnt.annotations annotation JOIN annotation.markers marker "
54
        	        + " WHERE annoEnt.id = :id AND marker.markerType = :status");
55
        	query.setParameter("status", status);
56
        }
57

    
58
        query.setParameter("id", annotatableEntity.getId());
59

    
60

    
61
		return ((Long)query.uniqueResult()).intValue();
62
	}
63

    
64
	@Override
65
    public List<Annotation> getAnnotations(T annotatableEntity,	MarkerType status, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
66
		checkNotInPriorView("AnnotatableDaoImpl.getAnnotations(T annotatableEntity, MarkerType status, Integer pageSize, Integer pageNumber)");
67
        Query query = null;
68

    
69
        StringBuffer orderString = new StringBuffer();
70

    
71
        if(orderHints != null && !orderHints.isEmpty()) {
72
		    orderString.append(" ORDER BY");
73
		    for(OrderHint orderHint : orderHints) {
74
		    	orderString.append(" annotation." + orderHint.getPropertyName() + " ");
75

    
76
		    	if(orderHint.getSortOrder() == SortOrder.ASCENDING) {
77
		    		orderString.append("ASC");
78
		    	} else {
79
		    		orderString.append("DESC");
80
		    	}
81
		    }
82
		}
83

    
84
        String className = annotatableEntity.getClass().getName();
85
        if(status == null) {
86
            //AND annoEnt.class = :class  does not work for some reason
87
        	query = getSession().createQuery("SELECT annotation FROM " + className + " annoEnt JOIN annoEnt.annotations annotation WHERE annoEnt.id = :id " + orderString.toString());
88
        } else {
89
        	query = getSession().createQuery("SELECT annotation FROM " + className + " annoEnt JOIN annoEnt.annotations annotation JOIN annotation.markers marker " +
90
        	        " WHERE annoEnt.id = :id AND marker.markerType = :status" + orderString.toString());
91
        	query.setParameter("status",status);
92
        }
93

    
94
        query.setParameter("id",annotatableEntity.getId());
95

    
96

    
97
		if(pageSize != null) {
98
			query.setMaxResults(pageSize);
99
		    if(pageNumber != null) {
100
		    	query.setFirstResult(pageNumber * pageSize);
101
		    }
102
		}
103

    
104
		@SuppressWarnings("unchecked")
105
        List<Annotation> results = query.list();
106
		defaultBeanInitializer.initializeAll(results, propertyPaths);
107
		return results;
108
	}
109

    
110
	@Override
111
    public int countMarkers(T annotatableEntity, Boolean technical) {
112
		checkNotInPriorView("AnnotatableDaoImpl.countMarkers(T annotatableEntity, Boolean technical");
113
        Query query = null;
114

    
115
        String className = annotatableEntity.getClass().getName();
116
		if(technical == null) {
117
			query = getSession().createQuery("SELECT COUNT(marker) FROM " + className + " annoEnt JOIN annoEnt.markers marker  WHERE annoEnt.id = :id ");
118
		} else {
119
			query = getSession().createQuery("SELECT COUNT(marker) FROM " + className + " annoEnt JOIN annoEnt.markers marker JOIN marker.markerType type "
120
			        + " WHERE annoEnt.id = :id AND type.isTechnical = :technical");
121
			query.setParameter("technical", technical);
122
		}
123

    
124
		query.setParameter("id",annotatableEntity.getId());
125

    
126
		return ((Long)query.uniqueResult()).intValue();
127
	}
128

    
129
    @Override
130
    public List<Marker> getMarkers(T annotatableEntity, Boolean technical, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
131
    	checkNotInPriorView("AnnotatableDaoImpl.getMarkers(T annotatableEntity, Boolean technical, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)");
132
        Query query = null;
133

    
134
        StringBuffer orderString = new StringBuffer();
135

    
136
        if(orderHints != null && !orderHints.isEmpty()) {
137
		    orderString.append(" ORDER BY");
138
		    for(OrderHint orderHint : orderHints) {
139
		    	orderString.append(" marker." + orderHint.getPropertyName() + " ");
140

    
141
		    	if(orderHint.getSortOrder() == SortOrder.ASCENDING) {
142
		    		orderString.append("ASC");
143
		    	} else {
144
		    		orderString.append("DESC");
145
		    	}
146
		    }
147
		}
148

    
149
        String className = annotatableEntity.getClass().getName();
150
		if(technical == null) {
151
			query = getSession().createQuery("SELECT marker FROM " + className + " annoEnt JOIN annoEnt.markers marker WHERE annoEnt.id = :id" + orderString.toString());
152
		} else {
153
			query = getSession().createQuery("SELECT marker FROM " + className + " annoEnt JOIN annoEnt.markers marker JOIN marker.markerType type "
154
			        + " WHERE annoEnt.id = :id AND type.isTechnical = :technical" + orderString.toString());
155
			query.setParameter("technical",technical);
156
		}
157

    
158
		query.setParameter("id",annotatableEntity.getId());
159

    
160
		if(pageSize != null) {
161
			query.setMaxResults(pageSize);
162
		    if(pageNumber != null) {
163
		    	query.setFirstResult(pageNumber * pageSize);
164
		    }
165
		}
166

    
167
		List<Marker> results = query.list();
168
		defaultBeanInitializer.initializeAll(results, propertyPaths);
169
		return results;
170
    }
171

    
172
    @Override
173
    public int countMarkers(Class<? extends T> clazz, Boolean technical) {
174
		checkNotInPriorView("AnnotatableDaoImpl.countMarkers(Class<? extends T> clazz, Boolean technical, Integer pageSize, Integer pageNumber, List<String> propertyPaths)");
175
		Query query = null;
176
		String className = clazz == null ? type.getName() : clazz.getName();
177
		if(technical == null) {
178
			query = getSession().createQuery("SELECT count(marker) FROM " + className + " annoEnt JOIN annoEnt.markers marker JOIN marker.markerType type");
179
		} else {
180
			query = getSession().createQuery("SELECT count(marker) FROM " + className + " annoEnt JOIN annoEnt.markers marker JOIN marker.markerType type "
181
			        + " WHERE type.technical = :technical");
182
			query.setParameter("technical", technical);
183
		}
184

    
185
//		if(clazz == null) {
186
//		  query.setParameter("class", type.getName());
187
//		} else {
188
//	      query.setParameter("class", clazz.getName());
189
//		}
190

    
191
		return ((Long)query.uniqueResult()).intValue();
192
	}
193

    
194
	@Override
195
    public List<Object[]> groupMarkers(Class<? extends T> clazz, Boolean technical, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
196
		checkNotInPriorView("AnnotatableDaoImpl.groupMarkers(Class<? extends T> clazz, Boolean technical, Integer pageSize, Integer pageNumber, List<String> propertyPaths)");
197
		Query query = null;
198
		String className = clazz == null ? type.getName() : clazz.getName();
199
        if(technical == null) {
200
			query = getSession().createQuery("SELECT type, count(marker) FROM " + className + " annoEnt JOIN annoEnt.markers marker JOIN marker.markerType type GROUP BY type ORDER BY type.titleCache ASC");
201
		} else {
202
			query = getSession().createQuery("SELECT type, count(marker) FROM " + className + " annoEnt JOIN annoEnt.markers marker JOIN marker.markerType type "
203
			        + " WHERE type.technical = :technical GROUP BY type ORDER BY type.titleCache ASC");
204
			query.setParameter("technical", technical);
205
		}
206

    
207
//		if(clazz == null) {
208
//			  query.setParameter("class", type.getName());
209
//		} else {
210
//		      query.setParameter("class", clazz.getName());
211
//		}
212

    
213
		if(pageSize != null) {
214
			query.setMaxResults(pageSize);
215
		    if(pageNumber != null) {
216
		    	query.setFirstResult(pageNumber * pageSize);
217
		    }
218
		}
219

    
220
		List<Object[]> result = query.list();
221

    
222
		if(propertyPaths != null && !propertyPaths.isEmpty()) {
223
		  for(Object[] objects : result) {
224
			defaultBeanInitializer.initialize(objects[0], propertyPaths);
225
		  }
226
		}
227

    
228
		return result;
229
	}
230

    
231

    
232

    
233
}
(1-1/24)