Project

General

Profile

Download (8.87 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
package eu.etaxonomy.cdm.persistence.dao.hibernate.common;
10

    
11
import java.util.List;
12

    
13
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
14
import org.hibernate.query.Query;
15

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

    
24
/**
25
 * @author n.hoffmann
26
 * @since 24.09.2008
27
 */
28
public abstract class AnnotatableDaoBaseImpl<T extends AnnotatableEntity>
29
        extends VersionableDaoBase<T>
30
        implements IAnnotatableDao<T> {
31

    
32
    @SuppressWarnings("unused")
33
	private static Logger logger = LogManager.getLogger(AnnotatableDaoBaseImpl.class);
34

    
35
	public AnnotatableDaoBaseImpl(Class<T> type) {
36
		super(type);
37
	}
38

    
39
	@Override
40
    public long countAnnotations(T annotatableEntity, MarkerType status) {
41
		checkNotInPriorView("AnnotatableDaoBaseImpl.countAnnotations(T annotatableEntity, MarkerType status)");
42
		Query<Long> query = null;
43

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

    
55
        query.setParameter("id", annotatableEntity.getId());
56
		return query.uniqueResult();
57
	}
58

    
59
	@Override
60
    public List<Annotation> getAnnotations(T annotatableEntity,	MarkerType status, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
61
		checkNotInPriorView("AnnotatableDaoBaseImpl.getAnnotations(T annotatableEntity, MarkerType status, Integer pageSize, Integer pageNumber)");
62

    
63
		Query<Annotation> query = null;
64
        StringBuffer orderString = new StringBuffer();
65

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

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

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

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

    
92
        addPageSizeAndNumber(query, pageSize, pageNumber);
93

    
94
        List<Annotation> results = query.list();
95
		defaultBeanInitializer.initializeAll(results, propertyPaths);
96
		return results;
97
	}
98

    
99
	@Override
100
    public long countMarkers(T annotatableEntity, Boolean technical) {
101
		checkNotInPriorView("AnnotatableDaoBaseImpl.countMarkers(T annotatableEntity, Boolean technical");
102
        Query<Long> query = null;
103

    
104
        String className = annotatableEntity.getClass().getName();
105
		if(technical == null) {
106
			query = getSession().createQuery("SELECT COUNT(marker) FROM " + className + " annoEnt JOIN annoEnt.markers marker  WHERE annoEnt.id = :id ", Long.class);
107
		} else {
108
			query = getSession().createQuery("SELECT COUNT(marker) FROM " + className + " annoEnt JOIN annoEnt.markers marker JOIN marker.markerType type "
109
			        + " WHERE annoEnt.id = :id AND type.isTechnical = :technical",
110
			        Long.class);
111
			query.setParameter("technical", technical);
112
		}
113

    
114
		query.setParameter("id",annotatableEntity.getId());
115
		return query.uniqueResult();
116
	}
117

    
118
    @Override
119
    public List<Marker> getMarkers(T annotatableEntity, Boolean technical, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
120
    	checkNotInPriorView("AnnotatableDaoBaseImpl.getMarkers(T annotatableEntity, Boolean technical, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)");
121

    
122
    	Query<Marker> query = null;
123
        StringBuffer orderString = new StringBuffer();
124

    
125
        if(orderHints != null && !orderHints.isEmpty()) {
126
		    orderString.append(" ORDER BY");
127
		    for(OrderHint orderHint : orderHints) {
128
		    	orderString.append(" marker." + orderHint.getPropertyName() + " ");
129

    
130
		    	if(orderHint.getSortOrder() == SortOrder.ASCENDING) {
131
		    		orderString.append("ASC");
132
		    	} else {
133
		    		orderString.append("DESC");
134
		    	}
135
		    }
136
		}
137

    
138
        String className = annotatableEntity.getClass().getName();
139
		if(technical == null) {
140
			query = getSession().createQuery("SELECT marker FROM " + className + " annoEnt JOIN annoEnt.markers marker WHERE annoEnt.id = :id" + orderString.toString(), Marker.class);
141
		} else {
142
			query = getSession().createQuery("SELECT marker FROM " + className + " annoEnt JOIN annoEnt.markers marker JOIN marker.markerType type "
143
			        + " WHERE annoEnt.id = :id AND type.isTechnical = :technical" + orderString.toString(),
144
			        Marker.class);
145
			query.setParameter("technical",technical);
146
		}
147

    
148
		query.setParameter("id",annotatableEntity.getId());
149

    
150
		addPageSizeAndNumber(query, pageSize, pageNumber);
151
		List<Marker> results = query.list();
152
		defaultBeanInitializer.initializeAll(results, propertyPaths);
153
		return results;
154
    }
155

    
156
    @Override
157
    public int countMarkers(Class<? extends T> clazz, Boolean technical) {
158
		checkNotInPriorView("AnnotatableDaoBaseImpl.countMarkers(Class<? extends T> clazz, Boolean technical, Integer pageSize, Integer pageNumber, List<String> propertyPaths)");
159
		Query<Long> query = null;
160
		String className = clazz == null ? type.getName() : clazz.getName();
161
		if(technical == null) {
162
			query = getSession().createQuery("SELECT count(marker) FROM " + className + " annoEnt JOIN annoEnt.markers marker JOIN marker.markerType type", Long.class);
163
		} else {
164
			query = getSession().createQuery("SELECT count(marker) FROM " + className + " annoEnt JOIN annoEnt.markers marker JOIN marker.markerType type "
165
			        + " WHERE type.technical = :technical", Long.class);
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 query.uniqueResult().intValue();
176
	}
177

    
178
	@Override
179
    public List<Object[]> groupMarkers(Class<? extends T> clazz, Boolean technical, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
180
		checkNotInPriorView("AnnotatableDaoBaseImpl.groupMarkers(Class<? extends T> clazz, Boolean technical, Integer pageSize, Integer pageNumber, List<String> propertyPaths)");
181
		Query<Object[]> query = null;
182
		String className = clazz == null ? type.getName() : clazz.getName();
183
        if(technical == null) {
184
			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", Object[].class);
185
		} else {
186
			query = getSession().createQuery("SELECT type, count(marker) FROM " + className + " annoEnt JOIN annoEnt.markers marker JOIN marker.markerType type "
187
			        + " WHERE type.technical = :technical GROUP BY type ORDER BY type.titleCache ASC",
188
			        Object[].class);
189
			query.setParameter("technical", technical);
190
		}
191

    
192
//		if(clazz == null) {
193
//			  query.setParameter("class", type.getName());
194
//		} else {
195
//		      query.setParameter("class", clazz.getName());
196
//		}
197

    
198
        addPageSizeAndNumber(query, pageSize, pageNumber);
199
		List<Object[]> result = query.list();
200
		if(propertyPaths != null && !propertyPaths.isEmpty()) {
201
		  for(Object[] objects : result) {
202
			defaultBeanInitializer.initialize(objects[0], propertyPaths);
203
		  }
204
		}
205

    
206
		return result;
207
	}
208
}
(1-1/18)