Project

General

Profile

Download (12.2 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
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.
8
 */
9

    
10
package eu.etaxonomy.cdm.api.service;
11

    
12
import java.util.ArrayList;
13
import java.util.List;
14
import java.util.Set;
15
import java.util.UUID;
16

    
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.stereotype.Service;
19
import org.springframework.transaction.annotation.Transactional;
20

    
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.common.ICdmBase;
30
import eu.etaxonomy.cdm.model.description.DescriptionBase;
31
import eu.etaxonomy.cdm.model.description.IDescribable;
32
import eu.etaxonomy.cdm.model.description.MediaKey;
33
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
34
import eu.etaxonomy.cdm.model.description.TaxonDescription;
35
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
36
import eu.etaxonomy.cdm.model.description.TextData;
37
import eu.etaxonomy.cdm.model.location.NamedArea;
38
import eu.etaxonomy.cdm.model.media.Media;
39
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
40
import eu.etaxonomy.cdm.model.media.Rights;
41
import eu.etaxonomy.cdm.model.name.TaxonName;
42
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
43
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
44
import eu.etaxonomy.cdm.model.taxon.Taxon;
45
import eu.etaxonomy.cdm.persistence.dao.media.IMediaDao;
46
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
47

    
48
@Service
49
@Transactional(readOnly=true)
50
public class MediaServiceImpl extends IdentifiableServiceBase<Media,IMediaDao> implements IMediaService {
51

    
52
    @Override
53
    @Autowired
54
	protected void setDao(IMediaDao dao) {
55
		this.dao = dao;
56
	}
57

    
58
	@Autowired
59
    private IOccurrenceService specimenService;
60
	@Autowired
61
    private ITaxonService taxonService;
62
	@Autowired
63
    private INameService nameService;
64

    
65

    
66
	@Override
67
    public Pager<MediaKey> getMediaKeys(Set<Taxon> taxonomicScope, Set<NamedArea> geoScopes, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
68
        Integer numberOfResults = dao.countMediaKeys(taxonomicScope, geoScopes);
69

    
70
		List<MediaKey> results = new ArrayList<>();
71
		if(numberOfResults > 0) { // no point checking again  //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
72
			results = dao.getMediaKeys(taxonomicScope, geoScopes, pageSize, pageNumber, propertyPaths);
73
		}
74

    
75
		return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
76
	}
77

    
78
	@Override
79
    public Pager<Rights> getRights(Media t, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
80
        Integer numberOfResults = dao.countRights(t);
81

    
82
		List<Rights> results = new ArrayList<>();
83
		if(numberOfResults > 0) { // no point checking again  //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
84
			results = dao.getRights(t, pageSize, pageNumber,propertyPaths);
85
		}
86

    
87
		return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
88
	}
89

    
90
	@Override
91
	@Transactional(readOnly = false)
92
    public void updateTitleCache(Class<? extends Media> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<Media> cacheStrategy, IProgressMonitor monitor) {
93
		//IIdentifiableEntityCacheStrategy<Media> cacheStrategy = MediaDefaultCacheStrategy();
94
		if (clazz == null){
95
			clazz = Media.class;
96
		}
97
		super.updateTitleCacheImpl(clazz, stepSize, cacheStrategy, monitor);
98
	}
99

    
100
    @Override
101
    @Transactional(readOnly=false)
102
    public DeleteResult delete(UUID mediaUuid, MediaDeletionConfigurator config) {
103
        DeleteResult result = new DeleteResult();
104
        Media media = this.load(mediaUuid);
105

    
106
        result = isDeletable(mediaUuid, config);
107
        if (result.isOk()){
108
            Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(media);
109
            for (CdmBase ref: references){
110

    
111
                IDescribable<?> updatedObject = null;
112
                IService<ICdmBase> service = null;
113
                if (ref instanceof TextData){
114

    
115
                    TextData textData = HibernateProxyHelper.deproxy(ref, TextData.class);
116
                    DescriptionBase<?> description = HibernateProxyHelper.deproxy(textData.getInDescription(), DescriptionBase.class);
117

    
118
                    IDescribable<?> objectToUpdate = null;
119
                    boolean deleteIsMatchingInstance = false;
120
                    if (description instanceof TaxonDescription){
121
                        objectToUpdate = ((TaxonDescription)description).getTaxon();
122
                        deleteIsMatchingInstance = config.getDeleteFrom() instanceof Taxon;
123
                        service = (IService)taxonService;
124
                    }else if (description instanceof SpecimenDescription){
125
                        objectToUpdate = ((SpecimenDescription)description).getDescribedSpecimenOrObservation();
126
                        deleteIsMatchingInstance = config.getDeleteFrom() instanceof SpecimenOrObservationBase;
127
                        service = (IService)specimenService;
128
                    }else if (description instanceof TaxonNameDescription){
129
                        objectToUpdate = ((TaxonNameDescription)description).getTaxonName();
130
                        deleteIsMatchingInstance = config.getDeleteFrom() instanceof TaxonName;
131
                        service = (IService)nameService;
132
                    }else{
133
                        throw new RuntimeException("Unsupported DescriptionBase class");
134
                    }
135

    
136
                    if (objectToUpdate == null ){
137
                        continue;
138
                    } else if ( (config.isDeleteFromDescription() && deleteIsMatchingInstance  &&
139
                                   config.getDeleteFrom().getId() == objectToUpdate.getId())
140
                                || config.isDeleteFromEveryWhere()){
141
                        updatedObject = handleDeleteMedia(media, textData, description,
142
                                (IDescribable)objectToUpdate);
143
                    } else {
144
                        // this should not be happen, because it is not deletable. see isDeletable
145
                        result.setAbort();
146
                    }
147

    
148
//                } else if (ref instanceof MediaSpecimen && config.getDeleteFrom().getId() == ref.getId() && config.getDeleteFrom() instanceof MediaSpecimen){
149
//                        MediaSpecimen mediaSpecimen = HibernateProxyHelper.deproxy(ref, MediaSpecimen.class);
150
//                        mediaSpecimen.setMediaSpecimen(null);
151
//                        updatedObject = mediaSpecimen;
152
//                        service = (IService)specimenService;
153
                }else if (ref instanceof MediaRepresentation){
154
                    continue;
155
                }else {
156
                    result.setAbort();
157
                }
158

    
159
                if (updatedObject != null){
160
                    service.update(updatedObject); //service should always be != null if updatedObject != null
161
                    result.addUpdatedObject((CdmBase)updatedObject);
162
                }
163
            }
164
            if (result.isOk()){
165
                dao.delete(media);
166
            }
167

    
168
        }
169
        return result;
170
    }
171

    
172
    /**
173
     * @param media
174
     * @param textData
175
     * @param desc
176
     * @param taxon
177
     */
178
    private IDescribable<DescriptionBase<?>> handleDeleteMedia(Media media, TextData textData,
179
            DescriptionBase<?> desc, IDescribable<DescriptionBase<?>> describable) {
180
        while(textData.getMedia().contains(media)){
181
            textData.removeMedia(media);
182
        }
183
        //if the textData contains text it should not be deleted
184
        if (textData.getMedia().isEmpty() && textData.getMultilanguageText().isEmpty()){
185
            desc.removeElement(textData);
186
        }
187
        if (desc.getElements().isEmpty()){
188
            describable.removeDescription(desc);
189
        }
190
        return describable;
191
    }
192

    
193

    
194
    @Override
195
    public DeleteResult isDeletable(UUID mediaUuid, DeleteConfiguratorBase config){
196
        DeleteResult result = new DeleteResult();
197
        Media media = this.load(mediaUuid);
198
        Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(media);
199
        MediaDeletionConfigurator mediaConfig = (MediaDeletionConfigurator)config;
200
        CdmBase deleteFrom = mediaConfig.getDeleteFrom();
201

    
202
        if (mediaConfig.isDeleteFromEveryWhere()){
203
           return result;
204
        }
205
        for (CdmBase ref: references){
206
            String message = null;
207
            if (ref instanceof MediaRepresentation){
208
                continue;
209
            }
210
            if (ref instanceof TextData){
211
                TextData textData = HibernateProxyHelper.deproxy(ref, TextData.class);
212
                DescriptionBase description = HibernateProxyHelper.deproxy(textData.getInDescription(), DescriptionBase.class);
213

    
214
                if (description instanceof TaxonDescription){
215
                    TaxonDescription desc = HibernateProxyHelper.deproxy(description, TaxonDescription.class);
216
                    if (desc.getTaxon() == null || (mediaConfig.isDeleteFromDescription() && (deleteFrom instanceof Taxon && ((Taxon)deleteFrom).getId() == desc.getTaxon().getId()))){
217
                        continue;
218
                    } else{
219
                        message = "The media can't be deleted from the database because it is referenced by a taxon. ("+desc.getTaxon().getTitleCache()+")";
220
                        result.setAbort();
221
                    }
222

    
223
                } else if (description instanceof SpecimenDescription){
224
                    SpecimenDescription desc = HibernateProxyHelper.deproxy(description, SpecimenDescription.class);
225
                    if (desc.getDescribedSpecimenOrObservation() == null || (mediaConfig.isDeleteFromDescription() && (deleteFrom instanceof SpecimenOrObservationBase && ((SpecimenOrObservationBase)deleteFrom).getId() == desc.getDescribedSpecimenOrObservation().getId()))){
226
                        continue;
227
                    } else{
228
                        message = "The media can't be deleted from the database because it is referenced by a specimen or observation. ("+desc.getDescribedSpecimenOrObservation().getTitleCache()+")";
229
                        result.setAbort();
230
                    }
231
                } else if (description instanceof TaxonNameDescription){
232
                    TaxonNameDescription desc = HibernateProxyHelper.deproxy(description, TaxonNameDescription.class);
233
                    if (desc.getTaxonName() == null || (mediaConfig.isDeleteFromDescription() && (deleteFrom instanceof TaxonName && ((TaxonName)deleteFrom).getId() == desc.getTaxonName().getId()))){
234
                        continue;
235
                    } else{
236
                        message = "The media can't be deleted from the database because it is referenced by a scientific name. ("+desc.getTaxonName().getTitleCache()+")";
237
                        result.setAbort();
238
                    }
239
                }
240

    
241
            }if (ref instanceof MediaSpecimen){
242
               message = "The media can't be deleted from the database because it is referenced by a mediaspecimen. ("+((MediaSpecimen)ref).getTitleCache()+")";
243
               result.setAbort();
244
            }else {
245
                message = "The media can't be completely deleted because it is referenced by another " + ref.getUserFriendlyTypeName() ;
246
                result.setAbort();
247
            }
248
            if (message != null){
249
                result.addException(new ReferencedObjectUndeletableException(message));
250
                result.addRelatedObject(ref);
251

    
252
            }
253

    
254
        }
255

    
256
        return result;
257
    }
258
}
(73-73/101)