1 18 19 package org.apache.roller.ui.core.tags.calendar; 20 21 import java.text.ParsePosition ; 22 import java.text.SimpleDateFormat ; 23 import java.util.Calendar ; 24 import java.util.Date ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.roller.RollerException; 30 import org.apache.roller.model.RollerFactory; 31 import org.apache.roller.model.WeblogManager; 32 import org.apache.roller.pojos.WeblogEntryData; 33 import org.apache.roller.pojos.WebsiteData; 34 import org.apache.roller.ui.rendering.util.WeblogPageRequest; 35 import org.apache.roller.util.DateUtil; 36 import org.apache.roller.util.URLUtilities; 37 38 39 42 public class WeblogCalendarModel implements CalendarModel { 43 44 private static Log log = LogFactory.getLog(WeblogCalendarModel.class); 45 46 protected Map monthMap; 47 protected Date day; 48 protected String cat = null; 49 protected String pageLink = null; 50 protected String locale = null; 51 protected Calendar calendar = null; 52 protected WebsiteData weblog = null; 53 54 protected WeblogPageRequest pageRequest = null; 55 56 57 public WeblogCalendarModel(WeblogPageRequest pRequest, String catArgument) { 58 59 this.pageRequest = pRequest; 60 try { 61 this.weblog = pageRequest.getWeblog(); 62 63 pageLink = pageRequest.getWeblogPageName(); 64 65 day = DateUtil.parseWeblogURLDateString(pageRequest.getWeblogDate(), 66 weblog.getTimeZoneInstance(), weblog.getLocaleInstance()); 67 initDay(day); 68 69 locale = pageRequest.getLocale(); 70 71 if(weblog == null) { 72 throw new RollerException("unable to lookup weblog: "+ 73 pageRequest.getWeblogHandle()); 74 } 75 if (catArgument != null) { 77 cat = catArgument; 78 } else if (pageRequest.getWeblogCategoryName() != null) { 79 cat = pageRequest.getWeblogCategoryName(); 80 } 81 } catch (Exception e) { 82 log.debug("ERROR: initializing calendar", e); 84 } 85 86 } 87 88 89 protected void initDay(Date month) { 90 this.day = day; 91 calendar = Calendar.getInstance( 92 weblog.getTimeZoneInstance(), 93 weblog.getLocaleInstance()); 94 95 Calendar cal = (Calendar )calendar.clone(); 96 97 cal.setTime(month); 99 cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DAY_OF_MONTH)); 100 cal.set(Calendar.HOUR_OF_DAY, cal.getMinimum(Calendar.HOUR_OF_DAY)); 101 cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE)); 102 cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND)); 103 cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND)); 104 Date startDate = cal.getTime(); 105 106 cal.set(Calendar.DAY_OF_MONTH, cal.getMaximum(Calendar.DAY_OF_MONTH)); 108 cal.set(Calendar.HOUR_OF_DAY, cal.getMaximum(Calendar.HOUR_OF_DAY)); 109 cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE)); 110 cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND)); 111 cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND)); 112 Date endDate = cal.getTime(); 113 114 loadWeblogEntries(startDate, endDate, cat); 115 } 116 117 protected void loadWeblogEntries(Date startDate, Date endDate, String catName) { 118 try { 119 WeblogManager mgr = RollerFactory.getRoller().getWeblogManager(); 120 monthMap = mgr.getWeblogEntryStringMap( 121 weblog, startDate, endDate, catName, WeblogEntryData.PUBLISHED, locale, 127 0, -1); 128 } catch (RollerException e) { 129 log.error(e); 130 monthMap = new HashMap (); 131 } 132 } 133 134 public void setDay(String month) throws Exception { 135 SimpleDateFormat fmt = DateUtil.get8charDateFormat(); 136 ParsePosition pos = new ParsePosition (0); 137 initDay( fmt.parse( month, pos ) ); 138 } 139 140 public Date getDay() { 141 return (Date )day.clone(); 142 } 143 144 public String getParameterValue(Date day) { 145 return (String )monthMap.get( day ); 146 } 147 148 154 public String computeUrl(Date day, boolean monthURL, boolean alwaysURL) { 155 String url = null; 156 String dateString = (String )monthMap.get(day); 158 if (dateString == null && !alwaysURL) return null; 159 else if (dateString == null && !monthURL) { 160 dateString = DateUtil.format8chars(day); 161 } else if (dateString == null && monthURL) { 162 dateString = DateUtil.format6chars(day); 163 } 164 try { 165 if (pageLink == null) { url = URLUtilities.getWeblogCollectionURL(weblog, locale, cat, dateString, -1, false); 167 } else { url = URLUtilities.getWeblogPageURL(weblog, locale, pageLink, null, cat, dateString, -1, false); 169 } 170 } catch (Exception e) { 171 log.error("ERROR: creating URL",e); 172 } 173 return url; 174 } 175 176 public String getContent(Date day) { 177 return null; 178 } 179 180 public Calendar getCalendar() { 181 return (Calendar )calendar.clone(); 182 } 183 184 public Date getNextMonth() { 185 Calendar nextCal = getCalendar(); 186 nextCal.setTime( day ); 187 nextCal.add( Calendar.MONTH, 1 ); 188 return getFirstDayOfMonth(nextCal).getTime(); 189 } 190 191 protected Calendar getFirstDayOfMonth(Calendar cal) { 192 int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH); 193 cal.set(Calendar.DAY_OF_MONTH, firstDay); 194 return cal; 195 } 196 197 protected Calendar getLastDayOfMonth(Calendar cal) { 198 int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); 199 cal.set(Calendar.DAY_OF_MONTH, lastDay); 200 return cal; 201 } 202 203 public String computeNextMonthUrl() { 204 Calendar nextCal = getCalendar(); 206 nextCal.setTime( day ); 207 nextCal.add( Calendar.MONTH, 1 ); 208 String nextMonth = computeUrl(nextCal.getTime(), true, true); 209 210 return nextMonth; 212 } 213 214 public String computePrevMonthUrl() { 215 Calendar prevCal = getCalendar(); 217 prevCal.setTime(day); 218 prevCal.add(Calendar.MONTH, -1); 219 String prevMonth = computeUrl(prevCal.getTime(), true, true); 221 222 return prevMonth; 224 } 225 226 public String computeTodayMonthUrl() { 227 return URLUtilities.getWeblogCollectionURL(weblog, locale, cat, null, -1, false); 228 } 229 230 } 231 | Popular Tags |