comparison src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java @ 75:e0be7c0030f5

cleanup and better comments.
author casties
date Thu, 23 Feb 2017 19:05:47 +0100
parents 7acd7564e394
children 4c9ceb28cfd0
comparison
equal deleted inserted replaced
74:7acd7564e394 75:e0be7c0030f5
1719 1719
1720 // ################################################################# 1720 // #################################################################
1721 // ################################################################# 1721 // #################################################################
1722 // ################################################################# 1722 // #################################################################
1723 1723
1724 /**
1725 * Returns the number of Entities of the given objectClass in the database.
1726 *
1727 * Returns the number of all Entities if objectClass==null.
1728 *
1729 * @param objectClass
1730 * @return
1731 */
1724 public Long getEntityCount(String objectClass) { 1732 public Long getEntityCount(String objectClass) {
1725 Long count = null; 1733 Long count = null;
1726 try { 1734 try {
1727 Session session = HibernateUtil.getSessionFactory() 1735 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
1728 .getCurrentSession();
1729 session.getTransaction().begin(); 1736 session.getTransaction().begin();
1730 1737
1731 String hql = "select count(*) from Entity where "; 1738 String hql = "select count(*) from Entity where ";
1732 1739
1733 if (StringUtils.isNotEmpty(objectClass)) { 1740 if (StringUtils.isNotEmpty(objectClass)) {
1746 } 1753 }
1747 count = (Long) query.uniqueResult(); 1754 count = (Long) query.uniqueResult();
1748 1755
1749 session.getTransaction().commit(); 1756 session.getTransaction().commit();
1750 } catch (Exception e) { 1757 } catch (Exception e) {
1751 logger.error(e.getMessage(), e); 1758 logger.error(e);
1752 } 1759 }
1753 return count; 1760 return count;
1754 } 1761 }
1755 1762
1756 /** 1763 /**
1764 * Returns all Entities of the given objectClass in the database
1765 * between startRecord and endRecord.
1757 * 1766 *
1758 * @param objectClass 1767 * @param objectClass
1759 * if it is null, it will be returned all entities (no 1768 * if it is null, all entities are returned (except
1760 * definition). To get the definitions objectClass should be: 1769 * definitions). To get the definitions objectClass should be:
1761 * Node.TYPE_TBOX 1770 * Node.TYPE_TBOX
1762 * @param startRecord 1771 * @param startRecord
1763 * @param endRecord 1772 * @param endRecord
1764 * @return 1773 * @return
1765 */ 1774 */
1766 public List<Entity> getEntityPage(String objectClass, 1775 public List<Entity> getEntityPage(String objectClass, final int startRecord, final int endRecord) {
1767 final int startRecord, final int endRecord) {
1768 1776
1769 List<Entity> entities = new ArrayList<Entity>(); 1777 List<Entity> entities = new ArrayList<Entity>();
1770 try { 1778 try {
1771 Session session = HibernateUtil.getSessionFactory() 1779 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
1772 .getCurrentSession();
1773 session.getTransaction().begin(); 1780 session.getTransaction().begin();
1774 1781
1775 String hql = "from Entity where "; 1782 String hql = "from Entity where ";
1776 1783
1777 if (StringUtils.isNotEmpty(objectClass)) { 1784 if (StringUtils.isNotEmpty(objectClass)) {
1785 // real objectClass
1778 hql += "objectClass = :objectClass AND "; 1786 hql += "objectClass = :objectClass AND ";
1779 } else { 1787 } else {
1788 // objectClass == null -- match object_class != 'TBox'
1780 hql += "objectClass != :objectClass AND "; 1789 hql += "objectClass != :objectClass AND ";
1781 } 1790 }
1782 1791
1783 hql += "systemStatus = :systemStatus order by ownValue"; 1792 hql += "systemStatus = :systemStatus order by ownValue";
1784 1793
1785 Query query = session.createQuery(hql); 1794 Query query = session.createQuery(hql);
1795 // use Hibernate's result paging
1786 query.setFirstResult(startRecord); 1796 query.setFirstResult(startRecord);
1787 query.setMaxResults(endRecord); 1797 query.setMaxResults(endRecord);
1798 // add query params
1788 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); 1799 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION);
1789 if (StringUtils.isNotEmpty(objectClass)) { 1800 if (StringUtils.isNotEmpty(objectClass)) {
1801 // real objectClass
1790 query.setString("objectClass", objectClass); 1802 query.setString("objectClass", objectClass);
1791 } else { 1803 } else {
1804 // objectClass == null -- match object_class != 'TBox'
1792 query.setString("objectClass", Node.TYPE_TBOX); 1805 query.setString("objectClass", Node.TYPE_TBOX);
1793 } 1806 }
1807 // fetch Entities
1794 entities = query.list(); 1808 entities = query.list();
1795 1809
1796 session.getTransaction().commit(); 1810 session.getTransaction().commit();
1797 } catch (Exception e) { 1811 } catch (Exception e) {
1798 logger.error(e.getMessage(), e); 1812 logger.error(e);
1799 // e.printStackTrace();
1800 } 1813 }
1801 return entities; 1814 return entities;
1802 } 1815 }
1803 1816
1804 public static void main(String[] args) { 1817 public static void main(String[] args) {