You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@roller.apache.org by sn...@apache.org on 2006/06/25 21:35:02 UTC
svn commit: r417045 - in
/incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model:
RenderDayPager.java RenderPager.java WeblogEntriesPager.java
Author: snoopdave
Date: Sun Jun 25 12:35:01 2006
New Revision: 417045
URL: http://svn.apache.org/viewvc?rev=417045&view=rev
Log:
Summary of changes in this commit
*** Introduced the RenderDayPager interface
New interface defines pager for collections that are 1) organzied as a map
of lists where each map represents one day and 2) part of a sequence of
collections. Created a weblog entry specific implementation called
WeblogEntriesPager which encapsulates all of the next/prev logic from the
old PageModel for main, single-entry, day and month modes.
*** Simplified PageRenderModel
PageRenderModel no longer provides pager methods, instead it returns a
WeblogEntriesPager. Plus, I added to PageRenderModel simple methods for getting
most recent 100 entries or comments suitable for side-bar displays to recent
weblog activity.
*** Elimiated WeblogPagePluginHelper class
Replaced it with methods in WeblogEntryData getTransformedText() and
getTransformedSummary(). Per-request caching of initialized plugins is done
by the WebsiteData object.
*** Made front-page properties into runtime properties
site.frontpage.weblog.handle
site.frontpage.weblog.aggregated
*** Cleaned up presentation-side of unit tests
Removed unnecessary tests and other classes, refactored away old RollerTestBase
class and made all of the test work again against the 3.0 code base.
Added:
incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/RenderDayPager.java
incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/RenderPager.java
incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/WeblogEntriesPager.java
Added: incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/RenderDayPager.java
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/RenderDayPager.java?rev=417045&view=auto
==============================================================================
--- incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/RenderDayPager.java (added)
+++ incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/RenderDayPager.java Sun Jun 25 12:35:01 2006
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. The ASF licenses this file to You
+ * under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License. For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+package org.apache.roller.ui.rendering.model;
+
+import java.util.Map;
+
+/**
+ * Interface to a paged sub-collection, returned as map of lists of values,
+ * with one map per day.
+ */
+public interface RenderDayPager {
+
+ /**
+ * Current list of values, as map of lists of items, one map per day.
+ */
+ public Map getCurrentValues();
+
+ /**
+ * Get URL of next page of collection.
+ */
+ public String getNextLink();
+
+ /**
+ * Get name of next page of collection.
+ */
+ public String getNextLinkName();
+
+ /**
+ * Get URL of previous page of collection.
+ */
+ public String getPrevLink();
+
+ /**
+ * Get name of previous page of collection.
+ */
+ public String getPrevLinkName();
+
+ /**
+ * Get URL of next collection after this one.
+ */
+ public String getNextCollectionLink();
+
+ /**
+ * Get name of next collection after this one.
+ */
+ public String getNextCollectionName();
+
+ /**
+ * Get URL of previous collection before this one.
+ */
+ public String getPrevCollectionLink();
+
+ /**
+ * Get name of previous collection before this one.
+ */
+ public String getPrevCollectionName();
+}
Added: incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/RenderPager.java
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/RenderPager.java?rev=417045&view=auto
==============================================================================
--- incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/RenderPager.java (added)
+++ incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/RenderPager.java Sun Jun 25 12:35:01 2006
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. The ASF licenses this file to You
+ * under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License. For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+package org.apache.roller.ui.rendering.model;
+
+import java.util.List;
+
+/**
+ * Simple pager returns list of values, currently not used.
+ */
+public interface RenderPager {
+
+ /**
+ * Current list of values, as map of lists of items, one map per day.
+ */
+ public List getCurrentValues();
+
+ /**
+ * Get URL of next page of collection.
+ */
+ public String getNextLink();
+
+ /**
+ * Get name of next page of collection.
+ */
+ public String getNextLinkName();
+
+ /**
+ * Get URL of previous page of collection.
+ */
+ public String getPrevLink();
+
+ /**
+ * Get name of previous page of collection.
+ */
+ public String getPrevLinkName();
+}
Added: incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/WeblogEntriesPager.java
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/WeblogEntriesPager.java?rev=417045&view=auto
==============================================================================
--- incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/WeblogEntriesPager.java (added)
+++ incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/WeblogEntriesPager.java Sun Jun 25 12:35:01 2006
@@ -0,0 +1,593 @@
+package org.apache.roller.ui.rendering.model;
+
+import java.text.ParsePosition;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Collections;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.TimeZone;
+import java.util.TreeMap;
+import javax.servlet.http.HttpServletRequest;
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.roller.RollerException;
+import org.apache.roller.model.Roller;
+import org.apache.roller.model.RollerFactory;
+import org.apache.roller.model.WeblogManager;
+import org.apache.roller.pojos.WeblogEntryData;
+import org.apache.roller.pojos.WebsiteData;
+import org.apache.roller.pojos.wrapper.WeblogEntryDataWrapper;
+import org.apache.roller.util.DateUtil;
+
+
+/**
+ * Pager for weblog entries, handles latest, single-entry, month and day views.
+ * Collection returned is a list of lists of entries, where each list of
+ * entries represents one day.
+ */
+public class WeblogEntriesPager implements RenderDayPager {
+
+ protected RenderDayPager mode = null; // The classic state pattern
+ protected Map entries = null;
+ protected WebsiteData weblog = null;
+ protected int offset = 0;
+ protected int length = 0;
+ protected String chosenCatPath = null;
+ protected String dateString = null;
+ protected String entryAnchor = null;
+ protected boolean more = false;
+
+ protected static Log log =
+ LogFactory.getFactory().getInstance(WeblogEntriesPager.class);
+
+ public WeblogEntriesPager(
+ HttpServletRequest request,
+ WebsiteData weblog,
+ String entryAnchor,
+ String requestedCat,
+ String callerCat,
+ String dateString) {
+
+ this.weblog = weblog;
+ this.dateString = dateString;
+ this.entryAnchor = entryAnchor;
+
+ length = weblog.getEntryDisplayCount();
+
+ if (callerCat != null && "nil".equals(callerCat)) callerCat = null;
+
+ if (request.getParameter("offset") != null) {
+ try {
+ offset = Integer.parseInt(request.getParameter("offset"));
+ } catch (Exception ignored) {}
+ }
+
+ String chosenCatPath = callerCat != null ? callerCat : requestedCat;
+ if (chosenCatPath == null) {
+ // no category specifed so use default
+ chosenCatPath = weblog.getDefaultCategory().getPath();
+ chosenCatPath = chosenCatPath.equals("/") ? null : chosenCatPath;
+ }
+
+ if (entryAnchor != null) {
+ mode = new SingleEntryMode();
+ } else if (dateString != null && dateString.length() == 8) {
+ mode = new DayMode();
+ } else if (dateString != null && dateString.length() == 6) {
+ mode = new MonthMode();
+ } else {
+ mode = new LatestMode();
+ }
+ }
+
+ public Map getCurrentValues() {
+ return mode.getCurrentValues();
+ }
+
+ public String getNextLink() {
+ return mode.getNextLink();
+ }
+
+ public String getNextLinkName() {
+ return mode.getNextLink();
+ }
+
+ public String getPrevLink() {
+ return mode.getPrevLink();
+ }
+
+ public String getPrevLinkName() {
+ return mode.getPrevLinkName();
+ }
+
+ public String getNextCollectionLink() {
+ return mode.getNextCollectionLink();
+ }
+
+ public String getNextCollectionName() {
+ return mode.getNextCollectionName();
+ }
+
+ public String getPrevCollectionLink() {
+ return mode.getPrevCollectionLink();
+ }
+
+ public String getPrevCollectionName() {
+ return mode.getPrevCollectionName();
+ }
+
+ //-------------------------------------------------------------------------
+
+ class LatestMode implements RenderDayPager {
+
+ public Map getCurrentValues() {
+ return getCurrentValuesImpl();
+ }
+
+ public String getNextLink() {
+ String ret = null;
+ if (more) {
+ int nextOffset = offset + length;
+ ret = weblog.getURL() + "&offset=" + nextOffset;
+ }
+ return ret;
+ }
+
+ public String getNextLinkName() {
+ String ret = null;
+ if (getNextLink() != null) {
+ ret = "Next"; // TODO: I18N
+ }
+ return ret;
+ }
+
+ public String getPrevLink() {
+ String ret = null;
+ if (offset > 0) {
+ int prevOffset = offset + length;
+ prevOffset = (prevOffset < 0) ? 0 : prevOffset;
+ ret = weblog.getURL() + "&offset=" + prevOffset;
+ }
+ return ret;
+ }
+
+ public String getPrevLinkName() {
+ String ret = null;
+ if (getNextLink() != null) {
+ ret = "Prev"; // TODO: I18N
+ }
+ return ret;
+ }
+
+ public String getNextCollectionLink() {
+ return null;
+ }
+
+ public String getNextCollectionName() {
+ return null;
+ }
+
+ public String getPrevCollectionLink() {
+ return null;
+ }
+
+ public String getPrevCollectionName() {
+ return null;
+ }
+
+ }
+
+ //-------------------------------------------------------------------------
+
+ class SingleEntryMode implements RenderDayPager {
+ String nextLink = null;
+ WeblogEntryData entry = null;
+ WeblogEntryData nextEntry = null;
+ WeblogEntryData prevEntry = null;
+
+ public SingleEntryMode() {
+ SingleEntryMode.this.getCurrentValues();
+ }
+
+ public Map getCurrentValues() {
+ if (entries == null) try {
+ Roller roller = RollerFactory.getRoller();
+ WeblogManager wmgr = roller.getWeblogManager();
+ entry = wmgr.getWeblogEntryByAnchor(weblog, entryAnchor);
+ if (entry == null || !entry.getStatus().equals(WeblogEntryData.PUBLISHED)) {
+ entry = null;
+ } else {
+ entries = new TreeMap();
+ entries.put(
+ new Date(entry.getPubTime().getTime()),
+ Collections.singletonList(entry));
+ }
+ } catch (Exception e) {
+ log.error("ERROR: fetching entry");
+ }
+ return entries;
+ }
+
+ public String getNextLink() {
+ String ret = null;
+ if (getNextEntry() != null) {
+ ret = getNextEntry().getPermalink();
+ }
+ return ret;
+ }
+
+ public String getNextLinkName() {
+ String ret = null;
+ if (getNextEntry() != null) {
+ ret = getNextEntry().getTitle();
+ }
+ return ret;
+ }
+
+ public String getPrevLink() {
+ String ret = null;
+ if (getPrevEntry() != null) {
+ ret = getPrevEntry().getPermalink();
+ }
+ return ret;
+ }
+
+ public String getPrevLinkName() {
+ String ret = null;
+ if (getPrevEntry() != null) {
+ ret = getPrevEntry().getTitle();
+ }
+ return ret;
+ }
+
+ public String getNextCollectionLink() {
+ return null;
+ }
+
+ public String getNextCollectionName() {
+ return null;
+ }
+
+ public String getPrevCollectionLink() {
+ return null;
+ }
+
+ public String getPrevCollectionName() {
+ return null;
+ }
+
+ private WeblogEntryData getNextEntry() {
+ if (nextEntry == null) try {
+ Roller roller = RollerFactory.getRoller();
+ WeblogManager wmgr = roller.getWeblogManager();
+ nextEntry = wmgr.getNextEntry(entry, chosenCatPath);
+ // make sure that entry is published and not to future
+ if (nextEntry != null && nextEntry.getPubTime().after(new Date())
+ && nextEntry.getStatus().equals(WeblogEntryData.PUBLISHED)) {
+ nextEntry = null;
+ }
+ } catch (RollerException e) {
+ log.error("ERROR: getting next entry", e);
+ }
+ return nextEntry;
+ }
+
+ private WeblogEntryData getPrevEntry() {
+ if (prevEntry == null) try {
+ Roller roller = RollerFactory.getRoller();
+ WeblogManager wmgr = roller.getWeblogManager();
+ prevEntry = wmgr.getPreviousEntry(entry, chosenCatPath);
+ // make sure that entry is published and not to future
+ if (prevEntry != null && prevEntry.getPubTime().after(new Date())
+ && prevEntry.getStatus().equals(WeblogEntryData.PUBLISHED)) {
+ prevEntry = null;
+ }
+ } catch (RollerException e) {
+ log.error("ERROR: getting prev entry", e);
+ }
+ return prevEntry;
+ }
+ }
+
+ //-------------------------------------------------------------------------
+
+ class DayMode implements RenderDayPager {
+ private Date day;
+ private Date nextDay;
+ private Date prevDay;
+
+ public DayMode() {
+ getCurrentValuesImpl();
+ day = parseDate(dateString);
+
+ Calendar cal = Calendar.getInstance();
+
+ cal.setTime(day);
+ cal.add(Calendar.DAY_OF_MONTH, 1);
+ cal.set(Calendar.HOUR, 0);
+ cal.set(Calendar.MINUTE, 0);
+ cal.set(Calendar.SECOND, 0);
+ nextDay = cal.getTime();
+ if (nextDay.after(getToday())) {
+ nextDay = null;
+ }
+
+ cal.setTime(day);
+ cal.add(Calendar.DAY_OF_MONTH, -1);
+ cal.set(Calendar.HOUR, 23);
+ cal.set(Calendar.MINUTE, 59);
+ cal.set(Calendar.SECOND, 59);
+ prevDay = cal.getTime();
+ }
+
+ public Map getCurrentValues() {
+ return getCurrentValuesImpl();
+ }
+
+ public String getNextLink() {
+ String ret = null;
+ if (more) {
+ int nextOffset = offset + length;
+ ret = weblog.getURL() + "/date/" + dateString + "&offset=" + nextOffset;
+ }
+ return ret;
+ }
+
+ public String getNextLinkName() {
+ String ret = null;
+ if (getNextLink() != null) {
+ ret = "Next"; // TODO: I18N
+ }
+ return ret;
+ }
+
+ public String getPrevLink() {
+ String ret = null;
+ if (offset > 0) {
+ int prevOffset = offset + length;
+ prevOffset = (prevOffset < 0) ? 0 : prevOffset;
+ ret = weblog.getURL() + "/date/" + dateString + "&offset=" + prevOffset;
+ }
+ return ret;
+ }
+
+ public String getPrevLinkName() {
+ String ret = null;
+ if (getNextLink() != null) {
+ ret = "Prev"; // TODO: I18N
+ }
+ return ret;
+ }
+
+ public String getNextCollectionLink() {
+ String ret = null;
+ if (nextDay != null) {
+ String next = DateUtil.format8chars(nextDay);
+ ret = weblog.getURL() + "/date/" + next;
+ }
+ return ret;
+ }
+
+ public String getNextCollectionName() {
+ String ret = null;
+ if (nextDay != null) {
+ ret = DateUtil.format8chars(nextDay);
+ }
+ return ret;
+ }
+
+ public String getPrevCollectionLink() {
+ String ret = null;
+ if (prevDay != null) {
+ String prev = DateUtil.format8chars(prevDay);
+ ret = weblog.getURL() + "/date/" + prev;
+ }
+ return ret;
+ }
+
+ public String getPrevCollectionName() {
+ String ret = null;
+ if (prevDay != null) {
+ ret = DateUtil.format8chars(prevDay);
+ }
+ return ret;
+ }
+ }
+
+ //-------------------------------------------------------------------------
+
+ class MonthMode implements RenderDayPager {
+ private Date month;
+ private Date nextMonth;
+ private Date prevMonth;
+
+ public MonthMode() {
+ getCurrentValuesImpl();
+ month = parseDate(dateString);
+
+ Calendar cal = Calendar.getInstance();
+
+ cal.setTime(month);
+ cal.add(Calendar.MONTH, 1);
+ nextMonth = cal.getTime();
+ if (nextMonth.after(getToday())) {
+ nextMonth = null;
+ }
+
+ cal.setTime(month);
+ cal.add(Calendar.MONTH, -1);
+ prevMonth = cal.getTime();
+ }
+
+ public Map getCurrentValues() {
+ return getCurrentValuesImpl();
+ }
+
+ public String getNextLink() {
+ String ret = null;
+ if (more) {
+ int nextOffset = offset + length;
+ ret = weblog.getURL() + "/date/" + dateString + "&offset=" + nextOffset;
+ }
+ return ret;
+ }
+
+ public String getNextLinkName() {
+ String ret = null;
+ if (getNextLink() != null) {
+ ret = "Next"; // TODO: I18N
+ }
+ return ret;
+ }
+
+ public String getPrevLink() {
+ String ret = null;
+ if (offset > 0) {
+ int prevOffset = offset + length;
+ prevOffset = (prevOffset < 0) ? 0 : prevOffset;
+ ret = weblog.getURL() + "/date/" + dateString + "&offset=" + prevOffset;
+ }
+ return ret;
+ }
+
+ public String getPrevLinkName() {
+ String ret = null;
+ if (getNextLink() != null) {
+ ret = "Prev"; // TODO: I18N
+ }
+ return ret;
+ }
+
+ public String getNextCollectionLink() {
+ String ret = null;
+ if (nextMonth != null) {
+ String next = DateUtil.format6chars(nextMonth);
+ ret = weblog.getURL() + "/date/" + next;
+ }
+ return ret;
+ }
+
+ public String getNextCollectionName() {
+ String ret = null;
+ if (nextMonth != null) {
+ ret = DateUtil.format6chars(nextMonth);
+ }
+ return ret;
+ }
+
+ public String getPrevCollectionLink() {
+ String ret = null;
+ if (prevMonth != null) {
+ String prev = DateUtil.format6chars(prevMonth);
+ ret = weblog.getURL() + "/date/" + prev;
+ }
+ return ret;
+ }
+
+ public String getPrevCollectionName() {
+ String ret = null;
+ if (prevMonth != null) {
+ ret = DateUtil.format6chars(prevMonth);
+ }
+ return ret;
+ }
+
+ }
+
+ //------------------------------------------------------------------------
+
+ private Map getCurrentValuesImpl() {
+ if (entries == null) {
+ entries = new TreeMap();
+ try {
+ Roller roller = RollerFactory.getRoller();
+ WeblogManager wmgr = roller.getWeblogManager();
+ Date date = parseDate(dateString);
+
+ Calendar cal = Calendar.getInstance(weblog.getTimeZoneInstance());
+ Date startDate = null;
+ Date endDate = date;
+ if (endDate == null) endDate = new Date();
+ if (mode instanceof DayMode) {
+ // URL specified a specific day so get all entries for it
+ startDate = DateUtil.getStartOfDay(endDate, cal);
+ endDate = DateUtil.getEndOfDay(endDate, cal);
+ } else if (mode instanceof MonthMode) {
+ // URL specified a specific month so get all entries for it
+ startDate = DateUtil.getStartOfMonth(endDate, cal);
+ endDate = DateUtil.getEndOfMonth(endDate, cal);
+ }
+ Map mmap = RollerFactory.getRoller().getWeblogManager().getWeblogEntryObjectMap(
+ weblog,
+ startDate,
+ endDate,
+ chosenCatPath,
+ WeblogEntryData.PUBLISHED,
+ offset,
+ length);
+
+ // need to wrap pojos
+ java.util.Date key = null;
+ Iterator days = mmap.keySet().iterator();
+ while(days.hasNext()) {
+ key = (java.util.Date)days.next();
+
+ // now we need to go through each entry in a day and wrap
+ List wrapped = new ArrayList();
+ List unwrapped= (List) mmap.get(key);
+ for(int i=0; i < unwrapped.size(); i++) {
+ wrapped.add(i,
+ WeblogEntryDataWrapper.wrap((WeblogEntryData)unwrapped.get(i)));
+ }
+ entries.put(key, wrapped);
+ }
+ } catch (Exception e) {
+ log.error("ERROR: getting entry month map", e);
+ }
+ }
+ return entries;
+ }
+
+ private Date parseDate(String dateString) {
+ Date ret = null;
+ SimpleDateFormat char8DateFormat = DateUtil.get8charDateFormat();
+ SimpleDateFormat char6DateFormat = DateUtil.get6charDateFormat();
+ if ( dateString!=null
+ && dateString.length()==8
+ && StringUtils.isNumeric(dateString) ) {
+ ParsePosition pos = new ParsePosition(0);
+ ret = char8DateFormat.parse( dateString, pos );
+
+ // make sure the requested date is not in the future
+ Date today = getToday();
+ if (ret.after(today)) ret = today;
+ }
+ if ( dateString!=null
+ && dateString.length()==6
+ && StringUtils.isNumeric(dateString) ) {
+ ParsePosition pos = new ParsePosition(0);
+ ret = char6DateFormat.parse( dateString, pos );
+
+ // make sure the requested date is not in the future
+ Date today = getToday();
+ if (ret.after(today)) ret = today;
+ }
+ return ret;
+ }
+
+ private Date getToday() {
+ Calendar todayCal = Calendar.getInstance();
+ todayCal = Calendar.getInstance(
+ weblog.getTimeZoneInstance(),weblog.getLocaleInstance());
+ todayCal.setTime(new Date());
+ return todayCal.getTime();
+ }
+
+
+}
+