ref 3984: add service method to get uuidAndTitleCaches for a list of uuids
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / ReferenceServiceImpl.java
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.api.service;
11
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Set;
15 import java.util.UUID;
16
17 import org.apache.log4j.Logger;
18 import org.springframework.beans.factory.annotation.Autowired;
19 import org.springframework.stereotype.Service;
20 import org.springframework.transaction.annotation.Transactional;
21
22 import eu.etaxonomy.cdm.api.service.dto.IdentifiedEntityDTO;
23 import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
24 import eu.etaxonomy.cdm.model.common.DefinedTerm;
25 import eu.etaxonomy.cdm.model.reference.Reference;
26 import eu.etaxonomy.cdm.model.reference.ReferenceType;
27 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
28 import eu.etaxonomy.cdm.persistence.dao.common.ICdmGenericDao;
29 import eu.etaxonomy.cdm.persistence.dao.reference.IReferenceDao;
30 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
31 import eu.etaxonomy.cdm.persistence.query.MatchMode;
32 import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
33
34
35 @Service
36 @Transactional(readOnly = true)
37 public class ReferenceServiceImpl extends IdentifiableServiceBase<Reference,IReferenceDao> implements IReferenceService {
38
39 static Logger logger = Logger.getLogger(ReferenceServiceImpl.class);
40
41 @Autowired
42 private ICdmGenericDao genericDao;
43 /**
44 * Constructor
45 */
46 public ReferenceServiceImpl(){
47 if (logger.isDebugEnabled()) { logger.debug("Load ReferenceService Bean"); }
48 }
49
50 @Override
51 @Transactional(readOnly = false)
52 public void updateTitleCache(Class<? extends Reference> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<Reference> cacheStrategy, IProgressMonitor monitor) {
53 if (clazz == null){
54 clazz = Reference.class;
55 }
56 super.updateTitleCacheImpl(clazz, stepSize, cacheStrategy, monitor);
57 }
58
59
60 @Override
61 protected void setOtherCachesNull(Reference ref) {
62 if (! ref.isProtectedAbbrevTitleCache()){
63 ref.setAbbrevTitleCache(null, false);
64 }
65 }
66
67
68 @Override
69 @Autowired
70 protected void setDao(IReferenceDao dao) {
71 this.dao = dao;
72 }
73
74 @Override
75 public List<UuidAndTitleCache<Reference>> getUuidAndTitle() {
76
77 return dao.getUuidAndTitle();
78 }
79
80 @Override
81 public List<Reference> getAllReferencesForPublishing(){
82 return dao.getAllNotNomenclaturalReferencesForPublishing();
83 }
84
85 @Override
86 public List<Reference> getAllNomenclaturalReferences() {
87
88 return dao.getAllNomenclaturalReferences();
89 }
90
91 @Override
92 public List<TaxonBase> listCoveredTaxa(Reference reference, boolean includeSubordinateReferences, List<String> propertyPaths) {
93
94 List<TaxonBase> taxonList = dao.listCoveredTaxa(reference, includeSubordinateReferences, null, propertyPaths);
95
96 return taxonList;
97 }
98
99 @Override
100 public DeleteResult delete(Reference reference) {
101 //check whether the reference is used somewhere
102 DeleteResult result = isDeletable(reference.getUuid(), null);
103
104 if (result.isOk()){
105 dao.delete(reference);
106 result.addDeletedObject(reference);
107 }
108
109 return result;
110 }
111
112 @Override
113 @Transactional(readOnly=false)
114 public DeleteResult delete(UUID referenceUuid) {
115 return delete(dao.load(referenceUuid));
116 }
117
118 /* (non-Javadoc)
119 * @see eu.etaxonomy.cdm.api.service.IReferenceService#getUuidAndAbbrevTitleCache(java.lang.Integer, java.lang.String)
120 */
121 @Override
122 public List<UuidAndTitleCache<Reference>> getUuidAndAbbrevTitleCache(Integer limit, String pattern) {
123 return dao.getUuidAndAbbrevTitleCache(limit, pattern, null);
124 }
125
126 /* (non-Javadoc)
127 * @see eu.etaxonomy.cdm.api.service.IReferenceService#getUuidAndAbbrevTitleCache(java.lang.Integer, java.lang.String)
128 */
129 @Override
130 public List<UuidAndTitleCache<Reference>> getUuidAndAbbrevTitleCache(Integer limit, String pattern, ReferenceType type) {
131 ReferenceType inReferenceType = null;
132 inReferenceType = getInReferenceType(type);
133 return dao.getUuidAndAbbrevTitleCache(limit, pattern, inReferenceType);
134 }
135
136 /* (non-Javadoc)
137 * @see eu.etaxonomy.cdm.api.service.IReferenceService#getUuidAndAbbrevTitleCache(java.lang.Integer, java.lang.String)
138 */
139 @Override
140 public List<UuidAndTitleCache<Reference>> getUuidAndAbbrevTitleCacheForAuthor(Integer limit, String pattern, ReferenceType type) {
141 return dao.getUuidAndAbbrevTitleCacheForAuthor(limit, pattern, null);
142 }
143
144 @Override
145 public List<UuidAndTitleCache<Reference>> getUuidAndTitleCache(Integer limit, String pattern, ReferenceType type) {
146 ReferenceType inReferenceType = null;
147 inReferenceType = getInReferenceType(type);
148 return dao.getUuidAndTitleCache(limit, pattern, inReferenceType);
149 }
150
151
152 @Transactional(readOnly = true)
153 @Override
154 public List<IdentifiedEntityDTO<Reference>> listByIdentifierAbbrev(
155 String identifier, DefinedTerm identifierType, MatchMode matchmode,
156 Integer limit) {
157
158 Integer numberOfResults = dao.countByIdentifier(Reference.class, identifier, identifierType, matchmode);
159 List<Object[]> daoResults = new ArrayList<Object[]>();
160 if(numberOfResults > 0) { // no point checking again
161 daoResults = dao.findByIdentifierAbbrev( identifier, identifierType,
162 matchmode, limit);
163 }
164
165 List<IdentifiedEntityDTO<Reference>> result = new ArrayList<IdentifiedEntityDTO<Reference>>();
166 for (Object[] daoObj : daoResults){
167 result.add(new IdentifiedEntityDTO<Reference>((DefinedTerm)daoObj[0], (String)daoObj[1], (UUID)daoObj[2], (String)daoObj[3],(String)daoObj[4]));
168
169 }
170 return result;
171 }
172
173
174 private ReferenceType getInReferenceType(ReferenceType type){
175 ReferenceType inReferenceType = null;
176 if (type.equals(ReferenceType.Article)){
177 inReferenceType = ReferenceType.Journal;
178 } else if (type.equals(ReferenceType.BookSection)){
179 inReferenceType = ReferenceType.Book;
180 } else if (type.equals(ReferenceType.InProceedings) ){
181 inReferenceType = ReferenceType.Proceedings;
182 } else if (type.equals(ReferenceType.Book) || type.equals(ReferenceType.Proceedings)){
183 inReferenceType = ReferenceType.PrintSeries;
184 } else if (type.equals(ReferenceType.Generic)){
185 inReferenceType = ReferenceType.Generic;
186 }
187 return inReferenceType;
188 }
189
190 /**
191 * {@inheritDoc}
192 */
193 @Override
194 public List<UuidAndTitleCache<Reference>> getUuidAndTitleCacheForUUIDS(Set<UUID> uuids) {
195 return dao.getUuidAndTitle(uuids);
196 }
197
198 }