4a300dc7afc066d296d488e2980b541eb75b3131
[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 taxonNode = CdmStore.getService(ITaxonNodeService.class).merge(taxonNode);
57 generatedTaxonNodeUuid = taxonNode.getUuid();
58 Set<CdmBase> affectedObjects = new HashSet<CdmBase>();
59 //FIXME:Remoting Hack for now - need to generalise this
60 // by making the affected objects a set of ICdmBase
61 // Also, this needs to be moved to the new operations
62 // architecture.
63
64 TaxonNode parentNode = taxonNode.getParent();
65 if(parentNode.getParent() == null) {
66 affectedObjects.add(taxonNode.getClassification());
67 }
68
69 if(parentNode instanceof TaxonNode) {
70 affectedObjects.add(parentNode);
71 }
72
73 if(CdmStore.getCurrentSessionManager().isRemoting()) {
74 CdmApplicationState.getCurrentDataChangeService()
75 .fireChangeEvent(new CdmChangeEvent(Action.Create, affectedObjects, NewTaxonNodeWizard.class), true);
76 }
77 }catch(IllegalArgumentException e){
78 MessagingUtils.warningDialog("Taxon already exists in classfication", getClass(), e.getMessage());
79 }
80 }
81 }
82
83 @Override
84 protected ITaxonTreeNode createNewEntity() {
85 if(getSelection() != null){
86 Object selectedObject = getSelection().getFirstElement();
87 if(selectedObject instanceof ITaxonTreeNode){
88 ITaxonTreeNode treeNode = (ITaxonTreeNode) selectedObject;
89
90 if(treeNode instanceof Classification){
91 return CdmStore.getService(IClassificationService.class).load(treeNode.getUuid());
92 }
93 else if(treeNode instanceof TaxonNode){
94 return CdmStore.getService(ITaxonNodeService.class).load(treeNode.getUuid());
95 }
96 }
97 }
98
99 return null;
100 }
101
102 /**
103 * <p>openInEditor</p>
104 *
105 * @return a boolean.
106 */
107 public boolean openInEditor(){
108 return taxonNodePage.openInEditor();
109 }
110
111 /**
112 * <p>openEmpty</p>
113 *
114 * @return a boolean.
115 */
116 public boolean openEmpty(){
117 return openInEditor() && openEmptyEditor;
118 }
119
120 /**
121 * <p>getTaxonNode</p>
122 *
123 * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
124 */
125 public TaxonNode getTaxonNode(){
126 if(generatedTaxonNodeUuid != null){
127 return CdmStore.getService(ITaxonNodeService.class).load(generatedTaxonNodeUuid);
128 }
129 return null;
130 }
131
132 public ITaxonTreeNode getParentTreeNode(){
133 return taxonNodePage.getParentTreeNode();
134 }
135
136 @Override
137 protected String getEntityName() {
138 return "Taxon";
139 }
140
141 public void addWizardPageListener(IWizardPageListener wizardPageListener){
142 this.wizardPageListener = wizardPageListener;
143 }
144
145 }