1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2009 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.io.pesi.out;
|
11
|
|
12
|
import java.sql.SQLException;
|
13
|
import java.util.List;
|
14
|
import java.util.Set;
|
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.out.mapper.MethodMapper;
|
21
|
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
|
22
|
import eu.etaxonomy.cdm.io.common.Source;
|
23
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
24
|
import eu.etaxonomy.cdm.model.common.RelationshipBase;
|
25
|
import eu.etaxonomy.cdm.model.name.NameRelationship;
|
26
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
27
|
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
|
28
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
29
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
|
30
|
|
31
|
/**
|
32
|
* @author a.mueller
|
33
|
* @author e.-m.lee
|
34
|
* @date 23.02.2010
|
35
|
*
|
36
|
*/
|
37
|
@Component
|
38
|
@SuppressWarnings("unchecked")
|
39
|
public class PesiRelTaxonExport extends PesiExportBase {
|
40
|
private static final Logger logger = Logger.getLogger(PesiRelTaxonExport.class);
|
41
|
private static final Class<? extends CdmBase> standardMethodParameter = RelationshipBase.class;
|
42
|
|
43
|
private static int modCount = 1000;
|
44
|
private static final String dbTableName = "RelTaxon";
|
45
|
private static final String pluralString = "Relationships";
|
46
|
|
47
|
public PesiRelTaxonExport() {
|
48
|
super();
|
49
|
}
|
50
|
|
51
|
/* (non-Javadoc)
|
52
|
* @see eu.etaxonomy.cdm.io.common.DbExportBase#getStandardMethodParameter()
|
53
|
*/
|
54
|
@Override
|
55
|
public Class<? extends CdmBase> getStandardMethodParameter() {
|
56
|
return standardMethodParameter;
|
57
|
}
|
58
|
|
59
|
/* (non-Javadoc)
|
60
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
|
61
|
*/
|
62
|
@Override
|
63
|
protected boolean doCheck(PesiExportState state) {
|
64
|
boolean result = true;
|
65
|
return result;
|
66
|
}
|
67
|
|
68
|
/* (non-Javadoc)
|
69
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
|
70
|
*/
|
71
|
@Override
|
72
|
protected boolean doInvoke(PesiExportState state) {
|
73
|
try {
|
74
|
logger.error("*** Started Making " + pluralString + " ...");
|
75
|
|
76
|
// Get the limit for objects to save within a single transaction.
|
77
|
int limit = state.getConfig().getLimitSave();
|
78
|
|
79
|
// Stores whether this invoke was successful or not.
|
80
|
boolean success = true;
|
81
|
|
82
|
// PESI: Clear the database table RelTaxon.
|
83
|
doDelete(state);
|
84
|
|
85
|
// Get specific mappings: (CDM) Relationship -> (PESI) RelTaxon
|
86
|
PesiExportMapping mapping = getMapping();
|
87
|
|
88
|
// Initialize the db mapper
|
89
|
mapping.initialize(state);
|
90
|
|
91
|
// PESI: Create the RelTaxa
|
92
|
int count = 0;
|
93
|
int taxonCount = 0;
|
94
|
int pastCount = 0;
|
95
|
TransactionStatus txStatus = null;
|
96
|
List<RelationshipBase> list = null;
|
97
|
|
98
|
// Start transaction
|
99
|
txStatus = startTransaction(true);
|
100
|
logger.error("Started new transaction. Fetching some " + pluralString + " (max: " + limit + ") ...");
|
101
|
while ((list = getTaxonService().getAllRelationships(limit, taxonCount)).size() > 0) {
|
102
|
|
103
|
taxonCount += list.size();
|
104
|
logger.error("Fetched " + list.size() + " " + pluralString + ". Exporting...");
|
105
|
for (RelationshipBase<?, ?, ?> relation : list) {
|
106
|
|
107
|
// Focus on TaxonRelationships and SynonymRelationships.
|
108
|
if (relation.isInstanceOf(TaxonRelationship.class) || relation.isInstanceOf(SynonymRelationship.class)) {
|
109
|
doCount(count++, modCount, pluralString);
|
110
|
success &= mapping.invoke(relation);
|
111
|
}
|
112
|
}
|
113
|
|
114
|
// Commit transaction
|
115
|
commitTransaction(txStatus);
|
116
|
logger.error("Committed transaction.");
|
117
|
logger.error("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
|
118
|
pastCount = count;
|
119
|
|
120
|
// Start transaction
|
121
|
txStatus = startTransaction(true);
|
122
|
logger.error("Started new transaction. Fetching some " + pluralString + " (max: " + limit + ") ...");
|
123
|
}
|
124
|
if (list.size() == 0) {
|
125
|
logger.error("No " + pluralString + " left to fetch.");
|
126
|
}
|
127
|
// Commit transaction
|
128
|
commitTransaction(txStatus);
|
129
|
logger.error("Committed transaction.");
|
130
|
|
131
|
logger.error("*** Finished Making " + pluralString + " ..." + getSuccessString(success));
|
132
|
|
133
|
|
134
|
count = 0;
|
135
|
taxonCount = 0;
|
136
|
pastCount = 0;
|
137
|
// Start transaction
|
138
|
List<TaxonBase> taxonBaseList = null;
|
139
|
txStatus = startTransaction(true);
|
140
|
String nameRelationString = "NameRelationships";
|
141
|
logger.error("Started new transaction. Fetching some " + nameRelationString + " (max: " + limit + ") ...");
|
142
|
while ((taxonBaseList = getTaxonService().list(null, limit, count, null, null)).size() > 0) {
|
143
|
|
144
|
logger.error("Fetched " + taxonBaseList.size() + " " + nameRelationString + ". Exporting...");
|
145
|
for (TaxonBase taxonBase : taxonBaseList) {
|
146
|
doCount(count++, modCount, nameRelationString);
|
147
|
|
148
|
Set<NameRelationship> nameRelations = taxonBase.getName().getNameRelations();
|
149
|
for (NameRelationship nameRelationship : nameRelations) {
|
150
|
success &= mapping.invoke(nameRelationship);
|
151
|
}
|
152
|
}
|
153
|
|
154
|
// Commit transaction
|
155
|
commitTransaction(txStatus);
|
156
|
logger.error("Committed transaction.");
|
157
|
logger.error("Exported " + (count - pastCount) + " " + nameRelationString + ". Total: " + count);
|
158
|
pastCount = count;
|
159
|
|
160
|
// Start transaction
|
161
|
txStatus = startTransaction(true);
|
162
|
logger.error("Started new transaction. Fetching some " + nameRelationString + " (max: " + limit + ") ...");
|
163
|
}
|
164
|
if (list.size() == 0) {
|
165
|
logger.error("No " + nameRelationString + " left to fetch.");
|
166
|
}
|
167
|
// Commit transaction
|
168
|
commitTransaction(txStatus);
|
169
|
logger.error("Committed transaction.");
|
170
|
|
171
|
|
172
|
return success;
|
173
|
} catch (SQLException e) {
|
174
|
e.printStackTrace();
|
175
|
logger.error(e.getMessage());
|
176
|
return false;
|
177
|
}
|
178
|
}
|
179
|
|
180
|
/**
|
181
|
* Deletes all entries of database tables related to <code>RelTaxon</code>.
|
182
|
* @param state The {@link PesiExportState PesiExportState}.
|
183
|
* @return Whether the delete operation was successful or not.
|
184
|
*/
|
185
|
protected boolean doDelete(PesiExportState state) {
|
186
|
PesiExportConfigurator pesiConfig = (PesiExportConfigurator) state.getConfig();
|
187
|
|
188
|
String sql;
|
189
|
Source destination = pesiConfig.getDestination();
|
190
|
|
191
|
// Clear RelTaxon
|
192
|
sql = "DELETE FROM " + dbTableName;
|
193
|
destination.setQuery(sql);
|
194
|
destination.update(sql);
|
195
|
return true;
|
196
|
}
|
197
|
|
198
|
/* (non-Javadoc)
|
199
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
|
200
|
*/
|
201
|
@Override
|
202
|
protected boolean isIgnore(PesiExportState state) {
|
203
|
return ! state.getConfig().isDoRelTaxa();
|
204
|
}
|
205
|
|
206
|
/**
|
207
|
* Returns the <code>TaxonFk1</code> attribute. It corresponds to a CDM <code>TaxonRelationship</code>.
|
208
|
* @param relationship The {@link RelationshipBase Relationship}.
|
209
|
* @param state The {@link DbExportStateBase DbExportState}.
|
210
|
* @return The <code>TaxonFk1</code> attribute.
|
211
|
* @see MethodMapper
|
212
|
*/
|
213
|
@SuppressWarnings("unused")
|
214
|
private static Integer getTaxonFk1(RelationshipBase<?, ?, ?> relationship, DbExportStateBase<?> state) {
|
215
|
return getObjectFk(relationship, state, true);
|
216
|
}
|
217
|
|
218
|
/**
|
219
|
* Returns the <code>TaxonFk2</code> attribute. It corresponds to a CDM <code>SynonymRelationship</code>.
|
220
|
* @param relationship The {@link RelationshipBase Relationship}.
|
221
|
* @param state The {@link DbExportStateBase DbExportState}.
|
222
|
* @return The <code>TaxonFk2</code> attribute.
|
223
|
* @see MethodMapper
|
224
|
*/
|
225
|
@SuppressWarnings("unused")
|
226
|
private static Integer getTaxonFk2(RelationshipBase<?, ?, ?> relationship, DbExportStateBase<?> state) {
|
227
|
return getObjectFk(relationship, state, false);
|
228
|
}
|
229
|
|
230
|
/**
|
231
|
* Returns the <code>RelTaxonQualifierFk</code> attribute.
|
232
|
* @param relationship The {@link RelationshipBase Relationship}.
|
233
|
* @return The <code>RelTaxonQualifierFk</code> attribute.
|
234
|
* @see MethodMapper
|
235
|
*/
|
236
|
@SuppressWarnings("unused")
|
237
|
private static Integer getRelTaxonQualifierFk(RelationshipBase<?, ?, ?> relationship) {
|
238
|
return PesiTransformer.taxonRelation2RelTaxonQualifierFk(relationship);
|
239
|
}
|
240
|
|
241
|
/**
|
242
|
* Returns the <code>RelQualifierCache</code> attribute.
|
243
|
* @param relationship The {@link RelationshipBase Relationship}.
|
244
|
* @return The <code>RelQualifierCache</code> attribute.
|
245
|
* @see MethodMapper
|
246
|
*/
|
247
|
@SuppressWarnings("unused")
|
248
|
private static String getRelQualifierCache(RelationshipBase<?, ?, ?> relationship) {
|
249
|
return PesiTransformer.taxonRelation2RelTaxonQualifierCache(relationship);
|
250
|
}
|
251
|
|
252
|
/**
|
253
|
* Returns the <code>Notes</code> attribute.
|
254
|
* @param relationship The {@link RelationshipBase Relationship}.
|
255
|
* @return The <code>Notes</code> attribute.
|
256
|
* @see MethodMapper
|
257
|
*/
|
258
|
@SuppressWarnings("unused")
|
259
|
private static String getNotes(RelationshipBase<?, ?, ?> relationship) {
|
260
|
// TODO
|
261
|
return null;
|
262
|
}
|
263
|
|
264
|
/**
|
265
|
* Returns the database key of an object in the given relationship.
|
266
|
* @param relationship {@link RelationshipBase RelationshipBase}.
|
267
|
* @param state {@link DbExportStateBase DbExportStateBase}.
|
268
|
* @param isFrom A boolean value indicating whether the database key of the parent or child in this relationship is searched. <code>true</code> means the child is searched. <code>false</code> means the parent is searched.
|
269
|
* @return The database key of an object in the given relationship.
|
270
|
*/
|
271
|
private static Integer getObjectFk(RelationshipBase<?, ?, ?> relationship, DbExportStateBase<?> state, boolean isFrom) {
|
272
|
TaxonBase<?> taxon = null;
|
273
|
if (relationship.isInstanceOf(TaxonRelationship.class)) {
|
274
|
TaxonRelationship tr = (TaxonRelationship)relationship;
|
275
|
taxon = (isFrom) ? tr.getFromTaxon(): tr.getToTaxon();
|
276
|
} else if (relationship.isInstanceOf(SynonymRelationship.class)) {
|
277
|
SynonymRelationship sr = (SynonymRelationship)relationship;
|
278
|
taxon = (isFrom) ? sr.getSynonym() : sr.getAcceptedTaxon();
|
279
|
} else if (relationship.isInstanceOf(NameRelationship.class)) {
|
280
|
NameRelationship nr = (NameRelationship)relationship;
|
281
|
TaxonNameBase taxonName = (isFrom) ? nr.getFromName() : nr.getToName();
|
282
|
return state.getDbId(taxonName);
|
283
|
|
284
|
// Set taxa = taxonName.getTaxa();
|
285
|
// if (taxa.size() == 0) {
|
286
|
// logger.warn("This TaxonName has no Taxon: " + taxonName.getTitleCache());
|
287
|
// return state.getDbId(taxonName);
|
288
|
// } else if (taxa.size() == 1) {
|
289
|
// taxon = (TaxonBase<?>) taxa.iterator().next();
|
290
|
// } else if (taxa.size() > 1) {
|
291
|
// logger.warn("This TaxonName has " + taxa.size() + " Taxa: " + taxonName.getTitleCache());
|
292
|
// }
|
293
|
}
|
294
|
if (taxon != null) {
|
295
|
return state.getDbId(taxon.getName());
|
296
|
}
|
297
|
logger.warn("No taxon found in state for relationship: " + relationship.toString());
|
298
|
return null;
|
299
|
}
|
300
|
|
301
|
/**
|
302
|
* Returns the CDM to PESI specific export mappings.
|
303
|
* @return The {@link PesiExportMapping PesiExportMapping}.
|
304
|
*/
|
305
|
private PesiExportMapping getMapping() {
|
306
|
PesiExportMapping mapping = new PesiExportMapping(dbTableName);
|
307
|
|
308
|
// mapping.addMapper(IdMapper.NewInstance("RelTaxonId")); // Automagically generated on database level as primary key
|
309
|
mapping.addMapper(MethodMapper.NewInstance("TaxonFk1", this.getClass(), "getTaxonFk1", standardMethodParameter, DbExportStateBase.class));
|
310
|
mapping.addMapper(MethodMapper.NewInstance("TaxonFk2", this.getClass(), "getTaxonFk2", standardMethodParameter, DbExportStateBase.class));
|
311
|
mapping.addMapper(MethodMapper.NewInstance("RelTaxonQualifierFk", this));
|
312
|
mapping.addMapper(MethodMapper.NewInstance("RelQualifierCache", this));
|
313
|
mapping.addMapper(MethodMapper.NewInstance("Notes", this));
|
314
|
// mapping.addMapper(CreatedAndNotesMapper.NewInstance(false));
|
315
|
|
316
|
return mapping;
|
317
|
}
|
318
|
|
319
|
}
|