fix #7794: add finish and open button to taxonnode wizard
[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.HashSet;
7 import java.util.Set;
8 import java.util.UUID;
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.IClassificationService;
16 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
17 import eu.etaxonomy.cdm.api.service.ITaxonService;
18 import eu.etaxonomy.cdm.api.service.UpdateResult;
19 import eu.etaxonomy.cdm.model.common.CdmBase;
20 import eu.etaxonomy.cdm.model.name.TaxonName;
21 import eu.etaxonomy.cdm.model.reference.Reference;
22 import eu.etaxonomy.cdm.model.taxon.Classification;
23 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
24 import eu.etaxonomy.cdm.model.taxon.Taxon;
25 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
26 import eu.etaxonomy.cdm.persistence.dto.MergeResult;
27 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
28 import eu.etaxonomy.taxeditor.model.MessagingUtils;
29 import eu.etaxonomy.taxeditor.store.CdmStore;
30 import eu.etaxonomy.taxeditor.ui.section.classification.TaxonNodeDetailElement;
31 import eu.etaxonomy.taxeditor.ui.section.classification.TaxonNodeWizardPage;
32
33 /**
34 * <p>NewTaxonNodeWizard class.</p>
35 *
36 * @author n.hoffmann
37 * @created Sep 15, 2009
38 * @version 1.0
39 */
40 public class NewTaxonNodeWizard extends AbstractNewEntityWizard<ITaxonTreeNode>{
41
42 private TaxonNodeWizardPage taxonNodePage;
43 private boolean openEmptyEditor;
44 private UUID generatedTaxonNodeUuid;
45 private IWizardPageListener wizardPageListener;
46 private boolean isOpenInEditor = false;
47
48 @Override
49 public void addPages() {
50 taxonNodePage = new TaxonNodeWizardPage(formFactory, getConversationHolder(), getEntity(), wizardPageListener, true);
51 addPage(taxonNodePage);
52
53 }
54
55 @Override
56 protected void saveEntity() {
57 Taxon taxon = taxonNodePage.createTaxon();
58 if(taxon == null || StringUtils.isEmpty(taxon.getName().getFullTitleCache())){
59 openEmptyEditor = true;
60 }else{
61 getConversationHolder().bind();
62 ITaxonTreeNode parent = getParentTreeNode();
63 Reference sec = taxon.getSec();
64 taxon.setSec(null);
65 try{
66 UUID parentNodeUuid;
67 if(parent instanceof Classification){
68 parentNodeUuid = ((Classification) parent).getRootNode().getUuid();
69 }
70 else{
71 parentNodeUuid = parent.getUuid();
72 }
73
74 /*
75 * for (HybridRelationship rel : childRelations){
76 TaxonName name = rel.getHybridName();
77 TaxonName parentName = rel.getParentName();
78 nameService.saveOrUpdate(rel.getHybridName());
79 nameService.save(rel.getParentName());
80 }
81 */
82 UpdateResult result;
83 if (!taxon.getName().getHybridChildRelations().isEmpty() || !taxon.getName().getHybridParentRelations().isEmpty()){
84 Set<TaxonName> parentNames = new HashSet<>();
85 Set<TaxonName> childNames = new HashSet<>();
86 // for (HybridRelationship hybridRelationship: taxon.getName().getHybridChildRelations()){
87 // if (hybridRelationship.getParentName() != taxon.getName()){
88 // if (!hybridRelationship.getParentName().isPersited()){
89 // parentNames.add(hybridRelationship.getParentName());
90 // }
91 // }else {//if (hybridRelationship.getHybridName() != taxon.getName()){
92 // if (!hybridRelationship.getHybridName().isPersited()){
93 // childNames.add(hybridRelationship.getHybridName());
94 // }
95 // }
96 // for (HybridRelationship rel : taxon.getName().getHybridChildRelations()){
97 // TaxonName name = rel.getHybridName();
98 // TaxonName parentName = rel.getParentName();
99 // CdmStore.getService(INameService.class).save(rel.getHybridName());
100 // CdmStore.getService(INameService.class).save(rel.getParentName());
101 // }
102
103 // }
104
105 // for (HybridRelationship hybridRelationship: taxon.getName().getHybridParentRelations()){
106 // if (hybridRelationship.getParentName() != taxon.getName()){
107 // if (!hybridRelationship.getParentName().isPersited()){
108 // parentNames.add(hybridRelationship.getParentName());
109 // }
110 // }else {//if (hybridRelationship.getHybridName() != taxon.getName()){
111 // if (!hybridRelationship.getHybridName().isPersited()){
112 // childNames.add(hybridRelationship.getHybridName());
113 // }
114 // }
115 //
116 // }
117 // CdmStore.getService(INameService.class).save(childNames);
118 // CdmStore.getService(INameService.class).save(parentNames);
119
120 }
121
122 if (taxon.getId() == 0){
123 result = CdmStore.getService(ITaxonNodeService.class).createNewTaxonNode(parentNodeUuid, taxon, parent.getReference(), parent.getMicroReference());
124 }else{
125 result = CdmStore.getService(ITaxonNodeService.class).createNewTaxonNode(parentNodeUuid, taxon.getUuid(), parent.getReference(), parent.getMicroReference());
126 }
127 if (result.isOk()){
128 generatedTaxonNodeUuid = result.getCdmEntity().getUuid();
129 TaxonNode taxonNode = CdmStore.getService(ITaxonNodeService.class).load(generatedTaxonNodeUuid);
130 taxonNode.getTaxon().setSec(sec);
131 taxonNode.setExcluded(((TaxonNodeDetailElement)taxonNodePage.getDetailElement()).isExcluded());
132 taxonNode.setUnplaced(((TaxonNodeDetailElement)taxonNodePage.getDetailElement()).isUnplaced());
133
134
135 MergeResult resultMerge = CdmStore.getService(ITaxonNodeService.class).merge(taxonNode, true);
136 Set<CdmBase> affectedObjects = new HashSet<CdmBase>();
137 if (result.getUpdatedObjects().iterator().hasNext()){
138 TaxonNode parentNode = (TaxonNode)result.getUpdatedObjects().iterator().next();
139 if(parentNode.getParent() == null) {
140 affectedObjects.add(taxonNode.getClassification());
141 }
142 affectedObjects.add(parentNode);
143 }
144 if (resultMerge.getMergedEntity() instanceof TaxonNode){
145 TaxonNode node = (TaxonNode)resultMerge.getMergedEntity();
146 affectedObjects.remove(node);
147
148 affectedObjects.add(node);
149 }
150 CdmApplicationState.getCurrentDataChangeService()
151 .fireChangeEvent(new CdmChangeEvent(Action.Create, affectedObjects, NewTaxonNodeWizard.class), true);
152
153 }else{
154 if (!result.isOk()){
155 if (!result.getExceptions().isEmpty()){
156 MessagingUtils.error(getClass(), "Taxon could not be created", result.getExceptions().iterator().next());
157 }else{
158 MessagingUtils.warn(getClass(), "Taxon could not be created");
159 }
160 }
161 }
162 }catch(IllegalArgumentException e){
163 MessagingUtils.warningDialog("Taxon already exists in classfication", getClass(), e.getMessage());
164 }
165 }
166 }
167
168 @Override
169 protected ITaxonTreeNode createNewEntity() {
170 if(getSelection() != null){
171 Object selectedObject = getSelection().getFirstElement();
172 if(selectedObject instanceof TaxonNodeDto){
173 TaxonNodeDto taxonNodeDto = (TaxonNodeDto) selectedObject;
174 TaxonNode node = CdmStore.getService(ITaxonNodeService.class).load(taxonNodeDto.getUuid());
175 if(node.getTaxon()!=null){
176 //FIXME this taxon is not used. Do we need this call?
177 Taxon taxon = (Taxon) CdmStore.getService(ITaxonService.class).load(node.getTaxon().getUuid());
178 }
179 else if(node.getClassification()!=null){
180 return CdmStore.getService(IClassificationService.class).load(node.getClassification().getUuid());
181 }
182 return node;
183
184 } else if(selectedObject instanceof Classification){
185 return (Classification)selectedObject;
186 }
187 }
188
189 return null;
190 }
191
192
193 /**
194 * <p>
195 * openInEditor
196 * </p>
197 *
198 * @return a boolean.
199 */
200 public boolean openInEditor() {
201 return isOpenInEditor ;
202 }
203
204 public void setOpenInEditor(boolean isOpenInEditor) {
205 this.isOpenInEditor = isOpenInEditor;
206 }
207
208 /**
209 * <p>openEmpty</p>
210 *
211 * @return a boolean.
212 */
213 public boolean openEmpty(){
214 return openInEditor() && openEmptyEditor;
215 }
216
217 /**
218 * <p>getTaxonNode</p>
219 *
220 * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
221 */
222 public TaxonNode getTaxonNode(){
223 if(generatedTaxonNodeUuid != null){
224 return CdmStore.getService(ITaxonNodeService.class).load(generatedTaxonNodeUuid);
225 }
226 return null;
227 }
228
229 public ITaxonTreeNode getParentTreeNode(){
230 return taxonNodePage.getParentTreeNode();
231 }
232
233 @Override
234 protected String getEntityName() {
235 return "Taxon";
236 }
237
238 public void addWizardPageListener(IWizardPageListener wizardPageListener){
239 this.wizardPageListener = wizardPageListener;
240 }
241
242
243
244 }