Project

General

Profile

Download (9.56 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9

    
10
package eu.etaxonomy.taxeditor.ui.selection;
11

    
12
import java.util.Collections;
13
import java.util.List;
14
import java.util.Map;
15

    
16
import org.eclipse.core.runtime.IStatus;
17
import org.eclipse.jface.wizard.Wizard;
18
import org.eclipse.jface.wizard.WizardDialog;
19
import org.eclipse.swt.events.SelectionEvent;
20
import org.eclipse.swt.events.SelectionListener;
21

    
22
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
23
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;
24
import eu.etaxonomy.cdm.api.service.IService;
25
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
26
import eu.etaxonomy.cdm.model.agent.Institution;
27
import eu.etaxonomy.cdm.model.agent.Person;
28
import eu.etaxonomy.cdm.model.agent.Team;
29
import eu.etaxonomy.cdm.model.common.CdmBase;
30
import eu.etaxonomy.cdm.model.common.User;
31
import eu.etaxonomy.cdm.model.description.PolytomousKey;
32
import eu.etaxonomy.cdm.model.molecular.Amplification;
33
import eu.etaxonomy.cdm.model.molecular.Primer;
34
import eu.etaxonomy.cdm.model.name.TaxonName;
35
import eu.etaxonomy.cdm.model.occurrence.Collection;
36
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
37
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
38
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
39
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
40
import eu.etaxonomy.cdm.model.reference.Reference;
41
import eu.etaxonomy.taxeditor.model.MessagingUtils;
42
import eu.etaxonomy.taxeditor.model.TextHelper;
43
import eu.etaxonomy.taxeditor.newWizard.AmplificationGeneralWizardPage;
44
import eu.etaxonomy.taxeditor.newWizard.PolytomousKeyWizardPage;
45
import eu.etaxonomy.taxeditor.newWizard.PrimerWizardPage;
46
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
47
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
48
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
49
import eu.etaxonomy.taxeditor.store.CdmStore;
50
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
51
import eu.etaxonomy.taxeditor.ui.section.agent.InstitutionWizardPage;
52
import eu.etaxonomy.taxeditor.ui.section.agent.PersonWizardPage;
53
import eu.etaxonomy.taxeditor.ui.section.agent.TeamWizardPage;
54
import eu.etaxonomy.taxeditor.ui.section.name.NonViralNameWizardPage;
55
import eu.etaxonomy.taxeditor.ui.section.occurrence.CollectionWizardPage;
56
import eu.etaxonomy.taxeditor.ui.section.occurrence.DerivedUnitBaseWizardPage;
57
import eu.etaxonomy.taxeditor.ui.section.occurrence.DerivedUnitGeneralWizardPage;
58
import eu.etaxonomy.taxeditor.ui.section.occurrence.FieldUnitWizardPage;
59
import eu.etaxonomy.taxeditor.ui.section.occurrence.GatheringEventWizardPage;
60
import eu.etaxonomy.taxeditor.ui.section.reference.ReferenceWizardPage;
61
import eu.etaxonomy.taxeditor.ui.section.user.UserDetailWizardPage;
62

    
63
/**
64
 * @author n.hoffmann
65
 * @created Jun 1, 2010
66
 * @version 1.0
67
 */
68
public class EditFromSelectionWizard extends Wizard implements
69
		SelectionListener, ICdmEntitySessionEnabled {
70

    
71
    private ICdmEntitySession cdmEntitySession;
72
    private ICdmEntitySession previousCdmEntitySession;
73
	private final EntitySelectionElement selectionElement;
74
	private CdmBase rootElement;
75

    
76
	public EditFromSelectionWizard(EntitySelectionElement selectionElement) {
77
        if (CdmStore.isActive()) {
78
            previousCdmEntitySession = CdmStore.getCurrentSessionManager().getActiveSession();
79
            cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
80
            cdmEntitySession.bind();
81
        }
82
		this.selectionElement = selectionElement;
83
		this.setWindowTitle(String.format("Edit %s", TextHelper.deproxyClassName(selectionElement.getEntity().getClass())));
84
	}
85

    
86
	/** {@inheritDoc} */
87
	@Override
88
	public void addPages() {
89

    
90
		CdmFormFactory formFactory = selectionElement.getFormFactory();
91
		CdmBase entity = HibernateProxyHelper
92
				.deproxy(selectionElement.getEntity());
93
		//check if entity has already been persisted
94
		if(entity.getId()==0){
95
			rootElement = entity;
96
		}
97
		else{
98
			IService<CdmBase> service = CdmStore.getService(entity);
99
			rootElement = service.load(entity.getUuid());
100
		}
101
		if (rootElement.isInstanceOf(Reference.class)) {
102
			addPage(new ReferenceWizardPage(formFactory,
103
					selectionElement.getConversationHolder(),
104
					(HibernateProxyHelper.deproxy(rootElement, Reference.class))));
105
		} else if (rootElement.isInstanceOf(Team.class) && selectionElement instanceof EntitySelectionElementWithAbbreviatedTitle<?>) {
106
			addPage(new TeamWizardPage(formFactory,
107
					selectionElement.getConversationHolder(), HibernateProxyHelper.deproxy(rootElement, Team.class), true));
108
		} else if (rootElement.isInstanceOf(Team.class) && !(selectionElement instanceof EntitySelectionElementWithAbbreviatedTitle<?>)) {
109
            addPage(new TeamWizardPage(formFactory,
110
                    selectionElement.getConversationHolder(), HibernateProxyHelper.deproxy(rootElement, Team.class), false));
111
        } else if (rootElement.isInstanceOf(Person.class)) {
112
			addPage(new PersonWizardPage(formFactory,
113
					selectionElement.getConversationHolder(), HibernateProxyHelper.deproxy(rootElement, Person.class)));
114
		} else if (rootElement.isInstanceOf(TaxonName.class)) {
115
			addPage(new NonViralNameWizardPage(formFactory,
116
					selectionElement.getConversationHolder(),
117
					HibernateProxyHelper.deproxy(rootElement, TaxonName.class)));
118
		} else if (rootElement.isInstanceOf(SpecimenOrObservationBase.class)) {
119

    
120
			DerivedUnitFacade facade;
121
			try {
122
			    if(rootElement.isInstanceOf(DerivedUnit.class)){
123
			        facade = DerivedUnitFacade.NewInstance(
124
			                HibernateProxyHelper.deproxy(rootElement, DerivedUnit.class),
125
			                PreferencesUtil.getDerivedUnitConfigurator());
126
			    }
127
			    else {
128
                    facade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.FieldUnit,
129
                            HibernateProxyHelper.deproxy(rootElement, FieldUnit.class),
130
                            PreferencesUtil.getDerivedUnitConfigurator());
131
			    }
132

    
133
			} catch (DerivedUnitFacadeNotSupportedException e) {
134
				// we should never get here
135
				throw new IllegalStateException();
136
			}
137

    
138
			addPage(new DerivedUnitGeneralWizardPage(formFactory,
139
					selectionElement.getConversationHolder(), facade));
140
			addPage(new GatheringEventWizardPage(formFactory,
141
					selectionElement.getConversationHolder(), facade));
142
			addPage(new FieldUnitWizardPage(formFactory,
143
					selectionElement.getConversationHolder(), facade));
144
			if(facade.innerDerivedUnit()!=null){
145
			    addPage(new DerivedUnitBaseWizardPage(formFactory,
146
			            selectionElement.getConversationHolder(), facade));
147
			}
148
		} else if (rootElement.isInstanceOf(Collection.class)) {
149
			addPage(new CollectionWizardPage(formFactory,
150
					selectionElement.getConversationHolder(),
151
					HibernateProxyHelper.deproxy(rootElement, Collection.class)));
152
		} else if (rootElement instanceof Institution) {
153
			addPage(new InstitutionWizardPage(formFactory,
154
					selectionElement.getConversationHolder(),
155
					HibernateProxyHelper.deproxy(rootElement, Institution.class)));
156
		} else if (rootElement instanceof User) {
157
			addPage(new UserDetailWizardPage(formFactory,
158
					selectionElement.getConversationHolder(),
159
					HibernateProxyHelper.deproxy(rootElement, User.class)));
160
		} else if (rootElement instanceof Primer) {
161
            addPage(new PrimerWizardPage(formFactory,
162
                    selectionElement.getConversationHolder(),
163
                    HibernateProxyHelper.deproxy(rootElement, Primer.class)));
164
        } else if (rootElement instanceof Amplification) {
165
            addPage(new AmplificationGeneralWizardPage(formFactory,
166
                    selectionElement.getConversationHolder(),
167
                    HibernateProxyHelper.deproxy(rootElement, Amplification.class)));
168
        } else if (rootElement instanceof PolytomousKey) {
169
            addPage(new PolytomousKeyWizardPage(formFactory,
170
                    selectionElement.getConversationHolder(),
171
                    HibernateProxyHelper.deproxy(rootElement, PolytomousKey.class)));
172
        } else {
173
			MessagingUtils.warningDialog("Missing interface", this,
174
					"No detail element for current selection");
175
		}
176

    
177
	}
178

    
179
	/** {@inheritDoc} */
180
	@Override
181
	public boolean performFinish() {
182
	    IService<CdmBase> service = CdmStore.getService(rootElement);
183
	    if(rootElement.getId()!=0){
184
	        service.merge(rootElement, true);
185

    
186
	    }else{
187
	        rootElement = service.save(rootElement);
188
	    }
189
	    if(previousCdmEntitySession!=null){
190
            selectionElement.setEntity(previousCdmEntitySession.load(rootElement, true));
191
        }
192

    
193
		return true;
194
	}
195

    
196
	/** {@inheritDoc} */
197
	@Override
198
	public void widgetSelected(SelectionEvent e) {
199
		WizardDialog dialog = new WizardDialog(selectionElement.getShell(),
200
				this);
201
		if (dialog.open() == IStatus.OK) {
202
			selectionElement.updateFromWizard();
203
		}
204
	}
205

    
206
	/** {@inheritDoc} */
207
	@Override
208
	public void widgetDefaultSelected(SelectionEvent e) {
209
	}
210

    
211
    @Override
212
    public void dispose() {
213
        super.dispose();
214
        if(cdmEntitySession != null) {
215
            cdmEntitySession.dispose();
216
        }
217
        if(previousCdmEntitySession!=null){
218
            previousCdmEntitySession.bind();
219
        }
220
    }
221

    
222
    @Override
223
    public ICdmEntitySession getCdmEntitySession() {
224
        return cdmEntitySession;
225
    }
226

    
227
    @Override
228
    public java.util.Collection<CdmBase> getRootEntities() {
229
        return Collections.singleton(rootElement);
230
    }
231

    
232
    @Override
233
    public Map<Object, List<String>> getPropertyPathsMap() {
234
        return null;
235
    }
236
}
(2-2/6)