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.SQLException;
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.List;
|
14
|
|
15
|
import org.apache.log4j.Logger;
|
16
|
import org.springframework.stereotype.Component;
|
17
|
import org.springframework.transaction.TransactionStatus;
|
18
|
|
19
|
import eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer;
|
20
|
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelTaxonNameImport;
|
21
|
import eu.etaxonomy.cdm.io.berlinModel.out.mapper.RefDetailMapper;
|
22
|
import eu.etaxonomy.cdm.io.berlinModel.out.mapper.TeamOrPersonMapper;
|
23
|
import eu.etaxonomy.cdm.io.common.Source;
|
24
|
import eu.etaxonomy.cdm.io.common.mapping.out.CdmDbExportMapping;
|
25
|
import eu.etaxonomy.cdm.io.common.mapping.out.CollectionExportMapping;
|
26
|
import eu.etaxonomy.cdm.io.common.mapping.out.CreatedAndNotesMapper;
|
27
|
import eu.etaxonomy.cdm.io.common.mapping.out.DbBooleanMapper;
|
28
|
import eu.etaxonomy.cdm.io.common.mapping.out.DbExtensionMapper;
|
29
|
import eu.etaxonomy.cdm.io.common.mapping.out.DbMarkerMapper;
|
30
|
import eu.etaxonomy.cdm.io.common.mapping.out.DbObjectMapper;
|
31
|
import eu.etaxonomy.cdm.io.common.mapping.out.DbStringMapper;
|
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.ExtensionType;
|
37
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
38
|
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
|
39
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
40
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
41
|
|
42
|
|
43
|
/**
|
44
|
* @author a.mueller
|
45
|
* @created 20.03.2008
|
46
|
* @version 1.0
|
47
|
*/
|
48
|
@Component
|
49
|
public class BerlinModelTaxonNameExport extends BerlinModelExportBase<TaxonNameBase> {
|
50
|
private static final Logger logger = Logger.getLogger(BerlinModelTaxonNameExport.class);
|
51
|
|
52
|
private static int modCount = 2500;
|
53
|
private static final String dbTableName = "Name";
|
54
|
private static final String pluralString = "TaxonNames";
|
55
|
private static final Class<? extends CdmBase> standardMethodParameter = NonViralName.class;
|
56
|
|
57
|
public BerlinModelTaxonNameExport(){
|
58
|
super();
|
59
|
}
|
60
|
|
61
|
/* (non-Javadoc)
|
62
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
63
|
*/
|
64
|
@Override
|
65
|
protected boolean doCheck(BerlinModelExportState state){
|
66
|
boolean result = true;
|
67
|
logger.warn("Checking for " + pluralString + " not yet fully implemented");
|
68
|
List<TaxonNameBase> list = getObjectList();
|
69
|
checkRank(list);
|
70
|
|
71
|
//result &= checkRank(config);
|
72
|
|
73
|
return result;
|
74
|
}
|
75
|
|
76
|
private boolean checkRank(List<TaxonNameBase> list){
|
77
|
List<TaxonNameBase> errorNames = new ArrayList<TaxonNameBase>();
|
78
|
for (TaxonNameBase<?,?> name : list){
|
79
|
if (name.getRank() == null);
|
80
|
errorNames.add(name);
|
81
|
}
|
82
|
if (errorNames.size() >0){
|
83
|
System.out.println("The following names have no Rank:\n=======================");
|
84
|
for (TaxonNameBase<?,?> name : errorNames){
|
85
|
System.out.println(" " + name.toString());
|
86
|
System.out.println(" " + name.getUuid());
|
87
|
System.out.println(" " + name.getTitleCache());
|
88
|
}
|
89
|
return false;
|
90
|
}else{
|
91
|
return true;
|
92
|
}
|
93
|
}
|
94
|
|
95
|
private CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> getMapping(){
|
96
|
String tableName = dbTableName;
|
97
|
CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> mapping = new CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer>(tableName);
|
98
|
mapping.addMapper(IdMapper.NewInstance("NameId"));
|
99
|
mapping.addMapper(MethodMapper.NewInstance("RankFk", this));
|
100
|
mapping.addMapper(MethodMapper.NewInstance("SupraGenericName", this));
|
101
|
mapping.addMapper(MethodMapper.NewInstance("Genus", this));
|
102
|
mapping.addMapper(MethodMapper.NewInstance("NameCache", this));
|
103
|
mapping.addMapper(MethodMapper.NewInstance("FullNameCache", this));
|
104
|
mapping.addMapper(MethodMapper.NewInstance("PreliminaryFlag", this));
|
105
|
mapping.addMapper(DbStringMapper.NewInstance("infraGenericEpithet", "GenusSubDivisionEpi"));
|
106
|
mapping.addMapper(DbStringMapper.NewInstance("SpecificEpithet", "SpeciesEpi"));
|
107
|
mapping.addMapper(DbStringMapper.NewInstance("infraSpecificEpithet", "InfraSpeciesEpi"));
|
108
|
mapping.addMapper(DbStringMapper.NewInstance("appendedPhrase", "UnnamedNamePhrase"));
|
109
|
mapping.addMapper(DbBooleanMapper.NewInstance("isHybridFormula", "HybridFormulaFlag", false, false));
|
110
|
mapping.addMapper(DbBooleanMapper.NewInstance("isMonomHybrid", "MonomHybFlag", false, false));
|
111
|
mapping.addMapper(DbBooleanMapper.NewInstance("isBinomHybrid", "BinomHybFlag", false, false));
|
112
|
mapping.addMapper(DbBooleanMapper.NewInstance("isTrinomHybrid", "TrinomHybFlag", false, false));
|
113
|
mapping.addMapper(DbStringMapper.NewFacultativeInstance("cultivarName", "CultivarName"));
|
114
|
|
115
|
mapping.addMapper(TeamOrPersonMapper.NewInstance("combinationAuthorTeam", "AuthorTeamFk"));
|
116
|
mapping.addMapper(TeamOrPersonMapper.NewInstance("exCombinationAuthorTeam", "ExAuthorTeamFk"));
|
117
|
mapping.addMapper(TeamOrPersonMapper.NewInstance("basionymAuthorTeam", "BasAuthorTeamFk"));
|
118
|
mapping.addMapper(TeamOrPersonMapper.NewInstance("exBasionymAuthorTeam", "ExBasAuthorTeamFk"));
|
119
|
|
120
|
mapping.addMapper(DbObjectMapper.NewInstance("nomenclaturalReference", "NomRefFk"));
|
121
|
mapping.addMapper(RefDetailMapper.NewInstance("nomenclaturalMicroReference","nomenclaturalReference", "NomRefDetailFk"));
|
122
|
mapping.addMapper(CreatedAndNotesMapper.NewInstance(false));
|
123
|
ExtensionType sourceAccExtensionType = (ExtensionType)getTermService().find(BerlinModelTaxonNameImport.SOURCE_ACC_UUID);
|
124
|
if (sourceAccExtensionType != null){
|
125
|
mapping.addMapper(DbExtensionMapper.NewInstance(sourceAccExtensionType, "Source_Acc"));
|
126
|
}
|
127
|
mapping.addCollectionMapping(getNomStatusMapping());
|
128
|
|
129
|
|
130
|
|
131
|
//TODO
|
132
|
//CultivarGroupName
|
133
|
//NameSourceRefFk
|
134
|
// ,[Source_ACC]
|
135
|
|
136
|
//publicationYear
|
137
|
//originalPublicationYear
|
138
|
//breed
|
139
|
NonViralName<?> n = null;
|
140
|
//n.getNomenclaturalMicroReference()
|
141
|
return mapping;
|
142
|
}
|
143
|
|
144
|
private CollectionExportMapping getNomStatusMapping(){
|
145
|
String tableName = "NomStatusRel";
|
146
|
String collectionAttribute = "status";
|
147
|
IdMapper parentMapper = IdMapper.NewInstance("NameFk");
|
148
|
CollectionExportMapping mapping = CollectionExportMapping.NewInstance(tableName, collectionAttribute, parentMapper);
|
149
|
mapping.addMapper(MethodMapper.NewInstance("NomStatusFk", this.getClass(), "getNomStatusFk", NomenclaturalStatus.class));
|
150
|
mapping.addMapper(DbObjectMapper.NewInstance("citation", "NomStatusRefFk"));
|
151
|
mapping.addMapper(RefDetailMapper.NewInstance("citationMicroReference","citation", "NomStatusRefDetailFk"));
|
152
|
mapping.addMapper(DbMarkerMapper.NewInstance(MarkerType.IS_DOUBTFUL(), "DoubtfulFlag", false));
|
153
|
mapping.addMapper(CreatedAndNotesMapper.NewInstance());
|
154
|
|
155
|
return mapping;
|
156
|
}
|
157
|
|
158
|
protected void doInvoke(BerlinModelExportState state){
|
159
|
try{
|
160
|
logger.info("start make "+pluralString+" ...");
|
161
|
boolean success = true ;
|
162
|
doDelete(state);
|
163
|
|
164
|
TransactionStatus txStatus = startTransaction(true);
|
165
|
logger.info("load "+pluralString+" ...");
|
166
|
List<TaxonNameBase> names = getObjectList();
|
167
|
|
168
|
CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> mapping = getMapping();
|
169
|
mapping.initialize(state);
|
170
|
logger.info("save "+pluralString+" ...");
|
171
|
int count = 0;
|
172
|
for (TaxonNameBase<?,?> name : names){
|
173
|
doCount(count++, modCount, pluralString);
|
174
|
success &= mapping.invoke(name);
|
175
|
//TODO rank = null or rank < genus and genusOrUninomial != null
|
176
|
}
|
177
|
commitTransaction(txStatus);
|
178
|
logger.info("end make " + pluralString+ " ..." + getSuccessString(success));
|
179
|
|
180
|
if (!success){
|
181
|
state.setUnsuccessfull();
|
182
|
}
|
183
|
return;
|
184
|
}catch(SQLException e){
|
185
|
e.printStackTrace();
|
186
|
logger.error(e.getMessage());
|
187
|
state.setUnsuccessfull();
|
188
|
return;
|
189
|
}
|
190
|
}
|
191
|
|
192
|
protected List<TaxonNameBase> getObjectList(){
|
193
|
List<TaxonNameBase> list = getNameService().list(null,100000000, null,null,null);
|
194
|
return list;
|
195
|
}
|
196
|
|
197
|
|
198
|
protected boolean doDelete(BerlinModelExportState state){
|
199
|
BerlinModelExportConfigurator bmeConfig = state.getConfig();
|
200
|
|
201
|
String sql;
|
202
|
Source destination = bmeConfig.getDestination();
|
203
|
//RelPTaxon
|
204
|
sql = "DELETE FROM RelPTaxon";
|
205
|
destination.setQuery(sql);
|
206
|
destination.update(sql);
|
207
|
//Fact
|
208
|
sql = "DELETE FROM Fact";
|
209
|
destination.setQuery(sql);
|
210
|
destination.update(sql);
|
211
|
//PTaxon
|
212
|
sql = "DELETE FROM PTaxon";
|
213
|
destination.setQuery(sql);
|
214
|
destination.update(sql);
|
215
|
|
216
|
//NameHistory
|
217
|
sql = "DELETE FROM NameHistory";
|
218
|
destination.setQuery(sql);
|
219
|
destination.update(sql);
|
220
|
//RelName
|
221
|
sql = "DELETE FROM RelName";
|
222
|
destination.setQuery(sql);
|
223
|
destination.update(sql);
|
224
|
//NomStatusRel
|
225
|
sql = "DELETE FROM NomStatusRel";
|
226
|
destination.setQuery(sql);
|
227
|
destination.update(sql);
|
228
|
//Name
|
229
|
sql = "DELETE FROM Name";
|
230
|
destination.setQuery(sql);
|
231
|
destination.update(sql);
|
232
|
return true;
|
233
|
}
|
234
|
|
235
|
|
236
|
/* (non-Javadoc)
|
237
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
238
|
*/
|
239
|
protected boolean isIgnore(BerlinModelExportState state){
|
240
|
return ! state.getConfig().isDoTaxonNames();
|
241
|
}
|
242
|
|
243
|
//called by MethodMapper
|
244
|
@SuppressWarnings("unused")
|
245
|
private static Integer getRankFk(NonViralName<?> name){
|
246
|
Integer result = BerlinModelTransformer.rank2RankId(name.getRank());
|
247
|
if (result == null){
|
248
|
logger.warn ("Rank = null is not allowed in Berlin Model. Rank was changed to KINGDOM: " + name);
|
249
|
result = 1;
|
250
|
}
|
251
|
return result;
|
252
|
}
|
253
|
|
254
|
//called by MethodMapper
|
255
|
@SuppressWarnings("unused")
|
256
|
private static Integer getNomStatusFk(NomenclaturalStatus status){
|
257
|
return BerlinModelTransformer.nomStatus2nomStatusFk(status.getType());
|
258
|
}
|
259
|
|
260
|
//called by MethodMapper
|
261
|
@SuppressWarnings("unused")
|
262
|
private static String getSupraGenericName(NonViralName<?> name){
|
263
|
if (name.isSupraGeneric()){
|
264
|
return name.getGenusOrUninomial();
|
265
|
}else{
|
266
|
return null;
|
267
|
}
|
268
|
}
|
269
|
|
270
|
//called by MethodMapper
|
271
|
@SuppressWarnings("unused")
|
272
|
private static String getGenus(NonViralName<?> name){
|
273
|
if (! name.isSupraGeneric()){
|
274
|
return name.getGenusOrUninomial();
|
275
|
}else{
|
276
|
return null;
|
277
|
}
|
278
|
}
|
279
|
|
280
|
//called by MethodMapper
|
281
|
@SuppressWarnings("unused")
|
282
|
private static String getNameCache(NonViralName<?> name){
|
283
|
if (name.isProtectedNameCache()){
|
284
|
return name.getNameCache();
|
285
|
}else{
|
286
|
return null;
|
287
|
}
|
288
|
}
|
289
|
|
290
|
//called by MethodMapper
|
291
|
@SuppressWarnings("unused")
|
292
|
private static String getFullNameCache(NonViralName<?> name){
|
293
|
if (name.isProtectedTitleCache()){
|
294
|
return name.getTitleCache();
|
295
|
}else{
|
296
|
return null;
|
297
|
}
|
298
|
}
|
299
|
|
300
|
//called by MethodMapper
|
301
|
@SuppressWarnings("unused")
|
302
|
private static Boolean getPreliminaryFlag(NonViralName<?> name){
|
303
|
if (name.isProtectedTitleCache() || name.isProtectedNameCache()){
|
304
|
if (name.isProtectedTitleCache() && name.isProtectedNameCache()){
|
305
|
logger.warn("protectedTitleCache and protectedNameCache do not have the same value for name " + name.getTitleCache() + ". This can not be mapped appropriately to the Berlin Model ");
|
306
|
}
|
307
|
return true;
|
308
|
}else{
|
309
|
return false;
|
310
|
}
|
311
|
}
|
312
|
|
313
|
|
314
|
|
315
|
/* (non-Javadoc)
|
316
|
* @see eu.etaxonomy.cdm.io.berlinModel.out.BerlinModelExportBase#getStandardMethodParameter()
|
317
|
*/
|
318
|
@Override
|
319
|
public Class<? extends CdmBase> getStandardMethodParameter() {
|
320
|
return standardMethodParameter;
|
321
|
}
|
322
|
|
323
|
}
|