Project

General

Profile

Download (4.21 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

    
29
/**
30
 * @author k.luther
31
 * @date 24.03.2017
32
 *
33
 */
34
public class OutputmodelExportActivator {
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(OutputmodelExportActivator.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

    
50

    
51
        OutputModelConfigurator outputModelExportConfigurator;
52

    
53
        outputModelExportConfigurator = new OutputModelConfigurator(null);
54

    
55

    
56

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

    
60

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

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

    
79

    
80

    
81

    
82
    private CdmApplicationController initDb(ICdmDataSource db) {
83

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

    
87
        return appCtrInit;
88
    }
89

    
90

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

    
96

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

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

    
106

    
107
        try {
108

    
109

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

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

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

    
129

    
130
                }
131
            }
132

    
133

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

    
139
    }
140

    
141
}
(6-6/10)