Merge branch 'develop' into unify_derivative_views
[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 java.util.HashMap;
14 import java.util.Map;
15 import java.util.UUID;
16
17 import org.eclipse.core.commands.Command;
18 import org.eclipse.core.commands.ExecutionException;
19 import org.eclipse.core.commands.NotEnabledException;
20 import org.eclipse.core.commands.NotHandledException;
21 import org.eclipse.core.commands.ParameterizedCommand;
22 import org.eclipse.core.commands.common.NotDefinedException;
23 import org.eclipse.jface.dialogs.IDialogConstants;
24 import org.eclipse.jface.dialogs.MessageDialogWithToggle;
25 import org.eclipse.ui.IWorkbenchWindow;
26 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.ui.commands.ICommandService;
28 import org.eclipse.ui.handlers.IHandlerService;
29
30 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
31 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;
32 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
33 import eu.etaxonomy.cdm.model.media.Media;
34 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
35 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
36 import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
37 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
38 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
39 import eu.etaxonomy.taxeditor.model.AbstractUtility;
40 import eu.etaxonomy.taxeditor.model.IDerivedUnitFacadePart;
41 import eu.etaxonomy.taxeditor.model.MessagingUtils;
42 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
43 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
44 import eu.etaxonomy.taxeditor.store.CdmStore;
45 import eu.etaxonomy.taxeditor.ui.section.occurrence.DerivedUnitBaseWizardPage;
46 import eu.etaxonomy.taxeditor.ui.section.occurrence.DerivedUnitGeneralWizardPage;
47 import eu.etaxonomy.taxeditor.ui.section.occurrence.FieldUnitGeneralWizardPage;
48 import eu.etaxonomy.taxeditor.ui.section.occurrence.FieldUnitWizardPage;
49 import eu.etaxonomy.taxeditor.ui.section.occurrence.GatheringEventWizardPage;
50
51 /**
52 * @author n.hoffmann
53 * @created Jun 16, 2010
54 * @version 1.0
55 */
56 public class NewDerivedUnitBaseWizard extends AbstractNewEntityWizard<SpecimenOrObservationBase> implements IDerivedUnitFacadePart{
57 private SpecimenOrObservationType specOrObsType = null;
58
59 public NewDerivedUnitBaseWizard() {
60 super();
61 }
62
63 public NewDerivedUnitBaseWizard(SpecimenOrObservationType specOrObsType) {
64 super();
65 this.specOrObsType = specOrObsType;
66 }
67 /** {@inheritDoc} */
68 @Override
69 public void addPages() {
70 DerivedUnitFacade facade = null;
71 try {
72 if(getEntity() instanceof DerivedUnit) {
73 facade = DerivedUnitFacade.NewInstance((DerivedUnit)getEntity(), PreferencesUtil.getDerivedUnitConfigurator());
74 addPage(new DerivedUnitGeneralWizardPage(formFactory, getConversationHolder(), facade));
75 addPage(new GatheringEventWizardPage(formFactory, getConversationHolder(), facade));
76 addPage(new FieldUnitWizardPage(formFactory, getConversationHolder(), facade));
77 addPage(new DerivedUnitBaseWizardPage(formFactory, getConversationHolder(), facade));
78 }
79 else if(getEntity() instanceof FieldUnit){
80 facade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.FieldUnit, (FieldUnit)getEntity(), PreferencesUtil.getDerivedUnitConfigurator());
81 addPage(new FieldUnitGeneralWizardPage(formFactory, getConversationHolder(), facade));
82 addPage(new GatheringEventWizardPage(formFactory, getConversationHolder(), facade));
83 addPage(new FieldUnitWizardPage(formFactory, getConversationHolder(), facade));
84 }
85 } catch (DerivedUnitFacadeNotSupportedException e) {
86 // we should never get here
87 throw new IllegalStateException();
88 }
89 }
90
91 /** {@inheritDoc} */
92 @Override
93 protected SpecimenOrObservationBase createNewEntity() {
94 if (specOrObsType == null) {
95 return DerivedUnit.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
96 } else {
97 if (SpecimenOrObservationType.Media.equals(specOrObsType)
98 || ((specOrObsType.getKindOf() != null) && specOrObsType.getKindOf().equals(SpecimenOrObservationType.Media))) {
99 MediaSpecimen mediaSpecimen = MediaSpecimen.NewInstance(specOrObsType);
100 //a MediaSpecimen with no media attached does not make sense. Hence, we add one
101 mediaSpecimen.setMediaSpecimen(Media.NewInstance());
102 return mediaSpecimen;
103 } else if (specOrObsType.equals(SpecimenOrObservationType.FieldUnit)) {
104 return FieldUnit.NewInstance();
105 } else {
106 return DerivedUnit.NewInstance(specOrObsType);
107 }
108 }
109 }
110
111
112 /** {@inheritDoc} */
113 @Override
114 protected void saveEntity() {
115 if(CdmStore.getCurrentSessionManager().isRemoting()) {
116 CdmStore.getService(IOccurrenceService.class).merge(getEntity(), true);
117 } else {
118 CdmStore.getService(IOccurrenceService.class).save(getEntity());
119 }
120 }
121
122
123 @Override
124 protected String getEntityName() {
125 return "Specimen";
126 }
127
128 /**
129 * {@inheritDoc}
130 */
131 @Override
132 public boolean performFinish() {
133 boolean performFinish = super.performFinish();
134 int returnCode = IDialogConstants.NO_ID;
135 if (!PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.PROMPT_FOR_OPEN_SPECIMEN_IN_EDITOR)){
136 MessageDialogWithToggle messageDialog = MessageDialogWithToggle.openYesNoQuestion(getShell(),
137 "Choose opening option", "Do you want to open the specimen in the specimen editor?",
138 "Remember my decision?", false, PreferencesUtil.getPreferenceStore(),
139 IPreferenceKeys.ALWAYS_OPEN_SPECIMEN_IN_EDITOR);
140 returnCode = messageDialog.getReturnCode();
141 PreferencesUtil.getPreferenceStore().setValue(IPreferenceKeys.PROMPT_FOR_OPEN_SPECIMEN_IN_EDITOR, messageDialog.getToggleState());
142 if (returnCode != IDialogConstants.YES_ID) {
143 return performFinish;
144 }
145 }
146 if (returnCode==IDialogConstants.YES_ID ||
147 PreferencesUtil.getPreferenceStore().getString(IPreferenceKeys.ALWAYS_OPEN_SPECIMEN_IN_EDITOR)
148 .equals(MessageDialogWithToggle.ALWAYS)) {
149 IHandlerService handlerService = (IHandlerService) AbstractUtility.getService(IHandlerService.class);
150
151 // get the command from plugin.xml
152 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
153 ICommandService cmdService = (ICommandService) window.getService(ICommandService.class);
154 Command command = cmdService.getCommand("eu.etaxonomy.taxeditor.editor.openSpecimenEditor");
155 Map<String, UUID> parameters = new HashMap<String, UUID>();
156 parameters.put("eu.etaxonomy.taxeditor.specimenUuidParameter", getEntity().getUuid());
157 ParameterizedCommand parameterizedCommand = ParameterizedCommand.generateCommand(command, parameters);
158
159
160 // execute the command
161 try {
162 handlerService.executeCommand(parameterizedCommand, null);
163 } catch (ExecutionException e) {
164 MessagingUtils.error(NewDerivedUnitBaseWizard.class, e);
165 } catch (NotDefinedException e) {
166 MessagingUtils.error(NewDerivedUnitBaseWizard.class, e);
167 } catch (NotEnabledException e) {
168 MessagingUtils.error(NewDerivedUnitBaseWizard.class, e);
169 } catch (NotHandledException e) {
170 MessagingUtils.error(NewDerivedUnitBaseWizard.class, e);
171 }
172 }
173 return performFinish;
174 }
175
176
177 }