Project

General

Profile

Download (19 KB) Statistics
| Branch: | Tag: | Revision:
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.NullProgressMonitor;
28
import org.eclipse.core.runtime.Status;
29
import org.eclipse.e4.ui.di.UISynchronize;
30
import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
31

    
32
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
33
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
34
import eu.etaxonomy.cdm.api.service.IClassificationService;
35
import eu.etaxonomy.cdm.api.service.INameService;
36
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
37
import eu.etaxonomy.cdm.api.service.ITaxonService;
38
import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
39
import eu.etaxonomy.cdm.api.service.config.TaxonBaseDeletionConfigurator;
40
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
41
import eu.etaxonomy.cdm.model.common.CdmBase;
42
import eu.etaxonomy.cdm.model.name.HybridRelationship;
43
import eu.etaxonomy.cdm.model.name.TaxonName;
44
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
45
import eu.etaxonomy.cdm.model.taxon.Synonym;
46
import eu.etaxonomy.cdm.model.taxon.Taxon;
47
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
48
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
49
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
50
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
51
import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
52
import eu.etaxonomy.taxeditor.editor.ChooseFromMultipleAcceptedTaxaDialog;
53
import eu.etaxonomy.taxeditor.editor.ChooseFromMultipleTaxonNodesDialog;
54
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
55
import eu.etaxonomy.taxeditor.model.AbstractUtility;
56
import eu.etaxonomy.taxeditor.model.DataChangeBridge;
57
import eu.etaxonomy.taxeditor.model.MessagingUtils;
58
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
59
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
60
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
61
import eu.etaxonomy.taxeditor.store.CdmStore;
62
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
63

    
64

    
65
/**
66
 * @author pplitzner
67
 * @date Aug 24, 2017
68
 */
69
public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IConversationEnabled {
70

    
71
    private static final String INCORRECT_STATE = Messages.TaxonEditorInput_INCORRECT_STATE;
72

    
73
    private final ConversationHolder conversation;
74

    
75
    private TaxonNode taxonNode;
76

    
77
    private Map<TaxonBase, TaxonBaseDeletionConfigurator> toDeletes = new HashMap<>();
78
    private Set<Synonym> toSaveNewSynonyms = new HashSet<>();
79
    private List<TaxonBase> toSaveNewConcepts = new ArrayList<>();
80
    private List<TaxonName> toSaveNewNames = new ArrayList<>();
81

    
82

    
83
    private Set<AbstractPostOperation> operations = new HashSet<>();
84

    
85
    private TaxonEditorInputDataChangeBehaviourE4 dataChangeBehavior;
86

    
87
    private TaxonBase<?> initiallySelectedTaxonBase;
88

    
89
    private UISynchronize sync;
90

    
91

    
92
    public void setSync(UISynchronize sync) {
93
        this.sync = sync;
94
    }
95

    
96
    private enum CdmType {
97
        TAXON_NODE,
98
        TAXON_BASE,
99
        PARENT_TAXON_NODE
100
    }
101

    
102
    private TaxonEditorInputE4(UUID uuid, CdmType type) {
103
        super(true);
104
        this.conversation = CdmStore.createConversation();
105
        switch(type) {
106
        case PARENT_TAXON_NODE:
107
            initForParentTaxonNode(uuid);
108
            break;
109
        case TAXON_BASE:
110
            initForTaxonBase(uuid);
111
            break;
112
        case TAXON_NODE:
113
            initForTaxonNode(uuid);
114
            break;
115
        }
116
    }
117

    
118
    private void init(TaxonNode taxonNode) {
119
    	this.taxonNode = taxonNode;
120
    }
121

    
122
    private void initForTaxonNode(UUID taxonNodeUuid) {
123
    	this.getCdmEntitySession().bind();
124
        TaxonNode taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNodeUuid, getTaxonNodePropertyPaths());
125
//    	TaxonNode taxonNode = getCdmEntitySession().remoteLoad(CdmStore.getService(ITaxonNodeService.class), taxonNodeUuid, getTaxonNodePropertyPaths());
126
        if(taxonNode == null){
127
            MessagingUtils.warningDialog(Messages.TaxonEditorInput_NOT_IMPLEMENTED, TaxonEditorInputE4.class, Messages.TaxonEditorInput_NOT_IMPLEMENTED_MESSAGE);
128
        }
129
        init(taxonNode);
130
        setInitiallySelectedTaxonBase(getTaxon());
131

    
132
    }
