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.common.mapping.out.DbLastActionMapper;
|
29
|
import eu.etaxonomy.cdm.io.pesi.erms.validation.ErmsDistributionImportValidator;
|
30
|
import eu.etaxonomy.cdm.model.common.AnnotationType;
|
31
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
32
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
33
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
34
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
35
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
36
|
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
|
37
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
38
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
39
|
|
40
|
/**
|
41
|
* @author a.mueller
|
42
|
* @since 20.02.2010
|
43
|
*/
|
44
|
@Component
|
45
|
public class ErmsDistributionImport
|
46
|
extends ErmsImportBase<Distribution> {
|
47
|
|
48
|
private static final long serialVersionUID = 6169103238671736935L;
|
49
|
|
50
|
@SuppressWarnings("unused")
|
51
|
private static final Logger logger = Logger.getLogger(ErmsDistributionImport.class);
|
52
|
|
53
|
private DbImportMapping<ErmsImportState, ErmsImportConfigurator> mapping;
|
54
|
|
55
|
private static final String pluralString = "distributions";
|
56
|
private static final String dbTableName = "dr";
|
57
|
private static final Class<?> cdmTargetClass = Distribution.class;
|
58
|
|
59
|
public ErmsDistributionImport(){
|
60
|
super(pluralString, dbTableName, cdmTargetClass);
|
61
|
}
|
62
|
|
63
|
@Override
|
64
|
protected String getRecordQuery(ErmsImportConfigurator config) {
|
65
|
String strRecordQuery =
|
66
|
" SELECT dr.*, ISNULL(ISNULL(tu.tu_accfinal, tu.tu_accfinal), tu.id) acctaxon, " +
|
67
|
" s.sessiondate lastActionDate, a.action_name lastAction, s.ExpertName " +
|
68
|
" FROM dr INNER JOIN tu ON dr.tu_id = tu.id " +
|
69
|
" LEFT OUTER JOIN dr_sessions MN ON MN.dr_id = dr.id " +
|
70
|
" LEFT OUTER JOIN [sessions] s ON s.id = MN.session_id " +
|
71
|
" LEFT OUTER JOIN actions a ON a.id = MN.action_id " +
|
72
|
" WHERE ( dr.id IN (" + ID_LIST_TOKEN + ") )" +
|
73
|
" ORDER BY dr.id, s.sessiondate DESC, a.id DESC ";
|
74
|
return strRecordQuery;
|
75
|
}
|
76
|
|
77
|
@Override
|
78
|
protected DbImportMapping<ErmsImportState, ErmsImportConfigurator> getMapping() {
|
79
|
if (mapping == null){
|
80
|
mapping = new DbImportMapping<>();
|
81
|
|
82
|
PresenceAbsenceTerm status = PresenceAbsenceTerm.PRESENT();
|
83
|
DbImportDistributionCreationMapper<?> distributionMapper = DbImportDistributionCreationMapper
|
84
|
.NewFixedStatusInstance("id", DR_NAMESPACE, "acctaxon", ErmsImportBase.TAXON_NAMESPACE, status);
|
85
|
distributionMapper.setSource("source_id", REFERENCE_NAMESPACE, null);
|
86
|
mapping.addMapper(distributionMapper);
|
87
|
|
88
|
mapping.addMapper(DbImportObjectMapper.NewInstance("gu_id", "area", ErmsImportBase.AREA_NAMESPACE));
|
89
|
mapping.addMapper(DbImportAnnotationMapper.NewInstance("note", AnnotationType.EDITORIAL()));
|
90
|
//last action
|
91
|
AnnotationType speciesExpertNameAnnType = getAnnotationType(ErmsTransformer.uuidAnnSpeciesExpertName, "species expert name", "species expert name", "species expert name");
|
92
|
mapping.addMapper(DbImportAnnotationMapper.NewInstance("ExpertName", speciesExpertNameAnnType)); //according to sql script ExpertName maps to SpeciesExpertName in ERMS
|
93
|
AnnotationType lastActionDateType = getAnnotationType(DbLastActionMapper.uuidAnnotationTypeLastActionDate, "Last action date", "Last action date", null);
|
94
|
mapping.addMapper(DbImportAnnotationMapper.NewInstance("lastActionDate", lastActionDateType));
|
95
|
AnnotationType lastActionType = getAnnotationType(DbLastActionMapper.uuidAnnotationTypeLastAction, "Last action", "Last action", null);
|
96
|
MarkerType hasNoLastActionMarkerType = getMarkerType(DbLastActionMapper.uuidMarkerTypeHasNoLastAction, "has no last action", "No last action information available", "no last action");
|
97
|
mapping.addMapper(DbImportAnnotationMapper.NewInstance("lastAction", lastActionType, hasNoLastActionMarkerType));
|
98
|
|
99
|
mapping.addMapper(DbIgnoreMapper.NewInstance("unacceptsource_id"));
|
100
|
mapping.addMapper(DbIgnoreMapper.NewInstance("unacceptreason"));
|
101
|
mapping.addMapper(DbIgnoreMapper.NewInstance("valid_flag"));
|
102
|
mapping.addMapper(DbIgnoreMapper.NewInstance("certain_flag"));
|
103
|
mapping.addMapper(DbIgnoreMapper.NewInstance("map_flag"));
|
104
|
mapping.addMapper(DbIgnoreMapper.NewInstance("endemic_flag"));
|
105
|
mapping.addMapper(DbIgnoreMapper.NewInstance("exotic_flag"));
|
106
|
mapping.addMapper(DbIgnoreMapper.NewInstance("typelocality_flag"));
|
107
|
mapping.addMapper(DbIgnoreMapper.NewInstance("specimenflag"));
|
108
|
mapping.addMapper(DbIgnoreMapper.NewInstance("lat"));
|
109
|
mapping.addMapper(DbIgnoreMapper.NewInstance("long"));
|
110
|
mapping.addMapper(DbIgnoreMapper.NewInstance("depthshallow"));
|
111
|
mapping.addMapper(DbIgnoreMapper.NewInstance("depthdeep"));
|
112
|
mapping.addMapper(DbIgnoreMapper.NewInstance("beginyear"));
|
113
|
mapping.addMapper(DbIgnoreMapper.NewInstance("beginmonth"));
|
114
|
mapping.addMapper(DbIgnoreMapper.NewInstance("beginday"));
|
115
|
mapping.addMapper(DbIgnoreMapper.NewInstance("endyear"));
|
116
|
mapping.addMapper(DbIgnoreMapper.NewInstance("endmonth"));
|
117
|
mapping.addMapper(DbIgnoreMapper.NewInstance("endday"));
|
118
|
mapping.addMapper(DbIgnoreMapper.NewInstance("min_abundance"));
|
119
|
mapping.addMapper(DbIgnoreMapper.NewInstance("max_abundance"));
|
120
|
}
|
121
|
return mapping;
|
122
|
}
|
123
|
|
124
|
Integer lastDistributionId = null;
|
125
|
@Override
|
126
|
protected boolean ignoreRecord(ResultSet rs) throws SQLException {
|
127
|
Integer id = rs.getInt("id");
|
128
|
boolean result = id.equals(lastDistributionId);
|
129
|
lastDistributionId = id;
|
130
|
return result;
|
131
|
}
|
132
|
|
133
|
@Override
|
134
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, ErmsImportState state) {
|
135
|
|
136
|
String nameSpace;
|
137
|
Set<String> idSet;
|
138
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
|
139
|
|
140
|
try{
|
141
|
Set<String> taxonIdSet = new HashSet<>();
|
142
|
Set<String> areaIdSet = new HashSet<>();
|
143
|
Set<String> sourceIdSet = new HashSet<>();
|
144
|
while (rs.next()){
|
145
|
handleForeignKey(rs, taxonIdSet,"acctaxon" );
|
146
|
handleForeignKey(rs, areaIdSet, "gu_id");
|
147
|
handleForeignKey(rs, sourceIdSet, "source_id");
|
148
|
}
|
149
|
|
150
|
//taxon map
|
151
|
nameSpace = ErmsImportBase.TAXON_NAMESPACE;
|
152
|
idSet = taxonIdSet;
|
153
|
@SuppressWarnings("rawtypes")
|
154
|
Map<String, TaxonBase> taxonMap = getCommonService().getSourcedObjectsByIdInSourceC(TaxonBase.class, idSet, nameSpace);
|
155
|
result.put(nameSpace, taxonMap);
|
156
|
|
157
|
//areas
|
158
|
nameSpace = ErmsImportBase.AREA_NAMESPACE;
|
159
|
idSet = areaIdSet;
|
160
|
Map<String, NamedArea> areaMap = getCommonService().getSourcedObjectsByIdInSourceC(NamedArea.class, idSet, nameSpace);
|
161
|
result.put(nameSpace, areaMap);
|
162
|
|
163
|
//reference map
|
164
|
nameSpace = ErmsImportBase.REFERENCE_NAMESPACE;
|
165
|
idSet = sourceIdSet;
|
166
|
Map<String, Reference> referenceMap = getCommonService().getSourcedObjectsByIdInSourceC(Reference.class, idSet, nameSpace);
|
167
|
result.put(nameSpace, referenceMap);
|
168
|
|
169
|
} catch (SQLException e) {
|
170
|
throw new RuntimeException(e);
|
171
|
}
|
172
|
return result;
|
173
|
}
|
174
|
|
175
|
private void addSource(Distribution distribution, Integer source_id, ErmsImportState state) {
|
176
|
Reference ref = (Reference)state.getRelatedObject(ErmsImportBase.REFERENCE_NAMESPACE, String.valueOf(source_id));
|
177
|
distribution.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, ref, null);
|
178
|
}
|
179
|
|
180
|
@SuppressWarnings("unused")
|
181
|
public Distribution createObject(ResultSet rs, ErmsImportState state)
|
182
|
throws SQLException {
|
183
|
return null; //not needed
|
184
|
}
|
185
|
|
186
|
@Override
|
187
|
protected boolean doCheck(ErmsImportState state){
|
188
|
IOValidator<ErmsImportState> validator = new ErmsDistributionImportValidator();
|
189
|
return validator.validate(state);
|
190
|
}
|
191
|
|
192
|
@Override
|
193
|
protected boolean isIgnore(ErmsImportState state){
|
194
|
return ! state.getConfig().isDoDistributions();
|
195
|
}
|
196
|
}
|