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