cb64399c7b6ce320a7164236a5c3abdd745469f3
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / e4 / TaxonEditorInputE4.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.taxeditor.editor.e4;
11
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Map.Entry;
20 import java.util.Set;
21 import java.util.UUID;
22
23 import org.eclipse.core.commands.ExecutionException;
24 import org.eclipse.core.commands.operations.IOperationHistory;
25 import org.eclipse.core.runtime.IAdaptable;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.Status;
28 import org.eclipse.e4.ui.di.UISynchronize;
29 import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
30
31 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
32 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
33 import eu.etaxonomy.cdm.api.service.IClassificationService;
34 import eu.etaxonomy.cdm.api.service.INameService;
35 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
36 import eu.etaxonomy.cdm.api.service.ITaxonService;
37 import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
38 import eu.etaxonomy.cdm.api.service.config.TaxonBaseDeletionConfigurator;
39 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
40 import eu.etaxonomy.cdm.model.common.CdmBase;
41 import eu.etaxonomy.cdm.model.name.HybridRelationship;
42 import eu.etaxonomy.cdm.model.name.TaxonName;
43 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
44 import eu.etaxonomy.cdm.model.taxon.Synonym;
45 import eu.etaxonomy.cdm.model.taxon.Taxon;
46 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
47 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
48 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
49 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
50 import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
51 import eu.etaxonomy.taxeditor.editor.ChooseFromMultipleAcceptedTaxaDialog;
52 import eu.etaxonomy.taxeditor.editor.ChooseFromMultipleTaxonNodesDialog;
53 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
54 import eu.etaxonomy.taxeditor.model.AbstractUtility;
55 import eu.etaxonomy.taxeditor.model.DataChangeBridge;
56 import eu.etaxonomy.taxeditor.model.MessagingUtils;
57 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
58 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
59 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
60 import eu.etaxonomy.taxeditor.store.CdmStore;
61 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
62
63
64 /**
65 *
66 * @author pplitzner
67 * @date Aug 24, 2017
68 *
69 */
70 public class TaxonEditorInputE4 extends CdmEntitySessionInput implements IConversationEnabled {
71
72 private static final String INCORRECT_STATE = Messages.TaxonEditorInput_INCORRECT_STATE;
73
74 private final ConversationHolder conversation;
75
76 private TaxonNode taxonNode;
77
78 private Map<TaxonBase, TaxonBaseDeletionConfigurator> toDeletes = new HashMap<>();
79 private Set<Synonym> toSaveNewSynonyms = new HashSet<>();
80 private List<TaxonBase> toSaveNewConcepts = new ArrayList<>();
81 private List<TaxonName> toSaveNewNames = new ArrayList<>();
82
83
84 private Set<AbstractPostOperation> operations = new HashSet<>();
85
86 private TaxonEditorInputDataChangeBehaviourE4 dataChangeBehavior;
87
88 private TaxonBase<?> initiallySelectedTaxonBase;
89
90 private UISynchronize sync;
91
92
93 public void setSync(UISynchronize sync) {
94 this.sync = sync;
95 }
96
97 private enum CdmType {
98 TAXON_NODE,
99 TAXON_BASE,
100 PARENT_TAXON_NODE
101 }
102
103 private TaxonEditorInputE4(UUID uuid, CdmType type) {
104 super(true);
105 this.conversation = CdmStore.createConversation();
106 switch(type) {
107 case PARENT_TAXON_NODE:
108 initForParentTaxonNode(uuid);
109 break;
110 case TAXON_BASE:
111 initForTaxonBase(uuid);
112 break;
113 case TAXON_NODE:
114 initForTaxonNode(uuid);
115 break;
116 }
117 }
118
119 private void init(TaxonNode taxonNode) {
120 this.taxonNode = taxonNode;
121 }
122
123
124 private void initForTaxonNode(UUID taxonNodeUuid) {
125 this.getCdmEntitySession().bind();
126 TaxonNode taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNodeUuid, getTaxonNodePropertyPaths());
127 // TaxonNode taxonNode = getCdmEntitySession().remoteLoad(CdmStore.getService(ITaxonNodeService.class), taxonNodeUuid, getTaxonNodePropertyPaths());
128 if(taxonNode == null){
129 MessagingUtils.warningDialog(Messages.TaxonEditorInput_NOT_IMPLEMENTED, TaxonEditorInputE4.class, Messages.TaxonEditorInput_NOT_IMPLEMENTED_MESSAGE);
130 }
131 init(taxonNode);
132 setInitiallySelectedTaxonBase(getTaxon());
133
134 }
135
136 private void initForTaxonBase(UUID taxonBaseUuid) {
137 this.getCdmEntitySession().bind();
138 // TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).load(taxonBaseUuid, getTaxonBasePropertyPaths());
139 TaxonBase taxonBase = getCdmEntitySession().remoteLoad(CdmStore.getService(ITaxonService.class), taxonBaseUuid, getTaxonBasePropertyPaths());
140 if (taxonBase != null){
141 if(taxonBase.isInstanceOf(Taxon.class)){
142 Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
143 setInitiallySelectedTaxonBase(taxon);
144
145 if ( taxon.isMisapplication() || taxon.isProparteSynonym()){
146 // TODO get accepted taxon
147 MessagingUtils.info(Messages.TaxonEditorInput_OPEN_MISSAPPLIED_NAME);
148
149 Set<Taxon> acceptedTaxa = new HashSet<Taxon>();
150 Set<TaxonRelationship> relations = taxon.getRelationsFromThisTaxon();
151 for(TaxonRelationship relation : relations){
152 if(relation.getType().isAnyMisappliedName() || relation.getType().isAnySynonym()){
153 acceptedTaxa.add(relation.getToTaxon());
154 }
155 }
156 if (taxon.getTaxonNodes().size() > 0){
157 acceptedTaxa.add(taxon);
158 }
159 setInputForRelatedTaxa(conversation, acceptedTaxa);
160
161 }else{
162 setInputForMultipleNodes(conversation, taxon.getTaxonNodes());
163 }
164 }else if(taxonBase instanceof Synonym){
165 Synonym synonym = (Synonym) taxonBase;
166
167 Set<Taxon> taxa = new HashSet<>();
168 Taxon taxon = synonym.getAcceptedTaxon();
169 if (taxon != null){
170 taxa.add(taxon);
171 }
172 setInputForMultipleTaxa(conversation, taxa);
173 setInitiallySelectedTaxonBase(synonym);
174 }
175 }
176 }
177
178
179 private void initForParentTaxonNode(UUID parentNodeUuid){
180 this.getCdmEntitySession().bind();
181 TaxonName name = PreferencesUtil.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
182 ITaxonTreeNode parentNode = CdmStore.getService(IClassificationService.class).getTreeNodeByUuid(parentNodeUuid);
183
184 Taxon newTaxon = Taxon.NewInstance(name, parentNode.getReference());
185 TaxonNode newTaxonNode = parentNode.addChildTaxon(newTaxon, parentNode.getReference(), parentNode.getMicroReference());
186
187 // add the new taxon to the editors persistence context
188 UUID newTaxonNodeUuid = CdmStore.getService(ITaxonNodeService.class).save(newTaxonNode).getUuid();
189
190 initForTaxonNode(newTaxonNodeUuid);
191 }
192
193 private void setInputForMultipleNodes(ConversationHolder conversation, Set<TaxonNode> taxonNodes){
194 if(taxonNodes.size() == 1){
195 TaxonNode taxonNode = taxonNodes.iterator().next();
196 taxonNode = getCdmEntitySession().remoteLoad(CdmStore.getService(ITaxonNodeService.class), taxonNode.getUuid(), getTaxonNodePropertyPaths());
197 init(taxonNode);
198 }else if(taxonNodes.size() > 1){
199
200 TaxonNode taxonNode = ChooseFromMultipleTaxonNodesDialog.choose(taxonNodes);
201 if(taxonNode != null){
202 taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid(), getTaxonNodePropertyPaths());
203 }
204 if(taxonNode != null){
205 init(taxonNode);
206 }
207 } else if (taxonNodes.size() == 0) {
208 // this is an undesired state
209 MessagingUtils.warningDialog(INCORRECT_STATE,TaxonEditorInputE4.class,Messages.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION);
210 }
211 }
212
213 private void setInputForMultipleTaxa(ConversationHolder conversation, Set<Taxon> taxa){
214 if(taxa.size() == 1){
215 Taxon taxon = taxa.iterator().next();
216 Set<TaxonNode> nodes = taxon.getTaxonNodes();
217 setInputForMultipleNodes(conversation, nodes);
218 }else if(taxa.size() > 1){
219 Set<TaxonNode> taxonNodes = new HashSet<TaxonNode>();
220 for ( Taxon taxon : taxa ){
221 taxonNodes.addAll(taxon.getTaxonNodes());
222 }
223 setInputForMultipleNodes(conversation, taxonNodes);
224 }else if(taxa.size() == 0){
225 // this is an undesired state
226 MessagingUtils.warningDialog(INCORRECT_STATE, TaxonEditorInputE4.class, Messages.TaxonEditorInput_NO_ACCEPTED_TAXON_PRESENT);
227 }
228 }
229
230 private void setInputForRelatedTaxa(ConversationHolder conversation, Set<Taxon> taxa){
231 if(taxa.size() == 1){
232 Taxon taxon = taxa.iterator().next();
233 Set<TaxonNode> nodes = taxon.getTaxonNodes();
234 TaxonNode taxonNode = null;
235 if (nodes.size()>1){
236 taxonNode = ChooseFromMultipleTaxonNodesDialog.choose(nodes);
237 }else if (nodes.size()==1){
238 taxonNode = nodes.iterator().next();
239 }else{
240 MessagingUtils.warningDialog(INCORRECT_STATE,TaxonEditorInputE4.class,Messages.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION);
241 }
242 init(taxonNode);
243 }else if(taxa.size() > 1){
244 Iterator<Taxon> taxonIterator = taxa.iterator();
245 Set<TaxonNode> nodes = new HashSet<>();
246 while (taxonIterator.hasNext()){
247
248 nodes.addAll(taxonIterator.next().getTaxonNodes());
249 }
250 TaxonNode taxonNode = ChooseFromMultipleAcceptedTaxaDialog.choose(nodes);
251 if(taxonNode != null){
252 taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid(), getTaxonNodePropertyPaths());
253 }
254 if(taxonNode != null){
255 init(taxonNode);
256 }
257 } else if (taxa.size() == 0) {
258 // this is an undesired state
259 MessagingUtils.warningDialog(INCORRECT_STATE,TaxonEditorInputE4.class,Messages.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION);
260 }
261 }
262
263 public static TaxonEditorInputE4 NewInstance(UUID taxonNodeUuid) {
264 return new TaxonEditorInputE4(taxonNodeUuid, CdmType.TAXON_NODE);
265
266 }
267
268 public static TaxonEditorInputE4 NewInstanceFromTaxonBase(UUID taxonBaseUuid){
269 return new TaxonEditorInputE4(taxonBaseUuid, CdmType.TAXON_BASE);
270 }
271
272
273 public static TaxonEditorInputE4 NewEmptyInstance(UUID parentNodeUuid){
274 return new TaxonEditorInputE4(parentNodeUuid, CdmType.PARENT_TAXON_NODE);
275 }
276
277 public Object getAdapter(Class adapter) {
278
279 if (adapter == Taxon.class) {
280 return getTaxon();
281 }
282
283 if (adapter == TaxonNode.class) {
284 return taxonNode;
285 }
286
287 return null;
288 }
289
290 /**
291 * {@inheritDoc}
292 *
293 * Overrides equals to ensure that a taxon can only be edited by
294 * one editor at a time.
295 */
296 @Override
297 public boolean equals(Object obj) {
298 if (TaxonEditorInputE4.class.equals(obj.getClass())
299 && getTaxon() != null
300 && getTaxon().equals(((TaxonEditorInputE4) obj).getTaxon())) {
301 if (((TaxonEditorInputE4) obj).getInitiallySelectedTaxonBase() != null){
302 setInitiallySelectedTaxonBase(((TaxonEditorInputE4) obj).getInitiallySelectedTaxonBase());
303 }
304 return true;
305 }
306 return false;
307 }
308
309 public Taxon getTaxon(){
310 if(taxonNode!=null){
311 Taxon taxon = CdmBase.deproxy(taxonNode.getTaxon(), Taxon.class);
312 return taxon;
313 }
314 return null;
315 }
316
317 public TaxonNode getTaxonNode() {
318 return taxonNode;
319 }
320
321 @Override
322 public ConversationHolder getConversationHolder() {
323 return conversation;
324 }
325
326 /** {@inheritDoc} */
327 @Override
328 public void update(CdmDataChangeMap events) {
329 if(dataChangeBehavior == null){
330 dataChangeBehavior = new TaxonEditorInputDataChangeBehaviourE4(this);
331 }
332
333 DataChangeBridge.handleDataChange(events, dataChangeBehavior);
334 }
335
336 public void setInitiallySelectedTaxonBase(TaxonBase taxonBase) {
337 this.initiallySelectedTaxonBase = taxonBase;
338 }
339
340 public TaxonBase getInitiallySelectedTaxonBase() {
341 return initiallySelectedTaxonBase;
342 }
343
344 public Set<AbstractPostOperation> getOperations() {
345 return operations;
346 }
347
348 public void setOperations(Set<AbstractPostOperation> operations) {
349 this.operations = operations;
350 }
351
352 public void addOperation(AbstractPostOperation operation) {
353 this.operations.add(operation);
354 }
355
356 public void addToSaveNewSynonym(Synonym toSaveNew) {
357 this.toSaveNewSynonyms.add(toSaveNew);
358 }
359 public void addToSaveNewConcept(Taxon toSaveNew) {
360 this.toSaveNewConcepts.add(toSaveNew);
361
362 }
363
364 @Override
365 public List<TaxonNode> getRootEntities() {
366 return Arrays.asList(taxonNode);
367 }
368
369 @Override
370 public void merge() {
371 if (!this.getCdmEntitySession().isActive()){
372 this.getCdmEntitySession().bind();
373 }
374 for(Entry<TaxonBase, TaxonBaseDeletionConfigurator> entry:toDeletes.entrySet()){
375 delete(entry.getKey(), entry.getValue());
376 }
377 toDeletes.clear();
378 for(AbstractPostOperation entry:operations){
379 IStatus status = Status.CANCEL_STATUS;
380 final IAdaptable uiInfoAdapter = WorkspaceUndoUtil
381 .getUIInfoAdapter(AbstractUtility.getShell());
382 String operationlabel = entry.getLabel();
383 try {
384 entry.addContext(IOperationHistory.GLOBAL_UNDO_CONTEXT);
385 status = entry.execute(null, uiInfoAdapter);
386 } catch (ExecutionException e) {
387
388 MessagingUtils.operationDialog(AbstractUtility.class, e, TaxeditorStorePlugin.PLUGIN_ID, operationlabel, null);
389
390 }
391
392 String statusString = status.equals(Status.OK_STATUS) ? "completed"
393 : "cancelled";
394
395 IPostOperationEnabled postOperationEnabled = entry
396 .getPostOperationEnabled();
397 if (postOperationEnabled != null) {
398 postOperationEnabled.onComplete();
399 }
400
401
402 //AbstractUtility.executeOperation(entry,sync);
403 }
404
405 operations.clear();
406 for (Synonym syn: toSaveNewSynonyms){
407 for (HybridRelationship rel : syn.getName().getHybridChildRelations()){
408 // if (!rel.getParentName().isPersited()) {
409 toSaveNewNames.add(rel.getParentName());
410 // }
411 // if (!rel.getHybridName().isPersited()) {
412 toSaveNewNames.add(rel.getHybridName());
413 // }
414
415 }
416 }
417
418 for (TaxonBase concept: toSaveNewConcepts){
419 for (HybridRelationship rel : concept.getName().getHybridChildRelations()){
420 // if (!rel.getParentName().isPersited()) {
421 toSaveNewNames.add(rel.getParentName());
422 // }
423 // if (!rel.getHybridName().isPersited()) {
424 toSaveNewNames.add(rel.getHybridName());
425 // }
426
427 }
428 }
429 CdmStore.getService(INameService.class).merge(toSaveNewNames, true);
430 CdmStore.getService(ITaxonService.class).merge(toSaveNewConcepts, true);
431
432 toSaveNewNames.clear();
433 toSaveNewConcepts.clear();
434
435 CdmStore.getService(ITaxonNodeService.class).merge(taxonNode, true);
436
437
438 }
439
440 /**
441 * @param key
442 * @param value
443 */
444 private void delete(TaxonBase key, TaxonBaseDeletionConfigurator value) {
445 if (key instanceof Synonym){
446 CdmStore.getService(ITaxonService.class).deleteSynonym(((Synonym)key).getUuid(), (SynonymDeletionConfigurator) value);
447 }else{
448 CdmStore.getService(ITaxonService.class).deleteTaxon(((Taxon)key).getUuid(), (TaxonDeletionConfigurator) value, null);
449 }
450
451 }
452
453 @Override
454 public Map<Object, List<String>> getPropertyPathsMap() {
455 return null;
456 }
457
458 private List<String> getTaxonNodePropertyPaths() {
459 List<String> taxonNodePropertyPaths = new ArrayList<String>();
460 for(String propertyPath : getTaxonBasePropertyPaths()) {
461 taxonNodePropertyPaths.add("taxon." + propertyPath); //$NON-NLS-1$
462 }
463 return taxonNodePropertyPaths;
464 }
465
466 private List<String> getTaxonBasePropertyPaths() {
467 List<String> taxonBasePropertyPaths = Arrays.asList(new String[] {
468 "sec", //$NON-NLS-1$
469 "createdBy", //$NON-NLS-1$
470 "updatedBy", //$NON-NLS-1$
471 "annotations", //$NON-NLS-1$
472 "markers", //$NON-NLS-1$
473 "credits", //$NON-NLS-1$
474 "extensions", //$NON-NLS-1$
475 "rights", //$NON-NLS-1$
476 "sources", //$NON-NLS-1$
477 "identifiers",
478 "descriptions", //$NON-NLS-1$
479 "taxonNodes", //$NON-NLS-1$
480 "descriptions.descriptionElements.feature", //$NON-NLS-1$
481 "descriptions.descriptionElements.area", //$NON-NLS-1$
482 "descriptions.descriptionElements.status", //$NON-NLS-1$
483 "descriptions.markers", //$NON-NLS-1$
484 "name.descriptions", //$NON-NLS-1$
485 "name.typeDesignations.typeSpecimen.derivedFrom.originals", //$NON-NLS-1$
486 "name.status", //$NON-NLS-1$
487 "name.nomenclaturalReference.inReference", //$NON-NLS-1$
488 "name.taxonBases.taxonNodes", //$NON-NLS-1$
489 "name.relationsFromThisName", //$NON-NLS-1$
490 "name.relationsToThisName", //$NON-NLS-1$
491 "name.homotypicalGroup.typifiedNames.taxonBases.synonyms.synonym.name.status", //$NON-NLS-1$
492 "name.homotypicalGroup.typifiedNames.relationsToThisName.fromName", //$NON-NLS-1$
493 "synonyms.name.status.type", //$NON-NLS-1$
494 "synonyms.name.relationsToThisName.fromName", //$NON-NLS-1$
495 "synonyms.name.nomenclaturalReference.inReference.authorship", //$NON-NLS-1$
496 "synonyms.name.nomenclaturalReference.authorship", //$NON-NLS-1$
497 "synonyms.name.homotypicalGroup.typifiedNames.taxonBases.synonyms" //$NON-NLS-1$
498 });
499
500 return taxonBasePropertyPaths;
501 }
502
503 /**
504 * @param selectedElement
505 * @param deleteConfig
506 */
507 public void addTaxonBaseToDelete(TaxonBase selectedElement, TaxonBaseDeletionConfigurator deleteConfig) {
508 this.toDeletes.put(selectedElement, deleteConfig);
509
510 }
511
512 /**
513 * @param newName
514 */
515 public void addToSaveNewName(TaxonName newName) {
516 this.toSaveNewNames.add(newName);
517 }
518
519 }