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.wp6.palmae;
|
11
|
|
12
|
import java.io.File;
|
13
|
import java.net.MalformedURLException;
|
14
|
import java.net.URISyntaxException;
|
15
|
import java.net.URL;
|
16
|
|
17
|
import org.apache.log4j.Logger;
|
18
|
import org.springframework.transaction.TransactionStatus;
|
19
|
|
20
|
import eu.etaxonomy.cdm.app.common.CdmDestinations;
|
21
|
import eu.etaxonomy.cdm.database.DbSchemaValidation;
|
22
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
23
|
import eu.etaxonomy.cdm.io.common.CdmDefaultImport;
|
24
|
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
|
25
|
import eu.etaxonomy.cdm.io.common.IImportConfigurator.CHECK;
|
26
|
import eu.etaxonomy.cdm.io.common.ImportResult;
|
27
|
import eu.etaxonomy.cdm.io.taxonx.TaxonXImportConfigurator;
|
28
|
|
29
|
/**
|
30
|
* @author a.mueller
|
31
|
* @since 20.06.2008
|
32
|
* @version 1.0
|
33
|
*/
|
34
|
public class PalmaeTaxonXImportActivator {
|
35
|
private static final Logger logger = Logger.getLogger(PalmaeTaxonXImportActivator.class);
|
36
|
|
37
|
//database validation status (create, update, validate ...)
|
38
|
static DbSchemaValidation hbm2dll = DbSchemaValidation.UPDATE;
|
39
|
//static final String tcsSource = TcsSources.taxonX_local();
|
40
|
//static File source = TcsSources.taxonX_localDir();
|
41
|
static File source = new File("target/classes/taxonX");
|
42
|
static ICdmDataSource cdmDestination = CdmDestinations.localH2Palmae();
|
43
|
|
44
|
//check - import
|
45
|
static CHECK check = CHECK.IMPORT_WITHOUT_CHECK;
|
46
|
|
47
|
static boolean doDescriptions = true;
|
48
|
static boolean doNomenclature = true;
|
49
|
static boolean doMods = true;
|
50
|
|
51
|
|
52
|
public ImportResult runImport(){
|
53
|
ImportResult success = new ImportResult();
|
54
|
//make destination
|
55
|
ICdmDataSource destination = cdmDestination;
|
56
|
|
57
|
TaxonXImportConfigurator taxonXImportConfigurator = TaxonXImportConfigurator.NewInstance(null, destination);
|
58
|
// invoke import
|
59
|
CdmDefaultImport<IImportConfigurator> cdmImport = new CdmDefaultImport<IImportConfigurator>();
|
60
|
|
61
|
taxonXImportConfigurator.setDoFacts(doDescriptions);
|
62
|
taxonXImportConfigurator.setDoTypes(doNomenclature);
|
63
|
taxonXImportConfigurator.setDoMods(doMods);
|
64
|
|
65
|
taxonXImportConfigurator.setCheck(check);
|
66
|
taxonXImportConfigurator.setDbSchemaValidation(hbm2dll);
|
67
|
|
68
|
cdmImport.startController(taxonXImportConfigurator, destination);
|
69
|
|
70
|
//new Test().invoke(tcsImportConfigurator);
|
71
|
if (source.isDirectory()){
|
72
|
makeDirectory(cdmImport, taxonXImportConfigurator, source);
|
73
|
}else{
|
74
|
try {
|
75
|
success = importFile(cdmImport, taxonXImportConfigurator, source);
|
76
|
|
77
|
} catch (URISyntaxException e) {
|
78
|
success.addException(e);
|
79
|
e.printStackTrace();
|
80
|
}
|
81
|
}
|
82
|
return success;
|
83
|
}
|
84
|
|
85
|
private ImportResult makeDirectory(CdmDefaultImport<IImportConfigurator> cdmImport, TaxonXImportConfigurator taxonXImportConfigurator, File source){
|
86
|
ImportResult success = new ImportResult();
|
87
|
int count = 0;
|
88
|
for (File file : source.listFiles() ){
|
89
|
if (file.isFile()){
|
90
|
doCount(count++, 300, "Files");
|
91
|
try {
|
92
|
success = importFile(cdmImport, taxonXImportConfigurator, file);
|
93
|
} catch (URISyntaxException e) {
|
94
|
success = new ImportResult();
|
95
|
success.addException(e);
|
96
|
e.printStackTrace();
|
97
|
}
|
98
|
}else{
|
99
|
if (! file.getName().startsWith(".")){
|
100
|
makeDirectory(cdmImport, taxonXImportConfigurator, file);
|
101
|
}
|
102
|
}
|
103
|
}
|
104
|
return success;
|
105
|
}
|
106
|
|
107
|
private ImportResult importFile(CdmDefaultImport<IImportConfigurator> cdmImport,
|
108
|
TaxonXImportConfigurator config, File file) throws URISyntaxException{
|
109
|
ImportResult success ;
|
110
|
try{
|
111
|
URL url = file.toURI().toURL();
|
112
|
config.setSource(url.toURI());
|
113
|
String originalSourceId = file.getName();
|
114
|
originalSourceId =originalSourceId.replace(".xml", "");
|
115
|
logger.debug(originalSourceId);
|
116
|
config.setOriginalSourceId(originalSourceId);
|
117
|
TransactionStatus tx = cdmImport.getCdmAppController().startTransaction();
|
118
|
success = cdmImport.invoke(config);
|
119
|
cdmImport.getCdmAppController().commitTransaction(tx);
|
120
|
return success;
|
121
|
} catch (MalformedURLException e) {
|
122
|
logger.warn(e);
|
123
|
success = new ImportResult();
|
124
|
success.addException(e);
|
125
|
return success;
|
126
|
}
|
127
|
}
|
128
|
|
129
|
protected void doCount(int count, int modCount, String pluralString){
|
130
|
if ((count % modCount ) == 0 && count!= 0 ){ logger.info(pluralString + " handled: " + (count));}
|
131
|
}
|
132
|
|
133
|
/**
|
134
|
* @param args
|
135
|
*/
|
136
|
public static void main(String[] args) {
|
137
|
System.out.println("Start import from Source("+ source.toString() + ") ...");
|
138
|
|
139
|
PalmaeTaxonXImportActivator importer = new PalmaeTaxonXImportActivator();
|
140
|
|
141
|
importer.runImport();
|
142
|
|
143
|
|
144
|
System.out.println("End import from Source ("+ source.toString() + ")...");
|
145
|
}
|
146
|
|
147
|
|
148
|
}
|