Project

General

Profile

Download (5.69 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.indexFungorum;
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.Map;
17
import java.util.Set;
18

    
19
import org.apache.commons.lang.StringUtils;
20
import org.apache.log4j.Logger;
21
import org.springframework.stereotype.Component;
22

    
23
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
24
import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26
import eu.etaxonomy.cdm.model.common.Marker;
27
import eu.etaxonomy.cdm.model.common.MarkerType;
28
import eu.etaxonomy.cdm.model.description.Distribution;
29
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
30
import eu.etaxonomy.cdm.model.description.TaxonDescription;
31
import eu.etaxonomy.cdm.model.location.NamedArea;
32
import eu.etaxonomy.cdm.model.reference.Reference;
33
import eu.etaxonomy.cdm.model.taxon.Taxon;
34

    
35
/**
36
 * @author a.mueller
37
 * @since 27.02.2012
38
 */
39
@Component
40
public class IndexFungorumDistributionImport  extends IndexFungorumImportBase {
41

    
42
    private static final long serialVersionUID = -815842161276543719L;
43
    private static final Logger logger = Logger.getLogger(IndexFungorumDistributionImport.class);
44

    
45
	private static final String pluralString = "distributions";
46
	private static final String dbTableName = "[tblPESIfungi]";
47

    
48
	public IndexFungorumDistributionImport(){
49
		super(pluralString, dbTableName);
50
	}
51

    
52
	@Override
53
	protected String getIdQuery() {
54
		String result = " SELECT PreferredNameIFnumber FROM " + getTableName() +
55
				" ORDER BY PreferredNameIFnumber ";
56
		return result;
57
	}
58

    
59
	@Override
60
	protected String getRecordQuery(IndexFungorumImportConfigurator config) {
61
		String strRecordQuery =
62
				" SELECT distribution.* " +
63
				" FROM tblPESIfungi AS distribution  " +
64
			" WHERE ( distribution.PreferredNameIFnumber  IN (" + ID_LIST_TOKEN + ") )" +
65
			"";
66
		return strRecordQuery;
67
	}
68

    
69
	@Override
70
	public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner,
71
	            IndexFungorumImportState state) {
72

    
73
		boolean success = true;
74
//		Reference sourceReference = state.getRelatedObject(NAMESPACE_REFERENCE, SOURCE_REFERENCE, Reference.class);
75
		ResultSet rs = partitioner.getResultSet();
76

    
77
		try {
78
			//column names that do not hold distribution information
79
			Set<String> excludedColumns = new HashSet<>();
80
			excludedColumns.add("PreferredName");
81
			excludedColumns.add("PreferredNameIFnumber");
82
			excludedColumns.add("PreferredNameFDCnumber");
83

    
84
			PresenceAbsenceTerm status = PresenceAbsenceTerm.PRESENT();
85
			MarkerType noLastActionMarkerType = getNoLastActionMarkerType(state);
86
			while (rs.next()){
87

    
88
				//get taxon description
89
				Integer id = rs.getInt("PreferredNameIFnumber");
90
				Taxon taxon = state.getRelatedObject(NAMESPACE_SPECIES, String.valueOf(id), Taxon.class);
91
				Reference ref = null;
92
				if (taxon == null){
93
					logger.debug("taxon is null for id " + id);
94
				} else{
95
					TaxonDescription description = getTaxonDescription(taxon, ref, false, true);
96

    
97
					//handle single distributions
98
					int count = rs.getMetaData().getColumnCount();
99
					for (int i=1; i <= count; i++ ){
100
						String colName = rs.getMetaData().getColumnName(i);
101
						//exclude non distribution columns
102
						if (! excludedColumns.contains(colName)){
103
							String distributionValue = rs.getString(i);
104
							if (StringUtils.isNotBlank(distributionValue)){
105
								//create distribution for existing occurrences
106
								if (! distributionValue.equals("X")){
107
									logger.warn("Unexpected distribution value '" + distributionValue + "' for area " + colName);
108
								}
109
								NamedArea area = state.getTransformer().getNamedAreaByKey(colName);
110
								Distribution distribution = Distribution.NewInstance(area, status);
111
								description.addElement(distribution);
112
								//no last action
113
								distribution.addMarker(Marker.NewInstance(noLastActionMarkerType, true));
114
							}
115
						}
116
					}
117
					getTaxonService().saveOrUpdate(taxon);
118
				}
119
				//save
120
			}
121
		} catch (Exception e) {
122
			e.printStackTrace();
123
			logger.error(e.getMessage());
124
			state.setSuccess(false);
125
			success = false;
126
		}
127
		return success;
128
	}
129

    
130
	@Override
131
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, IndexFungorumImportState state) {
132

    
133
	    String nameSpace;
134
		Set<String> idSet;
135
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
136

    
137
		try{
138
			Set<String> taxonIdSet = new HashSet<>();
139
			while (rs.next()){
140
				handleForeignKey(rs, taxonIdSet, "PreferredNameIFnumber" );
141
			}
142

    
143
			//taxon map
144
			nameSpace = NAMESPACE_SPECIES;
145
			idSet = taxonIdSet;
146
            Map<String, Taxon> taxonMap = getCommonService().getSourcedObjectsByIdInSourceC(Taxon.class, idSet, nameSpace);
147
			result.put(nameSpace, taxonMap);
148

    
149
			//sourceReference
150
			Reference sourceReference = getReferenceService().find(PesiTransformer.uuidSourceRefIndexFungorum);
151
			Map<String, Reference> referenceMap = new HashMap<>();
152
			referenceMap.put(SOURCE_REFERENCE, sourceReference);
153
			result.put(NAMESPACE_REFERENCE, referenceMap);
154

    
155
		} catch (SQLException e) {
156
			throw new RuntimeException(e);
157
		}
158
		return result;
159
	}
160

    
161
	@Override
162
	protected boolean doCheck(IndexFungorumImportState state){
163
		return true;
164
	}
165

    
166
	@Override
167
	protected boolean isIgnore(IndexFungorumImportState state){
168
		return ! state.getConfig().isDoOccurrence();
169
	}
170
}
(1-1/10)