Project

General

Profile

Download (4.32 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
 * @created 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, URI uri) {
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 (uri !=null && sourceParam != null){
61
			jaxbExportConfigurator = JaxbExportConfigurator.NewInstance(sourceParam, uri);
62
		}else if (sourceParam != null){			
63
			jaxbExportConfigurator = JaxbExportConfigurator.NewInstance(sourceParam, URI.create(exportFileName));
64
		} else if (uri !=null ){
65
			jaxbExportConfigurator = JaxbExportConfigurator.NewInstance(cdmSource, uri);
66
		} else{
67
			jaxbExportConfigurator = JaxbExportConfigurator.NewInstance(cdmSource, URI.create(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
		for (String dest: args){
84
			if (dest.endsWith(".xml")){
85
				return args[0];
86
			}
87
		}
88
		return null;
89
	}
90

    
91
	
92

    
93
	
94
	private CdmApplicationController initDb(ICdmDataSource db) {
95

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

    
99
		return appCtrInit;
100
	}
101

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

    
108

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

    
114
		JaxbExportActivator sc = new JaxbExportActivator();
115
		ICdmDataSource source = CdmDestinations.chooseDestination(args) != null ? CdmDestinations.chooseDestination(args) : cdmSource;
116
		String file = chooseFile(args);
117
		if (file == null){
118
			file = exportFileName;
119
		}
120
		URI uri = URI.create(file);
121
		try {
122
			File myFile = new File(uri);
123
			PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(myFile), "UTF8"), true);
124
			sc.initDb(source);  //does this make sense here (it starts the appControler even if it is not needed later
125
					
126
			sc.invokeExport(source, uri);
127
		} catch (UnsupportedEncodingException e) {
128
			// TODO Auto-generated catch block
129
			e.printStackTrace();
130
		} catch (FileNotFoundException e) {
131
			// TODO Auto-generated catch block
132
			e.printStackTrace();
133
		}
134
		
135
	}
136

    
137
}
(2-2/3)