Project

General

Profile

Download (4.27 KB) Statistics
| Branch: | Revision:
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 java.net.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.common.CdmDefaultExport;
26
import eu.etaxonomy.cdm.io.common.ExportResult;
27
import eu.etaxonomy.cdm.io.common.ExportResultType;
28
import eu.etaxonomy.cdm.io.outputmodel.OutputModelConfigurator;
29

    
30
/**
31
 * @author k.luther
32
 * @date 24.03.2017
33
 *
34
 */
35
public class OutputmodelExportActivator {
36
private static final ICdmDataSource cdmSource = CdmDestinations.cdm_local_caryophyllales_nepenthaceae();
37

    
38
    // Export:
39
    private static String exportFileName = "file://C://Users//k.luther//Documents//Caryophyllales//OutputModel";
40

    
41

    
42
    private static final Logger logger = Logger.getLogger(OutputmodelExportActivator.class);
43

    
44
    private ExportResult invokeExport(ICdmDataSource sourceParam, URI uri) {
45
//      String server = "localhost";
46
//      String database = "EDITimport";
47
//      String username = "edit";
48
//      sourceParam = CdmDataSource.NewMySqlInstance(server, database, username, AccountStore.readOrStorePassword(server, database, username, null));
49

    
50

    
51

    
52
        OutputModelConfigurator outputModelExportConfigurator;
53

    
54
        outputModelExportConfigurator = new OutputModelConfigurator(null);
55

    
56

    
57

    
58
        CdmDefaultExport<OutputModelConfigurator> outputModelExport =
59
            new CdmDefaultExport<OutputModelConfigurator>();
60

    
61

    
62
        // invoke export
63
        logger.debug("Invoking OutputModel export");
64
        ExportResult result = outputModelExport.invoke(outputModelExportConfigurator);
65
        return result;
66

    
67
    }
68
    public static String chooseFile(String[] args) {
69
        if(args == null) {
70
            return null;
71
        }
72
        for (String dest: args){
73
            if (dest.endsWith(".xml")){
74
                return args[0];
75
            }
76
        }
77
        return null;
78
    }
79

    
80

    
81

    
82

    
83
    private CdmApplicationController initDb(ICdmDataSource db) {
84

    
85
        // Init source DB
86
        CdmApplicationController appCtrInit = TestDatabase.initDb(db, DbSchemaValidation.VALIDATE, false);
87

    
88
        return appCtrInit;
89
    }
90

    
91

    
92
    // Load test data to DB
93
    private void loadTestData(CdmApplicationController appCtrInit) {
94
        TestDatabase.loadTestData("", appCtrInit);
95
    }
96

    
97

    
98
    /**
99
     * @param args
100
     */
101
    public static void main(String[] args) {
102

    
103
        OutputmodelExportActivator sc = new OutputmodelExportActivator();
104
        ICdmDataSource source = CdmDestinations.chooseDestination(args) != null ? CdmDestinations.chooseDestination(args) : cdmSource;
105
      //  String file = chooseFile(args);
106

    
107

    
108
        try {
109

    
110

    
111
            sc.initDb(source);  //does this make sense here (it starts the appControler even if it is not needed later
112

    
113
            ExportResult result = sc.invokeExport(source, null);
114
            if (result.getExportData().getType().equals(ExportResultType.MAP_BYTE_ARRAY)){
115
                Map<String, byte[]> map = (Map<String, byte[]>)result.getExportData();
116

    
117
                for (String key:map.keySet()){
118
                    byte[] data =map.get(key);
119
                    String fileEnding =".csv";
120
                    File myFile = new File(exportFileName+File.separator + key + fileEnding);
121
                    FileOutputStream stream = new FileOutputStream(myFile);
122
                    try {
123
                        stream.write(data);
124
                        stream.close();
125
                    } catch (IOException e) {
126
                        // TODO Auto-generated catch block
127
                        e.printStackTrace();
128
                    }
129

    
130

    
131
                }
132
            }
133

    
134

    
135
        } catch (FileNotFoundException e) {
136
            // TODO Auto-generated catch block
137
            e.printStackTrace();
138
        }
139

    
140
    }
141

    
142
}
(5-5/9)