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