Project

General

Profile

Revision 7620acb4

ID7620acb4e3650faf82b92f6e4536f601d88b8b5b
Parent c7794cca
Child 489923b4

Added by Katja Luther almost 2 years ago

ref #6730: fix delete of media

View differences:

cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/MediaServiceImpl.java
36 36
import eu.etaxonomy.cdm.model.media.Media;
37 37
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
38 38
import eu.etaxonomy.cdm.model.media.Rights;
39
import eu.etaxonomy.cdm.model.name.TaxonName;
39 40
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
40 41
import eu.etaxonomy.cdm.model.taxon.Taxon;
41 42
import eu.etaxonomy.cdm.persistence.dao.media.IMediaDao;
......
106 107
            Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(media);
107 108
            String message = null;
108 109
            for (CdmBase ref: references){
109
                if (ref instanceof MediaRepresentation){
110
                    MediaRepresentation mediaRep = HibernateProxyHelper.deproxy(ref, MediaRepresentation.class);
111
                    media.removeRepresentation(mediaRep);
112
                }
113
                else if (ref instanceof TextData){
110

  
111
               if (ref instanceof TextData){
114 112

  
115 113
                    TextData textData = HibernateProxyHelper.deproxy(ref, TextData.class);
116 114
                    DescriptionBase description = HibernateProxyHelper.deproxy(textData.getInDescription(), DescriptionBase.class);
......
120 118
                            TaxonDescription desc = HibernateProxyHelper.deproxy(description, TaxonDescription.class);
121 119
                            if (desc.getTaxon() == null ){
122 120
                                continue;
123
                            } else if ( config.isDeleteIfUsedInTaxonDescription()){
121
                            } else if ( (config.isDeleteFromDescription() && config.getDeleteFrom() instanceof Taxon  && config.getDeleteFrom().getId() == desc.getTaxon().getId())|| config.isDeleteFromEveryWhere()){
124 122
                                Taxon taxon = desc.getTaxon();
125 123
                                desc.removeElement(textData);
126 124
                                textData.removeMedia(media);
......
136 134
                            }
137 135
                        } else if (description instanceof SpecimenDescription){
138 136
                            SpecimenDescription desc = HibernateProxyHelper.deproxy(description, SpecimenDescription.class);
139
                            if (desc.getDescribedSpecimenOrObservation() == null || config.isDeleteIfUsedInSpecimenDescription()){
137
                            if (desc.getDescribedSpecimenOrObservation() == null ){
140 138
                                continue;
141
                            } else if (config.isDeleteIfUsedInSpecimenDescription()){
139
                            } else if ((config.isDeleteFromDescription() && config.getDeleteFrom() instanceof SpecimenOrObservationBase  && config.getDeleteFrom().getId() == desc.getDescribedSpecimenOrObservation().getId())  || config.isDeleteFromEveryWhere()){
142 140
                                SpecimenOrObservationBase<?> specimen = desc.getDescribedSpecimenOrObservation();
143 141
                                desc.removeElement(textData);
144 142
                                textData.removeMedia(media);
......
152 150
                                result.addException(new ReferencedObjectUndeletableException(message));
153 151
                                return result;
154 152
                            }
153
                        }else if (description instanceof TaxonNameDescription){
154
                            TaxonNameDescription desc = HibernateProxyHelper.deproxy(description, TaxonNameDescription.class);
155
                            if (desc.getTaxonName() == null ){
156
                                continue;
157
                            } else if ((config.isDeleteFromDescription() && config.getDeleteFrom() instanceof TaxonName  && config.getDeleteFrom().getId() == desc.getTaxonName().getId())   || config.isDeleteFromEveryWhere()){
158
                                TaxonName name= desc.getTaxonName();
159
                                desc.removeElement(textData);
160
                                textData.removeMedia(media);
161
                                if (desc.getElements().isEmpty()){
162
                                    name.removeDescription(desc);
163
                                }
164
                            } else {
165
                             // this should not be happen, because it is not deletable. see isDeletable
166
                                result.setAbort();
167
                                message = "The media could not be deleted because it is referenced by a specimen.";
168
                                result.addException(new ReferencedObjectUndeletableException(message));
169
                                return result;
170
                            }
155 171
                        }
156 172
                    }
173
                }else if (ref instanceof MediaRepresentation){
174
                    MediaRepresentation mediaRep = HibernateProxyHelper.deproxy(ref, MediaRepresentation.class);
175
                    media.removeRepresentation(mediaRep);
157 176
                }
158 177

  
159 178
            }
......
169 188
        Media media = this.load(mediaUuid);
170 189
        Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(media);
171 190
        MediaDeletionConfigurator mediaConfig = (MediaDeletionConfigurator)config;
191
        CdmBase deleteFrom = mediaConfig.getDeleteFrom();
