1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
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.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.cdm.api.service;
|
12
|
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.List;
|
15
|
import java.util.Set;
|
16
|
import java.util.UUID;
|
17
|
|
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.config.DeleteConfiguratorBase;
|
23
|
import eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator;
|
24
|
import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;
|
25
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
26
|
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
|
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;
|
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;
|
35
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
36
|
import eu.etaxonomy.cdm.model.media.Media;
|
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;
|
42
|
|
43
|
@Service
|
44
|
@Transactional(readOnly=true)
|
45
|
public class MediaServiceImpl extends IdentifiableServiceBase<Media,IMediaDao> implements IMediaService {
|
46
|
|
47
|
@Override
|
48
|
@Autowired
|
49
|
protected void setDao(IMediaDao dao) {
|
50
|
this.dao = dao;
|
51
|
}
|
52
|
|
53
|
@Autowired
|
54
|
private IDescriptionService descriptionService;
|
55
|
|
56
|
@Override
|
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);
|
59
|
|
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);
|
63
|
}
|
64
|
|
65
|
return new DefaultPagerImpl<MediaKey>(pageNumber, numberOfResults, pageSize, results);
|
66
|
}
|
67
|
|
68
|
@Override
|
69
|
public Pager<Rights> getRights(Media t, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
70
|
Integer numberOfResults = dao.countRights(t);
|
71
|
|
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);
|
75
|
}
|
76
|
|
77
|
return new DefaultPagerImpl<Rights>(pageNumber, numberOfResults, pageSize, results);
|
78
|
}
|
79
|
|
80
|
/* (non-Javadoc)
|
81
|
* @see eu.etaxonomy.cdm.api.service.IIdentifiableEntityService#updateTitleCache(java.lang.Integer, eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy)
|
82
|
*/
|
83
|
@Override
|
84
|
@Transactional(readOnly = false)
|
85
|
public void updateTitleCache(Class<? extends Media> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<Media> cacheStrategy, IProgressMonitor monitor) {
|
86
|
//IIdentifiableEntityCacheStrategy<Media> cacheStrategy = MediaDefaultCacheStrategy();
|
87
|
if (clazz == null){
|
88
|
clazz = Media.class;
|
89
|
}
|
90
|
super.updateTitleCacheImpl(clazz, stepSize, cacheStrategy, monitor);
|
91
|
}
|
92
|
|
93
|
/* (non-Javadoc)
|
94
|
* @see eu.etaxonomy.cdm.api.service.IMediaService#delete(java.util.UUID, eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator)
|
95
|
*/
|
96
|
@Override
|
97
|
public DeleteResult delete(UUID mediaUuid, MediaDeletionConfigurator config) {
|
98
|
DeleteResult result = new DeleteResult();
|
99
|
Media media = this.load(mediaUuid);
|
100
|
|
101
|
result = isDeletable(media, config);
|
102
|
|
103
|
if (result.isOk()){
|
104
|
Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(media);
|
105
|
String message = null;
|
106
|
for (CdmBase ref: references){
|
107
|
if (ref instanceof TextData){
|
108
|
TextData textData = HibernateProxyHelper.deproxy(ref, TextData.class);
|
109
|
DescriptionBase description = textData.getInDescription();
|
110
|
if (description.isImageGallery()){
|
111
|
if (description instanceof TaxonDescription){
|
112
|
TaxonDescription desc = HibernateProxyHelper.deproxy(description, TaxonDescription.class);
|
113
|
if (desc.getTaxon() == null ){
|
114
|
continue;
|
115
|
} else if ( config.isDeleteIfUsedInTaxonDescription()){
|
116
|
Taxon taxon = desc.getTaxon();
|
117
|
desc.removeElement(textData);
|
118
|
textData.removeMedia(media);
|
119
|
if (desc.getElements().isEmpty()){
|
120
|
taxon.removeDescription(desc);
|
121
|
}
|
122
|
}
|
123
|
} else if (description instanceof SpecimenDescription){
|
124
|
SpecimenDescription desc = HibernateProxyHelper.deproxy(description, SpecimenDescription.class);
|
125
|
if (desc.getDescribedSpecimenOrObservation() == null || config.isDeleteIfUsedInSpecimenDescription()){
|
126
|
continue;
|
127
|
} else if (config.isDeleteIfUsedInSpecimenDescription()){
|
128
|
SpecimenOrObservationBase<?> specimen = desc.getDescribedSpecimenOrObservation();
|
129
|
desc.removeElement(textData);
|
130
|
textData.removeMedia(media);
|
131
|
if (desc.getElements().isEmpty()){
|
132
|
specimen.removeDescription(desc);
|
133
|
}
|
134
|
}
|
135
|
}
|
136
|
}
|
137
|
}
|
138
|
dao.delete(media);
|
139
|
}
|
140
|
}
|
141
|
return result;
|
142
|
}
|
143
|
|
144
|
@Override
|
145
|
public DeleteResult isDeletable(Media media, DeleteConfiguratorBase config){
|
146
|
DeleteResult result = new DeleteResult();
|
147
|
Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(media);
|
148
|
String message = null;
|
149
|
MediaDeletionConfigurator mediaConfig = (MediaDeletionConfigurator)config;
|
150
|
for (CdmBase ref: references){
|
151
|
if (ref instanceof TextData){
|
152
|
TextData textData = HibernateProxyHelper.deproxy(ref, TextData.class);
|
153
|
DescriptionBase description = textData.getInDescription();
|
154
|
if (description.isImageGallery()){
|
155
|
if (description instanceof TaxonDescription){
|
156
|
TaxonDescription desc = HibernateProxyHelper.deproxy(description, TaxonDescription.class);
|
157
|
if (desc.getTaxon() == null || mediaConfig.isDeleteIfUsedInTaxonDescription()){
|
158
|
continue;
|
159
|
} else{
|
160
|
message = "The media can't be deleted because it is referenced by a taxon. ("+desc.getTaxon().getTitleCache()+")";
|
161
|
}
|
162
|
|
163
|
} else if (description instanceof SpecimenDescription){
|
164
|
SpecimenDescription desc = HibernateProxyHelper.deproxy(description, SpecimenDescription.class);
|
165
|
if (desc.getDescribedSpecimenOrObservation() == null || mediaConfig.isDeleteIfUsedInSpecimenDescription()){
|
166
|
continue;
|
167
|
} else{
|
168
|
message = "The media can't be deleted because it is referenced by a specimen or observation. ("+desc.getDescribedSpecimenOrObservation().getTitleCache()+")";
|
169
|
}
|
170
|
}
|
171
|
}
|
172
|
}
|
173
|
if (message != null){
|
174
|
result.addException(new ReferencedObjectUndeletableException(message));
|
175
|
result.addRelatedObject(ref);
|
176
|
result.setAbort();
|
177
|
}
|
178
|
|
179
|
}
|
180
|
|
181
|
|
182
|
return result;
|
183
|
|
184
|
}
|
185
|
}
|