latest DwcA improvements
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / app / eflora / CentralAfricaEricaceaeActivator.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.List;
14 import java.util.UUID;
15
16 import org.apache.log4j.Logger;
17 import org.springframework.transaction.TransactionStatus;
18
19 import eu.etaxonomy.cdm.api.application.CdmApplicationController;
20 import eu.etaxonomy.cdm.api.service.ITermService;
21 import eu.etaxonomy.cdm.app.common.CdmDestinations;
22 import eu.etaxonomy.cdm.common.DefaultProgressMonitor;
23 import eu.etaxonomy.cdm.common.IProgressMonitor;
24 import eu.etaxonomy.cdm.database.DbSchemaValidation;
25 import eu.etaxonomy.cdm.database.ICdmDataSource;
26 import eu.etaxonomy.cdm.database.update.CdmUpdater;
27 import eu.etaxonomy.cdm.io.common.CdmDefaultImport;
28 import eu.etaxonomy.cdm.io.common.IImportConfigurator.CHECK;
29 import eu.etaxonomy.cdm.io.common.mapping.IInputTransformer;
30 import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
31 import eu.etaxonomy.cdm.io.eflora.EfloraImportConfigurator;
32 import eu.etaxonomy.cdm.io.eflora.centralAfrica.ericaceae.CentralAfricaEricaceaeImportConfigurator;
33 import eu.etaxonomy.cdm.io.eflora.centralAfrica.ericaceae.CentralAfricaEricaceaeTransformer;
34 import eu.etaxonomy.cdm.model.agent.Person;
35 import eu.etaxonomy.cdm.model.common.Language;
36 import eu.etaxonomy.cdm.model.description.Feature;
37 import eu.etaxonomy.cdm.model.description.FeatureNode;
38 import eu.etaxonomy.cdm.model.description.FeatureTree;
39 import eu.etaxonomy.cdm.model.description.PolytomousKey;
40 import eu.etaxonomy.cdm.model.reference.Reference;
41 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
42
43 /**
44 * @author a.mueller
45 * @created 20.06.2008
46 * @version 1.0
47 */
48 public class CentralAfricaEricaceaeActivator {
49 private static final Logger logger = Logger.getLogger(CentralAfricaEricaceaeActivator.class);
50
51 //database validation status (create, update, validate ...)
52 static DbSchemaValidation hbm2dll = DbSchemaValidation.VALIDATE;
53 static final URI source = EfloraSources.ericacea_local();
54
55
56 // static final ICdmDataSource cdmDestination = CdmDestinations.cdm_test_andreasM3();
57 // static final ICdmDataSource cdmDestination = CdmDestinations.cdm_flora_central_africa_preview();
58 // static final ICdmDataSource cdmDestination = CdmDestinations.cdm_flora_central_africa_production();
59 // static final ICdmDataSource cdmDestination = CdmDestinations.localH2();
60 // static final ICdmDataSource cdmDestination = CdmDestinations.cdm_local_postgres_CdmTest();
61 static final ICdmDataSource cdmDestination = CdmDestinations.cdm_test_local_mysql();
62 // static final ICdmDataSource cdmDestination = CdmDestinations.cdm_test_jaxb();
63
64 //feature tree uuid
65 public static final UUID featureTreeUuid = UUID.fromString("051d35ee-22f1-42d8-be07-9e9bfec5bcf7");
66
67 public static UUID defaultLanguageUuid = Language.uuidFrench;
68
69 //classification
70 static final UUID classificationUuid = UUID.fromString("10e5efcc-6e13-4abc-ad42-e0b46e50cbe7");
71
72 //check - import
73 static final CHECK check = CHECK.IMPORT_WITHOUT_CHECK;
74
75 static boolean doPrintKeys = false;
76
77 //taxa
78 static final boolean doTaxa = true;
79 static final boolean doDeduplicate = true;
80
81 private boolean includeEricaceae = true;
82
83
84
85 private void doImport(ICdmDataSource cdmDestination){
86
87 CdmUpdater su = CdmUpdater.NewInstance();
88 IProgressMonitor monitor = DefaultProgressMonitor.NewInstance();
89
90 // try {
91 // su.updateToCurrentVersion(cdmDestination, monitor);
92 // } catch (Exception e) {
93 // e.printStackTrace();
94 // }
95 // if (true){
96 // return;
97 // }
98
99 //make Source
100 CentralAfricaEricaceaeImportConfigurator config= CentralAfricaEricaceaeImportConfigurator.NewInstance(source, cdmDestination);
101 config.setClassificationUuid(classificationUuid);
102 config.setDoTaxa(doTaxa);
103 config.setCheck(check);
104 config.setDefaultLanguageUuid(defaultLanguageUuid);
105 config.setDoPrintKeys(doPrintKeys);
106 config.setDbSchemaValidation(hbm2dll);
107
108 CdmDefaultImport<EfloraImportConfigurator> myImport = new CdmDefaultImport<EfloraImportConfigurator>();
109
110
111 //Ericaceae
112 if (includeEricaceae){
113 System.out.println("Start import from ("+ source.toString() + ") ...");
114 config.setSourceReference(getSourceReference(config.getSourceReferenceTitle()));
115 myImport.invoke(config);
116 System.out.println("End import from ("+ source.toString() + ")...");
117 }
118
119 FeatureTree tree = makeFeatureNode(myImport.getCdmAppController().getTermService());
120 myImport.getCdmAppController().getFeatureTreeService().saveOrUpdate(tree);
121
122 //check keys
123 if (doPrintKeys){
124 TransactionStatus tx = myImport.getCdmAppController().startTransaction();
125 List<PolytomousKey> keys = myImport.getCdmAppController().getPolytomousKeyService().list(PolytomousKey.class, null, null, null, null);
126 for(PolytomousKey key : keys){
127 key.print(System.out);
128 System.out.println();
129 }
130 myImport.getCdmAppController().commitTransaction(tx);
131 }
132
133 //deduplicate
134 if (doDeduplicate){
135 CdmApplicationController app = myImport.getCdmAppController();
136 int count = app.getAgentService().deduplicate(Person.class, null, null);
137 logger.warn("Deduplicated " + count + " persons.");
138 // count = app.getAgentService().deduplicate(Team.class, null, null);
139 // logger.warn("Deduplicated " + count + " teams.");
140 count = app.getReferenceService().deduplicate(Reference.class, null, null);
141 logger.warn("Deduplicated " + count + " references.");
142 }
143
144 }
145
146 private Reference getSourceReference(String string) {
147 Reference result = ReferenceFactory.newGeneric();
148 result.setTitleCache(string);
149 return result;
150 }
151
152 private FeatureTree makeFeatureNode(ITermService service){
153 CentralAfricaEricaceaeTransformer transformer = new CentralAfricaEricaceaeTransformer();
154
155 FeatureTree result = FeatureTree.NewInstance(featureTreeUuid);
156 result.setTitleCache("Central Africa Ericaceae Feature Tree");
157 FeatureNode root = result.getRoot();
158 FeatureNode newNode;
159
160 newNode = FeatureNode.NewInstance(Feature.DESCRIPTION());
161 root.addChild(newNode);
162
163 addFeataureNodesByStringList(descriptionFeatureList, newNode, transformer, service);
164
165 addFeataureNodesByStringList(generellDescriptionsList, root, transformer, service);
166
167
168 newNode = FeatureNode.NewInstance(Feature.DISTRIBUTION());
169 root.addChild(newNode);
170
171 newNode = FeatureNode.NewInstance(Feature.ECOLOGY());
172 root.addChild(newNode);
173 addFeataureNodesByStringList(habitatEcologyList, root, transformer, service);
174
175 newNode = FeatureNode.NewInstance(Feature.USES());
176 root.addChild(newNode);
177
178 addFeataureNodesByStringList(chomosomesList, root, transformer, service);
179
180 newNode = FeatureNode.NewInstance(Feature.COMMON_NAME());
181 root.addChild(newNode);
182
183 newNode = FeatureNode.NewInstance(Feature.CITATION());
184 root.addChild(newNode);
185
186 return result;
187 }
188
189 private static String [] chomosomesList = new String[]{
190 "Chromosomes",
191 };
192
193
194 private static String [] habitatEcologyList = new String[]{
195 "Habitat",
196 "Habitat & Ecology"
197 };
198
199
200 private static String [] generellDescriptionsList = new String[]{
201 "Fossils",
202 "Morphology and anatomy",
203 "Morphology",
204 "Vegetative morphology and anatomy",
205 "Flower morphology",
206 "Palynology",
207 "Pollination",
208 "Pollen morphology",
209 "Life cycle",
210 "Fruits and embryology",
211 "Dispersal",
212 "Wood anatomy",
213 "Leaf anatomy",
214 "Chromosome numbers",
215 "Phytochemistry and Chemotaxonomy",
216 "Phytochemistry",
217 "Taxonomy",
218 };
219
220 private static String [] descriptionFeatureList = new String[]{
221 "lifeform",
222 "Bark",
223 "Indumentum",
224 "endophytic body",
225 "flowering buds",
226 "Branchlets",
227 "Branches",
228 "Branch",
229 "Flowering branchlets",
230 "Trees",
231 "Twigs",
232 "stem",
233 "Stems",
234 "stem leaves",
235 "Leaves",
236 "flower-bearing stems",
237 "Petiole",
238 "Petiolules",
239 "Leaflets",
240 "Thyrsus",
241 "Thyrses",
242 "Inflorescences",
243 "Inflorescence",
244 "Young inflorescences",
245 "Bracts",
246 "Pedicels",
247 "flowering buds",
248 "scales",
249 "Buds",
250 "Flowers",
251 "Flower",
252 "Flowering",
253 "Stigma",
254 "perianth",
255 "Sepals",
256 "Sepal",
257 "Outer Sepals",
258 "Axillary",
259 "cymes",
260 "Calyx",
261 "Petal",
262 "Petals",
263 "perigone tube",
264 "Disc",
265 "corolla",
266 "Stamens",
267 "Staminodes",
268 "Ovary",
269 "Anthers",
270 "anther",
271 "Pistil",
272 "Pistillode",
273 "Ovules",
274 "androecium",
275 "gynoecium",
276 "Filaments",
277 "Style",
278 "annulus",
279 "female flowers",
280 "Male flowers",
281 "Female",
282 "Infructescences", //order not consistent (sometimes before "Flowers")
283 "Fruit",
284 "Fruits",
285 "fruiting axes",
286 "drupes",
287 "Arillode",
288 "seed",
289 "Seeds",
290 "Seedling",
291 "flower tube",
292 "nutlets",
293 "pollen",
294 "secondary xylem",
295 "chromosome number",
296
297 "figure",
298 "fig",
299 "figs",
300
301
302
303
304 };
305
306 public void addFeataureNodesByStringList(String[] featureStringList, FeatureNode root, IInputTransformer transformer, ITermService termService){
307 try {
308 for (String featureString : featureStringList){
309 UUID featureUuid;
310 featureUuid = transformer.getFeatureUuid(featureString);
311 Feature feature = (Feature)termService.find(featureUuid);
312 if (feature != null){
313 FeatureNode child = FeatureNode.NewInstance(feature);
314 root.addChild(child);
315 }
316 }
317 } catch (UndefinedTransformerMethodException e) {
318 logger.error("getFeatureUuid is not implemented in transformer. Features could not be added");
319 }
320 }
321
322
323
324 /**
325 * @param args
326 */
327 public static void main(String[] args) {
328 CentralAfricaEricaceaeActivator me = new CentralAfricaEricaceaeActivator();
329 me.doImport(cdmDestination);
330 }
331
332 }