Project

General

Profile

Download (9.31 KB) Statistics
| Branch: | Revision:
1
package eu.etaxonomy.cdm.io.pesi.faunaEuropaea;
2

    
3
import java.sql.ResultSet;
4
import java.sql.SQLException;
5
import java.util.ArrayList;
6
import java.util.Collection;
7
import java.util.HashMap;
8
import java.util.HashSet;
9
import java.util.Iterator;
10
import java.util.List;
11
import java.util.Map;
12
import java.util.Set;
13
import java.util.UUID;
14
import java.util.Map.Entry;
15

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

    
19
import eu.etaxonomy.cdm.io.common.Source;
20
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
21
import eu.etaxonomy.cdm.model.common.CdmBase;
22
import eu.etaxonomy.cdm.model.common.Language;
23
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
24
import eu.etaxonomy.cdm.model.common.TermVocabulary;
25
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
26
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
27
import eu.etaxonomy.cdm.model.description.Distribution;
28
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
29
import eu.etaxonomy.cdm.model.description.TaxonDescription;
30
import eu.etaxonomy.cdm.model.location.NamedArea;
31
import eu.etaxonomy.cdm.model.reference.Reference;
32
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
33
import eu.etaxonomy.cdm.model.taxon.Taxon;
34
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
35

    
36
public class FaunaEuropaeaVernacularNamesImport extends FaunaEuropaeaImportBase {
37
	private static final Logger logger = Logger.getLogger(FaunaEuropaeaVernacularNamesImport.class);
38

    
39
	private HashMap<String, Reference> sourceMap = new HashMap<String, Reference>();
40
	
41
	@Override
42
	protected void doInvoke(FaunaEuropaeaImportState state) {
43
		int limit = state.getConfig().getLimitSave();
44
		
45
		/* Taxon store for retrieving taxa from and saving taxa to CDM */
46
		List<TaxonBase> taxonList = null;
47
		/* UUID store as input for retrieving taxa from CDM */
48
		Set<UUID> taxonUuids = null;
49
		/* Store to hold helper objects */
50
		Map<UUID, FaunaEuropaeaVernacularNamesTaxon> fauEuVernacularNamesMap = null;
51

    
52

    
53
		TransactionStatus txStatus = null;
54
		
55
		createSourceMap(state);
56
		
57
		FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
58
		Source source = fauEuConfig.getSource();
59

    
60
        int i = 0;
61

    
62
		String selectCount =
63
			" SELECT count(*) ";
64

    
65
		String selectColumns =
66
			" SELECT vernacularName.*, Taxon.UUID ";
67

    
68
		String fromClause =
69
			" FROM vernacularName INNER JOIN " +
70
            " Taxon ON vernacularname.taxon_UUID = Taxon.UUID ";
71
		String orderBy = " ORDER BY vernacularName.taxon_UUID";
72

    
73
		String countQuery =
74
			selectCount + fromClause;
75

    
76
		String selectQuery =
77
			selectColumns + fromClause + orderBy;
78

    
79

    
80

    
81
		if(logger.isInfoEnabled()) { logger.info("Start making distributions..."); }
82

    
83
		try {
84
			ResultSet rs = source.getResultSet(countQuery);
85
			rs.next();
86
			int count = rs.getInt(1);
87

    
88
			rs = source.getResultSet(selectQuery);
89

    
90
			if (logger.isInfoEnabled()) {
91
				logger.info("Number of rows: " + count);
92
				logger.info("Count Query: " + countQuery);
93
				logger.info("Select Query: " + selectQuery);
94
			}
95

    
96
			//int taxonId;
97

    
98
			while (rs.next()) {
99
				if ((i++ % limit) == 0) {
100

    
101
					txStatus = startTransaction();
102
					taxonUuids = new HashSet<UUID>(limit);
103
					fauEuVernacularNamesMap = new HashMap<UUID, FaunaEuropaeaVernacularNamesTaxon>(limit);
104

    
105
					if(logger.isInfoEnabled()) {
106
						logger.info("i = " + i + " - Distribution import transaction started");
107
					}
108
				}
109

    
110
				//taxonId = rs.getInt("dis_tax_id");
111
				String vernameGUID = rs.getString("GUID");
112
				String vernacularNameString = rs.getString("verName");
113
				int taxonFk = rs.getInt("taxonFK");
114
				int languageFK= rs.getInt("languageFK");
115
				String languageCache = rs.getString("languageCache");
116
				String taxonUUID= rs.getString("taxonUUID");
117
				String verSource = rs.getString("VerSource");
118
				UUID currentTaxonUuid = null;
119
				
120

    
121
				FaunaEuropaeaVernacularName fauEuVernacularName = new FaunaEuropaeaVernacularName();
122
				fauEuVernacularName.setGuid(vernameGUID);
123
				fauEuVernacularName.setLanguageCache(languageCache);
124
				fauEuVernacularName.setLanguageFk(languageFK);
125
				fauEuVernacularName.setVernacularName(vernacularNameString);
126
				fauEuVernacularName.setSource(verSource);
127

    
128
				if (!taxonUuids.contains(currentTaxonUuid)) {
129
					taxonUuids.add(currentTaxonUuid);
130
					FaunaEuropaeaVernacularNamesTaxon fauEuVernacularNameTaxon =
131
						new FaunaEuropaeaVernacularNamesTaxon(currentTaxonUuid);
132
					fauEuVernacularNamesMap.put(currentTaxonUuid, fauEuVernacularNameTaxon);
133
					fauEuVernacularNameTaxon = null;
134
				} else {
135
					if (logger.isTraceEnabled()) {
136
						logger.trace("Taxon (" + currentTaxonUuid + ") already stored.");
137
						continue;
138
					}
139
				}
140

    
141
				fauEuVernacularNamesMap.get(currentTaxonUuid).addVernacularName(fauEuVernacularName);
142

    
143
				if (((i % limit) == 0 && i != 1 ) || i == count ) {
144

    
145
					try {
146
						commitTaxaAndVernacularNames(state,  taxonUuids, fauEuVernacularNamesMap, txStatus);
147
						taxonUuids = null;
148
						taxonList = null;
149
						fauEuVernacularNamesMap = null;
150

    
151
					} catch (Exception e) {
152
						logger.error("Commit of taxa and distributions failed" + e.getMessage());
153
						e.printStackTrace();
154
					}
155

    
156
					if(logger.isInfoEnabled()) { logger.info("i = " + i + " - Transaction committed");}
157
				}
158

    
159

    
160
			}
161
			if (taxonUuids != null){
162
				try {
163
					commitTaxaAndVernacularNames(state, taxonUuids, fauEuVernacularNamesMap, txStatus);
164
					taxonUuids = null;
165
					taxonList = null;
166
					fauEuVernacularNamesMap = null;
167
				} catch (Exception e) {
168
					logger.error("Commit of taxa and distributions failed");
169
					logger.error(e.getMessage());
170
					e.printStackTrace();
171
				}
172
			}
173
			rs = null;
174
		} catch (SQLException e) {
175
			logger.error("SQLException:" +  e);
176
			state.setUnsuccessfull();
177
		}
178

    
179
		if(logger.isInfoEnabled()) { logger.info("End making distributions..."); }
180

    
181
		return;
182

    
183
	}
184

    
185
	@Override
186
	protected boolean doCheck(FaunaEuropaeaImportState state) {
187
		// TODO Auto-generated method stub
188
		return false;
189
	}
190

    
191
	@Override
192
	protected boolean isIgnore(FaunaEuropaeaImportState state) {
193
		// TODO Auto-generated method stub
194
		return false;
195
	}
196
	private void commitTaxaAndVernacularNames(
197
			FaunaEuropaeaImportState state, 
198
			Set<UUID> taxonUuids,
199
			Map<UUID, FaunaEuropaeaVernacularNamesTaxon> fauEuTaxonMap,
200
			TransactionStatus txStatus) throws Exception {
201
		 List<TaxonBase> taxonList = prepareTaxaAndVernacularNames(getTaxonService().find(taxonUuids), fauEuTaxonMap, state);
202

    
203
		getTaxonService().save(taxonList);
204
		taxonList = null;
205
		taxonUuids = null;
206
		fauEuTaxonMap = null;
207
		commitTransaction(txStatus);
208

    
209
	}
210
	
211
	private List<TaxonBase> prepareTaxaAndVernacularNames(List<TaxonBase> taxonList, Map<UUID, FaunaEuropaeaVernacularNamesTaxon> fauEuTaxonMap,  FaunaEuropaeaImportState state) throws Exception{
212

    
213
		CommonTaxonName verName = null;
214
		
215
		
216
		FaunaEuropaeaVernacularNamesTaxon fauEuHelperTaxon;
217
		UUID taxonUuid;
218
		TaxonDescription taxonDescription;
219
		Taxon taxon;
220
		for (TaxonBase<?> taxonBase : taxonList) {
221

    
222
			if (taxonBase != null) {
223

    
224
				if (taxonBase instanceof Taxon) {
225
					taxon = CdmBase.deproxy(taxonBase, Taxon.class);
226
				} else {
227
					logger.warn("TaxonBase (" + taxonBase.getId() + " is not of type Taxon but: "
228
							+ taxonBase.getClass().getSimpleName());
229
					continue;
230
				}
231

    
232

    
233
				Set<TaxonDescription> descriptionSet = taxon.getDescriptions();
234
				if (descriptionSet.size() > 0) {
235
					taxonDescription = descriptionSet.iterator().next();
236
				} else {
237
					taxonDescription = TaxonDescription.NewInstance();
238
					taxon.addDescription(taxonDescription);
239
				}
240

    
241
				taxonUuid = taxonBase.getUuid();
242
				fauEuHelperTaxon= fauEuTaxonMap.get(taxonUuid);
243

    
244
				for (FaunaEuropaeaVernacularName fauEuHelperVernacularName: fauEuHelperTaxon.getVernacularNames()) {
245
					
246
					verName = null;
247
					Language lang = FaunaEuropaeaTransformer.langFK2Language(fauEuHelperVernacularName.getLanguageFk());
248
					verName = CommonTaxonName.NewInstance(fauEuHelperVernacularName.getVernacularName(), lang);
249
					verName.setCreated(null);
250
					addOriginalSource(verName, null, null, sourceMap.get(fauEuHelperVernacularName.getSource()));
251
					taxonDescription.addElement(verName);
252
					
253
				}
254
			}
255
		}
256
		return taxonList;
257
	}
258
	
259
	private void createSourceMap(FaunaEuropaeaImportState state){
260
		FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
261
		Source source = fauEuConfig.getSource();
262
		TransactionStatus txStatus = null;
263
		String sourceSelect = "SELECT DISTINCT PESI_FaEu_vernaculars_export.VerSource FROM PESI_FaEu_vernaculars_export group by PESI_FaEu_vernaculars_export.VerSource";
264
		try {
265
			txStatus =startTransaction();
266
			ResultSet rs = source.getResultSet(sourceSelect);
267
			Reference sourceRef;
268
			String title ;
269
			Set<Reference> referncesToSave = new HashSet<Reference>();
270
			while (rs.next()) {
271
				sourceRef = ReferenceFactory.newGeneric();
272
				title = rs.getString("VerSource");
273
				sourceRef.setTitle(title);
274
				referncesToSave.add(sourceRef);
275
				
276
			}
277
			commitReferences(referncesToSave, txStatus);
278

    
279
		}catch (SQLException e) {
280
			logger.error("SQLException:" +  e);
281
			state.setUnsuccessfull();
282
		}
283

    
284
		
285
	}	
286
	private void commitReferences(Collection<Reference> references,  TransactionStatus txStatus) {
287
		Map <UUID, Reference> referenceMap =getReferenceService().save(references);
288
		Iterator<Entry<UUID, Reference>> it = referenceMap.entrySet().iterator();
289
		while (it.hasNext()){
290
			Reference<?> ref = it.next().getValue();
291
			String refID = ref.getTitle();
292
			sourceMap.put(refID, ref);
293
		}
294
		commitTransaction(txStatus);
295
	}
296
}
(19-19/20)