Project

General

Profile

Download (12.4 KB) Statistics
| Branch: | Revision:
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.Arrays;
14
import java.util.HashSet;
15
import java.util.List;
16
import java.util.Set;
17
import java.util.UUID;
18

    
19
import org.apache.log4j.Logger;
20
import org.springframework.transaction.TransactionStatus;
21

    
22
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
23
import eu.etaxonomy.cdm.api.application.ICdmRepository;
24
import eu.etaxonomy.cdm.api.service.ITermService;
25
import eu.etaxonomy.cdm.database.DbSchemaValidation;
26
import eu.etaxonomy.cdm.database.ICdmDataSource;
27
import eu.etaxonomy.cdm.io.common.CdmDefaultImport;
28
import eu.etaxonomy.cdm.io.common.IImportConfigurator.CHECK;
29
import eu.etaxonomy.cdm.io.common.events.IIoObserver;
30
import eu.etaxonomy.cdm.io.common.events.LoggingIoObserver;
31
import eu.etaxonomy.cdm.io.common.mapping.IInputTransformer;
32
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
33
import eu.etaxonomy.cdm.io.eflora.floraMalesiana.FloraMalesianaTransformer;
34
import eu.etaxonomy.cdm.io.markup.MarkupImportConfigurator;
35
import eu.etaxonomy.cdm.model.agent.Person;
36
import eu.etaxonomy.cdm.model.agent.Team;
37
import eu.etaxonomy.cdm.model.description.Feature;
38
import eu.etaxonomy.cdm.model.description.PolytomousKey;
39
import eu.etaxonomy.cdm.model.reference.Reference;
40
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
41
import eu.etaxonomy.cdm.model.term.TermNode;
42
import eu.etaxonomy.cdm.model.term.TermTree;
43

    
44
/**
45
 * @author a.mueller
46
 * @since 20.06.2008
47
 */
