Project

General

Profile

Download (9.56 KB) Statistics
| Branch: | Revision:
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.pesi.faunaEuropaea;
11

    
12
import java.sql.ResultSet;
13
import java.sql.SQLException;
14
import java.util.HashMap;
15
import java.util.HashSet;
16
import java.util.List;
17
import java.util.Map;
18
import java.util.Set;
19
import java.util.UUID;
20

    
21
import org.apache.log4j.Logger;
22
import org.springframework.stereotype.Component;
23
import org.springframework.transaction.TransactionStatus;
24

    
25
import eu.etaxonomy.cdm.io.common.Source;
26
import eu.etaxonomy.cdm.model.common.CdmBase;
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.taxon.Taxon;
32
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
33

    
34

    
35
/**
36
 * @author a.babadshanjan
37
 * @created 12.05.2009
38
 * @version 1.0
39
 */
40
@Component
41
public class FaunaEuropaeaDistributionImport extends FaunaEuropaeaImportBase {
42
	private static final Logger logger = Logger.getLogger(FaunaEuropaeaDistributionImport.class);
43

    
44

    
45
	@Override
46
	protected boolean doCheck(FaunaEuropaeaImportState state) {
47
		boolean result = true;
48
		FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
49
		logger.warn("Checking for Distributions not yet fully implemented");
50
		result &= checkReferenceStatus(fauEuConfig);
51

    
52
		return result;
53
	}
54

    
55
	private boolean checkReferenceStatus(FaunaEuropaeaImportConfigurator fauEuConfig) {
56
		boolean result = true;
57
//		try {
58
//			Source source = fauEuConfig.getSource();
59
//			String sqlStr = "";
60
	//		ResultSet rs = source.getResultSet(sqlStr);
61
			return result;
62
//		} catch (SQLException e) {
63
//			e.printStackTrace();
64
//			return false;
65
//		}
66
	}
67

    
68
	@Override
69
	protected void doInvoke(FaunaEuropaeaImportState state) {
70
		/*
71
		logger.warn("Start distribution doInvoke");
72
		ProfilerController.memorySnapshot();
73
		*/
74

    
75
		if (!state.getConfig().isDoOccurrence()){
76
			return;
77
		}
78

    
79
		int limit = state.getConfig().getLimitSave();
80
		UUID noDataUuid;
81
		/* Taxon store for retrieving taxa from and saving taxa to CDM */
82
		List<TaxonBase> taxonList = null;
83
		/* UUID store as input for retrieving taxa from CDM */
84
		Set<UUID> taxonUuids = null;
85
		/* Store to hold helper objects */
86
		Map<UUID, FaunaEuropaeaDistributionTaxon> fauEuTaxonMap = null;
87

    
88

    
89
		TransactionStatus txStatus = null;
90

    
91
		//txStatus = startTransaction();
92
		/*PresenceAbsenceTerm noDataStatusTerm = PresenceAbsenceTerm.NewPresenceInstance("no data", "no data", "nod");
93
		noDataUuid = noDataStatusTerm.getUuid();
94
		TermVocabulary<PresenceAbsenceTerm> voc = getVocabularyService().find(30);
95
		HibernateProxyHelper.deproxy(voc, OrderedTermVocabulary.class);
96
		//voc.addTerm(noDataStatusTerm);
97
	//	getVocabularyService().saveOrUpdate(voc);
98
		getTermService().save(noDataStatusTerm);*/
99
		//commitTransaction(txStatus);
100

    
101
	//	FaunaEuropaeaTransformer.setUUIDs(noDataUuid);
102

    
103
		FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
104
		Source source = fauEuConfig.getSource();
105

    
106
        int i = 0;
107

    
108
		String selectCount =
109
			" SELECT count(*) ";
110

    
111
		String selectColumns =
112
			" SELECT distribution.*, Area.*, Taxon.UUID ";
113

    
114
		String fromClause =
115
			" FROM distribution INNER JOIN " +
116
            " Area ON distribution.dis_ara_id = Area.ara_id INNER JOIN " +
117
            " Taxon ON distribution.dis_tax_id = Taxon.TAX_ID ";
118
		String orderBy = " ORDER BY distribution.dis_tax_id";
119

    
120
		String countQuery =
121
			selectCount + fromClause;
122

    
123
		String selectQuery =
124
			selectColumns + fromClause + orderBy;
125

    
126

    
127

    
128
		if(logger.isInfoEnabled()) { logger.info("Start making distributions..."); }
129

    
130
		try {
131
			ResultSet rs = source.getResultSet(countQuery);
132
			rs.next();
133
			int count = rs.getInt(1);
134

    
135
			rs = source.getResultSet(selectQuery);
136

    
137
			if (logger.isInfoEnabled()) {
138
				logger.info("Number of rows: " + count);
139
				logger.info("Count Query: " + countQuery);
140
				logger.info("Select Query: " + selectQuery);
141
			}
142

    
143
			//int taxonId;
144

    
145
			while (rs.next()) {
146
				if ((i++ % limit) == 0) {
147

    
148
					txStatus = startTransaction();
149
					taxonUuids = new HashSet<UUID>(limit);
150
					fauEuTaxonMap = new HashMap<UUID, FaunaEuropaeaDistributionTaxon>(limit);
151

    
152
					if(logger.isInfoEnabled()) {
153
						logger.info("i = " + i + " - Distribution import transaction started");
154
					}
155
				}
156

    
157
				//taxonId = rs.getInt("dis_tax_id");
158
				int disId = rs.getInt("dis_id");
159
				int occStatusId = rs.getInt("dis_present");
160
				int areaId = rs.getInt("ara_id");
161
				String areaName = rs.getString("ara_name");
162
				String areaCode = rs.getString("ara_code");
163
				int extraLimital = rs.getInt("ara_extralimital");
164
				UUID currentTaxonUuid = null;
165
				if (resultSetHasColumn(rs,"UUID")){
166
					currentTaxonUuid = UUID.fromString(rs.getString("UUID"));
167
				} else {
168
					currentTaxonUuid = UUID.randomUUID();
169
				}
170

    
171
				FaunaEuropaeaDistribution fauEuDistribution = new FaunaEuropaeaDistribution();
172
				fauEuDistribution.setDistributionId(disId);
173
				fauEuDistribution.setOccurrenceStatusId(occStatusId);
174
				fauEuDistribution.setAreaId(areaId);
175
				fauEuDistribution.setAreaName(areaName);
176
				fauEuDistribution.setAreaCode(areaCode);
177
				fauEuDistribution.setExtraLimital(extraLimital);
178

    
179
				if (!taxonUuids.contains(currentTaxonUuid)) {
180
					taxonUuids.add(currentTaxonUuid);
181
					FaunaEuropaeaDistributionTaxon fauEuDistributionTaxon =
182
						new FaunaEuropaeaDistributionTaxon(currentTaxonUuid);
183
					fauEuTaxonMap.put(currentTaxonUuid, fauEuDistributionTaxon);
184
					fauEuDistributionTaxon = null;
185
				} else {
186
					if (logger.isTraceEnabled()) {
187
						logger.trace("Taxon (" + currentTaxonUuid + ") already stored.");
188
						continue;
189
					}
190
				}
191

    
192
				fauEuTaxonMap.get(currentTaxonUuid).addDistribution(fauEuDistribution);
193

    
194
				if (((i % limit) == 0 && i != 1 ) || i == count ) {
195

    
196
					try {
197
						commitTaxaAndDistribution(state, taxonUuids, fauEuTaxonMap, txStatus);
198
						taxonUuids = null;
199
						taxonList = null;
200
						fauEuTaxonMap = null;
201

    
202
					} catch (Exception e) {
203
						logger.error("Commit of taxa and distributions failed" + e.getMessage());
204
						e.printStackTrace();
205
					}
206

    
207
					if(logger.isInfoEnabled()) { logger.info("i = " + i + " - Transaction committed");}
208
				}
209

    
210

    
211
			}
212
			if (taxonUuids != null){
213
				try {
214
					commitTaxaAndDistribution(state, taxonUuids, fauEuTaxonMap, txStatus);
215
					taxonUuids = null;
216
					taxonList = null;
217
					fauEuTaxonMap = null;
218
				} catch (Exception e) {
219
					logger.error("Commit of taxa and distributions failed");
220
					logger.error(e.getMessage());
221
					e.printStackTrace();
222
				}
223
			}
224
			rs = null;
225
		} catch (SQLException e) {
226
			logger.error("SQLException:" +  e);
227
			state.setUnsuccessfull();
228
		}
229

    
230
		if(logger.isInfoEnabled()) { logger.info("End making distributions..."); }
231

    
232

    
233
		return;
234
	}
235

    
236
	private void commitTaxaAndDistribution(
237
			FaunaEuropaeaImportState state,
238
			Set<UUID> taxonUuids,
239
			Map<UUID, FaunaEuropaeaDistributionTaxon> fauEuTaxonMap,
240
			TransactionStatus txStatus) throws Exception {
241
		 List<TaxonBase> taxonList = prepareTaxaAndDistribution(getTaxonService().find(taxonUuids), fauEuTaxonMap, state);
242

    
243
		getTaxonService().save(taxonList);
244
		taxonList = null;
245
		taxonUuids = null;
246
		fauEuTaxonMap = null;
247
		commitTransaction(txStatus);
248

    
249
	}
250

    
251
	private List<TaxonBase> prepareTaxaAndDistribution(List<TaxonBase> taxonList, Map<UUID, FaunaEuropaeaDistributionTaxon> fauEuTaxonMap,  FaunaEuropaeaImportState state) throws Exception{
252

    
253
		Distribution newDistribution = null;
254
		NamedArea namedArea;
255
		PresenceAbsenceTerm presenceAbsenceStatus;
256
		FaunaEuropaeaDistributionTaxon fauEuHelperTaxon;
257
		UUID taxonUuid;
258
		TaxonDescription taxonDescription;
259
		Taxon taxon;
260
		for (TaxonBase<?> taxonBase : taxonList) {
261

    
262
			if (taxonBase != null) {
263

    
264
				if (taxonBase instanceof Taxon) {
265
					taxon = CdmBase.deproxy(taxonBase, Taxon.class);
266
				} else {
267
					logger.warn("TaxonBase (" + taxonBase.getId() + " is not of type Taxon but: "
268
							+ taxonBase.getClass().getSimpleName());
269
					continue;
270
				}
271

    
272

    
273
				Set<TaxonDescription> descriptionSet = taxon.getDescriptions();
274
				if (descriptionSet.size() > 0) {
275
					taxonDescription = descriptionSet.iterator().next();
276
				} else {
277
					taxonDescription = TaxonDescription.NewInstance();
278
					taxon.addDescription(taxonDescription);
279
				}
280

    
281
				taxonUuid = taxonBase.getUuid();
282
				fauEuHelperTaxon= fauEuTaxonMap.get(taxonUuid);
283

    
284
				for (FaunaEuropaeaDistribution fauEuHelperDistribution : fauEuHelperTaxon.getDistributions()) {
285
					namedArea = null;
286
					newDistribution = null;
287
					presenceAbsenceStatus = null;
288

    
289
					presenceAbsenceStatus = FaunaEuropaeaTransformer.occStatus2PresenceAbsence(fauEuHelperDistribution.getOccurrenceStatusId());
290

    
291

    
292
					namedArea = FaunaEuropaeaTransformer.areaId2TdwgArea(fauEuHelperDistribution);
293

    
294
					if (namedArea == null){
295
						UUID areaUuid= FaunaEuropaeaTransformer.getUUIDByAreaAbbr(fauEuHelperDistribution.getAreaCode());
296
						if (areaUuid == null){
297
							logger.warn("Area " + fauEuHelperDistribution.getAreaCode() + "not found in FE transformer");
298
						}
299
						namedArea = getNamedArea(state, areaUuid, fauEuHelperDistribution.getAreaName(), fauEuHelperDistribution.getAreaName(), fauEuHelperDistribution.getAreaCode(), null, null);
300

    
301
					}
302

    
303
					newDistribution = Distribution.NewInstance(namedArea, presenceAbsenceStatus);
304
					newDistribution.setCreated(null);
305

    
306
					taxonDescription.addElement(newDistribution);
307
				}
308
			}
309
		}
310
		return taxonList;
311
	}
312

    
313
    @Override
314
    protected boolean isIgnore(FaunaEuropaeaImportState state){
315
		return !state.getConfig().isDoOccurrence();
316
	}
317
}
(4-4/20)