Project

General

Profile

Download (12.4 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
        if (media == null){
106
            return result;
107
        }
108
        result = isDeletable(mediaUuid, config);
109
        if (result.isOk()){
110
            Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(media);
111
            for (CdmBase ref: references){
112

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

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

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

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

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

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

    
171
        }
172
        return result;
173
    }
174

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

    
196

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

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

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

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

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

    
255
            }
256

    
257
        }
258

    
259
        return result;
260
    }
261
}
(74-74/105)