Project

General

Profile

Download (13.1 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 org.eclipse.jface.resource.ImageDescriptor;
21
import org.eclipse.ui.IEditorInput;
22
import org.eclipse.ui.IMemento;
23
import org.eclipse.ui.IPersistableElement;
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.ITaxonNodeService;
29
import eu.etaxonomy.cdm.api.service.ITaxonService;
30
import eu.etaxonomy.cdm.model.common.CdmBase;
31
import eu.etaxonomy.cdm.model.name.TaxonName;
32
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
33
import eu.etaxonomy.cdm.model.taxon.Synonym;
34
import eu.etaxonomy.cdm.model.taxon.Taxon;
35
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
36
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
37
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
38
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
39
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
40
import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
41
import eu.etaxonomy.taxeditor.editor.ChooseFromMultipleTaxonNodesDialog;
42
import eu.etaxonomy.taxeditor.editor.TaxonEditorInputFactory;
43
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
44
import eu.etaxonomy.taxeditor.model.DataChangeBridge;
45
import eu.etaxonomy.taxeditor.model.MessagingUtils;
46
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
47
import eu.etaxonomy.taxeditor.store.CdmStore;
48

    
49

    
50
/**
51
 *
52
 * @author pplitzner
53
 * @date Aug 24, 2017
54
 *
55
 */
56
public class TaxonEditorInputE4  extends CdmEntitySessionInput implements IEditorInput, IConversationEnabled, IPersistableElement {
57

    
58
    private static final String INCORRECT_STATE = Messages.TaxonEditorInput_INCORRECT_STATE;
59

    
60
    private final ConversationHolder conversation;
61

    
62
    private TaxonNode taxonNode;
63

    
64
    private TaxonEditorInputDataChangeBehaviourE4 dataChangeBehavior;
65

    
66
    private TaxonBase initiallySelectedTaxonBase;
67

    
68
    private enum CdmType {
69
        TAXON_NODE,
70
        TAXON_BASE,
71
        PARENT_TAXON_NODE
72
    }
73

    
74
    private TaxonEditorInputE4(UUID uuid, CdmType type) {
75
        super(true);
76
        this.conversation = CdmStore.createConversation();
77
        switch(type) {
78
        case PARENT_TAXON_NODE:
79
            initForParentTaxonNode(uuid);
80
            break;
81
        case TAXON_BASE:
82
            initForTaxonBase(uuid);
83
            break;
84
        case TAXON_NODE:
85
            initForTaxonNode(uuid);
86
            break;
87
        }
88
    }
89

    
90
    private void init(TaxonNode taxonNode) {
91
        this.taxonNode = taxonNode;
92
    }
93

    
94

    
95
    private void initForTaxonNode(UUID taxonNodeUuid) {
96

    
97

    
98
        TaxonNode taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNodeUuid, getTaxonNodePropertyPaths());
99

    
100
        if(taxonNode == null){
101
            MessagingUtils.warningDialog(Messages.TaxonEditorInput_NOT_IMPLEMENTED, TaxonEditorInputE4.class, Messages.TaxonEditorInput_NOT_IMPLEMENTED_MESSAGE);
102
        }
103
        init(taxonNode);
104

    
105
    }
106

    
107
    private void initForTaxonBase(UUID taxonBaseUuid) {
108
        TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).load(taxonBaseUuid, getTaxonBasePropertyPaths());
109
        if (taxonBase != null){
110
            if(taxonBase.isInstanceOf(Taxon.class)){
111
                Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
112
                setInitiallySelectedTaxonBase(taxon);
113

    
114
                if (taxon.getTaxonNodes().size() == 0 && taxon.isMisapplication()){
115
                    // TODO get accepted taxon
116
                    MessagingUtils.info(Messages.TaxonEditorInput_OPEN_MISSAPPLIED_NAME);
117

    
118
                    Set<Taxon> acceptedTaxa = new HashSet<Taxon>();
119
                    Set<TaxonRelationship> relations = taxon.getRelationsFromThisTaxon();
120
                    for(TaxonRelationship relation : relations){
121
                        if(relation.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
122
                            acceptedTaxa.add(relation.getToTaxon());
123
                        }
124
                    }
125
                    setInputForMultipleTaxa(conversation, acceptedTaxa);
126

    
127
                }else{
128
                    setInputForMultipleNodes(conversation, taxon.getTaxonNodes());
129
                }
130
            }else if(taxonBase instanceof Synonym){
131
                Synonym synonym = (Synonym) taxonBase;
132

    
133
                Set<Taxon> taxa = new HashSet<>();
134
                Taxon taxon = synonym.getAcceptedTaxon();
135
                if (taxon != null){
136
                	taxa.add(taxon);
137
                }
138
                setInputForMultipleTaxa(conversation, taxa);
139
                setInitiallySelectedTaxonBase(synonym);
140
            }
141
        }
142
    }
