Project

General

Profile

Download (7.46 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.List;
16
import java.util.Map;
17
import java.util.Map.Entry;
18
import java.util.UUID;
19

    
20
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
21
import eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase;
22
import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
23
import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;
24
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
27
import eu.etaxonomy.cdm.model.common.MarkerType;
28
import eu.etaxonomy.cdm.strategy.merge.IMergable;
29
import eu.etaxonomy.cdm.strategy.merge.MergeException;
30
import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityCreator;
31
import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService;
32
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorQuery;
33
import eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorSortProvider;
34
import eu.etaxonomy.taxeditor.bulkeditor.input.sortprovider.CdmBaseSortProvider;
35
import eu.etaxonomy.taxeditor.bulkeditor.input.sortprovider.TitleCacheComparator;
36
import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
37
import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
38
import eu.etaxonomy.taxeditor.model.MessagingUtils;
39
import eu.etaxonomy.taxeditor.store.CdmStore;
40

    
41
/**
42
 * @author p.ciardelli
43
 * @created 25.06.2009
44
 * @version 1.0
45
 * @param <T>
46
 */
47
public abstract class AbstractBulkEditorInput<T extends CdmBase> extends CdmEntitySessionInput implements
48
    IEntityPersistenceService<T> {
49

    
50
	private UUID entityUuid;
51

    
52
	private List<T> model = new ArrayList<>();
53

    
54
	private Map<T, DeleteConfiguratorBase> toDelete = new HashMap<>();
55

    
56
	private List<T> mergeCandidates = new ArrayList<>();
57
	private T mergeTarget = null;
58

    
59
	private IEntityCreator<T> entityCreator;
60
	private final ConversationHolder conversation;
61

    
62
	public AbstractBulkEditorInput() {
63
	    super(true);
64
	    this.conversation = CdmStore.createConversation();
65
	}
66

    
67
	static public AbstractBulkEditorInput NewInstance(BulkEditorInputType inputType) {
68

    
69
		return BulkEditorInputType.getInput(inputType);
70
	}
71

    
72
	public static AbstractBulkEditorInput NewInstance(IdentifiableEntity entity) {
73

    
74
		BulkEditorInputType inputType = BulkEditorInputType.getByType(entity.getClass());
75

    
76
		AbstractBulkEditorInput editorInput = NewInstance(inputType);
77

    
78
		editorInput.setEntityUuid(entity.getUuid());
79

    
80
		return editorInput;
81
	}
82

    
83
    public abstract String getName();
84

    
85
	protected abstract List<T> listEntities(IIdentifiableEntityServiceConfigurator configurator);
86

    
87
	protected abstract T loadEntity(UUID entityUuid);
88

    
89
	public void setMergeTarget(T t){
90
	    mergeTarget = t;
91
	}
92

    
93
    public List<T> getMergeCandidates() {
94
        return mergeCandidates;
95
    }
96

    
97
    public T getMergeTarget() {
98
        return mergeTarget;
99
    }
100

    
101
	public void removeMergeTarget(){
102
	    mergeTarget = null;
103
	}
104

    
105
	public void addMergeCandidate(T t){
106
	    mergeCandidates.add(t);
107
	}
108

    
109
	public void removeMergeCandidate(T t){
110
	    mergeCandidates.remove(t);
111
	}
112

    
113
    public void addToDelete(T t, DeleteConfiguratorBase config) {
114
        toDelete.put(t, config);
115
    }
116

    
117
	private void setEntityUuid(UUID entityUuid){
118
		this.entityUuid = entityUuid;
119
	}
120

    
121
	public UUID getEntityUuid() {
122
		return entityUuid;
123
	}
124

    
125
	public void performSearch(final BulkEditorQuery bulkEditorQuery) {
126

    
127
		List<T> entityList = new ArrayList<T>();
128

    
129
		if(getEntityUuid() != null){
130

    
131
			T entity = loadEntity(getEntityUuid());
132
			entityList.add(entity);
133
			model = entityList;
134
		}
135
		else if(bulkEditorQuery != null){
136

    
137
			IIdentifiableEntityServiceConfigurator configurator = bulkEditorQuery.getSearchConfigurator();
138
			Comparator queryComparator = (bulkEditorQuery.getComparator() != null) ? bulkEditorQuery.getComparator() : new TitleCacheComparator();
139

    
140
			entityList = listEntities(configurator);
141

    
142
			Collections.sort(entityList, queryComparator);
143

    
144

    
145
		}
146

    
147
		model = entityList;
148
	}
149

    
150
	public boolean isMergingEnabled() {
151
		return false;
152
	}
153

    
154
	public boolean isConvertingEnabled() {
155
		return false;
156
	}
157

    
158
	public boolean isMarkerTypeEditingEnabled(MarkerType markerType) {
159
		return false;
160
	}
161

    
162

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

    
181
	public void saveModel(){
182
	    saveModel(true);
183
	}
184

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

    
205

    
206
	/** {@inheritDoc} */
207
	@Override
208
    public T create(T entity) {
209
		return save(entity);
210
	}
211

    
212
	public IEntityCreator<T> getEntityCreator(){
213
		if(entityCreator == null){
214
			entityCreator = createEntityCreator();
215
		}
216
		return entityCreator;
217
	}
218

    
219
	protected abstract IEntityCreator<T> createEntityCreator();
220

    
221
	/**
222
	 * The default implementation returns an empty list of sort providers.
223
	 * @return
224
	 */
225
	public List<IBulkEditorSortProvider<T>> getSortProviders(){
226
		List<IBulkEditorSortProvider<T>> sortProviders = new ArrayList<IBulkEditorSortProvider<T>>();
227

    
228
		sortProviders.add(new CdmBaseSortProvider<T>());
229

    
230
		return sortProviders;
231
	}
232

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

    
245
	public String getText(T entity) {
246
		if(entity instanceof IdentifiableEntity){
247
			IdentifiableEntity identifiableEntity = (IdentifiableEntity) HibernateProxyHelper.deproxy(entity);
248

    
249
			return identifiableEntity.getTitleCache();
250
		}
251

    
252
		return "No text. Implement in subclass";
253
	}
254

    
255
	public List<T> getModel() {
256
		return model;
257
	}
258

    
259
	protected boolean replaceInModel(T entity) {
260
	    int index = model.indexOf(entity);
261
	    if(index >= 0) {
262
	        model.set(index, entity);
263
	        return true;
264
	    } else {
265
	        return false;
266
	    }
267
	}
268

    
269
    @Override
270
    public   List<T> getRootEntities() {
271
        return getModel();
272
    }
273

    
274

    
275
    @Override
276
    public Map<Object, List<String>> getPropertyPathsMap() {
277
        // TODO Auto-generated method stub
278
        return null;
279
    }
280

    
281
	public ConversationHolder getConversation() {
282
		return conversation;
283
	}
284
}
(1-1/11)