1
|
/**
|
2
|
* Copyright (C) 2017 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
|
package eu.etaxonomy.cdm.app.caryophyllales;
|
10
|
|
11
|
import java.io.File;
|
12
|
import java.io.FileNotFoundException;
|
13
|
import java.io.FileOutputStream;
|
14
|
import java.io.IOException;
|
15
|
import eu.etaxonomy.cdm.common.URI;
|
16
|
import java.util.Map;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
|
20
|
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
|
21
|
import eu.etaxonomy.cdm.app.common.CdmDestinations;
|
22
|
import eu.etaxonomy.cdm.app.util.TestDatabase;
|
23
|
import eu.etaxonomy.cdm.database.DbSchemaValidation;
|
24
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
25
|
import eu.etaxonomy.cdm.io.cdmLight.CdmLightExportConfigurator;
|
26
|
import eu.etaxonomy.cdm.io.common.CdmDefaultExport;
|
27
|
import eu.etaxonomy.cdm.io.common.ExportResult;
|
28
|
import eu.etaxonomy.cdm.io.common.ExportResultType;
|
29
|
|
30
|
/**
|
31
|
* @author k.luther
|
32
|
* @since 24.03.2017
|
33
|
*/
|
34
|
public class CdmLightExportActivator {
|
35
|
private static final ICdmDataSource cdmSource = CdmDestinations.cdm_local_caryophyllales_nepenthaceae();
|
36
|
|
37
|
// Export:
|
38
|
private static String exportFileName = "file://C://Users//k.luther//Documents//Caryophyllales//OutputModel";
|
39
|
|
40
|
|
41
|
private static final Logger logger = Logger.getLogger(CdmLightExportActivator.class);
|
42
|
|
43
|
private ExportResult invokeExport(ICdmDataSource sourceParam, URI uri) {
|
44
|
// String server = "localhost";
|
45
|
// String database = "EDITimport";
|
46
|
// String username = "edit";
|
47
|
// sourceParam = CdmDataSource.NewMySqlInstance(server, database, username, AccountStore.readOrStorePassword(server, database, username, null));
|
48
|
|
49
|
CdmLightExportConfigurator cdmlightExportConfigurator = new CdmLightExportConfigurator(null);
|
50
|
|
51
|
CdmDefaultExport<CdmLightExportConfigurator> cdmLightExport = new CdmDefaultExport<>();
|
52
|
|
53
|
|
54
|
// invoke export
|
55
|
logger.debug("Invoking OutputModel export");
|
56
|
ExportResult result = cdmLightExport.invoke(cdmlightExportConfigurator);
|
57
|
return result;
|
58
|
|
59
|
}
|
60
|
public static String chooseFile(String[] args) {
|
61
|
if(args == null) {
|
62
|
return null;
|
63
|
}
|
64
|
for (String dest: args){
|
65
|
if (dest.endsWith(".xml")){
|
66
|
return args[0];
|
67
|
}
|
68
|
}
|
69
|
return null;
|
70
|
}
|
71
|
|
72
|
|
73
|
private CdmApplicationController initDb(ICdmDataSource db) {
|
74
|
|
75
|
// Init source DB
|
76
|
CdmApplicationController appCtrInit = TestDatabase.initDb(db, DbSchemaValidation.VALIDATE, false);
|
77
|
|
78
|
return appCtrInit;
|
79
|
}
|
80
|
|
81
|
|
82
|
// Load test data to DB
|
83
|
private void loadTestData(CdmApplicationController appCtrInit) {
|
84
|
TestDatabase.loadTestData("", appCtrInit);
|
85
|
}
|
86
|
|
87
|
|
88
|
/**
|
89
|
* @param args
|
90
|
*/
|
91
|
public static void main(String[] args) {
|
92
|
|
93
|
CdmLightExportActivator sc = new CdmLightExportActivator();
|
94
|
ICdmDataSource source = CdmDestinations.chooseDestination(args) != null ? CdmDestinations.chooseDestination(args) : cdmSource;
|
95
|
// String file = chooseFile(args);
|
96
|
|
97
|
try {
|
98
|
|
99
|
sc.initDb(source); //does this make sense here (it starts the appControler even if it is not needed later
|
100
|
|
101
|
ExportResult result = sc.invokeExport(source, null);
|
102
|
if (result.getExportData().getType().equals(ExportResultType.MAP_BYTE_ARRAY)){
|
103
|
Map<String, byte[]> map = (Map<String, byte[]>)result.getExportData();
|
104
|
|
105
|
for (String key:map.keySet()){
|
106
|
byte[] data =map.get(key);
|
107
|
String fileEnding =".csv";
|
108
|
File myFile = new File(exportFileName+File.separator + key + fileEnding);
|
109
|
FileOutputStream stream = new FileOutputStream(myFile);
|
110
|
try {
|
111
|
stream.write(data);
|
112
|
stream.close();
|
113
|
} catch (IOException e) {
|
114
|
// TODO Auto-generated catch block
|
115
|
e.printStackTrace();
|
116
|
}
|
117
|
}
|
118
|
}
|
119
|
} catch (FileNotFoundException e) {
|
120
|
// TODO Auto-generated catch block
|
121
|
e.printStackTrace();
|
122
|
}
|
123
|
}
|
124
|
}
|