133

    
134
    private void initForTaxonBase(UUID taxonBaseUuid) {
135
    	this.getCdmEntitySession().bind();
136
//        TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).load(taxonBaseUuid, getTaxonBasePropertyPaths());
137
    	TaxonBase<?> taxonBase = getCdmEntitySession().remoteLoad(CdmStore.getService(ITaxonService.class), taxonBaseUuid, getTaxonBasePropertyPaths());
138
        if (taxonBase != null){
139
            if(taxonBase.isInstanceOf(Taxon.class)){
140
                Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
141
                setInitiallySelectedTaxonBase(taxon);
142

    
143
                if ( taxon.isMisapplication() || taxon.isProparteSynonym()){
144
                    // TODO get accepted taxon
145
                    MessagingUtils.info(Messages.TaxonEditorInput_OPEN_MISSAPPLIED_NAME);
146

    
147
                    Set<Taxon> acceptedTaxa = new HashSet<Taxon>();
148
                    Set<TaxonRelationship> relations = taxon.getRelationsFromThisTaxon();
149
                    for(TaxonRelationship relation : relations){
150
                        if(relation.getType().isAnyMisappliedName() || relation.getType().isAnySynonym()){
151
                            acceptedTaxa.add(relation.getToTaxon());
152
                        }
153
                    }
154
                    if (taxon.getTaxonNodes().size() > 0){
155
                        acceptedTaxa.add(taxon);
156
                    }
157
                    setInputForRelatedTaxa(conversation, acceptedTaxa);
158

    
159
                }else{
160
                    setInputForMultipleNodes(conversation, taxon.getTaxonNodes());
161
                }
162
            }else if(taxonBase instanceof Synonym){
163
                Synonym synonym = (Synonym) taxonBase;
164

    
165
                Set<Taxon> taxa = new HashSet<>();
166
                Taxon taxon = synonym.getAcceptedTaxon();
167
                if (taxon != null){
168
                	taxa.add(taxon);
169
                }
170
                setInputForMultipleTaxa(conversation, taxa);
171
                setInitiallySelectedTaxonBase(synonym);
172
            }
173
        }
174
    }
175

    
176

    
177
    private void initForParentTaxonNode(UUID parentNodeUuid){
178
    	this.getCdmEntitySession().bind();
179
        TaxonName name = PreferencesUtil.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
180
        ITaxonTreeNode parentNode = CdmStore.getService(IClassificationService.class).getTreeNodeByUuid(parentNodeUuid);
181

    
182
        Taxon newTaxon = Taxon.NewInstance(name, parentNode.getReference());
183
        TaxonNode newTaxonNode = parentNode.addChildTaxon(newTaxon, parentNode.getReference(), parentNode.getMicroReference());
184

    
185
        // add the new taxon to the editors persistence context
186
        UUID newTaxonNodeUuid = CdmStore.getService(ITaxonNodeService.class).save(newTaxonNode).getUuid();
187

    
188
        initForTaxonNode(newTaxonNodeUuid);
189
    }
190

    
191
    private void setInputForMultipleNodes(ConversationHolder conversation, Set<TaxonNode> taxonNodes){
192
        if(taxonNodes.size() == 1){
193
            TaxonNode taxonNode = taxonNodes.iterator().next();
194
            taxonNode = getCdmEntitySession().remoteLoad(CdmStore.getService(ITaxonNodeService.class), taxonNode.getUuid(), getTaxonNodePropertyPaths());
195
            init(taxonNode);
196
        }else if(taxonNodes.size() > 1){
197

    
198
            TaxonNode taxonNode = ChooseFromMultipleTaxonNodesDialog.choose(taxonNodes);
199
            if(taxonNode != null){
200
                taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid(), getTaxonNodePropertyPaths());
201
            }
202
            if(taxonNode != null){
203
                init(taxonNode);
204
            }
205
        } else if (taxonNodes.size() == 0) {
206
            // this is an undesired state
207
            MessagingUtils.warningDialog(INCORRECT_STATE,TaxonEditorInputE4.class,Messages.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION);
208
        }
209
    }
