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
|
package eu.etaxonomy.taxeditor.editor.view.media.operation;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.HashSet;
|
13
|
import java.util.List;
|
14
|
import java.util.Set;
|
15
|
import java.util.UUID;
|
16
|
|
17
|
import org.eclipse.core.commands.ExecutionException;
|
18
|
import org.eclipse.core.commands.operations.IUndoContext;
|
19
|
import org.eclipse.core.runtime.IAdaptable;
|
20
|
import org.eclipse.core.runtime.IProgressMonitor;
|
21
|
import org.eclipse.core.runtime.IStatus;
|
22
|
|
23
|
import eu.etaxonomy.cdm.api.service.DeleteResult;
|
24
|
import eu.etaxonomy.cdm.api.service.IMediaService;
|
25
|
import eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator;
|
26
|
import eu.etaxonomy.cdm.api.util.ImagesUtility;
|
27
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
28
|
import eu.etaxonomy.cdm.model.media.Media;
|
29
|
import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
|
30
|
import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
|
31
|
import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
|
32
|
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
|
33
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
34
|
|
35
|
/**
|
36
|
* <p>DeleteMediaOperation class.</p>
|
37
|
*
|
38
|
* @author p.ciardelli
|
39
|
* @created 31.03.2009
|
40
|
*/
|
41
|
public class DeleteMediaOperation extends AbstractPostTaxonOperation {
|
42
|
|
43
|
private final List<Media> media;
|
44
|
|
45
|
private final DescriptionBase<?> description;
|
46
|
|
47
|
private final MediaDeletionConfigurator config;
|
48
|
|
49
|
/**
|
50
|
* <p>Constructor for DeleteMediaOperation.</p>
|
51
|
*
|
52
|
* @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
|
53
|
* @param label a {@link java.lang.String} object.
|
54
|
* @param description a {@link eu.etaxonomy.cdm.model.description.DescriptionBase} object.
|
55
|
* @param media a {@link eu.etaxonomy.cdm.model.media.Media} object.
|
56
|
* @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
|
57
|
*/
|
58
|
public DeleteMediaOperation(String label, IUndoContext undoContext,
|
59
|
DescriptionBase<?> description, List<Media> media, MediaDeletionConfigurator config, IPostOperationEnabled postOperationEnabled) {
|
60
|
super(label, undoContext, postOperationEnabled);
|
61
|
this.config = config;
|
62
|
this.media = media;
|
63
|
this.description = description;
|
64
|
}
|
65
|
|
66
|
public DeleteMediaOperation(String label, IUndoContext undoContext,
|
67
|
DescriptionBase<?> description, Media media, MediaDeletionConfigurator config, IPostOperationEnabled postOperationEnabled) {
|
68
|
super(label, undoContext, postOperationEnabled);
|
69
|
this.config = config;
|
70
|
this.media = new ArrayList<>();
|
71
|
this.media.add(media);
|
72
|
this.description = description;
|
73
|
}
|
74
|
|
75
|
|
76
|
@Override
|
77
|
public IStatus execute(IProgressMonitor monitor, IAdaptable info)
|
78
|
throws ExecutionException {
|
79
|
|
80
|
|
81
|
if (monitor != null) {
|
82
|
monitor.worked(20);
|
83
|
}
|
84
|
if (!config.isOnlyRemoveFromGallery() ){
|
85
|
Set<UUID> toDelete= new HashSet<>();
|
86
|
for (Media mediaElement: media){
|
87
|
if (mediaElement.getId() != 0){
|
88
|
toDelete.add(mediaElement.getUuid());
|
89
|
}
|
90
|
}
|
91
|
|
92
|
DeleteResult result = CdmStore.getService(IMediaService.class).delete(toDelete, config);
|
93
|
String errorMessage = "The media ";
|
94
|
|
95
|
if (!result.isOk()){
|
96
|
List<String> messages = new ArrayList<String>();
|
97
|
|
98
|
for (Exception e:result.getExceptions()){
|
99
|
messages.add(e.getMessage());
|
100
|
}
|
101
|
errorMessage += "could not be deleted from database. ";
|
102
|
//MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
|
103
|
DeleteResultMessagingUtils.messageDialogWithDetails(result,errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
|
104
|
|
105
|
}
|
106
|
}
|
107
|
if (monitor != null){
|
108
|
monitor.worked(40);
|
109
|
}
|
110
|
|
111
|
return postExecute(null);
|
112
|
}
|
113
|
|
114
|
@Override
|
115
|
public IStatus redo(IProgressMonitor monitor, IAdaptable info)
|
116
|
throws ExecutionException {
|
117
|
return execute(monitor, info);
|
118
|
}
|
119
|
|
120
|
@Override
|
121
|
public IStatus undo(IProgressMonitor monitor, IAdaptable info)
|
122
|
throws ExecutionException {
|
123
|
for (Media mediaElement: media){
|
124
|
ImagesUtility.addMediaToGallery(description, mediaElement);
|
125
|
}
|
126
|
|
127
|
return postExecute(null);
|
128
|
}
|
129
|
}
|