fixes #2189
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / preference / wizard / VocabularyTermWizard.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.preference.wizard;
12
13 import org.eclipse.jface.wizard.Wizard;
14
15 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
16 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
17 import eu.etaxonomy.cdm.api.service.ITermService;
18 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
19 import eu.etaxonomy.cdm.model.common.Representation;
20 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
21 import eu.etaxonomy.taxeditor.store.CdmStore;
22 import eu.etaxonomy.taxeditor.store.StoreUtil;
23 import eu.etaxonomy.taxeditor.store.TermStore;
24
25 /**
26 * <p>VocabularyTermWizard class.</p>
27 *
28 * @author n.hoffmann
29 * @created 12.06.2009
30 * @version 1.0
31 */
32 public class VocabularyTermWizard<T extends DefinedTermBase> extends Wizard implements IConversationEnabled {
33
34 public T term = null;
35
36 private boolean editMode = false;
37
38 private Class<T> termClass;
39
40 private ConversationHolder conversation;
41
42 /**
43 * <p>Constructor for VocabularyTermWizard.</p>
44 *
45 * @param vocabularyClass a {@link java.lang.Class} object.
46 */
47 public VocabularyTermWizard(Class<T> vocabularyClass){
48 conversation = CdmStore.createConversation();
49 termClass = vocabularyClass;
50 }
51
52 /**
53 * <p>Constructor for VocabularyTermWizard.</p>
54 *
55 * @param vocabularyClass a {@link java.lang.Class} object.
56 * @param selectedTerm a T object.
57 */
58 public VocabularyTermWizard(Class<T> vocabularyClass, T selectedTerm){
59 this(vocabularyClass);
60 term = (T) CdmStore.getService(ITermService.class).load(selectedTerm.getUuid());
61 editMode = true;
62 }
63
64 /* (non-Javadoc)
65 * @see org.eclipse.jface.wizard.Wizard#addPages()
66 */
67 /** {@inheritDoc} */
68 @Override
69 public void addPages() {
70 super.addPages();
71
72 addPage(new VocabularyTermWizardPage());
73 }
74
75 /* (non-Javadoc)
76 * @see org.eclipse.jface.wizard.Wizard#performFinish()
77 */
78 /** {@inheritDoc} */
79 @Override
80 public boolean performFinish() {
81
82 if(editMode){
83 TermStore.updateVocabulary(getTerm());
84 }else{
85 TermStore.saveTerm(getTerm());
86 }
87
88 getConversationHolder().commit(false);
89 getConversationHolder().close();
90
91 return true;
92 }
93
94 /**
95 * <p>Setter for the field <code>term</code>.</p>
96 *
97 * @param term a T object.
98 */
99 public void setTerm(T term) {
100 this.term = term;
101 }
102
103 /**
104 * <p>Setter for the field <code>term</code>.</p>
105 *
106 * @param text
107 * @param text2
108 * @param text3
109 * @param label a {@link java.lang.String} object.
110 * @param abbreviatedLabel a {@link java.lang.String} object.
111 */
112 public void setTerm(String text, String label, String abbreviatedLabel) {
113
114 try {
115 if(getTerm() == null){
116 term = (T) termClass.newInstance();
117
118 term.addRepresentation(new Representation(text, label, abbreviatedLabel, CdmStore.getDefaultLanguage()));
119 }else{
120 Representation representation = term.getPreferredRepresentation(CdmStore.getDefaultLanguage());
121 representation.setText(text);
122 representation.setLabel(label);
123 representation.setAbbreviatedLabel(abbreviatedLabel);
124 }
125
126 } catch (InstantiationException e) {
127 StoreUtil.error(this.getClass(), "InstantiationException when setting term", e);
128 } catch (IllegalAccessException e) {
129 StoreUtil.error(this.getClass(), "IllegalAccessException when setting term", e);
130 }
131
132 }
133
134 /**
135 * <p>Getter for the field <code>term</code>.</p>
136 *
137 * @return the term
138 */
139 public T getTerm() {
140 return term;
141 }
142
143 public Representation getRepresentation(){
144 return term != null ? term.getRepresentation(CdmStore.getDefaultLanguage()) : null;
145 }
146
147 /**
148 * <p>isEditMode</p>
149 *
150 * @return the editMode
151 */
152 public boolean isEditMode() {
153 return editMode;
154 }
155
156 /**
157 * <p>Getter for the field <code>termClass</code>.</p>
158 *
159 * @return a {@link java.lang.Class} object.
160 */
161 public Class<T> getTermClass() {
162 return termClass;
163 }
164
165 /* (non-Javadoc)
166 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
167 */
168 @Override
169 public void update(CdmDataChangeMap changeEvents) {
170 // TODO Auto-generated method stub
171
172 }
173
174 /* (non-Javadoc)
175 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
176 */
177 @Override
178 public ConversationHolder getConversationHolder() {
179 return conversation;
180 }
181
182 }