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