\r
public static final String OS_NAMESPACE_TAXON = "Taxon";\r
private static final Logger logger = Logger.getLogger(FaunaEuropaeaRelTaxonIncludeImport.class);\r
-\r
+ \r
+// String fromClause = " FROM Taxon INNER JOIN Taxon AS Parent " +\r
+// " ON Taxon.TAX_TAX_IDPARENT = Parent.TAX_ID " +\r
+// " WHERE (Taxon.TAX_VALID = 0) AND (Taxon.TAX_AUT_ID <> " + A_AUCT + " OR Taxon.TAX_AUT_ID IS NULL)";\r
+\r
+ private static String PARENT_SYNONYM_FROM_CLAUSE = " FROM Taxon INNER JOIN Taxon AS Parent " +\r
+ " ON Taxon.TAX_TAX_IDPARENT = Parent.TAX_ID " +\r
+ " WHERE (Taxon.TAX_TAX_IDGENUS is null) AND (Taxon.TAX_VALID = 0) " +\r
+ " AND (Taxon.TAX_AUT_ID <> " + A_AUCT + " OR Taxon.TAX_AUT_ID IS NULL)";\r
+\r
+ private static String ORIGINAL_GENUS_SYNONYM_FROM_CLAUSE = " FROM Taxon INNER JOIN Taxon AS OGenus " +\r
+ " ON Taxon.TAX_TAX_IDGENUS = OGenus.TAX_ID " +\r
+ " WHERE (Taxon.TAX_TAX_IDGENUS is not null) AND (Taxon.TAX_VALID = 0) " +\r
+ " AND (Taxon.TAX_AUT_ID <> " + A_AUCT + " OR Taxon.TAX_AUT_ID IS NULL)";\r
+ \r
private int limit = 5000; // TODO: Make configurable\r
/* Max number of taxa to be retrieved from CDM DB with one service call */\r
- private ReferenceBase<?> sourceRef = null;\r
+// private ReferenceBase<?> sourceRef = null;\r
\r
\r
- /**\r
- * @return the sourceRef\r
- */\r
- private ReferenceBase<?> getSourceRef() {\r
- return sourceRef;\r
- }\r
-\r
- /**\r
- * @param sourceRef the sourceRef to set\r
- */\r
- private void setSourceRef(ReferenceBase<?> sourceRef) {\r
- this.sourceRef = sourceRef;\r
- }\r
-\r
/* (non-Javadoc)\r
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)\r
*/\r
TransactionStatus txStatus = startTransaction();\r
\r
TaxonBase taxon = getTaxonService().getTaxonByUuid(UUID.fromString("ac7b30dc-6207-4c71-9752-ee0fb838a271"));\r
- setSourceRef(taxon.getSec());\r
+ ReferenceBase<?> sourceRef = taxon.getSec();\r
\r
TaxonomicTree tree = getTaxonomicTreeFor(state, sourceRef);\r
commitTransaction(txStatus);\r
}\r
ProfilerController.memorySnapshot();\r
if (state.getConfig().isDoHeterotypicSynonyms()) {\r
- success = processHeterotypicSynonyms(state);\r
+ success = processHeterotypicSynonyms(state, PARENT_SYNONYM_FROM_CLAUSE);\r
+ success = processHeterotypicSynonyms(state, ORIGINAL_GENUS_SYNONYM_FROM_CLAUSE);\r
}\r
ProfilerController.memorySnapshot();\r
\r
String selectCount = \r
" SELECT count(*) ";\r
\r
- String selectColumns = " SELECT dbo.Taxon.UUID AS ChildUuid, Parent.UUID AS ParentUuid ";\r
+ String selectColumns = " SELECT Taxon.UUID AS ChildUuid, Parent.UUID AS ParentUuid ";\r
\r
- String fromClause = " FROM dbo.Taxon INNER JOIN dbo.Taxon AS Parent " +\r
- " ON dbo.Taxon.TAX_TAX_IDPARENT = Parent.TAX_ID " +\r
- " WHERE (dbo.Taxon.TAX_VALID <> 0) AND (dbo.Taxon.TAX_AUT_ID <> " + A_AUCT + " OR dbo.Taxon.TAX_AUT_ID IS NULL )";\r
+ String fromClause = " FROM Taxon INNER JOIN Taxon AS Parent " +\r
+ " ON Taxon.TAX_TAX_IDPARENT = Parent.TAX_ID " +\r
+ " WHERE (Taxon.TAX_VALID <> 0) AND (Taxon.TAX_AUT_ID <> " + A_AUCT + " OR Taxon.TAX_AUT_ID IS NULL )";\r
\r
- String orderClause = " ORDER BY dbo.Taxon.TAX_RNK_ID ASC";\r
+ String orderClause = " ORDER BY Taxon.TAX_RNK_ID ASC";\r
\r
String countQuery = \r
selectCount + fromClause;\r
return success; \r
}\r
\r
+\r
+ /** Retrieve synonyms from FauEuDB DB */\r
+ private boolean processHeterotypicSynonyms(FaunaEuropaeaImportState state, String fromClause) {\r
+\r
+ FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();\r
+ Source source = fauEuConfig.getSource();\r
+ boolean success = true;\r
+\r
+ String selectCount = \r
+ " SELECT count(*) ";\r
+\r
+ String selectColumns = " SELECT Taxon.UUID AS SynonymUuid, Parent.UUID AS AcceptedUuid ";\r
+ \r
+ String orderClause = " ORDER BY dbo.Taxon.TAX_RNK_ID ASC ";\r
+\r
+ String countQuery = \r
+ selectCount + fromClause;\r
+\r
+ String selectQuery = \r
+ selectColumns + fromClause + orderClause;\r
+ \r
+ try {\r
+\r
+ ResultSet rs = source.getResultSet(countQuery);\r
+ rs.next();\r
+ int count = rs.getInt(1);\r
+ \r
+ rs = source.getResultSet(selectQuery);\r
+\r
+ if (logger.isInfoEnabled()) {\r
+ logger.info("Number of rows: " + count);\r
+ logger.info("Count Query: " + countQuery);\r
+ logger.info("Select Query: " + selectQuery);\r
+ }\r
+ \r
+ success = storeSynonymRelationships(rs, count, state);\r
+\r
+ } catch (SQLException e) {\r
+ logger.error("SQLException:" + e);\r
+ success = false;\r
+ }\r
+ return success; \r
+ }\r
+\r
\r
/** Retrieve synonyms from FauEuDB DB */\r
- private boolean processHeterotypicSynonyms(FaunaEuropaeaImportState state) {\r
+ private boolean processHeterotypicSynonyms_(FaunaEuropaeaImportState state) {\r
\r
int limit = state.getConfig().getLimitSave();\r
\r
TransactionStatus txStatus = null;\r
\r
- Map<UUID, UUID> childParentMap = null;\r
+ Map<UUID, UUID> synonymAcceptedMap = null;\r
FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();\r
Source source = fauEuConfig.getSource();\r
int i = 0;\r
\r
String fromClause = " FROM Taxon INNER JOIN Taxon AS Parent " +\r
" ON Taxon.TAX_TAX_IDPARENT = Parent.TAX_ID " +\r
- " WHERE (Taxon.TAX_VALID = 0) AND (Taxon.TAX_AUT_ID <> " + A_AUCT + ")";\r
+ " WHERE (Taxon.TAX_VALID = 0) AND (Taxon.TAX_AUT_ID <> " + A_AUCT + " OR Taxon.TAX_AUT_ID IS NULL)";\r
\r
String orderClause = " ORDER BY dbo.Taxon.TAX_RNK_ID ASC ";\r
\r
if ((i++ % limit) == 0) {\r
\r
txStatus = startTransaction();\r
- childParentMap = new HashMap<UUID, UUID>(limit);\r
+ synonymAcceptedMap = new HashMap<UUID, UUID>(limit);\r
\r
if(logger.isInfoEnabled()) {\r
- logger.info("Synonyms retrieved: " + (i-1) ); \r
+ logger.info("Synonyms retrieved: " + (i-1)); \r
}\r
}\r
\r
- String childUuidStr = rs.getString("SynonymUuid");\r
- String parentUuidStr = rs.getString("AcceptedUuid");\r
- UUID childUuid = UUID.fromString(childUuidStr);\r
- UUID parentUuid = UUID.fromString(parentUuidStr);\r
+ String synonymUuidStr = rs.getString("SynonymUuid");\r
+ String acceptedUuidStr = rs.getString("AcceptedUuid");\r
+ UUID synonymUuid = UUID.fromString(synonymUuidStr);\r
+ UUID acceptedUuid = UUID.fromString(acceptedUuidStr);\r
\r
- if (!childParentMap.containsKey(childUuid)) {\r
+ if (!synonymAcceptedMap.containsKey(synonymUuid)) {\r
\r
- childParentMap.put(childUuid, parentUuid);\r
+ synonymAcceptedMap.put(synonymUuid, acceptedUuid);\r
\r
} else {\r
if(logger.isDebugEnabled()) {\r
- logger.debug("Duplicated child UUID (" + childUuid + ")");\r
+ logger.debug("Duplicated child UUID (" + synonymUuid + ")");\r
}\r
}\r
\r
if (((i % limit) == 0 && i != 1 ) || i == count) { \r
\r
- success = createHeterotypicSynonyms(state, childParentMap);\r
+ success = createHeterotypicSynonyms(state, synonymAcceptedMap);\r
\r
- childParentMap = null;\r
+ synonymAcceptedMap = null;\r
commitTransaction(txStatus);\r
\r
if(logger.isInfoEnabled()) {\r
}\r
return success; \r
}\r
+\r
+ \r
+ private boolean storeSynonymRelationships(ResultSet rs, int count, FaunaEuropaeaImportState state) \r
+ throws SQLException {\r
+\r
+ TransactionStatus txStatus = null;\r
+ Map<UUID, UUID> synonymAcceptedMap = null;\r
+ int i = 0;\r
+ boolean success = true;\r
+\r
+ while (rs.next()) {\r
+\r
+ if ((i++ % limit) == 0) {\r
+\r
+ txStatus = startTransaction();\r
+ synonymAcceptedMap = new HashMap<UUID, UUID>(limit);\r
+\r
+ if(logger.isInfoEnabled()) {\r
+ logger.info("Synonyms retrieved: " + (i-1)); \r
+ }\r
+ }\r
+\r
+ String synonymUuidStr = rs.getString("SynonymUuid");\r
+ String acceptedUuidStr = rs.getString("AcceptedUuid");\r
+ UUID synonymUuid = UUID.fromString(synonymUuidStr);\r
+ UUID acceptedUuid = UUID.fromString(acceptedUuidStr);\r
+\r
+ if (!synonymAcceptedMap.containsKey(synonymUuid)) {\r
+\r
+ synonymAcceptedMap.put(synonymUuid, acceptedUuid);\r
+\r
+ } else {\r
+ if(logger.isDebugEnabled()) {\r
+ logger.debug("Duplicated synonym UUID (" + synonymUuid + ")");\r
+ }\r
+ }\r
+\r
+ if (((i % limit) == 0 && i != 1 ) || i == count) { \r
+\r
+ success = createHeterotypicSynonyms(state, synonymAcceptedMap);\r
+\r
+ synonymAcceptedMap = null;\r
+ commitTransaction(txStatus);\r
+\r
+ if(logger.isInfoEnabled()) {\r
+ logger.info("i = " + i + " - Transaction committed"); \r
+ }\r
+ }\r
+ }\r
+ return success;\r
+ }\r
+ \r
\r
/* Creates parent-child relationships.\r
* Parent-child pairs are retrieved in blocks via findByUUID(Set<UUID>) from CDM DB. \r
*/\r
private boolean createParentChildRelationships(FaunaEuropaeaImportState state, Map<UUID, UUID> childParentMap) {\r
\r
-// TaxonBase taxon = getTaxonService().getTaxonByUuid(UUID.fromString("ac7b30dc-6207-4c71-9752-ee0fb838a271"));\r
-// ReferenceBase<?> sourceRef = taxon.getSec();\r
+ TaxonBase taxon = getTaxonService().getTaxonByUuid(UUID.fromString("ac7b30dc-6207-4c71-9752-ee0fb838a271"));\r
+ ReferenceBase<?> sourceRef = taxon.getSec();\r
boolean success = true;\r
\r
- TaxonomicTree tree = getTaxonomicTreeFor(state, getSourceRef());\r
+ TaxonomicTree tree = getTaxonomicTreeFor(state, sourceRef);\r
\r
Set<TaxonBase> childSet = new HashSet<TaxonBase>(limit);\r
\r
Set<UUID> childKeysSet = childParentMap.keySet();\r
Set<UUID> parentValuesSet = new HashSet<UUID>(childParentMap.values());\r
\r
- if (logger.isInfoEnabled()) {\r
- logger.info("Start reading children and parents");\r
+ if (logger.isTraceEnabled()) {\r
+ logger.trace("Start reading children and parents");\r
}\r
List<TaxonBase> children = getTaxonService().findByUuid(childKeysSet);\r
List<TaxonBase> parents = getTaxonService().findByUuid(parentValuesSet);\r
parentsMap.put(taxonBase.getUuid(), taxonBase);\r
}\r
\r
- \r
- if (logger.isInfoEnabled()) {\r
- logger.info("End reading children and parents");\r
- }\r
- \r
- \r
if (logger.isTraceEnabled()) {\r
+ logger.debug("End reading children and parents");\r
for (UUID uuid : childKeysSet) {\r
logger.trace("child uuid query: " + uuid);\r
}\r
- }\r
- if (logger.isTraceEnabled()) {\r
for (UUID uuid : parentValuesSet) {\r
logger.trace("parent uuid query: " + uuid);\r
}\r
- }\r
- if (logger.isTraceEnabled()) {\r
for (TaxonBase tb : children) {\r
logger.trace("child uuid result: " + tb.getUuid());\r
}\r
- }\r
- if (logger.isTraceEnabled()) {\r
for (TaxonBase tb : parents) {\r
logger.trace("parent uuid result: " + tb.getUuid());\r
}\r
logger.debug("Parent-child (" + mappedParentUuid + "-" + childUuid + \r
") relationship created");\r
}\r
- if (!childSet.contains(childTaxon)) {\r
+ if (childTaxon != null && !childSet.contains(childTaxon)) {\r
\r
childSet.add(childTaxon);\r
\r
}\r
}\r
\r
- if (childTaxon != null && !childSet.contains(childTaxon)) {\r
- childSet.add(childTaxon);\r
- if (logger.isDebugEnabled()) {\r
- logger.debug("Child taxon (" + childUuid + ") added to Set");\r
- }\r
- } else {\r
- if (logger.isDebugEnabled()) {\r
- logger.debug("Duplicated child taxon (" + childUuid + ")");\r
- }\r
- }\r
- \r
} catch (Exception e) {\r
logger.error("Error creating taxonomically included relationship parent-child (" + \r
mappedParentUuid + "-" + childUuid + ")", e);\r
}\r
\r
}\r
- if (logger.isInfoEnabled()) {\r
- logger.info("Start saving childSet");\r
+ if (logger.isTraceEnabled()) {\r
+ logger.trace("Start saving childSet");\r
}\r
getTaxonService().saveTaxonAll(childSet);\r
- if (logger.isInfoEnabled()) {\r
- logger.info("End saving childSet");\r
+ if (logger.isTraceEnabled()) {\r
+ logger.trace("End saving childSet");\r
}\r
\r
parentValuesSet = null;\r
Set<UUID> misappliedNamesSet = fromToMap.keySet();\r
Set<UUID> acceptedTaxaSet = new HashSet<UUID>(fromToMap.values());\r
\r
- if (logger.isInfoEnabled()) {\r
- logger.info("Start reading misapplied names and accepted taxa");\r
+ if (logger.isTraceEnabled()) {\r
+ logger.trace("Start reading misapplied names and accepted taxa");\r
}\r
List<TaxonBase> misappliedNames = getTaxonService().findByUuid(misappliedNamesSet);\r
List<TaxonBase> acceptedTaxa = getTaxonService().findByUuid(acceptedTaxaSet);\r
acceptedTaxaMap.put(taxonBase.getUuid(), taxonBase);\r
}\r
\r
- \r
- if (logger.isInfoEnabled()) {\r
- logger.info("End reading misapplied names and accepted taxa");\r
- }\r
- \r
- \r
if (logger.isTraceEnabled()) {\r
+ logger.info("End reading misapplied names and accepted taxa");\r
for (UUID uuid : misappliedNamesSet) {\r
logger.trace("misapplied name uuid query: " + uuid);\r
}\r
- }\r
- if (logger.isTraceEnabled()) {\r
for (UUID uuid : acceptedTaxaSet) {\r
logger.trace("accepted taxon uuid query: " + uuid);\r
}\r
- }\r
- if (logger.isTraceEnabled()) {\r
for (TaxonBase tb : misappliedNames) {\r
logger.trace("misapplied name uuid result: " + tb.getUuid());\r
}\r
- }\r
- if (logger.isTraceEnabled()) {\r
for (TaxonBase tb : acceptedTaxa) {\r
logger.trace("accepted taxon uuid result: " + tb.getUuid());\r
}\r
\r
String selectColumns = \r
" SELECT Parent.TAX_NAME AS P2Name, Parent.TAX_RNK_ID AS P2RankId, " +\r
- " GrandParent.TAX_NAME AS GP3Name, GrandParent.TAX_RNK_ID AS GP3RankId, " +\r
- " GreatGrandParent.TAX_NAME AS GGP4Name, GreatGrandParent.TAX_RNK_ID AS GGP4RankId, " +\r
- " GreatGreatGrandParent.TAX_NAME AS GGGP5Name, OriginalGenusTaxon.TAX_NAME AS OGenusName, " +\r
+ " GrandParent.TAX_ID AS GP3Id, GrandParent.TAX_NAME AS GP3Name, GrandParent.TAX_RNK_ID AS GP3RankId, " +\r
+ " GreatGrandParent.TAX_ID AS GGP4Id, GreatGrandParent.TAX_NAME AS GGP4Name, GreatGrandParent.TAX_RNK_ID AS GGP4RankId, " +\r
+ " GreatGreatGrandParent.TAX_ID AS GGG5Id, GreatGreatGrandParent.TAX_NAME AS GGGP5Name, OriginalGenusTaxon.TAX_NAME AS OGenusName, " +\r
" Taxon.*, rank.*, author.* ";\r
\r
String fromClause = \r
String nameString = \r
buildTaxonName(fauEuTaxon, taxonBase, taxonName, false, fauEuConfig);\r
\r
- if (fauEuConfig.isDoBasionyms() && fauEuTaxon.isValid() &&\r
- (fauEuTaxon.getOriginalGenusId() != 0) &&\r
- (fauEuTaxon.getParentId() != fauEuTaxon.getOriginalGenusId())) {\r
+// if (fauEuConfig.isDoBasionyms() && fauEuTaxon.isValid() &&\r
+// (fauEuTaxon.getOriginalGenusId() != 0) &&\r
+// (fauEuTaxon.getParentId() != fauEuTaxon.getOriginalGenusId())) {\r
+ if (fauEuConfig.isDoBasionyms() \r
+ && fauEuTaxon.getRankId() > R_SUBGENUS\r
+ && (fauEuTaxon.getOriginalGenusId() != 0)) {\r
success = createBasionym(fauEuTaxon, taxonBase, taxonName, fauEuConfig);\r
}\r
}\r
\r
return stringBuilder.toString();\r
}\r
+\r
+ /** Get actual genus id **/\r
+ private int getActualGenusId(FaunaEuropaeaTaxon fauEuTaxon) {\r
+ \r
+ int actualGenusId = -1;\r
+ int rank = fauEuTaxon.getRankId();\r
+ int parentRankId = fauEuTaxon.getParentRankId();\r
+ int grandParentRankId = fauEuTaxon.getGrandParentRankId();\r
+ int greatGrandParentRankId = fauEuTaxon.getGreatGrandParentRankId();\r
+ \r
+ if (fauEuTaxon.isValid()) { // Taxon\r
+ \r
+ if (rank == R_SPECIES) {\r
+\r
+ if(parentRankId == R_SUBGENUS) {\r
+\r
+ actualGenusId = fauEuTaxon.getGrandParentId();\r
+ \r
+ } else if(parentRankId == R_GENUS) {\r
+\r
+ actualGenusId = fauEuTaxon.getParentId();\r
+ }\r
+\r
+ } else if (rank == R_SUBSPECIES) {\r
+\r
+ if(grandParentRankId == R_SUBGENUS) {\r
+\r
+ actualGenusId = fauEuTaxon.getGreatGrandParentId();\r
+ \r
+ } else if (grandParentRankId == R_GENUS) {\r
+\r
+ actualGenusId = fauEuTaxon.getGrandParentId();\r
+\r
+ }\r
+ }\r
+ } else { // Synonym\r
+ \r
+ if (rank == R_SPECIES) {\r
+\r
+ if(grandParentRankId == R_SUBGENUS) {\r
+ \r
+ actualGenusId = fauEuTaxon.getGreatGrandParentId();\r
+ \r
+ } else if (grandParentRankId == R_GENUS) {\r
+ \r
+ actualGenusId = fauEuTaxon.getGrandParentId();\r
+\r
+ }\r
+\r
+ } else if (rank == R_SUBSPECIES) {\r
+ \r
+ if(greatGrandParentRankId == R_SUBGENUS) {\r
+ \r
+ actualGenusId = fauEuTaxon.getGreatGreatGrandParentId();\r
+ \r
+ } else if (greatGrandParentRankId == R_GENUS) {\r
+ \r
+ actualGenusId = fauEuTaxon.getGreatGrandParentId();\r
+ }\r
+ }\r
+ }\r
+ return actualGenusId;\r
+ }\r
\r
\r
/** Build species and subspecies names */\r