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