#5234 Add correct updated objects to change event
[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 CdmApplicationState.getCurrentDataChangeService()
74 .fireChangeEvent(new CdmChangeEvent(Action.Create, affectedObjects, NewTaxonNodeWizard.class), true);
75
76 }catch(IllegalArgumentException e){
77 MessagingUtils.warningDialog("Taxon already exists in classfication", getClass(), e.getMessage());
78 }
79 }
80 }
81
82 @Override
83 protected ITaxonTreeNode createNewEntity() {
84 if(getSelection() != null){
85 Object selectedObject = getSelection().getFirstElement();
86 if(selectedObject instanceof ITaxonTreeNode){
87 ITaxonTreeNode treeNode = (ITaxonTreeNode) selectedObject;
88
89 if(treeNode instanceof Classification){
90 return CdmStore.getService(IClassificationService.class).load(treeNode.getUuid());
91 }
92 else if(treeNode instanceof TaxonNode){
93 return CdmStore.getService(ITaxonNodeService.class).load(treeNode.getUuid());
94 }
95 }
96 }
97
98 return null;
99 }
100
101 /**
102 * <p>openInEditor</p>
103 *
104 * @return a boolean.
105 */
106 public boolean openInEditor(){
107 return taxonNodePage.openInEditor();
108 }
109
110 /**
111 * <p>openEmpty</p>
112 *
113 * @return a boolean.
114 */
115 public boolean openEmpty(){
116 return openInEditor() && openEmptyEditor;
117 }
118
119 /**
120 * <p>getTaxonNode</p>
121 *
122 * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
123 */
124 public TaxonNode getTaxonNode(){
125 if(generatedTaxonNodeUuid != null){
126 return CdmStore.getService(ITaxonNodeService.class).load(generatedTaxonNodeUuid);
127 }
128 return null;
129 }
130
131 public ITaxonTreeNode getParentTreeNode(){
132 return taxonNodePage.getParentTreeNode();
133 }
134
135 @Override
136 protected String getEntityName() {
137 return "Taxon";
138 }
139
140 public void addWizardPageListener(IWizardPageListener wizardPageListener){
141 this.wizardPageListener = wizardPageListener;
142 }
143
144 }