KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > cms > CofaxToolsLifeCycle


1 /*
2  * CofaxToolsLifeCycle 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/cms/CofaxToolsLifeCycle.java,v 1.9.2.1 2006/12/11 16:28:23 fxrobin Exp $
21  */

22 package org.cofax.cms;
23
24 import org.cofax.*;
25 import java.util.*;
26 import java.io.*;
27 import java.text.*;
28
29 /**
30  * The Cofax project's web serving program.
31  * <p>
32  *
33  *
34  * CofaxToolsLifeCycle : used to update articles out of their life cycle
35  *
36  * @author Smile - Badr Chentouf
37  */

38
39 public class CofaxToolsLifeCycle {
40
41     int count_articles_disabled;
42
43     int count_articles_enabled;
44
45     public String JavaDoc logFile = "";
46
47     public void setLogFile(String JavaDoc logFile) {
48         this.logFile = logFile;
49     }
50
51     public boolean updateAllPublications(DataStore db) {
52         db.setKillTime(1000 * 30000);
53         StringBuffer JavaDoc v_publications = new StringBuffer JavaDoc();
54         v_publications.append("SELECT pubName from tblpublications");
55         String JavaDoc returnMessage = "";
56         try {
57             HashMap ht = new HashMap();
58             List pubs = (ArrayList) db.getPackageData(ht, "", v_publications.toString(), false);
59             HashMap hashinfo;
60             while (pubs.size() > 0) {
61                 hashinfo = (HashMap) pubs.get(0);
62                 String JavaDoc pubName = (String JavaDoc) hashinfo.get("PUBNAME");
63                 returnMessage = updatePublication(db, pubName);
64                 pubs.remove(0);
65             }
66         } catch (Exception JavaDoc e) {
67             log("CofaxToolsLifeCycle : Exception " + e, logFile);
68         }
69         return true;
70     }
71
72     public String JavaDoc updatePublication(DataStore db, String JavaDoc pubName) {
73         try {
74             StringBuffer JavaDoc v_articles = new StringBuffer JavaDoc();
75             v_articles.append("select A.itemID, A.pubStart, A.pubEnd ").append("from tblarticles AS A ").append("where A.pubname= '" + pubName + "' ").append(
76                     "and A.lifeCycle = 1");
77
78             count_articles_disabled = 0;
79             count_articles_enabled = 0;
80
81             ArrayList articles = new ArrayList();
82             HashMap ht = new HashMap();
83             articles = (ArrayList) db.getPackageData(ht, "", v_articles.toString(), false);
84             while (articles.size() > 0) {
85                 HashMap hashinfo = (HashMap) articles.get(0);
86                 String JavaDoc itemID = (String JavaDoc) hashinfo.get("ITEMID");
87                 String JavaDoc pubStart = (String JavaDoc) hashinfo.get("PUBSTART");
88                 String JavaDoc pubEnd = (String JavaDoc) hashinfo.get("PUBEND");
89                 articles.remove(0);
90                 // Test the lifeCycle to publish or not the article
91
int startDateInt = Integer.parseInt(pubStart.substring(0, 4) + pubStart.substring(5, 7) + pubStart.substring(8, 10));
92                 int endDateInt = Integer.parseInt(pubEnd.substring(0, 4) + pubEnd.substring(5, 7) + pubEnd.substring(8, 10));
93                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
94                 String JavaDoc todayString = "" + (String JavaDoc) sdf.format(new java.util.Date JavaDoc()).toString();
95                 int todayDateInt = Integer.parseInt(todayString.substring(0, 4) + todayString.substring(5, 7) + todayString.substring(8, 10));
96                 if ((startDateInt > todayDateInt) || (endDateInt < todayDateInt)) {
97                     // check if the article is already published
98
String JavaDoc published = null;
99                     StringBuffer JavaDoc v_published = new StringBuffer JavaDoc();
100                     v_published.append("SELECT itemID from tblarticlepreload where itemID='" + itemID + "'");
101                     try {
102                         HashMap htTemp = new HashMap();
103                         HashMap ht2 = CofaxToolsDbUtils.getNameValuePackageHash(db, htTemp, v_published.toString());
104                         published = (String JavaDoc) ht2.get("ITEMID");
105                     } catch (Exception JavaDoc e) {
106                         published = null;
107                     }
108                     if ((published != null)) {
109                         // the article is to disable
110
count_articles_disabled++;
111                         HashMap ht3 = new HashMap();
112                         ht3.put("ITEMID", itemID);
113                         String JavaDoc tag = "exec s_updateArticlePreload 'req:ITEMID' "; // CofaxToolsDbUtils.fillTag(db,
114
// "updateArticlePreload");
115
HashMap hto = CofaxToolsDbUtils.getNameValuePackageHash(db, ht3, tag);
116                     }
117                 } else {
118                     // check if the article is already published
119
String JavaDoc published = "0";
120                     StringBuffer JavaDoc v_published = new StringBuffer JavaDoc();
121                     v_published.append("SELECT itemID from tblarticlepreload where itemID='" + itemID + "'");
122                     try {
123                         HashMap htTemp = new HashMap();
124                         HashMap ht2 = CofaxToolsDbUtils.getNameValuePackageHash(db, htTemp, v_published.toString());
125                         published = (String JavaDoc) ht2.get("ITEMID");
126                     } catch (Exception JavaDoc e) {
127                         published = "0";
128                     }
129                     if (((published == null) || published.equals("0"))) {
130                         // the article is to enable
131
count_articles_enabled++;
132                         HashMap ht3 = new HashMap();
133                         ht3.put("ITEMID", itemID);
134                         String JavaDoc tag = "exec s_updateArticlePreload 'req:ITEMID' "; // CofaxToolsDbUtils.fillTag(db,
135
// "updateArticlePreload");
136
HashMap hto = CofaxToolsDbUtils.getNameValuePackageHash(db, ht3, tag);
137                     }
138                 }
139
140             }
141
142         } catch (Exception JavaDoc e) {
143             log("CofaxToolsLifeCycle : Exception : " + e, logFile);
144         }
145         log("CofaxToolsLifeCycle : " + new java.util.Date JavaDoc() + " : Publication '" + pubName + "' : " + count_articles_disabled + " articles disabled and "
146                 + count_articles_enabled + " articles enabled.", logFile);
147         return ("Done Update Publication. ");
148     }
149
150     /**
151      * Logs to a file or to System.out - basic function is to concentrate error
152      * checking.
153      *
154      * @param input
155      * String to log
156      * @param outputFile
157      * Path of the log file
158      */

159     public static void log(String JavaDoc input, String JavaDoc outputFile) {
160         try {
161             if (!(outputFile.equals(""))) {
162                 PrintWriter fileOutputStream = new PrintWriter(new FileWriter(outputFile, true));
163                 fileOutputStream.println(input);
164                 fileOutputStream.close();
165             } else {
166                 System.out.println("CofaxToolsLifeCycle.log : " + input);
167             }
168         } catch (IOException e) {
169             System.out.println("CofaxToolsLifeCycle.log : error : " + e.toString());
170         }
171     }
172
173 }
174
Popular Tags