Project

General

Profile

Download (9.56 KB) Statistics
| Branch: | Tag: | Revision:
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.ui.selection;
12

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

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

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

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

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

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

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

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

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

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

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

    
178
	}
179

    
180
	/** {@inheritDoc} */
181
	@Override
182
	public boolean performFinish() {
183
	    IService<CdmBase> service = CdmStore.getService(rootElement);
184
	    if(rootElement.getId()!=0){
185
	        service.merge(rootElement, true);
186
	    }
187
	    if(previousCdmEntitySession!=null){
188
	        selectionElement.setEntity(previousCdmEntitySession.load(rootElement, true));
189
	    }
190
		return true;
191
	}
192

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

    
203
	/** {@inheritDoc} */
204
	@Override
205
	public void widgetDefaultSelected(SelectionEvent e) {
206
	}
207

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

    
219
    @Override
220
    public ICdmEntitySession getCdmEntitySession() {
221
        return cdmEntitySession;
222
    }
223

    
224
    @Override
225
    public java.util.Collection<CdmBase> getRootEntities() {
226
        return Collections.singleton(rootElement);
227
    }
228

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