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