Project

General

Profile

Download (16.5 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.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.INameService;
28
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
29
import eu.etaxonomy.cdm.api.service.ITaxonService;
30
import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
31
import eu.etaxonomy.cdm.api.service.config.TaxonBaseDeletionConfigurator;
32
import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
33
import eu.etaxonomy.cdm.model.common.CdmBase;
34
import eu.etaxonomy.cdm.model.name.HybridRelationship;
35
import eu.etaxonomy.cdm.model.name.TaxonName;
36
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
37
import eu.etaxonomy.cdm.model.taxon.Synonym;
38
import eu.etaxonomy.cdm.model.taxon.Taxon;
39
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
40
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
41
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
42
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
43
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
44
import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
45
import eu.etaxonomy.taxeditor.editor.ChooseFromMultipleTaxonNodesDialog;
46
import eu.etaxonomy.taxeditor.editor.EditorUtil;
47
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
48
import eu.etaxonomy.taxeditor.model.DataChangeBridge;
49
import eu.etaxonomy.taxeditor.model.MessagingUtils;
50
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
51
import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
52
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
53
import eu.etaxonomy.taxeditor.store.CdmStore;
54

    
55

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

    
64
    private static final String INCORRECT_STATE = Messages.TaxonEditorInput_INCORRECT_STATE;
65

    
66
    private final ConversationHolder conversation;
67

    
68
    private TaxonNode taxonNode;
69

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

    
75

    
76
    private Set<AbstractPostTaxonOperation> operations = new HashSet<>();
77

    
78
    private TaxonEditorInputDataChangeBehaviourE4 dataChangeBehavior;
79

    
80
    private TaxonBase<?> initiallySelectedTaxonBase;
81

    
82
    private UISynchronize sync;
83

    
84

    
85
    public void setSync(UISynchronize sync) {
86
        this.sync = sync;
87
    }
88

    
89
    private enum CdmType {
90
        TAXON_NODE,
91
        TAXON_BASE,
92
        PARENT_TAXON_NODE
93
    }
94

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

    
111
    private void init(TaxonNode taxonNode) {
112
    	this.taxonNode = taxonNode;
113
    }
114

    
115

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

    
126
    }
127

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

    
137
                if (taxon.getTaxonNodes().size() == 0 && taxon.isMisapplication()){
138
                    // TODO get accepted taxon
139
                    MessagingUtils.info(Messages.TaxonEditorInput_OPEN_MISSAPPLIED_NAME);
140

    
141
                    Set<Taxon> acceptedTaxa = new HashSet<Taxon>();
142
                    Set<TaxonRelationship> relations = taxon.getRelationsFromThisTaxon();
143
                    for(TaxonRelationship relation : relations){
144
                        if(relation.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
145
                            acceptedTaxa.add(relation.getToTaxon());
146
                        }
147
                    }
148
                    setInputForMultipleTaxa(conversation, acceptedTaxa);
149

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

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

    
167

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

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

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

    
179
        initForTaxonNode(newTaxonNodeUuid);
180
    }
181

    
182
    private void setInputForMultipleNodes(ConversationHolder conversation, Set<TaxonNode> taxonNodes){
183
        if(taxonNodes.size() == 1){
184
            TaxonNode taxonNode = taxonNodes.iterator().next();
185
            taxonNode = getCdmEntitySession().remoteLoad(CdmStore.getService(ITaxonNodeService.class), taxonNode.getUuid(), getTaxonNodePropertyPaths());
186
            init(taxonNode);
187
        }else if(taxonNodes.size() > 1){
188
            TaxonNode taxonNode = ChooseFromMultipleTaxonNodesDialog.choose(taxonNodes);
189
            if(taxonNode != null){
190
                taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid(), getTaxonNodePropertyPaths());
191
            }
192
            if(taxonNode != null){
193
                init(taxonNode);
194
            }
195
        } else if (taxonNodes.size() == 0) {
196
            // this is an undesired state
197
            MessagingUtils.warningDialog(INCORRECT_STATE,TaxonEditorInputE4.class,Messages.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION);
198
        }
199
    }
200

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

    
218
    public static TaxonEditorInputE4 NewInstance(UUID taxonNodeUuid) {
219
        return new TaxonEditorInputE4(taxonNodeUuid, CdmType.TAXON_NODE);
220

    
221
    }
222

    
223
    public static TaxonEditorInputE4 NewInstanceFromTaxonBase(UUID taxonBaseUuid){
224
        return new TaxonEditorInputE4(taxonBaseUuid, CdmType.TAXON_BASE);
225
    }
