View Javadoc

1   /**
2   * Copyright (C) 2007 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.io.pilotOutputHtml;
11  
12  import java.io.File;
13  import java.io.FileOutputStream;
14  import java.io.OutputStreamWriter;
15  import java.io.PrintWriter;
16  import java.util.HashSet;
17  import java.util.List;
18  import java.util.Set;
19  
20  import org.apache.log4j.Logger;
21  import org.springframework.stereotype.Component;
22  import org.springframework.transaction.TransactionStatus;
23  
24  import eu.etaxonomy.cdm.io.common.CdmExportBase;
25  import eu.etaxonomy.cdm.io.common.ICdmExport;
26  import eu.etaxonomy.cdm.io.common.IExportConfigurator;
27  import eu.etaxonomy.cdm.io.common.IImportConfigurator;
28  import eu.etaxonomy.cdm.io.sdd.SDDDataSet;
29  import eu.etaxonomy.cdm.model.agent.AgentBase;
30  import eu.etaxonomy.cdm.model.common.DefinedTermBase;
31  import eu.etaxonomy.cdm.model.common.RelationshipBase;
32  import eu.etaxonomy.cdm.model.name.TaxonNameBase;
33  import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
34  import eu.etaxonomy.cdm.model.reference.ReferenceBase;
35  import eu.etaxonomy.cdm.model.taxon.Synonym;
36  import eu.etaxonomy.cdm.model.taxon.Taxon;
37  import eu.etaxonomy.cdm.model.taxon.TaxonBase;
38  
39  /**
40   * @author h.fradin (from a.babadshanjan)
41   * @created 10.12.2008
42   * @versoin 1.0
43   */
44  @Component("pilotOutputDescriptionExporter")
45  public class PilotOutputDescriptionExporter extends CdmExportBase<PilotOutputExportConfigurator, PilotOutputExportState> implements ICdmExport<PilotOutputExportConfigurator, PilotOutputExportState> {
46  // public class JaxbExport extends CdmIoBase implements ICdmIoExport {
47  // TODO: public class JaxbExport extends CdmIoBase implements ICdmIO {
48  
49  	private static final Logger logger = Logger.getLogger(PilotOutputDescriptionExporter.class);
50  	private PilotOutputDocumentBuilder pilotOutputDocumentBuilder = null;
51  
52  	private String ioName = null;
53  
54  	
55  	/**
56  	 * 
57  	 */
58  	public PilotOutputDescriptionExporter() {
59  		super();
60  		this.ioName = this.getClass().getSimpleName();
61  	}
62  
63  	/** Retrieves data from a CDM DB and serializes them CDM to XML.
64  	 * Starts with root taxa and traverses the taxonomic tree to retrieve children taxa, synonyms and relationships.
65  	 * Taxa that are not part of the taxonomic tree are not found.
66  	 * 
67  	 * @param exImpConfig
68  	 * @param dbname
69  	 * @param filename
70  	 */
71  	@Override
72  	protected boolean doInvoke(PilotOutputExportState state){
73  //		protected boolean doInvoke(IExportConfigurator config,
74  //		Map<String, MapWrapper<? extends CdmBase>> stores) {
75  	
76  		PilotOutputExportConfigurator sddExpConfig = state.getConfig();
77  		String dbname = sddExpConfig.getSource().getName();
78      	String fileName = sddExpConfig.getDestinationNameString();
79  		logger.info("Serializing DB " + dbname + " to file " + fileName);
80  		logger.debug("DbSchemaValidation = " + sddExpConfig.getDbSchemaValidation());
81  
82  		TransactionStatus txStatus = startTransaction(true);
83  		SDDDataSet dataSet = new SDDDataSet();
84  		List<Taxon> taxa = null;
85  		List<DefinedTermBase> terms = null;
86  
87  		// get data from DB
88  
89  		try {
90  			logger.info("Retrieving data from DB");
91  
92  			retrieveData(sddExpConfig, dataSet);
93  
94  		} catch (Exception e) {
95  			logger.error("Error retrieving data");
96  			e.printStackTrace();
97  		}
98  
99  		logger.info("All data retrieved");
100 
101 		try {
102 			pilotOutputDocumentBuilder = new PilotOutputDocumentBuilder();
103 			File f = new File(fileName);
104 			// File f = new File(fileName);
105 			FileOutputStream fos = new FileOutputStream(f);
106 			PrintWriter writer = new PrintWriter(new OutputStreamWriter(fos, "UTF8"), true);
107 			pilotOutputDocumentBuilder.marshal(dataSet, fileName);
108 
109 			// TODO: Split into one file per data set member to see whether performance improves?
110 
111 			logger.info("XML file written");
112 			logger.info("Filename is: " + fileName);
113 
114 		} catch (Exception e) {
115 			logger.error("Marshalling error");
116 			e.printStackTrace();
117 		} 
118 		commitTransaction(txStatus);
119 		
120 		return true;
121 
122 	}
123 
124 
125 	private void retrieveData (IExportConfigurator config, SDDDataSet sddDataSet) {
126 
127 		PilotOutputExportConfigurator sddExpConfig = (PilotOutputExportConfigurator)config;
128 		final int MAX_ROWS = 50000;
129 		int numberOfRows = sddExpConfig.getMaxRows();
130 		// TODO: 
131 		//CdmApplicationController appCtr = config.getCdmAppController(false, true);
132 
133 		int agentRows = numberOfRows;
134 		int definedTermBaseRows = numberOfRows;
135 		int referenceBaseRows = numberOfRows;
136 		int taxonNameBaseRows = numberOfRows;
137 		int taxonBaseRows = numberOfRows;
138 		int relationshipRows = numberOfRows;
139 		int occurrencesRows = numberOfRows;
140 		int mediaRows = numberOfRows;
141 		int featureDataRows = numberOfRows;
142 		int languageDataRows = numberOfRows;
143 		int termVocabularyRows = numberOfRows;
144 		int homotypicalGroupRows = numberOfRows;
145 
146 		if (sddExpConfig.isDoTermVocabularies() == true) {
147 			if (termVocabularyRows == 0) { termVocabularyRows = MAX_ROWS; }
148 			logger.info("# TermVocabulary");
149 			sddDataSet.setTermVocabularies((List)getTermService().list(null,MAX_ROWS, 0,null,null));;
150 		}
151 
152 		if (sddExpConfig.isDoLanguageData() == true) {
153 			if (languageDataRows == 0) { languageDataRows = MAX_ROWS; }
154 			logger.info("# Representation, Language String");
155 			sddDataSet.setLanguageData(getTermService().getAllRepresentations(MAX_ROWS, 0));
156 			sddDataSet.addLanguageData(getTermService().getAllLanguageStrings(MAX_ROWS, 0));
157 		}
158 
159 		if (sddExpConfig.isDoTerms() == true) {
160 			if (definedTermBaseRows == 0) { definedTermBaseRows = getTermService().count(DefinedTermBase.class); }
161 			logger.info("# DefinedTermBase: " + definedTermBaseRows);
162 			sddDataSet.setTerms(getTermService().list(null,definedTermBaseRows, 0,null,null));
163 		}
164 
165 		if (sddExpConfig.isDoAuthors() == true) {
166 			if (agentRows == 0) { agentRows = getAgentService().count(AgentBase.class); }
167 			logger.info("# Agents: " + agentRows);
168 			//logger.info("    # Team: " + getAgentService().count(Team.class));
169 			sddDataSet.setAgents(getAgentService().list(null,agentRows, 0,null,null));
170 		}
171 
172 		if (sddExpConfig.getDoReferences() != IImportConfigurator.DO_REFERENCES.NONE) {
173 			if (referenceBaseRows == 0) { referenceBaseRows = getReferenceService().count(ReferenceBase.class); }
174 			logger.info("# ReferenceBase: " + referenceBaseRows);
175 			sddDataSet.setReferences(getReferenceService().list(null,referenceBaseRows, 0,null,null));
176 		}
177 
178 		if (sddExpConfig.isDoTaxonNames() == true) {
179 			if (taxonNameBaseRows == 0) { taxonNameBaseRows = getNameService().count(TaxonNameBase.class); }
180 			logger.info("# TaxonNameBase: " + taxonNameBaseRows);
181 			//logger.info("    # Taxon: " + getNameService().count(BotanicalName.class));
182 			sddDataSet.setTaxonomicNames(getNameService().list(null,taxonNameBaseRows, 0,null,null));
183 		}
184 
185 		if (sddExpConfig.isDoHomotypicalGroups() == true) {
186 			if (homotypicalGroupRows == 0) { homotypicalGroupRows = MAX_ROWS; }
187 			logger.info("# Homotypical Groups");
188 			sddDataSet.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows, 0));
189 		}
190 
191 		if (sddExpConfig.isDoTaxa() == true) {
192 			if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
193 			logger.info("# TaxonBase: " + taxonBaseRows);
194 //			dataSet.setTaxa(new ArrayList<Taxon>());
195 //			dataSet.setSynonyms(new ArrayList<Synonym>());
196 			List<TaxonBase> tb = getTaxonService().list(null,taxonBaseRows, 0,null,null);
197 			for (TaxonBase taxonBase : tb) {
198 				if (taxonBase instanceof Taxon) {
199 					sddDataSet.addTaxon((Taxon)taxonBase);
200 				} else if (taxonBase instanceof Synonym) {
201 					sddDataSet.addSynonym((Synonym)taxonBase);
202 				} else {
203 					logger.error("entry of wrong type: " + taxonBase.toString());
204 				}
205 			}
206 		}
207 
208 		// TODO: 
209 		// retrieve taxa and synonyms separately
210 		// need correct count for taxa and synonyms
211 //		if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
212 //		logger.info("# Synonym: " + taxonBaseRows);
213 //		dataSet.setSynonyms(new ArrayList<Synonym>());
214 //		dataSet.setSynonyms(getTaxonService().getAllSynonyms(taxonBaseRows, 0));
215 
216 		if (sddExpConfig.isDoRelTaxa() == true) {
217 			if (relationshipRows == 0) { relationshipRows = MAX_ROWS; }
218 			logger.info("# Relationships");
219 			List<RelationshipBase> relationList = getTaxonService().getAllRelationships(relationshipRows, 0);
220 			Set<RelationshipBase> relationSet = new HashSet<RelationshipBase>(relationList);
221 			sddDataSet.setRelationships(relationSet);
222 		}
223 
224 		if (sddExpConfig.isDoReferencedEntities() == true) {
225 			logger.info("# Referenced Entities");
226 			sddDataSet.setReferencedEntities(getNameService().getAllNomenclaturalStatus(MAX_ROWS, 0));
227 			sddDataSet.addReferencedEntities(getNameService().getAllTypeDesignations(MAX_ROWS, 0));
228 		}
229 
230 		if (sddExpConfig.isDoOccurrence() == true) {
231 			if (occurrencesRows == 0) { occurrencesRows = getOccurrenceService().count(SpecimenOrObservationBase.class); }
232 			logger.info("# SpecimenOrObservationBase: " + occurrencesRows);
233 			sddDataSet.setOccurrences(getOccurrenceService().list(null,occurrencesRows, 0,null,null));
234 		}
235 
236 		if (sddExpConfig.isDoMedia() == true) {
237 			if (mediaRows == 0) { mediaRows = MAX_ROWS; }
238 			logger.info("# Media");
239 			sddDataSet.setMedia(getMediaService().list(null,mediaRows, 0,null,null));
240 //			dataSet.addMedia(getMediaService().getAllMediaRepresentations(mediaRows, 0));
241 //			dataSet.addMedia(getMediaService().getAllMediaRepresentationParts(mediaRows, 0));
242 		}
243 
244 		if (sddExpConfig.isDoFeatureData() == true) {
245 			if (featureDataRows == 0) { featureDataRows = MAX_ROWS; }
246 			logger.info("# Feature Tree, Feature Node");
247 			sddDataSet.setFeatureData(getFeatureTreeService().getFeatureNodesAll());
248 			sddDataSet.addFeatureData(getFeatureTreeService().list(null,null,null,null,null));
249 		}
250 	}
251 
252 
253 	@Override
254 	protected boolean doCheck(PilotOutputExportState state) {
255 		boolean result = true;
256 		logger.warn("No check implemented for Jaxb export");
257 		return result;
258 	}
259 
260 
261 	@Override
262 	protected boolean isIgnore(PilotOutputExportState state) {
263 		return false;
264 	}
265 	
266 }