latest FMActivator changes
[cdmlib-apps.git] / cdm-eflora / src / main / java / eu / etaxonomy / cdm / app / eflora / FloraMalesianaActivator.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.eflora.floraMalesiana.FloraMalesianaTransformer;
31 import eu.etaxonomy.cdm.io.markup.MarkupImportConfigurator;
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 */
43 public class FloraMalesianaActivator extends EfloraActivatorBase {
44 private static final Logger logger = Logger.getLogger(FloraMalesianaActivator.class);
45
46 //database validation status (create, update, validate ...)
47 static DbSchemaValidation hbm2dll = DbSchemaValidation.CREATE;
48
49 static final URI fmSource12_1 = EfloraSources.fm_12_1();
50 static final URI fmSource12_2 = EfloraSources.fm_12_2();
51 static final URI fmSource13 = EfloraSources.fm_13();
52 static final URI fmSource14 = EfloraSources.fm_14();
53 static final URI fmSource15 = EfloraSources.fm_15();
54 static final URI fmSource16 = EfloraSources.fm_16();
55 static final URI fmSource17_1 = EfloraSources.fm_17_1();
56 static final URI fmSource17_2 = EfloraSources.fm_17_2();
57 static final URI fmSource18 = EfloraSources.fm_18();
58 static final URI fmSource19 = EfloraSources.fm_19();
59 static final URI fmSource20 = EfloraSources.fm_20();
60
61 static final URI fmSource_Ser2_02 = EfloraSources.fm_ser2_2();
62 static final URI fmSource_Ser2_03 = EfloraSources.fm_ser2_3();
63
64
65 // static final ICdmDataSource cdmDestination = CdmDestinations.cdm_flora_malesiana_preview();
66 // static final ICdmDataSource cdmDestination = CdmDestinations.cdm_flora_malesiana_production();
67 static final ICdmDataSource cdmDestination = CdmDestinations.localH2();
68 // static final ICdmDataSource cdmDestination = CdmDestinations.cdm_test_local_mysql();
69 // static final ICdmDataSource cdmDestination = CdmDestinations.cdm_test_local_mysql_test();
70
71 private boolean includeBase = true;
72 private boolean includeVol12_1 = includeBase;
73 private boolean includeVol12_2 = includeBase;
74 private boolean includeVol13 = includeBase;
75 private boolean includeVol14 = includeBase;
76 private boolean includeVol15 = includeBase;
77 private boolean includeVol16 = includeBase;
78 private boolean includeVol17_1 = includeBase;
79 private boolean includeVol17_2 = includeBase;
80 private boolean includeVol18 = includeBase;
81 private boolean includeVol19 = includeBase;
82 private boolean includeVol20 = includeBase;
83 private boolean includeVol2_2 = includeBase;
84 private boolean includeVol2_3 = includeBase;
85
86 private boolean h2ForCheck = false;
87
88 static final boolean reuseState = true; //when running multiple imports
89
90
91 //feature tree uuid
92 public static final UUID featureTreeUuid = UUID.fromString("168df0c6-6429-484c-b26f-ded1f7e44bd9");
93 private static final String featureTreeTitle = "Flora Malesiana Presentation Feature Tree";
94
95 //classification
96 static final UUID classificationUuid = UUID.fromString("ca4e4bcb-a1d1-4124-a358-a3d3c41dd450");
97 static final String classificationTitle = "Flora Malesiana";
98
99 //check - import
100 static CHECK check = CHECK.IMPORT_WITHOUT_CHECK;
101
102 static boolean doPrintKeys = false;
103
104 //taxa
105 static final boolean doTaxa = true;
106
107
108 private boolean replaceStandardKeyTitles = false;
109
110 private IIoObserver observer = new LoggingIoObserver();
111 private Set<IIoObserver> observerList = new HashSet<IIoObserver>();
112
113
114 private void doImport(ICdmDataSource cdmDestination){
115 observerList.add(observer);
116 if (h2ForCheck && cdmDestination.getDatabaseType().equals(CdmDestinations.localH2().getDatabaseType())){
117 check = CHECK.CHECK_ONLY;
118 }
119
120 //make Source
121 // URI source = fmSource13_small;
122 URI source = null;
123
124 MarkupImportConfigurator markupConfig= MarkupImportConfigurator.NewInstance(source, cdmDestination);
125 markupConfig.setClassificationUuid(classificationUuid);
126 markupConfig.setClassificationName(classificationTitle);
127 markupConfig.setDoTaxa(doTaxa);
128 markupConfig.setCheck(check);
129 markupConfig.setDoPrintKeys(doPrintKeys);
130 markupConfig.setDbSchemaValidation(hbm2dll);
131 markupConfig.setObservers(observerList);
132 markupConfig.setReplaceStandardKeyTitles(replaceStandardKeyTitles);
133 markupConfig.setReuseExistingState(reuseState);
134
135 markupConfig.setSourceReference(getSourceReference("Flora Malesiana - Vol. 13"));
136
137 CdmDefaultImport<MarkupImportConfigurator> myImport = new CdmDefaultImport<MarkupImportConfigurator>();
138
139
140
141 //Vol16
142 doSource(includeVol16, fmSource16, "Flora Malesiana - vol. 16", markupConfig, myImport);
143
144
145 //Vol12_1
146 doSource(includeVol12_1, fmSource12_1, "Flora Malesiana - vol. 12, pt.1", markupConfig, myImport);
147
148 //Vol12_2
149 doSource(includeVol12_2, fmSource12_2, "Flora Malesiana - vol. 12, pt.2", markupConfig, myImport);
150
151 //Vol13_large
152 doSource(includeVol13, fmSource13, "Flora Malesiana - vol. 13", markupConfig, myImport);
153
154 //Vol14
155 doSource(includeVol14, fmSource14, "Flora Malesiana - vol. 14", markupConfig, myImport);
156
157 //Vol15
158 doSource(includeVol15, fmSource15, "Flora Malesiana - vol. 15", markupConfig, myImport);
159 //Vol17, part1
160 doSource(includeVol17_1, fmSource17_1, "Flora Malesiana - vol. 17, part I", markupConfig, myImport);
161
162 //Vol17, part2
163 doSource(includeVol17_2, fmSource17_2, "Flora Malesiana - vol. 17, part II", markupConfig, myImport);
164
165 //Vol18
166 doSource(includeVol18, fmSource18, "Flora Malesiana - vol. 18", markupConfig, myImport);
167
168 //Vol19
169 doSource(includeVol19, fmSource19, "Flora Malesiana - vol. 19", markupConfig, myImport);
170
171 //Vol20
172 doSource(includeVol20, fmSource20, "Flora Malesiana - vol. 20", markupConfig, myImport);
173
174 //Vol_2_2
175 doSource(includeVol2_2, fmSource_Ser2_02, "Flora Malesiana - Ser.2, vol. 2", markupConfig, myImport);
176
177 //Vol_2_3
178 doSource(includeVol2_3, fmSource_Ser2_03, "Flora Malesiana - Ser.2, vol. 3", markupConfig, myImport);
179
180 makeAutomatedFeatureTree(myImport.getCdmAppController(), markupConfig.getState(),
181 featureTreeUuid, featureTreeTitle);
182
183 // makeGeoService();
184
185 FeatureTree tree = makeFeatureNode(myImport.getCdmAppController().getTermService());
186 myImport.getCdmAppController().getFeatureTreeService().saveOrUpdate(tree);
187
188 //check keys
189 if (doPrintKeys){
190 TransactionStatus tx = myImport.getCdmAppController().startTransaction();
191 List<PolytomousKey> keys = myImport.getCdmAppController().getPolytomousKeyService().list(PolytomousKey.class, null, null, null, null);
192 for(PolytomousKey key : keys){
193 key.print(System.out);
194 System.out.println();
195 }
196 myImport.getCdmAppController().commitTransaction(tx);
197 }
198
199 }
200
201 private void doSource(boolean doInclude, URI source, String sourceTitle, MarkupImportConfigurator markupConfig,
202 CdmDefaultImport<MarkupImportConfigurator> myImport) {
203 if (doInclude){
204 System.out.println("\nStart import from ("+ source.toString() + ") ...");
205 markupConfig.setSource(source);
206 markupConfig.setSourceReference(getSourceReference(sourceTitle));
207 myImport.invoke(markupConfig);
208 System.out.println("End import from ("+ source.toString() + ")...");
209 }
210 }
211
212 private Reference<?> getSourceReference(String string) {
213 Reference<?> result = ReferenceFactory.newGeneric();
214 result.setTitleCache(string);
215 return result;
216 }
217
218 private FeatureTree makeFeatureNode(ITermService service){
219 FloraMalesianaTransformer transformer = new FloraMalesianaTransformer();
220
221 FeatureTree result = FeatureTree.NewInstance(UUID.randomUUID());
222 result.setTitleCache("Flora Malesiana Presentation Feature Tree - Old");
223 FeatureNode root = result.getRoot();
224 FeatureNode newNode;
225
226 newNode = FeatureNode.NewInstance(Feature.DESCRIPTION());
227 root.addChild(newNode);
228
229 addFeataureNodesByStringList(descriptionFeatureList, newNode, transformer, service);
230
231 addFeataureNodesByStringList(generellDescriptionsUpToAnatomyList, root, transformer, service);
232 newNode = FeatureNode.NewInstance(Feature.ANATOMY());
233 addFeataureNodesByStringList(anatomySubfeatureList, newNode, transformer, service);
234
235 newNode = addFeataureNodesByStringList(generellDescriptionsFromAnatomyToPhytoChemoList, root, transformer, service);
236 addFeataureNodesByStringList(phytoChemoSubFeaturesList, newNode, transformer, service);
237
238 newNode = addFeataureNodesByStringList(generellDescriptionsFromPhytoChemoList, root, transformer, service);
239
240
241 newNode = FeatureNode.NewInstance(Feature.DISTRIBUTION());
242 root.addChild(newNode);
243
244 newNode = FeatureNode.NewInstance(Feature.ECOLOGY());
245 root.addChild(newNode);
246 addFeataureNodesByStringList(habitatEcologyList, root, transformer, service);
247
248 newNode = FeatureNode.NewInstance(Feature.USES());
249 root.addChild(newNode);
250
251 addFeataureNodesByStringList(chomosomesList, root, transformer, service);
252
253 newNode = FeatureNode.NewInstance(Feature.CITATION());
254 root.addChild(newNode);
255
256 return result;
257 }
258
259 private static String [] chomosomesList = new String[]{
260 "Chromosomes",
261 };
262
263
264 private static String [] habitatEcologyList = new String[]{
265 "Habitat",
266 "Habitat & Ecology"
267 };
268
269
270 private static String [] generellDescriptionsUpToAnatomyList = new String[]{
271 "Fossils",
272 "Morphology and anatomy",
273 "Morphology",
274 "Vegetative morphology and anatomy",
275 };
276
277
278 private static String [] anatomySubfeatureList = new String[]{
279 "Leaf anatomy",
280 "Wood anatomy"
281 };
282
283 private static String [] generellDescriptionsFromAnatomyToPhytoChemoList = new String[]{
284 "Flower morphology",
285 "Palynology",
286 "Pollination",
287 "Pollen morphology",
288 "embryology",
289 "cytology",
290 "Life cycle",
291 "Fruits and embryology",
292 "Dispersal",
293 "Chromosome numbers",
294 "Phytochemistry and Chemotaxonomy",
295 };
296
297
298 private static String [] phytoChemoSubFeaturesList = new String[]{
299 "Alkaloids",
300 "Iridoid glucosides",
301 "Leaf phenolics",
302 "Storage products of seeds",
303 "Aluminium",
304 "Chemotaxonomy",
305 };
306
307
308 private static String [] generellDescriptionsFromPhytoChemoList = new String[]{
309 "Phytochemistry",
310 "Taxonomy",
311 "history",
312 "cultivation",
313 "Notes"
314 };
315
316
317 private static String [] descriptionFeatureList = new String[]{
318 "lifeform",
319 "Bark",
320 //new
321 "wood",
322 "Indumentum",
323 "endophytic body",
324 "flowering buds",
325 "Branchlets",
326 "Branches",
327 "Branch",
328 "Flowering branchlets",
329 "Trees",
330 "Twigs",
331 "stem",
332 "Stems",
333 "stem leaves",
334 "Leaves",
335 "flower-bearing stems",
336 "Petiole",
337 "Petiolules",
338 "Leaflets",
339 "Thyrsus",
340 "Thyrses",
341 "Inflorescences",
342 "Inflorescence",
343 "Young inflorescences",
344 "Male inflorescences",
345 "Female inflorescences",
346 "Bracts",
347 "Pedicels",
348 "flowering buds",
349 "scales",
350 "Buds",
351 "Flowers",
352 "Flower",
353 "Flowering",
354 "Stigma",
355 "perianth",
356 "Sepals",
357 "Sepal",
358 "Outer Sepals",
359 "Axillary",
360 "cymes",
361 "Calyx",
362 "Petal",
363 "Petals",
364 "perigone",
365 "perigone lobes",
366 "perigone tube",
367 "Disc",
368 "corolla",
369 "Stamens",
370 "Staminodes",
371 "Ovary",
372 "Anthers",
373 "anther",
374 "Pistil",
375 "Pistillode",
376 "Ovules",
377 "androecium",
378 "gynoecium",
379 "Filaments",
380 "Style",
381 "annulus",
382 "female flowers",
383 "Male flowers",
384 "Female",
385 "Infructescences", //order not consistent (sometimes before "Flowers")
386 "Fruit",
387 "Fruits",
388 "fruiting axes",
389 "drupes",
390 "Arillode",
391 "seed",
392 "Seeds",
393 "Seedling",
394 "flower tube",
395 "nutlets",
396 "pollen",
397 "secondary xylem",
398 "chromosome number",
399
400 "figure",
401 "fig",
402 "figs",
403
404
405
406 };
407
408 public FeatureNode addFeataureNodesByStringList(String[] featureStringList, FeatureNode root, IInputTransformer transformer, ITermService termService){
409 FeatureNode lastChild = null;
410 try {
411 for (String featureString : featureStringList){
412 UUID featureUuid;
413 featureUuid = transformer.getFeatureUuid(featureString);
414 Feature feature = (Feature)termService.find(featureUuid);
415 if (feature != null){
416 FeatureNode child = FeatureNode.NewInstance(feature);
417 root.addChild(child);
418 }
419 }
420
421 } catch (UndefinedTransformerMethodException e) {
422 logger.error("getFeatureUuid is not implemented in transformer. Features could not be added");
423 }
424 return lastChild;
425 }
426
427
428
429 /**
430 * @param args
431 */
432 public static void main(String[] args) {
433 FloraMalesianaActivator me = new FloraMalesianaActivator();
434 me.doImport(cdmDestination);
435 }
436
437 }