Project

General

Profile

Download (17.9 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.e4.ui.di.UISynchronize;
24

    
25
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
26
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
27
import eu.etaxonomy.cdm.api.service.IClassificationService;
28
import eu.etaxonomy.cdm.api.service.INameService;
29
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
30
import eu.etaxonomy.cdm.api.service.ITaxonService;
31
import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
32
import eu.etaxonomy.cdm.api.service.config.TaxonBaseDeletionConfigurator;
33
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
34
import eu.etaxonomy.cdm.model.common.CdmBase;
35
import eu.etaxonomy.cdm.model.name.HybridRelationship;
36
import eu.etaxonomy.cdm.model.name.TaxonName;
37
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
38
import eu.etaxonomy.cdm.model.taxon.Synonym;
39
import eu.etaxonomy.cdm.model.taxon.Taxon;
40
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
41
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
42
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
43
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
44
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
45
import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
46
import eu.etaxonomy.taxeditor.editor.ChooseFromMultipleAcceptedTaxaDialog;
47
import eu.etaxonomy.taxeditor.editor.ChooseFromMultipleTaxonNodesDialog;
48
import eu.etaxonomy.taxeditor.editor.EditorUtil;
49
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
50
import eu.etaxonomy.taxeditor.model.DataChangeBridge;
51
import eu.etaxonomy.taxeditor.model.MessagingUtils;
52
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
53
import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
54
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
55
import eu.etaxonomy.taxeditor.store.CdmStore;
56

    
57

    
58
/**
59
 *
60
 * @author pplitzner
61
 * @date Aug 24, 2017
62
 *
63
 */
64
public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IConversationEnabled {
65

    
66
    private static final String INCORRECT_STATE = Messages.TaxonEditorInput_INCORRECT_STATE;
67

    
68
    private final ConversationHolder conversation;
69

    
70
    private TaxonNode taxonNode;
71

    
72
    private Map<TaxonBase, TaxonBaseDeletionConfigurator> toDeletes = new HashMap<>();
73
    private Set<Synonym> toSaveNewSynonyms = new HashSet<>();
74
    private List<TaxonBase> toSaveNewConcepts = new ArrayList<>();
75
    private List<TaxonName> toSaveNewNames = new ArrayList<>();
76

    
77

    
78
    private Set<AbstractPostTaxonOperation> operations = new HashSet<>();
79

    
80
    private TaxonEditorInputDataChangeBehaviourE4 dataChangeBehavior;
81

    
82
    private TaxonBase<?> initiallySelectedTaxonBase;
83

    
84
    private UISynchronize sync;
85

    
86

    
87
    public void setSync(UISynchronize sync) {
88
        this.sync = sync;
89
    }
90

    
91
    private enum CdmType {
92
        TAXON_NODE,
93
        TAXON_BASE,
94
        PARENT_TAXON_NODE
95
    }
96

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

    
113
    private void init(TaxonNode taxonNode) {
114
    	this.taxonNode = taxonNode;
115
    }
116

    
117

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

    
128
    }
129

    
130
    private void initForTaxonBase(UUID taxonBaseUuid) {
131
    	this.getCdmEntitySession().bind();
132
//        TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).load(taxonBaseUuid, getTaxonBasePropertyPaths());
133
    	TaxonBase taxonBase = getCdmEntitySession().remoteLoad(CdmStore.getService(ITaxonService.class), taxonBaseUuid, getTaxonBasePropertyPaths());
134
        if (taxonBase != null){
135
            if(taxonBase.isInstanceOf(Taxon.class)){
136
                Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
137
                setInitiallySelectedTaxonBase(taxon);
138

    
139
                if ( (taxon.isMisapplication() || taxon.isProparteSynonym())&& (taxon.getTaxonNodes().size() >0 || taxon.getRelationsFromThisTaxon().size() > 1)){
140
                    // TODO get accepted taxon
141
                    MessagingUtils.info(Messages.TaxonEditorInput_OPEN_MISSAPPLIED_NAME);
142

    
143
                    Set<Taxon> acceptedTaxa = new HashSet<Taxon>();
144
                    Set<TaxonRelationship> relations = taxon.getRelationsFromThisTaxon();
145
                    for(TaxonRelationship relation : relations){
146
                        if(relation.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
147
                            acceptedTaxa.add(relation.getToTaxon());
148
                        }
149
                    }
150
                    if (taxon.getTaxonNodes().size() > 0){
151
                        acceptedTaxa.add(taxon);
152
                    }
153
                    setInputForRelatedTaxa(conversation, acceptedTaxa);
154

    
155
                }else{
156
                    setInputForMultipleNodes(conversation, taxon.getTaxonNodes());
157
                }
158
            }else if(taxonBase instanceof Synonym){
159
                Synonym synonym = (Synonym) taxonBase;
160

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

    
172

    
173
    private void initForParentTaxonNode(UUID parentNodeUuid){
174
    	this.getCdmEntitySession().bind();
175
        TaxonName name = PreferencesUtil.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
176
        ITaxonTreeNode parentNode = CdmStore.getService(IClassificationService.class).getTreeNodeByUuid(parentNodeUuid);
177

    
178
        Taxon newTaxon = Taxon.NewInstance(name, parentNode.getReference());
179
        TaxonNode newTaxonNode = parentNode.addChildTaxon(newTaxon, parentNode.getReference(), parentNode.getMicroReference());
180

    
181
        // add the new taxon to the editors persistence context
182
        UUID newTaxonNodeUuid = CdmStore.getService(ITaxonNodeService.class).save(newTaxonNode).getUuid();
183

    
184
        initForTaxonNode(newTaxonNodeUuid);
185
    }
186

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

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

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

    
224
    private void setInputForRelatedTaxa(ConversationHolder conversation, Set<Taxon> taxa){
225
        if(taxa.size() == 1){
226
            Taxon taxon = taxa.iterator().next();
227
            Set<TaxonNode> nodes = taxon.getTaxonNodes();
228
            TaxonNode taxonNode = ChooseFromMultipleTaxonNodesDialog.choose(nodes);
229
            init(taxonNode);
230
        }else if(taxa.size() > 1){
231
            Iterator<Taxon> taxonIterator = taxa.iterator();
232
            Set<TaxonNode> nodes = new HashSet<>();
233
            while (taxonIterator.hasNext()){
234

    
235
                nodes.addAll(taxonIterator.next().getTaxonNodes());
236
            }
237
            TaxonNode taxonNode = ChooseFromMultipleAcceptedTaxaDialog.choose(nodes);
238
            if(taxonNode != null){
239
                taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid(), getTaxonNodePropertyPaths());
240
            }
241
            if(taxonNode != null){
242
                init(taxonNode);
243
            }
244
        } else if (taxa.size() == 0) {
245
            // this is an undesired state
246
            MessagingUtils.warningDialog(INCORRECT_STATE,TaxonEditorInputE4.class,Messages.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION);
247
        }
248
    }
249

    
250
    public static TaxonEditorInputE4 NewInstance(UUID taxonNodeUuid) {
251
        return new TaxonEditorInputE4(taxonNodeUuid, CdmType.TAXON_NODE);
252

    
253
    }
254

    
255
    public static TaxonEditorInputE4 NewInstanceFromTaxonBase(UUID taxonBaseUuid){
256
        return new TaxonEditorInputE4(taxonBaseUuid, CdmType.TAXON_BASE);
257
    }
258

    
259

    
260
    public static TaxonEditorInputE4 NewEmptyInstance(UUID parentNodeUuid){
261
        return new TaxonEditorInputE4(parentNodeUuid, CdmType.PARENT_TAXON_NODE);
262
    }
263

    
264
    public Object getAdapter(Class adapter) {
265

    
266
        if (adapter == Taxon.class) {
267
            return getTaxon();
268
        }
269

    
270
        if (adapter == TaxonNode.class) {
271
            return taxonNode;
272
        }
273

    
274
        return null;
275
    }
276

    
277
    /**
278
     * {@inheritDoc}
279
     *
280
     * Overrides equals to ensure that a taxon can only be edited by
281
     * one editor at a time.
282
     */
283
    @Override
284
    public boolean equals(Object obj) {
285
        if (TaxonEditorInputE4.class.equals(obj.getClass())
286
                && getTaxon() != null
287
                && getTaxon().equals(((TaxonEditorInputE4) obj).getTaxon())) {
288
            if (((TaxonEditorInputE4) obj).getInitiallySelectedTaxonBase() != null){
289
                setInitiallySelectedTaxonBase(((TaxonEditorInputE4) obj).getInitiallySelectedTaxonBase());
290
            }
291
            return true;
292
        }
293
        return false;
294
    }
295

    
296
    public Taxon getTaxon(){
297
        if(taxonNode!=null){
298
            Taxon taxon = CdmBase.deproxy(taxonNode.getTaxon(), Taxon.class);
299
            return taxon;
300
        }
301
        return null;
302
    }
303

    
304
    public TaxonNode getTaxonNode() {
305
        return taxonNode;
306
    }
307

    
308
    @Override
309
    public ConversationHolder getConversationHolder() {
310
        return conversation;
311
    }
312

    
313
    /** {@inheritDoc} */
314
    @Override
315
    public void update(CdmDataChangeMap events) {
316
        if(dataChangeBehavior == null){
317
            dataChangeBehavior = new TaxonEditorInputDataChangeBehaviourE4(this);
318
        }
319

    
320
        DataChangeBridge.handleDataChange(events, dataChangeBehavior);
321
    }
322

    
323
    public void setInitiallySelectedTaxonBase(TaxonBase taxonBase) {
324
        this.initiallySelectedTaxonBase = taxonBase;
325
    }
326

    
327
    public TaxonBase getInitiallySelectedTaxonBase() {
328
        return initiallySelectedTaxonBase;
329
    }
330

    
331
    public Set<AbstractPostTaxonOperation> getOperations() {
332
        return operations;
333
    }
334

    
335
    public void setOperations(Set<AbstractPostTaxonOperation> operations) {
336
        this.operations = operations;
337
    }
338

    
339
    public void addOperation(AbstractPostTaxonOperation operation) {
340
        this.operations.add(operation);
341
    }
342

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

    
349
    }
350

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

    
356
    @Override
357
    public void merge() {
358
        if (!this.getCdmEntitySession().isActive()){
359
            this.getCdmEntitySession().bind();
360
        }
361
        for(Entry<TaxonBase, TaxonBaseDeletionConfigurator> entry:toDeletes.entrySet()){
362
            delete(entry.getKey(), entry.getValue());
363
        }
364
        toDeletes.clear();
365
        for(AbstractPostOperation entry:operations){
366
            EditorUtil.executeOperation(entry,sync);
367
        }
368
        operations.clear();
369

    
370
        for (Synonym syn: toSaveNewSynonyms){
371
            for (HybridRelationship rel : syn.getName().getHybridChildRelations()){
372
//                if (!rel.getParentName().isPersited()) {
373
                    toSaveNewNames.add(rel.getParentName());
374
//                }
375
//                    if (!rel.getHybridName().isPersited()) {
376
                   toSaveNewNames.add(rel.getHybridName());
377
//                }
378

    
379
            }
380
        }
381

    
382
        for (TaxonBase concept: toSaveNewConcepts){
383
            for (HybridRelationship rel : concept.getName().getHybridChildRelations()){
384
//                if (!rel.getParentName().isPersited()) {
385
                    toSaveNewNames.add(rel.getParentName());
386
//                }
387
//                if (!rel.getHybridName().isPersited()) {
388
                    toSaveNewNames.add(rel.getHybridName());
389
//                }
390

    
391
            }
392
        }
393
        CdmStore.getService(INameService.class).merge(toSaveNewNames, true);
394
        CdmStore.getService(ITaxonService.class).merge(toSaveNewConcepts, true);
395

    
396
        toSaveNewNames.clear();
397
        toSaveNewConcepts.clear();
398

    
399
        CdmStore.getService(ITaxonNodeService.class).merge(taxonNode, true);
400

    
401
    }
402

    
403
    /**
404
     * @param key
405
     * @param value
406
     */
407
    private void delete(TaxonBase key, TaxonBaseDeletionConfigurator value) {
408
        if (key instanceof Synonym){
409
            CdmStore.getService(ITaxonService.class).deleteSynonym(((Synonym)key).getUuid(), (SynonymDeletionConfigurator) value);
410
        }else{
411
            CdmStore.getService(ITaxonService.class).deleteTaxon(((Taxon)key).getUuid(), (TaxonDeletionConfigurator) value, null);
412
        }
413

    
414
    }
415

    
416
    @Override
417
    public Map<Object, List<String>> getPropertyPathsMap() {
418
        return null;
419
    }
420

    
421
    private List<String> getTaxonNodePropertyPaths() {
422
        List<String> taxonNodePropertyPaths = new ArrayList<String>();
423
        for(String propertyPath : getTaxonBasePropertyPaths()) {
424
            taxonNodePropertyPaths.add("taxon." + propertyPath); //$NON-NLS-1$
425
        }
426
        return taxonNodePropertyPaths;
427
    }
428

    
429
    private List<String> getTaxonBasePropertyPaths() {
430
        List<String> taxonBasePropertyPaths = Arrays.asList(new String[] {
431
                "sec", //$NON-NLS-1$
432
                "createdBy", //$NON-NLS-1$
433
                "updatedBy", //$NON-NLS-1$
434
                "annotations", //$NON-NLS-1$
435
                "markers", //$NON-NLS-1$
436
                "credits", //$NON-NLS-1$
437
                "extensions", //$NON-NLS-1$
438
                "rights", //$NON-NLS-1$
439
                "sources", //$NON-NLS-1$
440
                "identifiers",
441
                "descriptions", //$NON-NLS-1$
442
                "taxonNodes", //$NON-NLS-1$
443
                "descriptions.descriptionElements.feature", //$NON-NLS-1$
444
                "descriptions.descriptionElements.area", //$NON-NLS-1$
445
                "descriptions.descriptionElements.status", //$NON-NLS-1$
446
                "descriptions.markers", //$NON-NLS-1$
447
                "name.descriptions", //$NON-NLS-1$
448
                "name.typeDesignations.typeSpecimen.derivedFrom.originals", //$NON-NLS-1$
449
                "name.status", //$NON-NLS-1$
450
                "name.nomenclaturalReference.inReference", //$NON-NLS-1$
451
                "name.taxonBases.taxonNodes", //$NON-NLS-1$
452
                "name.relationsFromThisName", //$NON-NLS-1$
453
                "name.relationsToThisName", //$NON-NLS-1$
454
                "name.homotypicalGroup.typifiedNames.taxonBases.synonyms.synonym.name.status", //$NON-NLS-1$
455
                "name.homotypicalGroup.typifiedNames.relationsToThisName.fromName", //$NON-NLS-1$
456
                "synonyms.name.status.type", //$NON-NLS-1$
457
                "synonyms.name.relationsToThisName.fromName", //$NON-NLS-1$
458
                "synonyms.name.nomenclaturalReference.inReference.authorship", //$NON-NLS-1$
459
                "synonyms.name.nomenclaturalReference.authorship", //$NON-NLS-1$
460
                "synonyms.name.homotypicalGroup.typifiedNames.taxonBases.synonyms" //$NON-NLS-1$
461
        });
462

    
463
        return taxonBasePropertyPaths;
464
    }
465

    
466
    /**
467
     * @param selectedElement
468
     * @param deleteConfig
469
     */
470
    public void addTaxonBaseToDelete(TaxonBase selectedElement, TaxonBaseDeletionConfigurator deleteConfig) {
471
        this.toDeletes.put(selectedElement, deleteConfig);
472

    
473
    }
474

    
475
    /**
476
     * @param newName
477
     */
478
    public void addToSaveNewName(TaxonName newName) {
479
        this.toSaveNewNames.add(newName);
480
    }
481

    
482
}
(2-2/2)