[jboss-cvs] jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/dao ...

Christian Bauer christian at hibernate.org
Tue Mar 20 21:24:48 EDT 2007


  User: cbauer  
  Date: 07/03/20 21:24:48

  Modified:    examples/wiki/src/main/org/jboss/seam/wiki/core/dao 
                        NodeDAO.java
  Log:
  Basic blog directory plugin
  
  Revision  Changes    Path
  1.5       +62 -0     jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/dao/NodeDAO.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: NodeDAO.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/dao/NodeDAO.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -b -r1.4 -r1.5
  --- NodeDAO.java	20 Mar 2007 02:38:15 -0000	1.4
  +++ NodeDAO.java	21 Mar 2007 01:24:48 -0000	1.5
  @@ -10,6 +10,13 @@
   import org.jboss.seam.wiki.core.model.File;
   import org.jboss.seam.Component;
   import org.hibernate.Session;
  +import org.hibernate.Criteria;
  +import org.hibernate.ScrollableResults;
  +import org.hibernate.transform.DistinctRootEntityResultTransformer;
  +import org.hibernate.criterion.Order;
  +import org.hibernate.criterion.Example;
  +import org.hibernate.criterion.MatchMode;
  +import org.hibernate.criterion.Restrictions;
   
   import javax.persistence.EntityManager;
   import javax.persistence.EntityNotFoundException;
  @@ -120,6 +127,16 @@
           return null;
       }
   
  +    public List<Document> findDocumentsInDirectoryOrderByCreatedOn(Directory directory, int firstResult, int maxResults) {
  +        //noinspection unchecked
  +        return (List<Document>)restrictedEntityManager
  +                .createQuery("select d from Document d where d.parent = :parentDir order by d.createdOn desc")
  +                .setParameter("parentDir", directory)
  +                .setFirstResult(firstResult)
  +                .setMaxResults(maxResults)
  +                .getResultList();
  +    }
  +
       public List<Document> findDocumentsOrderByLastModified(int maxResults) {
           //noinspection unchecked
           return (List<Document>)restrictedEntityManager
  @@ -223,6 +240,51 @@
           return null;
       }
   
  +    public <N extends Node> List<N> findWithParent(Class<N> nodeType, Directory directory,
  +                    String orderByProperty, boolean orderDescending, int firstResult, int maxResults) {
  +
  +        Criteria crit = prepareCriteria(nodeType, orderByProperty, orderDescending);
  +        crit.add(Restrictions.eq("parent", directory));
  +        if ( !(firstResult == 0 && maxResults == 0) )
  +            crit.setFirstResult(firstResult).setMaxResults(maxResults);
  +        //noinspection unchecked
  +        return crit.list();
  +    }
  +
  +    public int getRowCountWithParent(Class nodeType, Directory directory) {
  +        Criteria crit = prepareCriteria(nodeType, null, false);
  +        crit.add(Restrictions.eq("parent", directory));
  +        return getRowCount(crit);
  +    }
  +
  +    public <N extends Node> int getRowCountByExample(N exampleNode, String... ignoreProperty) {
  +        Criteria crit = prepareCriteria(exampleNode.getClass(), null, false);
  +        appendExample(crit, exampleNode, ignoreProperty);
  +        return getRowCount(crit);
  +    }
  +
  +    private int getRowCount(Criteria criteria) {
  +        restrictedEntityManager.joinTransaction();
  +        ScrollableResults cursor = criteria.scroll();
  +        cursor.last();
  +        int count = cursor.getRowNumber() + 1;
  +        cursor.close();
  +        return count;
  +    }
  +
  +    private Criteria prepareCriteria(Class rootClass, String orderByProperty, boolean orderDescending) {
  +        Criteria crit = getSession().createCriteria(rootClass);
  +        if (orderByProperty != null)
  +                crit.addOrder( orderDescending ? Order.desc(orderByProperty) : Order.asc(orderByProperty) );
  +        return crit.setResultTransformer(new DistinctRootEntityResultTransformer());
  +    }
  +
  +    private <N extends Node> void appendExample(Criteria criteria, N exampleNode, String... ignoreProperty) {
  +        Example example =  Example.create(exampleNode).enableLike(MatchMode.ANYWHERE).ignoreCase();
  +        for (String s : ignoreProperty) example.excludeProperty(s);
  +        criteria.add(example);
  +    }
  +
       private Session getSession() {
           return ((Session)((org.jboss.seam.persistence.EntityManagerProxy) restrictedEntityManager).getDelegate());
       }
  
  
  



More information about the jboss-cvs-commits mailing list