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
|
package eu.etaxonomy.cdm.io.berlinModel.in;
|
10
|
|
11
|
import java.sql.ResultSet;
|
12
|
import java.sql.SQLException;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.Map;
|
16
|
import java.util.Set;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.springframework.stereotype.Component;
|
20
|
|
21
|
import eu.etaxonomy.cdm.io.berlinModel.in.validation.BerlinModelWebMarkerImportValidator;
|
22
|
import eu.etaxonomy.cdm.io.common.IOValidator;
|
23
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
24
|
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
|
25
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
26
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
27
|
import eu.etaxonomy.cdm.model.common.Marker;
|
28
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
29
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
30
|
|
31
|
|
32
|
/**
|
33
|
* @author a.mueller
|
34
|
* @created 20.03.2008
|
35
|
* @version 1.0
|
36
|
*/
|
37
|
@Component
|
38
|
public class BerlinModelWebMarkerImport extends BerlinModelImportBase {
|
39
|
private static final Logger logger = Logger.getLogger(BerlinModelWebMarkerImport.class);
|
40
|
|
41
|
private static int modCount = 2000;
|
42
|
private static final String dbTableName = "webMarker";
|
43
|
private static final String pluralString = "markers";
|
44
|
|
45
|
public BerlinModelWebMarkerImport(){
|
46
|
super(dbTableName, pluralString);
|
47
|
}
|
48
|
|
49
|
@Override
|
50
|
protected String getIdQuery(BerlinModelImportState state) {
|
51
|
String result = " SELECT markerId FROM " + getTableName();
|
52
|
if (state.getConfig().getWebMarkerFilter() != null){
|
53
|
result += " WHERE " + state.getConfig().getWebMarkerFilter();
|
54
|
}
|
55
|
return result;
|
56
|
}
|
57
|
|
58
|
@Override
|
59
|
protected String getRecordQuery(BerlinModelImportConfigurator config) {
|
60
|
String strQuery =
|
61
|
" SELECT * " +
|
62
|
" FROM webMarker INNER JOIN webTableName ON webMarker.TableNameFk = webTableName.TableNameId " +
|
63
|
" WHERE (markerId IN ("+ ID_LIST_TOKEN + ") )";
|
64
|
return strQuery;
|
65
|
}
|
66
|
|
67
|
@Override
|
68
|
public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState state) {
|
69
|
boolean success = true ;
|
70
|
|
71
|
Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>) partitioner.getObjectMap(BerlinModelTaxonImport.NAMESPACE);
|
72
|
Set<TaxonBase> taxaToBeSaved = new HashSet<TaxonBase>();
|
73
|
|
74
|
Map<String, DefinedTermBase> definedTermMap = state.getDbCdmDefinedTermMap();
|
75
|
ResultSet rs = partitioner.getResultSet();
|
76
|
|
77
|
int i = 0;
|
78
|
//for each reference
|
79
|
try{
|
80
|
while (rs.next()){
|
81
|
try{
|
82
|
if ((i++ % modCount ) == 0 && i!= 1 ){ logger.info(""+pluralString+" handled: " + (i-1));}
|
83
|
//
|
84
|
int markerId = rs.getInt("MarkerId");
|
85
|
int markerCategoryFk = rs.getInt("MarkerCategoryFk");
|
86
|
int rIdentifierFk = rs.getInt("RIdentifierFk");
|
87
|
String tableName = rs.getString("TableName");
|
88
|
Boolean activeFlag = rs.getBoolean("ActiveFlag");
|
89
|
|
90
|
AnnotatableEntity annotatableEntity;
|
91
|
if ("PTaxon".equals(tableName)){
|
92
|
TaxonBase<?> taxon = taxonMap.get(String.valueOf(rIdentifierFk));
|
93
|
if (taxon != null){
|
94
|
annotatableEntity = taxon;
|
95
|
taxaToBeSaved.add(taxon);
|
96
|
addMarker(annotatableEntity, activeFlag, markerCategoryFk, definedTermMap);
|
97
|
}else{
|
98
|
logger.warn("TaxonBase (RIdentifier " + rIdentifierFk + ") could not be found for marker " + markerId);
|
99
|
}
|
100
|
}else{
|
101
|
logger.warn("Marker for table " + tableName + " not yet implemented.");
|
102
|
success = false;
|
103
|
}
|
104
|
|
105
|
|
106
|
}catch(Exception ex){
|
107
|
logger.error(ex.getMessage());
|
108
|
ex.printStackTrace();
|
109
|
success = false;
|
110
|
}
|
111
|
} //while rs.hasNext()
|
112
|
} catch (SQLException e) {
|
113
|
logger.error("SQLException:" + e);
|
114
|
return false;
|
115
|
}
|
116
|
|
117
|
logger.info("save " + i + " "+pluralString + " ...");
|
118
|
getTaxonService().saveOrUpdate(taxaToBeSaved);
|
119
|
return success;
|
120
|
}
|
121
|
|
122
|
@Override
|
123
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
|
124
|
String nameSpace;
|
125
|
Class<?> cdmClass;
|
126
|
Set<String> idSet;
|
127
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
|
128
|
|
129
|
try{
|
130
|
Set<String> taxonIdSet = new HashSet<String>();
|
131
|
while (rs.next()){
|
132
|
int tableNameId = rs.getInt("TableNameFk");
|
133
|
if (tableNameId != 500){
|
134
|
//TODO
|
135
|
logger.warn("A marker is not related to table PTaxon. This case is not handled yet!");
|
136
|
}else{
|
137
|
handleForeignKey(rs, taxonIdSet, "RIdentifierFk");
|
138
|
}
|
139
|
}
|
140
|
|
141
|
//taxon map
|
142
|
nameSpace = BerlinModelTaxonImport.NAMESPACE;
|
143
|
cdmClass = TaxonBase.class;
|
144
|
idSet = taxonIdSet;
|
145
|
Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
146
|
result.put(nameSpace, taxonMap);
|
147
|
|
148
|
} catch (SQLException e) {
|
149
|
throw new RuntimeException(e);
|
150
|
}
|
151
|
return result;
|
152
|
}
|
153
|
|
154
|
private boolean addMarker(AnnotatableEntity annotatableEntity, boolean activeFlag, int markerCategoryFk, Map<String, DefinedTermBase> map ){
|
155
|
MarkerType markerType = (MarkerType)map.get("webMarkerCategory_" + markerCategoryFk);
|
156
|
if (markerType == null){
|
157
|
logger.warn("MarkerType not found: " + markerCategoryFk);
|
158
|
}
|
159
|
Marker marker = Marker.NewInstance(markerType, activeFlag);
|
160
|
annotatableEntity.addMarker(marker);
|
161
|
return true;
|
162
|
|
163
|
}
|
164
|
|
165
|
@Override
|
166
|
protected boolean doCheck(BerlinModelImportState state){
|
167
|
IOValidator<BerlinModelImportState> validator = new BerlinModelWebMarkerImportValidator();
|
168
|
return validator.validate(state);
|
169
|
}
|
170
|
|
171
|
@Override
|
172
|
protected boolean isIgnore(BerlinModelImportState state){
|
173
|
return ! state.getConfig().isDoMarker();
|
174
|
}
|
175
|
}
|