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.bulkeditor.input;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Collections;
|
13
|
import java.util.Comparator;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
import java.util.UUID;
|
17
|
|
18
|
import org.eclipse.jface.resource.ImageDescriptor;
|
19
|
import org.eclipse.ui.IEditorInput;
|
20
|
import org.eclipse.ui.IPersistableElement;
|
21
|
|
22
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
23
|
import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
|
24
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
25
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
26
|
import eu.etaxonomy.cdm.model.common.ICdmBase;
|
27
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
28
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
29
|
import eu.etaxonomy.cdm.strategy.merge.IMergable;
|
30
|
import eu.etaxonomy.cdm.strategy.merge.MergeException;
|
31
|
import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityCreator;
|
32
|
import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService;
|
33
|
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
|
34
|
import eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorSortProvider;
|
35
|
import eu.etaxonomy.taxeditor.bulkeditor.e4.AnnotatedTableItem;
|
36
|
import eu.etaxonomy.taxeditor.bulkeditor.input.sortprovider.CdmBaseSortProvider;
|
37
|
import eu.etaxonomy.taxeditor.bulkeditor.input.sortprovider.TitleCacheComparator;
|
38
|
import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
|
39
|
import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
|
40
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
41
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
42
|
|
43
|
/**
|
44
|
* @author p.ciardelli
|
45
|
* @created 25.06.2009
|
46
|
* @version 1.0
|
47
|
* @param <T>
|
48
|
*/
|
49
|
public abstract class AbstractBulkEditorInput<T extends ICdmBase> extends CdmEntitySessionInput implements IEditorInput ,
|
50
|
IEntityPersistenceService<T> {
|
51
|
|
52
|
private UUID entityUuid;
|
53
|
|
54
|
private List<T> model;
|
55
|
|
56
|
private IEntityCreator<T> entityCreator;
|
57
|
private final ConversationHolder conversation;
|
58
|
|
59
|
public AbstractBulkEditorInput() {
|
60
|
super(true);
|
61
|
this.conversation = CdmStore.createConversation();
|
62
|
}
|
63
|
|
64
|
static public AbstractBulkEditorInput NewInstance(BulkEditorInputType inputType) {
|
65
|
|
66
|
return BulkEditorInputType.getInput(inputType);
|
67
|
}
|
68
|
|
69
|
public static AbstractBulkEditorInput NewInstance(IdentifiableEntity entity) {
|
70
|
|
71
|
BulkEditorInputType inputType = BulkEditorInputType.getByType(entity.getClass());
|
72
|
|
73
|
AbstractBulkEditorInput editorInput = NewInstance(inputType);
|
74
|
|
75
|
editorInput.setEntityUuid(entity.getUuid());
|
76
|
|
77
|
return editorInput;
|
78
|
}
|
79
|
|
80
|
protected abstract List<T> listEntities(IIdentifiableEntityServiceConfigurator configurator);
|
81
|
|
82
|
protected abstract T loadEntity(UUID entityUuid);
|
83
|
|
84
|
private void setEntityUuid(UUID entityUuid){
|
85
|
this.entityUuid = entityUuid;
|
86
|
}
|
87
|
|
88
|
public UUID getEntityUuid() {
|
89
|
return entityUuid;
|
90
|
}
|
91
|
|
92
|
@Override
|
93
|
public boolean exists() {
|
94
|
// TODO Auto-generated method stub
|
95
|
return false;
|
96
|
}
|
97
|
|
98
|
@Override
|
99
|
public ImageDescriptor getImageDescriptor() {
|
100
|
// TODO Auto-generated method stub
|
101
|
return null;
|
102
|
}
|
103
|
|
104
|
@Override
|
105
|
public IPersistableElement getPersistable() {
|
106
|
return null;
|
107
|
}
|
108
|
|
109
|
/** {@inheritDoc} */
|
110
|
@Override
|
111
|
@SuppressWarnings("unchecked")
|
112
|
public Object getAdapter(Class adapter) {
|
113
|
return null;
|
114
|
}
|
115
|
|
116
|
public void performSearch(final BulkEditorQuery bulkEditorQuery) {
|
117
|
|
118
|
List<T> entityList = new ArrayList<T>();
|
119
|
|
120
|
if(getEntityUuid() != null){
|
121
|
|
122
|
T entity = loadEntity(getEntityUuid());
|
123
|
entityList.add(entity);
|
124
|
model = entityList;
|
125
|
}
|
126
|
else if(bulkEditorQuery != null){
|
127
|
|
128
|
IIdentifiableEntityServiceConfigurator configurator = bulkEditorQuery.getSearchConfigurator();
|
129
|
Comparator queryComparator = (bulkEditorQuery.getComparator() != null) ? bulkEditorQuery.getComparator() : new TitleCacheComparator();
|
130
|
|
131
|
entityList = listEntities(configurator);
|
132
|
|
133
|
Collections.sort(entityList, queryComparator);
|
134
|
|
135
|
|
136
|
}
|
137
|
|
138
|
model = entityList;
|
139
|
}
|
140
|
|
141
|
public boolean isMergingEnabled() {
|
142
|
return false;
|
143
|
}
|
144
|
|
145
|
public boolean isConvertingEnabled() {
|
146
|
return false;
|
147
|
}
|
148
|
|
149
|
public boolean isMarkerTypeEditingEnabled(MarkerType markerType) {
|
150
|
return false;
|
151
|
}
|
152
|
|
153
|
|
154
|
/** {@inheritDoc} */
|
155
|
@Override
|
156
|
public boolean merge(T entity, T mergeTarget) {
|
157
|
if (entity instanceof IMergable) {
|
158
|
try {
|
159
|
CdmStore.getCommonService().merge(mergeTarget.getUuid(), entity.getUuid(), (Class<? extends CdmBase>)entity.getClass());
|
160
|
} catch (MergeException e) {
|
161
|
MessagingUtils.errorDialog("Bulk Editor Merge Error",
|
162
|
this,
|
163
|
"Could not merge chosen objects of type " + entity.getClass().getName(),
|
164
|
TaxeditorBulkeditorPlugin.PLUGIN_ID,
|
165
|
e,
|
166
|
true);
|
167
|
}
|
168
|
}
|
169
|
return true;
|
170
|
}
|
171
|
|
172
|
|
173
|
/** {@inheritDoc} */
|
174
|
@Override
|
175
|
public T create(T entity) {
|
176
|
return save(entity);
|
177
|
}
|
178
|
|
179
|
public IEntityCreator<T> getEntityCreator(){
|
180
|
if(entityCreator == null){
|
181
|
entityCreator = createEntityCreator();
|
182
|
}
|
183
|
return entityCreator;
|
184
|
}
|
185
|
|
186
|
protected abstract IEntityCreator<T> createEntityCreator();
|
187
|
|
188
|
/**
|
189
|
* The default implementation returns an empty list of sort providers.
|
190
|
* @return
|
191
|
*/
|
192
|
public List<IBulkEditorSortProvider<T>> getSortProviders(){
|
193
|
List<IBulkEditorSortProvider<T>> sortProviders = new ArrayList<IBulkEditorSortProvider<T>>();
|
194
|
|
195
|
sortProviders.add(new CdmBaseSortProvider<T>());
|
196
|
|
197
|
return sortProviders;
|
198
|
}
|
199
|
|
200
|
/**
|
201
|
* Returns a textual representation given object. The default implementation
|
202
|
* in the abstract base class returns the simple name of the class, this may
|
203
|
* be overwritten to something more specific in subclasses.
|
204
|
*
|
205
|
* @param entity
|
206
|
* @return a textual representation given object.
|
207
|
*/
|
208
|
public String getTypeText(Object entity){
|
209
|
return entity.getClass().getSimpleName();
|
210
|
}
|
211
|
|
212
|
public String getText(T entity) {
|
213
|
if(entity instanceof IdentifiableEntity){
|
214
|
IdentifiableEntity identifiableEntity = (IdentifiableEntity) HibernateProxyHelper.deproxy(entity);
|
215
|
|
216
|
return identifiableEntity.getTitleCache();
|
217
|
}
|
218
|
|
219
|
return "No text. Implement in subclass";
|
220
|
}
|
221
|
|
222
|
public List<T> getModel() {
|
223
|
return model;
|
224
|
}
|
225
|
|
226
|
public List<AnnotatedTableItem<T>> getWrappedModel() {
|
227
|
List<AnnotatedTableItem<T>> wrappetItems = new ArrayList<>();
|
228
|
for(T t:model){
|
229
|
wrappetItems.add(new AnnotatedTableItem<T>(t));
|
230
|
}
|
231
|
return wrappetItems;
|
232
|
}
|
233
|
|
234
|
protected boolean replaceInModel(T entity) {
|
235
|
int index = model.indexOf(entity);
|
236
|
if(index >= 0) {
|
237
|
model.set(index, entity);
|
238
|
return true;
|
239
|
} else {
|
240
|
return false;
|
241
|
}
|
242
|
}
|
243
|
|
244
|
@Override
|
245
|
public List<T> getRootEntities() {
|
246
|
return getModel();
|
247
|
}
|
248
|
|
249
|
|
250
|
@Override
|
251
|
public Map<Object, List<String>> getPropertyPathsMap() {
|
252
|
// TODO Auto-generated method stub
|
253
|
return null;
|
254
|
}
|
255
|
|
256
|
public ConversationHolder getConversation() {
|
257
|
return conversation;
|
258
|
}
|
259
|
}
|