Project

General

Profile

Download (6.22 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 *
3
 */
4
package eu.etaxonomy.taxeditor.newWizard;
5

    
6
import java.util.Map;
7
import java.util.UUID;
8

    
9
import javax.inject.Inject;
10

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

    
30
/**
31
 * <p>NewTaxonNodeWizard class.</p>
32
 *
33
 * @author n.hoffmann
34
 * @created Sep 15, 2009
35
 */
36
public class NewTaxonNodeWizard extends AbstractNewEntityWizardParentChild<TaxonNode>{
37

    
38
    private TaxonNodeWizardPage taxonNodePage;
39
    private boolean openEmptyEditor;
40
    private UUID generatedTaxonNodeUuid;
41
    private IWizardPageListener wizardPageListener;
42
    private boolean isOpenInEditor = false;
43
    private boolean success = true;
44

    
45
    @Inject
46
    public NewTaxonNodeWizard(){
47
        setWindowTitle(Messages.TaxonNodeWizardPage_new);
48
    }
49

    
50
    @Override
51
    public void addPages() {
52
        taxonNodePage = new TaxonNodeWizardPage(formFactory, getConversationHolder(), getParentEntity(), wizardPageListener, true);
53
        addPage(taxonNodePage);
54
    }
55

    
56
    @Override
57
    protected void saveEntity() {
58
        CreateTaxonDTO taxon = taxonNodePage.createTaxon();
59
        if(taxon == null ){
60
            openEmptyEditor = true;
61
        }else{
62
            getConversationHolder().bind();
63
            try{
64
             	UpdateResult result;
65
             	TaxonNode parent = getParentEntity();
66

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

    
70

    
71
             	TaxonNodeStatus status = ((TaxonNodeDetailElement)taxonNodePage.getDetailElement()).getTaxonNodeStatus();
72
             	if (status == null){
73
             	    notes = null;
74
             	}
75
             	if (taxonNodePage.getExistingTaxon() != null){
76
             	    result = CdmStore.getService(ITaxonNodeService.class).createNewTaxonNode(parent.getUuid(), taxonNodePage.getExistingTaxon().getUuid(), null, null);
77
             	}else{
78
             	    result = CdmStore.getService(ITaxonNodeService.class).createNewTaxonNode(parent.getUuid(), taxon, source, parent.getMicroReference(),
79
            	         status, notes);
80
             	}
81

    
82
            	if (result.isOk()){
83
            	    this.setEntity((TaxonNode)result.getCdmEntity());
84
                    CdmApplicationState.getCurrentDataChangeService()
85
                             .fireChangeEvent(new CdmChangeEvent(Action.Create, result.getUpdatedObjects(), NewTaxonNodeWizard.class), true);
86

    
87
	             }else{
88
	                 setSuccess(false);
89
	                 if (!result.isOk()){
90
	                     if (!result.getExceptions().isEmpty()){
91
	                         MessagingUtils.error(getClass(), "Taxon could not be created", result.getExceptions().iterator().next());
92
	                     }else{
93
	                         MessagingUtils.warn(getClass(), "Taxon could not be created");
94
	                     }
95

    
96
	                 }
97
	             }
98
          }catch(IllegalArgumentException e){
99
                MessagingUtils.warningDialog("Taxon already exists in classfication", getClass(), e.getMessage());
100
            }
101
        }
102
    }
103

    
104
//    @Override
105
//    protected TaxonNode createNewEntity() {
106
//        if(getSelection() != null){
107
//            Object selectedObject = getSelection().getFirstElement();
108
//            if(selectedObject instanceof TaxonNodeDto){
109
//                TaxonNodeDto taxonNodeDto = (TaxonNodeDto) selectedObject;
110
//            	TaxonNode node =  CdmStore.getService(ITaxonNodeService.class).load(taxonNodeDto.getUuid());
111
//            	Reference sec = node.getTaxon() != null? node.getTaxon().getSec():null;
112
//            	Taxon newTaxon = Taxon.NewInstance(TaxonNameFactory.NewNameInstance(PreferencesUtil.getPreferredNomenclaturalCode(), null), sec);
113
//            	TaxonNode child = node.addChildTaxon(newTaxon, null, null);
114
//
115
//                return child;
116
//
117
//            }
118
//        }
119
//
120
//        return null;
121
//    }
122

    
123
    public boolean openInEditor() {
124
        return isOpenInEditor ;
125
    }
126

    
127
    public void setOpenInEditor(boolean isOpenInEditor) {
128
        this.isOpenInEditor = isOpenInEditor;
129
    }
130

    
131
    public boolean openEmpty(){
132
        return openInEditor() && openEmptyEditor;
133
    }
134

    
135
    public TaxonNode getTaxonNode(){
136
        if(generatedTaxonNodeUuid != null){
137
            return CdmStore.getService(ITaxonNodeService.class).load(generatedTaxonNodeUuid);
138
        }
139
        return null;
140
    }
141

    
142
    public ITaxonTreeNode getParentTreeNode(){
143
        return taxonNodePage.getParentTreeNode();
144
    }
145

    
146
    @Override
147
    protected String getEntityName() {
148
        return "Taxon";
149
    }
150

    
151
    public void addWizardPageListener(IWizardPageListener wizardPageListener){
152
        this.wizardPageListener = wizardPageListener;
153
    }
154

    
155
    public boolean isSuccess() {
156
        return success;
157
    }
158

    
159
    public void setSuccess(boolean success) {
160
        this.success = success;
161
    }
162

    
163
    @Override
164
    public void createParentEntity() {
165
        Object selectedObject = getSelection().getFirstElement();
166
        if(selectedObject instanceof TaxonNodeDto){
167
            if(selectedObject instanceof TaxonNodeDto){
168
                TaxonNodeDto taxonNodeDto = (TaxonNodeDto) selectedObject;
169
                setParentEntity(CdmStore.getService(ITaxonNodeService.class).load(taxonNodeDto.getUuid()));
170
            }
171
        }
172
    }
173
}
(21-21/28)