Project

General

Profile

Download (8.46 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.taxeditor.bulkeditor.input;
11

    
12
import java.util.ArrayList;
13
import java.util.Arrays;
14
import java.util.Collections;
15
import java.util.Comparator;
16
import java.util.List;
17
import java.util.Map;
18
import java.util.UUID;
19

    
20
import org.eclipse.jface.resource.ImageDescriptor;
21
import org.eclipse.ui.IEditorInput;
22
import org.eclipse.ui.IPersistableElement;
23

    
24
import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
25
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
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.input.sortprovider.CdmBaseSortProvider;
36
import eu.etaxonomy.taxeditor.bulkeditor.input.sortprovider.TitleCacheComparator;
37
import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
38
import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
39
import eu.etaxonomy.taxeditor.model.MessagingUtils;
40
import eu.etaxonomy.taxeditor.store.CdmStore;
41

    
42
/**
43
 * <p>Abstract AbstractBulkEditorInput class.</p>
44
 *
45
 * @author p.ciardelli
46
 * @created 25.06.2009
47
 * @version 1.0
48
 * @param <T>
49
 */
50
public abstract class AbstractBulkEditorInput<T extends ICdmBase> extends CdmEntitySessionInput implements IEditorInput ,
51
    IEntityPersistenceService<T> {
52

    
53
	private UUID entityUuid;
54

    
55
	private List<T> model;
56

    
57
	private IEntityCreator<T> entityCreator;
58

    
59
	private static Class serviceClass;
60

    
61
	public AbstractBulkEditorInput() {
62

    
63
	}
64

    
65
	/**
66
	 * <p>NewInstance</p>
67
	 *
68
	 * @param inputType a {@link eu.etaxonomy.taxeditor.bulkeditor.input.BulkEditorInputTypeValues.BulkEditorInputType} object.
69
	 * @return a {@link eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput} object.
70
	 */
71
	static public AbstractBulkEditorInput NewInstance(BulkEditorInputType inputType) {
72

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

    
76
	/**
77
	 * <p>NewInstance</p>
78
	 *
79
	 * @param entity a {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity} object.
80
	 * @return a {@link eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput} object.
81
	 */
82
	public static AbstractBulkEditorInput NewInstance(IdentifiableEntity entity) {
83

    
84

    
85
		BulkEditorInputType inputType = BulkEditorInputType.getByType(entity.getClass());
86

    
87
		AbstractBulkEditorInput editorInput = NewInstance(inputType);
88

    
89
		editorInput.setEntityUuid(entity.getUuid());
90

    
91
		return editorInput;
92
	}
93

    
94
	/**
95
	 * <p>listEntities</p>
96
	 *
97
	 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
98
	 * @return a {@link java.util.List} object.
99
	 */
100
	protected abstract List<T> listEntities(IIdentifiableEntityServiceConfigurator configurator);
101

    
102
	/**
103
	 * <p>loadEntity</p>
104
	 *
105
	 * @param entityUuid a {@link java.util.UUID} object.
106
	 * @return a T object.
107
	 */
108
	protected T loadEntity(UUID entityUuid) {
109
		List<String> propertyPaths = Arrays.asList(new String[]{});
110
		return (T) CdmStore.getService(serviceClass).load(entityUuid, propertyPaths);
111
	}
112

    
113
	private void setEntityUuid(UUID entityUuid){
114
		this.entityUuid = entityUuid;
115
	}
116

    
117
	/**
118
	 * <p>Getter for the field <code>entityUuid</code>.</p>
119
	 *
120
	 * @return a {@link java.util.UUID} object.
121
	 */
122
	public UUID getEntityUuid() {
123
		return entityUuid;
124
	}
125

    
126

    
127
	/* (non-Javadoc)
128
	 * @see org.eclipse.ui.IEditorInput#exists()
129
	 */
130
	/**
131
	 * <p>exists</p>
132
	 *
133
	 * @return a boolean.
134
	 */
135
	@Override
136
    public boolean exists() {
137
		// TODO Auto-generated method stub
138
		return false;
139
	}
140

    
141
	/* (non-Javadoc)
142
	 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
143
	 */
144
	/**
145
	 * <p>getImageDescriptor</p>
146
	 *
147
	 * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
148
	 */
149
	@Override
150
    public ImageDescriptor getImageDescriptor() {
151
		// TODO Auto-generated method stub
152
		return null;
153
	}
154

    
155
	/* (non-Javadoc)
156
	 * @see org.eclipse.ui.IEditorInput#getPersistable()
157
	 */
158
	/**
159
	 * <p>getPersistable</p>
160
	 *
161
	 * @return a {@link org.eclipse.ui.IPersistableElement} object.
162
	 */
163
	@Override
164
    public IPersistableElement getPersistable() {
165
		return null;
166
	}
167

    
168
	/* (non-Javadoc)
169
	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
170
	 */
171
	/** {@inheritDoc} */
172
	@Override
173
    @SuppressWarnings("unchecked")
174
	public Object getAdapter(Class adapter) {
175
		return null;
176
	}
177

    
178
	/**
179
	 * <p>Setter for the field <code>query</code>.</p>
180
	 *
181
	 * @param bulkEditorQuery a {@link eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorQuery} object.
182
	 */
183
	public void performSearch(final BulkEditorQuery bulkEditorQuery) {
184

    
185
		List<T> entityList = new ArrayList<T>();
186

    
187
		if(getEntityUuid() != null){
188

    
189
			T entity = loadEntity(getEntityUuid());
190
			entityList.add(entity);
191
			model = entityList;
192
		}
193
		else if(bulkEditorQuery != null){
194

    
195
			IIdentifiableEntityServiceConfigurator configurator = bulkEditorQuery.getSearchConfigurator();
196
			Comparator queryComparator = (bulkEditorQuery.getComparator() != null) ? bulkEditorQuery.getComparator() : new TitleCacheComparator();
197

    
198
			entityList = listEntities(configurator);
199

    
200
			Collections.sort(entityList, queryComparator);
201

    
202

    
203
		}
204

    
205
		model = entityList;
206
	}
207

    
208
	/**
209
	 * <p>isMergingEnabled</p>
210
	 *
211
	 * @return a boolean.
212
	 */
213
	public boolean isMergingEnabled() {
214
		return false;
215
	}
216

    
217
	/**
218
	 * <p>isMergingEnabled</p>
219
	 *
220
	 * @return a boolean.
221
	 */
222
	public boolean isConvertingEnabled() {
223
		return false;
224
	}
225
	/**
226
	 * <p>isMarkerTypeEditingEnabled</p>
227
	 *
228
	 * @param markerType a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
229
	 * @return a boolean.
230
	 */
231
	public boolean isMarkerTypeEditingEnabled(MarkerType markerType) {
232
		return false;
233
	}
234

    
235

    
236
	/** {@inheritDoc} */
237
	@Override
238
    public boolean merge(T entity, T mergeTarget) {
239
		if (entity instanceof IMergable) {
240
			try {
241
				CdmStore.getCommonService().merge((IMergable)mergeTarget, (IMergable)entity);
242
			} catch (MergeException e) {
243
				MessagingUtils.errorDialog("Bulk Editor Merge Error",
244
						this,
245
						"Could not merge chosen objects of type " + entity.getClass().getName(),
246
						TaxeditorBulkeditorPlugin.PLUGIN_ID,
247
						e,
248
						true);
249
			}
250
		}
251
		return true;
252
	}
253

    
254

    
255
	/** {@inheritDoc} */
256
	@Override
257
    public boolean create(T entity) {
258
		return save(entity);
259
	}
260

    
261
	public IEntityCreator<T> getEntityCreator(){
262
		if(entityCreator == null){
263
			entityCreator = createEntityCreator();
264
		}
265
		return entityCreator;
266
	}
267

    
268
	/**
269
	 * @return
270
	 */
271
	protected abstract IEntityCreator<T> createEntityCreator();
272

    
273
	/**
274
	 * The default implementation returns an empty list of sort providers.
275
	 * @return
276
	 */
277
	public List<IBulkEditorSortProvider<T>> getSortProviders(){
278
		List<IBulkEditorSortProvider<T>> sortProviders = new ArrayList<IBulkEditorSortProvider<T>>();
279

    
280
		sortProviders.add(new CdmBaseSortProvider<T>());
281

    
282
		return sortProviders;
283
	}
284

    
285
	/**
286
	 * Returns a textual representation given object. The default implementation
287
	 * in the abstract base class returns the simple name of the class, this may
288
	 * be overwritten to something more specific in subclasses.
289
	 *
290
	 * @param entity
291
	 * @return a textual representation given object.
292
	 */
293
	public String getTypeText(Object entity){
294
		return entity.getClass().getSimpleName();
295
	}
296

    
297
	/**
298
	 * @param entity
299
	 * @return
300
	 */
301
	public String getText(T entity) {
302
		if(entity instanceof IdentifiableEntity){
303
			IdentifiableEntity identifiableEntity = (IdentifiableEntity) HibernateProxyHelper.deproxy(entity);
304

    
305
			return identifiableEntity.getTitleCache();
306
		}
307

    
308
		return "No text. Implement in subclass";
309
	}
310

    
311
	/**
312
	 * @return
313
	 */
314
	public List<T> getModel() {
315
		return model;
316
	}
317

    
318

    
319

    
320
    /* (non-Javadoc)
321
     * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
322
     */
323
    @Override
324
    public   List<T> getRootEntities() {
325
        return getModel();
326
    }
327

    
328

    
329
    /* (non-Javadoc)
330
     * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getPropertyPathsMap()
331
     */
332
    @Override
333
    public Map<Object, List<String>> getPropertyPathsMap() {
334
        // TODO Auto-generated method stub
335
        return null;
336
    }
337
}
(1-1/11)