1 22 package org.cofax; 23 24 import java.util.*; 25 import java.text.*; 26 import org.apache.oro.text.perl.Perl5Util; 27 28 49 50 public class SqlRedirection extends Redirection { 51 52 private static HashMap publicationSection = new HashMap(); 53 54 private static HashMap publicationRollOverTime = new HashMap(); 55 56 private static HashMap monthNumber = new HashMap(); 57 58 private static HashMap dayNumber = new HashMap(); 59 60 private static HashMap publicationSectionWithDate = new HashMap(); 61 62 private HashMap redirectionGlossary = new HashMap(); 63 64 private static Perl5Util regex; 65 66 69 public void init(DataStore db) { 70 71 regex = new Perl5Util(); 72 73 if (db.connect()) { 75 try { 77 getRedirectDefaultSection(db, "getPublications", publicationSection, publicationRollOverTime); 78 getRedirectMonthDay(db, "getMonthDays", monthNumber, dayNumber); 79 } catch (Exception e) { 80 } finally { 81 db.disConnect(); 82 } 83 } else { 84 System.out.println(db.getLastError()); 85 } 86 87 } 88 89 94 public final void getRedirectDefaultSection(DataStore db, String tag, HashMap publicationSection, HashMap publicationRollOverTime) { 95 96 String tagStatement = db.getPackageTag(tag, redirectionGlossary, "sqlredir:getRedirectDefaulttSection", "intCofax"); 97 List packageRows = db.getPackageData(tag, tagStatement, "sqlredir:getRedirectDefaulttSection", "intCofax"); 98 99 if (packageRows.size() > 0) { 100 101 Iterator rows = packageRows.iterator(); 102 while (rows.hasNext()) { 103 HashMap packageRow; 104 packageRow = (HashMap) rows.next(); 105 106 String publication = CofaxUtil.getString(packageRow, "getPublications:pubName"); 107 String defaultSection = CofaxUtil.getString(packageRow, "getPublications:defaultSection"); 108 String rollOverTime = CofaxUtil.getString(packageRow, "getPublications:rolloverTime"); 109 String defaultSectionWithDate = CofaxUtil.getString(packageRow, "getPublications:defaultSectionWithDate"); 110 publicationSection.put(publication.toLowerCase() + "", defaultSection.toLowerCase() + ""); 111 publicationRollOverTime.put(publication.toLowerCase() + "", rollOverTime.toLowerCase() + ""); 112 publicationSectionWithDate.put(publication.toLowerCase() + "", defaultSectionWithDate.toLowerCase() + ""); 113 } 114 } 115 116 } 118 public final void getRedirectMonthDay(DataStore db, String tag, HashMap monthInNumber, HashMap dayInNumber) { 119 120 String tagStatement = db.getPackageTag(tag, redirectionGlossary, "sqlredir:getRedirectMonthDay", "intCofax"); 121 List packageRows = db.getPackageData(tag, tagStatement, "sqlredir:getRedirectMonthDay", "intCofax"); 122 123 if (packageRows.size() > 0) { 124 125 Iterator rows = packageRows.iterator(); 126 while (rows.hasNext()) { 127 HashMap packageRow; 128 packageRow = (HashMap) rows.next(); 129 String monthNameShort = CofaxUtil.getString(packageRow, "getMonthDays:monthNameShort"); 130 String monthID = CofaxUtil.getString(packageRow, "getMonthDays:monthID"); 131 String dayNameShort = CofaxUtil.getString(packageRow, "getMonthDays:dayNameShort"); 132 String dayID = CofaxUtil.getString(packageRow, "getMonthDays:dayID"); 133 monthInNumber.put(monthNameShort.toLowerCase() + "", monthID + ""); 134 dayInNumber.put(dayNameShort.toLowerCase() + "", dayID + ""); 135 } 136 } 137 138 } 140 152 153 156 157 public static String getDatePreferred(int dayOfWeekNumberPre, int rollOverTime, String style) { 158 159 163 Calendar calTodayDate = new GregorianCalendar(); Locale currentLocale = new Locale("en", "US"); 165 int dayOfWeekNumber = calTodayDate.get(Calendar.DAY_OF_WEEK); int hourOfDay = calTodayDate.get(Calendar.HOUR_OF_DAY); int yearNumber = calTodayDate.get(Calendar.YEAR); int monthNumber = calTodayDate.get(Calendar.MONTH); int dayOfMonth = calTodayDate.get(Calendar.DATE); int previousDay = 0; 172 int monthNumberPre = 0; 173 int dayOfMonthPre = 0; 174 int yearNumberPre; 175 int numberdaysInMonth = 0; 176 177 yearNumberPre = yearNumber; 178 179 if (dayOfWeekNumberPre <= 0) { 181 dayOfWeekNumberPre = dayOfWeekNumber; 182 } 183 184 if (dayOfWeekNumberPre < dayOfWeekNumber) { 185 previousDay = (dayOfWeekNumber - dayOfWeekNumberPre); 186 dayOfMonthPre = dayOfMonth - previousDay; 187 } else if (dayOfWeekNumberPre > dayOfWeekNumber) { 188 previousDay = (7 - dayOfWeekNumberPre) + dayOfWeekNumber; 189 dayOfMonthPre = dayOfMonth - previousDay; 190 } else if (dayOfWeekNumberPre == dayOfWeekNumber) { 191 if (hourOfDay < rollOverTime) { previousDay = 1; 194 dayOfMonthPre = (dayOfMonth - previousDay); 195 } else { dayOfMonthPre = dayOfMonth; 197 } 198 } 199 200 if (dayOfMonthPre <= 0) { 202 monthNumberPre = monthNumber - 1; Calendar getDaysOfMonth = new GregorianCalendar(yearNumberPre, monthNumberPre, 1); int daysInMonth = getDaysOfMonth.getActualMaximum(Calendar.DAY_OF_MONTH); if ((previousDay - dayOfMonth) >= 0) { 208 dayOfMonthPre = daysInMonth - (previousDay - dayOfMonth); 209 } 210 } else { 211 monthNumberPre = monthNumber; 212 } 213 214 if (monthNumberPre < 0) { 216 monthNumberPre = 11; yearNumberPre = yearNumber - 1; Calendar getDaysOfMonth = new GregorianCalendar(yearNumberPre, monthNumberPre, 1); int daysInMonth = getDaysOfMonth.getActualMaximum(Calendar.DAY_OF_MONTH); if ((previousDay - dayOfMonth) >= 0) { 223 dayOfMonthPre = daysInMonth - (previousDay - dayOfMonth); 224 } 225 } 226 227 Calendar cal = new GregorianCalendar(yearNumberPre, monthNumberPre, dayOfMonthPre); 228 SimpleDateFormat formatter = new SimpleDateFormat(style); 229 return formatter.format(cal.getTime()); 230 231 } 233 236 public synchronized String getRedirection(String pathInfo) { 237 238 String pub = ""; 239 String date = ""; 240 String section = ""; 241 String preferredDate = ""; 242 String dayOfWeekNumber = ""; 243 String rollOverTime = ""; 244 String redirectPathInfo = ""; 245 246 int dayOfWeekNumberPre; 247 int rollOverTimeInt; 248 int publicationDefaultSectionWithDate; 249 250 if (regex.match("m#^/([-1-9a-z_]*)/(.*)$#", pathInfo)) { 251 pub = regex.group(1) + ""; 252 if (!publicationSection.containsKey(pub)) { 253 redirectPathInfo = "INVALID Publication : " + pub; 254 return redirectPathInfo; 255 } 256 } 257 258 if (regex.match("m#^/([-1-9a-z_]*)/$#", pathInfo)) { 260 264 section = publicationSection.get(pub).toString(); 265 publicationDefaultSectionWithDate = Integer.parseInt(publicationSectionWithDate.get(pub).toString()); 266 if (publicationDefaultSectionWithDate == 1) { 267 269 String dayInWeek = CofaxUtil.getDateFormat("EEE", "").toLowerCase(); dayOfWeekNumber = dayNumber.get(dayInWeek).toString(); 274 dayOfWeekNumberPre = Integer.parseInt(dayOfWeekNumber); 275 rollOverTime = publicationRollOverTime.get(pub.toLowerCase()).toString(); 276 rollOverTimeInt = Integer.parseInt(rollOverTime); 277 preferredDate = getDatePreferred(dayOfWeekNumberPre, rollOverTimeInt, "yyyy/MM/dd"); 278 date = preferredDate.toString(); 279 redirectPathInfo = "/" + pub + "/" + date + "/" + section + "/"; 280 281 } 282 283 else { 284 285 redirectPathInfo = "/" + pub + "/" + section + "/"; 286 } 287 } else if (regex.match("m#^/([-1-9a-z_]*)/(today|mon|tue|wed|thu|fri|sat|sun)/(.*)/(.*)$#i", pathInfo)) { 288 section = regex.group(3) + ""; 289 String fileName = regex.group(4) + ""; 290 if (regex.group(2) != null && !regex.group(2).equals("")) 291 dayOfWeekNumber = dayNumber.get(regex.group(2).toLowerCase()).toString(); 292 dayOfWeekNumberPre = Integer.parseInt(dayOfWeekNumber); 293 rollOverTime = publicationRollOverTime.get(pub.toLowerCase()).toString(); 294 rollOverTimeInt = Integer.parseInt(rollOverTime); 295 preferredDate = getDatePreferred(dayOfWeekNumberPre, rollOverTimeInt, "yyyy/MM/dd"); 296 date = preferredDate.toString(); 297 redirectPathInfo = "/" + pub + "/" + date + "/" + section + "/" + fileName; 298 } else if (regex.match("m#^/([-1-9a-z_]*)/([0-9]+)/(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)/([0-9]+)/(.*)/$#i", pathInfo)) { date = regex.group(2) + "/" + monthNumber.get(regex.group(3).toLowerCase()).toString() + "/" + regex.group(4) + ""; 301 section = regex.group(5) + ""; 302 redirectPathInfo = "/" + pub + "/" + date + "/" + section + "/"; 303 } return redirectPathInfo; 306 307 } 308 309 } 310 | Popular Tags |