226

    
227

    
228
    public static TaxonEditorInputE4 NewEmptyInstance(UUID parentNodeUuid){
229
        return new TaxonEditorInputE4(parentNodeUuid, CdmType.PARENT_TAXON_NODE);
230
    }
231

    
232
    public Object getAdapter(Class adapter) {
233

    
234
        if (adapter == Taxon.class) {
235
            return getTaxon();
236
        }
237

    
238
        if (adapter == TaxonNode.class) {
239
            return taxonNode;
240
        }
241

    
242
        return null;
243
    }
244

    
245
    /**
246
     * {@inheritDoc}
247
     *
248
     * Overrides equals to ensure that a taxon can only be edited by
249
     * one editor at a time.
250
     */
251
    @Override
252
    public boolean equals(Object obj) {
253
        if (TaxonEditorInputE4.class.equals(obj.getClass())
254
                && getTaxon() != null
255
                && getTaxon().equals(((TaxonEditorInputE4) obj).getTaxon())) {
256
            if (((TaxonEditorInputE4) obj).getInitiallySelectedTaxonBase() != null){
257
                setInitiallySelectedTaxonBase(((TaxonEditorInputE4) obj).getInitiallySelectedTaxonBase());
258
            }
259
            return true;
260
        }
261
        return false;
262
    }
263

    
264
    public Taxon getTaxon(){
265
        if(taxonNode!=null){
266
            Taxon taxon = CdmBase.deproxy(taxonNode.getTaxon(), Taxon.class);
267
            return taxon;
268
        }
269
        return null;
270
    }
271

    
272
    public TaxonNode getTaxonNode() {
273
        return taxonNode;
274
    }
275

    
276
    @Override
277
    public ConversationHolder getConversationHolder() {
278
        return conversation;
279
    }
280

    
281
    /** {@inheritDoc} */
282
    @Override
283
    public void update(CdmDataChangeMap events) {
284
        if(dataChangeBehavior == null){
285
            dataChangeBehavior = new TaxonEditorInputDataChangeBehaviourE4(this);
286
        }
287

    
288
        DataChangeBridge.handleDataChange(events, dataChangeBehavior);
289
    }
290

    
291
    public void setInitiallySelectedTaxonBase(TaxonBase taxonBase) {
292
        this.initiallySelectedTaxonBase = taxonBase;
293
    }
294

    
295
    public TaxonBase getInitiallySelectedTaxonBase() {
296
        return initiallySelectedTaxonBase;
297
    }
298

    
299
    public Set<AbstractPostTaxonOperation> getOperations() {
300
        return operations;
301
    }
302

    
303
    public void setOperations(Set<AbstractPostTaxonOperation> operations) {
304
        this.operations = operations;
305
    }
306

    
307
    public void addOperation(AbstractPostTaxonOperation operation) {
308
        this.operations.add(operation);
309
    }
310

    
311
    public void addToSaveNewSynonym(Synonym toSaveNew) {
312
        this.toSaveNewSynonyms.add(toSaveNew);
313
    }
314
    public void addToSaveNewConcept(Taxon toSaveNew) {
315
        if (!toSaveNew.isPersited()){
316
            this.toSaveNewConcepts.add(toSaveNew);
317
        }
318
    }
319

    
320
    @Override
321
    public List<TaxonNode> getRootEntities() {
322
        return Arrays.asList(taxonNode);
323
    }
324

    
325
    @Override
326
    public void merge() {
327
        if (!this.getCdmEntitySession().isActive()){
328
            this.getCdmEntitySession().bind();
329
        }
330
        for(Entry<TaxonBase, TaxonBaseDeletionConfigurator> entry:toDeletes.entrySet()){
331
            delete(entry.getKey(), entry.getValue());
332
        }
333
        toDeletes.clear();
334
        for(AbstractPostOperation entry:operations){
335
            EditorUtil.executeOperation(entry,sync);
336
        }
337
        operations.clear();
338

    
339
        for (Synonym syn: toSaveNewSynonyms){
340
            for (HybridRelationship rel : syn.getName().getHybridChildRelations()){
341
                if (!rel.getParentName().isPersited()) {
342
                    toSaveNewNames.add(rel.getParentName());
343
                }
344
                    if (!rel.getHybridName().isPersited()) {
345
                   toSaveNewNames.add(rel.getHybridName());
346
                }
347

    
348
            }
349
        }
350

    
351
        for (TaxonBase concept: toSaveNewConcepts){
352
            for (HybridRelationship rel : concept.getName().getHybridChildRelations()){
353
                if (!rel.getParentName().isPersited()) {
354
                    toSaveNewNames.add(rel.getParentName());
355
                }
356
                if (!rel.getHybridName().isPersited()) {
357
                    toSaveNewNames.add(rel.getHybridName());
358
                }
359

    
360
            }
361
        }
362
        CdmStore.getService(INameService.class).save(toSaveNewNames);
363
        CdmStore.getService(ITaxonService.class).save(toSaveNewConcepts);
364

    
365
        toSaveNewNames.clear();
366
        toSaveNewConcepts.clear();
367

    
368
        CdmStore.getService(ITaxonNodeService.class).merge(taxonNode, true);
369

    
370
    }
