2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.taxeditor
.bulkeditor
.input
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Comparator
;
13 import java
.util
.HashMap
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
17 import java
.util
.Map
.Entry
;
19 import java
.util
.UUID
;
21 import org
.eclipse
.core
.runtime
.ICoreRunnable
;
22 import org
.eclipse
.core
.runtime
.jobs
.Job
;
23 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
25 import ca
.odell
.glazedlists
.BasicEventList
;
26 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
27 import eu
.etaxonomy
.cdm
.api
.service
.config
.DeleteConfiguratorBase
;
28 import eu
.etaxonomy
.cdm
.api
.service
.config
.IIdentifiableEntityServiceConfigurator
;
29 import eu
.etaxonomy
.cdm
.api
.service
.exception
.ReferencedObjectUndeletableException
;
30 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
31 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
32 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
33 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
34 import eu
.etaxonomy
.cdm
.strategy
.merge
.IMergable
;
35 import eu
.etaxonomy
.cdm
.strategy
.merge
.MergeException
;
36 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.IEntityCreator
;
37 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.IEntityPersistenceService
;
38 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditorQuery
;
39 import eu
.etaxonomy
.taxeditor
.bulkeditor
.IBulkEditorSortProvider
;
40 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.sortprovider
.CdmBaseSortProvider
;
41 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.sortprovider
.TitleCacheComparator
;
42 import eu
.etaxonomy
.taxeditor
.bulkeditor
.internal
.TaxeditorBulkeditorPlugin
;
43 import eu
.etaxonomy
.taxeditor
.editor
.CdmEntitySessionInput
;
44 import eu
.etaxonomy
.taxeditor
.event
.EventUtility
;
45 import eu
.etaxonomy
.taxeditor
.event
.WorkbenchEventConstants
;
46 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
47 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
48 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
56 public abstract class AbstractBulkEditorInput
<T
extends CdmBase
> extends CdmEntitySessionInput
implements
57 IEntityPersistenceService
<T
> {
59 private UUID entityUuid
;
61 private BasicEventList
<T
> model
= new BasicEventList
<>();
63 private Map
<T
, DeleteConfiguratorBase
> toDelete
= new HashMap
<>();
64 private Set
<T
> saveCandidates
= new HashSet
<>();
67 private Set
<T
> markedMergeCandidates
= new HashSet
<>();
68 private T markedMergeTarget
= null;
70 private HashMap
<T
, Set
<T
>> mergedEntities
= new HashMap
<>();
72 private IEntityCreator
<T
> entityCreator
;
73 private final ConversationHolder conversation
;
75 private Job searchJob
;
77 public AbstractBulkEditorInput() {
79 this.conversation
= CdmStore
.createConversation();
82 static public AbstractBulkEditorInput
NewInstance(BulkEditorInputType inputType
) {
84 return BulkEditorInputType
.getInput(inputType
);
87 public static AbstractBulkEditorInput
NewInstance(IdentifiableEntity entity
) {
89 BulkEditorInputType inputType
= BulkEditorInputType
.getByType(entity
.getClass());
91 AbstractBulkEditorInput editorInput
= NewInstance(inputType
);
93 editorInput
.setEntityUuid(entity
.getUuid());
98 public abstract String
getName();
100 public String
getEditorName(){
104 protected int getPageSize(){
108 protected abstract List
<T
> listEntities(IIdentifiableEntityServiceConfigurator configurator
);
110 protected abstract long countEntities(IIdentifiableEntityServiceConfigurator configurator
);
112 protected abstract T
loadEntity(UUID entityUuid
);
114 public Comparator
<T
> getTitleComparator(){
115 return new TitleCacheComparator();
118 public void setMergeTarget(T t
){
119 markedMergeTarget
= t
;
122 public Set
<T
> getMergeCandidates() {
123 return markedMergeCandidates
;
126 public T
getMergeTarget() {
127 return markedMergeTarget
;
130 public void removeMergeTarget(){
131 markedMergeTarget
= null;
134 public void addMergeCandidate(T t
){
135 markedMergeCandidates
.add(t
);
138 public void removeMergeCandidate(T t
){
139 markedMergeCandidates
.remove(t
);
142 public void addToDelete(T t
, DeleteConfiguratorBase config
) {
143 toDelete
.put(t
, config
);
145 public void addSaveCandidate(T t
){
146 saveCandidates
.add(t
);
148 private void setEntityUuid(UUID entityUuid
){
149 this.entityUuid
= entityUuid
;
152 public UUID
getEntityUuid() {
156 public void performSearch(final BulkEditorQuery bulkEditorQuery
, IStructuredSelection selection
) {
157 //cancel previous search job
158 if(searchJob
!=null && searchJob
.getState()!=Job
.NONE
){
164 if(getEntityUuid() != null){
166 T entity
= loadEntity(getEntityUuid());
169 else if(bulkEditorQuery
!= null){
171 IIdentifiableEntityServiceConfigurator configurator
= bulkEditorQuery
.getSearchConfigurator();
173 int pageSize
= configurator
.getPageSize()!=null?configurator
.getPageSize():getPageSize();
174 configurator
.setPageSize(pageSize
);
175 long count
= countEntities(configurator
);
176 int totalWork
= count
>Integer
.MAX_VALUE?Integer
.MAX_VALUE
:(int)count
;
177 String jobLabel
= String
.format(Messages
.AbstractBulkEditorInput_LOADING
, getName(), bulkEditorQuery
.getSearchString());
178 searchJob
= Job
.create(jobLabel
, (ICoreRunnable
) monitor
-> {
179 monitor
.beginTask(jobLabel
, totalWork
);
183 if (monitor
.isCanceled()) {
186 configurator
.setPageNumber(pageNumber
);
187 entities
= listEntities(configurator
);
188 model
.addAll(entities
);
189 //select if entity is loaded
190 if(selection
!=null && model
.containsAll(selection
.toList())){
191 EventUtility
.postAsyncEvent(WorkbenchEventConstants
.BULK_EDITOR_SEARCH_FINISHED
, selection
);
194 monitor
.worked(pageSize
);
195 long workedLong
= pageSize
*pageNumber
;
196 int loadedCount
= workedLong
>Integer
.MAX_VALUE?Integer
.MAX_VALUE
:(int)workedLong
;
197 monitor
.setTaskName(String
.format(Messages
.AbstractBulkEditorInput_LOADED
, loadedCount
, totalWork
, getName()));
198 } while (!entities
.isEmpty());
200 EventUtility
.postAsyncEvent(WorkbenchEventConstants
.BULK_EDITOR_SEARCH_FINISHED
, selection
);
202 searchJob
.schedule();
206 public boolean isMergingEnabled() {
210 public boolean isConvertingEnabled() {
214 public boolean isMarkerTypeEditingEnabled(MarkerType markerType
) {
221 public boolean merge(T entity
, T mergeTarget
) {
222 if (entity
instanceof IMergable
) {
224 CdmStore
.getCommonService().merge(mergeTarget
.getUuid(), entity
.getUuid(), (Class
<?
extends CdmBase
>)entity
.getClass());
225 } catch (MergeException e
) {
226 MessagingUtils
.errorDialog(Messages
.AbstractBulkEditorInput_MERGE_ERROR_TITLE
,
228 String
.format(Messages
.AbstractBulkEditorInput_MERGE_ERROR_MESSAGE
, entity
.getClass().getName()),
229 TaxeditorBulkeditorPlugin
.PLUGIN_ID
,
237 public void saveModel(){
241 public void saveModel(boolean resetMerge
){
243 for(Entry
<T
, DeleteConfiguratorBase
> entry
:toDelete
.entrySet()){
245 delete(entry
.getKey(), entry
.getValue());
246 } catch (ReferencedObjectUndeletableException e
) {
250 if (!saveCandidates
.isEmpty()){
251 CdmStore
.getService(saveCandidates
.iterator().next()).merge(new ArrayList
<>(saveCandidates
), true);
255 for(T mergeTarget
:mergedEntities
.keySet()){
256 for (T mergeCandidate
: mergedEntities
.get(mergeTarget
)){
257 merge(mergeCandidate
, mergeTarget
);
262 saveCandidates
.clear();
263 mergedEntities
.clear();
269 public T
create(T entity
) {
273 public IEntityCreator
<T
> getEntityCreator(){
274 if(entityCreator
== null){
275 entityCreator
= createEntityCreator();
277 return entityCreator
;
280 protected abstract IEntityCreator
<T
> createEntityCreator();
283 * The default implementation returns an empty list of sort providers.
286 public List
<IBulkEditorSortProvider
<T
>> getSortProviders(){
287 List
<IBulkEditorSortProvider
<T
>> sortProviders
= new ArrayList
<IBulkEditorSortProvider
<T
>>();
289 sortProviders
.add(new CdmBaseSortProvider
<T
>());
291 return sortProviders
;
295 * Returns a textual representation given object. The default implementation
296 * in the abstract base class returns the simple name of the class, this may
297 * be overwritten to something more specific in subclasses.
300 * @return a textual representation given object.
302 public String
getTypeText(Object entity
){
303 return entity
.getClass().getSimpleName();
306 public String
getText(T entity
) {
307 if(entity
instanceof IdentifiableEntity
){
308 IdentifiableEntity identifiableEntity
= (IdentifiableEntity
) HibernateProxyHelper
.deproxy(entity
);
309 String text
= identifiableEntity
.getTitleCache();
313 return "No text. Implement in subclass"; //$NON-NLS-1$
316 public BasicEventList
<T
> getModel() {
320 public boolean replaceInModel(T entity
) {
321 int index
= model
.indexOf(entity
);
323 model
.set(index
, entity
);
331 public List
<T
> getRootEntities() {
337 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
338 // TODO Auto-generated method stub
342 public ConversationHolder
getConversation() {
346 public Set
<T
> getSaveCandidates() {
347 return saveCandidates
;
350 public HashMap
<T
, Set
<T
>> getMergedEntities() {
351 return mergedEntities
;
354 public void setMergedEntities(HashMap
<T
, Set
<T
>> mergedEntities
) {
355 this.mergedEntities
= mergedEntities
;