Project

General

Profile

Download (7.81 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
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.HashMap;
15
import java.util.HashSet;
16
import java.util.List;
17
import java.util.Map;
18
import java.util.Map.Entry;
19
import java.util.Set;
20
import java.util.UUID;
21

    
22
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
23
import eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase;
24
import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
25
import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;
26
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
29
import eu.etaxonomy.cdm.model.common.MarkerType;
30
import eu.etaxonomy.cdm.strategy.merge.IMergable;
31
import eu.etaxonomy.cdm.strategy.merge.MergeException;
32
import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityCreator;
33
import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService;
34
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
35
import eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorSortProvider;
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 CdmBase> extends CdmEntitySessionInput implements
50
    IEntityPersistenceService<T> {
51

    
52
	private UUID entityUuid;
53

    
54
	private List<T> model = new ArrayList<>();
55

    
56
	private Map<T, DeleteConfiguratorBase> toDelete = new HashMap<>();
57
	private List<T> saveCandidates = new ArrayList<>();
58

    
59

    
60
	private Set<T> mergeCandidates = new HashSet<>();
61
	private T mergeTarget = null;
62

    
63
	private IEntityCreator<T> entityCreator;
64
	private final ConversationHolder conversation;
65

    
66
	public AbstractBulkEditorInput() {
67
	    super(true);
68
	    this.conversation = CdmStore.createConversation();
69
	}
70

    
71
	static public AbstractBulkEditorInput NewInstance(BulkEditorInputType inputType) {
72

    
73
		return BulkEditorInputType.getInput(inputType);
74
	}
75

    
76
	public static AbstractBulkEditorInput NewInstance(IdentifiableEntity entity) {
77

    
78
		BulkEditorInputType inputType = BulkEditorInputType.getByType(entity.getClass());
79

    
80
		AbstractBulkEditorInput editorInput = NewInstance(inputType);
81

    
82
		editorInput.setEntityUuid(entity.getUuid());
83

    
84
		return editorInput;
85
	}
86

    
87
    public abstract String getName();
88

    
89
	protected abstract List<T> listEntities(IIdentifiableEntityServiceConfigurator configurator);
90

    
91
	protected abstract T loadEntity(UUID entityUuid);
92

    
93
	public void setMergeTarget(T t){
94
	    mergeTarget = t;
95
	}
96

    
97
    public Set<T> getMergeCandidates() {
98
        return mergeCandidates;
99
    }
100

    
101
    public T getMergeTarget() {
102
        return mergeTarget;
103
    }
104

    
105
	public void removeMergeTarget(){
106
	    mergeTarget = null;
107
	}
108

    
109
	public void addMergeCandidate(T t){
110
	    mergeCandidates.add(t);
111
	}
112

    
113
	public void removeMergeCandidate(T t){
114
	    mergeCandidates.remove(t);
115
	}
116

    
117
    public void addToDelete(T t, DeleteConfiguratorBase config) {
118
        toDelete.put(t, config);
119
    }
120
    public void addSaveCandidate(T t){
121
        saveCandidates.add(t);
122
    }
123
	private void setEntityUuid(UUID entityUuid){
124
		this.entityUuid = entityUuid;
125
	}
126

    
127
	public UUID getEntityUuid() {
128
		return entityUuid;
129
	}
130

    
131
	public void performSearch(final BulkEditorQuery bulkEditorQuery) {
132

    
133
		List<T> entityList = new ArrayList<T>();
134

    
135
		if(getEntityUuid() != null){
136

    
137
			T entity = loadEntity(getEntityUuid());
138
			entityList.add(entity);
139
			model = entityList;
140
		}
141
		else if(bulkEditorQuery != null){
142

    
143
			IIdentifiableEntityServiceConfigurator configurator = bulkEditorQuery.getSearchConfigurator();
144
			Comparator queryComparator = (bulkEditorQuery.getComparator() != null) ? bulkEditorQuery.getComparator() : new TitleCacheComparator();
145

    
146
			entityList = listEntities(configurator);
147

    
148
			Collections.sort(entityList, queryComparator);
149

    
150

    
151
		}
152

    
153
		model = entityList;
154
	}
155

    
156
	public boolean isMergingEnabled() {
157
		return false;
158
	}
159

    
160
	public boolean isConvertingEnabled() {
161
		return false;
162
	}
163

    
164
	public boolean isMarkerTypeEditingEnabled(MarkerType markerType) {
165
		return false;
166
	}
167

    
168

    
169
	/** {@inheritDoc} */
170
	@Override
171
    public boolean merge(T entity, T mergeTarget) {
172
		if (entity instanceof IMergable) {
173
			try {
174
				CdmStore.getCommonService().merge(mergeTarget.getUuid(), entity.getUuid(), (Class<? extends CdmBase>)entity.getClass());
175
			} catch (MergeException e) {
176
				MessagingUtils.errorDialog("Bulk Editor Merge Error",
177
						this,
178
						"Could not merge chosen objects of type " + entity.getClass().getName(),
179
						TaxeditorBulkeditorPlugin.PLUGIN_ID,
180
						e,
181
						true);
182
			}
183
		}
184
		return true;
185
	}
186

    
187
	public void saveModel(){
188
	    saveModel(true);
189
	}
190

    
191
	public void saveModel(boolean resetMerge){
192
	    //delete entities
193
	    for(Entry<T, DeleteConfiguratorBase> entry:toDelete.entrySet()){
194
	        try {
195
                delete(entry.getKey(), entry.getValue());
196
            } catch (ReferencedObjectUndeletableException e) {
197
                e.printStackTrace();
198
            }
199
	    }
200
	    toDelete.clear();
201
	    if(resetMerge){
202
	        //merge entities
203
	        for(T mergeCandidate:mergeCandidates){
204
	            merge(mergeCandidate, mergeTarget);
205
	        }
206
	        mergeCandidates.clear();
207
	        mergeTarget = null;
208
	    }
209
	}
210

    
211

    
212
	/** {@inheritDoc} */
213
	@Override
214
    public T create(T entity) {
215
		return save(entity);
216
	}
217

    
218
	public IEntityCreator<T> getEntityCreator(){
219
		if(entityCreator == null){
220
			entityCreator = createEntityCreator();
221
		}
222
		return entityCreator;
223
	}
224

    
225
	protected abstract IEntityCreator<T> createEntityCreator();
226

    
227
	/**
228
	 * The default implementation returns an empty list of sort providers.
229
	 * @return
230
	 */
231
	public List<IBulkEditorSortProvider<T>> getSortProviders(){
232
		List<IBulkEditorSortProvider<T>> sortProviders = new ArrayList<IBulkEditorSortProvider<T>>();
233

    
234
		sortProviders.add(new CdmBaseSortProvider<T>());
235

    
236
		return sortProviders;
237
	}
238

    
239
	/**
240
	 * Returns a textual representation given object. The default implementation
241
	 * in the abstract base class returns the simple name of the class, this may
242
	 * be overwritten to something more specific in subclasses.
243
	 *
244
	 * @param entity
245
	 * @return a textual representation given object.
246
	 */
247
	public String getTypeText(Object entity){
248
		return entity.getClass().getSimpleName();
249
	}
250

    
251
	public String getText(T entity) {
252
		if(entity instanceof IdentifiableEntity){
253
			IdentifiableEntity identifiableEntity = (IdentifiableEntity) HibernateProxyHelper.deproxy(entity);
254

    
255
			return identifiableEntity.getTitleCache();
256
		}
257

    
258
		return "No text. Implement in subclass";
259
	}
260

    
261
	public List<T> getModel() {
262
		return model;
263
	}
264

    
265
	protected boolean replaceInModel(T entity) {
266
	    int index = model.indexOf(entity);
267
	    if(index >= 0) {
268
	        model.set(index, entity);
269
	        return true;
270
	    } else {
271
	        return false;
272
	    }
273
	}
274

    
275
    @Override
276
    public   List<T> getRootEntities() {
277
        return getModel();
278
    }
279

    
280

    
281
    @Override
282
    public Map<Object, List<String>> getPropertyPathsMap() {
283
        // TODO Auto-generated method stub
284
        return null;
285
    }
286

    
287
	public ConversationHolder getConversation() {
288
		return conversation;
289
	}
290

    
291
	public List<T> getSaveCandidates() {
292
        return saveCandidates;
293
    }
294

    
295
    /**
296
     *
297
     */
298
    public void resetSaveCandidates() {
299
       this.saveCandidates.clear();
300

    
301
    }
302
}
(1-1/11)