001: /*
002: Copyright (C) 2003-2006 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.http.portlets;
034:
035: import java.io.File;
036: import java.io.IOException;
037: import java.io.ByteArrayOutputStream;
038: import java.io.ByteArrayInputStream;
039: import java.io.OutputStreamWriter;
040: import java.io.PrintWriter;
041:
042: import java.util.Date;
043: import java.util.Properties;
044: import java.util.Enumeration;
045:
046: import java.sql.SQLException;
047: import java.sql.PreparedStatement;
048: import java.sql.ResultSet;
049: import java.sql.Timestamp;
050:
051: import javax.xml.transform.TransformerException;
052: import javax.xml.transform.TransformerConfigurationException;
053:
054: import javax.portlet.*;
055:
056: import com.knowgate.debug.DebugFile;
057: import com.knowgate.jdc.JDCConnection;
058: import com.knowgate.dataobjs.*;
059: import com.knowgate.dataxslt.StylesheetCache;
060: import com.knowgate.misc.Gadgets;
061: import com.knowgate.dfs.FileSystem;
062:
063: /**
064: * Recent Posts Tabbed Dialog Portlet
065: * @author Sergio Montoro Ten
066: * @version 1.0
067: */
068:
069: public class RecentPostsTab extends GenericPortlet {
070: public RecentPostsTab() {
071: }
072:
073: public RecentPostsTab(HipergatePortletConfig oConfig)
074: throws javax.portlet.PortletException {
075:
076: init(oConfig);
077: }
078:
079: // ---------------------------------------------------------------------------
080:
081: public String render(RenderRequest req, String sEncoding)
082: throws PortletException, IOException, IllegalStateException {
083:
084: ByteArrayInputStream oInStream;
085: ByteArrayOutputStream oOutStream;
086:
087: if (DebugFile.trace) {
088: DebugFile.writeln("Begin RecentPosts.render()");
089: DebugFile.incIdent();
090: }
091:
092: final int iMaxRecent = 10;
093:
094: FileSystem oFS = new FileSystem(FileSystem.OS_PUREJAVA);
095:
096: String sOutput;
097: String sDomainId = req.getProperty("domain");
098: String sWorkAreaId = req.getProperty("workarea");
099: String sUserId = req.getProperty("user");
100: String sZone = req.getProperty("zone");
101: String sLang = req.getProperty("language");
102: String sTemplatePath = req.getProperty("template");
103: String sStorage = req.getProperty("storage");
104: String sFileDir = "file://" + sStorage + "domains"
105: + File.separator + sDomainId + File.separator
106: + "workareas" + File.separator + sWorkAreaId
107: + File.separator + "cache" + File.separator + sUserId;
108: String sCachedFile = "recentpoststab_"
109: + req.getWindowState().toString() + ".xhtm";
110:
111: if (DebugFile.trace) {
112: DebugFile.writeln("user=" + sUserId);
113: DebugFile.writeln("template=" + sTemplatePath);
114: DebugFile.writeln("cache dir=" + sFileDir);
115: DebugFile.writeln("modified="
116: + req.getAttribute("modified"));
117: DebugFile.writeln("encoding=" + sEncoding);
118: }
119:
120: Date oDtModified = (Date) req.getAttribute("modified");
121:
122: if (null != oDtModified) {
123: try {
124:
125: File oCached = new File(sFileDir.substring(7)
126: + File.separator + sCachedFile);
127:
128: if (!oCached.exists()) {
129: oFS.mkdirs(sFileDir);
130: } else if (oCached.lastModified() > oDtModified
131: .getTime()) {
132: sOutput = new String(oFS
133: .readfile(sFileDir + File.separator
134: + sCachedFile,
135: sEncoding == null ? "ISO8859_1"
136: : sEncoding));
137:
138: if (DebugFile.trace) {
139: DebugFile.writeln("cache hit " + sFileDir
140: + File.separator + sCachedFile);
141: DebugFile.decIdent();
142: DebugFile.writeln("End RecentPosts.render()");
143: }
144:
145: return sOutput;
146: }
147: } catch (Exception xcpt) {
148: DebugFile.writeln(xcpt.getClass().getName() + " "
149: + xcpt.getMessage());
150: }
151: }
152:
153: String sXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet type=\"text/xsl\"?>";
154:
155: int iPosts = 0;
156:
157: if (req.getWindowState().equals(WindowState.MINIMIZED)) {
158: sXML += "<posts/>";
159: } else {
160:
161: DBBind oDBB = (DBBind) getPortletContext().getAttribute(
162: "GlobalDBBind");
163:
164: DBSubset oPosts = new DBSubset(DB.k_newsmsgs + " m,"
165: + DB.k_x_cat_objs + " x," + DB.k_newsgroups + " g",
166: "x." + DB.gu_category + ",m." + DB.gu_msg + ",m."
167: + DB.gu_product + ",m." + DB.nm_author
168: + ",m." + DB.tx_subject + ",m."
169: + DB.dt_published + ",m." + DB.tx_email
170: + ",m." + DB.nu_thread_msgs + ",m."
171: + DB.gu_thread_msg, "m." + DB.id_status
172: + "=0 AND x." + DB.gu_category + "=" + "g."
173: + DB.gu_newsgrp + " AND m." + DB.gu_msg
174: + "=x." + DB.gu_object + " AND m."
175: + DB.gu_parent_msg + " IS NULL AND g."
176: + DB.gu_workarea + "=? ORDER BY 6 DESC",
177: iMaxRecent);
178:
179: JDCConnection oCon = null;
180:
181: try {
182: oCon = oDBB.getConnection("RecentPostsTab");
183:
184: iPosts = oPosts
185: .load(oCon, new Object[] { sWorkAreaId });
186:
187: oCon.close("RecentPostsTab");
188: oCon = null;
189:
190: Date dtPub;
191: for (int p = 0; p < iPosts; p++) {
192: dtPub = oPosts.getDate(5, p);
193: oPosts.setElementAt(Gadgets.leftPad(String
194: .valueOf(dtPub.getMonth() + 1), '0', 2)
195: + "-"
196: + Gadgets.leftPad(String.valueOf(dtPub
197: .getDate()), '0', 2)
198: + " "
199: + Gadgets.leftPad(String.valueOf(dtPub
200: .getHours()), '0', 2)
201: + ":"
202: + Gadgets.leftPad(String.valueOf(dtPub
203: .getMinutes()), '0', 2), 5, p);
204: }
205:
206: sXML += "<posts>\n" + oPosts.toXML("", "newsmsg")
207: + "</posts>";
208: } catch (SQLException e) {
209: sXML += "<posts/>";
210:
211: try {
212: if (null != oCon)
213: if (!oCon.isClosed())
214: oCon.close("RecentPostsTab");
215: } catch (SQLException ignore) {
216: }
217: }
218: }
219:
220: try {
221: if (DebugFile.trace)
222: DebugFile.writeln("new ByteArrayInputStream("
223: + String.valueOf(sXML.length()) + ")");
224:
225: if (sEncoding == null)
226: oInStream = new ByteArrayInputStream(sXML.getBytes());
227: else
228: oInStream = new ByteArrayInputStream(sXML
229: .getBytes(sEncoding));
230:
231: oOutStream = new ByteArrayOutputStream(4000);
232:
233: Properties oProps = new Properties();
234:
235: Enumeration oKeys = req.getPropertyNames();
236: while (oKeys.hasMoreElements()) {
237: String sKey = (String) oKeys.nextElement();
238: oProps.setProperty(sKey, req.getProperty(sKey));
239: } // wend
240:
241: if (req.getWindowState().equals(WindowState.MINIMIZED))
242: oProps.setProperty("windowstate", "MINIMIZED");
243: else
244: oProps.setProperty("windowstate", "NORMAL");
245:
246: StylesheetCache.transform(sTemplatePath, oInStream,
247: oOutStream, oProps);
248:
249: if (sEncoding == null)
250: sOutput = oOutStream.toString();
251: else
252: sOutput = oOutStream.toString("UTF-8");
253:
254: oOutStream.close();
255:
256: oInStream.close();
257: oInStream = null;
258:
259: oFS.writefilestr(sFileDir + File.separator + sCachedFile,
260: sOutput, sEncoding == null ? "ISO8859_1"
261: : sEncoding);
262: } catch (TransformerConfigurationException tce) {
263: if (DebugFile.trace) {
264: DebugFile.writeln("TransformerConfigurationException "
265: + tce.getMessageAndLocation());
266: try {
267: DebugFile
268: .write("--------------------------------------------------------------------------------\n");
269: DebugFile.write(FileSystem.readfile(sTemplatePath));
270: DebugFile
271: .write("\n--------------------------------------------------------------------------------\n");
272: DebugFile.write(sXML);
273: DebugFile
274: .write("\n--------------------------------------------------------------------------------\n");
275: } catch (java.io.IOException ignore) {
276: } catch (com.enterprisedt.net.ftp.FTPException ignore) {
277: }
278:
279: DebugFile.decIdent();
280: }
281: throw new PortletException(
282: "TransformerConfigurationException "
283: + tce.getMessage(), tce);
284: } catch (TransformerException tex) {
285: if (DebugFile.trace) {
286: DebugFile.writeln("TransformerException "
287: + tex.getMessageAndLocation());
288:
289: try {
290: DebugFile
291: .write("--------------------------------------------------------------------------------\n");
292: DebugFile.write(FileSystem.readfile(sTemplatePath));
293: DebugFile
294: .write("\n--------------------------------------------------------------------------------\n");
295: DebugFile.write(sXML);
296: DebugFile
297: .write("\n--------------------------------------------------------------------------------\n");
298: } catch (java.io.IOException ignore) {
299: } catch (com.enterprisedt.net.ftp.FTPException ignore) {
300: }
301:
302: DebugFile.decIdent();
303: }
304: throw new PortletException("TransformerException "
305: + tex.getMessage(), tex);
306: }
307:
308: if (DebugFile.trace) {
309: DebugFile.decIdent();
310: DebugFile.writeln("End RecentPosts.render()");
311: }
312: return sOutput;
313: }
314:
315: // --------------------------------------------------------------------------
316:
317: public void render(RenderRequest req, RenderResponse res)
318: throws PortletException, IOException, IllegalStateException {
319: res.getWriter().write(render(req, res.getCharacterEncoding()));
320: }
321: }
|