changes related to sorted distribution tree
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / DescriptionServiceImpl.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.cdm.api.service;
12
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.UUID;
19
20 import org.apache.log4j.Logger;
21 import org.springframework.beans.factory.annotation.Autowired;
22 import org.springframework.stereotype.Service;
23 import org.springframework.transaction.annotation.Propagation;
24 import org.springframework.transaction.annotation.Transactional;
25
26 import eu.etaxonomy.cdm.api.service.pager.Pager;
27 import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
28 import eu.etaxonomy.cdm.model.common.Annotation;
29 import eu.etaxonomy.cdm.model.common.MarkerType;
30 import eu.etaxonomy.cdm.model.common.TermVocabulary;
31 import eu.etaxonomy.cdm.model.description.DescriptionBase;
32 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
33 import eu.etaxonomy.cdm.model.description.Distribution;
34 import eu.etaxonomy.cdm.model.description.Feature;
35 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTermBase;
36 import eu.etaxonomy.cdm.model.description.Scope;
37 import eu.etaxonomy.cdm.model.description.TaxonDescription;
38 import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
39 import eu.etaxonomy.cdm.model.location.NamedArea;
40 import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
41 import eu.etaxonomy.cdm.model.media.Media;
42 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
43 import eu.etaxonomy.cdm.model.taxon.Taxon;
44 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
45 import eu.etaxonomy.cdm.persistence.dao.common.ITermVocabularyDao;
46 import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionDao;
47 import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionElementDao;
48 import eu.etaxonomy.cdm.persistence.dao.description.IFeatureDao;
49 import eu.etaxonomy.cdm.persistence.dao.description.IFeatureNodeDao;
50 import eu.etaxonomy.cdm.persistence.dao.description.IFeatureTreeDao;
51 import eu.etaxonomy.cdm.persistence.dao.description.IStatisticalMeasurementValueDao;
52 import eu.etaxonomy.cdm.persistence.query.OrderHint;
53
54 /**
55 * @author a.mueller
56 * @created 24.06.2008
57 * @version 1.0
58 */
59 @Service
60 @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
61 public class DescriptionServiceImpl extends IdentifiableServiceBase<DescriptionBase,IDescriptionDao> implements IDescriptionService {
62
63 private static final Logger logger = Logger.getLogger(DescriptionServiceImpl.class);
64
65 protected IDescriptionElementDao descriptionElementDao;
66 protected IFeatureTreeDao featureTreeDao;
67 protected IFeatureNodeDao featureNodeDao;
68 protected IFeatureDao featureDao;
69 protected ITermVocabularyDao vocabularyDao;
70 protected IStatisticalMeasurementValueDao statisticalMeasurementValueDao;
71
72 @Autowired
73 protected void setFeatureTreeDao(IFeatureTreeDao featureTreeDao) {
74 this.featureTreeDao = featureTreeDao;
75 }
76
77 @Autowired
78 protected void setFeatureNodeDao(IFeatureNodeDao featureNodeDao) {
79 this.featureNodeDao = featureNodeDao;
80 }
81
82 @Autowired
83 protected void setFeatureDao(IFeatureDao featureDao) {
84 this.featureDao = featureDao;
85 }
86
87 @Autowired
88 protected void setVocabularyDao(ITermVocabularyDao vocabularyDao) {
89 this.vocabularyDao = vocabularyDao;
90 }
91
92 @Autowired
93 protected void statisticalMeasurementValueDao(IStatisticalMeasurementValueDao statisticalMeasurementValueDao) {
94 this.statisticalMeasurementValueDao = statisticalMeasurementValueDao;
95 }
96
97 @Autowired
98 protected void setDescriptionElementDao(IDescriptionElementDao descriptionElementDao) {
99 this.descriptionElementDao = descriptionElementDao;
100 }
101
102 /**
103 *
104 */
105 public DescriptionServiceImpl() {
106 logger.debug("Load DescriptionService Bean");
107 }
108
109 /* (non-Javadoc)
110 * @see eu.etaxonomy.cdm.api.service.IIdentifiableEntityService#generateTitleCache()
111 */
112 public void generateTitleCache() {
113 logger.warn("generateTitleCache not yet implemented");
114 }
115
116 public TermVocabulary<Feature> getDefaultFeatureVocabulary(){
117 String uuidFeature = "b187d555-f06f-4d65-9e53-da7c93f8eaa8";
118 UUID featureUuid = UUID.fromString(uuidFeature);
119 return (TermVocabulary)vocabularyDao.findByUuid(featureUuid);
120 }
121
122 @Autowired
123 protected void setDao(IDescriptionDao dao) {
124 this.dao = dao;
125 }
126
127 public int count(Class<? extends DescriptionBase> type, Boolean hasImages, Boolean hasText,Set<Feature> feature) {
128 return dao.countDescriptions(type, hasImages, hasText, feature);
129 }
130
131 /**
132 * FIXME Candidate for harmonization
133 * rename -> getElements
134 */
135 public Pager<DescriptionElementBase> getDescriptionElements(DescriptionBase description,
136 Set<Feature> features, Class<? extends DescriptionElementBase> type, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
137
138 List<DescriptionElementBase> results = listDescriptionElements(description, features, type, pageSize, pageNumber, propertyPaths);
139 return new DefaultPagerImpl<DescriptionElementBase>(pageNumber, results.size(), pageSize, results);
140 }
141
142 public List<DescriptionElementBase> listDescriptionElements(DescriptionBase description,
143 Set<Feature> features, Class<? extends DescriptionElementBase> type, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
144 Integer numberOfResults = dao.countDescriptionElements(description, features, type);
145
146 List<DescriptionElementBase> results = new ArrayList<DescriptionElementBase>();
147 if(numberOfResults > 0) { // no point checking again
148 results = dao.getDescriptionElements(description, features, type, pageSize, pageNumber, propertyPaths);
149 }
150 return results;
151 }
152 public Pager<Annotation> getDescriptionElementAnnotations(DescriptionElementBase annotatedObj, MarkerType status, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths){
153 Integer numberOfResults = descriptionElementDao.countAnnotations(annotatedObj, status);
154
155 List<Annotation> results = new ArrayList<Annotation>();
156 if(numberOfResults > 0) { // no point checking again
157 results = descriptionElementDao.getAnnotations(annotatedObj, status, pageSize, pageNumber, orderHints, propertyPaths);
158 }
159
160 return new DefaultPagerImpl<Annotation>(pageNumber, numberOfResults, pageSize, results);
161 }
162
163
164
165 public Pager<Media> getMedia(DescriptionElementBase descriptionElement, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
166 Integer numberOfResults = descriptionElementDao.countMedia(descriptionElement);
167
168 List<Media> results = new ArrayList<Media>();
169 if(numberOfResults > 0) { // no point checking again
170 results = descriptionElementDao.getMedia(descriptionElement, pageSize, pageNumber, propertyPaths);
171 }
172
173 return new DefaultPagerImpl<Media>(pageNumber, numberOfResults, pageSize, results);
174 }
175
176 public Pager<TaxonDescription> getTaxonDescriptions(Taxon taxon, Set<Scope> scopes, Set<NamedArea> geographicalScope, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
177 Integer numberOfResults = dao.countTaxonDescriptions(taxon, scopes, geographicalScope);
178
179 List<TaxonDescription> results = new ArrayList<TaxonDescription>();
180 if(numberOfResults > 0) { // no point checking again
181 results = dao.getTaxonDescriptions(taxon, scopes, geographicalScope, pageSize, pageNumber, propertyPaths);
182 }
183
184 return new DefaultPagerImpl<TaxonDescription>(pageNumber, numberOfResults, pageSize, results);
185 }
186
187 public NamedAreaTree getOrderedDistributions(Set<TaxonDescription> taxonDescriptions, Set<NamedAreaLevel> omitLevels){
188 List<NamedArea> areaList = new ArrayList<NamedArea>();
189 NamedAreaTree tree = new NamedAreaTree();
190 //getting all the areas
191 for (TaxonDescription taxonDescription : taxonDescriptions) {
192 taxonDescription = (TaxonDescription) dao.load(taxonDescription.getUuid());
193 Set<DescriptionElementBase> elements = taxonDescription.getElements();
194 for (DescriptionElementBase element : elements) {
195 if(element.isInstanceOf(Distribution.class)){
196 Distribution distribution = (Distribution) element;
197 areaList.add(distribution.getArea());
198 }
199 }
200 }
201 //ordering the areas
202 tree.merge(areaList, omitLevels);
203 tree.sortChildren();
204 return tree;
205 }
206
207 public DistributionTree getOrderedDistributionsB(
208 Set<TaxonDescription> taxonDescriptions,
209 Set<NamedAreaLevel> omitLevels){
210
211 DistributionTree tree = new DistributionTree();
212 List<Distribution> distList = new ArrayList<Distribution>();
213
214 for (TaxonDescription taxonDescription : taxonDescriptions) {
215 taxonDescription = (TaxonDescription) dao.load(taxonDescription.getUuid());
216 Set<DescriptionElementBase> elements = taxonDescription.getElements();
217 for (DescriptionElementBase element : elements) {
218 if(element.isInstanceOf(Distribution.class)){
219 Distribution distribution = (Distribution) element;
220 distList.add(distribution);
221 }
222 }
223 }
224
225 //ordering the areas
226 tree.merge(distList, omitLevels);
227 tree.sortChildren();
228 return tree;
229 }
230
231 public Pager<TaxonNameDescription> getTaxonNameDescriptions(TaxonNameBase name, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
232 Integer numberOfResults = dao.countTaxonNameDescriptions(name);
233
234 List<TaxonNameDescription> results = new ArrayList<TaxonNameDescription>();
235 if(numberOfResults > 0) { // no point checking again
236 results = dao.getTaxonNameDescriptions(name, pageSize, pageNumber,propertyPaths);
237 }
238
239 return new DefaultPagerImpl<TaxonNameDescription>(pageNumber, numberOfResults, pageSize, results);
240 }
241
242
243 public Pager<DescriptionBase> page(Class<? extends DescriptionBase> type, Boolean hasImages, Boolean hasText, Set<Feature> feature, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
244 Integer numberOfResults = dao.countDescriptions(type, hasImages, hasText, feature);
245
246 List<DescriptionBase> results = new ArrayList<DescriptionBase>();
247 if(numberOfResults > 0) { // no point checking again
248 results = dao.listDescriptions(type, hasImages, hasText, feature, pageSize, pageNumber,orderHints,propertyPaths);
249 }
250
251 return new DefaultPagerImpl<DescriptionBase>(pageNumber, numberOfResults, pageSize, results);
252 }
253
254 /**
255 * FIXME Candidate for harmonization
256 * Rename: searchByDistribution
257 */
258 public Pager<TaxonDescription> searchDescriptionByDistribution(Set<NamedArea> namedAreas, PresenceAbsenceTermBase presence, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
259 Integer numberOfResults = dao.countDescriptionByDistribution(namedAreas, presence);
260
261 List<TaxonDescription> results = new ArrayList<TaxonDescription>();
262 if(numberOfResults > 0) { // no point checking again
263 results = dao.searchDescriptionByDistribution(namedAreas, presence, pageSize, pageNumber,orderHints,propertyPaths);
264 }
265
266 return new DefaultPagerImpl<TaxonDescription>(pageNumber, numberOfResults, pageSize, results);
267 }
268
269 /**
270 * FIXME Candidate for harmonization
271 * move: descriptionElementService.search
272 */
273 public Pager<DescriptionElementBase> searchElements(Class<? extends DescriptionElementBase> clazz, String queryString, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
274 Integer numberOfResults = descriptionElementDao.count(clazz,queryString);
275
276 List<DescriptionElementBase> results = new ArrayList<DescriptionElementBase>();
277 if(numberOfResults > 0) { // no point checking again
278 results = descriptionElementDao.search(clazz, queryString, pageSize, pageNumber, orderHints, propertyPaths);
279 }
280
281 return new DefaultPagerImpl<DescriptionElementBase>(pageNumber, numberOfResults, pageSize, results);
282 }
283
284 /**
285 * FIXME Candidate for harmonization
286 * descriptionElementService.find
287 */
288 public DescriptionElementBase getDescriptionElementByUuid(UUID uuid) {
289 return descriptionElementDao.findByUuid(uuid);
290 }
291
292 /**
293 * FIXME Candidate for harmonization
294 * descriptionElementService.load
295 */
296 public DescriptionElementBase loadDescriptionElement(UUID uuid, List<String> propertyPaths) {
297 return descriptionElementDao.load(uuid, propertyPaths);
298 }
299
300 /**
301 * FIXME Candidate for harmonization
302 * descriptionElementService.save
303 */
304 @Transactional(readOnly = false)
305 public UUID saveDescriptionElement(DescriptionElementBase descriptionElement) {
306 return descriptionElementDao.save(descriptionElement);
307 }
308
309 /**
310 * FIXME Candidate for harmonization
311 * descriptionElementService.save
312 */
313 @Transactional(readOnly = false)
314 public Map<UUID, DescriptionElementBase> saveDescriptionElement(Collection<DescriptionElementBase> descriptionElements) {
315 return descriptionElementDao.saveAll(descriptionElements);
316 }
317
318 /**
319 * FIXME Candidate for harmonization
320 * descriptionElementService.delete
321 */
322 public UUID deleteDescriptionElement(DescriptionElementBase descriptionElement) {
323 return descriptionElementDao.delete(descriptionElement);
324 }
325
326 public TermVocabulary<Feature> getFeatureVocabulary(UUID uuid) {
327 return (TermVocabulary)vocabularyDao.findByUuid(uuid);
328 }
329
330 public List<DescriptionElementBase> getDescriptionElementsForTaxon(
331 Taxon taxon, Set<Feature> features,
332 Class<? extends DescriptionElementBase> type, Integer pageSize,
333 Integer pageNumber, List<String> propertyPaths) {
334 return dao.getDescriptionElementForTaxon(taxon, features, type, pageSize, pageNumber, propertyPaths);
335 }
336 }