001: /*
002: Copyright (C) 2003 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: package com.knowgate.http.portlets;
033:
034: import java.io.File;
035: import java.io.IOException;
036: import java.io.ByteArrayOutputStream;
037: import java.io.ByteArrayInputStream;
038: import java.io.OutputStreamWriter;
039: import java.io.PrintWriter;
040:
041: import java.util.Date;
042: import java.util.Properties;
043: import java.util.Enumeration;
044:
045: import java.sql.SQLException;
046: import java.sql.PreparedStatement;
047: import java.sql.ResultSet;
048: import java.sql.Timestamp;
049:
050: import javax.xml.transform.TransformerException;
051: import javax.xml.transform.TransformerConfigurationException;
052:
053: import javax.portlet.*;
054:
055: import com.knowgate.debug.DebugFile;
056: import com.knowgate.jdc.JDCConnection;
057: import com.knowgate.dataobjs.*;
058: import com.knowgate.dataxslt.StylesheetCache;
059: import com.knowgate.misc.Gadgets;
060: import com.knowgate.dfs.FileSystem;
061:
062: /**
063: * @author Sergio Montoro Ten
064: * @version 2.2
065: */
066:
067: public class RecentContactsTab extends GenericPortlet {
068: public RecentContactsTab() {
069: }
070:
071: public RecentContactsTab(HipergatePortletConfig oConfig)
072: throws javax.portlet.PortletException {
073:
074: init(oConfig);
075: }
076:
077: // ---------------------------------------------------------------------------
078:
079: public String render(RenderRequest req, String sEncoding)
080: throws PortletException, IOException, IllegalStateException {
081:
082: ByteArrayInputStream oInStream;
083: ByteArrayOutputStream oOutStream;
084:
085: if (DebugFile.trace) {
086: DebugFile.writeln("Begin RecentContactsTab.render()");
087: DebugFile.incIdent();
088: }
089:
090: final int iMaxRecent = 8;
091:
092: FileSystem oFS = new FileSystem(FileSystem.OS_PUREJAVA);
093:
094: String sOutput;
095: String sDomainId = req.getProperty("domain");
096: String sWorkAreaId = req.getProperty("workarea");
097: String sUserId = req.getProperty("user");
098: String sZone = req.getProperty("zone");
099: String sLang = req.getProperty("language");
100: String sTemplatePath = req.getProperty("template");
101: String sStorage = req.getProperty("storage");
102: String sFileDir = "file://" + sStorage + "domains"
103: + File.separator + sDomainId + File.separator
104: + "workareas" + File.separator + sWorkAreaId
105: + File.separator + "cache" + File.separator + sUserId;
106: String sCachedFile = "recentcontactstab_"
107: + req.getWindowState().toString() + ".xhtm";
108:
109: if (DebugFile.trace) {
110: DebugFile.writeln("user=" + sUserId);
111: DebugFile.writeln("template=" + sTemplatePath);
112: DebugFile.writeln("cache dir=" + sFileDir);
113: DebugFile.writeln("modified="
114: + req.getAttribute("modified"));
115: DebugFile.writeln("encoding=" + sEncoding);
116: }
117:
118: Date oDtModified = (Date) req.getAttribute("modified");
119:
120: if (null != oDtModified) {
121:
122: try {
123:
124: File oCached = new File(sFileDir.substring(7)
125: + File.separator + sCachedFile);
126:
127: if (!oCached.exists()) {
128: oFS.mkdirs(sFileDir);
129: } else {
130: if (DebugFile.trace) {
131: DebugFile.writeln("file modified "
132: + new Date(oCached.lastModified())
133: .toString());
134: DebugFile.writeln("last modified "
135: + new Date(oDtModified.getTime())
136: .toString());
137: }
138: if (oCached.lastModified() > oDtModified.getTime()) {
139: sOutput = new String(FileSystem
140: .readfile(sFileDir + File.separator
141: + sCachedFile,
142: sEncoding == null ? "ISO8859_1"
143: : sEncoding));
144:
145: if (DebugFile.trace) {
146: DebugFile.writeln("cache hit " + sFileDir
147: + File.separator + sCachedFile);
148: DebugFile.decIdent();
149: DebugFile
150: .writeln("End RecentContactsTab.render()");
151: }
152:
153: return sOutput;
154: } // fi (oCached.lastModified()>oDtModified.getTime())
155: } // fi (!oCached.exists())
156: } catch (Exception xcpt) {
157: DebugFile.writeln(xcpt.getClass().getName() + " "
158: + xcpt.getMessage());
159: }
160: }
161:
162: String sXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet type=\"text/xsl\"?>";
163:
164: int iCalls = 0;
165:
166: if (req.getWindowState().equals(WindowState.MINIMIZED)) {
167: sXML += "<contacts/>";
168: } else {
169:
170: DBBind oDBB = (DBBind) getPortletContext().getAttribute(
171: "GlobalDBBind");
172:
173: JDCConnection oCon = null;
174:
175: try {
176: oCon = oDBB.getConnection("RecentContactsTab");
177:
178: PreparedStatement oStm = oCon
179: .prepareStatement(
180: "(SELECT dt_last_visit,gu_company,NULL AS gu_contact,nm_company,'' AS full_name,work_phone,tx_email FROM k_companies_recent WHERE gu_user=? UNION SELECT dt_last_visit,NULL AS gu_company,gu_contact,nm_company,full_name,work_phone,tx_email FROM k_contacts_recent WHERE gu_user=?) ORDER BY dt_last_visit DESC",
181: ResultSet.TYPE_FORWARD_ONLY,
182: ResultSet.CONCUR_READ_ONLY);
183: oStm.setString(1, sUserId);
184: oStm.setString(2, sUserId);
185: ResultSet oRSet = oStm.executeQuery();
186:
187: int iRecentCount = 0;
188: StringBuffer oXML = new StringBuffer();
189: String sStr;
190:
191: while (oRSet.next() && iRecentCount < iMaxRecent) {
192: oXML.append("<contact>");
193:
194: sStr = oRSet.getString(2);
195: if (oRSet.wasNull())
196: oXML.append("<gu_company/>");
197: else
198: oXML.append("<gu_company>" + oRSet.getString(2)
199: + "</gu_company>");
200:
201: sStr = oRSet.getString(3);
202: if (oRSet.wasNull())
203: oXML.append("<gu_contact/>");
204: else
205: oXML.append("<gu_contact>" + oRSet.getString(3)
206: + "</gu_contact>");
207:
208: sStr = oRSet.getString(4);
209: if (oRSet.wasNull())
210: oXML.append("<nm_company/>");
211: else
212: oXML.append("<nm_company><![CDATA["
213: + oRSet.getString(4)
214: + "]]></nm_company>");
215:
216: sStr = oRSet.getString(5);
217: if (oRSet.wasNull())
218: oXML.append("<full_name/>");
219: else
220: oXML.append("<full_name><![CDATA["
221: + oRSet.getString(5)
222: + "]]></full_name>");
223:
224: sStr = oRSet.getString(6);
225: if (oRSet.wasNull())
226: oXML.append("<work_phone/>");
227: else
228: oXML.append("<work_phone><![CDATA["
229: + oRSet.getString(6)
230: + "]]></work_phone>");
231:
232: sStr = oRSet.getString(7);
233: if (oRSet.wasNull())
234: oXML.append("<tx_email/>");
235: else
236: oXML.append("<tx_email>" + oRSet.getString(7)
237: + "</tx_email>");
238:
239: oXML.append("</contact>");
240:
241: iRecentCount++;
242: } // wend
243:
244: oCon.close("RecentContactsTab");
245: oCon = null;
246:
247: sXML += "<contacts>\n" + oXML.toString()
248: + "</contacts>";
249: } catch (SQLException e) {
250: sXML += "<contacts/>";
251:
252: try {
253: if (null != oCon)
254: if (!oCon.isClosed())
255: oCon.close("RecentContactsTab");
256: } catch (SQLException ignore) {
257: }
258: }
259: }
260:
261: try {
262: if (DebugFile.trace)
263: DebugFile.writeln("new ByteArrayInputStream("
264: + String.valueOf(sXML.length()) + ")");
265:
266: if (sEncoding == null)
267: oInStream = new ByteArrayInputStream(sXML.getBytes());
268: else
269: oInStream = new ByteArrayInputStream(sXML
270: .getBytes(sEncoding));
271:
272: oOutStream = new ByteArrayOutputStream(4000);
273:
274: Properties oProps = new Properties();
275:
276: Enumeration oKeys = req.getPropertyNames();
277: while (oKeys.hasMoreElements()) {
278: String sKey = (String) oKeys.nextElement();
279: oProps.setProperty(sKey, req.getProperty(sKey));
280: } // wend
281:
282: if (req.getWindowState().equals(WindowState.MINIMIZED))
283: oProps.setProperty("windowstate", "MINIMIZED");
284: else
285: oProps.setProperty("windowstate", "NORMAL");
286:
287: StylesheetCache.transform(sTemplatePath, oInStream,
288: oOutStream, oProps);
289:
290: if (sEncoding == null)
291: sOutput = oOutStream.toString();
292: else
293: sOutput = oOutStream.toString("UTF-8");
294:
295: oOutStream.close();
296:
297: oInStream.close();
298: oInStream = null;
299:
300: oFS.writefilestr(sFileDir + File.separator + sCachedFile,
301: sOutput, sEncoding == null ? "ISO8859_1"
302: : sEncoding);
303: } catch (TransformerConfigurationException tce) {
304: if (DebugFile.trace) {
305: DebugFile.writeln("TransformerConfigurationException "
306: + tce.getMessageAndLocation());
307: try {
308: DebugFile
309: .write("--------------------------------------------------------------------------------\n");
310: DebugFile.write(FileSystem.readfile(sTemplatePath));
311: DebugFile
312: .write("\n--------------------------------------------------------------------------------\n");
313: DebugFile.write(sXML);
314: DebugFile
315: .write("\n--------------------------------------------------------------------------------\n");
316: } catch (java.io.IOException ignore) {
317: } catch (com.enterprisedt.net.ftp.FTPException ignore) {
318: }
319:
320: DebugFile.decIdent();
321: }
322: throw new PortletException(
323: "TransformerConfigurationException "
324: + tce.getMessage(), tce);
325: } catch (TransformerException tex) {
326: if (DebugFile.trace) {
327: DebugFile.writeln("TransformerException "
328: + tex.getMessageAndLocation());
329:
330: try {
331: DebugFile
332: .write("--------------------------------------------------------------------------------\n");
333: DebugFile.write(FileSystem.readfile(sTemplatePath));
334: DebugFile
335: .write("\n--------------------------------------------------------------------------------\n");
336: DebugFile.write(sXML);
337: DebugFile
338: .write("\n--------------------------------------------------------------------------------\n");
339: } catch (java.io.IOException ignore) {
340: } catch (com.enterprisedt.net.ftp.FTPException ignore) {
341: }
342:
343: DebugFile.decIdent();
344: }
345: throw new PortletException("TransformerException "
346: + tex.getMessage(), tex);
347: }
348:
349: if (DebugFile.trace) {
350: DebugFile.decIdent();
351: DebugFile.writeln("End RecentContactsTab.render()");
352: }
353: return sOutput;
354: }
355:
356: // --------------------------------------------------------------------------
357:
358: public void render(RenderRequest req, RenderResponse res)
359: throws PortletException, IOException, IllegalStateException {
360: res.getWriter().write(render(req, res.getCharacterEncoding()));
361: }
362: }
|