Revision 892efc69
Added by Andreas Kohlbecker almost 14 years ago
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/taxon/TaxonDaoHibernateImpl.java | ||
---|---|---|
27 | 27 |
import org.hibernate.FetchMode; |
28 | 28 |
import org.hibernate.Hibernate; |
29 | 29 |
import org.hibernate.Query; |
30 |
import org.hibernate.Session; |
|
30 | 31 |
import org.hibernate.criterion.Criterion; |
31 | 32 |
import org.hibernate.criterion.Projections; |
32 | 33 |
import org.hibernate.criterion.Restrictions; |
... | ... | |
41 | 42 |
|
42 | 43 |
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper; |
43 | 44 |
import eu.etaxonomy.cdm.model.common.LSID; |
45 |
import eu.etaxonomy.cdm.model.common.OriginalSourceBase; |
|
44 | 46 |
import eu.etaxonomy.cdm.model.common.RelationshipBase; |
45 | 47 |
import eu.etaxonomy.cdm.model.common.UuidAndTitleCache; |
46 | 48 |
import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction; |
... | ... | |
48 | 50 |
import eu.etaxonomy.cdm.model.name.NonViralName; |
49 | 51 |
import eu.etaxonomy.cdm.model.name.Rank; |
50 | 52 |
import eu.etaxonomy.cdm.model.name.TaxonNameBase; |
53 |
import eu.etaxonomy.cdm.model.name.TaxonNameComparator; |
|
51 | 54 |
import eu.etaxonomy.cdm.model.name.ZoologicalName; |
52 | 55 |
import eu.etaxonomy.cdm.model.reference.ReferenceBase; |
53 | 56 |
import eu.etaxonomy.cdm.model.taxon.Synonym; |
... | ... | |
456 | 459 |
} |
457 | 460 |
} |
458 | 461 |
|
459 |
List<TaxonBase> taxa = new ArrayList<TaxonBase>();
|
|
460 |
List<TaxonBase> synonyms = new ArrayList<TaxonBase>();
|
|
462 |
List<Integer> taxa = new ArrayList<Integer>();
|
|
463 |
List<Integer> synonyms = new ArrayList<Integer>();
|
|
461 | 464 |
if(clazz.equals(Taxon.class)){ |
462 | 465 |
taxa = subTaxon.list(); |
463 | 466 |
|
... | ... | |
483 | 486 |
if(synonyms.size()>0 && taxa.size()>0){ |
484 | 487 |
hql = "select " + selectWhat + " from " + clazz.getSimpleName() + " t" + " where t.id in (:taxa) OR t.id in (:synonyms)"; |
485 | 488 |
}else if (synonyms.size()>0 ){ |
486 |
hql = "select " + selectWhat + " from " + clazz.getSimpleName() + " t" |
|
487 |
+ " where t in (:synonyms)"; |
|
489 |
hql = "select " + selectWhat + " from " + clazz.getSimpleName() + " t" + " where t.id in (:synonyms)"; |
|
488 | 490 |
} else if (taxa.size()>0 ){ |
489 | 491 |
hql = "select " + selectWhat + " from " + clazz.getSimpleName() + " t" + " where t.id in (:taxa) "; |
490 | 492 |
} else{ |
... | ... | |
495 | 497 |
if (hql == "") return null; |
496 | 498 |
if(!doCount){ |
497 | 499 |
//hql += " order by t.titleCache"; //" order by t.name.nameCache"; |
498 |
hql += " order by t.name.genusOrUninomial, t.name.rank desc, titleCache"; |
|
500 |
hql += " order by t.name.genusOrUninomial, case when t.name.specificEpithet like '\"%\"' then 1 else 0 end, t.name.specificEpithet, t.name.rank desc, t.name.nameCache"; |
|
501 |
|
|
502 |
|
|
499 | 503 |
} |
500 | 504 |
|
501 | 505 |
Query query = getSession().createQuery(hql); |
502 | 506 |
|
507 |
|
|
503 | 508 |
if(clazz.equals(Taxon.class) && taxa.size()>0){ |
504 | 509 |
//find taxa |
505 | 510 |
query.setParameterList("taxa", taxa ); |
... | ... | |
1020 | 1025 |
} |
1021 | 1026 |
|
1022 | 1027 |
} |
1028 |
|
|
1029 |
class SynonymRelationshipFromTaxonComparator implements Comparator<SynonymRelationship> { |
|
1030 |
|
|
1031 |
public int compare(SynonymRelationship o1, SynonymRelationship o2) { |
|
1032 |
return o1.getSynonym().getTitleCache().compareTo(o2.getSynonym().getTitleCache()); |
|
1033 |
} |
|
1034 |
|
|
1035 |
} |
|
1023 | 1036 |
|
1024 | 1037 |
/* |
1025 | 1038 |
* (non-Javadoc) |
... | ... | |
1231 | 1244 |
propertyPaths.add("relationsToThisTaxon.fromTaxon"); |
1232 | 1245 |
propertyPaths.add("relationsToThisTaxon.toTaxon"); |
1233 | 1246 |
propertyPaths.add("relationsFromThisTaxon"); |
1234 |
propertyPaths.add("relationsFromThisTaxon.toTaxon"); |
|
1247 |
propertyPaths.add("relationsFromThisTaxon.toTaxon");
|
|
1235 | 1248 |
propertyPaths.add("relationsToThisTaxon.type"); |
1236 | 1249 |
propertyPaths.add("synonymRelations"); |
1237 | 1250 |
propertyPaths.add("synonymRelations.synonym"); |
... | ... | |
1285 | 1298 |
List<String> synonymsEpithet = new ArrayList<String>(); |
1286 | 1299 |
if (node.getTaxonomicTree().equals(tree)){ |
1287 | 1300 |
if (!node.isTopmostNode()){ |
1288 |
TaxonNode parent = (TaxonNode) node.getParent();
|
|
1301 |
TaxonNode parent = (TaxonNode)node.getParent(); |
|
1289 | 1302 |
parent = (TaxonNode)HibernateProxyHelper.deproxy(parent); |
1290 | 1303 |
TaxonNameBase parentName = parent.getTaxon().getName(); |
1291 | 1304 |
parentName = (TaxonNameBase)HibernateProxyHelper.deproxy(parentName); |
... | ... | |
1533 | 1546 |
return taxonNames; |
1534 | 1547 |
} |
1535 | 1548 |
|
1536 |
|
|
1549 |
//TODO: mal nur mit UUID probieren (ohne fetch all properties), vielleicht geht das schneller? |
|
1550 |
public List<UUID> findIdenticalTaxonNameIds(List<String> propertyPaths){ |
|
1551 |
Query query=getSession().createQuery("select tmb2 from ZoologicalName tmb, ZoologicalName tmb2 fetch all properties where tmb.id != tmb2.id and tmb.nameCache = tmb2.nameCache"); |
|
1552 |
System.err.println("query: " + query.getQueryString()); |
|
1553 |
List<UUID> zooNames = query.list(); |
|
1554 |
System.err.println("number of identical names"+zooNames.size()); |
|
1555 |
|
|
1556 |
|
|
1557 |
return zooNames; |
|
1558 |
|
|
1559 |
} |
|
1560 |
|
|
1537 | 1561 |
public List<TaxonNameBase> findIdenticalTaxonNames(List<String> propertyPaths) { |
1538 |
List<String> identicalNames = new ArrayList<String>(); |
|
1562 |
|
|
1539 | 1563 |
//hole alle TaxonNames, die es mindestens zweimal gibt. |
1540 |
Query query=getSession().createQuery("select tmb2 from ZoologicalName tmb, ZoologicalName tmb2 fetch all properties where tmb.nameCache = tmb2.nameCache and tmb.id != tmb2.id"); |
|
1564 |
|
|
1565 |
|
|
1566 |
|
|
1567 |
Query query=getSession().createQuery("select tmb2 from ZoologicalName tmb, ZoologicalName tmb2 fetch all properties where tmb.id != tmb2.id and tmb.nameCache = tmb2.nameCache"); |
|
1568 |
System.err.println("query: " + query.getQueryString()); |
|
1541 | 1569 |
List<TaxonNameBase> zooNames = query.list(); |
1570 |
System.err.println("number of identical names"+zooNames.size()); |
|
1571 |
|
|
1542 | 1572 |
TaxonNameComparator taxComp = new TaxonNameComparator(); |
1543 | 1573 |
Collections.sort(zooNames, taxComp); |
1544 |
|
|
1574 |
System.err.println("list is sorted"); |
|
1545 | 1575 |
for (TaxonNameBase taxonNameBase: zooNames){ |
1546 | 1576 |
defaultBeanInitializer.initialize(taxonNameBase, propertyPaths); |
1547 | 1577 |
} |
... | ... | |
1549 | 1579 |
return zooNames; |
1550 | 1580 |
} |
1551 | 1581 |
|
1582 |
public List<TaxonNameBase> findIdenticalNamesNew(List<String> propertyPaths){ |
|
1583 |
|
|
1584 |
//Hole die beiden Source_ids von "Fauna Europaea" und "Erms" und in sources der names darf jeweils nur das entgegengesetzte auftreten (i member of tmb.taxonBases) |
|
1585 |
Query query = getSession().createQuery("Select id from ReferenceBase where titleCache like 'Fauna Europaea database'"); |
|
1586 |
List<String> secRefFauna = query.list(); |
|
1587 |
query = getSession().createQuery("Select id from ReferenceBase where titleCache like 'ERMS'"); |
|
1588 |
List<String> secRefErms = query.list(); |
|
1589 |
//Query query = getSession().createQuery("select tmb2.nameCache from ZoologicalName tmb, TaxonBase tb1, ZoologicalName tmb2, TaxonBase tb2 where tmb.id != tmb2.id and tb1.name = tmb and tb2.name = tmb2 and tmb.nameCache = tmb2.nameCache and tb1.sec != tb2.sec"); |
|
1590 |
//Get all names of fauna europaea |
|
1591 |
query = getSession().createQuery("select zn.nameCache from ZoologicalName zn, TaxonBase tb where tb.name = zn and tb.sec.id = :secRefFauna"); |
|
1592 |
query.setParameter("secRefFauna", secRefFauna.get(0)); |
|
1593 |
List<String> namesFauna= query.list(); |
|
1594 |
|
|
1595 |
//Get all names of erms |
|
1596 |
|
|
1597 |
query = getSession().createQuery("select zn.nameCache from ZoologicalName zn, TaxonBase tb where tb.name = zn and tb.sec.id = :secRefErms"); |
|
1598 |
query.setParameter("secRefErms", secRefErms.get(0)); |
|
1599 |
|
|
1600 |
List<String> namesErms = query.list(); |
|
1601 |
/*TaxonNameComparator comp = new TaxonNameComparator(); |
|
1602 |
Collections.sort(namesFauna); |
|
1603 |
Collections.sort(namesErms); |
|
1604 |
*/ |
|
1605 |
List <String> identicalNames = new ArrayList<String>(); |
|
1606 |
String predecessor = ""; |
|
1607 |
|
|
1608 |
for (String nameFauna: namesFauna){ |
|
1609 |
if (namesErms.contains(nameFauna)){ |
|
1610 |
identicalNames.add(nameFauna); |
|
1611 |
} |
|
1612 |
} |
|
1613 |
System.err.println("number of identical names: " + identicalNames.size()); |
|
1614 |
|
|
1615 |
query = getSession().createQuery("from ZoologicalName zn where zn.nameCache IN (:identicalNames)"); |
|
1616 |
query.setParameterList("identicalNames", identicalNames); |
|
1617 |
List<TaxonNameBase> result = query.list(); |
|
1618 |
TaxonNameBase temp = result.get(0); |
|
1619 |
|
|
1620 |
Iterator<OriginalSourceBase> sources = temp.getSources().iterator(); |
|
1621 |
System.err.println(temp.getSources().size()); |
|
1622 |
if (sources.hasNext()){ |
|
1623 |
System.err.println("funktioniert..."+ sources.next().getIdInSource()); |
|
1624 |
//sources.next().getIdInSource(); |
|
1625 |
} |
|
1626 |
TaxonNameComparator taxComp = new TaxonNameComparator(); |
|
1627 |
Collections.sort(result, taxComp); |
|
1628 |
defaultBeanInitializer.initializeAll(result, propertyPaths); |
|
1629 |
return result; |
|
1630 |
|
|
1631 |
} |
|
1632 |
|
|
1633 |
|
|
1634 |
|
|
1552 | 1635 |
public String getPhylumName(TaxonNameBase name){ |
1636 |
List results = new ArrayList(); |
|
1637 |
try{ |
|
1553 | 1638 |
Query query = getSession().createSQLQuery("select getPhylum("+ name.getId()+");"); |
1554 |
List results = query.list(); |
|
1555 |
System.err.println("phylum of "+ name.getTitleCache() + " : "+(String)results.get(0)); |
|
1639 |
results = query.list(); |
|
1640 |
}catch(Exception e){ |
|
1641 |
System.err.println(name.getUuid()); |
|
1642 |
return null; |
|
1643 |
} |
|
1644 |
System.err.println("phylum of "+ name.getTitleCache() ); |
|
1556 | 1645 |
return (String)results.get(0); |
1557 | 1646 |
} |
1558 | 1647 |
|
... | ... | |
1562 | 1651 |
Set<NamedArea> namedAreas) { |
1563 | 1652 |
boolean doCount = true; |
1564 | 1653 |
Query query = prepareTaxaByCommonName(searchString, taxonomicTree, matchMode, namedAreas, null, null, doCount); |
1565 |
if (query != null) { |
|
1566 |
return (Long)query.uniqueResult(); |
|
1654 |
if (query != null && !query.list().isEmpty()) { |
|
1655 |
Object o = query.uniqueResult(); |
|
1656 |
if(o != null) { |
|
1657 |
return (Long)o; |
|
1658 |
} |
|
1567 | 1659 |
} |
1568 | 1660 |
return 0; |
1569 | 1661 |
|
1570 | 1662 |
} |
1663 |
|
|
1664 |
|
|
1665 |
public long deleteSynonymRelationships(Synonym syn) { |
|
1666 |
|
|
1667 |
/* |
|
1668 |
* DELETE RT |
|
1669 |
FROM RelTaxon AS RT INNER JOIN |
|
1670 |
Taxon AS FaEuSyn ON RT.TaxonFk1 = FaEuSyn.TaxonId INNER JOIN |
|
1671 |
Taxon AS ERMSAcc ON FaEuSyn.RankFk = ERMSAcc.RankFk AND FaEuSyn.FullName = ERMSAcc.FullName AND ISNULL(FaEuSyn.TaxonStatusFk, 0) |
|
1672 |
<> ERMSAcc.TaxonStatusFk |
|
1673 |
WHERE (FaEuSyn.OriginalDB = N'FaEu') AND (ERMSAcc.OriginalDB = N'ERMS') AND (ERMSAcc.TaxonStatusFk = 1) AND (ERMSAcc.KingdomFk = 2) AND |
|
1674 |
(RT.RelTaxonQualifierFk > 100) |
|
1675 |
*/ |
|
1676 |
Session session = this.getSession(); |
|
1677 |
Query q = session.createQuery("delete SynonymRelationship sr where sr.relatedFrom = :syn"); |
|
1678 |
q.setParameter("syn", syn); |
|
1679 |
return q.executeUpdate(); |
|
1680 |
} |
|
1681 |
|
|
1682 |
|
|
1683 |
@Override |
|
1684 |
public Integer countSynonymRelationships(TaxonBase taxonBase, |
|
1685 |
SynonymRelationshipType type, Direction relatedfrom) { |
|
1686 |
AuditEvent auditEvent = getAuditEventFromContext(); |
|
1687 |
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) { |
|
1688 |
Query query = null; |
|
1689 |
|
|
1690 |
if(type == null) { |
|
1691 |
query = getSession().createQuery("select count(synonymRelationship) from SynonymRelationship synonymRelationship where synonymRelationship."+relatedfrom+" = :relatedSynonym"); |
|
1692 |
} else { |
|
1693 |
query = getSession().createQuery("select count(synonymRelationship) from SynonymRelationship synonymRelationship where synonymRelationship."+relatedfrom+" = :relatedSynonym and synonymRelationship.type = :type"); |
|
1694 |
query.setParameter("type",type); |
|
1695 |
} |
|
1696 |
query.setParameter("relatedTaxon", taxonBase); |
|
1697 |
|
|
1698 |
return ((Long)query.uniqueResult()).intValue(); |
|
1699 |
} else { |
|
1700 |
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonRelationship.class,auditEvent.getRevisionNumber()); |
|
1701 |
query.add(AuditEntity.relatedId(relatedfrom.toString()).eq(taxonBase.getId())); |
|
1702 |
query.addProjection(AuditEntity.id().count("id")); |
|
1703 |
|
|
1704 |
if(type != null) { |
|
1705 |
query.add(AuditEntity.relatedId("type").eq(type.getId())); |
|
1706 |
} |
|
1707 |
|
|
1708 |
return ((Long)query.getSingleResult()).intValue(); |
|
1709 |
} |
|
1710 |
} |
|
1711 |
|
|
1712 |
|
|
1713 |
@Override |
|
1714 |
public List<SynonymRelationship> getSynonymRelationships(TaxonBase taxonBase, |
|
1715 |
SynonymRelationshipType type, Integer pageSize, Integer pageNumber, |
|
1716 |
List<OrderHint> orderHints, List<String> propertyPaths, |
|
1717 |
Direction direction) { |
|
1718 |
|
|
1719 |
AuditEvent auditEvent = getAuditEventFromContext(); |
|
1720 |
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) { |
|
1721 |
Criteria criteria = getSession().createCriteria(SynonymRelationship.class); |
|
1722 |
|
|
1723 |
if (direction.equals(Direction.relatedTo)){ |
|
1724 |
criteria.add(Restrictions.eq("relatedTo", taxonBase)); |
|
1725 |
}else{ |
|
1726 |
criteria.add(Restrictions.eq("relatedFrom", taxonBase)); |
|
1727 |
} |
|
1728 |
if(type != null) { |
|
1729 |
criteria.add(Restrictions.eq("type", type)); |
|
1730 |
} |
|
1731 |
|
|
1732 |
addOrder(criteria,orderHints); |
|
1733 |
|
|
1734 |
if(pageSize != null) { |
|
1735 |
criteria.setMaxResults(pageSize); |
|
1736 |
if(pageNumber != null) { |
|
1737 |
criteria.setFirstResult(pageNumber * pageSize); |
|
1738 |
} else { |
|
1739 |
criteria.setFirstResult(0); |
|
1740 |
} |
|
1741 |
} |
|
1742 |
|
|
1743 |
List<SynonymRelationship> result = (List<SynonymRelationship>)criteria.list(); |
|
1744 |
defaultBeanInitializer.initializeAll(result, propertyPaths); |
|
1745 |
|
|
1746 |
return result; |
|
1747 |
} else { |
|
1748 |
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonRelationship.class,auditEvent.getRevisionNumber()); |
|
1749 |
|
|
1750 |
if (direction.equals(Direction.relatedTo)){ |
|
1751 |
query.add(AuditEntity.relatedId("relatedTo").eq(taxonBase.getId())); |
|
1752 |
}else{ |
|
1753 |
query.add(AuditEntity.relatedId("relatedFrom").eq(taxonBase.getId())); |
|
1754 |
} |
|
1755 |
|
|
1756 |
if(type != null) { |
|
1757 |
query.add(AuditEntity.relatedId("type").eq(type.getId())); |
|
1758 |
} |
|
1759 |
|
|
1760 |
if(pageSize != null) { |
|
1761 |
query.setMaxResults(pageSize); |
|
1762 |
if(pageNumber != null) { |
|
1763 |
query.setFirstResult(pageNumber * pageSize); |
|
1764 |
} else { |
|
1765 |
query.setFirstResult(0); |
|
1766 |
} |
|
1767 |
} |
|
1768 |
|
|
1769 |
List<SynonymRelationship> result = (List<SynonymRelationship>)query.getResultList(); |
|
1770 |
defaultBeanInitializer.initializeAll(result, propertyPaths); |
|
1771 |
|
|
1772 |
// Ugly, but for now, there is no way to sort on a related entity property in Envers, |
|
1773 |
// and we can't live without this functionality in CATE as it screws up the whole |
|
1774 |
// taxon tree thing |
|
1775 |
if(orderHints != null && !orderHints.isEmpty()) { |
|
1776 |
SortedSet<SynonymRelationship> sortedList = new TreeSet<SynonymRelationship>(new SynonymRelationshipFromTaxonComparator()); |
|
1777 |
sortedList.addAll(result); |
|
1778 |
return new ArrayList<SynonymRelationship>(sortedList); |
|
1779 |
} |
|
1780 |
|
|
1781 |
return result; |
|
1782 |
} |
|
1783 |
} |
|
1784 |
|
|
1785 |
|
|
1786 |
|
|
1787 |
|
|
1788 |
|
|
1571 | 1789 |
|
1572 | 1790 |
|
1573 | 1791 |
|
Also available in: Unified diff
merging /branches/cdmlib/SPRINT-Chichorieae1/ to trunk