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.out;
|
10
|
|
11
|
import java.sql.Connection;
|
12
|
import java.sql.PreparedStatement;
|
13
|
import java.sql.SQLException;
|
14
|
import java.util.List;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
import org.springframework.stereotype.Component;
|
18
|
import org.springframework.transaction.TransactionStatus;
|
19
|
|
20
|
import eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer;
|
21
|
import eu.etaxonomy.cdm.io.common.Source;
|
22
|
import eu.etaxonomy.cdm.io.common.IImportConfigurator.DO_REFERENCES;
|
23
|
import eu.etaxonomy.cdm.io.common.mapping.out.CdmDbExportMapping;
|
24
|
import eu.etaxonomy.cdm.io.common.mapping.out.CreatedAndNotesMapper;
|
25
|
import eu.etaxonomy.cdm.io.common.mapping.out.DbBooleanMapper;
|
26
|
import eu.etaxonomy.cdm.io.common.mapping.out.DbObjectMapper;
|
27
|
import eu.etaxonomy.cdm.io.common.mapping.out.DbStringMapper;
|
28
|
import eu.etaxonomy.cdm.io.common.mapping.out.DbTimePeriodMapper;
|
29
|
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
|
30
|
import eu.etaxonomy.cdm.io.common.mapping.out.IdMapper;
|
31
|
import eu.etaxonomy.cdm.io.common.mapping.out.MethodMapper;
|
32
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
33
|
/*import eu.etaxonomy.cdm.model.reference.Article;
|
34
|
import eu.etaxonomy.cdm.model.reference.BookSection;*/
|
35
|
import eu.etaxonomy.cdm.model.reference.IArticle;
|
36
|
import eu.etaxonomy.cdm.model.reference.IBookSection;
|
37
|
import eu.etaxonomy.cdm.model.reference.IInProceedings;
|
38
|
import eu.etaxonomy.cdm.model.reference.IPrintedUnitBase;
|
39
|
/*import eu.etaxonomy.cdm.model.reference.InProceedings;
|
40
|
import eu.etaxonomy.cdm.model.reference.PrintedUnitBase;*/
|
41
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
42
|
import eu.etaxonomy.cdm.model.reference.ReferenceType;
|
43
|
//import eu.etaxonomy.cdm.model.reference.Thesis;
|
44
|
|
45
|
|
46
|
/**
|
47
|
* @author a.mueller
|
48
|
* @created 20.03.2008
|
49
|
* @version 1.0
|
50
|
*/
|
51
|
@Component
|
52
|
public class BerlinModelReferenceExport extends BerlinModelExportBase<Reference> {
|
53
|
private static final Logger logger = Logger.getLogger(BerlinModelReferenceExport.class);
|
54
|
|
55
|
private static int modCount = 1000;
|
56
|
private static final String dbTableName = "Reference";
|
57
|
private static final String pluralString = "references";
|
58
|
private static final Class<? extends CdmBase> standardMethodParameter = Reference.class;
|
59
|
|
60
|
public BerlinModelReferenceExport(){
|
61
|
super();
|
62
|
}
|
63
|
|
64
|
/* (non-Javadoc)
|
65
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
66
|
*/
|
67
|
@Override
|
68
|
protected boolean doCheck(BerlinModelExportState state){
|
69
|
boolean result = true;
|
70
|
logger.warn("Checking for References not yet implemented");
|
71
|
//result &= checkArticlesWithoutJournal(bmiConfig);
|
72
|
//result &= checkPartOfJournal(bmiConfig);
|
73
|
|
74
|
return result;
|
75
|
}
|
76
|
|
77
|
private CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> getMapping(){
|
78
|
String tableName = dbTableName;
|
79
|
CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> mapping = new CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer>(tableName);
|
80
|
mapping.addMapper(IdMapper.NewInstance("RefId"));
|
81
|
mapping.addMapper(MethodMapper.NewInstance("RefCategoryFk", this));
|
82
|
mapping.addMapper(MethodMapper.NewInstance("RefCache", this));
|
83
|
mapping.addMapper(MethodMapper.NewInstance("NomRefCache", this));
|
84
|
mapping.addMapper(DbBooleanMapper.NewFalseInstance("isProtectedTitleCache","PreliminaryFlag"));
|
85
|
|
86
|
mapping.addMapper(DbObjectMapper.NewInstance("authorTeam", "NomAuthorTeamFk"));
|
87
|
mapping.addMapper(MethodMapper.NewInstance("RefAuthorString", this));
|
88
|
|
89
|
mapping.addMapper(DbStringMapper.NewInstance("title", "Title"));
|
90
|
// mapping.addMapper(MethodMapper.NewInstance("Title", this));
|
91
|
|
92
|
// mapping.addMapper(DbStringMapper.NewInstance("title", "NomTitleAbbrev"));
|
93
|
mapping.addMapper(MethodMapper.NewInstance("NomTitleAbbrev", this));
|
94
|
|
95
|
|
96
|
|
97
|
mapping.addMapper(DbStringMapper.NewFacultativeInstance("edition", "Edition"));
|
98
|
mapping.addMapper(DbStringMapper.NewFacultativeInstance("volume", "Volume"));
|
99
|
mapping.addMapper(DbStringMapper.NewFacultativeInstance("series", "Series"));
|
100
|
mapping.addMapper(DbStringMapper.NewFacultativeInstance("pages", "PageString"));
|
101
|
mapping.addMapper(DbStringMapper.NewFacultativeInstance("isbn", "ISBN"));
|
102
|
mapping.addMapper(DbStringMapper.NewFacultativeInstance("issn", "ISSN"));
|
103
|
mapping.addMapper(DbStringMapper.NewFacultativeInstance("publisher", "Publisher"));
|
104
|
mapping.addMapper(DbStringMapper.NewFacultativeInstance("placePublished", "PublicationTown"));
|
105
|
mapping.addMapper(DbTimePeriodMapper.NewInstance("datePublished", "RefYear"));
|
106
|
mapping.addMapper(MethodMapper.NewInstance("ThesisFlag", this));
|
107
|
|
108
|
mapping.addMapper(CreatedAndNotesMapper.NewInstance());
|
109
|
|
110
|
// ,[Series] ??
|
111
|
// ,[URL]
|
112
|
// ,[ExportDate]
|
113
|
// ,[InformalRefCategory]
|
114
|
// ,[IsPaper]
|
115
|
// ,[RefSourceFk]
|
116
|
// ,[IdInSource]
|
117
|
|
118
|
|
119
|
return mapping;
|
120
|
}
|
121
|
|
122
|
protected void doInvoke(BerlinModelExportState state){
|
123
|
try{
|
124
|
logger.info("start make "+pluralString+" ...");
|
125
|
boolean success = true ;
|
126
|
|
127
|
doDelete(state);
|
128
|
|
129
|
TransactionStatus txStatus = startTransaction(true);
|
130
|
|
131
|
List<Reference> list = getReferenceService().list(null,100000000, 0,null,null);
|
132
|
|
133
|
CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> mapping = getMapping();
|
134
|
mapping.initialize(state);
|
135
|
|
136
|
int count = 0;
|
137
|
for (Reference<?> ref : list){
|
138
|
doCount(count++, modCount, pluralString);
|
139
|
success &= mapping.invoke(ref);
|
140
|
}
|
141
|
//Prepare InRefStatement
|
142
|
logger.info("start make inRefences ...");
|
143
|
String inRefSql = "UPDATE Reference SET InRefFk = ? WHERE RefId = ?";
|
144
|
Connection con = state.getConfig().getDestination().getConnection();
|
145
|
PreparedStatement stmt = con.prepareStatement(inRefSql);
|
146
|
count = 0;
|
147
|
for (Reference<?> ref : list){
|
148
|
doCount(count++, modCount, "inReferences");
|
149
|
success &= invokeInRef(ref, state, stmt);
|
150
|
}
|
151
|
|
152
|
commitTransaction(txStatus);
|
153
|
logger.info("end make "+pluralString+" ..." + getSuccessString(success));
|
154
|
if (!success){
|
155
|
state.setUnsuccessfull();
|
156
|
}
|
157
|
return;
|
158
|
}catch(SQLException e){
|
159
|
e.printStackTrace();
|
160
|
logger.error(e.getMessage());
|
161
|
state.setUnsuccessfull();
|
162
|
return;
|
163
|
}
|
164
|
}
|
165
|
|
166
|
protected boolean invokeInRef(Reference ref, BerlinModelExportState state, PreparedStatement stmt) {
|
167
|
if (ref == null){
|
168
|
return true;
|
169
|
}else{
|
170
|
Reference<?> inRef = getInRef(ref);
|
171
|
if (inRef == null){
|
172
|
return true;
|
173
|
}else{
|
174
|
Integer inRefId = state.getDbId(inRef);
|
175
|
Integer refId = state.getDbId(ref);
|
176
|
try {
|
177
|
stmt.setInt(1, inRefId);
|
178
|
stmt.setInt(2, refId);
|
179
|
stmt.executeUpdate();
|
180
|
return true;
|
181
|
} catch (SQLException e) {
|
182
|
logger.error("SQLException during inRef invoke for reference " + ref.getTitleCache() + ": " + e.getMessage());
|
183
|
e.printStackTrace();
|
184
|
return false;
|
185
|
}
|
186
|
}
|
187
|
}
|
188
|
}
|
189
|
|
190
|
private Reference<?> getInRef(Reference<?> ref){
|
191
|
Reference<?> inRef;
|
192
|
if (ref.getType().equals(ReferenceType.Article)){
|
193
|
return (Reference)((IArticle)ref).getInJournal();
|
194
|
}else if (ref.getType().equals(ReferenceType.BookSection)){
|
195
|
return (Reference)((IBookSection)ref).getInBook();
|
196
|
}else if (ref.getType().equals(ReferenceType.InProceedings)){
|
197
|
return (Reference) ((IInProceedings)ref).getInProceedings();
|
198
|
}else if (ref.getType().isPrintedUnit()){
|
199
|
return (Reference)((IPrintedUnitBase)ref).getInSeries();
|
200
|
}else{
|
201
|
return null;
|
202
|
}
|
203
|
|
204
|
}
|
205
|
|
206
|
protected boolean doDelete(BerlinModelExportState state){
|
207
|
BerlinModelExportConfigurator bmeConfig = state.getConfig();
|
208
|
|
209
|
String sql;
|
210
|
Source destination = bmeConfig.getDestination();
|
211
|
|
212
|
//RelReference
|
213
|
sql = "DELETE FROM RelReference";
|
214
|
destination.setQuery(sql);
|
215
|
destination.update(sql);
|
216
|
//References
|
217
|
sql = "DELETE FROM Reference";
|
218
|
destination.setQuery(sql);
|
219
|
destination.update(sql);
|
220
|
return true;
|
221
|
}
|
222
|
|
223
|
|
224
|
/* (non-Javadoc)
|
225
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
226
|
*/
|
227
|
protected boolean isIgnore(BerlinModelExportState state){
|
228
|
if (state.getConfig().getDoReferences().equals(DO_REFERENCES.ALL)){
|
229
|
return false;
|
230
|
}else{
|
231
|
return true;
|
232
|
}
|
233
|
}
|
234
|
|
235
|
//called by MethodMapper
|
236
|
@SuppressWarnings("unused")
|
237
|
private static Integer getRefCategoryFk(Reference<?> ref){
|
238
|
return BerlinModelTransformer.ref2refCategoryId(ref);
|
239
|
}
|
240
|
|
241
|
//called by MethodMapper
|
242
|
@SuppressWarnings("unused")
|
243
|
private static String getRefCache(Reference<?> ref){
|
244
|
if (ref.isProtectedTitleCache()){
|
245
|
return ref.getTitleCache();
|
246
|
}else{
|
247
|
return null;
|
248
|
}
|
249
|
}
|
250
|
|
251
|
//called by MethodMapper
|
252
|
@SuppressWarnings("unused")
|
253
|
private static String getNomRefCache(Reference<?> ref){
|
254
|
if (ref.isProtectedTitleCache()){
|
255
|
return ref.getTitleCache();
|
256
|
}else{
|
257
|
return null;
|
258
|
}
|
259
|
}
|
260
|
|
261
|
// //called by MethodMapper
|
262
|
// @SuppressWarnings("unused")
|
263
|
// private static String getTtile(StrictReferenceBase<?> ref){
|
264
|
//// if (ref.isProtectedTitleCache()){
|
265
|
//// return ref.getTitleCache();
|
266
|
//// }else{
|
267
|
//// return null;
|
268
|
//// }
|
269
|
// }
|
270
|
|
271
|
|
272
|
//called by MethodMapper
|
273
|
@SuppressWarnings("unused")
|
274
|
private static String getNomTitleAbbrev(Reference<?> ref){
|
275
|
|
276
|
if (/*ref.isNomenclaturallyRelevant() &&*/ ref.getTitle() != null && ref.getTitle().length() <=80){
|
277
|
return ref.getTitle();
|
278
|
}else{
|
279
|
return null;
|
280
|
}
|
281
|
}
|
282
|
|
283
|
|
284
|
|
285
|
//called by MethodMapper
|
286
|
@SuppressWarnings("unused")
|
287
|
private static String getRefAuthorString(Reference<?> ref){
|
288
|
if (ref == null){
|
289
|
return null;
|
290
|
}else{
|
291
|
return (ref.getAuthorTeam() == null)? null: ref.getAuthorTeam().getTitleCache();
|
292
|
}
|
293
|
}
|
294
|
|
295
|
|
296
|
//called by MethodMapper
|
297
|
@SuppressWarnings("unused")
|
298
|
private static Boolean getPreliminaryFlag(Reference<?> ref){
|
299
|
if (ref.isProtectedTitleCache()){
|
300
|
return true;
|
301
|
}else{
|
302
|
return false;
|
303
|
}
|
304
|
}
|
305
|
|
306
|
//called by MethodMapper
|
307
|
@SuppressWarnings("unused")
|
308
|
private static Boolean getThesisFlag(Reference<?> ref){
|
309
|
if (ref.getType().equals(ReferenceType.Thesis)){
|
310
|
return true;
|
311
|
}else{
|
312
|
return false;
|
313
|
}
|
314
|
}
|
315
|
|
316
|
|
317
|
/* (non-Javadoc)
|
318
|
* @see eu.etaxonomy.cdm.io.berlinModel.out.BerlinModelExportBase#getStandardMethodParameter()
|
319
|
*/
|
320
|
@Override
|
321
|
public Class<? extends CdmBase> getStandardMethodParameter() {
|
322
|
return standardMethodParameter;
|
323
|
}
|
324
|
}
|