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