KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > SqlRedirection


1 /*
2  * SqlRedirection is part of the Cofax content management system library.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Please see http://www.cofax.org for contact information and other related informaion.
19  *
20  * $Header: /cvsroot/cofax/cofax/src/org/cofax/SqlRedirection.java,v 1.7.2.1 2006/12/11 16:28:45 fxrobin Exp $
21  */

22 package org.cofax;
23
24 import java.util.*;
25 import java.text.*;
26 import org.apache.oro.text.perl.Perl5Util;
27
28 /**
29  * Provides redirection capability for Cofax. A subclass of
30  * <code>Redirection</code>. SqlRedirection uses a DataStore class and
31  * exectutes a series of SQL queries to initialize it's private fields. These
32  * fields provide lookups for redirections.
33  *
34  * <p>
35  * There is no mention of the <code>SqlRedirection</code> class in
36  * <code>CofaxServlet</code>. <code>Redirection</code> classes are listed
37  * in the configuration.
38  * </p>
39  *
40  * @author Rajiv Pant
41  * @author Hung Dao
42  * @author Derek Dinh
43  * @author Karl Martino
44  * @author Sam Cohen
45  * @author Lee Bolding
46  * @version 1.9.7
47  *
48  */

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     /**
67      * To be ran once before using the redirection class.
68      */

69     public void init(DataStore db) {
70
71         regex = new Perl5Util();
72
73         // connect to the database.
74
if (db.connect()) {
75             // get package data from daatbase
76
try {
77                 getRedirectDefaultSection(db, "getPublications", publicationSection, publicationRollOverTime);
78                 getRedirectMonthDay(db, "getMonthDays", monthNumber, dayNumber);
79             } catch (Exception JavaDoc e) {
80             } finally {
81                 db.disConnect();
82             }
83         } else {
84             System.out.println(db.getLastError());
85         }
86
87     }
88
89     /**
90      * Returns a set of content by publication = tag value (inquirer =>
91      * front_page ; inquirer => 5).
92      *
93      */

94     public final void getRedirectDefaultSection(DataStore db, String JavaDoc tag, HashMap publicationSection, HashMap publicationRollOverTime) {
95
96         String JavaDoc 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 JavaDoc publication = CofaxUtil.getString(packageRow, "getPublications:pubName");
107                 String JavaDoc defaultSection = CofaxUtil.getString(packageRow, "getPublications:defaultSection");
108                 String JavaDoc rollOverTime = CofaxUtil.getString(packageRow, "getPublications:rolloverTime");
109                 String JavaDoc 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     } // getRedirectDefaultSection
117

118     public final void getRedirectMonthDay(DataStore db, String JavaDoc tag, HashMap monthInNumber, HashMap dayInNumber) {
119
120         String JavaDoc 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 JavaDoc monthNameShort = CofaxUtil.getString(packageRow, "getMonthDays:monthNameShort");
130                 String JavaDoc monthID = CofaxUtil.getString(packageRow, "getMonthDays:monthID");
131                 String JavaDoc dayNameShort = CofaxUtil.getString(packageRow, "getMonthDays:dayNameShort");
132                 String JavaDoc dayID = CofaxUtil.getString(packageRow, "getMonthDays:dayID");
133                 monthInNumber.put(monthNameShort.toLowerCase() + "", monthID + "");
134                 dayInNumber.put(dayNameShort.toLowerCase() + "", dayID + "");
135             }
136         }
137
138     } // getRedirectMonthDay
139

140     /**
141      * Returns a date, based upon a url pattern and rollOverTime, the date from
142      * the path
143      *
144      * @param dayOfWeekNumberPre
145      * contained the dayInNumber from sun-mon equals 1-7
146      * @param rollOverTime
147      * contained the number in hours from 0-23
148      * @param style
149      * is the formated date return, example like "yyyy/MM/dd" =
150      * "2000/10/04"
151      */

152
153     /**
154      * getDatePreferred was cleaned up by Derek 08/09/2001
155      */

