Project

General

Profile

Download (10.7 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2008 EDIT
3
* European Distributed Institute of Taxonomy 
4
* http://www.e-taxonomy.eu
5
*/
6

    
7
package eu.etaxonomy.cdm.io.jaxb;
8

    
9
import java.io.File;
10
import java.net.URI;
11
import java.net.URISyntaxException;
12
import java.util.Collection;
13
import java.util.List;
14
import java.util.Map;
15

    
16
import org.apache.log4j.Logger;
17
import org.springframework.stereotype.Component;
18
import org.springframework.transaction.TransactionStatus;
19

    
20
import eu.etaxonomy.cdm.io.common.CdmIoBase;
21
import eu.etaxonomy.cdm.io.common.ICdmIO;
22
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
23
import eu.etaxonomy.cdm.io.common.MapWrapper;
24
import eu.etaxonomy.cdm.model.agent.Agent;
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
27
import eu.etaxonomy.cdm.model.common.LanguageStringBase;
28
import eu.etaxonomy.cdm.model.common.ReferencedEntityBase;
29
import eu.etaxonomy.cdm.model.common.TermVocabulary;
30
import eu.etaxonomy.cdm.model.common.VersionableEntity;
31
import eu.etaxonomy.cdm.model.description.DescriptionBase;
32
import eu.etaxonomy.cdm.model.media.Media;
33
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
34
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
35
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
36
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
37
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
38

    
39
/**
40
 * @author a.babadshanjan
41
 * @created 13.11.2008
42
 */
43
@Component
44
public class JaxbImport extends CdmIoBase<IImportConfigurator> implements ICdmIO<IImportConfigurator> {
45

    
46
	private static final Logger logger = Logger.getLogger(JaxbImport.class);
47
	private CdmDocumentBuilder cdmDocumentBuilder = null;
48
	
49
	
50

    
51
	
52
    /** Reads data from an XML file and stores them into a CDM DB.
53
     * 
54
     * @param config
55
     * @param stores (not used)
56
     */
57
	@Override
58
	protected boolean doInvoke(IImportConfigurator config,
59
			Map<String, MapWrapper<? extends CdmBase>> stores) {
60
		
61
		boolean success = true;
62
        URI uri = null;
63
		JaxbImportConfigurator jaxbImpConfig = (JaxbImportConfigurator)config;
64
//    	String dbname = jaxbImpConfig.getDestination().getDatabase();
65
    	
66
    	String urlFileName = (String)config.getSource();
67
		logger.debug("urlFileName: " + urlFileName);
68
    	try {
69
    		uri = new URI(urlFileName);
70
			logger.debug("uri: " + uri.toString());
71
    	} catch (URISyntaxException ex) {
72
			logger.error("File not found");
73
			return false;
74
    	}
75

    
76
		logger.info("Deserializing file " + urlFileName + " to DB " ); //+ dbname
77

    
78
		DataSet dataSet = new DataSet();
79
		
80
        // unmarshalling XML file
81
		
82
		try {
83
			cdmDocumentBuilder = new CdmDocumentBuilder();
84
			logger.info("Unmarshalling file: " + urlFileName);
85
			File file = new File(uri);
86
			logger.debug("Absolute path: " + file.getAbsolutePath());
87
			dataSet = cdmDocumentBuilder.unmarshal(dataSet, file);
88

    
89
		} catch (Exception e) {
90
			logger.error("Unmarshalling error");
91
			e.printStackTrace();
92
		} 
93
		
94
		// save data in DB
95
		logger.info("Saving data to DB: " ); //+ dbname
96
		success = saveData(jaxbImpConfig, dataSet);
97
		
98
		return success;
99
	}
100

    
101
	
102
	/**  Saves data in DB */
103
	private boolean saveData (JaxbImportConfigurator jaxbImpConfig, DataSet dataSet) {
104

    
105
		boolean ret = true;
106
		Collection<TaxonBase> taxonBases;
107
		List<? extends Agent> agents;
108
		List<DefinedTermBase> terms;
109
		List<ReferenceBase> references;
110
		List<TaxonNameBase> taxonomicNames;
111
		List<DescriptionBase> descriptions;
112
		List<ReferencedEntityBase> referencedEntities;
113
		List<SpecimenOrObservationBase> occurrences;
114
		List<VersionableEntity> featureData;
115
//		List<VersionableEntity> featureData;
116
		List<Media> media;
117
		List<LanguageStringBase> languageData;
118
		List<TermVocabulary<DefinedTermBase>> termVocabularies;
119
		List<HomotypicalGroup> homotypicalGroups;
120

    
121
		// Get an app controller that omits term loading
122
		// CdmApplicationController.getCdmAppController(boolean createNew, boolean omitTermLoading){
123
//		CdmApplicationController appCtr = jaxbImpConfig.getCdmAppController(false, true);
124
		//TransactionStatus txStatus = appCtr.startTransaction();
125
		TransactionStatus txStatus = null;
126

    
127
		// Have single transactions per service save call. Otherwise, getting
128
		// H2 HYT00 error (timeout locking table DEFINEDTERMBASE) when running from editor.
129

    
130
		// If data of a certain type, such as terms, are not saved here explicitly, 
131
		// then only those data of this type that are referenced by other objects are saved implicitly.
132
		// For example, if taxa are saved all other data referenced by those taxa, such as synonyms, 
133
		// are automatically saved as well.
134

    
135
		if ((jaxbImpConfig.isDoTermVocabularies() == true) 
136
				&& (termVocabularies = dataSet.getTermVocabularies()).size() > 0) {
137
			txStatus = startTransaction();
138
			ret &= saveTermVocabularies(termVocabularies);
139
			commitTransaction(txStatus);
140
		}
141
		
142
		if ((jaxbImpConfig.isDoTerms() == true)
143
				&& (terms = dataSet.getTerms()).size() > 0) {
144
			txStatus = startTransaction();
145
			ret &= saveTerms(terms);
146
			commitTransaction(txStatus);
147
		}
148
		
149
		// TODO: Have separate data save methods
150

    
151
		txStatus = startTransaction();
152
		try {
153
			if (jaxbImpConfig.isDoLanguageData() == true) {
154
				if ((languageData = dataSet.getLanguageData()).size() > 0) {
155
					logger.info("Language data: " + languageData.size());
156
					getTermService().saveLanguageDataAll(languageData);
157
				}
158
			}
159
		} catch (Exception ex) {
160
			logger.error("Error saving language data");
161
			ret = false;
162
		}
163
		commitTransaction(txStatus);
164

    
165
		
166
		txStatus = startTransaction();
167
		try {
168
			if (jaxbImpConfig.isDoAuthors() == true) {
169
				if ((agents = dataSet.getAgents()).size() > 0) {
170
					logger.info("Agents: " + agents.size());
171
					getAgentService().saveAgentAll(agents);
172
				}
173
			}
174
		} catch (Exception ex) {
175
			logger.error("Error saving agents");
176
			ret = false;
177
		}
178
		commitTransaction(txStatus);
179

    
180

    
181
		txStatus = startTransaction();
182
		try {
183
			if (jaxbImpConfig.getDoReferences() != IImportConfigurator.DO_REFERENCES.NONE) {
184
				if ((references = dataSet.getReferences()).size() > 0) {
185
					logger.info("References: " + references.size());
186
					getReferenceService().saveReferenceAll(references);
187
				}
188
			}
189
		} catch (Exception ex) {
190
			logger.error("Error saving references");
191
			ret = false;
192
		}
193
		commitTransaction(txStatus);
194

    
195

    
196
		txStatus = startTransaction();
197
		try {
198
			if (jaxbImpConfig.isDoTaxonNames() == true) {
199
				if ((taxonomicNames = dataSet.getTaxonomicNames()).size() > 0) {
200
					logger.info("Taxonomic names: " + taxonomicNames.size());
201
					getNameService().saveTaxonNameAll(taxonomicNames);
202
				}
203
			}
204
		} catch (Exception ex) {
205
			logger.error("Error saving taxon names");
206
			ret = false;
207
		}
208
		commitTransaction(txStatus);
209

    
210

    
211
		txStatus = startTransaction();
212
		try {
213
			if (jaxbImpConfig.isDoHomotypicalGroups() == true) {
214
				if ((homotypicalGroups = dataSet.getHomotypicalGroups()).size() > 0) {
215
					logger.info("Homotypical groups: " + homotypicalGroups.size());
216
					getNameService().saveAllHomotypicalGroups(homotypicalGroups);
217
				}
218
			}
219
		} catch (Exception ex) {
220
			logger.error("Error saving homotypical groups");
221
			ret = false;
222
		}
223
		commitTransaction(txStatus);
224

    
225

    
226
		txStatus = startTransaction();
227
		// Need to get the taxa and the synonyms here.
228
		try {
229
			if (jaxbImpConfig.isDoTaxa() == true) {
230
				if ((taxonBases = dataSet.getTaxonBases()).size() > 0) {
231
					logger.info("Taxon bases: " + taxonBases.size());
232
					getTaxonService().saveTaxonAll(taxonBases);
233
				}
234
			}
235
		} catch (Exception ex) {
236
			logger.error("Error saving taxa");
237
			ret = false;
238
		}
239
		commitTransaction(txStatus);
240

    
241

    
242
		txStatus = startTransaction();
243
		// NomenclaturalStatus, TypeDesignations
244
		try {
245
			if (jaxbImpConfig.isDoReferencedEntities() == true) {
246
				if ((referencedEntities = dataSet.getReferencedEntities()).size() > 0) {
247
					logger.info("Referenced entities: " + referencedEntities.size());
248
					getNameService().saveReferencedEntitiesAll(referencedEntities);
249
				}
250
			}
251
		} catch (Exception ex) {
252
			logger.error("Error saving referenced entities");
253
			ret = false;
254
		}
255
		commitTransaction(txStatus);
256

    
257

    
258
		// TODO: Implement dataSet.getDescriptions() and IDescriptionService.saveDescriptionAll()
259
//		if ((descriptions = dataSet.getDescriptions()) != null) {
260
//		logger.info("Saving " + descriptions.size() + " descriptions");
261
//		getDescriptionService().saveDescriptionAll(descriptions);
262
//		}
263

    
264
		txStatus = startTransaction();
265
		try {
266
			if (jaxbImpConfig.isDoOccurrence() == true) {
267
				if ((occurrences = dataSet.getOccurrences()).size() > 0) {
268
					logger.info("Occurrences: " + occurrences.size());
269
					getOccurrenceService().saveSpecimenOrObservationBaseAll(occurrences);
270
				}
271
			}
272
		} catch (Exception ex) {
273
			logger.error("Error saving occurrences");
274
			ret = false;
275
		}
276
		commitTransaction(txStatus);
277

    
278

    
279
		txStatus = startTransaction();
280
		try {
281
			if (jaxbImpConfig.isDoFeatureData() == true) {
282
				if ((featureData = dataSet.getFeatureData()).size() > 0) {
283
					logger.info("Feature data: " + featureData.size());
284
					getDescriptionService().saveFeatureDataAll(featureData);
285
				}
286
			}
287
		} catch (Exception ex) {
288
			logger.error("Error saving feature data");
289
			ret = false;
290
		}
291
		commitTransaction(txStatus);
292

    
293

    
294
		txStatus = startTransaction();
295
		try {
296
			if (jaxbImpConfig.isDoMedia() == true) {
297
				if ((media = dataSet.getMedia()).size() > 0) {
298
					logger.info("Media: " + media.size());
299
					getMediaService().saveMediaAll(media);
300
				}
301
			}
302
		} catch (Exception ex) {
303
			logger.error("Error saving media");
304
			ret = false;
305
		}
306
		commitTransaction(txStatus);
307

    
308
//		commitTransaction(txStatus);
309
		logger.info("All data saved");
310

    
311
		return ret;
312

    
313
	}
314
	
315
	
316
	private boolean saveTermVocabularies(
317
			List<TermVocabulary<DefinedTermBase>> termVocabularies) {
318

    
319
		boolean success = true;
320
		logger.info("Term vocabularies: " + termVocabularies.size());
321
		try {
322
			getTermService().saveTermVocabulariesAll(termVocabularies);
323
		} catch (Exception ex) {
324
			logger.error("Error saving term vocabularies");
325
			success = false;
326
		}
327
		return success;
328
	}
329

    
330
	private boolean saveTerms(List<DefinedTermBase> terms) {
331

    
332
		boolean success = true;
333
		logger.info("Terms: " + terms.size());
334
		try {
335
			getTermService().saveTermsAll(terms);
336
		} catch (Exception ex) {
337
			logger.error("Error saving terms");
338
			success = false;
339
		}
340
		return success;
341
	}
342

    
343
	
344
	@Override
345
	protected boolean doCheck(IImportConfigurator config) {
346
		boolean result = true;
347
		logger.warn("No check implemented for Jaxb import");
348
		return result;
349
	}
350
	
351

    
352
	@Override
353
	protected boolean isIgnore(IImportConfigurator config) {
354
		return false;
355
	}
356
}
(8-8/10)