[jboss-cvs] jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/plugin/blogdirectory ...
Christian Bauer
christian at hibernate.org
Sat Aug 25 13:59:21 EDT 2007
User: cbauer
Date: 07/08/25 13:59:21
Modified: examples/wiki/src/main/org/jboss/seam/wiki/plugin/blogdirectory
BlogEntry.java BlogDirectory.java
Added: examples/wiki/src/main/org/jboss/seam/wiki/plugin/blogdirectory
BlogDAO.java BlogEntryCount.java
Log:
Major refactoring of navigation
Revision Changes Path
1.4 +6 -0 jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/plugin/blogdirectory/BlogEntry.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: BlogEntry.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/plugin/blogdirectory/BlogEntry.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- BlogEntry.java 26 Apr 2007 15:11:51 -0000 1.3
+++ BlogEntry.java 25 Aug 2007 17:59:21 -0000 1.4
@@ -9,6 +9,8 @@
Document entryDocument;
Long commentCount;
+ public BlogEntry() {}
+
public BlogEntry(Document entryDocument) {
this.entryDocument = entryDocument;
}
@@ -33,4 +35,8 @@
public void setCommentCount(Long commentCount) {
this.commentCount = commentCount;
}
+
+ public String toString() {
+ return "BlogEntry: " + entryDocument + " Comments: " + commentCount;
+ }
}
1.13 +103 -71 jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/plugin/blogdirectory/BlogDirectory.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: BlogDirectory.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/plugin/blogdirectory/BlogDirectory.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -b -r1.12 -r1.13
--- BlogDirectory.java 17 Aug 2007 13:00:32 -0000 1.12
+++ BlogDirectory.java 25 Aug 2007 17:59:21 -0000 1.13
@@ -27,6 +27,9 @@
NodeDAO nodeDAO;
@In
+ BlogDAO blogDAO;
+
+ @In
FacesMessages facesMessages;
@In
@@ -38,23 +41,36 @@
@RequestParameter
Boolean allEntries;
+ private Integer year;
+ private Integer month;
+ private Integer day;
+ private Integer page = 0;
+
@RequestParameter
- private void setBlogPage(Integer blogPage) {
- if (blogPage != null) this.page = blogPage;
+ public void setPage(Integer page) {
+ if (page != null) {
+ this.page = page;
}
-
+ }
+ @RequestParameter
+ public void setYear(Integer year) {
+ this.year = year;
+ }
+ @RequestParameter
+ public void setMonth(Integer month) {
+ this.month = month;
+ }
+ @RequestParameter
+ public void setDay(Integer day) {
+ this.day = day;
+ }
+ private long numOfBlogEntries;
private List<BlogEntry> blogEntries;
-
- // Need to expose this as a datamodel so Seam can convert our map to a bunch of Map.Entry objects
+ private List<BlogEntryCount> blogEntryCountsByYearAndMonth;
+ // Need to expose this as a datamodel so Seam can convert our map to a collection of Map.Entry objects
@DataModel
private Map<Date, List<BlogEntry>> recentBlogEntries;
- @DataModel
- private Map<Date, List<BlogEntry>> allBlogEntries;
- private String orderByProperty;
- private boolean orderDescending;
- private int totalRowCount;
- private int page;
@In("#{blogDirectoryPreferences.properties['pageSize']}")
private long pageSize;
@In("#{blogDirectoryPreferences.properties['recentHeadlines']}")
@@ -62,39 +78,53 @@
@Create
public void initialize() {
- orderByProperty = "createdOn";
- orderDescending = true;
refreshBlogEntries();
}
- private void queryRowCount() {
- totalRowCount = nodeDAO.getRowCountWithParent(Document.class, currentDirectory, currentDocument);
+ private void queryNumOfBlogEntries() {
+ numOfBlogEntries = blogDAO.countBlogEntries(currentDirectory, currentDocument, 99l, year, month, day);
}
private void queryBlogEntries() {
- // TODO: This could be done in one query but I'm too lazy to write the GROUP BY clause because Hibernate doesn't do it for me
- List<Document> documents =
- nodeDAO.findWithParent(Document.class, currentDirectory, currentDocument,
- orderByProperty, orderDescending, page * pageSize, pageSize);
- Map<Long,Long> commentCounts = nodeDAO.findCommentCount(currentDirectory);
-
- for (Document document : documents) {
- blogEntries.add(
- new BlogEntry(document, commentCounts.get(document.getId()) )
+ blogEntries =
+ blogDAO.findBlogEntriesWithCommentCount(
+ currentDirectory,
+ currentDocument,
+ 99l,
+ "createdOn",
+ true,
+ page * pageSize,
+ pageSize,
+ year, month, day
);
}
+
+ private void queryBlogEntryCountsByYearAndMonth() {
+ blogEntryCountsByYearAndMonth = blogDAO.countAllBlogEntriesGroupByYearMonth(currentDirectory, currentDocument, 99l);
}
- private void queryRecentBlogEntries() {
- List<Document> documents =
- nodeDAO.findWithParent(Document.class, currentDirectory, currentDocument, "createdOn", true, 0, recentBlogEntriesCount);
+ @Factory(value = "recentBlogEntries")
+ @Observer("PreferenceComponent.refresh.blogDirectoryPreferences")
+ public void queryRecentBlogEntries() {
+ List<BlogEntry> recentBlogEntriesNonAggregated =
+ blogDAO.findBlogEntriesWithCommentCount(
+ currentDirectory,
+ currentDocument,
+ 99l,
+ "createdOn",
+ true,
+ 0,
+ recentBlogEntriesCount,
+ null, null, null
+ );
+ // Now aggregate by day
recentBlogEntries = new LinkedHashMap<Date, List<BlogEntry>>();
- for (Document document : documents) {
+ for (BlogEntry blogEntry : recentBlogEntriesNonAggregated) {
// Find the day (ignore the hours, minutes, etc.)
Calendar createdOn = new GregorianCalendar();
- createdOn.setTime(document.getCreatedOn());
+ createdOn.setTime(blogEntry.getEntryDocument().getCreatedOn());
GregorianCalendar createdOnDay = new GregorianCalendar(
createdOn.get(Calendar.YEAR), createdOn.get(Calendar.MONTH), createdOn.get(Calendar.DAY_OF_MONTH)
);
@@ -106,55 +136,33 @@
? recentBlogEntries.get(createdOnDate)
: new ArrayList<BlogEntry>();
- entriesForDay.add(new BlogEntry(document));
+ entriesForDay.add(blogEntry);
recentBlogEntries.put(createdOnDate, entriesForDay);
}
}
- private void queryAllBlogEntries() {
- if (allEntries == null || !allEntries) return; // Don't query if the index isn't displayed
- List<Document> documents =
- nodeDAO.findWithParent(Document.class, currentDirectory, currentDocument, "createdOn", true, 0, 0);
-
- allBlogEntries = new LinkedHashMap<Date, List<BlogEntry>>();
- for (Document document : documents) {
-
- // Find the month (ignore the days, hours, minutes, etc.)
- Calendar createdOn = new GregorianCalendar();
- createdOn.setTime(document.getCreatedOn());
- GregorianCalendar createdOnMonth = new GregorianCalendar(
- createdOn.get(Calendar.YEAR), createdOn.get(Calendar.MONTH), 1
- );
- Date createdOnDate = createdOnMonth.getTime(); // Jesus, this API is just bad...
-
- // Aggregate by month
- List<BlogEntry> entriesForMonth =
- allBlogEntries.containsKey(createdOnDate)
- ? allBlogEntries.get(createdOnDate)
- : new ArrayList<BlogEntry>();
-
- entriesForMonth.add(new BlogEntry(document));
- allBlogEntries.put(createdOnDate, entriesForMonth);
- }
- }
-
- @Observer("Preferences.blogDirectoryPreferences")
+ @Observer("PreferenceComponent.refresh.blogDirectoryPreferences")
public void refreshBlogEntries() {
blogEntries = new ArrayList<BlogEntry>();
- queryRowCount();
- if (totalRowCount != 0) {
+ queryNumOfBlogEntries();
+ if (numOfBlogEntries != 0){
queryBlogEntries();
- queryRecentBlogEntries();
- queryAllBlogEntries();
}
}
+ public long getNumOfBlogEntries() {
+ return numOfBlogEntries;
+ }
+
public List<BlogEntry> getBlogEntries() {
return blogEntries;
}
- public int getTotalRowCount() {
- return totalRowCount;
+ public List<BlogEntryCount> getBlogEntryCountsByYearAndMonth() {
+ if (blogEntryCountsByYearAndMonth == null) {
+ queryBlogEntryCountsByYearAndMonth();
+ }
+ return blogEntryCountsByYearAndMonth;
}
public int getNextPage() {
@@ -174,22 +182,46 @@
}
public long getLastRow() {
- return (page * pageSize + pageSize) > totalRowCount
- ? totalRowCount
+ return (page * pageSize + pageSize) > numOfBlogEntries
+ ? numOfBlogEntries
: page * pageSize + pageSize;
}
public long getLastPage() {
- long lastPage = (totalRowCount / pageSize);
- if (totalRowCount % pageSize == 0) lastPage--;
+ long lastPage = (numOfBlogEntries / pageSize);
+ if (numOfBlogEntries % pageSize == 0) lastPage--;
return lastPage;
}
public boolean isNextPageAvailable() {
- return blogEntries != null && totalRowCount > ((page * pageSize) + pageSize);
+ return blogEntries != null && numOfBlogEntries > ((page * pageSize) + pageSize);
}
public boolean isPreviousPageAvailable() {
return blogEntries != null && page > 0;
}
+
+ public String getDateUrl() {
+ return dateAsString(year, month, day);
+ }
+
+ // Utilities
+
+ public static String dateAsString(Integer year, Integer month, Integer day) {
+ StringBuilder dateUrl = new StringBuilder();
+ if (year != null) dateUrl.append("/").append(year);
+ if (month != null) dateUrl.append("/").append(padInteger(month, 2));
+ if (day != null) dateUrl.append("/").append(padInteger(day, 2));
+ return dateUrl.toString();
+ }
+
+ private static String padInteger(Integer raw, int padding) {
+ String rawInteger = raw.toString();
+ StringBuilder paddedInteger = new StringBuilder( );
+ for ( int padIndex = rawInteger.length() ; padIndex < padding; padIndex++ ) {
+ paddedInteger.append('0');
+ }
+ return paddedInteger.append( rawInteger ).toString();
+ }
+
}
1.1 date: 2007/08/25 17:59:21; author: cbauer; state: Exp;jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/plugin/blogdirectory/BlogDAO.java
Index: BlogDAO.java
===================================================================
package org.jboss.seam.wiki.plugin.blogdirectory;
import org.jboss.seam.wiki.core.model.Node;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.AutoCreate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import javax.persistence.EntityManager;
import java.util.List;
@Name("blogDAO")
@AutoCreate
@Transactional
public class BlogDAO {
@In
protected EntityManager restrictedEntityManager;
public List<BlogEntry> findBlogEntriesWithCommentCount(Node startNode,
Node ignoreNode,
Long maxDepth,
String orderByProperty,
boolean orderDescending,
long firstResult,
long maxResults,
Integer year,
Integer month,
Integer day) {
StringBuilder queryString = new StringBuilder();
queryString.append("select n1 as entryDocument").append(", ");
queryString.append("(select count(c) from Comment c where c.document.id = n1.id) as commentCount").append(" ");
queryString.append("from ").append(startNode.getTreeSuperclassEntityName()).append(" n1, ");
queryString.append(startNode.getTreeSuperclassEntityName()).append(" n2 ");
queryString.append("fetch all properties").append(" ");
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(" ");
queryString.append("and n2.class = :clazz").append(" ");
queryString.append("and not n1 = :ignoreNode").append(" ");
if (year != null) queryString.append("and year(n1.createdOn) = :limitYear").append(" ");
if (month != null) queryString.append("and month(n1.createdOn) = :limitMonth").append(" ");
if (day != null) queryString.append("and day(n1.createdOn) = :limitDay").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(" ");
queryString.append("order by n1.").append(orderByProperty).append(" ");
queryString.append( orderDescending ? "desc" : "asc").append("");
Query nestedSetQuery = getSession().createQuery(queryString.toString());
nestedSetQuery.setParameter("thread", startNode.getNsThread());
nestedSetQuery.setParameter("startLeft", startNode.getNsLeft());
nestedSetQuery.setParameter("startRight", startNode.getNsRight());
nestedSetQuery.setParameter("clazz", "DOCUMENT"); // TODO: Hibernate can't bind the discriminator? Not even with Hibernate.CLASS type...
nestedSetQuery.setParameter("ignoreNode", ignoreNode);
if (year != null) nestedSetQuery.setParameter("limitYear", year);
if (month != null) nestedSetQuery.setParameter("limitMonth", month);
if (day != null) nestedSetQuery.setParameter("limitDay", day);
nestedSetQuery.setFirstResult( new Long(firstResult).intValue() );
nestedSetQuery.setMaxResults( new Long(maxResults).intValue() );
nestedSetQuery.setResultTransformer(Transformers.aliasToBean(BlogEntry.class));
return (List<BlogEntry>)nestedSetQuery.list();
}
public Long countBlogEntries(Node startNode, Node ignoreNode, Long maxDepth, Integer year, Integer month, Integer day ) {
return countBlogEntries(startNode, ignoreNode, maxDepth, false, false, false, year, month, day).get(0).getNumOfEntries();
}
public List<BlogEntryCount> countAllBlogEntriesGroupByYearMonth(Node startNode, Node ignoreNode, Long maxDepth) {
return countBlogEntries(startNode, ignoreNode, maxDepth, true, true, false, null, null, null);
}
private List<BlogEntryCount> countBlogEntries(Node startNode, Node ignoreNode, Long maxDepth,
boolean projectYear, boolean projectMonth, boolean projectDay,
Integer limitYear, Integer limitMonth, Integer limitDay) {
StringBuilder queryString = new StringBuilder();
queryString.append("select count(n1.id) as numOfEntries");
if (projectYear) queryString.append(", ").append("year(n1.createdOn) as year");
if (projectMonth) queryString.append(", ").append("month(n1.createdOn) as month");
if (projectDay) queryString.append(", ").append("day(n1.createdOn) as day");
queryString.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(" ");
queryString.append("and n2.class = :clazz").append(" ");
queryString.append("and not n1 = :ignoreNode").append(" ");
if (limitYear != null) queryString.append("and year(n1.createdOn) = :limitYear").append(" ");
if (limitMonth!= null) queryString.append("and month(n1.createdOn) = :limitMonth").append(" ");
if (limitDay != null) queryString.append("and day(n1.createdOn) = :limitDay").append(" ");
if (projectYear || projectMonth || projectDay) queryString.append("group by").append(" ");
if (projectYear) queryString.append("year(n1.createdOn)");
if (projectMonth) queryString.append(", month(n1.createdOn)");
if (projectDay) queryString.append(", day(n1.createdOn)");
queryString.append(" ");
if (projectYear || projectMonth || projectDay) queryString.append("order by").append(" ");
if (projectYear) queryString.append("year(n1.createdOn) desc");
if (projectMonth) queryString.append(", month(n1.createdOn) desc");
if (projectDay) queryString.append(", day(n1.createdOn) desc");
queryString.append(" ");
Query nestedSetQuery = getSession().createQuery(queryString.toString());
nestedSetQuery.setParameter("thread", startNode.getNsThread());
nestedSetQuery.setParameter("startLeft", startNode.getNsLeft());
nestedSetQuery.setParameter("startRight", startNode.getNsRight());
nestedSetQuery.setParameter("clazz", "DOCUMENT"); // TODO: Hibernate can't bind the discriminator? Not even with Hibernate.CLASS type...
nestedSetQuery.setParameter("ignoreNode", ignoreNode);
if (limitYear != null) nestedSetQuery.setParameter("limitYear", limitYear);
if (limitMonth!= null) nestedSetQuery.setParameter("limitMonth", limitMonth);
if (limitDay != null) nestedSetQuery.setParameter("limitDay", limitDay);
nestedSetQuery.setResultTransformer(Transformers.aliasToBean(BlogEntryCount.class));
return nestedSetQuery.list();
}
private Session getSession() {
restrictedEntityManager.joinTransaction();
return ((Session)((org.jboss.seam.persistence.EntityManagerProxy) restrictedEntityManager).getDelegate());
}
}
1.1 date: 2007/08/25 17:59:21; author: cbauer; state: Exp;jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/plugin/blogdirectory/BlogEntryCount.java
Index: BlogEntryCount.java
===================================================================
package org.jboss.seam.wiki.plugin.blogdirectory;
public class BlogEntryCount {
Long numOfEntries;
Integer year;
Integer month;
Integer day;
public BlogEntryCount() {}
public BlogEntryCount(Long numOfEntries, Integer year, Integer month, Integer day) {
this.numOfEntries = numOfEntries;
this.year = year;
this.month = month;
this.day = day;
}
public Long getNumOfEntries() {
return numOfEntries;
}
public Integer getYear() {
return year;
}
public Integer getMonth() {
return month;
}
public Integer getDay() {
return day;
}
public String getAsString() {
return BlogDirectory.dateAsString(year, month, day);
}
public String toString() {
return "NumOfEntries: " + getNumOfEntries() + " Year: " + getYear() + " Month: " + getMonth() + " Day: " + getDay();
}
}
More information about the jboss-cvs-commits
mailing list