Project

General

Profile

Download (8.85 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
 */
29
public abstract class AnnotatableDaoImpl<T extends AnnotatableEntity> extends VersionableDaoBase<T> implements IAnnotatableDao<T> {
30
	@SuppressWarnings("unused")
31
	private static Logger logger = Logger.getLogger(AnnotatableDaoImpl.class);
32

    
33
	/**
34
	 * @param type
35
	 */
36
	public AnnotatableDaoImpl(Class<T> type) {
37
		super(type);
38
	}
39

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

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

    
55
        query.setParameter("id", annotatableEntity.getId());
56

    
57

    
58
		return ((Long)query.uniqueResult()).intValue();
59
	}
60

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

    
66
        StringBuffer orderString = new StringBuffer();
67

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

    
73
		    	if(orderHint.getSortOrder() == SortOrder.ASCENDING) {
74
		    		orderString.append("ASC");
75
		    	} else {
76
		    		orderString.append("DESC");
77
		    	}
78
		    }
79
		}
80

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

    
91
        query.setParameter("id",annotatableEntity.getId());
92

    
93

    
94
		if(pageSize != null) {
95
			query.setMaxResults(pageSize);
96
		    if(pageNumber != null) {
97
		    	query.setFirstResult(pageNumber * pageSize);
98
		    }
99
		}
100

    
101
		List<Annotation> results = query.list();
102
		defaultBeanInitializer.initializeAll(results, propertyPaths);
103
		return results;
104
	}
105

    
106
	@Override
107
    public int countMarkers(T annotatableEntity, Boolean technical) {
108
		checkNotInPriorView("AnnotatableDaoImpl.countMarkers(T annotatableEntity, Boolean technical");
109
        Query query = null;
110

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

    
120
		query.setParameter("id",annotatableEntity.getId());
121

    
122
		return ((Long)query.uniqueResult()).intValue();
123
	}
124

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

    
130
        StringBuffer orderString = new StringBuffer();
131

    
132
        if(orderHints != null && !orderHints.isEmpty()) {
133
		    orderString.append(" ORDER BY");
134
		    for(OrderHint orderHint : orderHints) {
135
		    	orderString.append(" marker." + orderHint.getPropertyName() + " ");
136

    
137
		    	if(orderHint.getSortOrder() == SortOrder.ASCENDING) {
138
		    		orderString.append("ASC");
139
		    	} else {
140
		    		orderString.append("DESC");
141
		    	}
142
		    }
143
		}
144

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

    
154
		query.setParameter("id",annotatableEntity.getId());
155

    
156
		if(pageSize != null) {
157
			query.setMaxResults(pageSize);
158
		    if(pageNumber != null) {
159
		    	query.setFirstResult(pageNumber * pageSize);
160
		    }
161
		}
162

    
163
		List<Marker> results = query.list();
164
		defaultBeanInitializer.initializeAll(results, propertyPaths);
165
		return results;
166
    }
167

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

    
181
//		if(clazz == null) {
182
//		  query.setParameter("class", type.getName());
183
//		} else {
184
//	      query.setParameter("class", clazz.getName());
185
//		}
186

    
187
		return ((Long)query.uniqueResult()).intValue();
188
	}
189

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

    
203
//		if(clazz == null) {
204
//			  query.setParameter("class", type.getName());
205
//		} else {
206
//		      query.setParameter("class", clazz.getName());
207
//		}
208

    
209
		if(pageSize != null) {
210
			query.setMaxResults(pageSize);
211
		    if(pageNumber != null) {
212
		    	query.setFirstResult(pageNumber * pageSize);
213
		    }
214
		}
215

    
216
		List<Object[]> result = query.list();
217

    
218
		if(propertyPaths != null && !propertyPaths.isEmpty()) {
219
		  for(Object[] objects : result) {
220
			defaultBeanInitializer.initialize(objects[0], propertyPaths);
221
		  }
222
		}
223

    
224
		return result;
225
	}
226

    
227
}
(1-1/23)