KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > http > portlets > RecentPostsTab


1 /*
2   Copyright (C) 2003-2006 Know Gate S.L. All rights reserved.
3                            C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.http.portlets;
34
35 import java.io.File JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.ByteArrayOutputStream JavaDoc;
38 import java.io.ByteArrayInputStream JavaDoc;
39 import java.io.OutputStreamWriter JavaDoc;
40 import java.io.PrintWriter JavaDoc;
41
42 import java.util.Date JavaDoc;
43 import java.util.Properties JavaDoc;
44 import java.util.Enumeration JavaDoc;
45
46 import java.sql.SQLException JavaDoc;
47 import java.sql.PreparedStatement JavaDoc;
48 import java.sql.ResultSet JavaDoc;
49 import java.sql.Timestamp JavaDoc;
50
51 import javax.xml.transform.TransformerException JavaDoc;
52 import javax.xml.transform.TransformerConfigurationException JavaDoc;
53
54 import javax.portlet.*;
55
56 import com.knowgate.debug.DebugFile;
57 import com.knowgate.jdc.JDCConnection;
58 import com.knowgate.dataobjs.*;
59 import com.knowgate.dataxslt.StylesheetCache;
60 import com.knowgate.misc.Gadgets;
61 import com.knowgate.dfs.FileSystem;
62
63 /**
64  * Recent Posts Tabbed Dialog Portlet
65  * @author Sergio Montoro Ten
66  * @version 1.0
67  */

