Project

General

Profile

Download (12.4 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.HashSet;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.Set;
18
import java.util.UUID;
19

    
20
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
21
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
22
import eu.etaxonomy.cdm.api.service.IClassificationService;
23
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
24
import eu.etaxonomy.cdm.api.service.ITaxonService;
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26
import eu.etaxonomy.cdm.model.name.TaxonName;
27
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
28
import eu.etaxonomy.cdm.model.taxon.Synonym;
29
import eu.etaxonomy.cdm.model.taxon.Taxon;
30
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
31
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
32
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
33
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
34
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
35
import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
36
import eu.etaxonomy.taxeditor.editor.ChooseFromMultipleTaxonNodesDialog;
37
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
38
import eu.etaxonomy.taxeditor.model.DataChangeBridge;
39
import eu.etaxonomy.taxeditor.model.MessagingUtils;
40
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
41
import eu.etaxonomy.taxeditor.store.CdmStore;
42

    
43

    
44
/**
45
 *
46
 * @author pplitzner
47
 * @date Aug 24, 2017
48
 *
49
 */
50
public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IConversationEnabled {
51

    
52
    private static final String INCORRECT_STATE = Messages.TaxonEditorInput_INCORRECT_STATE;
53

    
54
    private final ConversationHolder conversation;
55

    
56
    private TaxonNode taxonNode;
57

    
58
    private TaxonEditorInputDataChangeBehaviourE4 dataChangeBehavior;
59

    
60
    private TaxonBase initiallySelectedTaxonBase;
61

    
62
    private enum CdmType {
63
        TAXON_NODE,
64
        TAXON_BASE,
65
        PARENT_TAXON_NODE
66
    }
67

    
68
    private TaxonEditorInputE4(UUID uuid, CdmType type) {
69
        super(true);
70
        this.conversation = CdmStore.createConversation();
71
        switch(type) {
72
        case PARENT_TAXON_NODE:
73
            initForParentTaxonNode(uuid);
74
            break;
75
        case TAXON_BASE:
76
            initForTaxonBase(uuid);
77
            break;
78
        case TAXON_NODE:
79
            initForTaxonNode(uuid);
80
            break;
81
        }
82
    }
83

    
84
    private void init(TaxonNode taxonNode) {
85
    	this.taxonNode = taxonNode;
86
    }
87

    
88

    
89
    private void initForTaxonNode(UUID taxonNodeUuid) {
90
    	this.getCdmEntitySession().bind();
91
        TaxonNode taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNodeUuid, getTaxonNodePropertyPaths());
92
        if(taxonNode == null){
93
            MessagingUtils.warningDialog(Messages.TaxonEditorInput_NOT_IMPLEMENTED, TaxonEditorInputE4.class, Messages.TaxonEditorInput_NOT_IMPLEMENTED_MESSAGE);
94
        }
95
        init(taxonNode);
96
        setInitiallySelectedTaxonBase(getTaxon());
97

    
98
    }
99

    
100
    private void initForTaxonBase(UUID taxonBaseUuid) {
101
    	this.getCdmEntitySession().bind();
102
        TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).load(taxonBaseUuid, getTaxonBasePropertyPaths());
103
        if (taxonBase != null){
104
            if(taxonBase.isInstanceOf(Taxon.class)){
105
                Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
106
                setInitiallySelectedTaxonBase(taxon);
107

    
108
                if (taxon.getTaxonNodes().size() == 0 && taxon.isMisapplication()){
109
                    // TODO get accepted taxon
110
                    MessagingUtils.info(Messages.TaxonEditorInput_OPEN_MISSAPPLIED_NAME);
111

    
112
                    Set<Taxon> acceptedTaxa = new HashSet<Taxon>();
113
                    Set<TaxonRelationship> relations = taxon.getRelationsFromThisTaxon();
114
                    for(TaxonRelationship relation : relations){
115
                        if(relation.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
116
                            acceptedTaxa.add(relation.getToTaxon());
117
                        }
118
                    }
119
                    setInputForMultipleTaxa(conversation, acceptedTaxa);
120

    
121
                }else{
122
                    setInputForMultipleNodes(conversation, taxon.getTaxonNodes());
123
                }
124
            }else if(taxonBase instanceof Synonym){
125
                Synonym synonym = (Synonym) taxonBase;
126

    
127
                Set<Taxon> taxa = new HashSet<>();
128
                Taxon taxon = synonym.getAcceptedTaxon();
129
                if (taxon != null){
130
                	taxa.add(taxon);
131
                }
132
                setInputForMultipleTaxa(conversation, taxa);
133
                setInitiallySelectedTaxonBase(synonym);
134
            }
135
        }
136
    }
