Project

General

Profile

Download (17.7 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.getTaxonNodes().size() == 0 && taxon.isMisapplication()){
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
                    setInputForRelatedTaxa(conversation, acceptedTaxa);
151

    
152
                }else{
153
                    setInputForMultipleNodes(conversation, taxon.getTaxonNodes());
154
                }
155
            }else if(taxonBase instanceof Synonym){
156
                Synonym synonym = (Synonym) taxonBase;
157

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

    
169

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

    
175
        Taxon newTaxon = Taxon.NewInstance(name, parentNode.getReference());
176
        TaxonNode newTaxonNode = parentNode.addChildTaxon(newTaxon, parentNode.getReference(), parentNode.getMicroReference());
177

    
178
        // add the new taxon to the editors persistence context
179
        UUID newTaxonNodeUuid = CdmStore.getService(ITaxonNodeService.class).save(newTaxonNode).getUuid();
180

    
181
        initForTaxonNode(newTaxonNodeUuid);
182
    }
183

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

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

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

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

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

    
247
    public static TaxonEditorInputE4 NewInstance(UUID taxonNodeUuid) {
248
        return new TaxonEditorInputE4(taxonNodeUuid, CdmType.TAXON_NODE);
249

    
250
    }
251

    
252
    public static TaxonEditorInputE4 NewInstanceFromTaxonBase(UUID taxonBaseUuid){
253
        return new TaxonEditorInputE4(taxonBaseUuid, CdmType.TAXON_BASE);
254
    }
255

    
256

    
257
    public static TaxonEditorInputE4 NewEmptyInstance(UUID parentNodeUuid){
258
        return new TaxonEditorInputE4(parentNodeUuid, CdmType.PARENT_TAXON_NODE);
259
    }
260

    
261
    public Object getAdapter(Class adapter) {
262

    
263
        if (adapter == Taxon.class) {
264
            return getTaxon();
265
        }
266

    
267
        if (adapter == TaxonNode.class) {
268
            return taxonNode;
269
        }
270

    
271
        return null;
272
    }
273

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

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

    
301
    public TaxonNode getTaxonNode() {
302
        return taxonNode;
303
    }
304

    
305
    @Override
306
    public ConversationHolder getConversationHolder() {
307
        return conversation;
308
    }
309

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

    
317
        DataChangeBridge.handleDataChange(events, dataChangeBehavior);
318
    }
319

    
320
    public void setInitiallySelectedTaxonBase(TaxonBase taxonBase) {
321
        this.initiallySelectedTaxonBase = taxonBase;
322
    }
323

    
324
    public TaxonBase getInitiallySelectedTaxonBase() {
325
        return initiallySelectedTaxonBase;
326
    }
327

    
328
    public Set<AbstractPostTaxonOperation> getOperations() {
329
        return operations;
330
    }
331

    
332
    public void setOperations(Set<AbstractPostTaxonOperation> operations) {
333
        this.operations = operations;
334
    }
335

    
336
    public void addOperation(AbstractPostTaxonOperation operation) {
337
        this.operations.add(operation);
338
    }
339

    
340
    public void addToSaveNewSynonym(Synonym toSaveNew) {
341
        this.toSaveNewSynonyms.add(toSaveNew);
342
    }
343
    public void addToSaveNewConcept(Taxon toSaveNew) {
344
       this.toSaveNewConcepts.add(toSaveNew);
345

    
346
    }
347

    
348
    @Override
349
    public List<TaxonNode> getRootEntities() {
350
        return Arrays.asList(taxonNode);
351
    }
352

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

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

    
376
            }
377
        }
378

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

    
388
            }
389
        }
390
        CdmStore.getService(INameService.class).merge(toSaveNewNames, true);
391
        CdmStore.getService(ITaxonService.class).merge(toSaveNewConcepts, true);
392

    
393
        toSaveNewNames.clear();
394
        toSaveNewConcepts.clear();
395

    
396
        CdmStore.getService(ITaxonNodeService.class).merge(taxonNode, true);
397

    
398
    }
399

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

    
411
    }
412

    
413
    @Override
414
    public Map<Object, List<String>> getPropertyPathsMap() {
415
        return null;
416
    }
417

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

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

    
460
        return taxonBasePropertyPaths;
461
    }
462

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

    
470
    }
471

    
472
    /**
473
     * @param newName
474
     */
475
    public void addToSaveNewName(TaxonName newName) {
476
        this.toSaveNewNames.add(newName);
477
    }
478

    
479
}
(2-2/2)