4 package eu
.etaxonomy
.taxeditor
.newWizard
;
6 import java
.util
.HashSet
;
10 import org
.apache
.commons
.lang
.StringUtils
;
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
;
36 * <p>NewTaxonNodeWizard class.</p>
39 * @created Sep 15, 2009
42 public class NewTaxonNodeWizard
extends AbstractNewEntityWizard
<ITaxonTreeNode
>{
44 private TaxonNodeWizardPage taxonNodePage
;
45 private boolean openEmptyEditor
;
46 private UUID generatedTaxonNodeUuid
;
47 private IWizardPageListener wizardPageListener
;
50 public void addPages() {
51 taxonNodePage
= new TaxonNodeWizardPage(formFactory
, getConversationHolder(), getEntity(), wizardPageListener
, true);
53 addPage(taxonNodePage
);
57 protected void saveEntity() {
58 Taxon taxon
= taxonNodePage
.createTaxon();
59 if(taxon
== null || StringUtils
.isEmpty(taxon
.getName().getFullTitleCache())){
60 openEmptyEditor
= true;
62 getConversationHolder().bind();
63 ITaxonTreeNode parent
= getParentTreeNode();
64 Reference sec
= taxon
.getSec();
68 if(parent
instanceof Classification
){
69 parentNodeUuid
= ((Classification
) parent
).getRootNode().getUuid();
72 parentNodeUuid
= parent
.getUuid();
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());
82 }else if (hybridRelationship
.getHybridName() != taxon
.getName()){
83 if (!hybridRelationship
.getHybridName().isPersited()){
84 namesToSave
.add(hybridRelationship
.getHybridName());
90 for (HybridRelationship hybridRelationship
: taxon
.getName().getHybridParentRelations()){
91 if (hybridRelationship
.getParentName() != taxon
.getName()){
92 if (!hybridRelationship
.getParentName().isPersited()){
93 namesToSave
.add(hybridRelationship
.getParentName());
95 }else if (hybridRelationship
.getHybridName() != taxon
.getName()){
96 if (!hybridRelationship
.getHybridName().isPersited()){
97 namesToSave
.add(hybridRelationship
.getHybridName());
102 CdmStore
.getService(INameService
.class).save(namesToSave
);
105 if (taxon
.getId() == 0){
106 result
= CdmStore
.getService(ITaxonNodeService
.class).createNewTaxonNode(parentNodeUuid
, taxon
, parent
.getReference(), parent
.getMicroReference());
108 result
= CdmStore
.getService(ITaxonNodeService
.class).createNewTaxonNode(parentNodeUuid
, taxon
.getUuid(), parent
.getReference(), parent
.getMicroReference());
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());
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());
125 affectedObjects
.add(parentNode
);
127 if (resultMerge
.getMergedEntity() instanceof TaxonNode
){
128 TaxonNode node
= (TaxonNode
)resultMerge
.getMergedEntity();
129 affectedObjects
.remove(node
);
131 affectedObjects
.add(node
);
133 CdmApplicationState
.getCurrentDataChangeService()
134 .fireChangeEvent(new CdmChangeEvent(Action
.Create
, affectedObjects
, NewTaxonNodeWizard
.class), true);
138 if (!result
.getExceptions().isEmpty()){
139 MessagingUtils
.error(getClass(), "Taxon could not be created", result
.getExceptions().iterator().next());
141 MessagingUtils
.warn(getClass(), "Taxon could not be created");
145 }catch(IllegalArgumentException e
){
146 MessagingUtils
.warningDialog("Taxon already exists in classfication", getClass(), e
.getMessage());
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());
162 else if(node
.getClassification()!=null){
163 return CdmStore
.getService(IClassificationService
.class).load(node
.getClassification().getUuid());
175 * <p>openInEditor</p>
179 public boolean openInEditor(){
180 return taxonNodePage
.openInEditor();
188 public boolean openEmpty(){
189 return openInEditor() && openEmptyEditor
;
193 * <p>getTaxonNode</p>
195 * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
197 public TaxonNode
getTaxonNode(){
198 if(generatedTaxonNodeUuid
!= null){
199 return CdmStore
.getService(ITaxonNodeService
.class).load(generatedTaxonNodeUuid
);
204 public ITaxonTreeNode
getParentTreeNode(){
205 return taxonNodePage
.getParentTreeNode();
209 protected String
getEntityName() {
213 public void addWizardPageListener(IWizardPageListener wizardPageListener
){
214 this.wizardPageListener
= wizardPageListener
;