Project

General

Profile

Download (9.88 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
package eu.etaxonomy.taxeditor.ui.selection;
10

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

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

    
21
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
22
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;
23
import eu.etaxonomy.cdm.api.service.IService;
24
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
25
import eu.etaxonomy.cdm.model.agent.Institution;
26
import eu.etaxonomy.cdm.model.agent.Person;
27
import eu.etaxonomy.cdm.model.agent.Team;
28
import eu.etaxonomy.cdm.model.common.CdmBase;
29
import eu.etaxonomy.cdm.model.permission.User;
30
import eu.etaxonomy.cdm.model.description.PolytomousKey;
31
import eu.etaxonomy.cdm.model.molecular.Amplification;
32
import eu.etaxonomy.cdm.model.molecular.Primer;
33
import eu.etaxonomy.cdm.model.name.TaxonName;
34
import eu.etaxonomy.cdm.model.occurrence.Collection;
35
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
36
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
37
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
38
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
39
import eu.etaxonomy.cdm.model.reference.Reference;
40
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
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.taxon.TaxonWizardPage;
62
import eu.etaxonomy.taxeditor.ui.section.user.UserDetailWizardPage;
63

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

    
71
    private ICdmEntitySession cdmEntitySession;
72
    private ICdmEntitySession previousCdmEntitySession;
73
	private final EntitySelectionElement<CdmBase> 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
	@Override
87
	public void addPages() {
88

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

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

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

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

    
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
	    }else{
188
	        rootElement = service.save(rootElement);
189
	    }
190
	    if(previousCdmEntitySession!=null){
191
            selectionElement.setEntity(previousCdmEntitySession.load(rootElement, true));
192
        }
193

    
194
		return true;
195
	}
196

    
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
	@Override
207
	public void widgetDefaultSelected(SelectionEvent e) {
208
	}
209

    
210
    @Override
211
    public void dispose() {
212
        super.dispose();
213
        if(cdmEntitySession != null) {
214
            cdmEntitySession.dispose();
215
            cdmEntitySession = null;
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
}
(3-3/8)