371

    
372
    /**
373
     * @param key
374
     * @param value
375
     */
376
    private void delete(TaxonBase key, TaxonBaseDeletionConfigurator value) {
377
        if (key instanceof Synonym){
378
            CdmStore.getService(ITaxonService.class).deleteSynonym(((Synonym)key).getUuid(), (SynonymDeletionConfigurator) value);
379
        }else{
380
            CdmStore.getService(ITaxonService.class).deleteTaxon(((Taxon)key).getUuid(), (TaxonDeletionConfigurator) value, null);
381
        }
382

    
383
    }
384

    
385
    @Override
386
    public Map<Object, List<String>> getPropertyPathsMap() {
387
        return null;
388
    }
389

    
390
    private List<String> getTaxonNodePropertyPaths() {
391
        List<String> taxonNodePropertyPaths = new ArrayList<String>();
392
        for(String propertyPath : getTaxonBasePropertyPaths()) {
393
            taxonNodePropertyPaths.add("taxon." + propertyPath); //$NON-NLS-1$
394
        }
395
        return taxonNodePropertyPaths;
396
    }
397

    
398
    private List<String> getTaxonBasePropertyPaths() {
399
        List<String> taxonBasePropertyPaths = Arrays.asList(new String[] {
400
                "sec", //$NON-NLS-1$
401
                "createdBy", //$NON-NLS-1$
402
                "updatedBy", //$NON-NLS-1$
403
                "annotations", //$NON-NLS-1$
404
                "markers", //$NON-NLS-1$
405
                "credits", //$NON-NLS-1$
406
                "extensions", //$NON-NLS-1$
407
                "rights", //$NON-NLS-1$
408
                "sources", //$NON-NLS-1$
409
                "identifiers",
410
                "descriptions", //$NON-NLS-1$
411
                "taxonNodes", //$NON-NLS-1$
412
                "descriptions.descriptionElements.feature", //$NON-NLS-1$
413
                "descriptions.descriptionElements.area", //$NON-NLS-1$
414
                "descriptions.descriptionElements.status", //$NON-NLS-1$
415
                "descriptions.markers", //$NON-NLS-1$
416
                "name.descriptions", //$NON-NLS-1$
417
                "name.typeDesignations.typeSpecimen.derivedFrom.originals", //$NON-NLS-1$
418
                "name.status", //$NON-NLS-1$
419
                "name.nomenclaturalReference.inReference", //$NON-NLS-1$
420
                "name.taxonBases.taxonNodes", //$NON-NLS-1$
421
                "name.relationsFromThisName", //$NON-NLS-1$
422
                "name.relationsToThisName", //$NON-NLS-1$
423
                "name.homotypicalGroup.typifiedNames.taxonBases.synonyms.synonym.name.status", //$NON-NLS-1$
424
                "name.homotypicalGroup.typifiedNames.relationsToThisName.fromName", //$NON-NLS-1$
425
                "synonyms.name.status.type", //$NON-NLS-1$
426
                "synonyms.name.relationsToThisName.fromName", //$NON-NLS-1$
427
                "synonyms.name.nomenclaturalReference.inReference.authorship", //$NON-NLS-1$
428
                "synonyms.name.nomenclaturalReference.authorship", //$NON-NLS-1$
429
                "synonyms.name.homotypicalGroup.typifiedNames.taxonBases.synonyms" //$NON-NLS-1$
430
        });
431

    
432
        return taxonBasePropertyPaths;
433
    }
434

    
435
    /**
436
     * @param selectedElement
437
     * @param deleteConfig
438
     */
439
    public void addTaxonBaseToDelete(TaxonBase selectedElement, TaxonBaseDeletionConfigurator deleteConfig) {
440
        this.toDeletes.put(selectedElement, deleteConfig);
441

    
442
    }
443

    
444
    /**
445
     * @param newName
446
     */
447
    public void addToSaveNewName(TaxonName newName) {
448
        this.toSaveNewNames.add(newName);
449
    }
450

    
451
}
(2-2/2)