3b9036faf261b9b7eb15a43d959ec1c6c6b4dae7
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / newWizard / NewDerivedUnitBaseWizard.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.newWizard;
12
13 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
14 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;
15 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
16 import eu.etaxonomy.cdm.model.media.Media;
17 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
18 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
19 import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
20 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
21 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
22 import eu.etaxonomy.taxeditor.model.IDerivedUnitFacadePart;
23 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
24 import eu.etaxonomy.taxeditor.store.CdmStore;
25 import eu.etaxonomy.taxeditor.ui.section.occurrence.DerivedUnitBaseWizardPage;
26 import eu.etaxonomy.taxeditor.ui.section.occurrence.DerivedUnitGeneralWizardPage;
27 import eu.etaxonomy.taxeditor.ui.section.occurrence.FieldUnitGeneralWizardPage;
28 import eu.etaxonomy.taxeditor.ui.section.occurrence.FieldUnitWizardPage;
29 import eu.etaxonomy.taxeditor.ui.section.occurrence.GatheringEventWizardPage;
30
31 /**
32 * @author n.hoffmann
33 * @created Jun 16, 2010
34 * @version 1.0
35 */
36 public class NewDerivedUnitBaseWizard extends AbstractNewEntityWizard<SpecimenOrObservationBase> implements IDerivedUnitFacadePart{
37 private SpecimenOrObservationType specOrObsType = null;
38
39 public NewDerivedUnitBaseWizard() {
40 super();
41 }
42
43 public NewDerivedUnitBaseWizard(SpecimenOrObservationType specOrObsType) {
44 super();
45 this.specOrObsType = specOrObsType;
46 }
47 /** {@inheritDoc} */
48 @Override
49 public void addPages() {
50 DerivedUnitFacade facade = null;
51 try {
52 if(getEntity() instanceof DerivedUnit) {
53 facade = DerivedUnitFacade.NewInstance((DerivedUnit)getEntity(), PreferencesUtil.getDerivedUnitConfigurator());
54 addPage(new DerivedUnitGeneralWizardPage(formFactory, getConversationHolder(), facade));
55 addPage(new GatheringEventWizardPage(formFactory, getConversationHolder(), facade));
56 addPage(new FieldUnitWizardPage(formFactory, getConversationHolder(), facade));
57 addPage(new DerivedUnitBaseWizardPage(formFactory, getConversationHolder(), facade));
58 }
59 else if(getEntity() instanceof FieldUnit){
60 facade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.FieldUnit, (FieldUnit)getEntity(), PreferencesUtil.getDerivedUnitConfigurator());
61 addPage(new FieldUnitGeneralWizardPage(formFactory, getConversationHolder(), facade));
62 addPage(new GatheringEventWizardPage(formFactory, getConversationHolder(), facade));
63 addPage(new FieldUnitWizardPage(formFactory, getConversationHolder(), facade));
64 }
65 } catch (DerivedUnitFacadeNotSupportedException e) {
66 // we should never get here
67 throw new IllegalStateException();
68 }
69 }
70
71 /** {@inheritDoc} */
72 @Override
73 protected SpecimenOrObservationBase createNewEntity() {
74 if (specOrObsType == null) {
75 return DerivedUnit.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
76 } else {
77 if (SpecimenOrObservationType.Media.equals(specOrObsType)
78 || ((specOrObsType.getKindOf() != null) && specOrObsType.getKindOf().equals(SpecimenOrObservationType.Media))) {
79 MediaSpecimen mediaSpecimen = MediaSpecimen.NewInstance(specOrObsType);
80 //a MediaSpecimen with no media attached does not make sense. Hence, we add one
81 mediaSpecimen.setMediaSpecimen(Media.NewInstance());
82 return mediaSpecimen;
83 } else if (specOrObsType.equals(SpecimenOrObservationType.FieldUnit)) {
84 return FieldUnit.NewInstance();
85 } else {
86 return DerivedUnit.NewInstance(specOrObsType);
87 }
88 }
89 }
90
91 /** {@inheritDoc} */
92 @Override
93 protected void saveEntity() {
94 CdmStore.getService(IOccurrenceService.class).saveOrUpdate(getEntity());
95 }
96
97 @Override
98 protected String getEntityName() {
99 return "Specimen";
100 }
101
102
103 }