Project

General

Profile

Download (7.87 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.Collections;
14
import java.util.Comparator;
15
import java.util.List;
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.service.ICommonService;
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.model.occurrence.SpecimenOrObservationBase;
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.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
	/**
51
	 *
52
	 */
53
	private static final long serialVersionUID = 416414530232743735L;
54

    
55
	private UUID entityUuid;
56

    
57
	private List<T> model;
58

    
59
	private IEntityCreator<T> entityCreator;
60

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

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

    
79

    
80
		BulkEditorInputType inputType = BulkEditorInputType.getByType(entity.getClass());
81

    
82
		AbstractBulkEditorInput editorInput = NewInstance(inputType);
83

    
84
		editorInput.setEntityUuid(entity.getUuid());
85

    
86
		return editorInput;
87
	}
88

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

    
97
	/**
98
	 * <p>loadEntity</p>
99
	 *
100
	 * @param entityUuid a {@link java.util.UUID} object.
101
	 * @return a T object.
102
	 */
103
	protected abstract T loadEntity(UUID entityUuid);
104

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

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

    
118

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

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

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

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

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

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

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

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

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

    
190
			entityList = listEntities(configurator);
191

    
192
			Collections.sort(entityList, queryComparator);
193

    
194
		}
195

    
196
		model = entityList;
197
	}
198

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

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

    
218
	/*
219
	 * (non-Javadoc)
220
	 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService#merge(eu.etaxonomy.cdm.model.common.IdentifiableEntity, eu.etaxonomy.cdm.model.common.IdentifiableEntity)
221
	 */
222
	/** {@inheritDoc} */
223
	@Override
224
    public boolean merge(T entity, T mergeTarget) {
225
		if (entity instanceof IMergable) {
226
			try {
227
				CdmStore.getService(ICommonService.class).merge((IMergable)mergeTarget, (IMergable)entity, null);
228
			} catch (MergeException e) {
229
				MessagingUtils.error(getClass(), e);
230
			}
231
		}
232
		return true;
233
	}
234

    
235
	/*
236
	 * (non-Javadoc)
237
	 * @see eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService#create(eu.etaxonomy.cdm.model.common.IdentifiableEntity)
238
	 */
239
	/** {@inheritDoc} */
240
	@Override
241
    public boolean create(T entity) {
242
		return save(entity);
243
	}
244

    
245
	public IEntityCreator<T> getEntityCreator(){
246
		if(entityCreator == null){
247
			entityCreator = createEntityCreator();
248
		}
249
		return entityCreator;
250
	}
251

    
252
	/**
253
	 * @return
254
	 */
255
	protected abstract IEntityCreator<T> createEntityCreator();
256

    
257
	/**
258
	 * The default implementation returns an empty list of sort providers.
259
	 * @return
260
	 */
261
	public List<IBulkEditorSortProvider<T>> getSortProviders(){
262
		List<IBulkEditorSortProvider<T>> sortProviders = new ArrayList<IBulkEditorSortProvider<T>>();
263

    
264
		sortProviders.add(new CdmBaseSortProvider<T>());
265

    
266
		return sortProviders;
267
	}
268

    
269
	/**
270
	 * Returns a textual representation given object. The default implementation
271
	 * in the abstract base class returns the simple name of the class, this may
272
	 * be overwritten to something more specific in subclasses.
273
	 * 
274
	 * @param entity
275
	 * @return a textual representation given object.
276
	 */
277
	public String getTypeText(Object entity){
278
		return entity.getClass().getSimpleName();
279
	}
280

    
281
	/**
282
	 * @param entity
283
	 * @return
284
	 */
285
	public String getText(T entity) {
286
		if(entity instanceof IdentifiableEntity){
287
			IdentifiableEntity identifiableEntity = (IdentifiableEntity) HibernateProxyHelper.deproxy(entity);
288

    
289
			return identifiableEntity.getTitleCache();
290
		}
291

    
292
		return "No text. Implement in subclass";
293
	}
294

    
295
	/**
296
	 * @return
297
	 */
298
	public List<?> getModel() {
299
		return model;
300
	}
301

    
302

    
303
}
(1-1/10)