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.URISyntaxException;
14
import java.util.Collection;
15
import java.util.List;
16

    
17
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
18
import org.springframework.stereotype.Component;
19
import org.springframework.transaction.TransactionStatus;
20

    
21
import eu.etaxonomy.cdm.common.URI;
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.LanguageStringBase;
28
import eu.etaxonomy.cdm.model.description.DescriptionBase;
29
import eu.etaxonomy.cdm.model.media.Media;
30
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
31
import eu.etaxonomy.cdm.model.name.TaxonName;
32
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
33
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
34
import eu.etaxonomy.cdm.model.permission.User;
35
import eu.etaxonomy.cdm.model.reference.Reference;
36
import eu.etaxonomy.cdm.model.taxon.Classification;
37
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
38
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
39
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
40
import eu.etaxonomy.cdm.model.term.TermNode;
41
import eu.etaxonomy.cdm.model.term.TermTree;
42
import eu.etaxonomy.cdm.model.term.TermVocabulary;
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 = LogManager.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.getJavaUri());
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<TermTree> featureTrees;
121
		List<TermNode> termNodes;
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
		} catch (Exception ex) {
251
			logger.error("Error saving homotypical groups");
252
			success = false;
253
		}
254
		//commitTransaction(txStatus);
255

    
256

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

    
280

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

    
296

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

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

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

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

    
344
		if (jaxbImpConfig.isDoClassificationData() == true) {
345
			logger.info("# Classification");
346

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

    
355

    
356
		commitTransaction(txStatus);
357
		logger.info("All data saved");
358
		if (!success){
359
			state.setUnsuccessfull();
360
		}
361

    
362
		return;
363

    
364
	}
365

    
366

    
367
	private boolean saveTermVocabularies(
368
			List<TermVocabulary<DefinedTermBase>> termVocabularies) {
369

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

    
381
	private boolean saveTerms(List<DefinedTermBase> terms) {
382

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

    
395

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

    
403

    
404
	@Override
405
	protected boolean isIgnore(JaxbImportState state) {
406
		return false;
407
	}
408
}
(12-12/17)