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.DbImportDescriptionElementSourceCreationMapper;
|
24
|
import eu.etaxonomy.cdm.io.common.mapping.DbImportMapping;
|
25
|
import eu.etaxonomy.cdm.io.pesi.erms.validation.ErmsVernacularSourceImportValidator;
|
26
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
27
|
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
|
28
|
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
|
29
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
30
|
|
31
|
/**
|
32
|
* @author a.mueller
|
33
|
* @since 12.03.2010
|
34
|
*/
|
35
|
@Component
|
36
|
public class ErmsVernacularSourcesImport
|
37
|
extends ErmsImportBase<CommonTaxonName> {
|
38
|
|
39
|
private static final long serialVersionUID = 8334548532717058431L;
|
40
|
@SuppressWarnings("unused")
|
41
|
private static final Logger logger = Logger.getLogger(ErmsVernacularSourcesImport.class);
|
42
|
|
43
|
|
44
|
//************************** VARIABLES ********************************************
|
45
|
|
46
|
private static String pluralString = "vernacular sources";
|
47
|
private static String dbTableName = "vernaculars_sources";
|
48
|
private static final Class<?> cdmTargetClass = DescriptionElementSource.class;
|
49
|
|
50
|
private DbImportMapping<?,?> mapping;
|
51
|
|
52
|
|
53
|
//******************************************* CONSTRUCTOR *******************************
|
54
|
|
55
|
public ErmsVernacularSourcesImport() {
|
56
|
super(pluralString, dbTableName, cdmTargetClass);
|
57
|
}
|
58
|
|
59
|
@Override
|
60
|
protected String getRecordQuery(ErmsImportConfigurator config) {
|
61
|
String strQuery =
|
62
|
" SELECT * " +
|
63
|
" FROM vernaculars_sources " +
|
64
|
" WHERE vernacular_id IN (" + ID_LIST_TOKEN + ") AND " +
|
65
|
" source_id IN (" + ID_LIST_TOKEN + ")";
|
66
|
return strQuery;
|
67
|
}
|
68
|
|
69
|
@Override
|
70
|
protected String getIdQuery() {
|
71
|
String strQuery =
|
72
|
" SELECT vernacular_id, source_id " +
|
73
|
" FROM vernaculars_sources "
|
74
|
;
|
75
|
return strQuery;
|
76
|
}
|
77
|
|
78
|
@Override
|
79
|
protected DbImportMapping<?,?> getMapping() {
|
80
|
if (mapping == null){
|
81
|
mapping = new DbImportMapping<>();
|
82
|
String vernacularNamespace = ErmsImportBase.VERNACULAR_NAMESPACE;
|
83
|
String referenceNamespace = ErmsImportBase.REFERENCE_NAMESPACE;
|
84
|
mapping.addMapper(DbImportDescriptionElementSourceCreationMapper.NewInstance("vernacular_id", vernacularNamespace, "source_id", referenceNamespace ));
|
85
|
}
|
86
|
return mapping;
|
87
|
}
|
88
|
|
89
|
@Override
|
90
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, ErmsImportState state) {
|
91
|
String nameSpace;
|
92
|
Class<?> cdmClass;
|
93
|
Set<String> idSet;
|
94
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
|
95
|
|
96
|
try{
|
97
|
Set<String> vernacularIdSet = new HashSet<>();
|
98
|
Set<String> sourceIdSet = new HashSet<>();
|
99
|
while (rs.next()){
|
100
|
handleForeignKey(rs, vernacularIdSet, "vernacular_id");
|
101
|
handleForeignKey(rs, sourceIdSet, "source_id");
|
102
|
}
|
103
|
|
104
|
//vernacular map
|
105
|
nameSpace = ErmsImportBase.VERNACULAR_NAMESPACE;
|
106
|
cdmClass = CommonTaxonName.class;
|
107
|
idSet = vernacularIdSet;
|
108
|
@SuppressWarnings("unchecked")
|
109
|
Map<String, CommonTaxonName> vernacularMap = (Map<String, CommonTaxonName>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
110
|
result.put(nameSpace, vernacularMap);
|
111
|
|
112
|
//reference map
|
113
|
nameSpace = ErmsImportBase.REFERENCE_NAMESPACE;
|
114
|
cdmClass = Reference.class;
|
115
|
idSet = sourceIdSet;
|
116
|
@SuppressWarnings("unchecked")
|
117
|
Map<String, Reference> referenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
118
|
result.put(nameSpace, referenceMap);
|
119
|
|
120
|
} catch (SQLException e) {
|
121
|
throw new RuntimeException(e);
|
122
|
}
|
123
|
return result;
|
124
|
}
|
125
|
|
126
|
@Override
|
127
|
protected boolean doCheck(ErmsImportState state) {
|
128
|
IOValidator<ErmsImportState> validator = new ErmsVernacularSourceImportValidator();
|
129
|
return validator.validate(state);
|
130
|
}
|
131
|
|
132
|
@Override
|
133
|
protected boolean isIgnore(ErmsImportState state) {
|
134
|
boolean isDo = state.getConfig().isDoVernaculars();
|
135
|
return ! isDo ;
|
136
|
}
|
137
|
}
|