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.erms;
|
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.log4j.Logger;
|
20
|
import org.springframework.stereotype.Component;
|
21
|
|
22
|
import eu.etaxonomy.cdm.io.common.IOValidator;
|
23
|
import eu.etaxonomy.cdm.io.common.mapping.DbIgnoreMapper;
|
24
|
import eu.etaxonomy.cdm.io.common.mapping.DbImportAnnotationMapper;
|
25
|
import eu.etaxonomy.cdm.io.common.mapping.DbImportDistributionCreationMapper;
|
26
|
import eu.etaxonomy.cdm.io.common.mapping.DbImportMapping;
|
27
|
import eu.etaxonomy.cdm.io.common.mapping.DbImportObjectMapper;
|
28
|
import eu.etaxonomy.cdm.io.pesi.erms.validation.ErmsDrImportValidator;
|
29
|
import eu.etaxonomy.cdm.model.common.AnnotationType;
|
30
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
31
|
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
|
32
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
33
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
34
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
35
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
36
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
37
|
|
38
|
/**
|
39
|
* @author a.mueller
|
40
|
* @since 20.02.2010
|
41
|
*/
|
42
|
@Component
|
43
|
public class ErmsDrImport
|
44
|
extends ErmsImportBase<Distribution> {
|
45
|
|
46
|
private static final long serialVersionUID = 6169103238671736935L;
|
47
|
|
48
|
@SuppressWarnings("unused")
|
49
|
private static final Logger logger = Logger.getLogger(ErmsDrImport.class);
|
50
|
|
51
|
private DbImportMapping<ErmsImportState, ErmsImportConfigurator> mapping;
|
52
|
|
53
|
private static final String pluralString = "distributions";
|
54
|
private static final String dbTableName = "dr";
|
55
|
private static final Class<?> cdmTargetClass = Distribution.class;
|
56
|
|
57
|
public ErmsDrImport(){
|
58
|
super(pluralString, dbTableName, cdmTargetClass);
|
59
|
}
|
60
|
|
61
|
@Override
|
62
|
protected String getRecordQuery(ErmsImportConfigurator config) {
|
63
|
String strRecordQuery =
|
64
|
" SELECT dr.*, tu.tu_acctaxon, tu.id " +
|
65
|
" FROM dr INNER JOIN tu ON dr.tu_id = tu.id " +
|
66
|
" WHERE ( dr.id IN (" + ID_LIST_TOKEN + ") )";
|
67
|
return strRecordQuery;
|
68
|
}
|
69
|
|
70
|
@Override
|
71
|
protected DbImportMapping<ErmsImportState, ErmsImportConfigurator> getMapping() {
|
72
|
if (mapping == null){
|
73
|
mapping = new DbImportMapping<>();
|
74
|
|
75
|
PresenceAbsenceTerm status = PresenceAbsenceTerm.PRESENT();
|
76
|
DbImportDistributionCreationMapper<?> distributionMapper = DbImportDistributionCreationMapper.NewFixedStatusInstance("id", DR_NAMESPACE, "tu_acctaxon", ErmsTaxonImport.TAXON_NAMESPACE, status);
|
77
|
distributionMapper.setSource("source_id", REFERENCE_NAMESPACE, null);
|
78
|
mapping.addMapper(distributionMapper);
|
79
|
|
80
|
mapping.addMapper(DbImportObjectMapper.NewInstance("gu_id", "area", ErmsAreaImport.AREA_NAMESPACE));
|
81
|
mapping.addMapper(DbImportAnnotationMapper.NewInstance("note", AnnotationType.EDITORIAL()));
|
82
|
|
83
|
mapping.addMapper(DbIgnoreMapper.NewInstance("unacceptsource_id"));
|
84
|
mapping.addMapper(DbIgnoreMapper.NewInstance("unacceptreason"));
|
85
|
mapping.addMapper(DbIgnoreMapper.NewInstance("valid_flag"));
|
86
|
mapping.addMapper(DbIgnoreMapper.NewInstance("certain_flag"));
|
87
|
mapping.addMapper(DbIgnoreMapper.NewInstance("map_flag"));
|
88
|
mapping.addMapper(DbIgnoreMapper.NewInstance("endemic_flag"));
|
89
|
mapping.addMapper(DbIgnoreMapper.NewInstance("exotic_flag"));
|
90
|
mapping.addMapper(DbIgnoreMapper.NewInstance("typelocality_flag"));
|
91
|
mapping.addMapper(DbIgnoreMapper.NewInstance("specimenflag"));
|
92
|
mapping.addMapper(DbIgnoreMapper.NewInstance("lat"));
|
93
|
mapping.addMapper(DbIgnoreMapper.NewInstance("long"));
|
94
|
mapping.addMapper(DbIgnoreMapper.NewInstance("depthshallow"));
|
95
|
mapping.addMapper(DbIgnoreMapper.NewInstance("depthdeep"));
|
96
|
mapping.addMapper(DbIgnoreMapper.NewInstance("beginyear"));
|
97
|
mapping.addMapper(DbIgnoreMapper.NewInstance("beginmonth"));
|
98
|
mapping.addMapper(DbIgnoreMapper.NewInstance("beginday"));
|
99
|
mapping.addMapper(DbIgnoreMapper.NewInstance("endyear"));
|
100
|
mapping.addMapper(DbIgnoreMapper.NewInstance("endmonth"));
|
101
|
mapping.addMapper(DbIgnoreMapper.NewInstance("endday"));
|
102
|
mapping.addMapper(DbIgnoreMapper.NewInstance("min_abundance"));
|
103
|
mapping.addMapper(DbIgnoreMapper.NewInstance("max_abundance"));
|
104
|
}
|
105
|
return mapping;
|
106
|
}
|
107
|
|
108
|
@Override
|
109
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, ErmsImportState state) {
|
110
|
String nameSpace;
|
111
|
Class<?> cdmClass;
|
112
|
Set<String> idSet;
|
113
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
|
114
|
|
115
|
try{
|
116
|
Set<String> taxonIdSet = new HashSet<>();
|
117
|
Set<String> areaIdSet = new HashSet<>();
|
118
|
Set<String> sourceIdSet = new HashSet<>();
|
119
|
while (rs.next()){
|
120
|
handleForeignKey(rs, taxonIdSet,"tu_acctaxon" );
|
121
|
handleForeignKey(rs, areaIdSet, "gu_id");
|
122
|
handleForeignKey(rs, sourceIdSet, "source_id");
|
123
|
}
|
124
|
|
125
|
//taxon map
|
126
|
nameSpace = ErmsImportBase.TAXON_NAMESPACE;
|
127
|
cdmClass = TaxonBase.class;
|
128
|
idSet = taxonIdSet;
|
129
|
@SuppressWarnings("unchecked")
|
130
|
Map<String, TaxonBase<?>> taxonMap = (Map<String, TaxonBase<?>>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
131
|
result.put(nameSpace, taxonMap);
|
132
|
|
133
|
//areas
|
134
|
nameSpace = ErmsImportBase.AREA_NAMESPACE;
|
135
|
cdmClass = NamedArea.class;
|
136
|
idSet = areaIdSet;
|
137
|
@SuppressWarnings("unchecked")
|
138
|
Map<String, NamedArea> areaMap = (Map<String, NamedArea>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
139
|
result.put(nameSpace, areaMap);
|
140
|
|
141
|
//reference map
|
142
|
nameSpace = ErmsImportBase.REFERENCE_NAMESPACE;
|
143
|
cdmClass = Reference.class;
|
144
|
idSet = sourceIdSet;
|
145
|
@SuppressWarnings("unchecked")
|
146
|
Map<String, Reference> referenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
147
|
result.put(nameSpace, referenceMap);
|
148
|
|
149
|
} catch (SQLException e) {
|
150
|
throw new RuntimeException(e);
|
151
|
}
|
152
|
return result;
|
153
|
}
|
154
|
|
155
|
/**
|
156
|
* @param distribution
|
157
|
* @param source_id
|
158
|
* @param state
|
159
|
*/
|
160
|
private void addSource(Distribution distribution, Integer source_id, ErmsImportState state) {
|
161
|
Reference ref = (Reference)state.getRelatedObject(ErmsReferenceImport.REFERENCE_NAMESPACE, String.valueOf(source_id));
|
162
|
distribution.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, ref, null);
|
163
|
}
|
164
|
|
165
|
@SuppressWarnings("unused")
|
166
|
public Distribution createObject(ResultSet rs, ErmsImportState state)
|
167
|
throws SQLException {
|
168
|
return null; //not needed
|
169
|
}
|
170
|
|
171
|
@Override
|
172
|
protected boolean doCheck(ErmsImportState state){
|
173
|
IOValidator<ErmsImportState> validator = new ErmsDrImportValidator();
|
174
|
return validator.validate(state);
|
175
|
}
|
176
|
|
177
|
@Override
|
178
|
protected boolean isIgnore(ErmsImportState state){
|
179
|
return ! state.getConfig().isDoOccurrence();
|
180
|
}
|
181
|
}
|