5856b19cfbf51802658a78747f6d9fdd0f9e2d58
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / UseObjectManager.java
1 /**
2 * Copyright (C) 2011 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.editor;
10
11 import java.net.URI;
12 import java.net.URISyntaxException;
13
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.ui.IMemento;
16
17 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
18 import eu.etaxonomy.cdm.api.service.ITermService;
19 import eu.etaxonomy.cdm.api.service.IVocabularyService;
20 import eu.etaxonomy.cdm.model.common.DefinedTerm;
21 import eu.etaxonomy.cdm.model.common.MarkerType;
22 import eu.etaxonomy.cdm.model.common.TermType;
23 import eu.etaxonomy.cdm.model.common.TermVocabulary;
24 import eu.etaxonomy.cdm.model.description.Feature;
25 import eu.etaxonomy.cdm.model.description.State;
26 import eu.etaxonomy.taxeditor.model.ContextListenerAdapter;
27 import eu.etaxonomy.taxeditor.model.MessagingUtils;
28 import eu.etaxonomy.taxeditor.store.CdmStore;
29
30 /**
31 * The context listener will call this class when the uses view is being loaded.
32 * It checks if the uses view's necessary terms are present in the database.
33 * It will persist the terms that aren't present, using the CDMLib's services classes.
34 * @author a.theys
35 * @created mar 13, 2012
36 */
37 public class UseObjectManager extends ContextListenerAdapter{
38
39 public UseObjectManager() {
40 CdmStore.getContextManager().addContextListener(this);
41 }
42
43
44 @Override
45 public void contextRefresh(IProgressMonitor monitor) {
46 monitor.subTask("Refreshing the Uses View");
47 MessagingUtils.warn(getClass(), "Refreshing Uses View warn");
48 setupNecessaryItems(monitor);
49 }
50
51
52 @Override
53 public void contextStart(IMemento memento, IProgressMonitor monitor) {
54 monitor.subTask("Starting the Uses View");
55 MessagingUtils.warn(getClass(), "Starting Uses View warn");
56 setupNecessaryItems(monitor);
57 }
58
59 private void setupNecessaryItems(IProgressMonitor monitor) {
60
61 //retrieve terms and vocabularies from db
62 Feature featureUseRecord = (Feature) CdmStore.getService(ITermService.class).find(UsageTermCollection.uuidUseRecordFeature);
63 Feature featureUseSummary = (Feature) CdmStore.getService(ITermService.class).find(UsageTermCollection.uuidUseSummaryFeature);
64
65 ConversationHolder conversation = CdmStore.createConversation();
66
67
68 //create use marker type if not exists
69 createUseMarkerType(conversation);
70
71 //create state vocabulary if not exists
72 createStateVocabulary(monitor, conversation);
73
74 //create state vocabulary if not exists
75 createCountryVocabulary(monitor, conversation);
76
77 //create plant part vocabulary
78 createPlantVocabulary(monitor, conversation);
79
80 //create human group vocabulary
81 createHumanGroupVocabulary(monitor, conversation);
82
83 if(featureUseRecord == null || featureUseSummary == null) {
84 TermVocabulary<Feature> featureVocabulary = CdmStore.getService(IVocabularyService.class).find((UsageTermCollection.uuidFeatureVocabulary));
85 // FeatureTree palmWebFeatureTree = CdmStore.getService(IFeatureTreeService.class).find(UsageTermCollection.uuidPalmWebFeatureTree);
86
87 if (featureUseRecord == null ) {
88 featureUseRecord = Feature.NewInstance(UsageTermCollection.useRecordFeatureLabel, UsageTermCollection.useRecordFeatureLabel, null);
89 featureUseRecord.setUuid(UsageTermCollection.uuidUseRecordFeature);
90 featureUseRecord.setSupportsCategoricalData(true);
91 featureVocabulary.addTerm(featureUseRecord);
92
93 // if (palmWebFeatureTree != null){
94 // FeatureNode useRecFeatureNode = FeatureNode.NewInstance(featureUseRecord);
95 // palmWebFeatureTree.getRoot().addChild(useRecFeatureNode);
96 // }else{
97 // StoreUtil.warn(getClass(), "No current feature tree available to add useRecord feature");
98 // }
99 }
100 if (featureUseSummary == null) {
101 featureUseSummary = Feature.NewInstance(UsageTermCollection.useSummaryFeatureLabel, UsageTermCollection.useSummaryFeatureLabel, null);
102 featureUseSummary.setUuid(UsageTermCollection.uuidUseSummaryFeature);
103 featureUseSummary.setSupportsTextData(true);
104 featureVocabulary.addTerm(featureUseSummary);
105 // if (palmWebFeatureTree != null){
106 // FeatureNode useSumFeatureNode = FeatureNode.NewInstance(featureUseSummary);
107 // palmWebFeatureTree.getRoot().addChild(useSumFeatureNode);
108 // }else{
109 // StoreUtil.warn(getClass(), "No current feature tree available to add use summary feature");
110 // }
111 }
112
113 CdmStore.getService(IVocabularyService.class).saveOrUpdate(featureVocabulary);
114 // if (palmWebFeatureTree != null){
115 // CdmStore.getService(IFeatureTreeService.class).saveOrUpdate(palmWebFeatureTree);
116 // }
117 conversation.commit(true);
118
119 }
120 conversation.close();
121 }
122
123
124 private void createHumanGroupVocabulary(IProgressMonitor monitor,ConversationHolder conversation) {
125 TermVocabulary<DefinedTerm> humanGroupVocabulary = CdmStore.getService(IVocabularyService.class).find(UsageTermCollection.uuidHumanGroupVocabulary);
126 if (humanGroupVocabulary == null){
127 monitor.subTask("create human group vocabulary");
128 URI termSourceUri = null;
129 try {
130 termSourceUri = new URI("eu.etaxonomy.cdm.model.description.DefinedTerm");
131 } catch (URISyntaxException e) {
132 e.printStackTrace();
133 }
134
135 humanGroupVocabulary = TermVocabulary.NewInstance(TermType.Modifier, UsageTermCollection.humanGroupLabel, UsageTermCollection.humanGroupLabel, null, termSourceUri);
136 humanGroupVocabulary.setUuid(UsageTermCollection.uuidHumanGroupVocabulary);
137
138 DefinedTerm newDummyModifier = DefinedTerm.NewInstance(TermType.Modifier, UsageTermCollection.notAvailableLabel, UsageTermCollection.notAvailableLabel, null);
139 newDummyModifier.setUuid(UsageTermCollection.uuidHumanGroupDummy);
140
141 DefinedTerm newSubDummyModifier = DefinedTerm.NewInstance(TermType.Modifier, UsageTermCollection.notAvailableLabel, UsageTermCollection.notAvailableLabel, null);
142 newSubDummyModifier.setUuid(UsageTermCollection.uuidEthnicGroupDummy);
143 newDummyModifier.addIncludes(newSubDummyModifier);
144 humanGroupVocabulary.addTerm(newDummyModifier);
145
146 CdmStore.getService(IVocabularyService.class).saveOrUpdate(humanGroupVocabulary);
147 conversation.commit(true);
148 }
149 }
150
151
152 private void createPlantVocabulary(IProgressMonitor monitor, ConversationHolder conversation) {
153 TermVocabulary<DefinedTerm> plantPartVocabulary = CdmStore.getService(IVocabularyService.class).find(UsageTermCollection.uuidPlantPartVocabulary);
154 if (plantPartVocabulary == null){
155 monitor.subTask("create plant part vocabulary");
156 URI termSourceUri = null;
157 try {
158 termSourceUri = new URI("eu.etaxonomy.cdm.model.description.DefinedTerm");
159 } catch (URISyntaxException e) {
160 e.printStackTrace();
161 }
162 plantPartVocabulary = TermVocabulary.NewInstance(TermType.Modifier, UsageTermCollection.plantPartLabel, UsageTermCollection.plantPartLabel, null, termSourceUri);
163 plantPartVocabulary.setUuid(UsageTermCollection.uuidPlantPartVocabulary);
164
165 DefinedTerm newDummyModifier = DefinedTerm.NewInstance(TermType.Modifier, UsageTermCollection.notAvailableLabel, UsageTermCollection.notAvailableLabel, null);
166 newDummyModifier.setUuid(UsageTermCollection.uuidPlantPartDummy);
167 plantPartVocabulary.addTerm(newDummyModifier);
168
169 CdmStore.getService(IVocabularyService.class).saveOrUpdate(plantPartVocabulary);
170 conversation.commit(true);
171 }
172 }
173
174
175 private void createCountryVocabulary(IProgressMonitor monitor, ConversationHolder conversation) {
176 TermVocabulary<DefinedTerm> countryVocabulary = CdmStore.getService(IVocabularyService.class).find(UsageTermCollection.uuidCountryVocabulary);
177 if (countryVocabulary == null){
178 monitor.subTask("create country vocabulary");
179 URI termSourceUri = null;
180 try {
181 termSourceUri = new URI("eu.etaxonomy.cdm.model.description.DefinedTerm");
182 } catch (URISyntaxException e) {
183 e.printStackTrace();
184 }
185 countryVocabulary = TermVocabulary.NewInstance(TermType.Modifier, UsageTermCollection.countryLabel, UsageTermCollection.countryLabel, null, termSourceUri);
186 countryVocabulary.setUuid(UsageTermCollection.uuidCountryVocabulary);
187
188 DefinedTerm newDummyModifier = DefinedTerm.NewInstance(TermType.Modifier, UsageTermCollection.notAvailableLabel, UsageTermCollection.notAvailableLabel, null);
189 newDummyModifier.setUuid(UsageTermCollection.uuidCountryDummy);
190 countryVocabulary.addTerm(newDummyModifier);
191
192 CdmStore.getService(IVocabularyService.class).saveOrUpdate(countryVocabulary);
193 conversation.commit(true);
194 }
195 }
196
197
198 private void createStateVocabulary(IProgressMonitor monitor, ConversationHolder conversation) {
199 TermVocabulary<State> stateVocabulary = CdmStore.getService(IVocabularyService.class).find(UsageTermCollection.uuidUseCategoryVocabulary);
200 if (stateVocabulary == null){
201 monitor.subTask("create state vocabulary");
202 URI termSourceUri = null;
203 try {
204 termSourceUri = new URI("eu.etaxonomy.cdm.model.description.State");
205 } catch (URISyntaxException e) {
206 e.printStackTrace();
207 }
208
209 stateVocabulary = TermVocabulary.NewInstance(TermType.State, UsageTermCollection.useCategoryVocabularyLabel, UsageTermCollection.useCategoryVocabularyLabel, null, termSourceUri);
210 stateVocabulary.setUuid(UsageTermCollection.uuidUseCategoryVocabulary);
211
212 State newDummyState = State.NewInstance(UsageTermCollection.notAvailableLabel, UsageTermCollection.notAvailableLabel, null);
213 newDummyState.setUuid(UsageTermCollection.uuidUseCategoryDummy);
214 State newDummySubCat = State.NewInstance(UsageTermCollection.notAvailableLabel, UsageTermCollection.notAvailableLabel, null);
215 newDummySubCat.setUuid(UsageTermCollection.uuidUseSubCategoryDummy);
216 newDummyState.addIncludes(newDummySubCat);
217
218 stateVocabulary.addTerm(newDummyState);
219
220 CdmStore.getService(IVocabularyService.class).saveOrUpdate(stateVocabulary);
221 conversation.commit(true);
222 }
223 }
224
225
226 private void createUseMarkerType(ConversationHolder conversation) {
227 MarkerType useMarkertype = (MarkerType) CdmStore.getService(ITermService.class).find(UsageTermCollection.uuidUseMarkerType);
228 if (useMarkertype == null){
229 useMarkertype = MarkerType.NewInstance(UsageTermCollection.useMakerLabel, UsageTermCollection.useMakerLabel, null);
230 useMarkertype.setUuid( UsageTermCollection.uuidUseMarkerType);
231 TermVocabulary<MarkerType> markerTypeVocabulary = CdmStore.getService(IVocabularyService.class).find((UsageTermCollection.uuidMarkersVocabulary));
232 markerTypeVocabulary.addTerm(useMarkertype);
233 CdmStore.getService(IVocabularyService.class).saveOrUpdate(markerTypeVocabulary);
234 conversation.commit(true);
235 }
236 }
237 }