Revision 96533ef2
Added by Andreas Müller about 12 years ago
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiRelTaxonExport.java | ||
---|---|---|
26 | 26 |
import eu.etaxonomy.cdm.model.common.CdmBase; |
27 | 27 |
import eu.etaxonomy.cdm.model.common.IdentifiableEntity; |
28 | 28 |
import eu.etaxonomy.cdm.model.common.RelationshipBase; |
29 |
import eu.etaxonomy.cdm.model.name.HybridRelationship; |
|
29 | 30 |
import eu.etaxonomy.cdm.model.name.NameRelationship; |
30 | 31 |
import eu.etaxonomy.cdm.model.name.NomenclaturalCode; |
31 | 32 |
import eu.etaxonomy.cdm.model.name.Rank; |
... | ... | |
54 | 55 |
private static final String dbTableName = "RelTaxon"; |
55 | 56 |
private static final String pluralString = "Relationships"; |
56 | 57 |
private static PreparedStatement synonymsStmt; |
58 |
|
|
57 | 59 |
private HashMap<Rank, Rank> rank2endRankMap = new HashMap<Rank, Rank>(); |
58 | 60 |
private List<Rank> rankList = new ArrayList<Rank>(); |
59 | 61 |
private PesiExportMapping mapping; |
60 | 62 |
private int count = 0; |
61 |
private static NomenclaturalCode nomenclaturalCode2; |
|
62 | 63 |
|
63 | 64 |
public PesiRelTaxonExport() { |
64 | 65 |
super(); |
... | ... | |
81 | 82 |
return result; |
82 | 83 |
} |
83 | 84 |
|
85 |
|
|
84 | 86 |
/* (non-Javadoc) |
85 | 87 |
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase) |
86 | 88 |
*/ |
87 | 89 |
@Override |
88 | 90 |
protected void doInvoke(PesiExportState state) { |
91 |
try { |
|
92 |
logger.info("*** Started Making " + pluralString + " ..."); |
|
93 |
|
|
94 |
Connection connection = state.getConfig().getDestination().getConnection(); |
|
95 |
String synonymsSql = "UPDATE Taxon SET KingdomFk = ?, RankFk = ?, RankCache = ? WHERE TaxonId = ?"; |
|
96 |
synonymsStmt = connection.prepareStatement(synonymsSql); |
|
97 |
|
|
98 |
// Stores whether this invoke was successful or not. |
|
99 |
boolean success = true; |
|
100 |
|
|
101 |
// PESI: Clear the database table RelTaxon. |
|
102 |
doDelete(state); |
|
103 |
|
|
104 |
// Get specific mappings: (CDM) Relationship -> (PESI) RelTaxon |
|
105 |
mapping = getMapping(); |
|
106 |
|
|
107 |
// Initialize the db mapper |
|
108 |
mapping.initialize(state); |
|
109 |
|
|
110 |
//Export Taxa.. |
|
111 |
// success &= doPhase01(state, mapping); |
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
// 2nd Round: Add ParentTaxonFk, TreeIndex to each Taxon |
|
116 |
success &= doPhase02(state, mapping); |
|
117 |
|
|
118 |
} catch (SQLException e) { |
|
119 |
e.printStackTrace(); |
|
120 |
logger.error(e.getMessage()); |
|
121 |
state.setUnsuccessfull(); |
|
122 |
return; |
|
123 |
} |
|
124 |
} |
|
125 |
|
|
126 |
|
|
127 |
private boolean doPhase01(PesiExportState state, PesiExportMapping mapping2) throws SQLException { |
|
128 |
logger.info("PHASE 1: Relationships ..."); |
|
129 |
boolean success = true; |
|
130 |
|
|
131 |
int limit = state.getConfig().getLimitSave(); |
|
132 |
// Start transaction |
|
133 |
TransactionStatus txStatus = startTransaction(true); |
|
134 |
logger.info("Started new transaction. Fetching some " + pluralString + " (max: " + limit + ") ..."); |
|
135 |
|
|
136 |
List<RelationshipBase> list; |
|
137 |
|
|
138 |
//taxon relations |
|
139 |
int partitionCount = 0; |
|
140 |
while ((list = getNextTaxonRelationshipPartition(null, limit, partitionCount++, null)).size() > 0 ) { |
|
141 |
for (RelationshipBase rel : list){ |
|
142 |
try { |
|
143 |
mapping.invoke(rel); |
|
144 |
} catch (Exception e) { |
|
145 |
logger.error(e.getMessage() + ". Relationship: " + rel.getUuid()); |
|
146 |
e.printStackTrace(); |
|
147 |
} |
|
148 |
} |
|
149 |
} |
|
150 |
|
|
151 |
return success; |
|
152 |
} |
|
153 |
|
|
154 |
private boolean doPhase02(PesiExportState state, PesiExportMapping mapping2) throws SQLException { |
|
155 |
logger.info("PHASE 1: Relationships ..."); |
|
156 |
boolean success = true; |
|
157 |
|
|
158 |
int limit = state.getConfig().getLimitSave(); |
|
159 |
// Start transaction |
|
160 |
TransactionStatus txStatus = startTransaction(true); |
|
161 |
logger.info("Started new transaction. Fetching some " + pluralString + " (max: " + limit + ") ..."); |
|
162 |
|
|
163 |
List<RelationshipBase> list; |
|
164 |
|
|
165 |
//name relations |
|
166 |
int partitionCount = 0; |
|
167 |
while ((list = getNextNameRelationshipPartition(null, limit, partitionCount++, null)).size() > 0 ) { |
|
168 |
for (RelationshipBase rel : list){ |
|
169 |
try { |
|
170 |
TaxonNameBase<?,?> name1; |
|
171 |
TaxonNameBase<?,?> name2; |
|
172 |
if (rel.isInstanceOf(HybridRelationship.class)){ |
|
173 |
HybridRelationship hybridRel = CdmBase.deproxy(rel, HybridRelationship.class); |
|
174 |
name1 = hybridRel.getParentName(); |
|
175 |
name2 = hybridRel.getHybridName(); |
|
176 |
}else if (rel.isInstanceOf(NameRelationship.class)){ |
|
177 |
NameRelationship nameRel = CdmBase.deproxy(rel, NameRelationship.class); |
|
178 |
name1 = nameRel.getFromName(); |
|
179 |
name2 = nameRel.getToName(); |
|
180 |
}else{ |
|
181 |
logger.warn ("Only hybrid- and name-relationships alowed here"); |
|
182 |
continue; |
|
183 |
} |
|
184 |
List<IdentifiableEntity> fromList = new ArrayList<IdentifiableEntity>(); |
|
185 |
List<IdentifiableEntity> toList = new ArrayList<IdentifiableEntity>(); |
|
186 |
makeList(name1, fromList); |
|
187 |
makeList(name2, toList); |
|
188 |
|
|
189 |
for (IdentifiableEntity fromEntity : fromList){ |
|
190 |
for (IdentifiableEntity toEntity : toList){ |
|
191 |
//TODO set entities to state |
|
192 |
state.setCurrentFromObject(fromEntity); |
|
193 |
state.setCurrentToObject(toEntity); |
|
194 |
mapping.invoke(rel); |
|
195 |
} |
|
196 |
} |
|
197 |
|
|
198 |
} catch (Exception e) { |
|
199 |
logger.error(e.getMessage() + ". Relationship: " + rel.getUuid()); |
|
200 |
e.printStackTrace(); |
|
201 |
} |
|
202 |
} |
|
203 |
} |
|
204 |
|
|
205 |
|
|
206 |
return success; |
|
207 |
} |
|
208 |
|
|
209 |
private void makeList(TaxonNameBase<?, ?> name, List<IdentifiableEntity> list) { |
|
210 |
if (! hasPesiTaxon(name)){ |
|
211 |
list.add(name); |
|
212 |
}else{ |
|
213 |
for (TaxonBase taxon: getPesiTaxa(name)){ |
|
214 |
list.add(taxon); |
|
215 |
} |
|
216 |
} |
|
217 |
} |
|
218 |
|
|
219 |
/* (non-Javadoc) |
|
220 |
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase) |
|
221 |
*/ |
|
222 |
protected void doInvoke_Old(PesiExportState state) { |
|
89 | 223 |
try { |
90 | 224 |
logger.info("*** Started Making " + pluralString + " ..."); |
91 | 225 |
|
... | ... | |
483 | 617 |
* @see MethodMapper |
484 | 618 |
*/ |
485 | 619 |
@SuppressWarnings("unused") |
486 |
private static String getRelQualifierCache(RelationshipBase<?, ?, ?> relationship) { |
|
620 |
private static String getRelQualifierCache(RelationshipBase<?, ?, ?> relationship, PesiExportState state) {
|
|
487 | 621 |
String result = null; |
488 | 622 |
NomenclaturalCode code = null; |
489 | 623 |
if (relationship.isInstanceOf(TaxonRelationship.class)){ |
... | ... | |
494 | 628 |
code = CdmBase.deproxy(relationship, NameRelationship.class).getFromName().getNomenclaturalCode(); |
495 | 629 |
} |
496 | 630 |
if (code != null) { |
497 |
result = PesiTransformer.taxonRelation2RelTaxonQualifierCache(relationship, code);
|
|
631 |
result = state.getConfig().getTransformer().getCacheByRelationshipType(relationship, code);
|
|
498 | 632 |
} else { |
499 | 633 |
logger.error("NomenclaturalCode is NULL while creating the following relationship: " + relationship.getUuid()); |
500 | 634 |
} |
... | ... | |
528 | 662 |
} else if (relationship.isInstanceOf(SynonymRelationship.class)) { |
529 | 663 |
SynonymRelationship sr = (SynonymRelationship)relationship; |
530 | 664 |
taxonBase = (isFrom) ? sr.getSynonym() : sr.getAcceptedTaxon(); |
531 |
} else if (relationship.isInstanceOf(NameRelationship.class)) { |
|
665 |
} else if (relationship.isInstanceOf(NameRelationship.class) || relationship.isInstanceOf(HybridRelationship.class)) {
|
|
532 | 666 |
if (isFrom){ |
533 | 667 |
return state.getDbId(state.getCurrentFromObject()); |
534 | 668 |
}else{ |
... | ... | |
594 | 728 |
mapping.addMapper(MethodMapper.NewInstance("TaxonFk1", this.getClass(), "getTaxonFk1", standardMethodParameter, PesiExportState.class)); |
595 | 729 |
mapping.addMapper(MethodMapper.NewInstance("TaxonFk2", this.getClass(), "getTaxonFk2", standardMethodParameter, PesiExportState.class)); |
596 | 730 |
mapping.addMapper(MethodMapper.NewInstance("RelTaxonQualifierFk", this)); |
597 |
mapping.addMapper(MethodMapper.NewInstance("RelQualifierCache", this)); |
|
731 |
mapping.addMapper(MethodMapper.NewInstance("RelQualifierCache", this, RelationshipBase.class, PesiExportState.class));
|
|
598 | 732 |
mapping.addMapper(MethodMapper.NewInstance("Notes", this)); |
599 | 733 |
|
600 | 734 |
return mapping; |
Also available in: Unified diff
getAllRelationships reimplemented to match only relevant relationship classes,
Export generics include Transformer type