3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.cdm
.api
.service
;
14 import java
.util
.ArrayList
;
15 import java
.util
.Collection
;
16 import java
.util
.Enumeration
;
17 import java
.util
.HashSet
;
18 import java
.util
.Iterator
;
19 import java
.util
.List
;
20 import java
.util
.Locale
;
22 import java
.util
.UUID
;
24 import org
.apache
.commons
.lang
.StringUtils
;
25 import org
.apache
.log4j
.Logger
;
26 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
27 import org
.springframework
.beans
.factory
.annotation
.Qualifier
;
28 import org
.springframework
.stereotype
.Service
;
29 import org
.springframework
.transaction
.annotation
.Transactional
;
31 import eu
.etaxonomy
.cdm
.api
.service
.UpdateResult
.Status
;
32 import eu
.etaxonomy
.cdm
.api
.service
.config
.DeleteConfiguratorBase
;
33 import eu
.etaxonomy
.cdm
.api
.service
.config
.TermDeletionConfigurator
;
34 import eu
.etaxonomy
.cdm
.api
.service
.exception
.DataChangeNoRollbackException
;
35 import eu
.etaxonomy
.cdm
.api
.service
.exception
.ReferencedObjectUndeletableException
;
36 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
37 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.DefaultPagerImpl
;
38 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
39 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
40 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
41 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
42 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
43 import eu
.etaxonomy
.cdm
.model
.common
.LanguageStringBase
;
44 import eu
.etaxonomy
.cdm
.model
.common
.Representation
;
45 import eu
.etaxonomy
.cdm
.model
.common
.TermType
;
46 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
47 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
48 import eu
.etaxonomy
.cdm
.model
.location
.NamedAreaLevel
;
49 import eu
.etaxonomy
.cdm
.model
.location
.NamedAreaType
;
50 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
51 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.IDefinedTermDao
;
52 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ILanguageStringBaseDao
;
53 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ILanguageStringDao
;
54 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.IRepresentationDao
;
55 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
56 import eu
.etaxonomy
.cdm
.strategy
.cache
.common
.IIdentifiableEntityCacheStrategy
;
59 @Transactional(readOnly
= true)
60 public class TermServiceImpl
extends IdentifiableServiceBase
<DefinedTermBase
,IDefinedTermDao
> implements ITermService
{
61 @SuppressWarnings("unused")
62 private static final Logger logger
= Logger
.getLogger(TermServiceImpl
.class);
64 private ILanguageStringDao languageStringDao
;
67 @Qualifier("langStrBaseDao")
68 private ILanguageStringBaseDao languageStringBaseDao
;
69 private IRepresentationDao representationDao
;
72 public void setLanguageStringDao(ILanguageStringDao languageStringDao
) {
73 this.languageStringDao
= languageStringDao
;
77 public void setRepresentationDao(IRepresentationDao representationDao
) {
78 this.representationDao
= representationDao
;
83 protected void setDao(IDefinedTermDao dao
) {
88 * @see eu.etaxonomy.cdm.api.service.ITermService#listByTermType(eu.etaxonomy.cdm.model.common.TermType, java.lang.Integer, java.lang.Integer, java.util.List, java.util.List)
91 public List
<DefinedTermBase
<?
>> listByTermType(TermType termType
, Integer limit
, Integer start
,
92 List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
93 return dao
.listByTermType(termType
, limit
, start
, orderHints
, propertyPaths
);
97 public DefinedTermBase
getByUri(URI uri
) {
98 return dao
.findByUri(uri
);
102 public Language
getLanguageByIso(String iso639
) {
103 return dao
.getLanguageByIso(iso639
);
107 public Language
getLanguageByLabel(String label
) {
108 return Language
.getLanguageByLabel(label
);
112 public List
<Language
> getLanguagesByLocale(Enumeration
<Locale
> locales
){
113 return dao
.getLanguagesByLocale(locales
);
117 public <TERM
extends DefinedTermBase
> TERM
findByIdInVocabulary(String id
, UUID vocabularyUuid
, Class
<TERM
> clazz
) throws IllegalArgumentException
{
118 List
<TERM
> list
= dao
.getDefinedTermByIdInVocabulary(id
, vocabularyUuid
, clazz
, null, null);
121 }else if (list
.size() == 1){
124 String message
= "There is more then 1 (%d) term with the same id in vocabulary. This is forbidden. Check the state of your database.";
125 throw new IllegalStateException(String
.format(message
, list
.size()));
131 public NamedArea
getAreaByTdwgAbbreviation(String tdwgAbbreviation
) {
132 if (StringUtils
.isBlank(tdwgAbbreviation
)){ //TDWG areas should always have a label
135 List
<NamedArea
> list
= dao
.getDefinedTermByIdInVocabulary(tdwgAbbreviation
, NamedArea
.uuidTdwgAreaVocabulary
, NamedArea
.class, null, null);
138 }else if (list
.size() == 1){
141 String message
= "There is more then 1 (%d) TDWG area with the same abbreviated label. This is forbidden. Check the state of your database.";
142 throw new IllegalStateException(String
.format(message
, list
.size()));
148 public <T
extends DefinedTermBase
> Pager
<T
> getGeneralizationOf(T definedTerm
, Integer pageSize
, Integer pageNumber
) {
149 Integer numberOfResults
= dao
.countGeneralizationOf(definedTerm
);
151 List
<T
> results
= new ArrayList
<T
>();
152 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
153 results
= dao
.getGeneralizationOf(definedTerm
, pageSize
, pageNumber
);
156 return new DefaultPagerImpl
<T
>(pageNumber
, numberOfResults
, pageSize
, results
);
160 public <T
extends DefinedTermBase
> Pager
<T
> getIncludes(Collection
<T
> definedTerms
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
161 Integer numberOfResults
= dao
.countIncludes(definedTerms
);
163 List
<T
> results
= new ArrayList
<T
>();
164 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
165 results
= dao
.getIncludes(definedTerms
, pageSize
, pageNumber
,propertyPaths
);
168 return new DefaultPagerImpl
<T
>(pageNumber
, numberOfResults
, pageSize
, results
);
172 public Pager
<Media
> getMedia(DefinedTermBase definedTerm
, Integer pageSize
, Integer pageNumber
) {
173 Integer numberOfResults
= dao
.countMedia(definedTerm
);
175 List
<Media
> results
= new ArrayList
<Media
>();
176 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
177 results
= dao
.getMedia(definedTerm
, pageSize
, pageNumber
);
180 return new DefaultPagerImpl
<Media
>(pageNumber
, numberOfResults
, pageSize
, results
);
184 public <T
extends DefinedTermBase
> Pager
<T
> getPartOf(Set
<T
> definedTerms
,Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
185 Integer numberOfResults
= dao
.countPartOf(definedTerms
);
187 List
<T
> results
= new ArrayList
<T
>();
188 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
189 results
= dao
.getPartOf(definedTerms
, pageSize
, pageNumber
, propertyPaths
);
192 return new DefaultPagerImpl
<T
>(pageNumber
, numberOfResults
, pageSize
, results
);
196 public Pager
<NamedArea
> list(NamedAreaLevel level
, NamedAreaType type
, Integer pageSize
, Integer pageNumber
,
197 List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
198 Integer numberOfResults
= dao
.count(level
, type
);
200 List
<NamedArea
> results
= new ArrayList
<NamedArea
>();
201 if (numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
202 results
= dao
.list(level
, type
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
205 return new DefaultPagerImpl
<NamedArea
>(pageNumber
, numberOfResults
, pageSize
, results
);
209 public <T
extends DefinedTermBase
> Pager
<T
> findByRepresentationText(String label
, Class
<T
> clazz
, Integer pageSize
, Integer pageNumber
) {
210 Integer numberOfResults
= dao
.countDefinedTermByRepresentationText(label
,clazz
);
212 List
<T
> results
= new ArrayList
<T
>();
213 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
214 results
= dao
.getDefinedTermByRepresentationText(label
, clazz
, pageSize
, pageNumber
);
217 return new DefaultPagerImpl
<T
>(pageNumber
, numberOfResults
, pageSize
, results
);
221 public <T
extends DefinedTermBase
> Pager
<T
> findByRepresentationAbbreviation(String abbrev
, Class
<T
> clazz
, Integer pageSize
, Integer pageNumber
) {
222 Integer numberOfResults
= dao
.countDefinedTermByRepresentationAbbrev(abbrev
,clazz
);
224 List
<T
> results
= new ArrayList
<T
>();
225 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
226 results
= dao
.getDefinedTermByRepresentationAbbrev(abbrev
, clazz
, pageSize
, pageNumber
);
229 return new DefaultPagerImpl
<T
>(pageNumber
, numberOfResults
, pageSize
, results
);
233 public List
<LanguageString
> getAllLanguageStrings(int limit
, int start
) {
234 return languageStringDao
.list(limit
, start
);
238 public List
<Representation
> getAllRepresentations(int limit
, int start
) {
239 return representationDao
.list(limit
,start
);
243 public UUID
saveLanguageData(LanguageStringBase languageData
) {
244 return languageStringBaseDao
.save(languageData
).getUuid();
248 * @see eu.etaxonomy.cdm.api.service.ServiceBase#delete(eu.etaxonomy.cdm.model.common.CdmBase)
250 /** @deprecated use {@link #delete(DefinedTermBase, TermDeletionConfigurator)} instead
251 * to allow DeleteResult return type*/
254 public DeleteResult
delete(DefinedTermBase term
){
255 DeleteResult result
= new DeleteResult();
257 TermDeletionConfigurator defaultConfig
= new TermDeletionConfigurator();
258 result
= delete(term
, defaultConfig
);
264 @Transactional(readOnly
= false)
265 public DeleteResult
delete(UUID termUuid
){
266 DeleteResult result
= new DeleteResult();
268 TermDeletionConfigurator defaultConfig
= new TermDeletionConfigurator();
269 result
= delete(dao
.load(termUuid
), defaultConfig
);
274 public DeleteResult
delete(DefinedTermBase term
, TermDeletionConfigurator config
){
276 config
= new TermDeletionConfigurator();
278 // boolean isInternal = config.isInternal();
280 Set
<DefinedTermBase
> termsToSave
= new HashSet
<DefinedTermBase
>();
282 DeleteResult result
= isDeletable(term
, config
);
283 //CdmBase.deproxy(dao.merge(term), DefinedTermBase.class);
286 Set
<DefinedTermBase
> specificTerms
= term
.getGeneralizationOf();
287 if (specificTerms
.size()>0){
288 if (config
.isDeleteGeneralizationOfRelations()){
289 DefinedTermBase generalTerm
= term
.getKindOf();
290 for (DefinedTermBase specificTerm
: specificTerms
){
291 term
.removeGeneralization(specificTerm
);
292 if (generalTerm
!= null){
293 generalTerm
.addGeneralizationOf(specificTerm
);
294 termsToSave
.add(generalTerm
);
298 //TODO Exception type
299 String message
= "This term has specifing terms. Move or delete specifiing terms prior to delete or change delete configuration.";
300 result
.addRelatedObjects(specificTerms
);
302 Exception ex
= new DataChangeNoRollbackException(message
);
303 result
.addException(ex
);
308 DefinedTermBase generalTerm
= term
.getKindOf();
309 if (generalTerm
!= null){
310 if (config
.isDeleteKindOfRelations()){
311 generalTerm
.removeGeneralization(term
);
313 //TODO Exception type
314 String message
= "This term is kind of another term. Move or delete kind of relationship prior to delete or change delete configuration.";
315 result
.addRelatedObject(generalTerm
);
317 DataChangeNoRollbackException ex
= new DataChangeNoRollbackException(message
);
318 result
.addException(ex
);
324 DefinedTermBase parentTerm
= term
.getPartOf();
325 if (parentTerm
!= null){
326 if (! config
.isDeletePartOfRelations()){
327 //TODO Exception type
328 String message
= "This term is included in another term. Remove from parent term prior to delete or change delete configuration.";
329 result
.addRelatedObject(parentTerm
);
331 DataChangeNoRollbackException ex
= new DataChangeNoRollbackException(message
);
332 result
.addException(ex
);
338 Set
<DefinedTermBase
> includedTerms
= term
.getIncludes();
339 if (includedTerms
.size()> 0){
340 // if (config.isDeleteIncludedTerms()){
341 // for (DefinedTermBase includedTerm: includedTerms){
342 // config.setCheck(true);
343 // DeleteResult includedResult = this.delete(includedTerm, config);
344 //// config.setCheck(isCheck);
345 // result.includeResult(includedResult);
348 if (config
.isDeleteIncludedRelations()){
349 DefinedTermBase parent
= term
.getPartOf();
350 for (DefinedTermBase includedTerm
: includedTerms
){
351 term
.removeIncludes(includedTerm
);
353 parent
.addIncludes(includedTerm
);
354 termsToSave
.add(parent
);
358 //TODO Exception type
359 String message
= "This term includes other terms. Move or delete included terms prior to delete or change delete configuration.";
360 result
.addRelatedObjects(includedTerms
);
362 Exception ex
= new DataChangeNoRollbackException(message
);
363 result
.addException(ex
);
368 if (parentTerm
!= null){
369 if (config
.isDeletePartOfRelations()){
370 parentTerm
.removeIncludes(term
);
371 termsToSave
.add(parentTerm
);
373 //handelede before "included in"
381 TermVocabulary voc
= term
.getVocabulary();
383 voc
.removeTerm(term
);
386 if (true /*!config.isInternal()*/){
388 dao
.saveOrUpdateAll(termsToSave
);
389 // for (DeleteResult.PersistPair persistPair : result.getObjectsToDelete()){
390 // persistPair.dao.delete(persistPair.objectToPersist);
392 // for (DeleteResult.PersistPair persistPair : result.getObjectsToSave()){
393 // persistPair.dao.saveOrUpdate(persistPair.objectToPersist);
398 } catch (DataChangeNoRollbackException e
) {
399 result
.setStatus(Status
.ERROR
);
405 @Transactional(readOnly
= false)
406 public DeleteResult
delete(UUID termUuid
, TermDeletionConfigurator config
){
407 return delete(dao
.load(termUuid
), config
);
411 @Transactional(readOnly
= false)
412 public void updateTitleCache(Class
<?
extends DefinedTermBase
> clazz
, Integer stepSize
, IIdentifiableEntityCacheStrategy
<DefinedTermBase
> cacheStrategy
, IProgressMonitor monitor
) {
413 //TODO shouldnt this be TermBase instead of DefinedTermBase
415 clazz
= DefinedTermBase
.class;
417 super.updateTitleCacheImpl(clazz
, stepSize
, cacheStrategy
, monitor
);
421 public DeleteResult
isDeletable(DefinedTermBase term
, DeleteConfiguratorBase config
){
422 DeleteResult result
= new DeleteResult();
423 Set
<CdmBase
> references
= commonService
.getReferencingObjectsForDeletion(term
);
424 if (references
!= null){
425 result
.addRelatedObjects(references
);
426 Iterator
<CdmBase
> iterator
= references
.iterator();
428 while (iterator
.hasNext()){
429 ref
= iterator
.next();
430 if (ref
instanceof TermVocabulary
){
431 result
.getRelatedObjects().remove(ref
);
434 String message
= "An object of " + ref
.getClass().getName() + " with ID " + ref
.getId() + " is referencing the object" ;
435 result
.addException(new ReferencedObjectUndeletableException(message
));