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
;
24 import ca
.odell
.glazedlists
.BasicEventList
;
25 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
26 import eu
.etaxonomy
.cdm
.api
.service
.config
.DeleteConfiguratorBase
;
27 import eu
.etaxonomy
.cdm
.api
.service
.config
.IIdentifiableEntityServiceConfigurator
;
28 import eu
.etaxonomy
.cdm
.api
.service
.exception
.ReferencedObjectUndeletableException
;
29 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
30 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
31 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
32 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
33 import eu
.etaxonomy
.cdm
.strategy
.merge
.IMergable
;
34 import eu
.etaxonomy
.cdm
.strategy
.merge
.MergeException
;
35 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.IEntityCreator
;
36 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.IEntityPersistenceService
;
37 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditorQuery
;
38 import eu
.etaxonomy
.taxeditor
.bulkeditor
.IBulkEditorSortProvider
;
39 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.sortprovider
.CdmBaseSortProvider
;
40 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.sortprovider
.TitleCacheComparator
;
41 import eu
.etaxonomy
.taxeditor
.bulkeditor
.internal
.TaxeditorBulkeditorPlugin
;
42 import eu
.etaxonomy
.taxeditor
.editor
.CdmEntitySessionInput
;
43 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
44 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
45 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
53 public abstract class AbstractBulkEditorInput
<T
extends CdmBase
> extends CdmEntitySessionInput
implements
54 IEntityPersistenceService
<T
> {
56 private UUID entityUuid
;
58 private BasicEventList
<T
> model
= new BasicEventList
<>();
60 private Map
<T
, DeleteConfiguratorBase
> toDelete
= new HashMap
<>();
61 private List
<T
> saveCandidates
= new ArrayList
<>();
64 private Set
<T
> markedMergeCandidates
= new HashSet
<>();
65 private T markedMergeTarget
= null;
67 private HashMap
<T
, Set
<T
>> mergedEntities
= new HashMap
<>();
69 private IEntityCreator
<T
> entityCreator
;
70 private final ConversationHolder conversation
;
72 private Job searchJob
;
74 public AbstractBulkEditorInput() {
76 this.conversation
= CdmStore
.createConversation();
79 static public AbstractBulkEditorInput
NewInstance(BulkEditorInputType inputType
) {
81 return BulkEditorInputType
.getInput(inputType
);
84 public static AbstractBulkEditorInput
NewInstance(IdentifiableEntity entity
) {
86 BulkEditorInputType inputType
= BulkEditorInputType
.getByType(entity
.getClass());
88 AbstractBulkEditorInput editorInput
= NewInstance(inputType
);
90 editorInput
.setEntityUuid(entity
.getUuid());
95 public abstract String
getName();
97 public String
getEditorName(){
101 protected int getPageSize(){
105 protected abstract List
<T
> listEntities(IIdentifiableEntityServiceConfigurator configurator
);
107 protected abstract long countEntities(IIdentifiableEntityServiceConfigurator configurator
);
109 protected abstract T
loadEntity(UUID entityUuid
);
111 public void setMergeTarget(T t
){
112 markedMergeTarget
= t
;
115 public Set
<T
> getMergeCandidates() {
116 return markedMergeCandidates
;
119 public T
getMergeTarget() {
120 return markedMergeTarget
;
123 public void removeMergeTarget(){
124 markedMergeTarget
= null;
127 public void addMergeCandidate(T t
){
128 markedMergeCandidates
.add(t
);
131 public void removeMergeCandidate(T t
){
132 markedMergeCandidates
.remove(t
);
135 public void addToDelete(T t
, DeleteConfiguratorBase config
) {
136 toDelete
.put(t
, config
);
138 public void addSaveCandidate(T t
){
139 saveCandidates
.add(t
);
141 private void setEntityUuid(UUID entityUuid
){
142 this.entityUuid
= entityUuid
;
145 public UUID
getEntityUuid() {
149 public void performSearch(final BulkEditorQuery bulkEditorQuery
) {
150 //cancel previous search job
151 if(searchJob
!=null && searchJob
.getState()!=Job
.NONE
){
157 if(getEntityUuid() != null){
159 T entity
= loadEntity(getEntityUuid());
162 else if(bulkEditorQuery
!= null){
164 IIdentifiableEntityServiceConfigurator configurator
= bulkEditorQuery
.getSearchConfigurator();
165 Comparator queryComparator
= (bulkEditorQuery
.getComparator() != null) ? bulkEditorQuery
.getComparator() : new TitleCacheComparator();
168 int pageSize
= configurator
.getPageSize()!=null?configurator
.getPageSize():getPageSize();
169 long count
= countEntities(configurator
);
170 int totalWork
= count
>Integer
.MAX_VALUE?Integer
.MAX_VALUE
:(int)count
;
171 String jobLabel
= String
.format(Messages
.AbstractBulkEditorInput_LOADING
, getName(), bulkEditorQuery
.getSearchString());
172 searchJob
= Job
.create(jobLabel
, (ICoreRunnable
) monitor
-> {
173 monitor
.beginTask(jobLabel
, totalWork
);
177 if (monitor
.isCanceled()) {
180 configurator
.setPageNumber(pageNumber
);
181 entities
= listEntities(configurator
);
182 model
.addAll(entities
);
184 monitor
.worked(pageSize
);
185 long workedLong
= pageSize
*pageNumber
;
186 int loadedCount
= workedLong
>Integer
.MAX_VALUE?Integer
.MAX_VALUE
:(int)workedLong
;
187 monitor
.setTaskName(String
.format(Messages
.AbstractBulkEditorInput_LOADED
, loadedCount
, totalWork
, getName()));
188 } while (!entities
.isEmpty());
191 searchJob
.setUser(true);
192 searchJob
.schedule();
196 public boolean isMergingEnabled() {
200 public boolean isConvertingEnabled() {
204 public boolean isMarkerTypeEditingEnabled(MarkerType markerType
) {
211 public boolean merge(T entity
, T mergeTarget
) {
212 if (entity
instanceof IMergable
) {
214 CdmStore
.getCommonService().merge(mergeTarget
.getUuid(), entity
.getUuid(), (Class
<?
extends CdmBase
>)entity
.getClass());
215 } catch (MergeException e
) {
216 MessagingUtils
.errorDialog(Messages
.AbstractBulkEditorInput_MERGE_ERROR_TITLE
,
218 String
.format(Messages
.AbstractBulkEditorInput_MERGE_ERROR_MESSAGE
, entity
.getClass().getName()),
219 TaxeditorBulkeditorPlugin
.PLUGIN_ID
,
227 public void saveModel(){
231 public void saveModel(boolean resetMerge
){
233 for(Entry
<T
, DeleteConfiguratorBase
> entry
:toDelete
.entrySet()){
235 delete(entry
.getKey(), entry
.getValue());
236 } catch (ReferencedObjectUndeletableException e
) {
241 if (!saveCandidates
.isEmpty()){
242 CdmStore
.getService(saveCandidates
.get(0)).merge(saveCandidates
, true);
246 for(T mergeTarget
:mergedEntities
.keySet()){
247 for (T mergeCandidate
: mergedEntities
.get(mergeTarget
)){
248 merge(mergeCandidate
, mergeTarget
);
251 mergedEntities
.clear();
259 public T
create(T entity
) {
263 public IEntityCreator
<T
> getEntityCreator(){
264 if(entityCreator
== null){
265 entityCreator
= createEntityCreator();
267 return entityCreator
;
270 protected abstract IEntityCreator
<T
> createEntityCreator();
273 * The default implementation returns an empty list of sort providers.
276 public List
<IBulkEditorSortProvider
<T
>> getSortProviders(){
277 List
<IBulkEditorSortProvider
<T
>> sortProviders
= new ArrayList
<IBulkEditorSortProvider
<T
>>();
279 sortProviders
.add(new CdmBaseSortProvider
<T
>());
281 return sortProviders
;
285 * Returns a textual representation given object. The default implementation
286 * in the abstract base class returns the simple name of the class, this may
287 * be overwritten to something more specific in subclasses.
290 * @return a textual representation given object.
292 public String
getTypeText(Object entity
){
293 return entity
.getClass().getSimpleName();
296 public String
getText(T entity
) {
297 if(entity
instanceof IdentifiableEntity
){
298 IdentifiableEntity identifiableEntity
= (IdentifiableEntity
) HibernateProxyHelper
.deproxy(entity
);
299 String text
= ""; //$NON-NLS-1$
300 if(markedMergeCandidates
.contains(entity
)){
301 text
+= Messages
.AbstractBulkEditorInput_CANDIDATE
;
303 else if(markedMergeTarget
==entity
){
304 text
+= Messages
.AbstractBulkEditorInput_TARGET
;
306 text
+= identifiableEntity
.getTitleCache();
310 return "No text. Implement in subclass"; //$NON-NLS-1$
313 public BasicEventList
<T
> getModel() {
317 protected boolean replaceInModel(T entity
) {
318 int index
= model
.indexOf(entity
);
320 model
.set(index
, entity
);
328 public List
<T
> getRootEntities() {
334 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
335 // TODO Auto-generated method stub
339 public ConversationHolder
getConversation() {
343 public List
<T
> getSaveCandidates() {
344 return saveCandidates
;
350 public void resetSaveCandidates() {
351 this.saveCandidates
.clear();
355 public HashMap
<T
, Set
<T
>> getMergedEntities() {
356 return mergedEntities
;
359 public void setMergedEntities(HashMap
<T
, Set
<T
>> mergedEntities
) {
360 this.mergedEntities
= mergedEntities
;