210

    
211
    private void setInputForMultipleTaxa(ConversationHolder conversation, Set<Taxon> taxa){
212
        if(taxa.size() == 1){
213
            Taxon taxon = taxa.iterator().next();
214
            Set<TaxonNode> nodes = taxon.getTaxonNodes();
215
            setInputForMultipleNodes(conversation, nodes);
216
        }else if(taxa.size() > 1){
217
            Set<TaxonNode> taxonNodes = new HashSet<TaxonNode>();
218
            for ( Taxon taxon : taxa ){
219
                taxonNodes.addAll(taxon.getTaxonNodes());
220
            }
221
            setInputForMultipleNodes(conversation, taxonNodes);
222
        }else if(taxa.size() == 0){
223
            // this is an undesired state
224
            MessagingUtils.warningDialog(INCORRECT_STATE, TaxonEditorInputE4.class, Messages.TaxonEditorInput_NO_ACCEPTED_TAXON_PRESENT);
225
        }
226
    }
227

    
228
    private void setInputForRelatedTaxa(ConversationHolder conversation, Set<Taxon> taxa){
229
        if(taxa.size() == 1){
230
            Taxon taxon = taxa.iterator().next();
231
            Set<TaxonNode> nodes = taxon.getTaxonNodes();
232
            TaxonNode taxonNode = null;
233
            if (nodes.size()>1){
234
                taxonNode = ChooseFromMultipleTaxonNodesDialog.choose(nodes);
235
            }else if (nodes.size()==1){
236
                taxonNode = nodes.iterator().next();
237
            }else{
238
                MessagingUtils.warningDialog(INCORRECT_STATE,TaxonEditorInputE4.class,Messages.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION);
239
            }
240
            init(taxonNode);
241
        }else if(taxa.size() > 1){
242
            Iterator<Taxon> taxonIterator = taxa.iterator();
243
            Set<TaxonNode> nodes = new HashSet<>();
244
            while (taxonIterator.hasNext()){
245

    
246
                nodes.addAll(taxonIterator.next().getTaxonNodes());
247
            }
248
            TaxonNode taxonNode = ChooseFromMultipleAcceptedTaxaDialog.choose(nodes);
249
            if(taxonNode != null){
250
                taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid(), getTaxonNodePropertyPaths());
251
            }
252
            if(taxonNode != null){
253
                init(taxonNode);
254
            }
255
        } else if (taxa.size() == 0) {
256
            // this is an undesired state
257
            MessagingUtils.warningDialog(INCORRECT_STATE,TaxonEditorInputE4.class,Messages.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION);
258
        }
259
    }
260

    
261
    public static TaxonEditorInputE4 NewInstance(UUID taxonNodeUuid) {
262
        return new TaxonEditorInputE4(taxonNodeUuid, CdmType.TAXON_NODE);
263

    
264
    }
265

    
266
    public static TaxonEditorInputE4 NewInstanceFromTaxonBase(UUID taxonBaseUuid){
267
        return new TaxonEditorInputE4(taxonBaseUuid, CdmType.TAXON_BASE);
268
    }
269

    
270

    
271
    public static TaxonEditorInputE4 NewEmptyInstance(UUID parentNodeUuid){
272
        return new TaxonEditorInputE4(parentNodeUuid, CdmType.PARENT_TAXON_NODE);
273
    }
274

    
275
    public Object getAdapter(Class adapter) {
276

    
277
        if (adapter == Taxon.class) {
278
            return getTaxon();
279
        }
280

    
281
        if (adapter == TaxonNode.class) {
282
            return taxonNode;
283
        }
284

    
285
        return null;
286
    }
287

    
288
    /**
289
     * {@inheritDoc}
290
     *
291
     * Overrides equals to ensure that a taxon can only be edited by
292
     * one editor at a time.
293
     */
294
    @Override
295
    public boolean equals(Object obj) {
296
        if (TaxonEditorInputE4.class.equals(obj.getClass())
297
                && getTaxon() != null
298
                && getTaxon().equals(((TaxonEditorInputE4) obj).getTaxon())) {
299
            if (((TaxonEditorInputE4) obj).getInitiallySelectedTaxonBase() != null){
300
                setInitiallySelectedTaxonBase(((TaxonEditorInputE4) obj).getInitiallySelectedTaxonBase());
301
            }
302
            return true;
303
        }
304
        return false;
305
    }
