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