latest changes to Gabon and Guiana activators
[cdmlib-apps.git] / cdm-eflora / src / main / java / eu / etaxonomy / cdm / app / eflora / FloraGuianasActivator.java
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.cdm.app.eflora;
11
12 import java.net.URI;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Set;
16 import java.util.UUID;
17
18 import org.apache.log4j.Logger;
19 import org.springframework.transaction.TransactionStatus;
20
21 import eu.etaxonomy.cdm.api.service.ITermService;
22 import eu.etaxonomy.cdm.database.DbSchemaValidation;
23 import eu.etaxonomy.cdm.database.ICdmDataSource;
24 import eu.etaxonomy.cdm.io.common.CdmDefaultImport;
25 import eu.etaxonomy.cdm.io.common.IImportConfigurator.CHECK;
26 import eu.etaxonomy.cdm.io.common.events.IIoObserver;
27 import eu.etaxonomy.cdm.io.common.events.LoggingIoObserver;
28 import eu.etaxonomy.cdm.io.common.mapping.IInputTransformer;
29 import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
30 import eu.etaxonomy.cdm.io.markup.MarkupImportConfigurator;
31 import eu.etaxonomy.cdm.io.markup.MarkupTransformer;
32 import eu.etaxonomy.cdm.model.description.Feature;
33 import eu.etaxonomy.cdm.model.description.FeatureNode;
34 import eu.etaxonomy.cdm.model.description.FeatureTree;
35 import eu.etaxonomy.cdm.model.description.PolytomousKey;
36 import eu.etaxonomy.cdm.model.reference.Reference;
37 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
38
39 /**
40 * @author a.mueller
41 * @created 20.06.2008
42 * @version 1.0
43 */
44 public class FloraGuianasActivator {
45 private static final Logger logger = Logger.getLogger(FloraGuianasActivator.class);
46
47 //database validation status (create, update, validate ...)
48 static DbSchemaValidation hbm2dll = DbSchemaValidation.CREATE;
49 // static final URI source = EfloraSources.fdg_sample();
50 static final URI fgu1 = EfloraSources.fgu_1();
51 // static final URI fgu2 = EfloraSources.fgu_2();
52 // static final URI fgu3 = EfloraSources.fgu_3();
53 // static final URI fgu4 = EfloraSources.fgu_4();
54
55
56 // static final ICdmDataSource cdmDestination = CdmDestinations.cdm_flora_guianas_preview();
57 // static final ICdmDataSource cdmDestination = CdmDestinations.cdm_flora_guianas_production();
58 static final ICdmDataSource cdmDestination = CdmDestinations.localH2();
59 // static final ICdmDataSource cdmDestination = CdmDestinations.cdm_test_local_mysql();
60
61
62 //feature tree uuid
63 public static final UUID featureTreeUuid = UUID.fromString("2be99595-92fc-4f80-b9c4-b48d38505f5d");
64
65 //classification
66 static final UUID classificationUuid = UUID.fromString("5e3a1b07-2609-4597-bbda-7b02dfe8c2b3");
67
68 private static final String SOURCE_REFERENCE_TITLE = "Flora of the Guianas";
69 private static final String FEATURE_TREE_TITLE = "Flora of the Guianas Feature Tree";
70
71 //check - import
72 private boolean h2ForCheck = true;
73 static CHECK check = CHECK.IMPORT_WITHOUT_CHECK;
74
75 static boolean doPrintKeys = false;
76
77 //taxa
78 static final boolean doTaxa = true;
79
80 private boolean includeFgu1 = true;
81 private boolean includeFgu2 = false;
82 private boolean includeFgu3 = false;
83 private boolean includeFgu4 = false;
84
85
86 private boolean replaceStandardKeyTitles = true;
87
88 private IIoObserver observer = new LoggingIoObserver();
89 private Set<IIoObserver> observerList = new HashSet<IIoObserver>();
90
91
92 private void doImport(ICdmDataSource cdmDestination){
93 observerList.add(observer);
94 if (h2ForCheck && cdmDestination.getDatabaseType().equals(CdmDestinations.localH2().getDatabaseType())){
95 check = CHECK.CHECK_ONLY;
96 }
97
98 //make config
99 URI source = fgu1;
100 MarkupImportConfigurator markupConfig= MarkupImportConfigurator.NewInstance(source, cdmDestination);
101 markupConfig.setClassificationUuid(classificationUuid);
102 markupConfig.setDoTaxa(doTaxa);
103 markupConfig.setCheck(check);
104 markupConfig.setDoPrintKeys(doPrintKeys);
105 markupConfig.setDbSchemaValidation(hbm2dll);
106 markupConfig.setObservers(observerList);
107 markupConfig.setReplaceStandardKeyTitles(replaceStandardKeyTitles);
108
109
110 markupConfig.setSourceReference(getSourceReference(SOURCE_REFERENCE_TITLE));
111
112 CdmDefaultImport<MarkupImportConfigurator> myImport = new CdmDefaultImport<MarkupImportConfigurator>();
113
114 //Vol1
115 if (includeFgu1){
116 source = fgu1;
117 System.out.println("\nStart import from ("+ fgu1.toString() + ") ...");
118 markupConfig.setSource(source);
119 myImport.invoke(markupConfig);
120 System.out.println("End import from ("+ fgu1.toString() + ")...");
121 }
122
123 // //Vol2
124 // if (includeFgu2){
125 // source = fgu2;
126 // System.out.println("\nStart import from ("+ source.toString() + ") ...");
127 // markupConfig.setSource(source);
128 // myImport.invoke(markupConfig);
129 // System.out.println("End import from ("+ source.toString() + ")...");
130 // }
131 //
132 // //Vol3
133 // if (includeFgu3){
134 // source = fgu3;
135 // System.out.println("\nStart import from ("+ source.toString() + ") ...");
136 // markupConfig.setSource(source);
137 // myImport.invoke(markupConfig);
138 // System.out.println("End import from ("+ source.toString() + ")...");
139 // }
140 //
141 // //Vol4
142 // if (includeFgu4){
143 // source = fgu4;
144 // System.out.println("\nStart import from ("+ source.toString() + ") ...");
145 // markupConfig.setSource(source);
146 // myImport.invoke(markupConfig);
147 // System.out.println("End import from ("+ source.toString() + ")...");
148 // }
149
150 FeatureTree tree = makeFeatureNode(myImport.getCdmAppController().getTermService());
151 myImport.getCdmAppController().getFeatureTreeService().saveOrUpdate(tree);
152
153 //check keys
154 if (doPrintKeys){
155 TransactionStatus tx = myImport.getCdmAppController().startTransaction();
156 List<PolytomousKey> keys = myImport.getCdmAppController().getPolytomousKeyService().list(PolytomousKey.class, null, null, null, null);
157 for(PolytomousKey key : keys){
158 key.print(System.out);
159 System.out.println();
160 }
161 myImport.getCdmAppController().commitTransaction(tx);
162 }
163
164 }
165
166 private Reference<?> getSourceReference(String string) {
167 Reference<?> result = ReferenceFactory.newGeneric();
168 result.setTitleCache(string);
169 return result;
170 }
171
172 private FeatureTree makeFeatureNode(ITermService service){
173 MarkupTransformer transformer = new MarkupTransformer();
174
175 FeatureTree result = FeatureTree.NewInstance(featureTreeUuid);
176 result.setTitleCache(FEATURE_TREE_TITLE);
177 FeatureNode root = result.getRoot();
178 FeatureNode newNode;
179
180 newNode = FeatureNode.NewInstance(Feature.DESCRIPTION());
181 root.addChild(newNode);
182
183 addFeataureNodesByStringList(descriptionFeatureList, newNode, transformer, service);
184
185 addFeataureNodesByStringList(generellDescriptionsUpToAnatomyList, root, transformer, service);
186 newNode = FeatureNode.NewInstance(Feature.ANATOMY());
187 addFeataureNodesByStringList(anatomySubfeatureList, newNode, transformer, service);
188
189 newNode = addFeataureNodesByStringList(generellDescriptionsFromAnatomyToPhytoChemoList, root, transformer, service);
190 addFeataureNodesByStringList(phytoChemoSubFeaturesList, newNode, transformer, service);
191
192 newNode = addFeataureNodesByStringList(generellDescriptionsFromPhytoChemoList, root, transformer, service);
193
194
195 newNode = FeatureNode.NewInstance(Feature.DISTRIBUTION());
196 root.addChild(newNode);
197
198 newNode = FeatureNode.NewInstance(Feature.COMMON_NAME());
199 root.addChild(newNode);
200
201 newNode = FeatureNode.NewInstance(Feature.PHENOLOGY());
202 root.addChild(newNode);
203
204 newNode = FeatureNode.NewInstance(Feature.ECOLOGY());
205 root.addChild(newNode);
206 addFeataureNodesByStringList(habitatEcologyList, root, transformer, service);
207
208 newNode = FeatureNode.NewInstance(Feature.USES());
209 root.addChild(newNode);
210
211 addFeataureNodesByStringList(chomosomesList, root, transformer, service);
212
213 newNode = FeatureNode.NewInstance(Feature.CITATION());
214 root.addChild(newNode);
215
216 String sql = "\nSELECT feature.titleCache " +
217 " FROM DescriptionElementBase deb INNER JOIN DefinedTermBase feature ON deb.feature_id = feature.id " +
218 " GROUP BY feature.id " +
219 " HAVING feature.id NOT IN (SELECT DISTINCT fn.feature_id " +
220 " FROM FeatureNode fn " +
221 " WHERE fn.feature_id IS NOT NULL) ";
222 logger.warn("Check for missing features in feature tree: " + sql);
223
224 return result;
225 }
226
227 private static String [] chomosomesList = new String[]{
228 "Chromosomes",
229 };
230
231
232 private static String [] habitatEcologyList = new String[]{
233 "Habitat",
234 "Habitat & Ecology"
235 };
236
237
238 private static String [] generellDescriptionsUpToAnatomyList = new String[]{
239 "Fossils",
240 "Morphology and anatomy",
241 "Morphology",
242 "Vegetative morphology and anatomy",
243 };
244
245
246 private static String [] anatomySubfeatureList = new String[]{
247 "Leaf anatomy",
248 "Wood anatomy"
249 };
250
251 private static String [] generellDescriptionsFromAnatomyToPhytoChemoList = new String[]{
252 "Flower morphology",
253 "Palynology",
254 "Pollination",
255 "Pollen morphology",
256 "embryology",
257 "cytology",
258 "Life cycle",
259 "Fruits and embryology",
260 "Dispersal",
261 "Chromosome numbers",
262 "Phytochemistry and Chemotaxonomy",
263 };
264
265
266 private static String [] phytoChemoSubFeaturesList = new String[]{
267 "Alkaloids",
268 "Iridoid glucosides",
269 "Leaf phenolics",
270 "Storage products of seeds",
271 "Aluminium",
272 "Chemotaxonomy",
273 };
274
275
276 private static String [] generellDescriptionsFromPhytoChemoList = new String[]{
277 "Phytochemistry",
278 "Taxonomy",
279 "history",
280 "cultivation",
281 "Notes"
282 };
283
284
285 private static String [] descriptionFeatureList = new String[]{
286 "lifeform",
287 "Juvenile parts",
288 "Bark",
289 //new
290 "wood",
291 "Indumentum",
292 "endophytic body",
293 "flowering buds",
294 "Branchlets",
295 "Branches",
296 "Branch",
297 "Flowering branchlets",
298 "Trees",
299 "Twigs",
300 "stem",
301 "Stems",
302 "stem leaves",
303 "Leaves",
304 "extraxylary sclerenchyma",
305 "flower-bearing stems",
306 "Petiole",
307 "Petiolules",
308 "Leaflets",
309 "Lamina",
310 "Veins",
311 "Thyrsus",
312 "Thyrses",
313 "Inflorescences",
314 "Inflorescence",
315 "Young inflorescences",
316 "Male inflorescences",
317 "Female inflorescences",
318 "rachises",
319 "Pedicels",
320 "Bracts",
321 "flowering buds",
322 "scales",
323 "Buds",
324 "Flowers",
325 "Flower",
326 "Flowering",
327 "Stigma",
328 "perianth",
329 "Sepals",
330 "Sepal",
331 "Outer Sepals",
332 "Axillary",
333 "cymes",
334 "Calyx",
335 "Petal",
336 "Petals",
337 "perigone",
338 "perigone lobes",
339 "perigone tube",
340 "Disc",
341 "corolla",
342 "Stamens",
343 "Staminodes",
344 "Ovary",
345 "Anthers",
346 "anther",
347 "Pistil",
348 "Pistillode",
349 "Ovules",
350 "androecium",
351 "gynoecium",
352 "Filaments",
353 "Style",
354 "annulus",
355 "female flowers",
356 "Male flowers",
357 "Female",
358 "Infructescences", //order not consistent (sometimes before "Flowers")
359 "Fruit",
360 "Fruits",
361 "fruiting axes",
362 "drupes",
363 "Arillode",
364 "seed",
365 "Seeds",
366 "Seedling",
367 "flower tube",
368 "nutlets",
369 "pollen",
370 "secondary xylem",
371 "chromosome number",
372
373 "figure",
374 "fig",
375 "figs",
376
377
378
379 };
380
381 public FeatureNode addFeataureNodesByStringList(String[] featureStringList, FeatureNode root, IInputTransformer transformer, ITermService termService){
382 FeatureNode lastChild = null;
383 try {
384 for (String featureString : featureStringList){
385 UUID featureUuid;
386 featureUuid = transformer.getFeatureUuid(featureString);
387 Feature feature = (Feature)termService.find(featureUuid);
388 if (feature != null){
389 FeatureNode child = FeatureNode.NewInstance(feature);
390 root.addChild(child);
391 }
392 }
393
394 } catch (UndefinedTransformerMethodException e) {
395 logger.error("getFeatureUuid is not implemented in transformer. Features could not be added");
396 }
397 return lastChild;
398 }
399
400
401
402 /**
403 * @param args
404 */
405 public static void main(String[] args) {
406 FloraGuianasActivator me = new FloraGuianasActivator();
407 me.doImport(cdmDestination);
408 }
409 }