156
157     public static String JavaDoc getDatePreferred(int dayOfWeekNumberPre, int rollOverTime, String JavaDoc style) {
158
159         // get today info and compare with request preferred date
160
// Calendar calTodayDate = new GregorianCalendar(2000,0,2) ; // default
161
// test date for next year
162

163         Calendar calTodayDate = new GregorianCalendar(); // default date
164
Locale currentLocale = new Locale("en", "US");
165         int dayOfWeekNumber = calTodayDate.get(Calendar.DAY_OF_WEEK); // sun-sat
166
// = 1-7
167
int hourOfDay = calTodayDate.get(Calendar.HOUR_OF_DAY); // hours = 0-23
168
int yearNumber = calTodayDate.get(Calendar.YEAR); // 2000
169
int monthNumber = calTodayDate.get(Calendar.MONTH); // 10 Jan-Dec = 0-11
170
int dayOfMonth = calTodayDate.get(Calendar.DATE); // 4
171
int previousDay = 0;
172         int monthNumberPre = 0;
173         int dayOfMonthPre = 0;
174         int yearNumberPre;
175         int numberdaysInMonth = 0;
176
177         yearNumberPre = yearNumber;
178
179         // calculated dayOfMonth
180
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) { // if before roll over time Day of
192
// Month Equal
193
previousDay = 1;
194                 dayOfMonthPre = (dayOfMonth - previousDay);
195             } else { // if after roll over time Day of Month Equal
196
dayOfMonthPre = dayOfMonth;
197             }
198         }
199
200         // check if dayOfMonth is neg, then monthNumber -1, else monthNumber
201
if (dayOfMonthPre <= 0) {
202             monthNumberPre = monthNumber - 1; // use last month
203
Calendar getDaysOfMonth = new GregorianCalendar(yearNumberPre, monthNumberPre, 1); //
204
int daysInMonth = getDaysOfMonth.getActualMaximum(Calendar.DAY_OF_MONTH); // days
205
// in
206
// month
207
if ((previousDay - dayOfMonth) >= 0) {
208                 dayOfMonthPre = daysInMonth - (previousDay - dayOfMonth);
209             }
210         } else {
211             monthNumberPre = monthNumber;
212         }
213
214         // check if monthNumber is neg, then yearNumber -1, else yearNumber
215
if (monthNumberPre < 0) {
216             monthNumberPre = 11; // december of lastyear
217
yearNumberPre = yearNumber - 1; // use last year
218
Calendar getDaysOfMonth = new GregorianCalendar(yearNumberPre, monthNumberPre, 1); //
219
int daysInMonth = getDaysOfMonth.getActualMaximum(Calendar.DAY_OF_MONTH); // days
220
// in
221
// month
222
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     } // end getDatePreferred
232

233     /**
234      * Get's a redirection
235      */

236     public synchronized String JavaDoc getRedirection(String JavaDoc pathInfo) {
237
238         String JavaDoc pub = "";
239         String JavaDoc date = "";
240         String JavaDoc section = "";
241         String JavaDoc preferredDate = "";
242         String JavaDoc dayOfWeekNumber = "";
243         String JavaDoc rollOverTime = "";
244         String JavaDoc 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         // need to redirect to Default Section
259
if (regex.match("m#^/([-1-9a-z_]*)/$#", pathInfo)) {
260             // e.g. /inquirer/
261
// dealing with default rollovertime e.g. inq.philly.com =>
262
// ~/content/inquirer/ in mod-rewrite
263

264             section = publicationSection.get(pub).toString();
265             publicationDefaultSectionWithDate = Integer.parseInt(publicationSectionWithDate.get(pub).toString());
266             if (publicationDefaultSectionWithDate == 1) {
267                 // date manipulation only in this case
268

269                 String JavaDoc dayInWeek = CofaxUtil.getDateFormat("EEE", "").toLowerCase(); // return:
270
// mon
271
// -
272
// sun
273
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 JavaDoc 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)) { // ok
299
// /inquirer/2000/(jan|feb...)/10/sports/
300
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         } // default //redirectPathInfo = "/" + pub + "/" + date + "/" +
304
// section + "/" ;
305
return redirectPathInfo;
306
307     }
308
309 }
310
Popular Tags