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

Christian Bauer christian at hibernate.org
Fri Nov 9 10:08:26 EST 2007


  User: cbauer  
  Date: 07/11/09 10:08:26

  Modified:    examples/wiki/src/main/org/jboss/seam/wiki/core/dao   
                        WikiTreeNodeAdapter.java FeedDAO.java NodeDAO.java
  Log:
  Various updates to core classes and views, required for forum plugin
  
  Revision  Changes    Path
  1.3       +7 -3      jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/dao/WikiTreeNodeAdapter.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: WikiTreeNodeAdapter.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/dao/WikiTreeNodeAdapter.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -b -r1.2 -r1.3
  --- WikiTreeNodeAdapter.java	24 Sep 2007 08:23:19 -0000	1.2
  +++ WikiTreeNodeAdapter.java	9 Nov 2007 15:08:26 -0000	1.3
  @@ -9,6 +9,13 @@
   
   import java.util.*;
   
  +/**
  + * Adapts the node hierarchy of the wiki to the richfaces tree model.
  + * <p>
  + * TODO: Incomplete and not in use at the moment!
  + *
  + * @author Christian Bauer
  + */
   public class WikiTreeNodeAdapter extends NestedSetNodeWrapper<Node>
           implements org.richfaces.model.TreeNode {
   
  @@ -35,15 +42,12 @@
       public boolean isLeaf() {
           //return !WikiUtil.isDirectory(this.getWrappedNode());
           if (!childrenLoaded && getWrappedNode().getTotalChildCount() != 0) {
  -            System.out.println("###### LOADING CHILDREN OF: " + this);
               loadChildren();
               childrenLoaded = true;
           }
           if (childrenLoaded) {
  -            System.out.println("###### CHILDREN ARE LOADED OF: " + this);
               return getWrappedChildren().size() == 0;
           }
  -        System.out.println("###### HUH?: " + this);
           return true;
       }
   
  
  
  
  1.22      +207 -79   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.21
  retrieving revision 1.22
  diff -u -b -r1.21 -r1.22
  --- FeedDAO.java	22 Oct 2007 05:10:50 -0000	1.21
  +++ FeedDAO.java	9 Nov 2007 15:08:26 -0000	1.22
  @@ -12,10 +12,10 @@
   import org.jboss.seam.wiki.core.engine.WikiTextRenderer;
   import org.jboss.seam.wiki.core.engine.WikiLink;
   import org.jboss.seam.wiki.core.engine.WikiLinkResolver;
  -import org.jboss.seam.wiki.core.action.prefs.WikiPreferences;
   import org.jboss.seam.ui.validator.FormattedTextValidator;
   import org.jboss.seam.wiki.util.WikiUtil;
   import org.jboss.seam.Component;
  +import org.jboss.seam.international.Messages;
   import org.jboss.seam.log.Log;
   
   import javax.persistence.EntityManager;
  @@ -45,76 +45,132 @@
   
       @In protected EntityManager restrictedEntityManager;
   
  -    public List<FeedEntry> findLastFeedEntries(Long feedId, int maxResults) {
  -        return (List<FeedEntry>) restrictedEntityManager
  -                .createQuery("select fe from Feed f join f.feedEntries fe where f.id = :feedId order by fe.publishedDate desc")
  -                .setParameter("feedId", feedId)
  +    /* ############################# FINDERS ################################ */
  +
  +    public Feed findFeed(Long feedId) {
  +        try {
  +            return (Feed) restrictedEntityManager
  +                .createQuery("select f from Feed f where f.id = :id")
  +                .setParameter("id", feedId)
                   .setHint("org.hibernate.cacheable", true)
  -                .setMaxResults(maxResults)
  +                .getSingleResult();
  +        } catch (EntityNotFoundException ex) {
  +        } catch (NoResultException ex) {}
  +        return null;
  +    }
  +
  +    public List<Feed> findFeeds(Document document) {
  +        return restrictedEntityManager
  +                .createQuery("select distinct f from Feed f join f.feedEntries fe where fe.document = :document")
  +                .setParameter("document", document)
                   .getResultList();
       }
   
  -    public void removeFeedEntry(Document document) {
  +    public List<FeedEntry> findFeedEntries(Document document) {
  +        return restrictedEntityManager.createQuery("select fe from FeedEntry fe where fe.document = :doc")
  +            .setParameter("doc", document)
  +            .getResultList();
  +    }
  +
  +    public FeedEntry findFeedEntry(Document document) {
           try {
  -            FeedEntry fe = (FeedEntry)
  -                    restrictedEntityManager.createQuery("select fe from FeedEntry fe where not fe.document is null and fe.document = :doc")
  +            return (FeedEntry)restrictedEntityManager
  +                .createQuery("select fe from FeedEntry fe where fe.document = :doc and fe.commentIdentifier is null")
                   .setParameter("doc", document)
                   .getSingleResult();
  -            if (fe != null) {
  -                // Unlink feed entry from all feeds
  -                Set<Feed> feeds = getAvailableFeeds(document, true);
  -                for (Feed feed : feeds) {
  -                    log.debug("remove feed entry from feed: " + feed.getId());
  -                    feed.getFeedEntries().remove(fe);
  -                }
  -                log.debug("deleting feed entry");
  -                restrictedEntityManager.remove(fe);
  -            }
           } catch (EntityNotFoundException ex) {
           } catch (NoResultException ex) {}
  +        return null;
       }
   
  -    public Feed findFeed(Long feedId) {
  +    public FeedEntry findFeedEntry(Document document, Comment comment) {
           try {
  -            return (Feed) restrictedEntityManager
  -                .createQuery("select f from Feed f where f.id = :id")
  -                .setParameter("id", feedId)
  -                .setHint("org.hibernate.cacheable", true)
  +            return (FeedEntry)restrictedEntityManager
  +                .createQuery("select fe from FeedEntry fe where fe.document = :doc and fe.commentIdentifier = :cid")
  +                .setParameter("doc", document)
  +                .setParameter("cid", comment.getId())
                   .getSingleResult();
           } catch (EntityNotFoundException ex) {
           } catch (NoResultException ex) {}
           return null;
       }
   
  -    public FeedEntry findSiteFeedEntry(Document document) {
  -        try {
  -            return (FeedEntry) restrictedEntityManager
  -                    .createQuery("select fe from Feed f join f.feedEntries fe where f = :feed and fe.document = :document")
  +    public List<FeedEntry> findLastFeedEntries(Long feedId, int maxResults) {
  +        return (List<FeedEntry>) restrictedEntityManager
  +                .createQuery("select fe from Feed f join f.feedEntries fe where f.id = :feedId order by fe.publishedDate desc")
  +                .setParameter("feedId", feedId)
  +                .setHint("org.hibernate.cacheable", true)
  +                .setMaxResults(maxResults)
  +                .getResultList();
  +    }
  +
  +    public boolean isOnSiteFeed(Document document) {
  +        Long count = (Long)restrictedEntityManager
  +                .createQuery("select count(fe) from Feed f join f.feedEntries fe where f = :feed and fe.document = :document")
                       .setParameter("feed", ((Directory)Component.getInstance("wikiRoot")).getFeed() )
                       .setParameter("document", document)
                       .setHint("org.hibernate.cacheable", true)
                       .getSingleResult();
  -        } catch (EntityNotFoundException ex) {
  -        } catch (NoResultException ex) {}
  -        return null;
  +        return count != 0;
  +    }
  +
  +    /* ############################# FEED CUD ################################ */
  +
  +    public void createFeed(Directory dir) {
  +        Feed feed = new Feed();
  +        feed.setDirectory(dir);
  +        feed.setAuthor(dir.getCreatedBy().getFullname());
  +        feed.setTitle(dir.getName());
  +        feed.setDescription(dir.getDescription());
  +        dir.setFeed(feed);
       }
   
  +    public void updateFeed(Directory dir) {
  +        dir.getFeed().setTitle(dir.getName());
  +        dir.getFeed().setAuthor(dir.getCreatedBy().getFullname());
  +        dir.getFeed().setDescription(dir.getDescription());
  +    }
  +
  +    public void removeFeed(Directory dir) {
  +        restrictedEntityManager.remove(dir.getFeed());
  +        dir.setFeed(null);
  +    }
  +
  +    /* ############################# FEEDENTRY CUD ################################ */
  +
  +
       public void createFeedEntry(Document document, boolean pushOnSiteFeed) {
  -        Set<Feed> feeds = getAvailableFeeds(document, pushOnSiteFeed);
  +        createFeedEntry(document, null, pushOnSiteFeed, null);
  +    }
  +
  +    public void createFeedEntry(Document document, Comment comment, boolean pushOnSiteFeed) {
  +        createFeedEntry(document, comment, pushOnSiteFeed, null);
  +    }
  +
  +    public void createFeedEntry(Document document, boolean pushOnSiteFeed, String overrideTitle) {
  +        createFeedEntry(document, null, pushOnSiteFeed, overrideTitle);
  +    }
  +
  +    public void createFeedEntry(Document document, Comment comment, boolean pushOnSiteFeed, String overrideTitle) {
  +        Set<Feed> feeds = getAvailableFeeds(document, pushOnSiteFeed, true);
   
           // Now create a feedentry and link it to all the feeds
           if (feeds.size() >0) {
               log.debug("creating new feed entry for document: " + document.getId());
  +
               FeedEntry fe = new FeedEntry();
  -            fe.setLink(renderFeedURL(document));
  -            fe.setTitle(document.getName());
  -            fe.setAuthor(document.getCreatedBy().getFullname());
  +            fe.setLink(renderFeedURL(document, comment));
  +            fe.setTitle(renderTitle(document, comment, overrideTitle));
  +            fe.setAuthor(renderAuthor(document, comment));
               fe.setUpdatedDate(fe.getPublishedDate());
               // Do NOT use text/html, the fabulous Sun "Rome" software will
               // render type="HTML" (uppercase!) which kills the Firefox feed renderer!
               fe.setDescriptionType("html");
  -            fe.setDescriptionValue(renderWikiText(document.getContent()));
  +            fe.setDescriptionValue(renderDescription(document, comment));
               fe.setDocument(document);
  +
  +            if (comment != null) fe.setCommentIdentifier(comment.getId());
  +
               restrictedEntityManager.persist(fe);
               for (Feed feed : feeds) {
                   log.debug("linking new feed entry with feed: " + feed.getId());
  @@ -124,65 +180,137 @@
       }
   
       public void updateFeedEntry(Document document, boolean pushOnSiteFeed) {
  -        try {
  -            FeedEntry fe = (FeedEntry)restrictedEntityManager.createQuery("select fe from FeedEntry fe where fe.document = :doc")
  -                    .setParameter("doc", document).getSingleResult();
  +        updateFeedEntry(document, null, pushOnSiteFeed, null);
  +    }
  +
  +    public void updateFeedEntry(Document document, Comment comment, boolean pushOnSiteFeed) {
  +        updateFeedEntry(document, comment, pushOnSiteFeed, null);
  +    }
  +
  +    public void updateFeedEntry(Document document, boolean pushOnSiteFeed, String overrideTitle) {
  +        updateFeedEntry(document, null, pushOnSiteFeed, overrideTitle);
  +    }
  +
  +    // This absolutely needs to be called in beforeUpdate(), so that the lastModifiedOn() timestamp of
  +    // the document is still the old one, not the new Date()!
  +    public void updateFeedEntry(Document document, Comment comment, boolean pushOnSiteFeed, String overrideTitle) {
  +        FeedEntry feedEntry;
  +        if (comment == null) {
  +            feedEntry = findFeedEntry(document);
  +        } else {
  +            feedEntry = findFeedEntry(document, comment);
  +        }
  +
  +        if (feedEntry == null) {
  +            log.debug("no feed entry for updating found");
  +            createFeedEntry(document, comment, pushOnSiteFeed, overrideTitle);
  +            return;
  +        }
   
  -            log.debug("updating feed entry: " + fe.getId());
  +        log.debug("updating feed entry: " + feedEntry.getId());
   
  -            // Update the feed entry for this document
  -            fe.setLink(renderFeedURL(document));
  -            fe.setUpdatedDate(document.getLastModifiedOn());
  -            fe.setTitle(document.getName());
  -            fe.setAuthor(document.getCreatedBy().getFullname());
  -            fe.setDescriptionValue(renderWikiText(document.getContent()));
  +        feedEntry.setLink(renderFeedURL(document, comment));
  +        feedEntry.setUpdatedDate(comment == null ? document.getLastModifiedOn() : comment.getCreatedOn());
  +        feedEntry.setTitle(renderTitle(document, comment, overrideTitle));
  +        feedEntry.setAuthor(renderAuthor(document, comment));
  +        feedEntry.setDescriptionValue(renderDescription(document, comment));
   
               // Link feed entry with all feeds (there might be new feeds since this feed entry was created)
  -            Set<Feed> feeds = getAvailableFeeds(document, pushOnSiteFeed);
  +        Set<Feed> feeds = getAvailableFeeds(document, pushOnSiteFeed, true);
               for (Feed feed : feeds) {
                   log.debug("linking feed entry with feed: " + feed.getId());
  -                feed.getFeedEntries().add(fe);
  +            feed.getFeedEntries().add(feedEntry);
               }
  -        } catch (NoResultException ex) {
  -            // Couldn't find feed entry for this document, create a new one
  -            log.debug("no feed entry for updating found");
  -            createFeedEntry(document, pushOnSiteFeed);
  +    }
  +
  +    public void removeFeedEntry(Document document, Comment comment) {
  +        removeFeedEntry(document, findFeedEntry(document, comment) );
  +    }
  +
  +    public void removeFeedEntries(Document document) {
  +        List<FeedEntry> entries = findFeedEntries(document);
  +        if (entries.size() != 0) for (FeedEntry fe : entries) removeFeedEntry(document, fe);
  +    }
  +
  +    private void removeFeedEntry(Document document, FeedEntry feedEntry) {
  +        if (feedEntry != null) {
  +            // Unlink feed entry from all feeds
  +            List<Feed> feeds = findFeeds(document);
  +            for (Feed feed : feeds) {
  +                log.debug("remove feed entry from feed: " + feed.getId());
  +                feed.getFeedEntries().remove(feedEntry);
  +            }
  +            log.debug("deleting feed entry");
  +            restrictedEntityManager.remove(feedEntry);
           }
       }
   
  -    // TODO: Maybe the wiki needs a real maintenance thread at some point... @Observer("Feeds.purgeFeedEntries")
  -    public void purgeOldFeedEntries() {
  +    public void purgeOldFeedEntries(Date olderThan) {
           // Clean up _all_ feed entries that are older than N days
  -        WikiPreferences wikiPrefs = (WikiPreferences) Component.getInstance("wikiPreferences");
  -        Calendar oldestDate = GregorianCalendar.getInstance();
  -        oldestDate.roll(Calendar.DAY_OF_YEAR, -wikiPrefs.getPurgeFeedEntriesAfterDays().intValue());
           int result = restrictedEntityManager.createQuery("delete from FeedEntry fe where fe.updatedDate < :oldestDate")
  -                .setParameter("oldestDate", oldestDate.getTime()).executeUpdate();
  +                .setParameter("oldestDate", olderThan).executeUpdate();
           log.debug("cleaned up " + result + " outdated feed entries");
       }
   
  -    private Set<Feed> getAvailableFeeds(Document document, boolean includeSiteFeed) {
  +    /* ############################# INTERNAL ################################ */
  +
  +    private Set<Feed> getAvailableFeeds(Document document, boolean includeSiteFeed, boolean restrictAccess) {
           // Walk up the directory tree and extract all the feeds from directories
           Set<Feed> feeds = new HashSet<Feed>();
  -        Node temp = document.getParent();
  -        while (temp.getParent() != null) {
  -            if (temp instanceof Directory && ((Directory)temp).getFeed() != null)
  -                feeds.add( ((Directory)temp).getFeed());
  -            temp = temp.getParent();
  +        Node dir = document.getParent();
  +        while (dir.getParent() != null) {
  +            // Only include feeds if the directory (owner of feed) has lower or equal read access level as the doc
  +            if (dir instanceof Directory && ((Directory)dir).getFeed() != null &&
  +                (!restrictAccess || dir.getReadAccessLevel() <= document.getReadAccessLevel()) ) {
  +                feeds.add( ((Directory)dir).getFeed());
  +            }
  +            dir = dir.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( ((Directory)temp).getFeed());
  +        if (includeSiteFeed && (!restrictAccess || dir.getReadAccessLevel() <= document.getReadAccessLevel()) )
  +            feeds.add( ((Directory)dir).getFeed());
   
           return feeds;
       }
   
  -    private String renderFeedURL(Node node) {
  -        /*
  -        WikiPreferences wikiPrefs = (WikiPreferences) Component.getInstance("wikiPreferences");
  -        return wikiPrefs.getBaseUrl() + "/" + node.getId() + wikiPrefs.getPermlinkSuffix();
  -        */
  -        return WikiUtil.renderURL(node);
  +    private String renderTitle(Document document, Comment comment, String overrideTitle) {
  +        if (overrideTitle != null) {
  +            return overrideTitle;
  +        } else if (comment != null) {
  +            return comment.getSubject();
  +        } else {
  +            return document.getName();
  +        }
  +    }
  +
  +    private String renderAuthor(Document document, Comment comment) {
  +        if (comment != null && comment.getFromUser() != null) {
  +            return comment.getFromUser().getFullname();
  +        } else if (comment != null) {
  +            return comment.getFromUserName();
  +        } else {
  +            return document.getCreatedBy().getFullname();
  +        }
  +    }
  +
  +    private String renderDescription(Document document, Comment comment) {
  +        if (comment != null) {
  +            StringBuilder desc = new StringBuilder();
  +            desc.append(Messages.instance().get("lacewiki.msg.comment.FeedIntro"));
  +            desc.append("&#160;");
  +            desc.append("<a href=\"").append(WikiUtil.renderPermLink(document)).append("\">");
  +            desc.append("'").append(document.getName()).append("'");
  +            desc.append("</a>.");
  +            desc.append("<hr/>");
  +            desc.append(renderWikiText(comment.getText()));
  +            return desc.toString();
  +        }
  +        return renderWikiText(document.getContent());
  +    }
  +
  +    private String renderFeedURL(Node node, Comment comment) {
  +        return WikiUtil.renderURL(node, comment);
       }
   
       private String renderWikiText(String wikiText) {
  @@ -199,7 +327,7 @@
                   public String renderInlineLink(WikiLink inlineLink) {
                       return !inlineLink.isBroken() ?
                               "<a href=\""
  -                            + renderFeedURL(inlineLink.getNode())
  +                            + renderFeedURL(inlineLink.getNode(), null)
                               + "\">"
                               + inlineLink.getDescription()
                               + "</a>" : "[Broken Link]";
  @@ -222,7 +350,7 @@
                   }
   
                   public String renderMacro(String macroName) {
  -                    return "[Embedded Plugin]";
  +                    return "";
                   }
   
                   public void setAttachmentLinks(List<WikiLink> attachmentLinks) {}
  
  
  
  1.30      +2 -2      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.29
  retrieving revision 1.30
  diff -u -b -r1.29 -r1.30
  --- NodeDAO.java	25 Sep 2007 14:30:44 -0000	1.29
  +++ NodeDAO.java	9 Nov 2007 15:08:26 -0000	1.30
  @@ -306,11 +306,11 @@
                   public Node duplicate(Node nestedSetNode) {
                       Node copy = null;
                       if (nestedSetNode instanceof Document) {
  -                        copy = new Document((Document)nestedSetNode);
  +                        copy = new Document((Document)nestedSetNode, false); // Do not copy the content!
                       } else if (nestedSetNode instanceof Directory) {
                           copy = new Directory((Directory)nestedSetNode);
                       } else if (nestedSetNode instanceof File) {
  -                        copy = new File((File)nestedSetNode);
  +                        copy = new File((File)nestedSetNode, false);
                       }
                       if (copy != null) {
                           copy.setId(nestedSetNode.getId());
  
  
  



More information about the jboss-cvs-commits mailing list