553438baeb2c118f7dbff14010d7161bfadd71ae
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / newWizard / NewTaxonNodeWizard.java
1 /**
2 *
3 */
4 package eu.etaxonomy.taxeditor.newWizard;
5
6 import java.util.UUID;
7
8 import javax.inject.Inject;
9
10 import org.apache.commons.lang.StringUtils;
11
12 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
13 import eu.etaxonomy.cdm.api.application.CdmChangeEvent;
14 import eu.etaxonomy.cdm.api.application.CdmChangeEvent.Action;
15 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
16 import eu.etaxonomy.cdm.api.service.UpdateResult;
17 import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
18 import eu.etaxonomy.cdm.model.reference.Reference;
19 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
20 import eu.etaxonomy.cdm.model.taxon.Taxon;
21 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
22 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
23 import eu.etaxonomy.taxeditor.l10n.Messages;
24 import eu.etaxonomy.taxeditor.model.MessagingUtils;
25 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
26 import eu.etaxonomy.taxeditor.store.CdmStore;
27 import eu.etaxonomy.taxeditor.ui.section.classification.TaxonNodeWizardPage;
28
29 /**
30 * <p>NewTaxonNodeWizard class.</p>
31 *
32 * @author n.hoffmann
33 * @created Sep 15, 2009
34 * @version 1.0
35 */
36 public class NewTaxonNodeWizard extends AbstractNewEntityWizard<TaxonNode>{
37
38 private TaxonNodeWizardPage taxonNodePage;
39 private boolean openEmptyEditor;
40 private UUID generatedTaxonNodeUuid;
41 private IWizardPageListener wizardPageListener;
42 private boolean isOpenInEditor = false;
43
44 /**
45 * <p>Constructor for AbstractNewEntityWizard.</p>
46 *
47 * @param <T> a T object.
48 */
49 @Inject
50 public NewTaxonNodeWizard(){
51 setWindowTitle(Messages.TaxonNodeWizardPage_new);
52 }
53
54
55 @Override
56 public void addPages() {
57 taxonNodePage = new TaxonNodeWizardPage(formFactory, getConversationHolder(), getEntity(), wizardPageListener, true);
58 addPage(taxonNodePage);
59
60 }
61
62 @Override
63 protected void saveEntity() {
64 Taxon taxon = taxonNodePage.createTaxon();
65 if(taxon == null || StringUtils.isEmpty(taxon.getName().getFullTitleCache())){
66 openEmptyEditor = true;
67 }else{
68 getConversationHolder().bind();
69 // ITaxonTreeNode parent = getParentTreeNode();
70 try{
71 UUID parentNodeUuid;
72 // if(parent instanceof Classification){
73 // parentNodeUuid = ((Classification) parent).getRootNode().getUuid();
74 // }
75 // else{
76 // parentNodeUuid = parent.getUuid();
77 // }
78 UpdateResult result;
79 result = CdmStore.getService(ITaxonNodeService.class).saveNewTaxonNode(getEntity());
80 if (result.isOk()){
81 this.setEntity((TaxonNode)result.getCdmEntity());
82 CdmApplicationState.getCurrentDataChangeService()
83 .fireChangeEvent(new CdmChangeEvent(Action.Create, result.getUpdatedObjects(), NewTaxonNodeWizard.class), true);
84
85 }else{
86 if (!result.isOk()){
87 if (!result.getExceptions().isEmpty()){
88 MessagingUtils.error(getClass(), "Taxon could not be created", result.getExceptions().iterator().next());
89 }else{
90 MessagingUtils.warn(getClass(), "Taxon could not be created");
91 }
92 }
93 }
94 }catch(IllegalArgumentException e){
95 MessagingUtils.warningDialog("Taxon already exists in classfication", getClass(), e.getMessage());
96 }
97 }
98 }
99
100 @Override
101 protected TaxonNode createNewEntity() {
102 if(getSelection() != null){
103 Object selectedObject = getSelection().getFirstElement();
104 if(selectedObject instanceof TaxonNodeDto){
105 TaxonNodeDto taxonNodeDto = (TaxonNodeDto) selectedObject;
106 TaxonNode node = CdmStore.getService(ITaxonNodeService.class).load(taxonNodeDto.getUuid());
107 Reference sec = node.getTaxon() != null? node.getTaxon().getSec():null;
108 Taxon newTaxon = Taxon.NewInstance(TaxonNameFactory.NewNameInstance(PreferencesUtil.getPreferredNomenclaturalCode(), null), sec);
109 TaxonNode child = node.addChildTaxon(newTaxon, null, null);
110
111 return child;
112
113 }
114 }
115
116 return null;
117 }
118
119
120 /**
121 * <p>
122 * openInEditor
123 * </p>
124 *
125 * @return a boolean.
126 */
127 public boolean openInEditor() {
128 return isOpenInEditor ;
129 }
130
131 public void setOpenInEditor(boolean isOpenInEditor) {
132 this.isOpenInEditor = isOpenInEditor;
133 }
134
135 /**
136 * <p>openEmpty</p>
137 *
138 * @return a boolean.
139 */
140 public boolean openEmpty(){
141 return openInEditor() && openEmptyEditor;
142 }
143
144 /**
145 * <p>getTaxonNode</p>
146 *
147 * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
148 */
149 public TaxonNode getTaxonNode(){
150 if(generatedTaxonNodeUuid != null){
151 return CdmStore.getService(ITaxonNodeService.class).load(generatedTaxonNodeUuid);
152 }
153 return null;
154 }
155
156 public ITaxonTreeNode getParentTreeNode(){
157 return taxonNodePage.getParentTreeNode();
158 }
159
160 @Override
161 protected String getEntityName() {
162 return "Taxon";
163 }
164
165 public void addWizardPageListener(IWizardPageListener wizardPageListener){
166 this.wizardPageListener = wizardPageListener;
167 }
168
169
170
171 }