143

    
144

    
145
    private void initForParentTaxonNode(UUID parentNodeUuid){
146

    
147

    
148
        TaxonName name = PreferencesUtil.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
149
        ITaxonTreeNode parentNode = CdmStore.getService(IClassificationService.class).getTreeNodeByUuid(parentNodeUuid);
150

    
151
        Taxon newTaxon = Taxon.NewInstance(name, parentNode.getReference());
152
        TaxonNode newTaxonNode = parentNode.addChildTaxon(newTaxon, parentNode.getReference(), parentNode.getMicroReference());
153

    
154
        // add the new taxon to the editors persistence context
155
        UUID newTaxonNodeUuid = CdmStore.getService(ITaxonNodeService.class).save(newTaxonNode).getUuid();
156

    
157
        initForTaxonNode(newTaxonNodeUuid);
158
    }
159

    
160
    private void setInputForMultipleNodes(ConversationHolder conversation, Set<TaxonNode> taxonNodes){
161
        if(taxonNodes.size() == 1){
162
            TaxonNode taxonNode = taxonNodes.iterator().next();
163
            init(taxonNode);
164
        }else if(taxonNodes.size() > 1){
165
            TaxonNode taxonNode = ChooseFromMultipleTaxonNodesDialog.choose(taxonNodes);
166
            if(taxonNode != null){
167
                init(taxonNode);
168
            }
169
        } else if (taxonNodes.size() == 0) {
170
            // this is an undesired state
171
            MessagingUtils.warningDialog(INCORRECT_STATE,TaxonEditorInputE4.class,Messages.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION);
172
        }
173
    }
174

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

    
192
    public static TaxonEditorInputE4 NewInstance(UUID taxonNodeUuid) throws Exception {
193
        return new TaxonEditorInputE4(taxonNodeUuid, CdmType.TAXON_NODE);
194

    
195
    }
196

    
197
    public static TaxonEditorInputE4 NewInstanceFromTaxonBase(UUID taxonBaseUuid){
198
        return new TaxonEditorInputE4(taxonBaseUuid, CdmType.TAXON_BASE);
199
    }
200

    
201

    
202
    public static TaxonEditorInputE4 NewEmptyInstance(UUID parentNodeUuid){
203
        return new TaxonEditorInputE4(parentNodeUuid, CdmType.PARENT_TAXON_NODE);
204
    }
205

    
206
    @Override
207
    public boolean exists() {
208
        return taxonNode != null;
209
    }
210

    
211
    @Override
212
    public ImageDescriptor getImageDescriptor() {
213
        return null;
214
    }
215

    
216
    @Override
217
    public String getName() {
218
        if(getTaxon() == null){
219
            return null;
220
        }
221
        TaxonName name = getTaxon().getName();
222
        if (name == null || name.getTitleCache() == null) {
223
            return Messages.TaxonEditorInput_NEW_TAXON;
224
        } else {
225
            return name.getTitleCache();
226
        }
227
    }
228

    
229
    @Override
230
    public IPersistableElement getPersistable() {
231
        return null;
232
    }
233

    
234
    @Override
235
    public String getToolTipText() {
236
        return getName();
237
    }
238

    
239
    /** {@inheritDoc} */
240
    @Override
241
    public Object getAdapter(Class adapter) {
242

    
243
        if (adapter == Taxon.class) {
244
            return getTaxon();
245
        }
246

    
247
        if (adapter == TaxonNode.class) {
248
            return taxonNode;
249
        }
250

    
251
        return null;
252
    }
253

    
254
    /**
255
     * {@inheritDoc}
256
     *
257
     * Overrides equals to ensure that a taxon can only be edited by
258
     * one editor at a time.
259
     */
260
    @Override
261
    public boolean equals(Object obj) {
262
        if (TaxonEditorInputE4.class.equals(obj.getClass())
263
                && getTaxon() != null
264
                && getTaxon().equals(((TaxonEditorInputE4) obj).getTaxon())) {
265
            if (((TaxonEditorInputE4) obj).getInitiallySelectedTaxonBase() != null){
266
                setInitiallySelectedTaxonBase(((TaxonEditorInputE4) obj).getInitiallySelectedTaxonBase());
267
            }
268
            return true;
269
        }
270
        return false;
271
    }
