Project

General

Profile

Download (4.33 KB) Statistics
| Branch: | Revision:
1
/**
2
 * Copyright (C) 2008 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.jaxb;
11

    
12
import java.io.File;
13
import java.io.FileNotFoundException;
14
import java.io.FileOutputStream;
15
import java.io.OutputStreamWriter;
16
import java.io.PrintWriter;
17
import java.io.UnsupportedEncodingException;
18
import java.net.URI;
19

    
20
import org.apache.log4j.Logger;
21

    
22
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
23
import eu.etaxonomy.cdm.app.common.CdmDestinations;
24
import eu.etaxonomy.cdm.app.util.TestDatabase;
25
import eu.etaxonomy.cdm.database.DbSchemaValidation;
26
import eu.etaxonomy.cdm.database.ICdmDataSource;
27
import eu.etaxonomy.cdm.io.common.CdmDefaultExport;
28
import eu.etaxonomy.cdm.io.jaxb.JaxbExportConfigurator;
29

    
30
/**
31
 * @author a.babadshanjan
32
 * @since 25.09.2008
33
 */
34
public class JaxbExportActivator {
35

    
36
	/* SerializeFrom DB **/
37
//	private static final ICdmDataSource cdmSource = CdmDestinations.localH2();
38
	private static final ICdmDataSource cdmSource = CdmDestinations.cdm_production_flora_deutschland();
39

    
40
	// Export:
41
	private static String exportFileName = "file:/F:/data/redlist/standardliste/standardliste_jaxb.xml";
42

    
43
	/** NUMBER_ROWS_TO_RETRIEVE = 0 is the default case to retrieve all rows.
44
	 * For testing purposes: If NUMBER_ROWS_TO_RETRIEVE >0 then retrieve
45
	 *  as many rows as specified for agents, references, etc.
46
	 *  Only root taxa and no synonyms and relationships are retrieved. */
47
	private static final int NUMBER_ROWS_TO_RETRIEVE = 0;
48

    
49
	private static final Logger logger = Logger.getLogger(JaxbImportActivator.class);
50

    
51
	private void invokeExport(ICdmDataSource sourceParam, File file) {
52
//		String server = "localhost";
53
//		String database = "EDITimport";
54
//		String username = "edit";
55
//		sourceParam = CdmDataSource.NewMySqlInstance(server, database, username, AccountStore.readOrStorePassword(server, database, username, null));
56

    
57

    
58

    
59
		JaxbExportConfigurator jaxbExportConfigurator;
60
		if (file !=null && sourceParam != null){
61
			jaxbExportConfigurator = JaxbExportConfigurator.NewInstance(sourceParam, file);
62
		}else if (sourceParam != null){
63
			jaxbExportConfigurator = JaxbExportConfigurator.NewInstance(sourceParam, new File(exportFileName));
64
		} else if (file !=null ){
65
			jaxbExportConfigurator = JaxbExportConfigurator.NewInstance(cdmSource, file);
66
		} else{
67
			jaxbExportConfigurator = JaxbExportConfigurator.NewInstance(cdmSource, new File(exportFileName));
68
		}
69

    
70

    
71
		CdmDefaultExport<JaxbExportConfigurator> jaxbExport =
72
			new CdmDefaultExport<JaxbExportConfigurator>();
73

    
74

    
75
		// invoke export
76
		logger.debug("Invoking Jaxb export");
77
		jaxbExport.invoke(jaxbExportConfigurator);
78

    
79
	}
80
	public static String chooseFile(String[] args) {
81
		if(args == null) {
82
            return null;
83
        }
84
		for (String dest: args){
85
			if (dest.endsWith(".xml")){
86
				return args[0];
87
			}
88
		}
89
		return null;
90
	}
91

    
92

    
93

    
94

    
95
	private CdmApplicationController initDb(ICdmDataSource db) {
96

    
97
		// Init source DB
98
		CdmApplicationController appCtrInit = TestDatabase.initDb(db, DbSchemaValidation.VALIDATE, false);
99

    
100
		return appCtrInit;
101
	}
102

    
103

    
104
	// Load test data to DB
105
	private void loadTestData(CdmApplicationController appCtrInit) {
106
		TestDatabase.loadTestData("", appCtrInit);
107
	}
108

    
109

    
110
	/**
111
	 * @param args
112
	 */
113
	public static void main(String[] args) {
114

    
115
		JaxbExportActivator sc = new JaxbExportActivator();
116
		ICdmDataSource source = CdmDestinations.chooseDestination(args) != null ? CdmDestinations.chooseDestination(args) : cdmSource;
117
		String file = chooseFile(args);
118
		if (file == null){
119
			file = exportFileName;
120
		}
121
		URI uri = URI.create(file);
122
		try {
123
			File myFile = new File(uri);
124
			PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(myFile), "UTF8"), true);
125
			sc.initDb(source);  //does this make sense here (it starts the appControler even if it is not needed later
126

    
127
			sc.invokeExport(source, new File(file));
128
		} catch (UnsupportedEncodingException e) {
129
			// TODO Auto-generated catch block
130
			e.printStackTrace();
131
		} catch (FileNotFoundException e) {
132
			// TODO Auto-generated catch block
133
			e.printStackTrace();
134
		}
135

    
136
	}
137

    
138
}
(2-2/3)