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