172 192
        for (CdmBase ref: references){
173 193
            String message = null;
174 194
            if (ref instanceof MediaRepresentation){
......
180 200
                if (description.isImageGallery()){
181 201
                    if (description instanceof TaxonDescription){
182 202
                        TaxonDescription desc = HibernateProxyHelper.deproxy(description, TaxonDescription.class);
183
                        if (desc.getTaxon() == null || mediaConfig.isDeleteIfUsedInTaxonDescription() ){
203
                        if (desc.getTaxon() == null || (mediaConfig.isDeleteFromDescription() && (deleteFrom instanceof Taxon && ((Taxon)deleteFrom).getId() == desc.getTaxon().getId()))){
184 204
                            continue;
185 205
                        } else{
186 206
                            message = "The media can't be deleted because it is referenced by a taxon. ("+desc.getTaxon().getTitleCache()+")";
......
188 208

  
189 209
                    } else if (description instanceof SpecimenDescription){
190 210
                        SpecimenDescription desc = HibernateProxyHelper.deproxy(description, SpecimenDescription.class);
191
                        if (desc.getDescribedSpecimenOrObservation() == null || mediaConfig.isDeleteIfUsedInSpecimenDescription()){
211
                        if (desc.getDescribedSpecimenOrObservation() == null || (mediaConfig.isDeleteFromDescription() && (deleteFrom instanceof SpecimenOrObservationBase && ((SpecimenOrObservationBase)deleteFrom).getId() == desc.getDescribedSpecimenOrObservation().getId()))){
192 212
                            continue;
193 213
                        } else{
194 214
                            message = "The media can't be deleted because it is referenced by a specimen or observation. ("+desc.getDescribedSpecimenOrObservation().getTitleCache()+")";
195 215
                        }
196 216
                    } else if (description instanceof TaxonNameDescription){
197 217
                        TaxonNameDescription desc = HibernateProxyHelper.deproxy(description, TaxonNameDescription.class);
198
                        if (desc.getTaxonName() == null || mediaConfig.isDeleteIfUsedInNameDescription()){
218
                        if (desc.getTaxonName() == null || (mediaConfig.isDeleteFromDescription() && (deleteFrom instanceof TaxonName && ((TaxonName)deleteFrom).getId() == desc.getTaxonName().getId()))){
199 219
                            continue;
200 220
                        } else{
201 221
                            message = "The media can't be deleted because it is referenced by a specimen or observation. ("+desc.getDescribedSpecimenOrObservation().getTitleCache()+")";
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/config/MediaDeletionConfigurator.java
16 16
 *
17 17
 */
18 18
public class MediaDeletionConfigurator extends DeleteConfiguratorBase {
19
    private boolean deleteFromAllTaxonDescription = false;
20
    private boolean deleteFromAllSpecimenDescription = false;
21
    private boolean deleteFromAllNameDescription = false;
19

  
20
    private boolean deleteFromDescription = false;
22 21
    private boolean deleteFromEveryWhere = false;
23 22
    private boolean onlyRemoveFromGallery = true;
24 23
    private CdmBase deleteFrom;
25 24

  
26
    public boolean isDeleteIfUsedInSpecimenDescription() {
27
        return deleteFromAllSpecimenDescription;
28
    }
29
    public void setDeleteIfUsedInSpecimenDescription(boolean deleteIfUsedInSpecimenDescription) {
30
        this.deleteFromAllSpecimenDescription = deleteIfUsedInSpecimenDescription;
31
    }
32
    public boolean isDeleteIfUsedInTaxonDescription() {
33
        return deleteFromAllTaxonDescription;
34
    }
35
    public void setDeleteIfUsedInTaxonDescription(boolean deleteIfUsedInTaxonDescription) {
36
        this.deleteFromAllTaxonDescription = deleteIfUsedInTaxonDescription;
37
    }
25

  
38 26
    /**
39 27
     * @return the onlyRemoveFromGallery
40 28
     */
......
47 35
    public void setOnlyRemoveFromGallery(boolean onlyRemoveFromGallery) {
48 36
        this.onlyRemoveFromGallery = onlyRemoveFromGallery;
49 37
    }
50
    public boolean isDeleteIfUsedInNameDescription() {
51
        return deleteFromAllNameDescription;
52
    }
53
    public void setDeleteIfUsedInNameDescription(boolean deleteIfUsedInNameDescription) {
54
        this.deleteFromAllNameDescription = deleteIfUsedInNameDescription;
55
    }
38

  
56 39
    public CdmBase getDeleteFrom() {
57 40
        return deleteFrom;
58 41
    }
......
65 48
    public void setDeleteFromEveryWhere(boolean deleteFromEveryWhere) {
66 49
        this.deleteFromEveryWhere = deleteFromEveryWhere;
67 50
    }
51
    public boolean isDeleteFromDescription() {
52
        return deleteFromDescription;
53
    }
54
    public void setDeleteFromDescription(boolean deleteFromDescription) {
55
        this.deleteFromDescription = deleteFromDescription;
56
    }
68 57
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)