fix creation of hybrids after deleting cascading in relationships
[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.List;
17 import java.util.Map;
18 import java.util.Map.Entry;
19 import java.util.Set;
20 import java.util.UUID;
21
22 import org.eclipse.e4.ui.di.UISynchronize;
23
24 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
25 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
26 import eu.etaxonomy.cdm.api.service.IClassificationService;
27 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
28 import eu.etaxonomy.cdm.api.service.ITaxonService;
29 import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
30 import eu.etaxonomy.cdm.api.service.config.TaxonBaseDeletionConfigurator;
31 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
32 import eu.etaxonomy.cdm.model.common.CdmBase;
33 import eu.etaxonomy.cdm.model.name.TaxonName;
34 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
35 import eu.etaxonomy.cdm.model.taxon.Synonym;
36 import eu.etaxonomy.cdm.model.taxon.Taxon;
37 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
38 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
39 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
40 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
41 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
42 import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
43 import eu.etaxonomy.taxeditor.editor.ChooseFromMultipleTaxonNodesDialog;
44 import eu.etaxonomy.taxeditor.editor.EditorUtil;
45 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
46 import eu.etaxonomy.taxeditor.model.DataChangeBridge;
47 import eu.etaxonomy.taxeditor.model.MessagingUtils;
48 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
49 import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
50 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
51 import eu.etaxonomy.taxeditor.store.CdmStore;
52
53
54 /**
55 *
56 * @author pplitzner
57 * @date Aug 24, 2017
58 *
59 */
60 public class TaxonEditorInputE4 extends CdmEntitySessionInput implements IConversationEnabled {
61
62 private static final String INCORRECT_STATE = Messages.TaxonEditorInput_INCORRECT_STATE;
63
64 private final ConversationHolder conversation;
65
66 private TaxonNode taxonNode;
67
68 private Map<TaxonBase, TaxonBaseDeletionConfigurator> toDelete = new HashMap<>();
69 private Set<Synonym> toSaveNewSynonym = new HashSet<>();
70 private Set<Taxon> toSaveNewConcept = new HashSet<>();
71
72
73 private Set<AbstractPostTaxonOperation> operations = new HashSet<>();
74
75 private TaxonEditorInputDataChangeBehaviourE4 dataChangeBehavior;
76
77 private TaxonBase initiallySelectedTaxonBase;
78
79 UISynchronize sync;
80
81
82 public void setSync(UISynchronize sync) {
83 this.sync = sync;
84 }
85
86 private enum CdmType {
87 TAXON_NODE,
88 TAXON_BASE,
89 PARENT_TAXON_NODE
90 }
91
92 private TaxonEditorInputE4(UUID uuid, CdmType type) {
93 super(true);
94 this.conversation = CdmStore.createConversation();
95 switch(type) {
96 case PARENT_TAXON_NODE:
97 initForParentTaxonNode(uuid);
98 break;
99 case TAXON_BASE:
100 initForTaxonBase(uuid);
101 break;
102 case TAXON_NODE:
103 initForTaxonNode(uuid);
104 break;
105 }
106 }
107
108 private void init(TaxonNode taxonNode) {
109 this.taxonNode = taxonNode;
110 }
111
112
113 private void initForTaxonNode(UUID taxonNodeUuid) {
114 this.getCdmEntitySession().bind();
115 TaxonNode taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNodeUuid, getTaxonNodePropertyPaths());
116 if(taxonNode == null){
117 MessagingUtils.warningDialog(Messages.TaxonEditorInput_NOT_IMPLEMENTED, TaxonEditorInputE4.class, Messages.TaxonEditorInput_NOT_IMPLEMENTED_MESSAGE);
118 }
119 init(taxonNode);
120 setInitiallySelectedTaxonBase(getTaxon());
121
122 }
123
124 private void initForTaxonBase(UUID taxonBaseUuid) {
125 this.getCdmEntitySession().bind();
126 TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).load(taxonBaseUuid, getTaxonBasePropertyPaths());
127 if (taxonBase != null){
128 if(taxonBase.isInstanceOf(Taxon.class)){
129 Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
130 setInitiallySelectedTaxonBase(taxon);
131
132 if (taxon.getTaxonNodes().size() == 0 && taxon.isMisapplication()){
133 // TODO get accepted taxon
134 MessagingUtils.info(Messages.TaxonEditorInput_OPEN_MISSAPPLIED_NAME);
135
136 Set<Taxon> acceptedTaxa = new HashSet<Taxon>();
137 Set<TaxonRelationship> relations = taxon.getRelationsFromThisTaxon();
138 for(TaxonRelationship relation : relations){
139 if(relation.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
140 acceptedTaxa.add(relation.getToTaxon());
141 }
142 }
143 setInputForMultipleTaxa(conversation, acceptedTaxa);
144
145 }else{
146 setInputForMultipleNodes(conversation, taxon.getTaxonNodes());
147 }
148 }else if(taxonBase instanceof Synonym){
149 Synonym synonym = (Synonym) taxonBase;
150
151 Set<Taxon> taxa = new HashSet<>();
152 Taxon taxon = synonym.getAcceptedTaxon();
153 if (taxon != null){
154 taxa.add(taxon);
155 }
156 setInputForMultipleTaxa(conversation, taxa);
157 setInitiallySelectedTaxonBase(synonym);
158 }
159 }
160 }
161
162
163 private void initForParentTaxonNode(UUID parentNodeUuid){
164 this.getCdmEntitySession().bind();
165 TaxonName name = PreferencesUtil.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
166 ITaxonTreeNode parentNode = CdmStore.getService(IClassificationService.class).getTreeNodeByUuid(parentNodeUuid);
167
168 Taxon newTaxon = Taxon.NewInstance(name, parentNode.getReference());
169 TaxonNode newTaxonNode = parentNode.addChildTaxon(newTaxon, parentNode.getReference(), parentNode.getMicroReference());
170
171 // add the new taxon to the editors persistence context
172 UUID newTaxonNodeUuid = CdmStore.getService(ITaxonNodeService.class).save(newTaxonNode).getUuid();
173
174 initForTaxonNode(newTaxonNodeUuid);
175 }
176
177 private void setInputForMultipleNodes(ConversationHolder conversation, Set<TaxonNode> taxonNodes){
178 if(taxonNodes.size() == 1){
179 TaxonNode taxonNode = taxonNodes.iterator().next();
180 taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid(), getTaxonNodePropertyPaths());
181 init(taxonNode);
182 }else if(taxonNodes.size() > 1){
183 TaxonNode taxonNode = ChooseFromMultipleTaxonNodesDialog.choose(taxonNodes);
184 taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid(), getTaxonNodePropertyPaths());
185 if(taxonNode != null){
186 init(taxonNode);
187 }
188 } else if (taxonNodes.size() == 0) {
189 // this is an undesired state
190 MessagingUtils.warningDialog(INCORRECT_STATE,TaxonEditorInputE4.class,Messages.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION);
191 }
192 }
193
194 private void setInputForMultipleTaxa(ConversationHolder conversation, Set<Taxon> taxa){
195 if(taxa.size() == 1){
196 Taxon taxon = taxa.iterator().next();
197 Set<TaxonNode> nodes = taxon.getTaxonNodes();
198 setInputForMultipleNodes(conversation, nodes);
199 }else if(taxa.size() > 1){
200 Set<TaxonNode> taxonNodes = new HashSet<TaxonNode>();
201 for ( Taxon taxon : taxa ){
202 taxonNodes.addAll(taxon.getTaxonNodes());
203 }
204 setInputForMultipleNodes(conversation, taxonNodes);
205 }else if(taxa.size() == 0){
206 // this is an undesired state
207 MessagingUtils.warningDialog(INCORRECT_STATE, TaxonEditorInputE4.class, Messages.TaxonEditorInput_NO_ACCEPTED_TAXON_PRESENT);
208 }
209 }
210
211 public static TaxonEditorInputE4 NewInstance(UUID taxonNodeUuid) {
212 return new TaxonEditorInputE4(taxonNodeUuid, CdmType.TAXON_NODE);
213
214 }
215
216 public static TaxonEditorInputE4 NewInstanceFromTaxonBase(UUID taxonBaseUuid){
217 return new TaxonEditorInputE4(taxonBaseUuid, CdmType.TAXON_BASE);
218 }
219
220
221 public static TaxonEditorInputE4 NewEmptyInstance(UUID parentNodeUuid){
222 return new TaxonEditorInputE4(parentNodeUuid, CdmType.PARENT_TAXON_NODE);
223 }
224
225 public Object getAdapter(Class adapter) {
226
227 if (adapter == Taxon.class) {
228 return getTaxon();
229 }
230
231 if (adapter == TaxonNode.class) {
232 return taxonNode;
233 }
234
235 return null;
236 }
237
238 /**
239 * {@inheritDoc}
240 *
241 * Overrides equals to ensure that a taxon can only be edited by
242 * one editor at a time.
243 */
244 @Override
245 public boolean equals(Object obj) {
246 if (TaxonEditorInputE4.class.equals(obj.getClass())
247 && getTaxon() != null
248 && getTaxon().equals(((TaxonEditorInputE4) obj).getTaxon())) {
249 if (((TaxonEditorInputE4) obj).getInitiallySelectedTaxonBase() != null){
250 setInitiallySelectedTaxonBase(((TaxonEditorInputE4) obj).getInitiallySelectedTaxonBase());
251 }
252 return true;
253 }
254 return false;
255 }
256
257 public Taxon getTaxon(){
258 if(taxonNode!=null){
259 Taxon taxon = CdmBase.deproxy(taxonNode.getTaxon(), Taxon.class);
260 return taxon;
261 }
262 return null;
263 }
264
265 public TaxonNode getTaxonNode() {
266 return taxonNode;
267 }
268
269 @Override
270 public ConversationHolder getConversationHolder() {
271 return conversation;
272 }
273
274 /** {@inheritDoc} */
275 @Override
276 public void update(CdmDataChangeMap events) {
277 if(dataChangeBehavior == null){
278 dataChangeBehavior = new TaxonEditorInputDataChangeBehaviourE4(this);
279 }
280
281 DataChangeBridge.handleDataChange(events, dataChangeBehavior);
282 }
283
284 public void setInitiallySelectedTaxonBase(TaxonBase taxonBase) {
285 this.initiallySelectedTaxonBase = taxonBase;
286 }
287
288 public TaxonBase getInitiallySelectedTaxonBase() {
289 return initiallySelectedTaxonBase;
290 }
291
292 public Set<AbstractPostTaxonOperation> getOperations() {
293 return operations;
294 }
295
296 public void setOperations(Set<AbstractPostTaxonOperation> operations) {
297 this.operations = operations;
298 }
299
300 public void addOperation(AbstractPostTaxonOperation operation) {
301 this.operations.add(operation);
302 }
303
304 public void addToSaveNewSynonym(Synonym toSaveNew) {
305 this.toSaveNewSynonym.add(toSaveNew);
306 }
307 public void addToSaveNewConcept(Taxon toSaveNew) {
308 this.toSaveNewConcept.add(toSaveNew);
309 }
310
311 @Override
312 public List<TaxonNode> getRootEntities() {
313 return Arrays.asList(taxonNode);
314 }
315
316 @Override
317 public void merge() {
318 if (!this.getCdmEntitySession().isActive()){
319 this.getCdmEntitySession().bind();
320 }
321 for(Entry<TaxonBase, TaxonBaseDeletionConfigurator> entry:toDelete.entrySet()){
322 delete(entry.getKey(), entry.getValue());
323 }
324 toDelete.clear();
325 for(AbstractPostOperation entry:operations){
326 EditorUtil.executeOperation(entry,sync);
327 }
328 operations.clear();
329
330 for (Synonym syn: toSaveNewSynonym){
331 CdmStore.getService(ITaxonService.class).createSynonym(taxonNode.getTaxon().getUuid(),syn);
332 }
333 // for (Taxon taxon: toSaveNewConcept){
334 // CdmStore.getService(ITaxonService.class).createConcept(taxonNode.getTaxon().getUuid(),taxon);
335 // }
336
337 CdmStore.getService(ITaxonNodeService.class).merge(taxonNode, true);
338
339 }
340
341 /**
342 * @param key
343 * @param value
344 */
345 private void delete(TaxonBase key, TaxonBaseDeletionConfigurator value) {
346 if (key instanceof Synonym){
347 CdmStore.getService(ITaxonService.class).deleteSynonym(((Synonym)key).getUuid(), (SynonymDeletionConfigurator) value);
348 }else{
349 CdmStore.getService(ITaxonService.class).deleteTaxon(((Taxon)key).getUuid(), (TaxonDeletionConfigurator) value, null);
350 }
351
352 }
353
354 @Override
355 public Map<Object, List<String>> getPropertyPathsMap() {
356 return null;
357 }
358
359 private List<String> getTaxonNodePropertyPaths() {
360 List<String> taxonNodePropertyPaths = new ArrayList<String>();
361 for(String propertyPath : getTaxonBasePropertyPaths()) {
362 taxonNodePropertyPaths.add("taxon." + propertyPath); //$NON-NLS-1$
363 }
364 return taxonNodePropertyPaths;
365 }
366
367 private List<String> getTaxonBasePropertyPaths() {
368 List<String> taxonBasePropertyPaths = Arrays.asList(new String[] {
369 "sec", //$NON-NLS-1$
370 "createdBy", //$NON-NLS-1$
371 "updatedBy", //$NON-NLS-1$
372 "annotations", //$NON-NLS-1$
373 "markers", //$NON-NLS-1$
374 "credits", //$NON-NLS-1$
375 "extensions", //$NON-NLS-1$
376 "rights", //$NON-NLS-1$
377 "sources", //$NON-NLS-1$
378 "identifiers",
379 "descriptions", //$NON-NLS-1$
380 "relationsToThisTaxon.relatedFrom.name.taxonBases.taxonNodes", //$NON-NLS-1$
381 "relationsToThisTaxon.relatedFrom.name.taxonBases.relationsFromThisTaxon", //$NON-NLS-1$
382 "relationsToThisTaxon.relatedFrom.name.taxonBases.relationsToThisTaxon", //$NON-NLS-1$
383 "relationsFromThisTaxon.relatedTo.name.taxonBases.taxonNodes", //$NON-NLS-1$
384 "relationsFromThisTaxon.relatedTo.name.taxonBases.relationsFromThisTaxon", //$NON-NLS-1$
385 "relationsFromThisTaxon.relatedTo.name.taxonBases.relationsToThisTaxon", //$NON-NLS-1$
386 "taxonNodes", //$NON-NLS-1$
387 "descriptions.descriptionElements.feature", //$NON-NLS-1$
388 "descriptions.descriptionElements.area", //$NON-NLS-1$
389 "descriptions.descriptionElements.status", //$NON-NLS-1$
390 "descriptions.markers", //$NON-NLS-1$
391 "name.descriptions", //$NON-NLS-1$
392 "name.typeDesignations", //$NON-NLS-1$
393 "name.status", //$NON-NLS-1$
394 "name.nomenclaturalReference.inReference", //$NON-NLS-1$
395 "name.taxonBases.taxonNodes", //$NON-NLS-1$
396 "name.relationsFromThisName", //$NON-NLS-1$
397 "name.relationsToThisName", //$NON-NLS-1$
398 "name.homotypicalGroup.typifiedNames.taxonBases.synonyms.synonym.name.status", //$NON-NLS-1$
399 "name.homotypicalGroup.typifiedNames.relationsToThisName.fromName", //$NON-NLS-1$
400 "synonyms.name.status.type", //$NON-NLS-1$
401 "synonyms.name.relationsToThisName.fromName", //$NON-NLS-1$
402 "synonyms.name.nomenclaturalReference.inReference.authorship", //$NON-NLS-1$
403 "synonyms.name.nomenclaturalReference.authorship", //$NON-NLS-1$
404 // "synonyms.name.homotypicalGroup", //$NON-NLS-1$
405 // "synonyms.synonym.name.homotypicalGroup.typifiedNames", //$NON-NLS-1$
406 "synonyms.name.homotypicalGroup.typifiedNames.taxonBases.synonyms" //$NON-NLS-1$
407 });
408
409 return taxonBasePropertyPaths;
410 }
411
412 /**
413 * @param selectedElement
414 * @param deleteConfig
415 */
416 public void addTaxonBaseToDelete(TaxonBase selectedElement, TaxonBaseDeletionConfigurator deleteConfig) {
417 this.toDelete.put(selectedElement, deleteConfig);
418
419 }
420
421 }