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.HashSet;
|
14
|
import java.util.List;
|
15
|
import java.util.Set;
|
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.io.berlinModel.BerlinModelTransformer;
|
22
|
import eu.etaxonomy.cdm.io.berlinModel.out.mapper.CreatedAndNotesMapper;
|
23
|
import eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbObjectMapper;
|
24
|
import eu.etaxonomy.cdm.io.berlinModel.out.mapper.IdMapper;
|
25
|
import eu.etaxonomy.cdm.io.berlinModel.out.mapper.MethodMapper;
|
26
|
import eu.etaxonomy.cdm.io.berlinModel.out.mapper.RefDetailMapper;
|
27
|
import eu.etaxonomy.cdm.io.common.Source;
|
28
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
29
|
import eu.etaxonomy.cdm.model.common.RelationshipBase;
|
30
|
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
|
31
|
import eu.etaxonomy.cdm.model.name.HybridRelationship;
|
32
|
import eu.etaxonomy.cdm.model.name.NameRelationship;
|
33
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
34
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
35
|
|
36
|
|
37
|
/**
|
38
|
* @author a.mueller
|
39
|
* @created 20.03.2008
|
40
|
* @version 1.0
|
41
|
*/
|
42
|
@Component
|
43
|
public class BerlinModelNameRelationExport extends BerlinModelExportBase<RelationshipBase> {
|
44
|
private static final Logger logger = Logger.getLogger(BerlinModelNameRelationExport.class);
|
45
|
|
46
|
private static int modCount = 100;
|
47
|
private static final String dbTableName = "RelName";
|
48
|
private static final String pluralString = "NameRelationships";
|
49
|
private static final Class<? extends CdmBase> standardMethodParameter = RelationshipBase.class;
|
50
|
|
51
|
|
52
|
public BerlinModelNameRelationExport(){
|
53
|
super();
|
54
|
}
|
55
|
|
56
|
/* (non-Javadoc)
|
57
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
58
|
*/
|
59
|
@Override
|
60
|
protected boolean doCheck(BerlinModelExportState state){
|
61
|
boolean result = true;
|
62
|
logger.warn("Checking for " + pluralString + " not yet implemented");
|
63
|
//result &= checkArticlesWithoutJournal(bmiConfig);
|
64
|
//result &= checkPartOfJournal(bmiConfig);
|
65
|
|
66
|
return result;
|
67
|
}
|
68
|
|
69
|
private BerlinModelExportMapping getMapping(){
|
70
|
String tableName = dbTableName;
|
71
|
BerlinModelExportMapping mapping = new BerlinModelExportMapping(tableName);
|
72
|
mapping.addMapper(IdMapper.NewInstance("RelNameId"));
|
73
|
|
74
|
mapping.addMapper(DbObjectMapper.NewInstance("fromName", "NameFk1"));
|
75
|
mapping.addMapper(DbObjectMapper.NewInstance("toName", "NameFk2"));
|
76
|
|
77
|
|
78
|
mapping.addMapper(MethodMapper.NewInstance("RelNameQualifierFk", this));
|
79
|
|
80
|
mapping.addMapper(DbObjectMapper.NewInstance("citation", "RefFk"));
|
81
|
mapping.addMapper(RefDetailMapper.NewInstance("citationMicroReference","citation", "RefDetailFk"));
|
82
|
mapping.addMapper(CreatedAndNotesMapper.NewInstance());
|
83
|
|
84
|
return mapping;
|
85
|
}
|
86
|
|
87
|
protected void doInvoke(BerlinModelExportState state){
|
88
|
try{
|
89
|
logger.info("start make " + pluralString + " ...");
|
90
|
boolean success = true ;
|
91
|
doDelete(state);
|
92
|
|
93
|
TransactionStatus txStatus = startTransaction(true);
|
94
|
|
95
|
List<RelationshipBase> list = getNameService().getAllRelationships(100000000, 0);
|
96
|
|
97
|
BerlinModelExportMapping mapping = getMapping();
|
98
|
mapping.initialize(state);
|
99
|
|
100
|
int count = 0;
|
101
|
for (RelationshipBase<?,?,?> rel : list){
|
102
|
if (rel.isInstanceOf(NameRelationship.class) || rel.isInstanceOf(HybridRelationship.class )){
|
103
|
doCount(count++, modCount, pluralString);
|
104
|
success &= mapping.invoke(rel);
|
105
|
}
|
106
|
}
|
107
|
commitTransaction(txStatus);
|
108
|
|
109
|
success &= makeIsHomotypicRelation(state, mapping);
|
110
|
|
111
|
logger.info("end make " + pluralString + " ..." + getSuccessString(success));
|
112
|
if (!success){
|
113
|
state.setUnsuccessfull();
|
114
|
}
|
115
|
return;
|
116
|
}catch(SQLException e){
|
117
|
e.printStackTrace();
|
118
|
logger.error(e.getMessage());
|
119
|
state.setUnsuccessfull();
|
120
|
return;
|
121
|
}
|
122
|
}
|
123
|
|
124
|
|
125
|
private boolean makeIsHomotypicRelation(BerlinModelExportState state, BerlinModelExportMapping mapping){
|
126
|
boolean success = true ;
|
127
|
try{
|
128
|
Integer homotypicId = state.getConfig().getIsHomotypicId();
|
129
|
if (homotypicId == null){
|
130
|
return success;
|
131
|
}
|
132
|
logger.info("start make IsHomotypicRelations ...");
|
133
|
|
134
|
TransactionStatus txStatus = startTransaction(true);
|
135
|
|
136
|
List<HomotypicalGroup> list = getNameService().getAllHomotypicalGroups(100000000, 0);
|
137
|
|
138
|
int count = 0;
|
139
|
modCount = 1000;
|
140
|
Set<NameRelationship> basionymNameRels = new HashSet<NameRelationship>();
|
141
|
for (HomotypicalGroup homoGroup : list){
|
142
|
doCount(count++, modCount, "homotypical groups");
|
143
|
Set<TaxonNameBase> allNames = homoGroup.getTypifiedNames();
|
144
|
if (allNames.size() > 1){
|
145
|
Set<TaxonNameBase> readyNames = new HashSet<TaxonNameBase>();
|
146
|
Set<TaxonNameBase> unrelateds = homoGroup.getUnrelatedNames();
|
147
|
for (TaxonNameBase unrelated : unrelateds){
|
148
|
for (TaxonNameBase oneOfAllNames: allNames){
|
149
|
if(!unrelated.equals(oneOfAllNames) && ! readyNames.contains(oneOfAllNames)){
|
150
|
success &= invokeIsHomotypic(state, mapping, unrelated, oneOfAllNames, null, null);
|
151
|
}
|
152
|
}
|
153
|
readyNames.add(unrelated);
|
154
|
}
|
155
|
}
|
156
|
}
|
157
|
commitTransaction(txStatus);
|
158
|
|
159
|
logger.info("end make homotypical groups ... " + getSuccessString(success));
|
160
|
return success;
|
161
|
}catch(SQLException e){
|
162
|
logger.error(e.getMessage());
|
163
|
e.printStackTrace();
|
164
|
return false;
|
165
|
}
|
166
|
}
|
167
|
|
168
|
private boolean invokeIsHomotypic(BerlinModelExportState state, BerlinModelExportMapping mapping, TaxonNameBase fromName, TaxonNameBase toName, Reference refId, String microCitation) throws SQLException{
|
169
|
try{
|
170
|
logger.info(fromName.getTitleCache() + "->" + toName.getTitleCache());
|
171
|
String maxQuery = " SELECT max(relNameId) as max FROM relName ";
|
172
|
ResultSet rs = state.getConfig().getDestination().getResultSet(maxQuery);
|
173
|
int maxId = 1;
|
174
|
if (rs.next()){
|
175
|
maxId = rs.getInt("max") + 1;
|
176
|
}
|
177
|
int fromNameId = state.getDbId(fromName);
|
178
|
int toNameId = state.getDbId(toName);
|
179
|
int catId = state.getConfig().getIsHomotypicId();
|
180
|
String query = "INSERT INTO relName (relNameId, nameFk1, nameFk2, RelNameQualifierFk) " +
|
181
|
" VALUES ("+maxId+","+fromNameId+","+toNameId+","+catId+")";
|
182
|
int ui = state.getConfig().getDestination().getConnection().createStatement().executeUpdate(query);
|
183
|
}catch(SQLException e){
|
184
|
throw e;
|
185
|
}
|
186
|
return true;
|
187
|
}
|
188
|
|
189
|
private Set<TaxonNameBase> getAllRelatedNames(Set<NameRelationship> rels){
|
190
|
Set<TaxonNameBase> result = new HashSet<TaxonNameBase>();
|
191
|
for (NameRelationship rel : rels){
|
192
|
result.add(rel.getFromName());
|
193
|
result.add(rel.getToName());
|
194
|
}
|
195
|
return result;
|
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 RelName";
|
205
|
destination.setQuery(sql);
|
206
|
destination.update(sql);
|
207
|
|
208
|
return true;
|
209
|
}
|
210
|
|
211
|
|
212
|
/* (non-Javadoc)
|
213
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
214
|
*/
|
215
|
protected boolean isIgnore(BerlinModelExportState state){
|
216
|
return ! state.getConfig().isDoRelNames();
|
217
|
}
|
218
|
|
219
|
//called by MethodMapper
|
220
|
@SuppressWarnings("unused")
|
221
|
private static Integer getRelNameQualifierFk(RelationshipBase<?, ?, ?> rel) throws Exception {
|
222
|
// if (config.getRelNameQualifierMethod() != null){
|
223
|
// try {
|
224
|
// return (Integer)config.getRelNameQualifierMethod().invoke(rel);
|
225
|
// } catch (Exception e) {
|
226
|
// logger.error(e.getMessage());
|
227
|
// throw e;
|
228
|
// }
|
229
|
// }else{
|
230
|
return BerlinModelTransformer.nameRel2RelNameQualifierFk(rel);
|
231
|
// }
|
232
|
}
|
233
|
|
234
|
/* (non-Javadoc)
|
235
|
* @see eu.etaxonomy.cdm.io.berlinModel.out.BerlinModelExportBase#getStandardMethodParameter()
|
236
|
*/
|
237
|
@Override
|
238
|
public Class<? extends CdmBase> getStandardMethodParameter() {
|
239
|
return standardMethodParameter;
|
240
|
}
|
241
|
}
|