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
.conversation
.ConversationHolder
;
25 import eu
.etaxonomy
.cdm
.api
.service
.config
.IIdentifiableEntityServiceConfigurator
;
26 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
27 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
28 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
29 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
30 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
31 import eu
.etaxonomy
.cdm
.strategy
.merge
.IMergable
;
32 import eu
.etaxonomy
.cdm
.strategy
.merge
.MergeException
;
33 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.IEntityCreator
;
34 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.IEntityPersistenceService
;
35 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditorQuery
;
36 import eu
.etaxonomy
.taxeditor
.bulkeditor
.IBulkEditorSortProvider
;
37 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.sortprovider
.CdmBaseSortProvider
;
38 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.sortprovider
.TitleCacheComparator
;
39 import eu
.etaxonomy
.taxeditor
.bulkeditor
.internal
.TaxeditorBulkeditorPlugin
;
40 import eu
.etaxonomy
.taxeditor
.editor
.CdmEntitySessionInput
;
41 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
42 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
45 * <p>Abstract AbstractBulkEditorInput class.</p>
52 public abstract class AbstractBulkEditorInput
<T
extends ICdmBase
> extends CdmEntitySessionInput
implements IEditorInput
,
53 IEntityPersistenceService
<T
> {
55 private UUID entityUuid
;
57 private List
<T
> model
;
59 private IEntityCreator
<T
> entityCreator
;
60 private final ConversationHolder conversation
;
62 private static Class serviceClass
;
64 public AbstractBulkEditorInput() {
66 this.conversation
= CdmStore
.createConversation();
72 * @param inputType a {@link eu.etaxonomy.taxeditor.bulkeditor.input.BulkEditorInputTypeValues.BulkEditorInputType} object.
73 * @return a {@link eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput} object.
75 static public AbstractBulkEditorInput
NewInstance(BulkEditorInputType inputType
) {
77 return BulkEditorInputType
.getInput(inputType
);
83 * @param entity a {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity} object.
84 * @return a {@link eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput} object.
86 public static AbstractBulkEditorInput
NewInstance(IdentifiableEntity entity
) {
89 BulkEditorInputType inputType
= BulkEditorInputType
.getByType(entity
.getClass());
91 AbstractBulkEditorInput editorInput
= NewInstance(inputType
);
93 editorInput
.setEntityUuid(entity
.getUuid());
101 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
102 * @return a {@link java.util.List} object.
104 protected abstract List
<T
> listEntities(IIdentifiableEntityServiceConfigurator configurator
);
109 * @param entityUuid a {@link java.util.UUID} object.
110 * @return a T object.
112 protected T
loadEntity(UUID entityUuid
) {
113 List
<String
> propertyPaths
= Arrays
.asList(new String
[]{});
114 return (T
) CdmStore
.getService(serviceClass
).load(entityUuid
, propertyPaths
);
117 private void setEntityUuid(UUID entityUuid
){
118 this.entityUuid
= entityUuid
;
122 * <p>Getter for the field <code>entityUuid</code>.</p>
124 * @return a {@link java.util.UUID} object.
126 public UUID
getEntityUuid() {
132 * @see org.eclipse.ui.IEditorInput#exists()
140 public boolean exists() {
141 // TODO Auto-generated method stub
146 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
149 * <p>getImageDescriptor</p>
151 * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
154 public ImageDescriptor
getImageDescriptor() {
155 // TODO Auto-generated method stub
160 * @see org.eclipse.ui.IEditorInput#getPersistable()
163 * <p>getPersistable</p>
165 * @return a {@link org.eclipse.ui.IPersistableElement} object.
168 public IPersistableElement
getPersistable() {
173 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
177 @SuppressWarnings("unchecked")
178 public Object
getAdapter(Class adapter
) {
183 * <p>Setter for the field <code>query</code>.</p>
185 * @param bulkEditorQuery a {@link eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorQuery} object.
187 public void performSearch(final BulkEditorQuery bulkEditorQuery
) {
189 List
<T
> entityList
= new ArrayList
<T
>();
191 if(getEntityUuid() != null){
193 T entity
= loadEntity(getEntityUuid());
194 entityList
.add(entity
);
197 else if(bulkEditorQuery
!= null){
199 IIdentifiableEntityServiceConfigurator configurator
= bulkEditorQuery
.getSearchConfigurator();
200 Comparator queryComparator
= (bulkEditorQuery
.getComparator() != null) ? bulkEditorQuery
.getComparator() : new TitleCacheComparator();
202 entityList
= listEntities(configurator
);
204 Collections
.sort(entityList
, queryComparator
);
213 * <p>isMergingEnabled</p>
217 public boolean isMergingEnabled() {
222 * <p>isMergingEnabled</p>
226 public boolean isConvertingEnabled() {
230 * <p>isMarkerTypeEditingEnabled</p>
232 * @param markerType a {@link eu.etaxonomy.cdm.model.common.MarkerType} object.
235 public boolean isMarkerTypeEditingEnabled(MarkerType markerType
) {
242 public boolean merge(T entity
, T mergeTarget
) {
243 if (entity
instanceof IMergable
) {
245 CdmStore
.getCommonService().merge(mergeTarget
.getUuid(), entity
.getUuid(), (Class
<?
extends CdmBase
>)entity
.getClass());
246 } catch (MergeException e
) {
247 MessagingUtils
.errorDialog("Bulk Editor Merge Error",
249 "Could not merge chosen objects of type " + entity
.getClass().getName(),
250 TaxeditorBulkeditorPlugin
.PLUGIN_ID
,
261 public T
create(T entity
) {
265 public IEntityCreator
<T
> getEntityCreator(){
266 if(entityCreator
== null){
267 entityCreator
= createEntityCreator();
269 return entityCreator
;
275 protected abstract IEntityCreator
<T
> createEntityCreator();
278 * The default implementation returns an empty list of sort providers.
281 public List
<IBulkEditorSortProvider
<T
>> getSortProviders(){
282 List
<IBulkEditorSortProvider
<T
>> sortProviders
= new ArrayList
<IBulkEditorSortProvider
<T
>>();
284 sortProviders
.add(new CdmBaseSortProvider
<T
>());
286 return sortProviders
;
290 * Returns a textual representation given object. The default implementation
291 * in the abstract base class returns the simple name of the class, this may
292 * be overwritten to something more specific in subclasses.
295 * @return a textual representation given object.
297 public String
getTypeText(Object entity
){
298 return entity
.getClass().getSimpleName();
305 public String
getText(T entity
) {
306 if(entity
instanceof IdentifiableEntity
){
307 IdentifiableEntity identifiableEntity
= (IdentifiableEntity
) HibernateProxyHelper
.deproxy(entity
);
309 return identifiableEntity
.getTitleCache();
312 return "No text. Implement in subclass";
318 public List
<T
> getModel() {
322 protected boolean replaceInModel(T entity
) {
323 int index
= model
.indexOf(entity
);
325 model
.set(index
, entity
);
333 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
336 public List
<T
> getRootEntities() {
342 * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getPropertyPathsMap()
345 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
346 // TODO Auto-generated method stub
350 public ConversationHolder
getConversation() {