1 25 package org.snipsnap.date; 26 27 import org.radeox.util.i18n.ResourceManager; 28 import org.snipsnap.app.Application; 29 import org.snipsnap.config.Configuration; 30 import org.snipsnap.snip.Snip; 31 import org.snipsnap.snip.SnipLink; 32 import org.snipsnap.snip.SnipSpace; 33 import org.snipsnap.snip.SnipSpaceFactory; 34 35 import java.text.DateFormatSymbols ; 36 import java.util.Calendar ; 37 import java.util.GregorianCalendar ; 38 import java.util.HashSet ; 39 import java.util.Iterator ; 40 import java.util.List ; 41 import java.util.Locale ; 42 import java.util.Set ; 43 44 50 public class Month { 51 52 53 private String [] monthsValue = { 59 "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" 60 }; 61 62 private String [] months; 64 private String [] weekDaysShort; 65 private String [] weekDaysLong; 66 private Locale locale; 67 68 71 public final static int dom[] = { 72 31, 28, 31, 30, 73 31, 30, 31, 31, 74 30, 31, 30, 31 75 }; 76 77 public Month() { 78 locale = ResourceManager.getLocale("i18n.messages"); 79 DateFormatSymbols symbols = new DateFormatSymbols (locale); 80 months = symbols.getMonths(); 81 weekDaysLong = symbols.getWeekdays(); 82 weekDaysShort = symbols.getShortWeekdays(); 83 } 84 85 public static String toKey(Calendar calendar) { 86 return toKey(calendar.get(Calendar.YEAR), 87 calendar.get(Calendar.MONTH) + 1, 88 calendar.get(Calendar.DAY_OF_MONTH)); 89 } 90 91 public static String toKey(int year, int month, int day) { 92 return year + "-" + (month < 10 ? "0" + month : "" + month) + "-" + (day < 10 ? "0" + day : "" + day); 93 } 94 95 99 public Set getDays(String namespace, int month, int year) { 100 String start = toKey(year, month, 1); 101 String end = toKey(year, month, 31); 102 List snips = SnipSpaceFactory.getInstance().getByDate(namespace, start, end); 103 Iterator iterator = snips.iterator(); 104 105 Set days = new HashSet (); 106 107 while (iterator.hasNext()) { 108 Snip snip = (Snip) iterator.next(); 109 days.add(snip.getName()); 110 } 111 return days; 112 } 113 114 public String getView(boolean navigation) { 115 Calendar today = new GregorianCalendar (locale); 116 today.setTime(new java.util.Date ()); 117 return getView(today.get(Calendar.MONTH) + 1, today.get(Calendar.YEAR), navigation); 118 } 119 120 public String getView(int month, int year, boolean navigation) { 121 122 int nextYear = year; 123 int nextMonth = month + 1; 124 if (nextMonth == 13) { 125 nextYear++; 126 nextMonth = 1; 127 } 128 int prevYear = year; 129 int prevMonth = month - 1; 130 if (prevMonth == 0) { 131 prevYear--; 132 prevMonth = 12; 133 } 134 135 Application app = Application.get(); 136 Configuration config = app.getConfiguration(); 137 Snip viewedSnip = (Snip) app.getParameters().get("viewed"); 138 String weblogName = (String ) app.getParameters().get("calsnip"); 139 if (weblogName != null) { 140 SnipSpace space = SnipSpaceFactory.getInstance(); 141 if (space.exists(weblogName)) { 142 viewedSnip = SnipSpaceFactory.getInstance().load(weblogName); 143 } 144 } 145 String viewed = viewedSnip != null && viewedSnip.isWeblog() ? viewedSnip.getName() : config.getStartSnip(); 146 147 StringBuffer view = new StringBuffer (); 148 view.append("<div class=\"calendar\">"); 149 view.append("<table summary=\""); 150 view.append(ResourceManager.getString("i18n.messages", "month.summary")); 151 view.append("\">"); 152 view.append("<caption>"); 153 154 155 if (navigation) { 156 view.append("<a HREF=\""); 157 view.append(SnipLink.getSpaceRoot()).append("/"); 158 view.append(viewed); 159 view.append("?calsnip="); 160 view.append(SnipLink.encode(viewed)); 161 view.append("&calmonth="); 162 view.append(prevMonth); 163 view.append("&calyear="); 164 view.append(prevYear); 165 view.append("\"><</a> "); 166 } 167 view.append(months[month - 1]); 168 view.append(" "); 169 view.append(year); 170 view.append(" "); 171 if (navigation) { 172 view.append("<a HREF=\""); 173 view.append(SnipLink.getSpaceRoot()).append("/"); 174 view.append(viewed); 175 view.append("?calsnip="); 176 view.append(SnipLink.encode(viewed)); 177 view.append("&calmonth="); 178 view.append(nextMonth); 179 view.append("&calyear="); 180 view.append(nextYear); 181 view.append("\">></a>"); 182 } 183 if (viewedSnip != null && !viewed.equals(config.getStartSnip())) { 184 view.append(" ("); 185 view.append(SnipLink.cutLength(viewedSnip.getTitle(), 20)); 186 view.append(")"); 187 } 188 view.append("</caption>"); 189 190 if (month < 1 || month > 12) { 191 throw new IllegalArgumentException ("Month " + month + " bad, must be 1-12"); 192 } 193 194 Calendar today = new GregorianCalendar (locale); 195 today.setTime(new java.util.Date ()); 196 int todayNumber = today.get(Calendar.DAY_OF_MONTH); 197 198 GregorianCalendar calendar = new GregorianCalendar (locale); 199 calendar.set(year, month - 1, 1); 200 201 int leadGap = calendar.get(Calendar.DAY_OF_WEEK); 204 leadGap = leadGap - calendar.getFirstDayOfWeek(); 205 if (leadGap < 0) { 206 leadGap = 6; 207 } 208 209 int daysInMonth = dom[month - 1]; 210 if (calendar.isLeapYear(calendar.get(Calendar.YEAR)) && month == 2) { 211 ++daysInMonth; 212 } 213 214 view.append(getHeader(calendar.getFirstDayOfWeek())); 215 217 StringBuffer week = new StringBuffer (); 218 for (int i = 0; i < leadGap; i++) { 220 week.append("<td></td>"); 221 } 222 223 Set days = getDays(viewed, month, year); 225 226 for (int i = 1; i <= daysInMonth; i++) { 227 String day = "" + i; 228 229 String calBlogOld = toKey(year, month, i); 230 String calBlogNew = viewed + "/" + calBlogOld + "/1"; 231 232 if (days.contains(calBlogNew)) { 233 day = makeLink(SnipLink.encode(calBlogNew) + "?calsnip=" + SnipLink.encode(viewed) + "&calmonth=" + month + "&calyear=" + year, day); 234 } else if (days.contains(calBlogOld)) { 235 day = makeLink(SnipLink.encode(calBlogOld) + "?calsnip=" + SnipLink.encode(viewed) + "&calmonth=" + month + "&calyear=" + year, day); 236 } 237 238 if (i == todayNumber && month == today.get(Calendar.MONTH) + 1 && year == today.get(Calendar.YEAR)) { 239 day = "<span class=\"today\">" + day + "</span>"; 240 } 241 week.append("<td>"); 242 week.append(day); 243 week.append("</td>"); 244 245 if ((leadGap + i) % 7 == 0) { 247 view.append("<tr>"); 248 view.append(week.toString()); 249 week.setLength(0); 250 view.append("</tr>"); 251 } 252 253 } 254 view.append("<tr>"); 255 view.append(week); 256 view.append("</tr>"); 257 view.append("</table></div>"); 258 return view.toString(); 259 } 260 261 private String getHeader(int offset) { 262 StringBuffer buffer = new StringBuffer (); 263 buffer.append("<tr>"); 264 for (int i = offset - 1; i < weekDaysShort.length - 2 + offset; i++) { 265 buffer.append("<th abbr=\""); 266 buffer.append(weekDaysLong[i % 7 + 1]); 267 buffer.append("\">"); 268 buffer.append(weekDaysShort[i % 7 + 1]); 269 buffer.append("</th>"); 270 } 271 buffer.append("</tr>"); 272 return buffer.toString(); 273 } 274 275 private String makeLink(String snipName, String view) { 276 StringBuffer linkBuffer = new StringBuffer (); 277 return SnipLink.appendLinkWithRoot(linkBuffer, SnipLink.getSpaceRoot(), snipName, view).toString(); 278 } 279 } 280 | Popular Tags |