48
public class NepenthesActivator extends EfloraActivatorBase {
49
	private static final Logger logger = Logger.getLogger(NepenthesActivator.class);
50

    
51
	//database validation status (create, update, validate ...)
52
	static DbSchemaValidation hbm2dll = DbSchemaValidation.CREATE;
53

    
54
//	static final ICdmDataSource cdmDestination = CdmDestinations.cdm_nepenthes_production();
55
	static final ICdmDataSource cdmDestination = CdmDestinations.localH2();
56
//	static final ICdmDataSource cdmDestination = CdmDestinations.cdm_test_local_mysql();
57
//	static final ICdmDataSource cdmDestination = CdmDestinations.cdm_test_local_mysql_test();
58

    
59
	//1775   /   16415
60
	//no syso, 5 uncovered, 2 of them epiphytic
61
//	private final boolean includeVol15 = includeBase;
62

    
63
    static final URI fmSource15 = EfloraSources.fm_15();
64

    
65
	private final boolean h2ForCheck = true;
66

    
67
	static final boolean reuseState = true;  //when running multiple imports
68

    
69

    
70
	//check - import
71
	static CHECK check = CHECK.IMPORT_WITHOUT_CHECK;
72

    
73
	static boolean doPrintKeys = false;
74
	static boolean doDeduplicate = false;
75

    
76
	//taxa
77
	static final boolean doTaxa = true;
78

    
79

    
80
    //feature tree uuid
81
    public static final UUID featureTreeUuid = UUID.fromString("ef2e2978-1ea4-44d2-a819-4e79b372b9b7");
82
    private static final String featureTreeTitle = "Nepenthaceae Feature Tree";
83

    
84
    //classification
85
    static final UUID classificationUuid = UUID.fromString("a245793a-a70f-4fcf-a626-dd4aa6d2aa1c");
86
    static final String classificationTitle = "Nepenthaceae";
87

    
88
    static final UUID specimenNotSeenMarkerTypeUuid = UUID.fromString("fb8ad1dd-ac70-4453-bc33-a9eeafd5e439");
89
    static final String specimenNotSeenMarkerTypeLabel = "Not seen for Flora Malesiana";
90

    
91
	private final boolean replaceStandardKeyTitles = false;
92
	private boolean ignoreLocalityClass = true;
93
	private boolean handleWriterManually = true;
94
	private boolean doExtensionForTaxonTitle = false;
95

    
96

    
97
	private final IIoObserver observer = new LoggingIoObserver();
98
	private final Set<IIoObserver> observerList = new HashSet<>();
99

    
100

    
101
	private void doImport(ICdmDataSource cdmDestination){
102
		observerList.add(observer);
103
		if (h2ForCheck && cdmDestination.getDatabaseType().equals(CdmDestinations.localH2().getDatabaseType())){
104
			check = CHECK.CHECK_ONLY;
105
		}
106

    
107
		//make Source
108
        URI source = null;
109

    
110
		MarkupImportConfigurator config= MarkupImportConfigurator.NewInstance(source, cdmDestination);
111
		config.setClassificationUuid(classificationUuid);
112
		config.setClassificationName(classificationTitle);
113
		config.setDoTaxa(doTaxa);
114
		config.setCheck(check);
115
		config.setDoPrintKeys(doPrintKeys);
116
		config.setDbSchemaValidation(hbm2dll);
117
		config.setObservers(observerList);
118
		config.setReplaceStandardKeyTitles(replaceStandardKeyTitles);
119
		config.setReuseExistingState(reuseState);
120
		config.setIgnoreLocalityClass(ignoreLocalityClass);
121
		config.setHandleWriterManually(handleWriterManually);
122
		config.setKnownCollections(getKnownCollections());
123
		config.setSpecimenNotSeenMarkerTypeUuid(specimenNotSeenMarkerTypeUuid);
124
		config.setSpecimenNotSeenMarkerTypeLabel(specimenNotSeenMarkerTypeLabel);
125
		config.setDoExtensionForTaxonTitle(doExtensionForTaxonTitle);
126

    
127
		config.setSourceReference(getSourceReference("Flora Malesiana - Vol. 15"));
128

    
129
		CdmDefaultImport<MarkupImportConfigurator> myImport = new CdmDefaultImport<>();
130

    
131

    
132
		//Vol15
133
		doSource(true, fmSource15, "Flora Malesiana - vol. 15", config, myImport);
134

    
135

    
136
		makeAutomatedFeatureTree(myImport.getCdmAppController(), config.getState(),
137
				featureTreeUuid, featureTreeTitle);
138

    
139
//		makeGeoService();
140

    
141
		TermTree<Feature> tree = makeFeatureNode(myImport.getCdmAppController().getTermService());
142
		myImport.getCdmAppController().getFeatureTreeService().saveOrUpdate(tree);
143

    
144
		//check keys
145
		if (doPrintKeys){
146
			TransactionStatus tx = myImport.getCdmAppController().startTransaction();
147
			List<PolytomousKey> keys = myImport.getCdmAppController().getPolytomousKeyService().list(PolytomousKey.class, null, null, null, null);
148
			for(PolytomousKey key : keys){
149
				key.print(System.out);
150
				System.out.println();
151
			}
152
			myImport.getCdmAppController().commitTransaction(tx);
153
		}
154

    
155
	      //deduplicate
156
        if (doDeduplicate){
157
            ICdmRepository app = myImport.getCdmAppController();
158
            if (app == null){
159
                app = CdmApplicationController.NewInstance(cdmDestination, hbm2dll, false);
160
            }
161
//            app.getAgentService().updateTitleCache(Team.class, null, null, null);
162
//            return;
163
          int count = app.getAgentService().deduplicate(Person.class, null, null);
164
          logger.warn("Deduplicated " + count + " persons.");
165
          count = app.getAgentService().deduplicate(Team.class, null, null);
166
//            logger.warn("Deduplicated " + count + " teams.");
167
          count = app.getReferenceService().deduplicate(Reference.class, null, null);
168
          logger.warn("Deduplicated " + count + " references.");
169
        }
170

    
171
	}
172

    
173
	/**
174
     * @return
175
     */
176
    private List<String> getKnownCollections() {
177
        List<String> result = Arrays.asList(new String[]
178
                {"Nippon Dental College","Nagoya","Sabah National Parks Herbarium","K-Wall"}) ;
179
        return result;
180
    }
181

    
182
    private void doSource(boolean doInclude, URI source, String sourceTitle, MarkupImportConfigurator markupConfig,
183
			CdmDefaultImport<MarkupImportConfigurator> myImport) {
184
		if (doInclude){
185
			System.out.println("\nStart import from ("+ source.toString() + ") ...");
186
			markupConfig.setSource(source);
187
			markupConfig.setSourceReference(getSourceReference(sourceTitle));
188
			myImport.invoke(markupConfig);
189
			System.out.println("End import from ("+ source.toString() + ")...");
190
		}
191
	}
192

    
193
	private Reference getSourceReference(String string) {
194
		Reference result = ReferenceFactory.newGeneric();
195
		result.setTitleCache(string, true);
196
		return result;
197
	}
198

    
199
	private TermTree<Feature> makeFeatureNode(ITermService service){
200
		FloraMalesianaTransformer transformer = new FloraMalesianaTransformer();
201

    
202
		TermTree<Feature> result = TermTree.NewFeatureInstance(UUID.randomUUID());
203
		result.setTitleCache("Flora Malesiana Nepenthaceae Simple Feature Tree", true);
204
		TermNode<Feature> root = result.getRoot();
205

    
206
		TermNode<Feature> newNode = root.addChild(Feature.CITATION());
207

    
208
		newNode = root.addChild(Feature.DESCRIPTION());
209

    
210
	    newNode = root.addChild(Feature.DISTRIBUTION());
211

    
212
        newNode = root.addChild(Feature.ECOLOGY());
213

    
214
        newNode = root.addChild(Feature.SPECIMEN());
215

    
216
        newNode = root.addChild(Feature.NOTES());
217

    
218
//		addFeatureNodesByStringList(descriptionFeatureList, newNode, transformer, service);
219

    
220
//		addFeatureNodesByStringList(generellDescriptionsUpToAnatomyList, root, transformer, service);
221
//		newNode = FeatureNode.NewInstance(Feature.ANATOMY());
222
//		addFeatureNodesByStringList(anatomySubfeatureList, newNode, transformer, service);
223
//
224
//		newNode = addFeatureNodesByStringList(generellDescriptionsFromAnatomyToPhytoChemoList, root, transformer, service);
225
//		addFeatureNodesByStringList(phytoChemoSubFeaturesList, newNode, transformer, service);
226
//
227
//		newNode = addFeatureNodesByStringList(generellDescriptionsFromPhytoChemoList, root, transformer, service);
228
//
229
//
230

    
231
//
232
//		newNode = FeatureNode.NewInstance(Feature.ECOLOGY());
233
//		root.addChild(newNode);
234
//		addFeatureNodesByStringList(habitatEcologyList, root, transformer, service);
235
//
236
//		newNode = FeatureNode.NewInstance(Feature.USES());
237
//		root.addChild(newNode);
238
//
239
//		addFeatureNodesByStringList(chomosomesList, root, transformer, service);
240

    
241

    
242
		return result;
243
	}
244

    
245
	private static String [] chomosomesList = new String[]{
246
		"Chromosomes",
247
	};
248

    
249

    
250
	private static String [] habitatEcologyList = new String[]{
251
		"Habitat",
252
		"Habitat & Ecology"
253
	};
254

    
255

    
256
	private static String [] generellDescriptionsUpToAnatomyList = new String[]{
257
		"Fossils",
258
		"Morphology and anatomy",
259
		"Morphology",
260
		"Vegetative morphology and anatomy",
261
	};
262

    
263

    
264
	private static String [] anatomySubfeatureList = new String[]{
265
		"Leaf anatomy",
266
		"Wood anatomy"
267
	};
268

    
269
	private static String [] generellDescriptionsFromAnatomyToPhytoChemoList = new String[]{
270
		"Flower morphology",
271
		"Palynology",
272
		"Pollination",
273
		"Pollen morphology",
274
		"embryology",
275
		"cytology",
276
		"Life cycle",
277
		"Fruits and embryology",
278
		"Dispersal",
279
		"Chromosome numbers",
280
		"Phytochemistry and Chemotaxonomy",
281
	};
282

    
283

    
284
	private static String [] phytoChemoSubFeaturesList = new String[]{
285
		"Alkaloids",
286
		"Iridoid glucosides",
287
		"Leaf phenolics",
288
		"Storage products of seeds",
289
		"Aluminium",
290
		"Chemotaxonomy",
291
	};
292

    
293

    
294
	private static String [] generellDescriptionsFromPhytoChemoList = new String[]{
295
		"Phytochemistry",
296
		"Taxonomy",
297
		"history",
298
		"cultivation",
299
		"Notes"
300
	};
301

    
302

    
303
	private static String [] descriptionFeatureList = new String[]{
304
		"lifeform",
305
		"Bark",
306
		//new
307
		"wood",
308
		"Indumentum",
309
		"endophytic body",
310
		"flowering buds",
311
		"Branchlets",
312
		"Branches",
313
		"Branch",
314
		"Flowering branchlets",
315
		"Trees",
316
		"Twigs",
317
		"stem",
318
		"Stems",
319
		"stem leaves",
320
		"Leaves",
321
		"flower-bearing stems",
322
		"Petiole",
323
		"Petiolules",
324
		"Leaflets",
325
		"Thyrsus",
326
		"Thyrses",
327
		"Inflorescences",
328
		"Inflorescence",
329
		"Young inflorescences",
330
		"Male inflorescences",
331
		"Female inflorescences",
332
		"Bracts",
333
		"Pedicels",
334
		"flowering buds",
335
		"scales",
336
		"Buds",
337
		"Flowers",
338
		"Flower",
339
		"Flowering",
340
		"Stigma",
341
		"perianth",
342
		"Sepals",
343
		"Sepal",
344
		"Outer Sepals",
345
		"Axillary",
346
		"cymes",
347
		"Calyx",
348
		"Petal",
349
		"Petals",
350
		"perigone",
351
		"perigone lobes",
352
		"perigone tube",
353
		"Disc",
354
		"corolla",
355
		"Stamens",
356
		"Staminodes",
357
		"Ovary",
358
		"Anthers",
359
		"anther",
360
		"Pistil",
361
		"Pistillode",
362
		"Ovules",
363
		"androecium",
364
		"gynoecium",
365
		"Filaments",
366
		"Style",
367
		"annulus",
368
		"female flowers",
369
		"Male flowers",
370
		"Female",
371
		"Infructescences",    //order not consistent (sometimes before "Flowers")
372
		"Fruit",
373
		"Fruits",
374
		"fruiting axes",
375
		"drupes",
376
		"Arillode",
377
		"seed",
378
		"Seeds",
379
		"Seedling",
380
		"flower tube",
381
		"nutlets",
382
		"pollen",
383
		"secondary xylem",
384
		"chromosome number",
385

    
386
		"figure",
387
		"fig",
388
		"figs",
389

    
390

    
391

    
392
	};
393

    
394
	public TermNode<Feature> addFeatureNodesByStringList(String[] featureStringList,
395
	            TermNode<Feature> root, IInputTransformer transformer, ITermService termService){
396

    
397
	    TermNode<Feature> lastChild = null;
398
		try {
399
			for (String featureString : featureStringList){
400
				UUID featureUuid;
401
				featureUuid = transformer.getFeatureUuid(featureString);
402
				Feature feature = (Feature)termService.find(featureUuid);
403
				if (feature != null){
404
					root.addChild(feature);
405
				}
406
			}
407

    
408
		} catch (UndefinedTransformerMethodException e) {
409
			logger.error("getFeatureUuid is not implemented in transformer. Features could not be added");
410
		}
411
		return lastChild;
412
	}
413

    
414

    
415

    
416
	/**
417
	 * @param args
418
	 */
419
	public static void main(String[] args) {
420
		NepenthesActivator me = new NepenthesActivator();
421
		me.doImport(cdmDestination);
422
		System.exit(0);
423
	}
424

    
425
}
(15-15/15)