1 2 package org.roller.presentation.weblog.tags; 3 4 import org.apache.commons.logging.Log; 5 import org.apache.commons.logging.LogFactory; 6 import org.roller.RollerException; 7 import org.roller.model.WeblogManager; 8 import org.roller.presentation.RollerRequest; 9 import org.roller.presentation.tags.calendar.CalendarModel; 10 import org.roller.util.DateUtil; 11 12 import java.text.ParsePosition ; 13 import java.text.SimpleDateFormat ; 14 import java.util.Calendar ; 15 import java.util.Date ; 16 import java.util.HashMap ; 17 import java.util.Map ; 18 19 import javax.servlet.http.HttpServletRequest ; 20 import javax.servlet.http.HttpServletResponse ; 21 22 25 public class WeblogCalendarModel implements CalendarModel 26 { 27 private static Log mLogger = 28 LogFactory.getFactory().getInstance(WeblogCalendarModel.class); 29 30 protected RollerRequest mRollerReq = null; 31 protected HttpServletRequest mReq = null; 32 protected HttpServletResponse mRes = null; 33 protected Map mMonthMap; 34 protected String mSelfUrl; 35 protected Date mDay; 36 protected String mCatName = null; 37 protected Calendar mCalendar = null; 38 39 public WeblogCalendarModel( 40 RollerRequest rreq, HttpServletResponse res, String url, String cat) 41 { 42 mRollerReq = rreq; 43 mReq = rreq.getRequest(); 44 mRes = res; 45 mSelfUrl = url; 46 47 setDay( mRollerReq.getDate(true) ); 48 49 String catKey = RollerRequest.WEBLOGCATEGORYNAME_KEY; 51 String catToUse = mReq.getParameter(catKey); 52 if (mReq.getParameter(catKey) == null) 53 { 54 catToUse = cat; 56 } 57 if ( catToUse != null ) 58 { 59 mCatName = "?"+catKey+"="+catToUse; 60 } 61 else 62 { 63 mCatName = ""; 64 } 65 } 66 73 private void setDay(Date month) 74 { 75 mDay = month; 76 77 String catName = 79 mReq.getParameter(RollerRequest.WEBLOGCATEGORYNAME_KEY); 80 81 mCalendar = Calendar.getInstance( 82 mRollerReq.getWebsite().getTimeZoneInstance(), 83 mRollerReq.getWebsite().getLocaleInstance() 84 ); 85 86 Calendar cal = (Calendar )mCalendar.clone(); 87 88 cal.setTime(month); 90 cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DAY_OF_MONTH)); 91 cal.set(Calendar.HOUR_OF_DAY, cal.getMinimum(Calendar.HOUR_OF_DAY)); 92 cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE)); 93 cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND)); 94 cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND)); 95 Date startDate = cal.getTime(); 96 97 cal.set(Calendar.DAY_OF_MONTH, cal.getMaximum(Calendar.DAY_OF_MONTH)); 99 cal.set(Calendar.HOUR_OF_DAY, cal.getMaximum(Calendar.HOUR_OF_DAY)); 100 cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE)); 101 cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND)); 102 cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND)); 103 Date endDate = cal.getTime(); 104 105 loadWeblogEntries(startDate, endDate, catName); 106 } 107 108 112 protected void loadWeblogEntries(Date startDate, Date endDate, String catName) 113 { 114 try 115 { 116 WeblogManager mgr = mRollerReq.getRoller().getWeblogManager(); 117 mMonthMap = mgr.getWeblogEntryStringMap( 118 mRollerReq.getWebsite(), startDate, endDate, catName, WeblogManager.PUB_ONLY, null 124 ); 125 } 126 catch (RollerException e) 127 { 128 mLogger.error(e); 129 mMonthMap = new HashMap (); 130 } 131 } 132 133 public void setDay(String month) throws Exception 134 { 135 SimpleDateFormat fmt = DateUtil.get8charDateFormat(); 136 ParsePosition pos = new ParsePosition (0); 137 setDay( fmt.parse( month, pos ) ); 138 } 139 140 public Date getDay() 141 { 142 return (Date )mDay.clone(); 143 } 144 145 public String getSelfUrl() throws Exception 146 { 147 return mRes.encodeURL(mSelfUrl); 148 } 149 150 public String getTargetUrl() throws Exception 151 { 152 return getSelfUrl(); 153 } 154 155 public String getParameterName() 156 { 157 return RollerRequest.WEBLOGDAY_KEY; 158 } 159 160 public String getParameterValue( Date day ) 161 { 162 return (String )mMonthMap.get( day ); 163 } 164 165 171 public String computeUrl(java.util.Date day, boolean valid) 172 { 173 String url = null; 174 try 175 { 176 if ( day == null ) 177 { 178 url = mRes.encodeURL(mSelfUrl + mCatName); 179 } 180 else 181 { 182 String dateString = (String )mMonthMap.get( day ); 185 if ( dateString != null ) 186 { 187 url = mRes.encodeURL(mSelfUrl+"/"+dateString+mCatName); 189 } 190 else if ( valid ) 191 { 192 dateString = DateUtil.format8chars( day ); 194 url = mRes.encodeURL( mSelfUrl+"/"+dateString+mCatName); 195 } 196 } 197 } 198 catch (Exception e) 199 { 200 mRollerReq.getServletContext().log("ERROR: creating URL",e); 201 } 202 return url; 203 } 204 205 208 public String getContent(Date day) 209 { 210 return null; 211 } 212 213 public Calendar getCalendar() 214 { 215 return (Calendar )mCalendar.clone(); 216 } 217 218 public Date getNextMonth() 219 { 220 Calendar nextCal = getCalendar(); 221 nextCal.setTime( mDay ); 222 nextCal.add( Calendar.MONTH, 1 ); 223 return getFirstDayOfMonth(nextCal).getTime(); 224 } 225 226 protected Calendar getFirstDayOfMonth(Calendar cal) 227 { 228 int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH); 229 cal.set(Calendar.DAY_OF_MONTH, firstDay); 230 return cal; 231 } 232 233 protected Calendar getLastDayOfMonth(Calendar cal) 234 { 235 int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); 236 cal.set(Calendar.DAY_OF_MONTH, lastDay); 237 return cal; 238 } 239 240 public String computeNextMonthUrl() 241 { 242 Calendar nextCal = getCalendar(); 244 nextCal.setTime( mDay ); 245 nextCal.add( Calendar.MONTH, 1 ); 246 Date nextMonth = getLastDayOfMonth(nextCal).getTime(); 247 String nextMonthUrl = computeUrl(nextMonth, true); 248 return nextMonthUrl; 249 } 250 251 public String computePrevMonthUrl() 252 { 253 Calendar prevCal = getCalendar(); 254 prevCal.setTime( mDay ); 255 prevCal.add( Calendar.MONTH, -1 ); 256 getLastDayOfMonth( prevCal ); 257 String prevMonth = computeUrl(prevCal.getTime(),true); 258 return prevMonth; 259 } 260 261 public String computeTodayMonthUrl() 262 { 263 return computeUrl(null,true); 264 } 265 } 266 267 | Popular Tags |