Project

General

Profile

Download (5.65 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
package eu.etaxonomy.taxeditor.newWizard;
10

    
11
import java.util.Map;
12
import java.util.UUID;
13

    
14
import javax.inject.Inject;
15

    
16
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
17
import eu.etaxonomy.cdm.api.application.CdmChangeEvent;
18
import eu.etaxonomy.cdm.api.application.CdmChangeEvent.Action;
19
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
20
import eu.etaxonomy.cdm.api.service.UpdateResult;
21
import eu.etaxonomy.cdm.api.service.dto.CreateTaxonDTO;
22
import eu.etaxonomy.cdm.model.common.Language;
23
import eu.etaxonomy.cdm.model.common.LanguageString;
24
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
25
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
26
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
27
import eu.etaxonomy.cdm.model.taxon.TaxonNodeStatus;
28
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
29
import eu.etaxonomy.taxeditor.l10n.Messages;
30
import eu.etaxonomy.taxeditor.model.MessagingUtils;
31
import eu.etaxonomy.taxeditor.store.CdmStore;
32
import eu.etaxonomy.taxeditor.ui.section.classification.TaxonNodeDetailElement;
33
import eu.etaxonomy.taxeditor.ui.section.classification.TaxonNodeWizardPage;
34

    
35
/**
36
 * NewTaxonNodeWizard class.
37
 *
38
 * @author n.hoffmann
39
 * @created Sep 15, 2009
40
 */
41
public class NewTaxonNodeWizard extends AbstractNewEntityWizardParentChild<TaxonNode>{
42

    
43
    private TaxonNodeWizardPage taxonNodePage;
44
    private boolean openEmptyEditor;
45
    private UUID generatedTaxonNodeUuid;
46
    private IWizardPageListener wizardPageListener;
47
    private boolean isOpenInEditor = false;
48
    private boolean success = true;
49

    
50
    @Inject
51
    public NewTaxonNodeWizard(){
52
        setWindowTitle(Messages.TaxonNodeWizardPage_new);
53
    }
54

    
55
    @Override
56
    public void addPages() {
57
        taxonNodePage = new TaxonNodeWizardPage(formFactory, getConversationHolder(), getParentEntity(), wizardPageListener, true);
58
        addPage(taxonNodePage);
59
    }
60

    
61
    @Override
62
    protected void saveEntity() {
63
        CreateTaxonDTO taxon = taxonNodePage.createTaxon();
64
        if(taxon == null ){
65
            openEmptyEditor = true;
66
        }else{
67
            getConversationHolder().bind();
68
            try{
69
             	UpdateResult result;
70
             	TaxonNode parent = getParentEntity();
71

    
72
             	Map<Language, LanguageString> notes = ((TaxonNodeDetailElement)taxonNodePage.getDetailElement()).getMultiLanguageTextExcludedNotes();
73
             	DescriptionElementSource source = parent.getSource().checkEmpty()? null: parent.getSource();
74

    
75

    
76
             	TaxonNodeStatus status = ((TaxonNodeDetailElement)taxonNodePage.getDetailElement()).getTaxonNodeStatus();
77
             	if (status == null){
78
             	    notes = null;
79
             	}
80
             	if (taxonNodePage.getExistingTaxon() != null){
81
             	    result = CdmStore.getService(ITaxonNodeService.class).createNewTaxonNode(parent.getUuid(), taxonNodePage.getExistingTaxon().getUuid(), null, null);
82
             	}else{
83
             	    result = CdmStore.getService(ITaxonNodeService.class).createNewTaxonNode(parent.getUuid(), taxon, source, parent.getMicroReference(),
84
            	         status, notes);
85
             	}
86

    
87
            	if (result.isOk()){
88
            	    this.setEntity((TaxonNode)result.getCdmEntity());
89
                    CdmApplicationState.getCurrentDataChangeService()
90
                             .fireChangeEvent(new CdmChangeEvent(Action.Create, result.getUpdatedObjects(), NewTaxonNodeWizard.class), true);
91

    
92
	             }else{
93
	                 setSuccess(false);
94
	                 if (!result.isOk()){
95
	                     if (!result.getExceptions().isEmpty()){
96
	                         MessagingUtils.error(getClass(), "Taxon could not be created", result.getExceptions().iterator().next());
97
	                     }else{
98
	                         MessagingUtils.warn(getClass(), "Taxon could not be created");
99
	                     }
100

    
101
	                 }
102
	             }
103
          }catch(IllegalArgumentException e){
104
                MessagingUtils.warningDialog("Taxon already exists in classfication", getClass(), e.getMessage());
105
            }
106
        }
107
    }
108

    
109
    public boolean openInEditor() {
110
        return isOpenInEditor ;
111
    }
112

    
113
    public void setOpenInEditor(boolean isOpenInEditor) {
114
        this.isOpenInEditor = isOpenInEditor;
115
    }
116

    
117
    public boolean openEmpty(){
118
        return openInEditor() && openEmptyEditor;
119
    }
120

    
121
    public TaxonNode getTaxonNode(){
122
        if(generatedTaxonNodeUuid != null){
123
            return CdmStore.getService(ITaxonNodeService.class).load(generatedTaxonNodeUuid);
124
        }
125
        return null;
126
    }
127

    
128
    public ITaxonTreeNode getParentTreeNode(){
129
        return taxonNodePage.getParentTreeNode();
130
    }
131

    
132
    @Override
133
    protected String getEntityName() {
134
        return "Taxon";
135
    }
136

    
137
    public void addWizardPageListener(IWizardPageListener wizardPageListener){
138
        this.wizardPageListener = wizardPageListener;
139
    }
140

    
141
    public boolean isSuccess() {
142
        return success;
143
    }
144

    
145
    public void setSuccess(boolean success) {
146
        this.success = success;
147
    }
148

    
149
    @Override
150
    public void createParentEntity() {
151
        Object selectedObject = getSelection().getFirstElement();
152
        if(selectedObject instanceof TaxonNodeDto){
153
            if(selectedObject instanceof TaxonNodeDto){
154
                TaxonNodeDto taxonNodeDto = (TaxonNodeDto) selectedObject;
155
                setParentEntity(CdmStore.getService(ITaxonNodeService.class).load(taxonNodeDto.getUuid()));
156
            }
157
        }
158
    }
159
}
(21-21/28)