3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.bulkeditor
.input
;
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
;
18 import java
.util
.UUID
;
20 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
21 import org
.eclipse
.ui
.IEditorInput
;
22 import org
.eclipse
.ui
.IPersistableElement
;
24 import eu
.etaxonomy
.cdm
.api
.service
.config
.IIdentifiableEntityServiceConfigurator
;
25 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
26 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
27 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
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
;
44 * <p>Abstract AbstractBulkEditorInput class.</p>
51 public abstract class AbstractBulkEditorInput
<T
extends ICdmBase
> extends CdmEntitySessionInput
implements IEditorInput
,
52 IEntityPersistenceService
<T
> {
54 private UUID entityUuid
;
56 private List
<T
> model
;
58 private IEntityCreator
<T
> entityCreator
;
60 private static Class serviceClass
;
62 public AbstractBulkEditorInput() {
69 * @param inputType a {@link eu.etaxonomy.taxeditor.bulkeditor.input.BulkEditorInputTypeValues.BulkEditorInputType} object.
70 * @return a {@link eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput} object.
72 static public AbstractBulkEditorInput
NewInstance(BulkEditorInputType inputType
) {
74 return BulkEditorInputType
.getInput(inputType
);
80 * @param entity a {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity} object.
81 * @return a {@link eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput} object.
83 public static AbstractBulkEditorInput
NewInstance(IdentifiableEntity entity
) {
86 BulkEditorInputType inputType
= BulkEditorInputType
.getByType(entity
.getClass());
88 AbstractBulkEditorInput editorInput
= NewInstance(inputType
);
90 editorInput
.setEntityUuid(entity
.getUuid());
98 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
99 * @return a {@link java.util.List} object.
101 protected abstract List
<T
> listEntities(IIdentifiableEntityServiceConfigurator configurator
);
106 * @param entityUuid a {@link java.util.UUID} object.
107 * @return a T object.
109 protected T
loadEntity(UUID entityUuid
) {
110 List
<String
> propertyPaths
= Arrays
.asList(new String
[]{});
111 return (T
) CdmStore
.getService(serviceClass
).load(entityUuid
, propertyPaths
);
114 private void setEntityUuid(UUID entityUuid
){
115 this.entityUuid
= entityUuid
;
119 * <p>Getter for the field <code>entityUuid</code>.</p>
121 * @return a {@link java.util.UUID} object.
123 public UUID
getEntityUuid() {
129 * @see org.eclipse.ui.IEditorInput#exists()
137 public boolean exists() {
138 // TODO Auto-generated method stub
143 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
146 * <p>getImageDescriptor</p>
148 * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
151 public ImageDescriptor
getImageDescriptor() {
152 // TODO Auto-generated method stub
157 * @see org.eclipse.ui.IEditorInput#getPersistable()
160 * <p>getPersistable</p>
162 * @return a {@link org.eclipse.ui.IPersistableElement} object.
165 public IPersistableElement
getPersistable() {
170 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
174 @SuppressWarnings("unchecked")
175 public Object
getAdapter(Class adapter
) {
180 * <p>Setter for the field <code>query</code>.</p>
182 * @param bulkEditorQuery a {@link eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorQuery} object.
184 public void performSearch(final BulkEditorQuery bulkEditorQuery
) {
186 List
<T
> entityList
= new ArrayList
<T
>();
188 if(getEntityUuid() != null){
190 T entity
= loadEntity(getEntityUuid());
191 entityList
.add(entity
);
194 else if(bulkEditorQuery
!= null){
196 IIdentifiableEntityServiceConfigurator configurator
= bulkEditorQuery
.getSearchConfigurator();
197 Comparator queryComparator
= (bulkEditorQuery
.getComparator() != null) ? bulkEditorQuery
.getComparator() : new TitleCacheComparator();
199 entityList
= listEntities(configurator
);
201 Collections
.sort(entityList
, queryComparator
);
210 * <p>isMergingEnabled</p>
214 public boolean isMergingEnabled() {
219 * <p>isMergingEnabled</p>
223 public boolean isConvertingEnabled() {
227 * <p>isMarkerTypeEditingEnabled</p>
229 * @param markerType a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
232 public boolean isMarkerTypeEditingEnabled(MarkerType markerType
) {
239 public boolean merge(T entity
, T mergeTarget
) {
240 if (entity
instanceof IMergable
) {
242 CdmStore
.getCommonService().merge(mergeTarget
.getUuid(), entity
.getUuid(), (Class
<?
extends CdmBase
>)entity
.getClass());
243 } catch (MergeException e
) {
244 MessagingUtils
.errorDialog("Bulk Editor Merge Error",
246 "Could not merge chosen objects of type " + entity
.getClass().getName(),
247 TaxeditorBulkeditorPlugin
.PLUGIN_ID
,
258 public T
create(T entity
) {
262 public IEntityCreator
<T
> getEntityCreator(){
263 if(entityCreator
== null){
264 entityCreator
= createEntityCreator();
266 return entityCreator
;
272 protected abstract IEntityCreator
<T
> createEntityCreator();
275 * The default implementation returns an empty list of sort providers.
278 public List
<IBulkEditorSortProvider
<T
>> getSortProviders(){
279 List
<IBulkEditorSortProvider
<T
>> sortProviders
= new ArrayList
<IBulkEditorSortProvider
<T
>>();
281 sortProviders
.add(new CdmBaseSortProvider
<T
>());
283 return sortProviders
;
287 * Returns a textual representation given object. The default implementation
288 * in the abstract base class returns the simple name of the class, this may
289 * be overwritten to something more specific in subclasses.
292 * @return a textual representation given object.
294 public String
getTypeText(Object entity
){
295 return entity
.getClass().getSimpleName();
302 public String
getText(T entity
) {
303 if(entity
instanceof IdentifiableEntity
){
304 IdentifiableEntity identifiableEntity
= (IdentifiableEntity
) HibernateProxyHelper
.deproxy(entity
);
306 return identifiableEntity
.getTitleCache();
309 return "No text. Implement in subclass";
315 public List
<T
> getModel() {
319 protected boolean replaceInModel(T entity
) {
320 int index
= model
.indexOf(entity
);
322 model
.set(index
, entity
);
330 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
333 public List
<T
> getRootEntities() {
339 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getPropertyPathsMap()
342 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
343 // TODO Auto-generated method stub