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.globis;
|
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.commons.lang.StringUtils;
|
20
|
import org.apache.log4j.Logger;
|
21
|
import org.springframework.stereotype.Component;
|
22
|
|
23
|
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
|
24
|
import eu.etaxonomy.cdm.io.common.IOValidator;
|
25
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
26
|
import eu.etaxonomy.cdm.io.common.mapping.IMappingImport;
|
27
|
import eu.etaxonomy.cdm.io.globis.validation.GlobisReferenceImportValidator;
|
28
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
29
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
30
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
31
|
import eu.etaxonomy.cdm.model.reference.ReferenceType;
|
32
|
|
33
|
|
34
|
/**
|
35
|
* @author a.mueller
|
36
|
* @created 20.02.2010
|
37
|
* @version 1.0
|
38
|
*/
|
39
|
@Component
|
40
|
public class GlobisReferenceImport extends GlobisImportBase<Reference> implements IMappingImport<Reference, GlobisImportState>{
|
41
|
private static final Logger logger = Logger.getLogger(GlobisReferenceImport.class);
|
42
|
|
43
|
private int modCount = 10000;
|
44
|
private static final String pluralString = "references";
|
45
|
private static final String dbTableName = "Literatur";
|
46
|
private static final Class cdmTargetClass = Reference.class;
|
47
|
|
48
|
public GlobisReferenceImport(){
|
49
|
super(pluralString, dbTableName, cdmTargetClass);
|
50
|
}
|
51
|
|
52
|
@Override
|
53
|
protected String getIdQuery() {
|
54
|
String strRecordQuery =
|
55
|
" SELECT refID " +
|
56
|
" FROM " + dbTableName
|
57
|
+ " WHERE RefSource like 'Original' or refID in (SELECT fiSpecRefId FROM specTax)";
|
58
|
return strRecordQuery;
|
59
|
}
|
60
|
|
61
|
@Override
|
62
|
protected String getRecordQuery(GlobisImportConfigurator config) {
|
63
|
String strRecordQuery =
|
64
|
" SELECT l.*, l.DateCreated as Created_When, l.CreatedBy as Created_Who," +
|
65
|
" l.ModifiedBy as Updated_who, l.DateModified as Updated_When, l.RefRemarks as Notes " +
|
66
|
" FROM " + getTableName() + " l " +
|
67
|
" WHERE ( l.refId IN (" + ID_LIST_TOKEN + ") )";
|
68
|
return strRecordQuery;
|
69
|
}
|
70
|
|
71
|
@Override
|
72
|
public boolean doPartition(ResultSetPartitioner partitioner, GlobisImportState state) {
|
73
|
boolean success = true;
|
74
|
|
75
|
Set<Reference> objectsToSave = new HashSet<Reference>();
|
76
|
|
77
|
// Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>) partitioner.getObjectMap(BerlinModelTaxonImport.NAMESPACE);
|
78
|
// Map<String, DerivedUnit> ecoFactDerivedUnitMap = (Map<String, DerivedUnit>) partitioner.getObjectMap(ECO_FACT_DERIVED_UNIT_NAMESPACE);
|
79
|
|
80
|
ResultSet rs = partitioner.getResultSet();
|
81
|
|
82
|
try {
|
83
|
|
84
|
int i = 0;
|
85
|
|
86
|
//for each reference
|
87
|
while (rs.next()){
|
88
|
|
89
|
if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));}
|
90
|
|
91
|
Integer refId = rs.getInt("RefId");
|
92
|
String title = rs.getString("RefTitle");
|
93
|
String refJournal = rs.getString("RefJournal");
|
94
|
|
95
|
try {
|
96
|
|
97
|
//source ref
|
98
|
Reference<?> sourceRef = state.getTransactionalSourceReference();
|
99
|
|
100
|
Reference<?> ref = createObject(rs, state);
|
101
|
ref.setTitle(title);
|
102
|
|
103
|
if (StringUtils.isNotBlank(refJournal)){
|
104
|
if (ref.getType().equals(ReferenceType.Article) ){
|
105
|
Reference<?> journal = getJournal(state, rs, refJournal);
|
106
|
ref.setInJournal(journal);
|
107
|
}else{
|
108
|
logger.warn("Reference type not supported for journal: " + ref.getType().toString() + ", refId: " + refId );
|
109
|
}
|
110
|
}
|
111
|
|
112
|
this.doIdCreatedUpdatedNotes(state, ref, rs, refId, REFERENCE_NAMESPACE);
|
113
|
|
114
|
|
115
|
|
116
|
//DONE
|
117
|
// RefType, RefTitle, RefJournal
|
118
|
|
119
|
//TODO
|
120
|
//Refid,CreatedBy,DateCreated,DateModified,ModifiedBy
|
121
|
//RefAuthor,RefBookTitle,RefDatePublished
|
122
|
//RefEdition, RefEditor, RefGeneralKeywords
|
123
|
//RefGeoKeywords, RefIll only,RefISSN, RefJournal
|
124
|
//RefLibrary, RefMarker,RefPages,RefPages only,
|
125
|
//RefPlace, RefPublisher, RefRemarks,
|
126
|
//RefSerial, RefSource, RefSpecificKeywords, RefTaxKeywords,
|
127
|
//RefURL, RefVolPageFig, RefVolume, RefYear
|
128
|
//SpecificKeywordDummy
|
129
|
|
130
|
//no data
|
131
|
//CountryDummy
|
132
|
|
133
|
objectsToSave.add(ref);
|
134
|
|
135
|
|
136
|
} catch (Exception e) {
|
137
|
logger.warn("Exception in literature: RefId " + refId + ". " + e.getMessage());
|
138
|
// e.printStackTrace();
|
139
|
}
|
140
|
|
141
|
}
|
142
|
|
143
|
// logger.warn("Specimen: " + countSpecimen + ", Descriptions: " + countDescriptions );
|
144
|
|
145
|
logger.warn(pluralString + " to save: " + objectsToSave.size());
|
146
|
getReferenceService().save(objectsToSave);
|
147
|
|
148
|
return success;
|
149
|
} catch (SQLException e) {
|
150
|
logger.error("SQLException:" + e);
|
151
|
return false;
|
152
|
}
|
153
|
}
|
154
|
|
155
|
|
156
|
|
157
|
|
158
|
private Reference<?> getJournal(GlobisImportState state, ResultSet rs, String refJournal) throws SQLException {
|
159
|
|
160
|
|
161
|
Reference<?> journal = ReferenceFactory.newJournal();
|
162
|
String issn = rs.getString("RefISSN");
|
163
|
if (StringUtils.isNotBlank(issn)){
|
164
|
issn.replaceAll("ISSN", "").trim();
|
165
|
journal.setIssn(issn);
|
166
|
}
|
167
|
|
168
|
|
169
|
|
170
|
//TODO deduplicate
|
171
|
journal.setTitle(refJournal);
|
172
|
return journal;
|
173
|
}
|
174
|
|
175
|
|
176
|
|
177
|
|
178
|
/* (non-Javadoc)
|
179
|
* @see eu.etaxonomy.cdm.io.common.mapping.IMappingImport#createObject(java.sql.ResultSet, eu.etaxonomy.cdm.io.common.ImportStateBase)
|
180
|
*/
|
181
|
public Reference<?> createObject(ResultSet rs, GlobisImportState state)
|
182
|
throws SQLException {
|
183
|
String refJournal = rs.getString("RefJournal");
|
184
|
boolean isInJournal =isNotBlank(refJournal);
|
185
|
String refBookTitle = rs.getString("RefBookTitle");
|
186
|
boolean isInBook =isNotBlank(refBookTitle);
|
187
|
|
188
|
|
189
|
|
190
|
Reference<?> ref;
|
191
|
String refType = rs.getString("RefType");
|
192
|
if (refType == null){
|
193
|
if (isInJournal && ! isInBook){
|
194
|
ref = ReferenceFactory.newArticle();
|
195
|
}else{
|
196
|
ref = ReferenceFactory.newGeneric();
|
197
|
}
|
198
|
}else if (refType.equals("book")){
|
199
|
ref = ReferenceFactory.newBook();
|
200
|
}else if (refType.equals("paper in journal")){
|
201
|
ref = ReferenceFactory.newArticle();
|
202
|
}else if (refType.startsWith("unpublished") ){
|
203
|
ref = ReferenceFactory.newGeneric();
|
204
|
}else if (refType.endsWith("paper in journal")){
|
205
|
ref = ReferenceFactory.newArticle();
|
206
|
}else if (refType.equals("paper in book")){
|
207
|
ref = ReferenceFactory.newBookSection();
|
208
|
}else if (refType.matches("paper in journal.*website.*")){
|
209
|
ref = ReferenceFactory.newArticle();
|
210
|
}else{
|
211
|
logger.warn("Unknown reference type: " + refType);
|
212
|
ref = ReferenceFactory.newGeneric();
|
213
|
}
|
214
|
return ref;
|
215
|
}
|
216
|
|
217
|
/* (non-Javadoc)
|
218
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
|
219
|
*/
|
220
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs) {
|
221
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
|
222
|
return result; //not needed
|
223
|
}
|
224
|
|
225
|
/* (non-Javadoc)
|
226
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
227
|
*/
|
228
|
@Override
|
229
|
protected boolean doCheck(GlobisImportState state){
|
230
|
IOValidator<GlobisImportState> validator = new GlobisReferenceImportValidator();
|
231
|
return validator.validate(state);
|
232
|
}
|
233
|
|
234
|
|
235
|
/* (non-Javadoc)
|
236
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
237
|
*/
|
238
|
protected boolean isIgnore(GlobisImportState state){
|
239
|
//TODO
|
240
|
return state.getConfig().getDoReferences() != IImportConfigurator.DO_REFERENCES.ALL;
|
241
|
}
|
242
|
|
243
|
|
244
|
|
245
|
|
246
|
|
247
|
}
|