137

    
138

    
139
    private void initForParentTaxonNode(UUID parentNodeUuid){
140
    	this.getCdmEntitySession().bind();
141
        TaxonName name = PreferencesUtil.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
142
        ITaxonTreeNode parentNode = CdmStore.getService(IClassificationService.class).getTreeNodeByUuid(parentNodeUuid);
143

    
144
        Taxon newTaxon = Taxon.NewInstance(name, parentNode.getReference());
145
        TaxonNode newTaxonNode = parentNode.addChildTaxon(newTaxon, parentNode.getReference(), parentNode.getMicroReference());
146

    
147
        // add the new taxon to the editors persistence context
148
        UUID newTaxonNodeUuid = CdmStore.getService(ITaxonNodeService.class).save(newTaxonNode).getUuid();
149

    
150
        initForTaxonNode(newTaxonNodeUuid);
151
    }
152

    
153
    private void setInputForMultipleNodes(ConversationHolder conversation, Set<TaxonNode> taxonNodes){
154
        if(taxonNodes.size() == 1){
155
            TaxonNode taxonNode = taxonNodes.iterator().next();
156
            taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid(), getTaxonNodePropertyPaths());
157
            init(taxonNode);
158
        }else if(taxonNodes.size() > 1){
159
            TaxonNode taxonNode = ChooseFromMultipleTaxonNodesDialog.choose(taxonNodes);
160
            taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid(), getTaxonNodePropertyPaths());
161
            if(taxonNode != null){
162
                init(taxonNode);
163
            }
164
        } else if (taxonNodes.size() == 0) {
165
            // this is an undesired state
166
            MessagingUtils.warningDialog(INCORRECT_STATE,TaxonEditorInputE4.class,Messages.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION);
167
        }
168
    }
169

    
170
    private void setInputForMultipleTaxa(ConversationHolder conversation, Set<Taxon> taxa){
171
        if(taxa.size() == 1){
172
            Taxon taxon = taxa.iterator().next();
173
            Set<TaxonNode> nodes = taxon.getTaxonNodes();
174
            setInputForMultipleNodes(conversation, nodes);
175
        }else if(taxa.size() > 1){
176
            Set<TaxonNode> taxonNodes = new HashSet<TaxonNode>();
177
            for ( Taxon taxon : taxa ){
178
                taxonNodes.addAll(taxon.getTaxonNodes());
179
            }
180
            setInputForMultipleNodes(conversation, taxonNodes);
181
        }else if(taxa.size() == 0){
182
            // this is an undesired state
183
            MessagingUtils.warningDialog(INCORRECT_STATE, TaxonEditorInputE4.class, Messages.TaxonEditorInput_NO_ACCEPTED_TAXON_PRESENT);
184
        }
185
    }
186

    
187
    public static TaxonEditorInputE4 NewInstance(UUID taxonNodeUuid) {
188
        return new TaxonEditorInputE4(taxonNodeUuid, CdmType.TAXON_NODE);
189

    
190
    }
191

    
192
    public static TaxonEditorInputE4 NewInstanceFromTaxonBase(UUID taxonBaseUuid){
193
        return new TaxonEditorInputE4(taxonBaseUuid, CdmType.TAXON_BASE);
194
    }
195

    
196

    
197
    public static TaxonEditorInputE4 NewEmptyInstance(UUID parentNodeUuid){
198
        return new TaxonEditorInputE4(parentNodeUuid, CdmType.PARENT_TAXON_NODE);
199
    }
200

    
201
    public Object getAdapter(Class adapter) {
202

    
203
        if (adapter == Taxon.class) {
204
            return getTaxon();
205
        }
206

    
207
        if (adapter == TaxonNode.class) {
208
            return taxonNode;
209
        }
210

    
211
        return null;
212
    }
213

    
214
    /**
215
     * {@inheritDoc}
216
     *
217
     * Overrides equals to ensure that a taxon can only be edited by
218
     * one editor at a time.
219
     */
220
    @Override
221
    public boolean equals(Object obj) {
222
        if (TaxonEditorInputE4.class.equals(obj.getClass())
223
                && getTaxon() != null
224
                && getTaxon().equals(((TaxonEditorInputE4) obj).getTaxon())) {
225
            if (((TaxonEditorInputE4) obj).getInitiallySelectedTaxonBase() != null){
226
                setInitiallySelectedTaxonBase(((TaxonEditorInputE4) obj).getInitiallySelectedTaxonBase());
227
            }
228
            return true;
229
        }
230
        return false;
231
    }
