Project

General

Profile

Download (14.6 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.Arrays;
13
import java.util.HashSet;
14
import java.util.List;
15
import java.util.Set;
16
import java.util.UUID;
17

    
18
import org.eclipse.jface.resource.ImageDescriptor;
19
import org.eclipse.ui.IEditorInput;
20
import org.eclipse.ui.IMemento;
21
import org.eclipse.ui.IPersistableElement;
22

    
23
import eu.etaxonomy.cdm.api.application.CdmChangeEvent;
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.ITaxonNodeService;
28
import eu.etaxonomy.cdm.api.service.ITaxonService;
29
import eu.etaxonomy.cdm.model.common.CdmBase;
30
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
31
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
32
import eu.etaxonomy.cdm.model.taxon.Synonym;
33
import eu.etaxonomy.cdm.model.taxon.Taxon;
34
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
35
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
36
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
37
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
38
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
39
import eu.etaxonomy.taxeditor.model.DataChangeBridge;
40
import eu.etaxonomy.taxeditor.model.MessagingUtils;
41
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
42
import eu.etaxonomy.taxeditor.store.CdmStore;
43

    
44

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

    
54
    private final ConversationHolder conversation;
55

    
56
    private TaxonNode taxonNode;
57

    
58
    private TaxonEditorInputDataChangeBehaviour dataChangeBehavior;
59

    
60
    private TaxonBase initiallySelectedTaxonBase;
61

    
62
    private final ITaxonNodeService service;
63

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

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

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

    
90

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

    
100

    
101
        TaxonNode taxonNode = getCdmEntitySession().remoteLoad(service,taxonNodeUuid, null);
102

    
103
        if(taxonNode == null){
104
            MessagingUtils.warningDialog("Not yet implemented", TaxonEditorInput.class, "Selected element is not type TaxonBase.");
105
        }
106
        init(taxonNode);
107

    
108
    }
109

    
110
    private void initForTaxonBase(UUID taxonBaseUuid) {
111

    
112
        TaxonBase taxonBase = getCdmEntitySession().remoteLoad(CdmStore.getService(ITaxonService.class),taxonBaseUuid);
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("trying to open Mispplied 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 = synonym.getAcceptedTaxa();
137
                setInputForMultipleTaxa(conversation, taxa);
138
            }
139
        }
140
    }
141

    
142

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

    
151

    
152
        TaxonNameBase<?, ?> name = PreferencesUtil.getPreferredNomenclaturalCode().getNewTaxonNameInstance(null);
153
        ITaxonTreeNode parentNode = CdmStore.getService(IClassificationService.class).getTreeNodeByUuid(parentNodeUuid);
154

    
155
        Taxon newTaxon = Taxon.NewInstance(name, parentNode.getReference());
156
        TaxonNode newTaxonNode = parentNode.addChildTaxon(newTaxon, parentNode.getReference(), parentNode.getMicroReference());
157

    
158
        // add the new taxon to the editors persistence context
159
        UUID newTaxonNodeUuid = getCdmEntitySession().remoteSave(CdmStore.getService(ITaxonNodeService.class),newTaxonNode);
160

    
161
        initForTaxonNode(newTaxonNodeUuid);
162
    }
163

    
164

    
165

    
166

    
167
    private void setInputForMultipleNodes(ConversationHolder conversation, Set<TaxonNode> taxonNodes){
168
        if(taxonNodes.size() == 1){
169
            TaxonNode taxonNode = taxonNodes.iterator().next();
170
            init(taxonNode);
171
        }else if(taxonNodes.size() > 1){
172
            TaxonNode taxonNode = ChooseFromMultipleTaxonNodesDialog.choose(taxonNodes);
173
            if(taxonNode != null){
174
                init(taxonNode);
175
            }
176
        }else if(taxonNodes.size() == 0){
177
            // this is an undesired state
178
            MessagingUtils.warningDialog("Incorrect state", TaxonEditorInput.class, "The accepted taxon is not part of any classification. This should not have happened.");
179
        }
180
    }
181

    
182
    private void setInputForMultipleTaxa(ConversationHolder conversation, Set<Taxon> taxa){
183
        if(taxa.size() == 1){
184
            Taxon taxon = taxa.iterator().next();
185
            Set<TaxonNode> nodes = taxon.getTaxonNodes();
186
            setInputForMultipleNodes(conversation, nodes);
187
        }else if(taxa.size() > 1){
188
            Set<TaxonNode> taxonNodes = new HashSet<TaxonNode>();
189
            for ( Taxon taxon : taxa ){
190
                taxonNodes.addAll(taxon.getTaxonNodes());
191
            }
192
            setInputForMultipleNodes(conversation, taxonNodes);
193
        }else if(taxa.size() == 0){
194
            // this is an undesired state
195
            MessagingUtils.warningDialog("Incorrect state", TaxonEditorInput.class, "Trying to open accepted taxon for a synonym or misapplication but" +
196
                    " no accepted taxa are present. This should not have happened.");
197
        }
198
    }
199

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

    
210
    }
211

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

    
222

    
223

    
224

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

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

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

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

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

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

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

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

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

    
330
        return null;
331
    }
332

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

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

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

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

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

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

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

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

    
421

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

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

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

    
445
    /* (non-Javadoc)
446
     * @see eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput#getService()
447
     */
448
    @Override
449
    public ITaxonNodeService getService() {
450
        return service;
451
    }
452

    
453

    
454
    /* (non-Javadoc)
455
     * @see eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput#getRootEntities()
456
     */
457
    @Override
458
    public  List<TaxonNode> getRootEntities() {
459
        return Arrays.asList(taxonNode);
460
    }
461

    
462

    
463
    /* (non-Javadoc)
464
     * @see eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput#update()
465
     */
466
    @Override
467
    public <T extends CdmBase> void update() {
468
        taxonNode = getCdmEntitySession().remoteUpdate(service, taxonNode);
469
    }
470

    
471
    /* (non-Javadoc)
472
     * @see eu.etaxonomy.cdm.api.application.ICdmChangeListener#onChange(eu.etaxonomy.cdm.api.application.CdmChangeEvent)
473
     */
474
    @Override
475
    public void onChange(CdmChangeEvent event) {
476
        // TODO Auto-generated method stub
477

    
478
    }
479

    
480
}
(15-15/19)