b1da433c4d95823569267382ba4cace84a2bde7b
[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.UpdateResult;
18 import eu.etaxonomy.cdm.model.common.CdmBase;
19 import eu.etaxonomy.cdm.model.reference.Reference;
20 import eu.etaxonomy.cdm.model.taxon.Classification;
21 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
22 import eu.etaxonomy.cdm.model.taxon.Taxon;
23 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
24 import eu.etaxonomy.taxeditor.model.MessagingUtils;
25 import eu.etaxonomy.taxeditor.store.CdmStore;
26 import eu.etaxonomy.taxeditor.ui.section.classification.TaxonNodeWizardPage;
27
28 /**
29 * <p>NewTaxonNodeWizard class.</p>
30 *
31 * @author n.hoffmann
32 * @created Sep 15, 2009
33 * @version 1.0
34 */
35 public class NewTaxonNodeWizard extends AbstractNewEntityWizard<ITaxonTreeNode>{
36
37 private TaxonNodeWizardPage taxonNodePage;
38 private boolean openEmptyEditor;
39 private UUID generatedTaxonNodeUuid;
40 private IWizardPageListener wizardPageListener;
41
42 @Override
43 public void addPages() {
44 taxonNodePage = new TaxonNodeWizardPage(formFactory, getConversationHolder(), getEntity(), wizardPageListener);
45
46 addPage(taxonNodePage);
47 }
48
49 @Override
50 protected void saveEntity() {
51 if(taxonNodePage.getTaxon() == null || StringUtils.isEmpty(taxonNodePage.getTaxon().getName().getFullTitleCache())){
52 openEmptyEditor = true;
53 }else{
54 getConversationHolder().bind();
55 ITaxonTreeNode parent = getParentTreeNode();
56 Taxon taxon = taxonNodePage.getTaxon();
57 Reference sec = taxon.getSec();
58 taxon.setSec(null);
59 try{
60 UUID parentNodeUuid;
61 if(parent instanceof Classification){
62 parentNodeUuid = ((Classification) parent).getRootNode().getUuid();
63 }
64 else{
65 parentNodeUuid = parent.getUuid();
66 }
67 UpdateResult result = CdmStore.getService(ITaxonNodeService.class).createNewTaxonNode(parentNodeUuid, taxon, parent.getReference(), parent.getMicroReference());
68
69 //TaxonNode taxonNode = parent.addChildTaxon(taxon, parent.getReference(), parent.getMicroReference());
70
71 /* if(CdmStore.getCurrentSessionManager().isRemoting()) {
72 taxonNode = CdmStore.getService(ITaxonNodeService.class).merge(taxonNode);
73 } else {
74 taxonNode = CdmStore.getService(ITaxonNodeService.class).save(taxonNode);
75 }*/
76 if (result.isOk()){
77 generatedTaxonNodeUuid = result.getCdmEntity().getUuid();
78 TaxonNode taxonNode = CdmStore.getService(ITaxonNodeService.class).load(generatedTaxonNodeUuid);
79 taxonNode.getTaxon().setSec(sec);
80 CdmStore.getService(ITaxonNodeService.class).merge(taxonNode, true);
81 Set<CdmBase> affectedObjects = new HashSet<CdmBase>();
82 if (result.getUpdatedObjects().iterator().hasNext()){
83 TaxonNode parentNode = (TaxonNode)result.getUpdatedObjects().iterator().next();
84 if(parentNode.getParent() == null) {
85 affectedObjects.add(taxonNode.getClassification());
86 }
87 affectedObjects.add(parentNode);
88 }
89 if(CdmStore.getCurrentSessionManager().isRemoting()) {
90 CdmApplicationState.getCurrentDataChangeService()
91 .fireChangeEvent(new CdmChangeEvent(Action.Create, affectedObjects, NewTaxonNodeWizard.class), true);
92 }
93 }else{
94 if (!result.isOk()){
95 if (!result.getExceptions().isEmpty()){
96 MessagingUtils.error(getClass(), "Taxon could not be created", result.getExceptions().iterator().next());
97 }else{
98 MessagingUtils.warn(getClass(), "Taxon could not be created");
99 }
100 }
101 }
102 }catch(IllegalArgumentException e){
103 MessagingUtils.warningDialog("Taxon already exists in classfication", getClass(), e.getMessage());
104 }
105 }
106 }
107
108 @Override
109 protected ITaxonTreeNode createNewEntity() {
110 if(getSelection() != null){
111 Object selectedObject = getSelection().getFirstElement();
112 if(selectedObject instanceof ITaxonTreeNode){
113 ITaxonTreeNode treeNode = (ITaxonTreeNode) selectedObject;
114
115 if(treeNode instanceof Classification){
116 return CdmStore.getService(IClassificationService.class).load(treeNode.getUuid());
117 }
118 else if(treeNode instanceof TaxonNode){
119 return CdmStore.getService(ITaxonNodeService.class).load(treeNode.getUuid());
120 }
121 }
122 }
123
124 return null;
125 }
126
127 /**
128 * <p>openInEditor</p>
129 *
130 * @return a boolean.
131 */
132 public boolean openInEditor(){
133 return taxonNodePage.openInEditor();
134 }
135
136 /**
137 * <p>openEmpty</p>
138 *
139 * @return a boolean.
140 */
141 public boolean openEmpty(){
142 return openInEditor() && openEmptyEditor;
143 }
144
145 /**
146 * <p>getTaxonNode</p>
147 *
148 * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
149 */
150 public TaxonNode getTaxonNode(){
151 if(generatedTaxonNodeUuid != null){
152 return CdmStore.getService(ITaxonNodeService.class).load(generatedTaxonNodeUuid);
153 }
154 return null;
155 }
156
157 public ITaxonTreeNode getParentTreeNode(){
158 return taxonNodePage.getParentTreeNode();
159 }
160
161 @Override
162 protected String getEntityName() {
163 return "Taxon";
164 }
165
166 public void addWizardPageListener(IWizardPageListener wizardPageListener){
167 this.wizardPageListener = wizardPageListener;
168 }
169
170 }