232

    
233
    public Taxon getTaxon(){
234
        if(taxonNode!=null){
235
            Taxon taxon = CdmBase.deproxy(taxonNode.getTaxon(), Taxon.class);
236
            return taxon;
237
        }
238
        return null;
239
    }
240

    
241
    public TaxonNode getTaxonNode() {
242
        return taxonNode;
243
    }
244

    
245
    @Override
246
    public ConversationHolder getConversationHolder() {
247
        return conversation;
248
    }
249

    
250
    /** {@inheritDoc} */
251
    @Override
252
    public void update(CdmDataChangeMap events) {
253
        if(dataChangeBehavior == null){
254
            dataChangeBehavior = new TaxonEditorInputDataChangeBehaviourE4(this);
255
        }
256

    
257
        DataChangeBridge.handleDataChange(events, dataChangeBehavior);
258
    }
259

    
260
    public void setInitiallySelectedTaxonBase(TaxonBase taxonBase) {
261
        this.initiallySelectedTaxonBase = taxonBase;
262
    }
263

    
264
    public TaxonBase getInitiallySelectedTaxonBase() {
265
        return initiallySelectedTaxonBase;
266
    }
267

    
268
    @Override
269
    public List<TaxonNode> getRootEntities() {
270
        return Arrays.asList(taxonNode);
271
    }
272

    
273
    @Override
274
    public void merge() {
275
        if (!this.getCdmEntitySession().isActive()){
276
            this.getCdmEntitySession().bind();
277
        }
278
        CdmStore.getService(ITaxonNodeService.class).merge(taxonNode, true);
279

    
280
    }
281

    
282
    @Override
283
    public Map<Object, List<String>> getPropertyPathsMap() {
284
        return null;
285
    }
286

    
287
    private List<String> getTaxonNodePropertyPaths() {
288
        List<String> taxonNodePropertyPaths = new ArrayList<String>();
289
        for(String propertyPath : getTaxonBasePropertyPaths()) {
290
            taxonNodePropertyPaths.add("taxon." + propertyPath); //$NON-NLS-1$
291
        }
292
        return taxonNodePropertyPaths;
293
    }
294

    
295
    private List<String> getTaxonBasePropertyPaths() {
296
        List<String> taxonBasePropertyPaths = Arrays.asList(new String[] {
297
                "sec", //$NON-NLS-1$
298
                "createdBy", //$NON-NLS-1$
299
                "updatedBy", //$NON-NLS-1$
300
                "annotations", //$NON-NLS-1$
301
                "markers", //$NON-NLS-1$
302
                "credits", //$NON-NLS-1$
303
                "extensions", //$NON-NLS-1$
304
                "rights", //$NON-NLS-1$
305
                "sources", //$NON-NLS-1$
306
                "descriptions", //$NON-NLS-1$
307
                "relationsToThisTaxon", //$NON-NLS-1$
308
                "relationsFromThisTaxon", //$NON-NLS-1$
309
                "taxonNodes", //$NON-NLS-1$
310
                "descriptions.descriptionElements.feature", //$NON-NLS-1$
311
                "descriptions.descriptionElements.area", //$NON-NLS-1$
312
                "descriptions.descriptionElements.status", //$NON-NLS-1$
313
                "descriptions.markers", //$NON-NLS-1$
314
                "name.descriptions", //$NON-NLS-1$
315
                "name.typeDesignations", //$NON-NLS-1$
316
                "name.status", //$NON-NLS-1$
317
                "name.nomenclaturalReference.inReference", //$NON-NLS-1$
318
                "name.taxonBases.taxonNodes", //$NON-NLS-1$
319
                "name.relationsFromThisName", //$NON-NLS-1$
320
                "name.relationsToThisName", //$NON-NLS-1$
321
                "name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations.synonym.name.status", //$NON-NLS-1$
322
                "name.homotypicalGroup.typifiedNames.relationsToThisName.fromName", //$NON-NLS-1$
323
                "synonymRelations.synonym.name.status.type", //$NON-NLS-1$
324
                "synonymRelations.synonym.name.relationsToThisName.fromName", //$NON-NLS-1$
325
                "synonymRelations.synonym.name.nomenclaturalReference.inReference.authorship", //$NON-NLS-1$
326
                "synonymRelations.synonym.name.nomenclaturalReference.authorship", //$NON-NLS-1$
327
                "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations" //$NON-NLS-1$
328
        });
329

    
330
        return taxonBasePropertyPaths;
331
    }
332

    
333
}
(2-2/2)