Revision 1118cbec
Added by Katja Luther over 6 years ago
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/MediaServiceImpl.java | ||
---|---|---|
37 | 37 |
import eu.etaxonomy.cdm.model.media.MediaRepresentation; |
38 | 38 |
import eu.etaxonomy.cdm.model.media.Rights; |
39 | 39 |
import eu.etaxonomy.cdm.model.name.TaxonName; |
40 |
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen; |
|
40 | 41 |
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase; |
41 | 42 |
import eu.etaxonomy.cdm.model.taxon.Taxon; |
43 |
import eu.etaxonomy.cdm.model.taxon.TaxonBase; |
|
42 | 44 |
import eu.etaxonomy.cdm.persistence.dao.media.IMediaDao; |
43 | 45 |
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy; |
44 | 46 |
|
... | ... | |
55 | 57 |
@Autowired |
56 | 58 |
private IDescriptionService descriptionService; |
57 | 59 |
|
60 |
@Autowired |
|
61 |
private IOccurrenceService specimenService; |
|
62 |
@Autowired |
|
63 |
private ITaxonService taxonService; |
|
64 |
@Autowired |
|
65 |
private INameService nameService; |
|
66 |
|
|
58 | 67 |
@Override |
59 | 68 |
public Pager<MediaKey> getMediaKeys(Set<Taxon> taxonomicScope, Set<NamedArea> geoScopes, Integer pageSize, Integer pageNumber, List<String> propertyPaths) { |
60 | 69 |
Integer numberOfResults = dao.countMediaKeys(taxonomicScope, geoScopes); |
... | ... | |
102 | 111 |
Media media = this.load(mediaUuid); |
103 | 112 |
|
104 | 113 |
result = isDeletable(mediaUuid, config); |
105 |
|
|
114 |
CdmBase updatedObject = null; |
|
106 | 115 |
if (result.isOk()){ |
107 | 116 |
Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(media); |
108 | 117 |
String message = null; |
... | ... | |
120 | 129 |
continue; |
121 | 130 |
} else if ( (config.isDeleteFromDescription() && config.getDeleteFrom() instanceof Taxon && config.getDeleteFrom().getId() == desc.getTaxon().getId())|| config.isDeleteFromEveryWhere()){ |
122 | 131 |
Taxon taxon = desc.getTaxon(); |
132 |
updatedObject = taxon; |
|
123 | 133 |
desc.removeElement(textData); |
124 | 134 |
textData.removeMedia(media); |
125 | 135 |
if (desc.getElements().isEmpty()){ |
126 | 136 |
taxon.removeDescription(desc); |
127 | 137 |
} |
138 |
|
|
128 | 139 |
} else { |
129 | 140 |
// this should not be happen, because it is not deletable. see isDeletable |
130 | 141 |
result.setAbort(); |
131 |
message = "The media could not be deleted because it is referenced by a taxon."; |
|
132 |
result.addException(new ReferencedObjectUndeletableException(message)); |
|
133 |
return result; |
|
142 |
|
|
143 |
|
|
134 | 144 |
} |
135 | 145 |
} else if (description instanceof SpecimenDescription){ |
136 | 146 |
SpecimenDescription desc = HibernateProxyHelper.deproxy(description, SpecimenDescription.class); |
... | ... | |
138 | 148 |
continue; |
139 | 149 |
} else if ((config.isDeleteFromDescription() && config.getDeleteFrom() instanceof SpecimenOrObservationBase && config.getDeleteFrom().getId() == desc.getDescribedSpecimenOrObservation().getId()) || config.isDeleteFromEveryWhere()){ |
140 | 150 |
SpecimenOrObservationBase<?> specimen = desc.getDescribedSpecimenOrObservation(); |
151 |
updatedObject = specimen; |
|
141 | 152 |
desc.removeElement(textData); |
142 | 153 |
textData.removeMedia(media); |
143 | 154 |
if (desc.getElements().isEmpty()){ |
... | ... | |
146 | 157 |
} else { |
147 | 158 |
// this should not be happen, because it is not deletable. see isDeletable |
148 | 159 |
result.setAbort(); |
149 |
message = "The media could not be deleted because it is referenced by a specimen."; |
|
150 |
result.addException(new ReferencedObjectUndeletableException(message)); |
|
151 |
return result; |
|
160 |
|
|
161 |
|
|
152 | 162 |
} |
153 | 163 |
}else if (description instanceof TaxonNameDescription){ |
154 | 164 |
TaxonNameDescription desc = HibernateProxyHelper.deproxy(description, TaxonNameDescription.class); |
165 |
|
|
155 | 166 |
if (desc.getTaxonName() == null ){ |
156 | 167 |
continue; |
157 | 168 |
} else if ((config.isDeleteFromDescription() && config.getDeleteFrom() instanceof TaxonName && config.getDeleteFrom().getId() == desc.getTaxonName().getId()) || config.isDeleteFromEveryWhere()){ |
158 | 169 |
TaxonName name= desc.getTaxonName(); |
170 |
updatedObject = name; |
|
159 | 171 |
desc.removeElement(textData); |
160 | 172 |
textData.removeMedia(media); |
161 | 173 |
if (desc.getElements().isEmpty()){ |
... | ... | |
164 | 176 |
} else { |
165 | 177 |
// this should not be happen, because it is not deletable. see isDeletable |
166 | 178 |
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; |
|
179 |
|
|
180 |
|
|
170 | 181 |
} |
171 | 182 |
} |
172 | 183 |
} |
173 | 184 |
}else if (ref instanceof MediaRepresentation){ |
174 | 185 |
MediaRepresentation mediaRep = HibernateProxyHelper.deproxy(ref, MediaRepresentation.class); |
175 | 186 |
media.removeRepresentation(mediaRep); |
187 |
} else if (ref instanceof MediaSpecimen){ |
|
188 |
MediaSpecimen mediaSpecimen = HibernateProxyHelper.deproxy(ref, MediaSpecimen.class); |
|
189 |
if (config.getDeleteFrom().getId() == mediaSpecimen.getId() || config.isDeleteFromEveryWhere()){ |
|
190 |
mediaSpecimen.setMediaSpecimen(null); |
|
191 |
updatedObject = mediaSpecimen; |
|
192 |
} |
|
176 | 193 |
} |
177 | 194 |
|
178 | 195 |
} |
179 |
dao.delete(media); |
|
196 |
if (result.isOk()){ |
|
197 |
dao.delete(media); |
|
198 |
} else{ |
|
199 |
if (updatedObject instanceof |
|
200 |
TaxonBase){ |
|
201 |
taxonService.update((TaxonBase)updatedObject); |
|
202 |
result.addUpdatedObject(updatedObject); |
|
203 |
} |
|
204 |
if (updatedObject instanceof |
|
205 |
TaxonName){ |
|
206 |
nameService.update((TaxonName)updatedObject); |
|
207 |
} |
|
208 |
if (updatedObject instanceof |
|
209 |
SpecimenOrObservationBase){ |
|
210 |
specimenService.update((SpecimenOrObservationBase)updatedObject); |
|
211 |
} |
|
212 |
} |
|
180 | 213 |
} |
181 | 214 |
return result; |
182 | 215 |
} |
... | ... | |
189 | 222 |
Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(media); |
190 | 223 |
MediaDeletionConfigurator mediaConfig = (MediaDeletionConfigurator)config; |
191 | 224 |
CdmBase deleteFrom = mediaConfig.getDeleteFrom(); |
225 |
|
|
226 |
if (mediaConfig.isDeleteFromEveryWhere()){ |
|
227 |
return result; |
|
228 |
} |
|
192 | 229 |
for (CdmBase ref: references){ |
193 | 230 |
String message = null; |
194 | 231 |
if (ref instanceof MediaRepresentation){ |
... | ... | |
203 | 240 |
if (desc.getTaxon() == null || (mediaConfig.isDeleteFromDescription() && (deleteFrom instanceof Taxon && ((Taxon)deleteFrom).getId() == desc.getTaxon().getId()))){ |
204 | 241 |
continue; |
205 | 242 |
} else{ |
206 |
message = "The media can't be deleted because it is referenced by a taxon. ("+desc.getTaxon().getTitleCache()+")";
|
|
243 |
message = "The media can't be deleted from the database because it is referenced by another taxon. ("+desc.getTaxon().getTitleCache()+")";
|
|
207 | 244 |
} |
208 | 245 |
|
209 | 246 |
} else if (description instanceof SpecimenDescription){ |
... | ... | |
211 | 248 |
if (desc.getDescribedSpecimenOrObservation() == null || (mediaConfig.isDeleteFromDescription() && (deleteFrom instanceof SpecimenOrObservationBase && ((SpecimenOrObservationBase)deleteFrom).getId() == desc.getDescribedSpecimenOrObservation().getId()))){ |
212 | 249 |
continue; |
213 | 250 |
} else{ |
214 |
message = "The media can't be deleted because it is referenced by a specimen or observation. ("+desc.getDescribedSpecimenOrObservation().getTitleCache()+")";
|
|
251 |
message = "The media can't be deleted from the database because it is referenced by another specimen or observation. ("+desc.getDescribedSpecimenOrObservation().getTitleCache()+")";
|
|
215 | 252 |
} |
216 | 253 |
} else if (description instanceof TaxonNameDescription){ |
217 | 254 |
TaxonNameDescription desc = HibernateProxyHelper.deproxy(description, TaxonNameDescription.class); |
218 | 255 |
if (desc.getTaxonName() == null || (mediaConfig.isDeleteFromDescription() && (deleteFrom instanceof TaxonName && ((TaxonName)deleteFrom).getId() == desc.getTaxonName().getId()))){ |
219 | 256 |
continue; |
220 | 257 |
} else{ |
221 |
message = "The media can't be deleted because it is referenced by a specimen or observation. ("+desc.getDescribedSpecimenOrObservation().getTitleCache()+")";
|
|
258 |
message = "The media can't be deleted from the database because it is referenced by another specimen or observation. ("+desc.getDescribedSpecimenOrObservation().getTitleCache()+")";
|
|
222 | 259 |
} |
223 | 260 |
} |
224 | 261 |
} |
225 | 262 |
} else { |
226 |
message = "The media can't be deleted because it is referenced by a " + ref.getUserFriendlyTypeName() ;
|
|
263 |
message = "The media can't be completely deleted because it is referenced by another " + ref.getUserFriendlyTypeName() ;
|
|
227 | 264 |
} |
228 | 265 |
if (message != null){ |
229 | 266 |
result.addException(new ReferencedObjectUndeletableException(message)); |
230 | 267 |
result.addRelatedObject(ref); |
231 |
result.setAbort(); |
|
268 |
|
|
232 | 269 |
} |
233 | 270 |
|
234 | 271 |
} |
Also available in: Unified diff
improve delete of shared media