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.taxon.Taxon;
|
39
|
import eu.etaxonomy.cdm.persistence.dao.media.IMediaDao;
|
40
|
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
|
41
|
|
42
|
@Service
|
43
|
@Transactional(readOnly=true)
|
44
|
public class MediaServiceImpl extends IdentifiableServiceBase<Media,IMediaDao> implements IMediaService {
|
45
|
|
46
|
@Override
|
47
|
@Autowired
|
48
|
protected void setDao(IMediaDao dao) {
|
49
|
this.dao = dao;
|
50
|
}
|
51
|
|
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) {
|
57
|
Integer numberOfResults = dao.countMediaKeys(taxonomicScope, geoScopes);
|
58
|
|
59
|
List<MediaKey> results = new ArrayList<MediaKey>();
|
60
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
61
|
results = dao.getMediaKeys(taxonomicScope, geoScopes, pageSize, pageNumber, propertyPaths);
|
62
|
}
|
63
|
|
64
|
return new DefaultPagerImpl<MediaKey>(pageNumber, numberOfResults, pageSize, results);
|
65
|
}
|
66
|
|
67
|
@Override
|
68
|
public Pager<Rights> getRights(Media t, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
69
|
Integer numberOfResults = dao.countRights(t);
|
70
|
|
71
|
List<Rights> results = new ArrayList<Rights>();
|
72
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
73
|
results = dao.getRights(t, pageSize, pageNumber,propertyPaths);
|
74
|
}
|
75
|
|
76
|
return new DefaultPagerImpl<Rights>(pageNumber, numberOfResults, pageSize, results);
|
77
|
}
|
78
|
|
79
|
/* (non-Javadoc)
|
80
|
* @see eu.etaxonomy.cdm.api.service.IIdentifiableEntityService#updateTitleCache(java.lang.Integer, eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy)
|
81
|
*/
|
82
|
@Override
|
83
|
@Transactional(readOnly = false)
|
84
|
public void updateTitleCache(Class<? extends Media> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<Media> cacheStrategy, IProgressMonitor monitor) {
|
85
|
//IIdentifiableEntityCacheStrategy<Media> cacheStrategy = MediaDefaultCacheStrategy();
|
86
|
if (clazz == null){
|
87
|
clazz = Media.class;
|
88
|
}
|
89
|
super.updateTitleCacheImpl(clazz, stepSize, cacheStrategy, monitor);
|
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
|
}
|
148
|
}
|