2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.app
.eflora
;
13 import java
.util
.HashSet
;
14 import java
.util
.List
;
16 import java
.util
.UUID
;
18 import org
.apache
.log4j
.Logger
;
19 import org
.springframework
.transaction
.TransactionStatus
;
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
;
44 public class FloraGuianasActivator
{
45 private static final Logger logger
= Logger
.getLogger(FloraGuianasActivator
.class);
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();
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();
63 public static final UUID featureTreeUuid
= UUID
.fromString("2be99595-92fc-4f80-b9c4-b48d38505f5d");
66 static final UUID classificationUuid
= UUID
.fromString("5e3a1b07-2609-4597-bbda-7b02dfe8c2b3");
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";
72 private boolean h2ForCheck
= true;
73 static CHECK check
= CHECK
.IMPORT_WITHOUT_CHECK
;
75 static boolean doPrintKeys
= false;
78 static final boolean doTaxa
= true;
80 private boolean includeFgu1
= true;
81 private boolean includeFgu2
= false;
82 private boolean includeFgu3
= false;
83 private boolean includeFgu4
= false;
86 private boolean replaceStandardKeyTitles
= true;
88 private IIoObserver observer
= new LoggingIoObserver();
89 private Set
<IIoObserver
> observerList
= new HashSet
<IIoObserver
>();
92 private void doImport(ICdmDataSource cdmDestination
){
93 observerList
.add(observer
);
94 if (h2ForCheck
&& cdmDestination
.getDatabaseType().equals(CdmDestinations
.localH2().getDatabaseType())){
95 check
= CHECK
.CHECK_ONLY
;
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
);
110 markupConfig
.setSourceReference(getSourceReference(SOURCE_REFERENCE_TITLE
));
112 CdmDefaultImport
<MarkupImportConfigurator
> myImport
= new CdmDefaultImport
<MarkupImportConfigurator
>();
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() + ")...");
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() + ")...");
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() + ")...");
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() + ")...");
150 FeatureTree tree
= makeFeatureNode(myImport
.getCdmAppController().getTermService());
151 myImport
.getCdmAppController().getFeatureTreeService().saveOrUpdate(tree
);
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();
161 myImport
.getCdmAppController().commitTransaction(tx
);
166 private Reference
<?
> getSourceReference(String string
) {
167 Reference
<?
> result
= ReferenceFactory
.newGeneric();
168 result
.setTitleCache(string
);
172 private FeatureTree
makeFeatureNode(ITermService service
){
173 MarkupTransformer transformer
= new MarkupTransformer();
175 FeatureTree result
= FeatureTree
.NewInstance(featureTreeUuid
);
176 result
.setTitleCache(FEATURE_TREE_TITLE
);
177 FeatureNode root
= result
.getRoot();
180 newNode
= FeatureNode
.NewInstance(Feature
.DESCRIPTION());
181 root
.addChild(newNode
);
183 addFeataureNodesByStringList(descriptionFeatureList
, newNode
, transformer
, service
);
185 addFeataureNodesByStringList(generellDescriptionsUpToAnatomyList
, root
, transformer
, service
);
186 newNode
= FeatureNode
.NewInstance(Feature
.ANATOMY());
187 addFeataureNodesByStringList(anatomySubfeatureList
, newNode
, transformer
, service
);
189 newNode
= addFeataureNodesByStringList(generellDescriptionsFromAnatomyToPhytoChemoList
, root
, transformer
, service
);
190 addFeataureNodesByStringList(phytoChemoSubFeaturesList
, newNode
, transformer
, service
);
192 newNode
= addFeataureNodesByStringList(generellDescriptionsFromPhytoChemoList
, root
, transformer
, service
);
195 newNode
= FeatureNode
.NewInstance(Feature
.DISTRIBUTION());
196 root
.addChild(newNode
);
198 newNode
= FeatureNode
.NewInstance(Feature
.COMMON_NAME());
199 root
.addChild(newNode
);
201 newNode
= FeatureNode
.NewInstance(Feature
.PHENOLOGY());
202 root
.addChild(newNode
);
204 newNode
= FeatureNode
.NewInstance(Feature
.ECOLOGY());
205 root
.addChild(newNode
);
206 addFeataureNodesByStringList(habitatEcologyList
, root
, transformer
, service
);
208 newNode
= FeatureNode
.NewInstance(Feature
.USES());
209 root
.addChild(newNode
);
211 addFeataureNodesByStringList(chomosomesList
, root
, transformer
, service
);
213 newNode
= FeatureNode
.NewInstance(Feature
.CITATION());
214 root
.addChild(newNode
);
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
);
227 private static String
[] chomosomesList
= new String
[]{
232 private static String
[] habitatEcologyList
= new String
[]{
238 private static String
[] generellDescriptionsUpToAnatomyList
= new String
[]{
240 "Morphology and anatomy",
242 "Vegetative morphology and anatomy",
246 private static String
[] anatomySubfeatureList
= new String
[]{
251 private static String
[] generellDescriptionsFromAnatomyToPhytoChemoList
= new String
[]{
259 "Fruits and embryology",
261 "Chromosome numbers",
262 "Phytochemistry and Chemotaxonomy",
266 private static String
[] phytoChemoSubFeaturesList
= new String
[]{
268 "Iridoid glucosides",
270 "Storage products of seeds",
276 private static String
[] generellDescriptionsFromPhytoChemoList
= new String
[]{
285 private static String
[] descriptionFeatureList
= new String
[]{
297 "Flowering branchlets",
304 "extraxylary sclerenchyma",
305 "flower-bearing stems",
315 "Young inflorescences",
316 "Male inflorescences",
317 "Female inflorescences",
358 "Infructescences", //order not consistent (sometimes before "Flowers")
381 public FeatureNode
addFeataureNodesByStringList(String
[] featureStringList
, FeatureNode root
, IInputTransformer transformer
, ITermService termService
){
382 FeatureNode lastChild
= null;
384 for (String featureString
: featureStringList
){
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
);
394 } catch (UndefinedTransformerMethodException e
) {
395 logger
.error("getFeatureUuid is not implemented in transformer. Features could not be added");
405 public static void main(String
[] args
) {
406 FloraGuianasActivator me
= new FloraGuianasActivator();
407 me
.doImport(cdmDestination
);