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
> {
61 private static final String PROPERTY_PROTECTED_TITLECACHE
= "Protect TitleCache";
62 private static final String TYPE_PROPERTY
= Messages
.BulkEditorE4_TYPE
;
63 private static final String ID_PROPERTY
= "Id"; //$NON-NLS-1$
64 private static final String UUID_PROPERTY
= "Uuid"; //$NON-NLS-1$
66 private UUID entityUuid
;
68 private BasicEventList
<T
> model
= new BasicEventList
<>();
70 private Map
<T
, DeleteConfiguratorBase
> toDelete
= new HashMap
<>();
71 public Map
<T
, DeleteConfiguratorBase
> getToDelete() {
75 private Set
<T
> saveCandidates
= new HashSet
<>();
78 private Set
<T
> markedMergeCandidates
= new HashSet
<>();
79 private T markedMergeTarget
= null;
81 private HashMap
<T
, Set
<T
>> mergedEntities
= new HashMap
<>();
83 private IEntityCreator
<T
> entityCreator
;
84 private final ConversationHolder conversation
;
86 private Job searchJob
;
88 public AbstractBulkEditorInput() {
90 this.conversation
= CdmStore
.createConversation();
93 static public AbstractBulkEditorInput
NewInstance(BulkEditorInputType inputType
) {
95 return BulkEditorInputType
.getInput(inputType
);
98 public static AbstractBulkEditorInput
NewInstance(IdentifiableEntity entity
) {
100 BulkEditorInputType inputType
= BulkEditorInputType
.getByType(entity
.getClass());
102 AbstractBulkEditorInput editorInput
= NewInstance(inputType
);
104 editorInput
.setEntityUuid(entity
.getUuid());
109 public abstract String
getName();
111 public String
getEditorName(){
115 protected int getPageSize(){
119 protected abstract List
<T
> listEntities(IIdentifiableEntityServiceConfigurator configurator
);
121 protected abstract long countEntities(IIdentifiableEntityServiceConfigurator configurator
);
123 protected abstract T
loadEntity(UUID entityUuid
);
125 public List
<String
> getPropertyKeys(){
126 List
<String
> properties
= new ArrayList
<>();
127 properties
.add(getName());
128 properties
.add(PROPERTY_PROTECTED_TITLECACHE
);
129 properties
.addAll(getPropertyKeys_internal());
130 properties
.add(TYPE_PROPERTY
);
131 properties
.add(ID_PROPERTY
);
132 properties
.add(UUID_PROPERTY
);
136 protected abstract List
<String
> getPropertyKeys_internal();
139 public Object
getPropertyValue(T cdmBase
, String property
){
140 if(property
.equals(getName())){
141 return getText(cdmBase
);
143 else if(property
.equals(PROPERTY_PROTECTED_TITLECACHE
)
144 &&cdmBase
.isInstanceOf(IdentifiableEntity
.class)){
145 return HibernateProxyHelper
.deproxy(cdmBase
, IdentifiableEntity
.class).isProtectedTitleCache();
147 else if(property
.equals(TYPE_PROPERTY
)){
148 return getTypeText(cdmBase
);
150 else if(property
.equals(UUID_PROPERTY
)){
151 return cdmBase
.getUuid();
153 else if(property
.equals(ID_PROPERTY
)){
154 return cdmBase
.getId();
159 public boolean isBooleanProperty(String property
) {
160 if(property
.equals(PROPERTY_PROTECTED_TITLECACHE
)){
166 public boolean isCacheProperty(String property
) {
167 if(property
.equals(PROPERTY_PROTECTED_TITLECACHE
)){
173 public Comparator
<T
> getTitleComparator(){
174 return new TitleCacheComparator();
177 public void setMergeTarget(T t
){
178 markedMergeTarget
= t
;
181 public Set
<T
> getMergeCandidates() {
182 return markedMergeCandidates
;
185 public T
getMergeTarget() {
186 return markedMergeTarget
;
189 public void removeMergeTarget(){
190 markedMergeTarget
= null;
193 public void addMergeCandidate(T t
){
194 markedMergeCandidates
.add(t
);
197 public void removeMergeCandidate(T t
){
198 markedMergeCandidates
.remove(t
);
201 public void addToDelete(T t
, DeleteConfiguratorBase config
) {
202 toDelete
.put(t
, config
);
204 public void addSaveCandidate(T t
){
205 saveCandidates
.add(t
);
207 private void setEntityUuid(UUID entityUuid
){
208 this.entityUuid
= entityUuid
;
211 public UUID
getEntityUuid() {
215 public void performSearch(final BulkEditorQuery bulkEditorQuery
, IStructuredSelection selection
) {
216 //cancel previous search job
217 if(searchJob
!=null && searchJob
.getState()!=Job
.NONE
){
221 * wait for a little while for the job to finish
222 * to avoid asynchronously loaded results of the
223 * previous search being shown in the next search
224 * (not critical but explicitly waiting for the job to finish
225 * could run into an endless loop by mistake)
229 } catch (InterruptedException e
) {
233 markedMergeCandidates
.clear();
234 markedMergeTarget
= null;
236 if(getEntityUuid() != null){
237 T entity
= loadEntity(getEntityUuid());
240 else if(bulkEditorQuery
!= null){
241 IIdentifiableEntityServiceConfigurator configurator
= bulkEditorQuery
.getSearchConfigurator();
243 // check for UUID search
244 String titleSearchString
= configurator
.getTitleSearchString();
246 UUID uuid
= UUID
.fromString(titleSearchString
);
247 T entity
= loadEntity(uuid
);
248 //UUID search found -> add entity to list and return
251 } catch (IllegalArgumentException e
) {
252 // search string was no UUID
254 //-> continue with standard search
256 int pageSize
= configurator
.getPageSize()!=null?configurator
.getPageSize():getPageSize();
257 configurator
.setPageSize(pageSize
);
258 long count
= countEntities(configurator
);
259 int totalWork
= count
>Integer
.MAX_VALUE?Integer
.MAX_VALUE
:(int)count
;
260 String jobLabel
= String
.format(Messages
.AbstractBulkEditorInput_LOADING
, getName(), bulkEditorQuery
.getSearchString());
261 searchJob
= Job
.create(jobLabel
, (ICoreRunnable
) monitor
-> {
262 monitor
.beginTask(jobLabel
, totalWork
);
266 //load previously selected element
267 UUID selectedUuid
= null;
268 if(selection
!=null && selection
.getFirstElement() instanceof CdmBase
){
269 selectedUuid
= ((CdmBase
) selection
.getFirstElement()).getUuid();
270 T entity
= loadEntity(selectedUuid
);
275 if (monitor
.isCanceled()) {
278 configurator
.setPageNumber(pageNumber
);
279 entities
= listEntities(configurator
);
281 addToModel(entities
, selectedUuid
);
284 monitor
.worked(pageSize
);
285 long workedLong
= pageSize
*pageNumber
;
286 int loadedCount
= workedLong
>Integer
.MAX_VALUE?Integer
.MAX_VALUE
:(int)workedLong
;
287 monitor
.setTaskName(String
.format(Messages
.AbstractBulkEditorInput_LOADED
, loadedCount
, totalWork
, getName()));
290 EventUtility
.postAsyncEvent(WorkbenchEventConstants
.BULK_EDITOR_SEARCH_FINISHED
, selection
);
291 } while (!entities
.isEmpty());
293 EventUtility
.postAsyncEvent(WorkbenchEventConstants
.BULK_EDITOR_SEARCH_FINISHED
, selection
);
295 searchJob
.schedule();
299 private void addToModel(List
<T
> entities
, UUID selectedUuid
){
300 //filter pre-loaded previously selected element
301 if(selectedUuid
!=null){
302 entities
= entities
.stream().filter(entity
->!entity
.getUuid().equals(selectedUuid
)).collect(Collectors
.toList());
306 * Entities have to be loaded into the main session because they are
307 * loaded in a parallel asynchronous thread
309 if(getCdmEntitySession()!=null){//is null when closing the bulk editor during loading
310 getCdmEntitySession().load(entities
, true);
312 model
.addAll(entities
);
315 public boolean isMergingEnabled() {
319 public boolean isConvertingEnabled() {
323 public boolean isMarkerTypeEditingEnabled(MarkerType markerType
) {
330 public boolean merge(T entity
, T mergeTarget
) {
331 if (entity
instanceof IMergable
) {
333 CdmStore
.getCommonService().merge(mergeTarget
.getUuid(), entity
.getUuid(), (Class
<?
extends CdmBase
>)entity
.getClass());
334 } catch (MergeException e
) {
335 MessagingUtils
.errorDialog(Messages
.AbstractBulkEditorInput_MERGE_ERROR_TITLE
,
337 String
.format(Messages
.AbstractBulkEditorInput_MERGE_ERROR_MESSAGE
, entity
.getClass().getName()),
338 TaxeditorBulkeditorPlugin
.PLUGIN_ID
,
346 public void saveModel(){
350 public void saveModel(boolean resetMerge
){
352 for(Entry
<T
, DeleteConfiguratorBase
> entry
:toDelete
.entrySet()){
354 delete(entry
.getKey(), entry
.getValue());
355 } catch (ReferencedObjectUndeletableException e
) {
359 if (!saveCandidates
.isEmpty()){
360 CdmStore
.getService(saveCandidates
.iterator().next()).merge(new ArrayList
<>(saveCandidates
), true);
364 for(T mergeTarget
:mergedEntities
.keySet()){
365 for (T mergeCandidate
: mergedEntities
.get(mergeTarget
)){
366 merge(mergeCandidate
, mergeTarget
);
371 saveCandidates
.clear();
372 mergedEntities
.clear();
377 public T
create(T entity
) {
381 public IEntityCreator
<T
> getEntityCreator(){
382 if(entityCreator
== null){
383 entityCreator
= createEntityCreator();
385 return entityCreator
;
388 protected abstract IEntityCreator
<T
> createEntityCreator();
391 * The default implementation returns an empty list of sort providers.
394 public List
<IBulkEditorSortProvider
<T
>> getSortProviders(){
395 List
<IBulkEditorSortProvider
<T
>> sortProviders
= new ArrayList
<IBulkEditorSortProvider
<T
>>();
397 sortProviders
.add(new CdmBaseSortProvider
<T
>());
399 return sortProviders
;
403 * Returns a textual representation given object. The default implementation
404 * in the abstract base class returns the simple name of the class, this may
405 * be overwritten to something more specific in subclasses.
408 * @return a textual representation given object.
410 public String
getTypeText(Object entity
){
411 return entity
.getClass().getSimpleName();
414 public String
getText(T entity
) {
415 if(entity
.isInstanceOf(IdentifiableEntity
.class)){
416 IdentifiableEntity identifiableEntity
= HibernateProxyHelper
.deproxy(entity
, IdentifiableEntity
.class);
417 String text
= identifiableEntity
.getTitleCache();
421 return "No text. Implement in subclass"; //$NON-NLS-1$
424 public BasicEventList
<T
> getModel() {
428 public boolean replaceInModel(T entity
) {
429 int index
= model
.indexOf(entity
);
431 model
.set(index
, entity
);
439 public List
<T
> getRootEntities() {
445 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
446 // TODO Auto-generated method stub
450 public ConversationHolder
getConversation() {
454 public Set
<T
> getSaveCandidates() {
455 return saveCandidates
;
458 public HashMap
<T
, Set
<T
>> getMergedEntities() {
459 return mergedEntities
;
462 public void setMergedEntities(HashMap
<T
, Set
<T
>> mergedEntities
) {
463 this.mergedEntities
= mergedEntities
;