Project

General

Profile

Download (8.11 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.UUID;
18

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

    
23
import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
24
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
25
import eu.etaxonomy.cdm.model.common.ICdmBase;
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.model.MessagingUtils;
38
import eu.etaxonomy.taxeditor.store.CdmStore;
39

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

    
50
	private UUID entityUuid;
51

    
52
	private List<T> model;
53

    
54
	private IEntityCreator<T> entityCreator;
55
	
56
	private static Class serviceClass;
57

    
58
	/**
59
	 * <p>NewInstance</p>
60
	 *
61
	 * @param inputType a {@link eu.etaxonomy.taxeditor.bulkeditor.input.BulkEditorInputTypeValues.BulkEditorInputType} object.
62
	 * @return a {@link eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput} object.
63
	 */
64
	static public AbstractBulkEditorInput NewInstance(BulkEditorInputType inputType) {
65
		
66
		return BulkEditorInputType.getInput(inputType);
67
	}
68

    
69
	/**
70
	 * <p>NewInstance</p>
71
	 *
72
	 * @param entity a {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity} object.
73
	 * @return a {@link eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput} object.
74
	 */
75
	public static AbstractBulkEditorInput NewInstance(IdentifiableEntity entity) {
76

    
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
	/**
88
	 * <p>listEntities</p>
89
	 *
90
	 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
91
	 * @return a {@link java.util.List} object.
92
	 */
93
	protected abstract List<T> listEntities(IIdentifiableEntityServiceConfigurator configurator);
94

    
95
	/**
96
	 * <p>loadEntity</p>
97
	 *
98
	 * @param entityUuid a {@link java.util.UUID} object.
99
	 * @return a T object.
100
	 */
101
	protected T loadEntity(UUID entityUuid) {
102
		List<String> propertyPaths = Arrays.asList(new String[]{}); 
103
		return (T) CdmStore.getService(serviceClass).load(entityUuid, propertyPaths);
104
	}
105

    
106
	private void setEntityUuid(UUID entityUuid){
107
		this.entityUuid = entityUuid;
108
	}
109

    
110
	/**
111
	 * <p>Getter for the field <code>entityUuid</code>.</p>
112
	 *
113
	 * @return a {@link java.util.UUID} object.
114
	 */
115
	public UUID getEntityUuid() {
116
		return entityUuid;
117
	}
118

    
119

    
120
	/* (non-Javadoc)
121
	 * @see org.eclipse.ui.IEditorInput#exists()
122
	 */
123
	/**
124
	 * <p>exists</p>
125
	 *
126
	 * @return a boolean.
127
	 */
128
	@Override
129
    public boolean exists() {
130
		// TODO Auto-generated method stub
131
		return false;
132
	}
133

    
134
	/* (non-Javadoc)
135
	 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
136
	 */
137
	/**
138
	 * <p>getImageDescriptor</p>
139
	 *
140
	 * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
141
	 */
142
	@Override
143
    public ImageDescriptor getImageDescriptor() {
144
		// TODO Auto-generated method stub
145
		return null;
146
	}
147

    
148
	/* (non-Javadoc)
149
	 * @see org.eclipse.ui.IEditorInput#getPersistable()
150
	 */
151
	/**
152
	 * <p>getPersistable</p>
153
	 *
154
	 * @return a {@link org.eclipse.ui.IPersistableElement} object.
155
	 */
156
	@Override
157
    public IPersistableElement getPersistable() {
158
		return null;
159
	}
160

    
161
	/* (non-Javadoc)
162
	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
163
	 */
164
	/** {@inheritDoc} */
165
	@Override
166
    @SuppressWarnings("unchecked")
167
	public Object getAdapter(Class adapter) {
168
		return null;
169
	}
170

    
171
	/**
172
	 * <p>Setter for the field <code>query</code>.</p>
173
	 *
174
	 * @param bulkEditorQuery a {@link eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorQuery} object.
175
	 */
176
	public void performSearch(final BulkEditorQuery bulkEditorQuery) {
177

    
178
		List<T> entityList = new ArrayList<T>();
179

    
180
		if(getEntityUuid() != null){
181

    
182
			T entity = (T) loadEntity(getEntityUuid());
183
			entityList.add(entity);
184
			model = entityList;
185
		}
186
		else if(bulkEditorQuery != null){
187

    
188
			IIdentifiableEntityServiceConfigurator configurator = bulkEditorQuery.getSearchConfigurator();
189
			Comparator queryComparator = (bulkEditorQuery.getComparator() != null) ? bulkEditorQuery.getComparator() : new TitleCacheComparator();
190

    
191
			entityList = listEntities(configurator);
192

    
193
			Collections.sort(entityList, queryComparator);
194
			
195

    
196
		}
197

    
198
		model = entityList;
199
	}
200

    
201
	/**
202
	 * <p>isMergingEnabled</p>
203
	 *
204
	 * @return a boolean.
205
	 */
206
	public boolean isMergingEnabled() {
207
		return false;
208
	}
209

    
210
	/**
211
	 * <p>isMarkerTypeEditingEnabled</p>
212
	 *
213
	 * @param markerType a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
214
	 * @return a boolean.
215
	 */
216
	public boolean isMarkerTypeEditingEnabled(MarkerType markerType) {
217
		return false;
218
	}
219

    
220
	/*
221
	 * (non-Javadoc)
222
	 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService#merge(eu.etaxonomy.cdm.model.common.IdentifiableEntity, eu.etaxonomy.cdm.model.common.IdentifiableEntity)
223
	 */
224
	/** {@inheritDoc} */
225
	@Override
226
    public boolean merge(T entity, T mergeTarget) {
227
		if (entity instanceof IMergable) {
228
			try {
229
				CdmStore.getCommonService().merge((IMergable)mergeTarget, (IMergable)entity, null);				
230
			} catch (MergeException e) {
231
				MessagingUtils.errorDialog("Bulk Editor Merge Error",
232
						this,
233
						"Could not merge chosen objects of type " + entity.getClass().getName(),
234
						TaxeditorBulkeditorPlugin.PLUGIN_ID,
235
						e,
236
						true);
237
			}
238
		}
239
		return true;
240
	}
241

    
242
	/*
243
	 * (non-Javadoc)
244
	 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService#create(eu.etaxonomy.cdm.model.common.IdentifiableEntity)
245
	 */
246
	/** {@inheritDoc} */
247
	@Override
248
    public boolean create(T entity) {
249
		return save(entity);
250
	}
251

    
252
	public IEntityCreator<T> getEntityCreator(){
253
		if(entityCreator == null){
254
			entityCreator = createEntityCreator();
255
		}
256
		return entityCreator;
257
	}
258

    
259
	/**
260
	 * @return
261
	 */
262
	protected abstract IEntityCreator<T> createEntityCreator();
263

    
264
	/**
265
	 * The default implementation returns an empty list of sort providers.
266
	 * @return
267
	 */
268
	public List<IBulkEditorSortProvider<T>> getSortProviders(){
269
		List<IBulkEditorSortProvider<T>> sortProviders = new ArrayList<IBulkEditorSortProvider<T>>();
270

    
271
		sortProviders.add(new CdmBaseSortProvider<T>());
272

    
273
		return sortProviders;
274
	}
275

    
276
	/**
277
	 * Returns a textual representation given object. The default implementation
278
	 * in the abstract base class returns the simple name of the class, this may
279
	 * be overwritten to something more specific in subclasses.
280
	 * 
281
	 * @param entity
282
	 * @return a textual representation given object.
283
	 */
284
	public String getTypeText(Object entity){
285
		return entity.getClass().getSimpleName();
286
	}
287

    
288
	/**
289
	 * @param entity
290
	 * @return
291
	 */
292
	public String getText(T entity) {
293
		if(entity instanceof IdentifiableEntity){
294
			IdentifiableEntity identifiableEntity = (IdentifiableEntity) HibernateProxyHelper.deproxy(entity);
295

    
296
			return identifiableEntity.getTitleCache();
297
		}
298

    
299
		return "No text. Implement in subclass";
300
	}
301

    
302
	/**
303
	 * @return
304
	 */
305
	public List<?> getModel() {
306
		return model;
307
	}
308

    
309

    
310
}
(1-1/10)