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
;
20 import java
.util
.stream
.Collectors
;
22 import org
.eclipse
.core
.runtime
.ICoreRunnable
;
23 import org
.eclipse
.core
.runtime
.jobs
.Job
;
24 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
26 import ca
.odell
.glazedlists
.BasicEventList
;
27 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
28 import eu
.etaxonomy
.cdm
.api
.service
.config
.DeleteConfiguratorBase
;
29 import eu
.etaxonomy
.cdm
.api
.service
.config
.IIdentifiableEntityServiceConfigurator
;
30 import eu
.etaxonomy
.cdm
.api
.service
.exception
.ReferencedObjectUndeletableException
;
31 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
32 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
33 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
34 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
35 import eu
.etaxonomy
.cdm
.strategy
.merge
.IMergable
;
36 import eu
.etaxonomy
.cdm
.strategy
.merge
.MergeException
;
37 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.IEntityCreator
;
38 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.IEntityPersistenceService
;
39 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditorQuery
;
40 import eu
.etaxonomy
.taxeditor
.bulkeditor
.IBulkEditorSortProvider
;
41 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.sortprovider
.CdmBaseSortProvider
;
42 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.sortprovider
.TitleCacheComparator
;
43 import eu
.etaxonomy
.taxeditor
.bulkeditor
.internal
.TaxeditorBulkeditorPlugin
;
44 import eu
.etaxonomy
.taxeditor
.editor
.CdmEntitySessionInput
;
45 import eu
.etaxonomy
.taxeditor
.event
.EventUtility
;
46 import eu
.etaxonomy
.taxeditor
.event
.WorkbenchEventConstants
;
47 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
48 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
49 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
57 public abstract class AbstractBulkEditorInput
<T
extends CdmBase
> extends CdmEntitySessionInput
implements
58 IEntityPersistenceService
<T
> {
60 private UUID entityUuid
;
62 private BasicEventList
<T
> model
= new BasicEventList
<>();
64 private Map
<T
, DeleteConfiguratorBase
> toDelete
= new HashMap
<>();
65 private Set
<T
> saveCandidates
= new HashSet
<>();
68 private Set
<T
> markedMergeCandidates
= new HashSet
<>();
69 private T markedMergeTarget
= null;
71 private HashMap
<T
, Set
<T
>> mergedEntities
= new HashMap
<>();
73 private IEntityCreator
<T
> entityCreator
;
74 private final ConversationHolder conversation
;
76 private Job searchJob
;
78 public AbstractBulkEditorInput() {
80 this.conversation
= CdmStore
.createConversation();
83 static public AbstractBulkEditorInput
NewInstance(BulkEditorInputType inputType
) {
85 return BulkEditorInputType
.getInput(inputType
);
88 public static AbstractBulkEditorInput
NewInstance(IdentifiableEntity entity
) {
90 BulkEditorInputType inputType
= BulkEditorInputType
.getByType(entity
.getClass());
92 AbstractBulkEditorInput editorInput
= NewInstance(inputType
);
94 editorInput
.setEntityUuid(entity
.getUuid());
99 public abstract String
getName();
101 public String
getEditorName(){
105 protected int getPageSize(){
109 protected abstract List
<T
> listEntities(IIdentifiableEntityServiceConfigurator configurator
);
111 protected abstract long countEntities(IIdentifiableEntityServiceConfigurator configurator
);
113 protected abstract T
loadEntity(UUID entityUuid
);
115 public Comparator
<T
> getTitleComparator(){
116 return new TitleCacheComparator();
119 public void setMergeTarget(T t
){
120 markedMergeTarget
= t
;
123 public Set
<T
> getMergeCandidates() {
124 return markedMergeCandidates
;
127 public T
getMergeTarget() {
128 return markedMergeTarget
;
131 public void removeMergeTarget(){
132 markedMergeTarget
= null;
135 public void addMergeCandidate(T t
){
136 markedMergeCandidates
.add(t
);
139 public void removeMergeCandidate(T t
){
140 markedMergeCandidates
.remove(t
);
143 public void addToDelete(T t
, DeleteConfiguratorBase config
) {
144 toDelete
.put(t
, config
);
146 public void addSaveCandidate(T t
){
147 saveCandidates
.add(t
);
149 private void setEntityUuid(UUID entityUuid
){
150 this.entityUuid
= entityUuid
;
153 public UUID
getEntityUuid() {
157 public void performSearch(final BulkEditorQuery bulkEditorQuery
, IStructuredSelection selection
) {
158 //cancel previous search job
159 if(searchJob
!=null && searchJob
.getState()!=Job
.NONE
){
163 * wait for a little while for the job to finish
164 * to avoid asynchronously loaded results of the
165 * previous search being shown in the next search
166 * (not critical but explicitly waiting for the job to finish
167 * could run into an endless loop by mistake)
171 } catch (InterruptedException e
) {
176 if(getEntityUuid() != null){
177 T entity
= loadEntity(getEntityUuid());
180 else if(bulkEditorQuery
!= null){
181 IIdentifiableEntityServiceConfigurator configurator
= bulkEditorQuery
.getSearchConfigurator();
183 // check for UUID search
184 String titleSearchString
= configurator
.getTitleSearchString();
186 UUID uuid
= UUID
.fromString(titleSearchString
);
187 T entity
= loadEntity(uuid
);
188 //UUID search found -> add entity to list and return
191 } catch (IllegalArgumentException e
) {
192 // search string was no UUID
194 //-> continue with standard search
196 int pageSize
= configurator
.getPageSize()!=null?configurator
.getPageSize():getPageSize();
197 configurator
.setPageSize(pageSize
);
198 long count
= countEntities(configurator
);
199 int totalWork
= count
>Integer
.MAX_VALUE?Integer
.MAX_VALUE
:(int)count
;
200 String jobLabel
= String
.format(Messages
.AbstractBulkEditorInput_LOADING
, getName(), bulkEditorQuery
.getSearchString());
201 searchJob
= Job
.create(jobLabel
, (ICoreRunnable
) monitor
-> {
202 monitor
.beginTask(jobLabel
, totalWork
);
206 //load previously selected element
207 UUID selectedUuid
= null;
208 if(selection
!=null && selection
.getFirstElement() instanceof CdmBase
){
209 selectedUuid
= ((CdmBase
) selection
.getFirstElement()).getUuid();
210 T entity
= loadEntity(selectedUuid
);
215 if (monitor
.isCanceled()) {
218 configurator
.setPageNumber(pageNumber
);
219 entities
= listEntities(configurator
);
221 addToModel(entities
, selectedUuid
);
224 monitor
.worked(pageSize
);
225 long workedLong
= pageSize
*pageNumber
;
226 int loadedCount
= workedLong
>Integer
.MAX_VALUE?Integer
.MAX_VALUE
:(int)workedLong
;
227 monitor
.setTaskName(String
.format(Messages
.AbstractBulkEditorInput_LOADED
, loadedCount
, totalWork
, getName()));
230 EventUtility
.postAsyncEvent(WorkbenchEventConstants
.BULK_EDITOR_SEARCH_FINISHED
, selection
);
231 } while (!entities
.isEmpty());
233 EventUtility
.postAsyncEvent(WorkbenchEventConstants
.BULK_EDITOR_SEARCH_FINISHED
, selection
);
235 searchJob
.schedule();
239 private void addToModel(List
<T
> entities
, UUID selectedUuid
){
240 //filter pre-loaded previously selected element
241 if(selectedUuid
!=null){
242 entities
= entities
.stream().filter(entity
->!entity
.getUuid().equals(selectedUuid
)).collect(Collectors
.toList());
246 * Entities have to be loaded into the main session because they are
247 * loaded in a parallel asynchronous thread
249 if(getCdmEntitySession()!=null){//is null when closing the bulk editor during loading
250 getCdmEntitySession().load(entities
, true);
252 model
.addAll(entities
);
255 public boolean isMergingEnabled() {
259 public boolean isConvertingEnabled() {
263 public boolean isMarkerTypeEditingEnabled(MarkerType markerType
) {
270 public boolean merge(T entity
, T mergeTarget
) {
271 if (entity
instanceof IMergable
) {
273 CdmStore
.getCommonService().merge(mergeTarget
.getUuid(), entity
.getUuid(), (Class
<?
extends CdmBase
>)entity
.getClass());
274 } catch (MergeException e
) {
275 MessagingUtils
.errorDialog(Messages
.AbstractBulkEditorInput_MERGE_ERROR_TITLE
,
277 String
.format(Messages
.AbstractBulkEditorInput_MERGE_ERROR_MESSAGE
, entity
.getClass().getName()),
278 TaxeditorBulkeditorPlugin
.PLUGIN_ID
,
286 public void saveModel(){
290 public void saveModel(boolean resetMerge
){
292 for(Entry
<T
, DeleteConfiguratorBase
> entry
:toDelete
.entrySet()){
294 delete(entry
.getKey(), entry
.getValue());
295 } catch (ReferencedObjectUndeletableException e
) {
299 if (!saveCandidates
.isEmpty()){
300 CdmStore
.getService(saveCandidates
.iterator().next()).merge(new ArrayList
<>(saveCandidates
), true);
304 for(T mergeTarget
:mergedEntities
.keySet()){
305 for (T mergeCandidate
: mergedEntities
.get(mergeTarget
)){
306 merge(mergeCandidate
, mergeTarget
);
311 saveCandidates
.clear();
312 mergedEntities
.clear();
318 public T
create(T entity
) {
322 public IEntityCreator
<T
> getEntityCreator(){
323 if(entityCreator
== null){
324 entityCreator
= createEntityCreator();
326 return entityCreator
;
329 protected abstract IEntityCreator
<T
> createEntityCreator();
332 * The default implementation returns an empty list of sort providers.
335 public List
<IBulkEditorSortProvider
<T
>> getSortProviders(){
336 List
<IBulkEditorSortProvider
<T
>> sortProviders
= new ArrayList
<IBulkEditorSortProvider
<T
>>();
338 sortProviders
.add(new CdmBaseSortProvider
<T
>());
340 return sortProviders
;
344 * Returns a textual representation given object. The default implementation
345 * in the abstract base class returns the simple name of the class, this may
346 * be overwritten to something more specific in subclasses.
349 * @return a textual representation given object.
351 public String
getTypeText(Object entity
){
352 return entity
.getClass().getSimpleName();
355 public String
getText(T entity
) {
356 if(entity
instanceof IdentifiableEntity
){
357 IdentifiableEntity identifiableEntity
= (IdentifiableEntity
) HibernateProxyHelper
.deproxy(entity
);
358 String text
= identifiableEntity
.getTitleCache();
362 return "No text. Implement in subclass"; //$NON-NLS-1$
365 public BasicEventList
<T
> getModel() {
369 public boolean replaceInModel(T entity
) {
370 int index
= model
.indexOf(entity
);
372 model
.set(index
, entity
);
380 public List
<T
> getRootEntities() {
386 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
387 // TODO Auto-generated method stub
391 public ConversationHolder
getConversation() {
395 public Set
<T
> getSaveCandidates() {
396 return saveCandidates
;
399 public HashMap
<T
, Set
<T
>> getMergedEntities() {
400 return mergedEntities
;
403 public void setMergedEntities(HashMap
<T
, Set
<T
>> mergedEntities
) {
404 this.mergedEntities
= mergedEntities
;