Revision d9f134f9
Added by Katja Luther about 8 years ago
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/MediaServiceImpl.java | ||
---|---|---|
1 | 1 |
// $Id$ |
2 | 2 |
/** |
3 | 3 |
* Copyright (C) 2007 EDIT |
4 |
* European Distributed Institute of Taxonomy
|
|
4 |
* European Distributed Institute of Taxonomy |
|
5 | 5 |
* http://www.e-taxonomy.eu |
6 |
*
|
|
6 |
* |
|
7 | 7 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
8 | 8 |
* See LICENSE.TXT at the top of this package for the full license terms. |
9 | 9 |
*/ |
... | ... | |
13 | 13 |
import java.util.ArrayList; |
14 | 14 |
import java.util.List; |
15 | 15 |
import java.util.Set; |
16 |
import java.util.UUID; |
|
16 | 17 |
|
17 | 18 |
import org.springframework.beans.factory.annotation.Autowired; |
18 | 19 |
import org.springframework.stereotype.Service; |
19 | 20 |
import org.springframework.transaction.annotation.Transactional; |
20 | 21 |
|
22 |
import eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase; |
|
23 |
import eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator; |
|
24 |
import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException; |
|
21 | 25 |
import eu.etaxonomy.cdm.api.service.pager.Pager; |
22 | 26 |
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl; |
23 | 27 |
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor; |
28 |
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper; |
|
29 |
import eu.etaxonomy.cdm.model.common.CdmBase; |
|
30 |
import eu.etaxonomy.cdm.model.description.DescriptionBase; |
|
24 | 31 |
import eu.etaxonomy.cdm.model.description.MediaKey; |
32 |
import eu.etaxonomy.cdm.model.description.SpecimenDescription; |
|
33 |
import eu.etaxonomy.cdm.model.description.TaxonDescription; |
|
34 |
import eu.etaxonomy.cdm.model.description.TextData; |
|
25 | 35 |
import eu.etaxonomy.cdm.model.location.NamedArea; |
26 | 36 |
import eu.etaxonomy.cdm.model.media.Media; |
27 | 37 |
import eu.etaxonomy.cdm.model.media.Rights; |
... | ... | |
33 | 43 |
@Transactional(readOnly=true) |
34 | 44 |
public class MediaServiceImpl extends IdentifiableServiceBase<Media,IMediaDao> implements IMediaService { |
35 | 45 |
|
36 |
@Autowired |
|
46 |
@Override |
|
47 |
@Autowired |
|
37 | 48 |
protected void setDao(IMediaDao dao) { |
38 | 49 |
this.dao = dao; |
39 | 50 |
} |
40 | 51 |
|
41 |
public Pager<MediaKey> getMediaKeys(Set<Taxon> taxonomicScope, Set<NamedArea> geoScopes, Integer pageSize, Integer pageNumber, List<String> propertyPaths) { |
|
52 |
@Autowired |
|
53 |
private IDescriptionService descriptionService; |
|
54 |
|
|
55 |
@Override |
|
56 |
public Pager<MediaKey> getMediaKeys(Set<Taxon> taxonomicScope, Set<NamedArea> geoScopes, Integer pageSize, Integer pageNumber, List<String> propertyPaths) { |
|
42 | 57 |
Integer numberOfResults = dao.countMediaKeys(taxonomicScope, geoScopes); |
43 |
|
|
58 |
|
|
44 | 59 |
List<MediaKey> results = new ArrayList<MediaKey>(); |
45 | 60 |
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize) |
46 |
results = dao.getMediaKeys(taxonomicScope, geoScopes, pageSize, pageNumber, propertyPaths);
|
|
61 |
results = dao.getMediaKeys(taxonomicScope, geoScopes, pageSize, pageNumber, propertyPaths); |
|
47 | 62 |
} |
48 |
|
|
63 |
|
|
49 | 64 |
return new DefaultPagerImpl<MediaKey>(pageNumber, numberOfResults, pageSize, results); |
50 | 65 |
} |
51 |
|
|
52 |
public Pager<Rights> getRights(Media t, Integer pageSize, Integer pageNumber, List<String> propertyPaths) { |
|
66 |
|
|
67 |
@Override |
|
68 |
public Pager<Rights> getRights(Media t, Integer pageSize, Integer pageNumber, List<String> propertyPaths) { |
|
53 | 69 |
Integer numberOfResults = dao.countRights(t); |
54 |
|
|
70 |
|
|
55 | 71 |
List<Rights> results = new ArrayList<Rights>(); |
56 | 72 |
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize) |
57 |
results = dao.getRights(t, pageSize, pageNumber,propertyPaths);
|
|
73 |
results = dao.getRights(t, pageSize, pageNumber,propertyPaths); |
|
58 | 74 |
} |
59 |
|
|
75 |
|
|
60 | 76 |
return new DefaultPagerImpl<Rights>(pageNumber, numberOfResults, pageSize, results); |
61 | 77 |
} |
62 | 78 |
|
... | ... | |
72 | 88 |
} |
73 | 89 |
super.updateTitleCacheImpl(clazz, stepSize, cacheStrategy, monitor); |
74 | 90 |
} |
91 |
|
|
92 |
/* (non-Javadoc) |
|
93 |
* @see eu.etaxonomy.cdm.api.service.IMediaService#delete(java.util.UUID, eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator) |
|
94 |
*/ |
|
95 |
@Override |
|
96 |
public DeleteResult delete(UUID mediaUuid, MediaDeletionConfigurator config) { |
|
97 |
DeleteResult result = new DeleteResult(); |
|
98 |
Media media = this.load(mediaUuid); |
|
99 |
|
|
100 |
result = isDeletable(media, config); |
|
101 |
|
|
102 |
if (result.isOk()){ |
|
103 |
dao.delete(media); |
|
104 |
} |
|
105 |
return result; |
|
106 |
} |
|
107 |
|
|
108 |
@Override |
|
109 |
public DeleteResult isDeletable(Media media, DeleteConfiguratorBase config){ |
|
110 |
DeleteResult result = new DeleteResult(); |
|
111 |
Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(media); |
|
112 |
String message = null; |
|
113 |
for (CdmBase ref: references){ |
|
114 |
if (ref instanceof TextData){ |
|
115 |
TextData textData = HibernateProxyHelper.deproxy(ref, TextData.class); |
|
116 |
DescriptionBase description = textData.getInDescription(); |
|
117 |
if (description.isImageGallery()){ |
|
118 |
if (description instanceof TaxonDescription){ |
|
119 |
TaxonDescription desc = HibernateProxyHelper.deproxy(description, TaxonDescription.class); |
|
120 |
if (desc.getTaxon() == null){ |
|
121 |
continue; |
|
122 |
} else{ |
|
123 |
message = "The media can't be deleted because it is referenced by a taxon. ("+desc.getTaxon().getTitleCache()+")"; |
|
124 |
} |
|
125 |
|
|
126 |
} else if (description instanceof SpecimenDescription){ |
|
127 |
SpecimenDescription desc = HibernateProxyHelper.deproxy(description, SpecimenDescription.class); |
|
128 |
if (desc.getDescribedSpecimenOrObservation() == null){ |
|
129 |
continue; |
|
130 |
} else{ |
|
131 |
message = "The media can't be deleted because it is referenced by a specimen or observation. ("+desc.getDescribedSpecimenOrObservation().getTitleCache()+")"; |
|
132 |
} |
|
133 |
} |
|
134 |
} |
|
135 |
} |
|
136 |
if (message != null){ |
|
137 |
result.addException(new ReferencedObjectUndeletableException(message)); |
|
138 |
result.addRelatedObject(ref); |
|
139 |
result.setAbort(); |
|
140 |
} |
|
141 |
|
|
142 |
} |
|
143 |
|
|
144 |
|
|
145 |
return result; |
|
146 |
|
|
147 |
} |
|
75 | 148 |
} |
Also available in: Unified diff
TaxonNodeByNameComparator is not a component anymore and add configurable delete method for media