Project

General

Profile

Download (16.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;
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.TaxonNameBase;
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.model.DataChangeBridge;
41
import eu.etaxonomy.taxeditor.model.MessagingUtils;
42
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
43
import eu.etaxonomy.taxeditor.store.CdmStore;
44

    
45

    
46
/**
47
 * <p>TaxonEditorInput class.</p>
48
 *
49
 * @author n.hoffmann
50
 * @created 19.03.2009
51
 * @version 1.0
52
 */
53
public class TaxonEditorInput  extends CdmEntitySessionInput implements IEditorInput, IConversationEnabled, IPersistableElement {
54

    
55
    private static final String INCORRECT_STATE = Messages.TaxonEditorInput_INCORRECT_STATE;
56

    
57
    private final ConversationHolder conversation;
58

    
59
    private TaxonNode taxonNode;
60

    
61
    private TaxonEditorInputDataChangeBehaviour dataChangeBehavior;
62

    
63
    private TaxonBase initiallySelectedTaxonBase;
64

    
65
    private enum CdmType {
66
        TAXON_NODE,
67
        TAXON_BASE,
68
        PARENT_TAXON_NODE
69
    }
70

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

    
87
    private void init(TaxonNode taxonNode) {
88
        this.taxonNode = taxonNode;
89
    }
90

    
91

    
92
    /**
93
     * <p>NewInstance</p>
94
     *
95
     * @param taxonNodeUuid a {@link java.util.UUID} object.
96
     * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
97
     * @throws java.lang.Exception if any.
98
     */
99
    private void initForTaxonNode(UUID taxonNodeUuid) {
100

    
101

    
102
        TaxonNode taxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNodeUuid, getTaxonNodePropertyPaths());
103

    
104
        if(taxonNode == null){
105
            MessagingUtils.warningDialog(Messages.TaxonEditorInput_NOT_IMPLEMENTED, TaxonEditorInput.class, Messages.TaxonEditorInput_NOT_IMPLEMENTED_MESSAGE);
106
        }
107
        init(taxonNode);
108

    
109
    }
110

    
111
    private void initForTaxonBase(UUID taxonBaseUuid) {
112
        TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).load(taxonBaseUuid, getTaxonBasePropertyPaths());
113
        if (taxonBase != null){
114
            if(taxonBase.isInstanceOf(Taxon.class)){
115
                Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
116

    
117
                if (taxon.getTaxonNodes().size() == 0 && taxon.isMisapplication()){
118
                    // TODO get accepted taxon
119
                    MessagingUtils.info(Messages.TaxonEditorInput_OPEN_MISSAPPLIED_NAME);
120

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

    
130
                }else{
131
                    setInputForMultipleNodes(conversation, taxon.getTaxonNodes());
132
                }
133
            }else if(taxonBase instanceof Synonym){
134
                Synonym synonym = (Synonym) taxonBase;
135

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

    
146

    
147
    /**
148
     * <p>NewEmptyInstance</p>
149
     *
150
     * @param parentNodeUuid a {@link java.util.UUID} object.
151
     * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
152
     */
153
    private void initForParentTaxonNode(UUID parentNodeUuid){
154

    
155

    
156
        TaxonNameBase<?, ?> name = PreferencesUtil.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
157
        ITaxonTreeNode parentNode = CdmStore.getService(IClassificationService.class).getTreeNodeByUuid(parentNodeUuid);
158

    
159
        Taxon newTaxon = Taxon.NewInstance(name, parentNode.getReference());
160
        TaxonNode newTaxonNode = parentNode.addChildTaxon(newTaxon, parentNode.getReference(), parentNode.getMicroReference());
161

    
162
        // add the new taxon to the editors persistence context
163
        UUID newTaxonNodeUuid = CdmStore.getService(ITaxonNodeService.class).save(newTaxonNode).getUuid();
164

    
165
        initForTaxonNode(newTaxonNodeUuid);
166
    }
167

    
168

    
169

    
170

    
171
    private void setInputForMultipleNodes(ConversationHolder conversation, Set<TaxonNode> taxonNodes){
172
        if(taxonNodes.size() == 1){
173
            TaxonNode taxonNode = taxonNodes.iterator().next();
174
            init(taxonNode);
175
        }else if(taxonNodes.size() > 1){
176
            TaxonNode taxonNode = ChooseFromMultipleTaxonNodesDialog.choose(taxonNodes);
177
            if(taxonNode != null){
178
                init(taxonNode);
179
            }
180
        } else if (taxonNodes.size() == 0) {
181
            // this is an undesired state
182
            MessagingUtils.warningDialog(INCORRECT_STATE,TaxonEditorInput.class,Messages.TaxonEditorInput_TAXON_NOT_IN_CLASSIFICATION);
183
        }
184
    }
185

    
186
    private void setInputForMultipleTaxa(ConversationHolder conversation, Set<Taxon> taxa){
187
        if(taxa.size() == 1){
188
            Taxon taxon = taxa.iterator().next();
189
            Set<TaxonNode> nodes = taxon.getTaxonNodes();
190
            setInputForMultipleNodes(conversation, nodes);
191
        }else if(taxa.size() > 1){
192
            Set<TaxonNode> taxonNodes = new HashSet<TaxonNode>();
193
            for ( Taxon taxon : taxa ){
194
                taxonNodes.addAll(taxon.getTaxonNodes());
195
            }
196
            setInputForMultipleNodes(conversation, taxonNodes);
197
        }else if(taxa.size() == 0){
198
            // this is an undesired state
199
            MessagingUtils.warningDialog(INCORRECT_STATE, TaxonEditorInput.class, Messages.TaxonEditorInput_NO_ACCEPTED_TAXON_PRESENT);
200
        }
201
    }
202

    
203
    /**
204
     * <p>NewInstance</p>
205
     *
206
     * @param taxonNodeUuid a {@link java.util.UUID} object.
207
     * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
208
     * @throws java.lang.Exception if any.
209
     */
210
    public static TaxonEditorInput NewInstance(UUID taxonNodeUuid) throws Exception {
211
        return new TaxonEditorInput(taxonNodeUuid, CdmType.TAXON_NODE);
212

    
213
    }
214

    
215
    /**
216
     * <p>NewInstanceFromTaxonBase</p>
217
     *
218
     * @param taxonBaseUuid a {@link java.util.UUID} object.
219
     * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
220
     */
221
    public static TaxonEditorInput NewInstanceFromTaxonBase(UUID taxonBaseUuid){
222
        return new TaxonEditorInput(taxonBaseUuid, CdmType.TAXON_BASE);
223
    }
224

    
225

    
226
    /**
227
     * <p>NewEmptyInstance</p>
228
     *
229
     * @param parentNodeUuid a {@link java.util.UUID} object.
230
     * @return a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
231
     */
232
    public static TaxonEditorInput NewEmptyInstance(UUID parentNodeUuid){
233
        return new TaxonEditorInput(parentNodeUuid, CdmType.PARENT_TAXON_NODE);
234
    }
235

    
236
    /* (non-Javadoc)
237
     * @see org.eclipse.ui.IEditorInput#exists()
238
     */
239
    /**
240
     * <p>exists</p>
241
     *
242
     * @return a boolean.
243
     */
244
    @Override
245
    public boolean exists() {
246
        return taxonNode != null;
247
    }
248

    
249
    /* (non-Javadoc)
250
     * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
251
     */
252
    /**
253
     * <p>getImageDescriptor</p>
254
     *
255
     * @return a {@link org.eclipse.jface.resource.ImageDescriptor} object.
256
     */
257
    @Override
258
    public ImageDescriptor getImageDescriptor() {
259
        return null;
260
    }
261

    
262
    /* (non-Javadoc)
263
     * @see org.eclipse.ui.IEditorInput#getName()
264
     */
265
    /**
266
     * <p>getName</p>
267
     *
268
     * @return a {@link java.lang.String} object.
269
     */
270
    @Override
271
    public String getName() {
272
        if(getTaxon() == null){
273
            return null;
274
        }
275
        TaxonNameBase<?, ?> name = getTaxon().getName();
276
        if (name == null || name.getTitleCache() == null) {
277
            return Messages.TaxonEditorInput_NEW_TAXON;
278
        } else {
279
            return name.getTitleCache();
280
        }
281
    }
282

    
283
    /* (non-Javadoc)
284
     * @see org.eclipse.ui.IEditorInput#getPersistable()
285
     */
286
    /**
287
     * <p>getPersistable</p>
288
     *
289
     * @return a {@link org.eclipse.ui.IPersistableElement} object.
290
     */
291
    @Override
292
    public IPersistableElement getPersistable() {
293
        //		if(CdmStore.isActive()){
294
        //			TaxonNode test = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid());
295
        //			boolean isPersistable = CdmStore.getTaxonTreeService().getTaxonNodeByUuid(taxonNode.getUuid()) != null;
296
        //			if (isPersistable) {
297
        //				return this;
298
        //			}
299
        //		}
300
        return null;
301
    }
302

    
303
    /* (non-Javadoc)
304
     * @see org.eclipse.ui.IEditorInput#getToolTipText()
305
     */
306
    /**
307
     * <p>getToolTipText</p>
308
     *
309
     * @return a {@link java.lang.String} object.
310
     */
311
    @Override
312
    public String getToolTipText() {
313
        return getName();
314
    }
315

    
316
    /* (non-Javadoc)
317
     * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
318
     */
319
    /** {@inheritDoc} */
320
    @Override
321
    public Object getAdapter(Class adapter) {
322

    
323
        if (adapter == Taxon.class) {
324
            return getTaxon();
325
        }
326

    
327
        if (adapter == TaxonNode.class) {
328
            return taxonNode;
329
        }
330

    
331
        return null;
332
    }
333

    
334
    /**
335
     * {@inheritDoc}
336
     *
337
     * Overrides equals to ensure that a taxon can only be edited by
338
     * one editor at a time.
339
     */
340
    @Override
341
    public boolean equals(Object obj) {
342
        if (TaxonEditorInput.class.equals(obj.getClass())
343
                && getTaxon() != null
344
                && getTaxon().equals(((TaxonEditorInput) obj).getTaxon())) {
345
            if (((TaxonEditorInput) obj).getInitiallySelectedTaxonBase() != null){
346
                setInitiallySelectedTaxonBase(((TaxonEditorInput) obj).getInitiallySelectedTaxonBase());
347
            }
348
            return true;
349
        }
350
        return false;
351
    }
352

    
353
    /**
354
     * <p>getTaxon</p>
355
     *
356
     * @return the taxon
357
     */
358
    public Taxon getTaxon(){
359
        Taxon taxon = CdmBase.deproxy(taxonNode.getTaxon(), Taxon.class);
360
        return taxon;
361
    }
362

    
363
    /**
364
     * <p>Getter for the field <code>taxonNode</code>.</p>
365
     *
366
     * @return the taxonNode
367
     */
368
    public TaxonNode getTaxonNode() {
369
        return taxonNode;
370
    }
371

    
372
    /*
373
     * (non-Javadoc)
374
     * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
375
     */
376
    /**
377
     * <p>getConversationHolder</p>
378
     *
379
     * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
380
     */
381
    @Override
382
    public ConversationHolder getConversationHolder() {
383
        return conversation;
384
    }
385

    
386
    /*
387
     * (non-Javadoc)
388
     * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
389
     */
390
    /** {@inheritDoc} */
391
    @Override
392
    public void update(CdmDataChangeMap events) {
393
        if(dataChangeBehavior == null){
394
            dataChangeBehavior = new TaxonEditorInputDataChangeBehaviour(this);
395
        }
396

    
397
        DataChangeBridge.handleDataChange(events, dataChangeBehavior);
398
    }
399

    
400
    /* (non-Javadoc)
401
     * @see org.eclipse.ui.IPersistableElement#getFactoryId()
402
     */
403
    /**
404
     * <p>getFactoryId</p>
405
     *
406
     * @return a {@link java.lang.String} object.
407
     */
408
    @Override
409
    public String getFactoryId() {
410
        return TaxonEditorInputFactory.getFactoryId();
411
    }
412

    
413
    /* (non-Javadoc)
414
     * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento)
415
     */
416
    /** {@inheritDoc} */
417
    @Override
418
    public void saveState(IMemento memento) {
419
        TaxonEditorInputFactory.saveState(memento, this);
420
    }
421

    
422

    
423
    /**
424
     * <p>Setter for the field <code>initiallySelectedTaxonBase</code>.</p>
425
     *
426
     * @param taxonBase a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
427
     */
428
    public void setInitiallySelectedTaxonBase(TaxonBase taxonBase) {
429
        this.initiallySelectedTaxonBase = taxonBase;
430
    }
431

    
432
    /**
433
     * <p>Getter for the field <code>initiallySelectedTaxonBase</code>.</p>
434
     *
435
     * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonBase} object.
436
     */
437
    public TaxonBase getInitiallySelectedTaxonBase() {
438
        return initiallySelectedTaxonBase;
439
    }
440

    
441
//    @Override
442
//    public String toString() {
443
//        return String.format("%s[%s]", this.getClass().getSimpleName(), getTaxon());
444
//    }
445

    
446

    
447
    /* (non-Javadoc)
448
     * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
449
     */
450
    @Override
451
    public List<TaxonNode> getRootEntities() {
452
        return Arrays.asList(taxonNode);
453
    }
454

    
455
    /* (non-Javadoc)
456
     * @see eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput#merge()
457
     */
458
    @Override
459
    public void merge() {
460
       CdmStore.getService(ITaxonNodeService.class).merge(taxonNode, true);
461

    
462
    }
463

    
464
    @Override
465
    public Map<Object, List<String>> getPropertyPathsMap() {
466
        return null;
467
    }
468

    
469
    private List<String> getTaxonNodePropertyPaths() {
470
        List<String> taxonNodePropertyPaths = new ArrayList<String>();
471
        for(String propertyPath : getTaxonBasePropertyPaths()) {
472
            taxonNodePropertyPaths.add("taxon." + propertyPath); //$NON-NLS-1$
473
        }
474
        return taxonNodePropertyPaths;
475
    }
476

    
477
    private List<String> getTaxonBasePropertyPaths() {
478
        List<String> taxonBasePropertyPaths = Arrays.asList(new String[] {
479
                "sec", //$NON-NLS-1$
480
                "createdBy", //$NON-NLS-1$
481
                "updatedBy", //$NON-NLS-1$
482
                "annotations", //$NON-NLS-1$
483
                "markers", //$NON-NLS-1$
484
                "credits", //$NON-NLS-1$
485
                "extensions", //$NON-NLS-1$
486
                "rights", //$NON-NLS-1$
487
                "sources", //$NON-NLS-1$
488
                "descriptions", //$NON-NLS-1$
489
                "relationsToThisTaxon", //$NON-NLS-1$
490
                "relationsFromThisTaxon", //$NON-NLS-1$
491
                "taxonNodes", //$NON-NLS-1$
492
                "descriptions.descriptionElements.feature", //$NON-NLS-1$
493
                "descriptions.descriptionElements.area", //$NON-NLS-1$
494
                "descriptions.descriptionElements.status", //$NON-NLS-1$
495
                "descriptions.markers", //$NON-NLS-1$
496
                "name.descriptions", //$NON-NLS-1$
497
                "name.typeDesignations", //$NON-NLS-1$
498
                "name.status", //$NON-NLS-1$
499
                "name.nomenclaturalReference.inReference", //$NON-NLS-1$
500
                "name.taxonBases.taxonNodes", //$NON-NLS-1$
501
                "name.relationsFromThisName", //$NON-NLS-1$
502
                "name.relationsToThisName", //$NON-NLS-1$
503
                "name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations.synonym.name.status", //$NON-NLS-1$
504
                "name.homotypicalGroup.typifiedNames.relationsToThisName.fromName", //$NON-NLS-1$
505
                "synonymRelations.synonym.name.status.type", //$NON-NLS-1$
506
                "synonymRelations.synonym.name.relationsToThisName.fromName", //$NON-NLS-1$
507
                "synonymRelations.synonym.name.nomenclaturalReference.inReference.authorship", //$NON-NLS-1$
508
                "synonymRelations.synonym.name.nomenclaturalReference.authorship", //$NON-NLS-1$
509
                "synonymRelations.synonym.name.homotypicalGroup.typifiedNames.taxonBases.synonymRelations" //$NON-NLS-1$
510
        });
511

    
512
        return taxonBasePropertyPaths;
513
    }
514

    
515
}
(14-14/19)