2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.api
.service
;
12 import java
.util
.ArrayList
;
13 import java
.util
.List
;
15 import java
.util
.UUID
;
17 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
18 import org
.springframework
.stereotype
.Service
;
19 import org
.springframework
.transaction
.annotation
.Transactional
;
21 import eu
.etaxonomy
.cdm
.api
.service
.config
.DeleteConfiguratorBase
;
22 import eu
.etaxonomy
.cdm
.api
.service
.config
.MediaDeletionConfigurator
;
23 import eu
.etaxonomy
.cdm
.api
.service
.exception
.ReferencedObjectUndeletableException
;
24 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
25 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.DefaultPagerImpl
;
26 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
27 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
28 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
29 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
30 import eu
.etaxonomy
.cdm
.model
.description
.MediaKey
;
31 import eu
.etaxonomy
.cdm
.model
.description
.SpecimenDescription
;
32 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
33 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
34 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
35 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
36 import eu
.etaxonomy
.cdm
.model
.media
.MediaRepresentation
;
37 import eu
.etaxonomy
.cdm
.model
.media
.Rights
;
38 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
39 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
40 import eu
.etaxonomy
.cdm
.persistence
.dao
.media
.IMediaDao
;
41 import eu
.etaxonomy
.cdm
.strategy
.cache
.common
.IIdentifiableEntityCacheStrategy
;
44 @Transactional(readOnly
=true)
45 public class MediaServiceImpl
extends IdentifiableServiceBase
<Media
,IMediaDao
> implements IMediaService
{
49 protected void setDao(IMediaDao dao
) {
54 private IDescriptionService descriptionService
;
57 public Pager
<MediaKey
> getMediaKeys(Set
<Taxon
> taxonomicScope
, Set
<NamedArea
> geoScopes
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
58 Integer numberOfResults
= dao
.countMediaKeys(taxonomicScope
, geoScopes
);
60 List
<MediaKey
> results
= new ArrayList
<MediaKey
>();
61 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
62 results
= dao
.getMediaKeys(taxonomicScope
, geoScopes
, pageSize
, pageNumber
, propertyPaths
);
65 return new DefaultPagerImpl
<MediaKey
>(pageNumber
, numberOfResults
, pageSize
, results
);
69 public Pager
<Rights
> getRights(Media t
, Integer pageSize
, Integer pageNumber
, List
<String
> propertyPaths
) {
70 Integer numberOfResults
= dao
.countRights(t
);
72 List
<Rights
> results
= new ArrayList
<Rights
>();
73 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
74 results
= dao
.getRights(t
, pageSize
, pageNumber
,propertyPaths
);
77 return new DefaultPagerImpl
<Rights
>(pageNumber
, numberOfResults
, pageSize
, results
);
81 * @see eu.etaxonomy.cdm.api.service.IIdentifiableEntityService#updateTitleCache(java.lang.Integer, eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy)
84 @Transactional(readOnly
= false)
85 public void updateTitleCache(Class
<?
extends Media
> clazz
, Integer stepSize
, IIdentifiableEntityCacheStrategy
<Media
> cacheStrategy
, IProgressMonitor monitor
) {
86 //IIdentifiableEntityCacheStrategy<Media> cacheStrategy = MediaDefaultCacheStrategy();
90 super.updateTitleCacheImpl(clazz
, stepSize
, cacheStrategy
, monitor
);
94 * @see eu.etaxonomy.cdm.api.service.IMediaService#delete(java.util.UUID, eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator)
97 @Transactional(readOnly
=false)
98 public DeleteResult
delete(UUID mediaUuid
, MediaDeletionConfigurator config
) {
99 DeleteResult result
= new DeleteResult();
100 Media media
= this.load(mediaUuid
);
102 result
= isDeletable(mediaUuid
, config
);
105 Set
<CdmBase
> references
= commonService
.getReferencingObjectsForDeletion(media
);
106 String message
= null;
107 for (CdmBase ref
: references
){
108 if (ref
instanceof MediaRepresentation
){
109 MediaRepresentation mediaRep
= HibernateProxyHelper
.deproxy(ref
, MediaRepresentation
.class);
110 media
.removeRepresentation(mediaRep
);
112 else if (ref
instanceof TextData
){
114 TextData textData
= HibernateProxyHelper
.deproxy(ref
, TextData
.class);
115 DescriptionBase description
= HibernateProxyHelper
.deproxy(textData
.getInDescription(), DescriptionBase
.class);
117 if (description
.isImageGallery()){
118 if (description
instanceof TaxonDescription
){
119 TaxonDescription desc
= HibernateProxyHelper
.deproxy(description
, TaxonDescription
.class);
120 if (desc
.getTaxon() == null ){
122 } else if ( config
.isDeleteIfUsedInTaxonDescription()){
123 Taxon taxon
= desc
.getTaxon();
124 desc
.removeElement(textData
);
125 textData
.removeMedia(media
);
126 if (desc
.getElements().isEmpty()){
127 taxon
.removeDescription(desc
);
130 } else if (description
instanceof SpecimenDescription
){
131 SpecimenDescription desc
= HibernateProxyHelper
.deproxy(description
, SpecimenDescription
.class);
132 if (desc
.getDescribedSpecimenOrObservation() == null || config
.isDeleteIfUsedInSpecimenDescription()){
134 } else if (config
.isDeleteIfUsedInSpecimenDescription()){
135 SpecimenOrObservationBase
<?
> specimen
= desc
.getDescribedSpecimenOrObservation();
136 desc
.removeElement(textData
);
137 textData
.removeMedia(media
);
138 if (desc
.getElements().isEmpty()){
139 specimen
.removeDescription(desc
);
153 public DeleteResult
isDeletable(UUID mediaUuid
, DeleteConfiguratorBase config
){
154 DeleteResult result
= new DeleteResult();
155 Media media
= this.load(mediaUuid
);
156 Set
<CdmBase
> references
= commonService
.getReferencingObjectsForDeletion(media
);
157 MediaDeletionConfigurator mediaConfig
= (MediaDeletionConfigurator
)config
;
158 for (CdmBase ref
: references
){
159 String message
= null;
160 if (ref
instanceof TextData
){
161 TextData textData
= HibernateProxyHelper
.deproxy(ref
, TextData
.class);
162 DescriptionBase
<?
> description
= textData
.getInDescription();
163 if (description
.isImageGallery()){
164 if (description
instanceof TaxonDescription
){
165 TaxonDescription desc
= HibernateProxyHelper
.deproxy(description
, TaxonDescription
.class);
166 if (desc
.getTaxon() == null || mediaConfig
.isDeleteIfUsedInTaxonDescription()){
169 message
= "The media can't be deleted because it is referenced by a taxon. ("+desc
.getTaxon().getTitleCache()+")";
172 } else if (description
instanceof SpecimenDescription
){
173 SpecimenDescription desc
= HibernateProxyHelper
.deproxy(description
, SpecimenDescription
.class);
174 if (desc
.getDescribedSpecimenOrObservation() == null || mediaConfig
.isDeleteIfUsedInSpecimenDescription()){
177 message
= "The media can't be deleted because it is referenced by a specimen or observation. ("+desc
.getDescribedSpecimenOrObservation().getTitleCache()+")";
182 if (message
!= null){
183 result
.addException(new ReferencedObjectUndeletableException(message
));
184 result
.addRelatedObject(ref
);