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.ResultSet;
|
12
|
import java.sql.SQLException;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
|
17
|
import org.apache.log4j.Logger;
|
18
|
import org.springframework.stereotype.Component;
|
19
|
import org.springframework.transaction.TransactionStatus;
|
20
|
|
21
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
22
|
import eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer;
|
23
|
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelFactsImport;
|
24
|
import eu.etaxonomy.cdm.io.berlinModel.out.mapper.RefDetailMapper;
|
25
|
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
|
26
|
import eu.etaxonomy.cdm.io.common.Source;
|
27
|
import eu.etaxonomy.cdm.io.common.mapping.out.CdmDbExportMapping;
|
28
|
import eu.etaxonomy.cdm.io.common.mapping.out.CreatedAndNotesMapper;
|
29
|
import eu.etaxonomy.cdm.io.common.mapping.out.DbIntegerAnnotationMapper;
|
30
|
import eu.etaxonomy.cdm.io.common.mapping.out.DbMarkerMapper;
|
31
|
import eu.etaxonomy.cdm.io.common.mapping.out.DbObjectMapper;
|
32
|
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
|
33
|
import eu.etaxonomy.cdm.io.common.mapping.out.IdMapper;
|
34
|
import eu.etaxonomy.cdm.io.common.mapping.out.MethodMapper;
|
35
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
36
|
import eu.etaxonomy.cdm.model.common.Language;
|
37
|
import eu.etaxonomy.cdm.model.common.LanguageString;
|
38
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
39
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
40
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
41
|
import eu.etaxonomy.cdm.model.description.Feature;
|
42
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
43
|
import eu.etaxonomy.cdm.model.description.TextData;
|
44
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
45
|
|
46
|
|
47
|
/**
|
48
|
* @author a.mueller
|
49
|
* @created 20.03.2008
|
50
|
* @version 1.0
|
51
|
*/
|
52
|
@Component
|
53
|
public class BerlinModelFactExport extends BerlinModelExportBase<TextData> {
|
54
|
private static final Logger logger = Logger.getLogger(BerlinModelFactExport.class);
|
55
|
|
56
|
private static int modCount = 2500;
|
57
|
private static final String dbTableName = "Fact";
|
58
|
private static final String pluralString = "Facts";
|
59
|
private static final Class<? extends CdmBase> standardMethodParameter = TextData.class;
|
60
|
@Deprecated
|
61
|
private static Source source;
|
62
|
|
63
|
public BerlinModelFactExport(){
|
64
|
super();
|
65
|
}
|
66
|
|
67
|
/* (non-Javadoc)
|
68
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
69
|
*/
|
70
|
@Override
|
71
|
protected boolean doCheck(BerlinModelExportState state){
|
72
|
boolean result = true;
|
73
|
logger.warn("Checking for " + pluralString + " not yet implemented");
|
74
|
//result &= checkArticlesWithoutJournal(bmiConfig);
|
75
|
//result &= checkPartOfJournal(bmiConfig);
|
76
|
|
77
|
return result;
|
78
|
}
|
79
|
|
80
|
private CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> getMapping(){
|
81
|
String tableName = dbTableName;
|
82
|
CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> mapping = new CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer>(tableName);
|
83
|
mapping.addMapper(IdMapper.NewInstance("FactId"));
|
84
|
mapping.addMapper(MethodMapper.NewInstance("PTNameFk", this.getClass(), "getPTNameFk", TextData.class, DbExportStateBase.class));
|
85
|
mapping.addMapper(MethodMapper.NewInstance("PTRefFk", this.getClass(), "getPTRefFk", TextData.class, DbExportStateBase.class));
|
86
|
mapping.addMapper(MethodMapper.NewInstance("Fact", this));
|
87
|
mapping.addMapper(MethodMapper.NewInstance("FactCategoryFk", this));
|
88
|
|
89
|
mapping.addMapper(DbObjectMapper.NewInstance("citation", "FactRefFk"));
|
90
|
mapping.addMapper(RefDetailMapper.NewInstance("citationMicroReference","citation", "FactRefDetailFk"));
|
91
|
mapping.addMapper(DbObjectMapper.NewInstance("citation", "PTDesignationRefFk"));
|
92
|
mapping.addMapper(RefDetailMapper.NewInstance("citationMicroReference","citation", "PTDesignationRefDetailFk"));
|
93
|
mapping.addMapper(DbMarkerMapper.NewInstance(MarkerType.IS_DOUBTFUL(), "DoubtfulFlag", false));
|
94
|
mapping.addMapper(DbMarkerMapper.NewInstance(MarkerType.IS_DOUBTFUL(), "PublishFlag", true));
|
95
|
mapping.addMapper(DbIntegerAnnotationMapper.NewInstance(BerlinModelFactsImport.SEQUENCE_PREFIX, "Sequence", 999));
|
96
|
mapping.addMapper(CreatedAndNotesMapper.NewInstance());
|
97
|
|
98
|
//TODO
|
99
|
// designationRef
|
100
|
return mapping;
|
101
|
}
|
102
|
|
103
|
@Override
|
104
|
protected void doInvoke(BerlinModelExportState state){
|
105
|
try{
|
106
|
logger.info("start make " + pluralString + " ...");
|
107
|
boolean success = true ;
|
108
|
doDelete(state);
|
109
|
|
110
|
TransactionStatus txStatus = startTransaction(true);
|
111
|
|
112
|
List<DescriptionBase> list = getDescriptionService().list(null,1000000000, 0,null,null);
|
113
|
|
114
|
CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> mapping = getMapping();
|
115
|
mapping.initialize(state);
|
116
|
|
117
|
this.source = state.getConfig().getDestination();
|
118
|
int count = 0;
|
119
|
for (DescriptionBase<?> desc : list){
|
120
|
for (DescriptionElementBase descEl : desc.getElements()){
|
121
|
doCount(count++, modCount, pluralString);
|
122
|
if (descEl.isInstanceOf(TextData.class)){
|
123
|
success &= mapping.invoke(descEl);
|
124
|
}else{
|
125
|
logger.warn (descEl.getClass().getSimpleName() + " not yet supported for Fact Export.");
|
126
|
}
|
127
|
}
|
128
|
}
|
129
|
commitTransaction(txStatus);
|
130
|
logger.info("end make " + pluralString + " ..." + getSuccessString(success));
|
131
|
|
132
|
if (!success){
|
133
|
String message = "An undefined error occurred during Fact export";
|
134
|
state.getResult().addError(message);
|
135
|
}
|
136
|
return;
|
137
|
}catch(SQLException e){
|
138
|
e.printStackTrace();
|
139
|
logger.error(e.getMessage());
|
140
|
state.getResult().addException(e);
|
141
|
return;
|
142
|
}
|
143
|
}
|
144
|
|
145
|
|
146
|
|
147
|
|
148
|
protected boolean doDelete(BerlinModelExportState state){
|
149
|
BerlinModelExportConfigurator bmeConfig = state.getConfig();
|
150
|
|
151
|
String sql;
|
152
|
Source destination = bmeConfig.getDestination();
|
153
|
//Fact
|
154
|
sql = "DELETE FROM Fact";
|
155
|
destination.setQuery(sql);
|
156
|
destination.update(sql);
|
157
|
|
158
|
return true;
|
159
|
}
|
160
|
|
161
|
|
162
|
/* (non-Javadoc)
|
163
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
164
|
*/
|
165
|
@Override
|
166
|
protected boolean isIgnore(BerlinModelExportState state){
|
167
|
return ! state.getConfig().isDoFacts();
|
168
|
}
|
169
|
|
170
|
//called by MethodMapper
|
171
|
@SuppressWarnings("unused")
|
172
|
private static Integer getFactCategoryFk(TextData textData){
|
173
|
Feature feature = textData.getFeature();
|
174
|
Integer catFk = BerlinModelTransformer.textData2FactCategoryFk(feature);
|
175
|
//catFk = 302;
|
176
|
if (catFk == null){
|
177
|
catFk = findCategory(feature);
|
178
|
}
|
179
|
if (catFk == null){
|
180
|
//catFk = insertCategory(feature);
|
181
|
}
|
182
|
return catFk;
|
183
|
}
|
184
|
|
185
|
|
186
|
//called by MethodMapper
|
187
|
@SuppressWarnings("unused")
|
188
|
private static Integer getPTNameFk(TextData textData, DbExportStateBase<?, IExportTransformer> state){
|
189
|
return getObjectFk(textData, state, true);
|
190
|
}
|
191
|
|
192
|
//called by MethodMapper
|
193
|
@SuppressWarnings("unused")
|
194
|
private static Integer getPTRefFk(TextData textData, DbExportStateBase<?, IExportTransformer> state){
|
195
|
return getObjectFk(textData, state, false);
|
196
|
}
|
197
|
|
198
|
private static Integer getObjectFk(TextData textData, DbExportStateBase<?, IExportTransformer> state, boolean isName){
|
199
|
DescriptionBase<?> desc = textData.getInDescription();
|
200
|
if (desc.isInstanceOf(TaxonDescription.class)){
|
201
|
TaxonDescription taxonDesc = (TaxonDescription)desc;
|
202
|
Taxon taxon = taxonDesc.getTaxon();
|
203
|
if (taxon != null){
|
204
|
CdmBase cdmBase = (isName) ? taxon.getName(): taxon.getSec();
|
205
|
return state.getDbId(cdmBase);
|
206
|
}
|
207
|
}
|
208
|
logger.warn("No taxon found for description: " + textData.toString());
|
209
|
return null;
|
210
|
}
|
211
|
|
212
|
//called by MethodMapper
|
213
|
@SuppressWarnings("unused")
|
214
|
private static String getFact(TextData textData){
|
215
|
// Map<Language, LanguageString> map = textData.getMultilanguageText();
|
216
|
|
217
|
String result = textData.getText(Language.DEFAULT());
|
218
|
if (result == null){
|
219
|
Map<Language, LanguageString> map = textData.getMultilanguageText();
|
220
|
for (Language language : map.keySet()){
|
221
|
String tmp = textData.getText(language);
|
222
|
if (! CdmUtils.Nz(tmp).trim().equals("")){
|
223
|
result = tmp;
|
224
|
break;
|
225
|
}
|
226
|
}
|
227
|
}
|
228
|
return result;
|
229
|
}
|
230
|
|
231
|
private static Map<Feature, Integer> featureMap = new HashMap<Feature, Integer>();
|
232
|
|
233
|
@Deprecated //TODO quick and dirty for Salvador export
|
234
|
private static Integer findCategory(Feature feature){
|
235
|
if (featureMap.get(feature) != null){
|
236
|
return featureMap.get(feature);
|
237
|
}
|
238
|
Integer result = null;
|
239
|
String label = feature.getLabel();
|
240
|
ResultSet rs = source.getResultSet("SELECT FactCategoryId FROM FactCategory WHERE FactCategory = '"+label+"'");
|
241
|
try {
|
242
|
while (rs.next()){
|
243
|
if (result != null){
|
244
|
logger.warn("FactCategory is not distinct: " + label);
|
245
|
}else{
|
246
|
result = rs.getInt(1) ;
|
247
|
}
|
248
|
}
|
249
|
} catch (SQLException e) {
|
250
|
logger.error(e.getMessage());
|
251
|
e.printStackTrace();
|
252
|
return null;
|
253
|
}
|
254
|
featureMap.put(feature, result);
|
255
|
return result;
|
256
|
}
|
257
|
|
258
|
|
259
|
/* (non-Javadoc)
|
260
|
* @see eu.etaxonomy.cdm.io.berlinModel.out.BerlinModelExportBase#getStandardMethodParameter()
|
261
|
*/
|
262
|
@Override
|
263
|
public Class<? extends CdmBase> getStandardMethodParameter() {
|
264
|
return standardMethodParameter;
|
265
|
}
|
266
|
}
|