306

    
307
    public Taxon getTaxon(){
308
        if(taxonNode!=null){
309
            Taxon taxon = CdmBase.deproxy(taxonNode.getTaxon(), Taxon.class);
310
            return taxon;
311
        }
312
        return null;
313
    }
314

    
315
    public TaxonNode getTaxonNode() {
316
        return taxonNode;
317
    }
318

    
319
    @Override
320
    public ConversationHolder getConversationHolder() {
321
        return conversation;
322
    }
323

    
324
    @Override
325
    public void update(CdmDataChangeMap events) {
326
        if(dataChangeBehavior == null){
327
            dataChangeBehavior = new TaxonEditorInputDataChangeBehaviourE4(this);
328
        }
329

    
330
        DataChangeBridge.handleDataChange(events, dataChangeBehavior);
331
    }
332

    
333
    public void setInitiallySelectedTaxonBase(TaxonBase taxonBase) {
334
        this.initiallySelectedTaxonBase = taxonBase;
335
    }
336

    
337
    public TaxonBase getInitiallySelectedTaxonBase() {
338
        return initiallySelectedTaxonBase;
339
    }
340

    
341
    public void addOperation(AbstractPostOperation operation) {
342
        this.operations.add(operation);
343
    }
344

    
345
    public void addToSaveNewSynonym(Synonym toSaveNew) {
346
        this.toSaveNewSynonyms.add(toSaveNew);
347
    }
348
    public void addToSaveNewConcept(Taxon toSaveNew) {
349
       this.toSaveNewConcepts.add(toSaveNew);
350

    
351
    }
352

    
353
    @Override
354
    public List<TaxonNode> getRootEntities() {
355
        return Arrays.asList(taxonNode);
356
    }
357

    
358
    @Override
359
    public void merge() {
360
        if (!this.getCdmEntitySession().isActive()){
361
            this.getCdmEntitySession().bind();
362
        }
363
        for(Entry<TaxonBase, TaxonBaseDeletionConfigurator> entry:toDeletes.entrySet()){
364
            delete(entry.getKey(), entry.getValue());
365
        }
366
        toDeletes.clear();
367
        for(AbstractPostOperation<?> entry:operations){
368
            IStatus status = Status.CANCEL_STATUS;
369
            final IAdaptable uiInfoAdapter = WorkspaceUndoUtil
370
                    .getUIInfoAdapter(AbstractUtility.getShell());
371
            String operationlabel = entry.getLabel();
372
            try {
373
                entry.addContext(IOperationHistory.GLOBAL_UNDO_CONTEXT);
374
                status = entry.execute(new NullProgressMonitor(), uiInfoAdapter);
375
            } catch (ExecutionException e) {
376

    
377
                MessagingUtils.operationDialog(AbstractUtility.class, e, TaxeditorStorePlugin.PLUGIN_ID, operationlabel, null);
378

    
379
            }
380

    
381
            String statusString = status.equals(Status.OK_STATUS) ? "completed"
382
                    : "cancelled";
383

    
384
            IPostOperationEnabled postOperationEnabled = entry
385
                    .getPostOperationEnabled();
386
            if (postOperationEnabled != null) {
387
                postOperationEnabled.onComplete();
388
            }
389

    
390

    
391
            //AbstractUtility.executeOperation(entry,sync);
392
        }
393

    
394
        operations.clear();
395
        for (Synonym syn: toSaveNewSynonyms){
396
            for (HybridRelationship rel : syn.getName().getHybridChildRelations()){
397
//                if (!rel.getParentName().isPersited()) {
398
                    toSaveNewNames.add(rel.getParentName());
399
//                }
400
//                    if (!rel.getHybridName().isPersited()) {
401
                   toSaveNewNames.add(rel.getHybridName());
402
//                }
403

    
404
            }
405
        }
406

    
407
        for (TaxonBase<?> concept: toSaveNewConcepts){
408
            for (HybridRelationship rel : concept.getName().getHybridChildRelations()){
409
//                if (!rel.getParentName().isPersited()) {
410
                    toSaveNewNames.add(rel.getParentName());
411
//                }
412
//                if (!rel.getHybridName().isPersited()) {
413
                    toSaveNewNames.add(rel.getHybridName());
414
//                }
415

    
416
            }
417
        }
418
        CdmStore.getService(INameService.class).merge(toSaveNewNames, true);
419
        CdmStore.getService(ITaxonService.class).merge(toSaveNewConcepts, true);
420

    
421
        toSaveNewNames.clear();
422
        toSaveNewConcepts.clear();
423

    
424
        CdmStore.getService(ITaxonNodeService.class).merge(taxonNode, true);
425

    
426

    
427
    }