272

    
273
    public Taxon getTaxon(){
274
        Taxon taxon = CdmBase.deproxy(taxonNode.getTaxon(), Taxon.class);
275
        return taxon;
276
    }
277

    
278
    public TaxonNode getTaxonNode() {
279
        return taxonNode;
280
    }
281

    
282
    @Override
283
    public ConversationHolder getConversationHolder() {
284
        return conversation;
285
    }
286

    
287
    /** {@inheritDoc} */
288
    @Override
289
    public void update(CdmDataChangeMap events) {
290
        if(dataChangeBehavior == null){
291
            dataChangeBehavior = new TaxonEditorInputDataChangeBehaviourE4(this);
292
        }
293

    
294
        DataChangeBridge.handleDataChange(events, dataChangeBehavior);
295
    }
296

    
297
    @Override
298
    public String getFactoryId() {
299
        return TaxonEditorInputFactory.getFactoryId();
300
    }
301

    
302
    /** {@inheritDoc} */
303
    @Override
304
    public void saveState(IMemento memento) {
305
        TaxonEditorInputFactoryE4.saveState(memento, this);
306
    }
307

    
308

    
309
    public void setInitiallySelectedTaxonBase(TaxonBase taxonBase) {
310
        this.initiallySelectedTaxonBase = taxonBase;
311
    }
312

    
313
    public TaxonBase getInitiallySelectedTaxonBase() {
314
        return initiallySelectedTaxonBase;
315
    }
316

    
317
    @Override
318
    public List<TaxonNode> getRootEntities() {
319
        return Arrays.asList(taxonNode);
320
    }
321

    
322
    @Override
323
    public void merge() {
324
       CdmStore.getService(ITaxonNodeService.class).merge(taxonNode, true);
325

    
326
    }
327

    
328
    @Override
329
    public Map<Object, List<String>> getPropertyPathsMap() {
330
        return null;
331
    }
332

    
333
    private List<String> getTaxonNodePropertyPaths() {
334
        List<String> taxonNodePropertyPaths = new ArrayList<String>();
335
        for(String propertyPath : getTaxonBasePropertyPaths()) {
336
            taxonNodePropertyPaths.add("taxon." + propertyPath); //$NON-NLS-1$
337
        }
338
        return taxonNodePropertyPaths;
339
    }
340

    
341
    private List<String> getTaxonBasePropertyPaths() {
342
        List<String> taxonBasePropertyPaths = Arrays.asList(new String[] {
343
                "sec", //$NON-NLS-1$
344
                "createdBy", //$NON-NLS-1$
345
                "updatedBy", //$NON-NLS-1$
346
                "annotations", //$NON-NLS-1$
347
                "markers", //$NON-NLS-1$
348
                "credits", //$NON-NLS-1$
349
                "extensions", //$NON-NLS-1$
350
                "rights", //$NON-NLS-1$
351
                "sources", //$NON-NLS-1$
352
                "descriptions", //$NON-NLS-1$
353
                "relationsToThisTaxon", //$NON-NLS-1$
354
                "relationsFromThisTaxon", //$NON-NLS-1$
355
                "taxonNodes", //$NON-NLS-1$
356
                "descriptions.descriptionElements.feature", //$NON-NLS-1$
357
                "descriptions.descriptionElements.area", //$NON-NLS-1$
358
                "descriptions.descriptionElements.status", //$NON-NLS-1$
359
                "descriptions.markers", //$NON-NLS-1$
360
                "name.descriptions", //$NON-NLS-1$
361
                "name.typeDesignations", //$NON-NLS-1$
362
                "name.status", //$NON-NLS-1$
363
                "name.nomenclaturalReference.inReference", //$NON-NLS-1$
364
                "name.taxonBases.taxonNodes", //$NON-NLS-1$
365
                "name.relationsFromThisName", //$NON-NLS-1$
366
                "name.relationsToThisName", //$NON-NLS-1$
367
                "name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations.synonym.name.status", //$NON-NLS-1$
368
                "name.homotypicalGroup.typifiedNames.relationsToThisName.fromName", //$NON-NLS-1$
369
                "synonymRelations.synonym.name.status.type", //$NON-NLS-1$
370
                "synonymRelations.synonym.name.relationsToThisName.fromName", //$NON-NLS-1$
371
                "synonymRelations.synonym.name.nomenclaturalReference.inReference.authorship", //$NON-NLS-1$
372
                "synonymRelations.synonym.name.nomenclaturalReference.authorship", //$NON-NLS-1$
373
                "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations" //$NON-NLS-1$
374
        });
375

    
376
        return taxonBasePropertyPaths;
377
    }
378

    
379
}
(2-2/3)