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

Christian Bauer christian at hibernate.org
Fri Aug 17 09:00:26 EDT 2007


  User: cbauer  
  Date: 07/08/17 09:00:26

  Modified:    examples/wiki/src/main/org/jboss/seam/wiki/core/dao   
                        WikiNodeFactory.java FeedDAO.java NodeDAO.java
  Log:
  Major refactoring of core data schema and some new features
  
  Revision  Changes    Path
  1.9       +7 -7      jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/dao/WikiNodeFactory.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: WikiNodeFactory.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/dao/WikiNodeFactory.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -b -r1.8 -r1.9
  --- WikiNodeFactory.java	20 Jun 2007 01:30:37 -0000	1.8
  +++ WikiNodeFactory.java	17 Aug 2007 13:00:26 -0000	1.9
  @@ -1,3 +1,9 @@
  +/*
  + * JBoss, Home of Professional Open Source
  + *
  + * Distributable under LGPL license.
  + * See terms of license at gnu.org.
  + */
   package org.jboss.seam.wiki.core.dao;
   
   import org.jboss.seam.annotations.*;
  @@ -17,6 +23,7 @@
   import java.util.*;
   
   @Name("wikiNodeFactory")
  + at Transactional
   public class WikiNodeFactory implements Serializable {
   
       @In
  @@ -25,9 +32,7 @@
       @In
       protected EntityManager restrictedEntityManager;
   
  -
       @Factory(value = "wikiRoot", scope = ScopeType.CONVERSATION, autoCreate = true)
  -    @Transactional
       public Directory loadWikiRoot() {
           entityManager.joinTransaction();
           try {
  @@ -41,7 +46,6 @@
       }
   
       @Factory(value = "wikiStart", scope = ScopeType.CONVERSATION, autoCreate = true)
  -    @Transactional
       public Document loadWikiStart() {
           restrictedEntityManager.joinTransaction();
           WikiPreferences wikiPreferences = (WikiPreferences) Component.getInstance("wikiPreferences");
  @@ -61,7 +65,6 @@
   
       // Loads the same instance into a different persistence context
       @Factory(value = "restrictedWikiRoot", scope = ScopeType.CONVERSATION, autoCreate = true)
  -    @Transactional
       public Directory loadWikiRootRestricted() {
           Directory wikiroot = (Directory) Component.getInstance("wikiRoot");
   
  @@ -78,7 +81,6 @@
       }
   
       @Factory(value = "memberArea", scope = ScopeType.CONVERSATION, autoCreate = true)
  -    @Transactional
       public Directory loadMemberArea() {
           Long memberAreaId = ((WikiPreferences)Component.getInstance("wikiPreferences")).getMemberAreaId();
           entityManager.joinTransaction();
  @@ -89,7 +91,6 @@
                       .setHint("org.hibernate.cacheable", true)
                       .getSingleResult();
           } catch (RuntimeException ex) {
  -            System.out.println("######################### MESSAGE ###############################");
               FacesMessages.instance().addFromResourceBundleOrDefault(
                   FacesMessage.SEVERITY_ERROR,
                   "memberHomeDirectoryNotFound",
  @@ -99,7 +100,6 @@
       }
   
       @Factory(value = "linkProtocolMap", scope = ScopeType.CONVERSATION, autoCreate = true)
  -    @Transactional
       public Map<String, LinkProtocol> loadLinkProtocols() {
           entityManager.joinTransaction();
           Map<String, LinkProtocol> linkProtocols = new TreeMap<String, LinkProtocol>();
  
  
  
  1.11      +20 -4     jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/dao/FeedDAO.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: FeedDAO.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/dao/FeedDAO.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -b -r1.10 -r1.11
  --- FeedDAO.java	6 Jul 2007 14:38:38 -0000	1.10
  +++ FeedDAO.java	17 Aug 2007 13:00:26 -0000	1.11
  @@ -1,7 +1,12 @@
  +/*
  + * JBoss, Home of Professional Open Source
  + *
  + * Distributable under LGPL license.
  + * See terms of license at gnu.org.
  + */
   package org.jboss.seam.wiki.core.dao;
   
   import org.jboss.seam.annotations.*;
  -import org.jboss.seam.annotations.Observer;
   import org.jboss.seam.wiki.core.model.*;
   import org.jboss.seam.wiki.core.engine.WikiTextParser;
   import org.jboss.seam.wiki.core.engine.WikiTextRenderer;
  @@ -33,6 +38,16 @@
   
       @In protected EntityManager restrictedEntityManager;
   
  +    public List<FeedEntry> findLastFeedEntries(Long feedId, int maxResults) {
  +        restrictedEntityManager.joinTransaction();
  +        return (List<FeedEntry>) restrictedEntityManager
  +                .createQuery("select fe from Feed f join f.feedEntries fe where f.id = :feedId order by f.publishedDate desc")
  +                .setParameter("feedId", feedId)
  +                .setHint("org.hibernate.cacheable", true)
  +                .setMaxResults(maxResults)
  +                .getResultList();
  +    }
  +
       public Feed findFeed(Long feedId) {
           restrictedEntityManager.joinTransaction();
           try {
  @@ -117,14 +132,15 @@
       private Set<Feed> getAvailableFeeds(boolean includeSiteFeed, Document document) {
           // Walk up the directory tree and extract all the feeds from directories
           Set<Feed> feeds = new HashSet<Feed>();
  -        Directory temp = document.getParent();
  +        Node temp = document.getParent();
           while (temp.getParent() != null) {
  -            if (temp.getFeed() != null) feeds.add(temp.getFeed());
  +            if (temp instanceof Directory && ((Directory)temp).getFeed() != null)
  +                feeds.add( ((Directory)temp).getFeed());
               temp = temp.getParent();
           }
   
           // If the user wants it on the site feed, that's the wiki root feed which is the top of the dir tree
  -        if (includeSiteFeed) feeds.add(temp.getFeed());
  +        if (includeSiteFeed) feeds.add( ((Directory)temp).getFeed());
   
           return feeds;
       }
  
  
  
  1.18      +101 -10   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.17
  retrieving revision 1.18
  diff -u -b -r1.17 -r1.18
  --- NodeDAO.java	12 Jun 2007 12:30:03 -0000	1.17
  +++ NodeDAO.java	17 Aug 2007 13:00:26 -0000	1.18
  @@ -1,27 +1,31 @@
  +/*
  + * JBoss, Home of Professional Open Source
  + *
  + * Distributable under LGPL license.
  + * See terms of license at gnu.org.
  + */
   package org.jboss.seam.wiki.core.dao;
   
   import org.jboss.seam.annotations.Name;
   import org.jboss.seam.annotations.In;
   import org.jboss.seam.annotations.Transactional;
   import org.jboss.seam.annotations.AutoCreate;
  -import org.jboss.seam.wiki.core.model.Node;
  -import org.jboss.seam.wiki.core.model.Directory;
  -import org.jboss.seam.wiki.core.model.Document;
  -import org.jboss.seam.wiki.core.model.File;
  +import org.jboss.seam.wiki.core.model.*;
  +import org.jboss.seam.wiki.core.nestedset.NestedSetNode;
  +import org.jboss.seam.wiki.core.nestedset.NestedSetNodeWrapper;
  +import org.jboss.seam.wiki.core.nestedset.NestedSetResultTransformer;
   import org.jboss.seam.Component;
   import org.hibernate.Session;
   import org.hibernate.Criteria;
   import org.hibernate.ScrollableResults;
  +import org.hibernate.Query;
   import org.hibernate.transform.DistinctRootEntityResultTransformer;
   import org.hibernate.criterion.*;
   
   import javax.persistence.EntityManager;
   import javax.persistence.EntityNotFoundException;
   import javax.persistence.NoResultException;
  -import java.util.List;
  -import java.util.ArrayList;
  -import java.util.Map;
  -import java.util.HashMap;
  +import java.util.*;
   
   /**
    * DAO for nodes, transparently respects security access levels.
  @@ -79,6 +83,7 @@
                       .createQuery("select n from Node n where n.areaNumber = :areaNumber and n.wikiname = :wikiname")
                       .setParameter("areaNumber", areaNumber)
                       .setParameter("wikiname", wikiname)
  +                    .setHint("org.hibernate.comment", "Find node in area")
                       .setHint("org.hibernate.cacheable", true)
                       .getSingleResult();
           } catch (EntityNotFoundException ex) {
  @@ -95,6 +100,7 @@
                       .createQuery("select d from Document d where d.areaNumber = :areaNumber and d.wikiname = :wikiname")
                       .setParameter("areaNumber", areaNumber)
                       .setParameter("wikiname", wikiname)
  +                    .setHint("org.hibernate.comment", "Find document in area")
                       .setHint("org.hibernate.cacheable", true)
                       .getSingleResult();
           } catch (EntityNotFoundException ex) {
  @@ -111,6 +117,7 @@
                       .createQuery("select d from Directory d where d.areaNumber = :areaNumber and d.wikiname = :wikiname")
                       .setParameter("areaNumber", areaNumber)
                       .setParameter("wikiname", wikiname)
  +                    .setHint("org.hibernate.comment", "Find directory in area")
                       .setHint("org.hibernate.cacheable", true)
                       .getSingleResult();
           } catch (EntityNotFoundException ex) {
  @@ -127,6 +134,7 @@
                       .createQuery("select d from Directory d where d.parent = :root and d.wikiname = :wikiname")
                       .setParameter("root", Component.getInstance("wikiRoot"))
                       .setParameter("wikiname", wikiname)
  +                    .setHint("org.hibernate.comment", "Find area by wikiname")
                       .setHint("org.hibernate.cacheable", true)
                       .getSingleResult();
           } catch (EntityNotFoundException ex) {
  @@ -143,6 +151,7 @@
                       .createQuery("select d from Directory d where d.parent = :root and d.areaNumber = :areaNumber")
                       .setParameter("root", Component.getInstance("wikiRoot"))
                       .setParameter("areaNumber", areaNumber)
  +                    .setHint("org.hibernate.comment", "Find area by area number")
                       .setHint("org.hibernate.cacheable", true)
                       .getSingleResult();
           } catch (EntityNotFoundException ex) {
  @@ -156,6 +165,7 @@
           return (List<Document>)restrictedEntityManager
                   .createQuery("select d from Document d where d.parent = :parentDir order by d.createdOn desc")
                   .setParameter("parentDir", directory)
  +                .setHint("org.hibernate.comment", "Find documents in directory order by createdOn")
                   .setFirstResult(firstResult)
                   .setMaxResults(maxResults)
                   .getResultList();
  @@ -165,6 +175,7 @@
           //noinspection unchecked
           return (List<Document>)restrictedEntityManager
                   .createQuery("select d from Document d order by d.lastModifiedOn desc")
  +                .setHint("org.hibernate.comment", "Find documents order by lastModified")
                   .setMaxResults(maxResults)
                   .getResultList();
       }
  @@ -207,6 +218,7 @@
               return (Document) restrictedEntityManager
                       .createQuery("select d from Document d where d.id = :id")
                       .setParameter("id", documentId)
  +                    .setHint("org.hibernate.comment", "Find document by id")
                       .setHint("org.hibernate.cacheable", true)
                       .getSingleResult();
           } catch (EntityNotFoundException ex) {
  @@ -222,6 +234,7 @@
               return (Directory) restrictedEntityManager
                       .createQuery("select d from Directory d where d.id = :id")
                       .setParameter("id", directoryId)
  +                    .setHint("org.hibernate.comment", "Find directory by id")
                       .setHint("org.hibernate.cacheable", true)
                       .getSingleResult();
           } catch (EntityNotFoundException ex) {
  @@ -237,6 +250,7 @@
               return (File) restrictedEntityManager
                       .createQuery("select f from File f where f.id = :id")
                       .setParameter("id", fileId)
  +                    .setHint("org.hibernate.comment", "Find file by id")
                       .setHint("org.hibernate.cacheable", true)
                       .getSingleResult();
           } catch (EntityNotFoundException ex) {
  @@ -245,7 +259,7 @@
           return null;
       }
   
  -    public Document findDefaultDocument(Directory directory) {
  +    public Document findDefaultDocument(Node directory) {
           if (directory == null) return null;
           restrictedEntityManager.joinTransaction();
           try {
  @@ -253,6 +267,7 @@
                       .createQuery("select doc from Document doc, Directory dir" +
                                    " where doc.id = dir.defaultDocument.id and dir.id = :did")
                       .setParameter("did", directory.getId())
  +                    .setHint("org.hibernate.comment", "Find default document")
                       .setHint("org.hibernate.cacheable", true)
                       .getSingleResult();
           } catch (EntityNotFoundException ex) {
  @@ -264,8 +279,9 @@
       public Map<Long,Long> findCommentCount(Directory directory) {
           //noinspection unchecked
           List<Object[]> result = restrictedEntityManager
  -                .createQuery("select n.nodeId, count(c) from Node n, Comment c where c.document = n and n.parent is :parent group by n.nodeId")
  +                .createQuery("select n.nodeId, count(c) from Node n, Comment c where c.document = n and n.parent = :parent group by n.nodeId")
                   .setParameter("parent", directory)
  +                .setHint("org.hibernate.comment", "Find comment cound for all nodes in directory")
                   .getResultList();
   
           Map<Long,Long> resultMap = new HashMap<Long,Long>(result.size());
  @@ -275,6 +291,81 @@
           return resultMap;
       }
   
  +    public NestedSetNodeWrapper<Node> findMenuItems(Node startNode, Long maxDepth, Long flattenToLevel, boolean showAdminOnly) {
  +
  +        // Needs to be equals() safe (SortedSet):
  +        // - compare by display position, if equal
  +        // - compare by name, if equal
  +        // - compare by id
  +        Comparator<NestedSetNodeWrapper<Node>> comp =
  +            new Comparator<NestedSetNodeWrapper<Node>>() {
  +                public int compare(NestedSetNodeWrapper<Node> o1, NestedSetNodeWrapper<Node> o2) {
  +                    Node node1 = o1.getWrappedNode();
  +                    Node node2 = o2.getWrappedNode();
  +                    if (node1.getDisplayPosition().compareTo(node2.getDisplayPosition()) != 0) {
  +                        return node1.getDisplayPosition().compareTo(node2.getDisplayPosition());
  +                    } else if (node1.getName().compareTo(node2.getName()) != 0) {
  +                        return node1.getName().compareTo(node2.getName());
  +                    }
  +                    return node1.getId().compareTo(node2.getId());
  +                }
  +            };
  +
  +        NestedSetNodeWrapper<Node> startNodeWrapper = new NestedSetNodeWrapper<Node>(startNode, comp);
  +        NestedSetResultTransformer<Node> transformer = new NestedSetResultTransformer<Node>(startNodeWrapper, flattenToLevel);
  +
  +        appendNestedSetNodes(transformer, maxDepth, showAdminOnly, "n1.menuItem = true");
  +        return startNodeWrapper;
  +    }
  +
  +    public <N extends NestedSetNode> void appendNestedSetNodes(NestedSetResultTransformer<N> transformer,
  +                                                               Long maxDepth,
  +                                                               boolean showAdminOnly,
  +                                                               String... restrictionFragment) {
  +
  +        N startNode = transformer.getRootWrapper().getWrappedNode();
  +        StringBuffer queryString = new StringBuffer();
  +
  +        queryString.append("select").append(" ");
  +        queryString.append("count(n1.id) as nestedSetNodeLevel").append(", ");
  +        queryString.append("n1 as nestedSetNode").append(" ");
  +        queryString.append("from ").append(startNode.getTreeSuperclassEntityName()).append(" n1, ");
  +        queryString.append(startNode.getTreeSuperclassEntityName()).append(" n2 ");
  +        queryString.append("where n1.nsThread = :thread and n2.nsThread = :thread").append(" ");
  +        queryString.append("and n1.nsLeft between n2.nsLeft and n2.nsRight").append(" ");
  +        queryString.append("and n2.nsLeft > :startLeft and n2.nsRight < :startRight").append(" ");
  +        if (showAdminOnly) {
  +            queryString.append("and n1.createdBy = :adminUser").append(" ");
  +        }
  +        for (String fragment: restrictionFragment) {
  +            queryString.append("and ").append(fragment).append(" ");
  +        }
  +        queryString.append("group by").append(" ");
  +        for (int i = 0; i < startNode.getTreeSuperclassPropertiesForGrouping().length; i++) {
  +            queryString.append("n1.").append(startNode.getTreeSuperclassPropertiesForGrouping()[i]);
  +            if (i != startNode.getTreeSuperclassPropertiesForGrouping().length-1) queryString.append(", ");
  +        }
  +        queryString.append(" ");
  +
  +        if (maxDepth != null) {
  +            queryString.append("having count(n1.id) <= :maxDepth").append(" ");
  +        }
  +
  +        queryString.append("order by n1.nsLeft");
  +
  +        Query nestedSetQuery = getSession().createQuery(queryString.toString());
  +        nestedSetQuery.setParameter("thread", startNode.getNsThread());
  +        nestedSetQuery.setParameter("startLeft", startNode.getNsLeft());
  +        nestedSetQuery.setParameter("startRight", startNode.getNsRight());
  +        if (showAdminOnly) nestedSetQuery.setParameter("adminUser", Component.getInstance("adminUser"));
  +        if (maxDepth != null) nestedSetQuery.setParameter("maxDepth", maxDepth);
  +
  +        nestedSetQuery.setCacheable(true);
  +
  +        nestedSetQuery.setResultTransformer(transformer);
  +        nestedSetQuery.list(); // Append all children hierarchically to the startNodeWrapper
  +    }
  +
       public <N extends Node> List<N> findWithParent(Class<N> nodeType, Directory directory, Node ignoreNode,
                                                      String orderByProperty, boolean orderDescending, long firstResult, long maxResults) {
   
  
  
  



More information about the jboss-cvs-commits mailing list