68
69 public class RecentPostsTab extends GenericPortlet {
70   public RecentPostsTab() { }
71
72   public RecentPostsTab(HipergatePortletConfig oConfig)
73     throws javax.portlet.PortletException {
74
75     init(oConfig);
76   }
77
78   // ---------------------------------------------------------------------------
79

80   public String JavaDoc render(RenderRequest req, String JavaDoc sEncoding)
81     throws PortletException, IOException JavaDoc, IllegalStateException JavaDoc {
82
83     ByteArrayInputStream JavaDoc oInStream;
84     ByteArrayOutputStream JavaDoc oOutStream;
85
86     if (DebugFile.trace) {
87       DebugFile.writeln("Begin RecentPosts.render()");
88       DebugFile.incIdent();
89     }
90
91     final int iMaxRecent = 10;
92
93     FileSystem oFS = new FileSystem(FileSystem.OS_PUREJAVA);
94
95     String JavaDoc sOutput;
96     String JavaDoc sDomainId = req.getProperty("domain");
97     String JavaDoc sWorkAreaId = req.getProperty("workarea");
98     String JavaDoc sUserId = req.getProperty("user");
99     String JavaDoc sZone = req.getProperty("zone");
100     String JavaDoc sLang = req.getProperty("language");
101     String JavaDoc sTemplatePath = req.getProperty("template");
102     String JavaDoc sStorage = req.getProperty("storage");
103     String JavaDoc sFileDir = "file://" + sStorage + "domains" + File.separator + sDomainId + File.separator + "workareas" + File.separator + sWorkAreaId + File.separator + "cache" + File.separator + sUserId;
104     String JavaDoc sCachedFile = "recentpoststab_" + req.getWindowState().toString() + ".xhtm";
105
106     if (DebugFile.trace) {
107       DebugFile.writeln ("user=" + sUserId);
108       DebugFile.writeln ("template=" + sTemplatePath);
109       DebugFile.writeln ("cache dir=" + sFileDir);
110       DebugFile.writeln ("modified=" + req.getAttribute("modified"));
111       DebugFile.writeln ("encoding=" + sEncoding);
112     }
113
114     Date JavaDoc oDtModified = (Date JavaDoc) req.getAttribute("modified");
115
116     if (null!=oDtModified) {
117       try {
118
119         File JavaDoc oCached = new File JavaDoc(sFileDir.substring(7)+File.separator+sCachedFile);
120
121         if (!oCached.exists()) {
122           oFS.mkdirs(sFileDir);
123         }
124         else if (oCached.lastModified()>oDtModified.getTime()) {
125           sOutput = new String JavaDoc(oFS.readfile(sFileDir+File.separator+sCachedFile, sEncoding==null ? "ISO8859_1" : sEncoding));
126
127           if (DebugFile.trace) {
128             DebugFile.writeln("cache hit " + sFileDir+File.separator+sCachedFile);
129             DebugFile.decIdent();
130             DebugFile.writeln("End RecentPosts.render()");
131           }
132
133           return sOutput;
134         }
135       }
136       catch (Exception JavaDoc xcpt) {
137         DebugFile.writeln(xcpt.getClass().getName() + " " + xcpt.getMessage());
138       }
139     }
140
141     String JavaDoc sXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet type=\"text/xsl\"?>";
142
143     int iPosts = 0;
144
145     if (req.getWindowState().equals(WindowState.MINIMIZED)) {
146       sXML += "<posts/>";
147     }
148     else {
149
150       DBBind oDBB = (DBBind) getPortletContext().getAttribute("GlobalDBBind");
151
152       DBSubset oPosts = new DBSubset (DB.k_newsmsgs + " m," + DB.k_x_cat_objs + " x," + DB.k_newsgroups + " g",
153       "x." + DB.gu_category + ",m." + DB.gu_msg + ",m." + DB.gu_product + ",m." + DB.nm_author + ",m." + DB.tx_subject +
154       ",m." + DB.dt_published + ",m." + DB.tx_email + ",m." + DB.nu_thread_msgs + ",m." + DB.gu_thread_msg,
155       "m." + DB.id_status + "=0 AND x." + DB.gu_category + "=" + "g." + DB.gu_newsgrp + " AND m." +
156       DB.gu_msg + "=x." + DB.gu_object + " AND m." + DB.gu_parent_msg + " IS NULL AND g." + DB.gu_workarea +
157       "=? ORDER BY 6 DESC", iMaxRecent);
158
159       JDCConnection oCon = null;
160
161       try {
162         oCon = oDBB.getConnection("RecentPostsTab");
163
164         iPosts = oPosts.load (oCon, new Object JavaDoc[]{sWorkAreaId});
165
166         oCon.close("RecentPostsTab");
167         oCon = null;
168
169         Date JavaDoc dtPub;
170         for (int p=0; p<iPosts; p++) {
171           dtPub = oPosts.getDate(5,p);
172           oPosts.setElementAt(Gadgets.leftPad(String.valueOf(dtPub.getMonth()+1),'0',2)+"-"+Gadgets.leftPad(String.valueOf(dtPub.getDate()),'0',2)+" "+Gadgets.leftPad(String.valueOf(dtPub.getHours()),'0',2)+":"+Gadgets.leftPad(String.valueOf(dtPub.getMinutes()),'0',2), 5,p);
173         }
174
175         sXML += "<posts>\n"+oPosts.toXML("","newsmsg")+"</posts>";
176       }
177       catch (SQLException JavaDoc e) {
178         sXML += "<posts/>";
179
180         try {
181           if (null != oCon)
182             if (!oCon.isClosed())
183               oCon.close("RecentPostsTab");
184         } catch (SQLException JavaDoc ignore) { }
185       }
186     }
187
188     try {
189        if (DebugFile.trace) DebugFile.writeln("new ByteArrayInputStream(" + String.valueOf(sXML.length()) + ")");
190
191        if (sEncoding==null)
192          oInStream = new ByteArrayInputStream JavaDoc(sXML.getBytes());
193        else
194          oInStream = new ByteArrayInputStream JavaDoc(sXML.getBytes(sEncoding));
195
196        oOutStream = new ByteArrayOutputStream JavaDoc(4000);
197
198        Properties JavaDoc oProps = new Properties JavaDoc();
199
200        Enumeration JavaDoc oKeys = req.getPropertyNames();
201        while (oKeys.hasMoreElements()) {
202          String JavaDoc sKey = (String JavaDoc) oKeys.nextElement();
203          oProps.setProperty(sKey, req.getProperty(sKey));
204        } // wend
205

206        if (req.getWindowState().equals(WindowState.MINIMIZED))
207          oProps.setProperty("windowstate", "MINIMIZED");
208        else
209          oProps.setProperty("windowstate", "NORMAL");
210
211        StylesheetCache.transform (sTemplatePath, oInStream, oOutStream, oProps);
212
213        if (sEncoding==null)
214          sOutput = oOutStream.toString();
215        else
216          sOutput = oOutStream.toString("UTF-8");
217
218        oOutStream.close();
219
220        oInStream.close();
221        oInStream = null;
222
223        oFS.writefilestr (sFileDir+File.separator+sCachedFile, sOutput, sEncoding==null ? "ISO8859_1" : sEncoding);
224      }
225      catch (TransformerConfigurationException JavaDoc tce) {
226        if (DebugFile.trace) {
227          DebugFile.writeln("TransformerConfigurationException " + tce.getMessageAndLocation());
228          try {
229            DebugFile.write("--------------------------------------------------------------------------------\n");
230            DebugFile.write(FileSystem.readfile(sTemplatePath));
231            DebugFile.write("\n--------------------------------------------------------------------------------\n");
232            DebugFile.write(sXML);
233            DebugFile.write("\n--------------------------------------------------------------------------------\n");
234          }
235          catch (java.io.IOException JavaDoc ignore) { }
236          catch (com.enterprisedt.net.ftp.FTPException ignore) { }
237
238          DebugFile.decIdent();
239        }
240        throw new PortletException("TransformerConfigurationException " + tce.getMessage(), tce);
241      }
242      catch (TransformerException JavaDoc tex) {
243        if (DebugFile.trace) {
244          DebugFile.writeln("TransformerException " + tex.getMessageAndLocation());
245
246          try {
247            DebugFile.write("--------------------------------------------------------------------------------\n");
248            DebugFile.write(FileSystem.readfile(sTemplatePath));
249            DebugFile.write("\n--------------------------------------------------------------------------------\n");
250            DebugFile.write(sXML);
251            DebugFile.write("\n--------------------------------------------------------------------------------\n");
252          }
253          catch (java.io.IOException JavaDoc ignore) { }
254          catch (com.enterprisedt.net.ftp.FTPException ignore) { }
255
256          DebugFile.decIdent();
257        }
258        throw new PortletException("TransformerException " + tex.getMessage(), tex);
259      }
260
261      if (DebugFile.trace) {
262        DebugFile.decIdent();
263        DebugFile.writeln("End RecentPosts.render()");
264      }
265      return sOutput;
266    }
267
268    // --------------------------------------------------------------------------
269

270    public void render(RenderRequest req, RenderResponse res)
271      throws PortletException, IOException JavaDoc, IllegalStateException JavaDoc {
272      res.getWriter().write(render(req,res.getCharacterEncoding()));
273     }
274 }
275
Popular Tags