[jboss-cvs] jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/feeds ...
Christian Bauer
christian at hibernate.org
Tue Dec 18 23:29:22 EST 2007
User: cbauer
Date: 07/12/18 23:29:22
Added: examples/wiki/src/main/org/jboss/seam/wiki/core/feeds
FeedDAO.java WikiDocumentFeedEntryManager.java
FeedEntryManager.java
WikiCommentFeedEntryManager.java
Log:
Major rewrite of the most of the application
Revision Changes Path
1.1 date: 2007/12/19 04:29:22; author: cbauer; state: Exp;jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/feeds/FeedDAO.java
Index: FeedDAO.java
===================================================================
/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.seam.wiki.core.feeds;
import org.jboss.seam.Component;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.log.Log;
import org.jboss.seam.wiki.core.model.*;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import java.util.Date;
import java.util.List;
/**
* DAO for feeds.
* <p>
* Uses the <tt>restrictedEntityManager</tt> because it is used in the context of
* directory/document/comment editing (same PC).
* </p>
* <p>
*
* @author Christian Bauer
*
*/
@Name("feedDAO")
@AutoCreate
public class FeedDAO {
@Logger static Log log;
@In protected EntityManager restrictedEntityManager;
/* ############################# FINDERS ################################ */
public List<Feed> findFeeds() {
return restrictedEntityManager
.createQuery("select f from Feed f join fetch f.directory d order by d.createdOn asc")
.getResultList();
}
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)
.getSingleResult();
} catch (EntityNotFoundException ex) {
} catch (NoResultException ex) {}
return null;
}
public List<Feed> findFeeds(WikiDocument document) {
if (document == null || document.getId() == null) throw new IllegalArgumentException("document is null or unsaved");
return restrictedEntityManager
.createQuery(
"select distinct f from WikiDocumentFeedEntry fe, Feed f join f.feedEntries allFe " +
" where fe.document = :doc and fe = allFe order by f.publishedDate desc"
)
.setParameter("doc", document)
.getResultList();
}
public List<Feed> findFeeds(WikiComment comment) {
if (comment == null || comment.getId() == null) throw new IllegalArgumentException("comment is null or unsaved");
return restrictedEntityManager
.createQuery(
"select distinct f from WikiCommentFeedEntry fe, Feed f join f.feedEntries allFe " +
" where fe.comment = :comment and fe = allFe order by f.publishedDate desc"
)
.setParameter("comment", comment)
.getResultList();
}
public List<Feed> findParentFeeds(WikiDirectory startDir, boolean includeSiteFeed) {
StringBuilder queryString = new StringBuilder();
queryString.append("select f from WikiDirectory d join d.feed f ");
queryString.append("where d.nodeInfo.nsThread = :nsThread and ");
queryString.append("d.nodeInfo.nsLeft <= :nsLeft and d.nodeInfo.nsRight >= :nsRight ");
if (!includeSiteFeed) queryString.append("and not d = :wikiRoot ");
queryString.append("order by f.publishedDate desc ");
Query query = restrictedEntityManager.createQuery(queryString.toString())
.setParameter("nsThread", startDir.getNodeInfo().getNsThread())
.setParameter("nsLeft", startDir.getNodeInfo().getNsLeft())
.setParameter("nsRight", startDir.getNodeInfo().getNsRight());
if (!includeSiteFeed)
query.setParameter("wikiRoot", Component.getInstance("wikiRoot"));
return query.getResultList();
}
public List<FeedEntry> findFeedEntriesasdf(WikiFile file) {
return restrictedEntityManager.createQuery("select fe from FeedEntry fe where fe.file = :file")
.setParameter("file", file)
.getResultList();
}
public WikiDocumentFeedEntry findFeedEntry(WikiDocument document) {
try {
return (WikiDocumentFeedEntry)restrictedEntityManager
.createQuery("select fe from WikiDocumentFeedEntry fe where fe.document = :document")
.setParameter("document", document)
.getSingleResult();
} catch (EntityNotFoundException ex) {
} catch (NoResultException ex) {}
return null;
}
public WikiCommentFeedEntry findFeedEntry(WikiComment comment) {
try {
return (WikiCommentFeedEntry)restrictedEntityManager
.createQuery("select fe from WikiCommentFeedEntry fe where fe.comment = :comment")
.setParameter("comment", comment)
.getSingleResult();
} catch (EntityNotFoundException ex) {
} catch (NoResultException ex) {}
return null;
}
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(WikiDocument document) {
if (document == null || document.getId() == null) throw new IllegalArgumentException("document is null or unsaved");
Long count = (Long)restrictedEntityManager
.createQuery("select count(fe) from WikiDocumentFeedEntry fe, Feed f join f.feedEntries allFe " +
" where f = :feed and fe.document = :doc and fe = allFe")
.setParameter("feed", ((WikiDirectory)Component.getInstance("wikiRoot")).getFeed() )
.setParameter("doc", document)
.setHint("org.hibernate.cacheable", true)
.getSingleResult();
return count != 0;
}
/* ############################# FEED CUD ################################ */
public void createFeed(WikiDirectory 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(WikiDirectory dir) {
dir.getFeed().setTitle(dir.getName());
dir.getFeed().setAuthor(dir.getCreatedBy().getFullname());
dir.getFeed().setDescription(dir.getDescription());
}
public void removeFeed(WikiDirectory dir) {
restrictedEntityManager.remove(dir.getFeed());
dir.setFeed(null);
}
/* ############################# FEEDENTRY CUD ################################ */
public void createFeedEntry(WikiDirectory parentDir, WikiNode node, FeedEntry feedEntry, boolean pushOnSiteFeed) {
List<Feed> feeds = findParentFeeds(parentDir, pushOnSiteFeed);
// Now create a feedentry and link it to all the feeds
if (feeds.size() >0) {
log.debug("persisting new feed entry for: " + node);
restrictedEntityManager.persist(feedEntry);
for (Feed feed : feeds) {
log.debug("linking new feed entry with feed: " + feed.getId());
feed.getFeedEntries().add(feedEntry);
}
} else {
log.debug("no available feeds found");
}
}
public void updateFeedEntry(WikiDirectory parentDir, WikiNode node, FeedEntry feedEntry, boolean pushOnSiteFeed) {
log.debug("updating feed entry: " + feedEntry.getId());
// Link feed entry with all feeds (there might be new feeds since this feed entry was created)
List<Feed> feeds = findParentFeeds(parentDir, pushOnSiteFeed);
for (Feed feed : feeds) {
log.debug("linking feed entry with feed: " + feed.getId());
feed.getFeedEntries().add(feedEntry);
}
}
public void removeFeedEntry(List<Feed> feeds, FeedEntry feedEntry) {
if (feedEntry == null) return;
// Unlink feed entry from all feeds
for (Feed feed : feeds) {
log.debug("remove feed entry from feed: " + feed);
feed.getFeedEntries().remove(feedEntry);
}
log.debug("deleting feed entry");
restrictedEntityManager.remove(feedEntry);
}
public void purgeOldFeedEntries(Date olderThan) {
log.debug("cleaning up feed entries older than: " + olderThan);
// Clean up _all_ feed entries that are older than N days
int result = restrictedEntityManager.createQuery("delete from FeedEntry fe where fe.publishedDate < :oldestDate")
.setParameter("oldestDate", olderThan).executeUpdate();
log.debug("cleaned up " + result + " outdated feed entries");
}
}
1.1 date: 2007/12/19 04:29:22; author: cbauer; state: Exp;jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/feeds/WikiDocumentFeedEntryManager.java
Index: WikiDocumentFeedEntryManager.java
===================================================================
/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.seam.wiki.core.feeds;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.wiki.core.model.WikiDocument;
import org.jboss.seam.wiki.core.model.WikiDocumentFeedEntry;
import org.jboss.seam.wiki.util.WikiUtil;
@Name("wikiDocumentFeedEntryManager")
public class WikiDocumentFeedEntryManager extends FeedEntryManager<WikiDocument, WikiDocumentFeedEntry> {
public WikiDocumentFeedEntry createFeedEntry(WikiDocument document) {
WikiDocumentFeedEntry fe = new WikiDocumentFeedEntry();
fe.setLink(WikiUtil.renderURL(document));
fe.setTitle(getFeedEntryTitle(document));
fe.setAuthor(document.getCreatedBy().getFullname());
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.getAreaNumber(), document.getFeedDescription()));
fe.setDocument(document);
return fe;
}
public void updateFeedEntry(WikiDocumentFeedEntry fe, WikiDocument document) {
fe.setLink(WikiUtil.renderURL(document));
fe.setTitle(getFeedEntryTitle(document));
fe.setAuthor(document.getCreatedBy().getFullname());
fe.setUpdatedDate(document.getLastModifiedOn());
fe.setDescriptionValue(renderWikiText(document.getAreaNumber(), document.getFeedDescription()));
}
public String getFeedEntryTitle(WikiDocument document) {
return document.getName();
}
}
1.1 date: 2007/12/19 04:29:22; author: cbauer; state: Exp;jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/feeds/FeedEntryManager.java
Index: FeedEntryManager.java
===================================================================
package org.jboss.seam.wiki.core.feeds;
import org.jboss.seam.wiki.core.model.FeedEntry;
import org.jboss.seam.wiki.core.engine.DefaultWikiTextRenderer;
import org.jboss.seam.wiki.core.engine.WikiTextParser;
import org.jboss.seam.wiki.core.engine.WikiLink;
import org.jboss.seam.wiki.core.engine.WikiLinkResolver;
import org.jboss.seam.wiki.util.WikiUtil;
import org.jboss.seam.ui.validator.FormattedTextValidator;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.log.Log;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Scope;
import antlr.ANTLRException;
import antlr.RecognitionException;
@Scope(ScopeType.APPLICATION)
public abstract class FeedEntryManager<M, FE extends FeedEntry> {
@Logger
static Log log;
public abstract FE createFeedEntry(M source);
public abstract void updateFeedEntry(FE feedEntry, M source);
public abstract String getFeedEntryTitle(M source);
protected String renderWikiText(Long currentAreaNumber, String wikiText) {
WikiTextParser parser = new WikiTextParser(wikiText, true, true);
parser.setCurrentAreaNumber(currentAreaNumber);
parser.setResolver((WikiLinkResolver) Component.getInstance("wikiLinkResolver"));
class FeedRenderer extends DefaultWikiTextRenderer {
public String renderInlineLink(WikiLink inlineLink) {
return !inlineLink.isBroken() ?
"<a href=\""
+ WikiUtil.renderURL(inlineLink.getFile())
+ "\">"
+ inlineLink.getDescription()
+ "</a>" : "[Broken Link]";
}
// Preserve the macro that marks the end of the teaser
public String renderMacro(String macroName) {
if (macroName.equals(FeedEntry.END_TEASER_MACRO)) {
return FeedEntry.END_TEASER_MARKER;
} else {
return "";
}
}
}
parser.setRenderer( new FeedRenderer() );
// Run the parser
try {
parser.parse();
} catch (RecognitionException rex) {
// Swallow and log and low debug level
log.debug( "Ignored parse error generating feed entry text: " + FormattedTextValidator.getErrorMessage(wikiText, rex) );
} catch (ANTLRException ex) {
// All other errors are fatal;
throw new RuntimeException(ex);
}
return parser.toString();
}
}
1.1 date: 2007/12/19 04:29:22; author: cbauer; state: Exp;jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/feeds/WikiCommentFeedEntryManager.java
Index: WikiCommentFeedEntryManager.java
===================================================================
/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.seam.wiki.core.feeds;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.international.Messages;
import org.jboss.seam.wiki.core.model.WikiComment;
import org.jboss.seam.wiki.core.model.WikiCommentFeedEntry;
import org.jboss.seam.wiki.util.WikiUtil;
@Name("wikiCommentFeedEntryManager")
public class WikiCommentFeedEntryManager extends FeedEntryManager<WikiComment, WikiCommentFeedEntry> {
public WikiCommentFeedEntry createFeedEntry(WikiComment comment) {
WikiCommentFeedEntry fe = new WikiCommentFeedEntry();
fe.setLink(WikiUtil.renderURL(comment));
fe.setTitle(getFeedEntryTitle(comment));
fe.setAuthor(comment.getCreatedBy().getFullname());
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(getCommentDescription(comment));
fe.setComment(comment);
return fe;
}
public void updateFeedEntry(WikiCommentFeedEntry fe, WikiComment comment) {
fe.setLink(WikiUtil.renderURL(comment));
fe.setTitle(Messages.instance().get("lacewiki.label.comment.FeedEntryTitlePrefix") + " " + comment.getSubject());
fe.setAuthor(comment.getCreatedBy().getFullname());
fe.setUpdatedDate(comment.getLastModifiedOn());
fe.setDescriptionValue(getCommentDescription(comment));
}
public String getFeedEntryTitle(WikiComment comment) {
return Messages.instance().get("lacewiki.label.comment.FeedEntryTitlePrefix") + " " + comment.getSubject();
}
private String getCommentDescription(WikiComment comment) {
StringBuilder desc = new StringBuilder();
desc.append(Messages.instance().get("lacewiki.msg.comment.FeedIntro"));
desc.append(" ");
desc.append("<a href=\"").append(WikiUtil.renderURL(comment.getParentDocument())).append("\">");
desc.append("'").append(comment.getParentDocument().getName()).append("'");
desc.append("</a>.");
desc.append("<hr/>");
desc.append(renderWikiText(comment.getAreaNumber(), comment.getContent()));
return desc.toString();
}
}
More information about the jboss-cvs-commits
mailing list