Project

General

Profile

Download (12 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
 * 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.jaxb;
11

    
12
import java.io.File;
13
import java.net.URI;
14
import java.net.URISyntaxException;
15
import java.util.Collection;
16
import java.util.List;
17

    
18
import org.apache.log4j.Logger;
19
import org.springframework.stereotype.Component;
20
import org.springframework.transaction.TransactionStatus;
21

    
22
import eu.etaxonomy.cdm.config.Configuration;
23
import eu.etaxonomy.cdm.io.common.CdmImportBase;
24
import eu.etaxonomy.cdm.io.common.ICdmIO;
25
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
26
import eu.etaxonomy.cdm.model.agent.AgentBase;
27
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
28
import eu.etaxonomy.cdm.model.common.LanguageStringBase;
29
import eu.etaxonomy.cdm.model.common.TermVocabulary;
30
import eu.etaxonomy.cdm.model.common.User;
31
import eu.etaxonomy.cdm.model.description.DescriptionBase;
32
import eu.etaxonomy.cdm.model.description.FeatureNode;
33
import eu.etaxonomy.cdm.model.description.FeatureTree;
34
import eu.etaxonomy.cdm.model.media.Media;
35
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
36
import eu.etaxonomy.cdm.model.name.TaxonName;
37
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
38
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
39
import eu.etaxonomy.cdm.model.reference.Reference;
40
import eu.etaxonomy.cdm.model.taxon.Classification;
41
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
42
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
43

    
44
/**
45
 * @author a.babadshanjan
46
 * @since 13.11.2008
47
 */
48
@Component
49
public class JaxbImport
50
            extends CdmImportBase<JaxbImportConfigurator, JaxbImportState>
51
            implements ICdmIO<JaxbImportState> {
52

    
53
    private static final long serialVersionUID = -96388140688227297L;
54
    private static final Logger logger = Logger.getLogger(JaxbImport.class);
55

    
56
	private CdmDocumentBuilder cdmDocumentBuilder = null;
57

    
58

    
59
	/** Reads data from an XML file and stores them into a CDM DB.
60
     *
61
     * @param config
62
     * @param stores (not used)
63
     */
64

    
65
	@Override
66
	protected void doInvoke(JaxbImportState state) {
67

    
68
		JaxbImportConfigurator jaxbImpConfig = state.getConfig();
69

    
70
		URI uri;
71
    	String urlFileName = jaxbImpConfig.getSource().toString();
72
		try {
73
    		uri = new URI(urlFileName);
74
		} catch (URISyntaxException ex) {
75
			logger.error("File not found");
76
			state.setUnsuccessfull();
77
			return;
78
    	}
79

    
80
		logger.info("Deserializing " + urlFileName + " to DB " ); //+ dbname
81

    
82
		DataSet dataSet = new DataSet();
83

    
84
        // unmarshalling XML file
85
		try {
86
			cdmDocumentBuilder = new CdmDocumentBuilder();
87
			logger.info("Unmarshalling " + urlFileName);
88
			File file = new File(uri);
89
			logger.debug("Absolute path: " + file.getAbsolutePath());
90
			dataSet = cdmDocumentBuilder.unmarshal(DataSet.class, file);
91

    
92
		} catch (Exception e) {
93
			logger.error("Unmarshalling error");
94
			e.printStackTrace();
95
		}
96

    
97
		// save data in DB
98
		logger.info("Saving data to DB... "); //+ dbname
99

    
100
		saveData(state, dataSet);
101

    
102
		return;
103
	}
104

    
105

    
106
	/**  Saves data in DB */
107
	private void saveData (JaxbImportState state, DataSet dataSet) {
108
		JaxbImportConfigurator jaxbImpConfig = state.getConfig();
109

    
110
		boolean success = true;
111
		Collection<TaxonBase> taxonBases;
112
		List<? extends AgentBase> agents;
113
		List<DefinedTermBase> terms;
114
		List<User> users;
115
		List<Reference> references;
116
		List<TaxonName> taxonomicNames;
117
		List<DescriptionBase> descriptions;
118
		List<TypeDesignationBase> typeDesignations;
119
		List<SpecimenOrObservationBase> occurrences;
120
		List<FeatureTree> featureTrees;
121
		List<FeatureNode> featureNodes;
122
		List<Media> media;
123
		List<LanguageStringBase> languageData;
124
		List<TermVocabulary<DefinedTermBase>> termVocabularies;
125
		List<HomotypicalGroup> homotypicalGroups;
126

    
127
		TransactionStatus txStatus = startTransaction();
128
		//TransactionStatus txStatus = null;
129

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

    
133
		try {
134
			if (jaxbImpConfig.isDoUser() == true) {
135
				/*
136
				 * this is a crucial call, otherwise the password will not be set correctly
137
				 * and the whole authentication will not work
138
				 *
139
				 *  a bit preliminary, should be used only if the complete database is replaced
140
				 */
141
				authenticate(Configuration.adminLogin, Configuration.adminPassword);
142

    
143
				logger.info("Users: " + (users = dataSet.getUsers()).size());
144
				for (User user : users) {
145

    
146
					List<User> usersList = getUserService().listByUsername(user.getUsername(),null, null, null, null, null, null);
147
					if (usersList.isEmpty()){
148
						getUserService().save(user);
149
					}else{
150
//						User existingUser = usersList.get(0);
151
//						user.setId(existingUser.getId());
152
//						getUserService().merge(user);
153
						//merging does not yet work because of #4102
154

    
155
					}
156
				}
157
			}
158
		} catch (Exception ex) {
159
			logger.error("Error saving users");
160
			ex.printStackTrace();
161
			success = false;
162
		}
163

    
164
		if ((jaxbImpConfig.isDoTerms() == true)
165
				&& (terms = dataSet.getTerms()).size() > 0) {
166
			//txStatus = startTransaction();
167
			success &= saveTerms(terms);
168

    
169
			//commitTransaction(txStatus);
170
		}
171
		if ((jaxbImpConfig.isDoTermVocabularies() == true)
172
				&& (termVocabularies = dataSet.getTermVocabularies()).size() > 0) {
173
			//txStatus = startTransaction();
174
			success &= saveTermVocabularies(termVocabularies);
175

    
176
		}
177

    
178
		// TODO: Have separate data save methods
179

    
180
//		txStatus = startTransaction();
181
//		try {
182
//			if (jaxbImpConfig.isDoLanguageData() == true) {
183
//				if ((languageData = dataSet.getLanguageData()).size() > 0) {
184
//					logger.info("Language data: " + languageData.size());
185
//					getTermService().saveLanguageDataAll(languageData);
186
//				}
187
//			}
188
//		} catch (Exception ex) {
189
//			logger.error("Error saving language data");
190
//			ret = false;
191
//		}
192
//		commitTransaction(txStatus);
193

    
194

    
195
		//txStatus = startTransaction();
196
		try {
197
			if (jaxbImpConfig.isDoAuthors() == true) {
198
				if ((agents = dataSet.getAgents()).size() > 0) {
199
					logger.info("Agents: " + agents.size());
200
					getAgentService().saveOrUpdate((Collection)agents);
201
				}
202
			}
203
		} catch (Exception ex) {
204
			logger.error("Error saving agents");
205
			success = false;
206
		}
207
		//commitTransaction(txStatus);
208

    
209

    
210
		//txStatus = startTransaction();
211
		try {
212
			if (jaxbImpConfig.getDoReferences() != IImportConfigurator.DO_REFERENCES.NONE) {
213
				if ((references = dataSet.getReferences()).size() > 0) {
214
					logger.info("References: " + references.size());
215
					getReferenceService().saveOrUpdate(references);
216
					logger.info("ready...");
217
				}
218
			}
219
		} catch (Exception ex) {
220
			ex.printStackTrace();
221
			logger.error("Error saving references");
222
			success = false;
223
		}
224
		//commitTransaction(txStatus);
225

    
226

    
227
		//txStatus = startTransaction();
228
		try {
229
			if (jaxbImpConfig.isDoTaxonNames() == true) {
230
				if ((taxonomicNames = dataSet.getTaxonomicNames()).size() > 0) {
231
					logger.info("Taxonomic names: " + taxonomicNames.size());
232
					getNameService().saveOrUpdate(taxonomicNames);
233
				}
234
			}
235
		} catch (Exception ex) {
236
			logger.error("Error saving taxon names");
237
			success = false;
238
		}
239
		//commitTransaction(txStatus);
240

    
241

    
242
		//txStatus = startTransaction();
243
		try {
244
			if (jaxbImpConfig.isDoHomotypicalGroups() == true) {
245
				if ((homotypicalGroups = dataSet.getHomotypicalGroups()).size() > 0) {
246
					logger.info("Homotypical groups: " + homotypicalGroups.size());
247
					getNameService().saveAllHomotypicalGroups(homotypicalGroups);
248

    
249
				}
250
			}
251
		} catch (Exception ex) {
252
			logger.error("Error saving homotypical groups");
253
			success = false;
254
		}
255
		//commitTransaction(txStatus);
256

    
257

    
258
		//txStatus = startTransaction();
259
		// Need to get the taxa and the synonyms here.
260
		try {
261
			if (jaxbImpConfig.isDoTaxa() == true) {
262
				if ( dataSet.getTaxonBases().size() > 0) {
263
					List taxa = dataSet.getTaxonBases();
264
					dataSet.getTaxonBases();
265
					logger.info("Taxon bases: " + taxa.size());
266
					taxa.iterator();
267
					getTaxonService().saveOrUpdate(taxa);
268
					/*while (taxBases.hasNext()){
269
						getTaxonService().save(taxBases.next());
270
					}*/
271
					//getTaxonService().saveTaxonAll(taxonBases);
272
				}
273
			}
274
		} catch (Exception ex) {
275
			logger.error("Error saving taxa");
276
			ex.printStackTrace();
277
			success = false;
278
		}
279
		//commitTransaction(txStatus);
280

    
281

    
282
		//txStatus = startTransaction();
283
		// NomenclaturalStatus, TypeDesignations
284
		try {
285
			if (jaxbImpConfig.isDoTypeDesignations() == true) {
286
				if ((typeDesignations = dataSet.getTypeDesignations()).size() > 0) {
287
					logger.info("Type Designations: " + typeDesignations.size());
288
					getNameService().saveTypeDesignationAll(typeDesignations);
289
				}
290
			}
291
		} catch (Exception ex) {
292
			logger.error("Error saving type designations");
293
			success = false;
294
		}
295
		//commitTransaction(txStatus);
296

    
297

    
298
		// TODO: Implement dataSet.getDescriptions() and IDescriptionService.saveDescriptionAll()
299
//		if ((descriptions = dataSet.getDescriptions()) != null) {
300
//		logger.info("Saving " + descriptions.size() + " descriptions");
301
//		getDescriptionService().saveDescriptionAll(descriptions);
302
//		}
303

    
304
		//txStatus = startTransaction();
305
		try {
306
			if (jaxbImpConfig.isDoOccurrence() == true) {
307
				if ((occurrences = dataSet.getOccurrences()).size() > 0) {
308
					logger.info("Occurrences: " + occurrences.size());
309
					getOccurrenceService().saveOrUpdate(occurrences);
310
				}
311
			}
312
		} catch (Exception ex) {
313
			logger.error("Error saving occurrences");
314
			success = false;
315
		}
316
		//commitTransaction(txStatus);
317

    
318

    
319
		//txStatus = startTransaction();
320
		try {
321
			if (jaxbImpConfig.isDoFeatureData() == true) {
322
				if ((featureTrees = dataSet.getFeatureTrees()).size() > 0) {
323
					logger.info("Feature data: " + featureTrees.size());
324
					getFeatureTreeService().saveOrUpdate(featureTrees);
325
				}
326
			}
327
		} catch (Exception ex) {
328
			logger.error("Error saving feature data");
329
			success = false;
330
		}
331
		//commitTransaction(txStatus);
332

    
333

    
334
		//txStatus = startTransaction();
335
		try {
336
			if (jaxbImpConfig.isDoMedia() == true) {
337
				if ((media = dataSet.getMedia()).size() > 0) {
338
					logger.info("Media: " + media.size());
339
					getMediaService().saveOrUpdate(media);
340
				}
341
			}
342
		} catch (Exception ex) {
343
			logger.error("Error saving media");
344
			success = false;
345
		}
346

    
347
		if (jaxbImpConfig.isDoClassificationData() == true) {
348
			logger.info("# Classification");
349

    
350
			Collection<TaxonNode> nodes = dataSet.getTaxonNodes();
351
			Collection<Classification> classifications = dataSet.getClassifications();
352
			getClassificationService().saveTaxonNodeAll(nodes);
353
			for (Classification tree: classifications){
354
				getClassificationService().saveOrUpdate(tree);
355
			}
356
		}
357

    
358

    
359
		commitTransaction(txStatus);
360
		logger.info("All data saved");
361
		if (!success){
362
			state.setUnsuccessfull();
363
		}
364

    
365
		return;
366

    
367
	}
368

    
369

    
370
	private boolean saveTermVocabularies(
371
			List<TermVocabulary<DefinedTermBase>> termVocabularies) {
372

    
373
		boolean success = true;
374
		logger.info("Term vocabularies: " + termVocabularies.size());
375
		try {
376
			getVocabularyService().saveOrUpdate((List)termVocabularies);
377
		} catch (Exception ex) {
378
			logger.error("Error saving term vocabularies");
379
			success = false;
380
		}
381
		return success;
382
	}
383

    
384
	private boolean saveTerms(List<DefinedTermBase> terms) {
385

    
386
		boolean success = true;
387
		logger.info("Terms: " + terms.size());
388
		try {
389
			getTermService().save(terms);
390
		} catch (Exception ex) {
391
			logger.error("Error saving terms");
392
			ex.printStackTrace();
393
			success = false;
394
		}
395
		return success;
396
	}
397

    
398

    
399
	@Override
400
	protected boolean doCheck(JaxbImportState state) {
401
		boolean result = true;
402
		logger.warn("No validation implemented for Jaxb import");
403
		return result;
404
	}
405

    
406

    
407
	@Override
408
	protected boolean isIgnore(JaxbImportState state) {
409
		return false;
410
	}
411
}
(12-12/17)