428

    
429
    private void delete(TaxonBase key, TaxonBaseDeletionConfigurator value) {
430
        if (key instanceof Synonym){
431
            CdmStore.getService(ITaxonService.class).deleteSynonym(((Synonym)key).getUuid(), (SynonymDeletionConfigurator) value);
432
        }else{
433
            CdmStore.getService(ITaxonService.class).deleteTaxon(((Taxon)key).getUuid(), (TaxonDeletionConfigurator) value, null);
434
        }
435
    }
436

    
437
    @Override
438
    public Map<Object, List<String>> getPropertyPathsMap() {
439
        return null;
440
    }
441

    
442
    private List<String> getTaxonNodePropertyPaths() {
443
        List<String> taxonNodePropertyPaths = new ArrayList<String>();
444
        for(String propertyPath : getTaxonBasePropertyPaths()) {
445
            taxonNodePropertyPaths.add("taxon." + propertyPath); //$NON-NLS-1$
446
        }
447
        return taxonNodePropertyPaths;
448
    }
449

    
450
    private List<String> getTaxonBasePropertyPaths() {
451
        List<String> taxonBasePropertyPaths = Arrays.asList(new String[] {
452
                "sec", //$NON-NLS-1$
453
                "createdBy", //$NON-NLS-1$
454
                "updatedBy", //$NON-NLS-1$
455
                "annotations", //$NON-NLS-1$
456
                "markers", //$NON-NLS-1$
457
                "credits", //$NON-NLS-1$
458
                "extensions", //$NON-NLS-1$
459
                "rights", //$NON-NLS-1$
460
                "sources", //$NON-NLS-1$
461
                "identifiers",
462
                "descriptions", //$NON-NLS-1$
463
                "taxonNodes", //$NON-NLS-1$
464
                "descriptions.descriptionElements.feature", //$NON-NLS-1$
465
                "descriptions.descriptionElements.area", //$NON-NLS-1$
466
                "descriptions.descriptionElements.status", //$NON-NLS-1$
467
                "descriptions.markers", //$NON-NLS-1$
468
                "name.descriptions", //$NON-NLS-1$
469
                "name.typeDesignations.typeSpecimen.derivedFrom.originals", //$NON-NLS-1$
470
                "name.status", //$NON-NLS-1$
471
                "name.nomenclaturalReference.inReference", //$NON-NLS-1$
472
                "name.taxonBases.taxonNodes", //$NON-NLS-1$
473
                "name.relationsFromThisName", //$NON-NLS-1$
474
                "name.relationsToThisName", //$NON-NLS-1$
475
                "name.homotypicalGroup.typifiedNames.taxonBases.synonyms.synonym.name.status", //$NON-NLS-1$
476
                "name.homotypicalGroup.typifiedNames.relationsToThisName.fromName", //$NON-NLS-1$
477
                "synonyms.name.status.type", //$NON-NLS-1$
478
                "synonyms.name.relationsToThisName.fromName", //$NON-NLS-1$
479
                "synonyms.name.nomenclaturalReference.inReference.authorship", //$NON-NLS-1$
480
                "synonyms.name.nomenclaturalReference.authorship", //$NON-NLS-1$
481
                "synonyms.name.homotypicalGroup.typifiedNames.taxonBases.synonyms" //$NON-NLS-1$
482
        });
483

    
484
        return taxonBasePropertyPaths;
485
    }
486

    
487
    public void addTaxonBaseToDelete(TaxonBase selectedElement, TaxonBaseDeletionConfigurator deleteConfig) {
488
        this.toDeletes.put(selectedElement, deleteConfig);
489

    
490
    }
491

    
492
    public void addToSaveNewName(TaxonName newName) {
493
        this.toSaveNewNames.add(newName);
494
    }
495
}
(2-2/2)