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: * Phone Calls Tabbed Dialog Portlet
065: * @author Sergio Montoro Ten
066: * @version 2.2
067: */
068:
069: public class CallsTab extends GenericPortlet {
070: public CallsTab() {
071: }
072:
073: public CallsTab(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 CallsTab.render()");
089: DebugFile.incIdent();
090: }
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 = "callstab_"
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: try {
122:
123: File oCached = new File(sFileDir.substring(7)
124: + File.separator + sCachedFile);
125:
126: if (!oCached.exists()) {
127: oFS.mkdirs(sFileDir);
128: } else {
129: if (DebugFile.trace) {
130: DebugFile.writeln("file modified "
131: + new Date(oCached.lastModified())
132: .toString());
133: DebugFile.writeln("last modified "
134: + new Date(oDtModified.getTime())
135: .toString());
136: }
137: if (oCached.lastModified() > oDtModified.getTime()) {
138: sOutput = new String(FileSystem
139: .readfile(sFileDir + File.separator
140: + sCachedFile,
141: sEncoding == null ? "ISO8859_1"
142: : sEncoding));
143:
144: if (DebugFile.trace) {
145: DebugFile.writeln("cache hit " + sFileDir
146: + File.separator + sCachedFile);
147: DebugFile.decIdent();
148: DebugFile.writeln("End CallsTab.render()");
149: }
150:
151: return sOutput;
152: }
153: }
154: } catch (Exception xcpt) {
155: DebugFile.writeln(xcpt.getClass().getName() + " "
156: + xcpt.getMessage());
157: }
158: }
159:
160: String sXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet type=\"text/xsl\"?>";
161:
162: int iCalls = 0;
163:
164: if (req.getWindowState().equals(WindowState.MINIMIZED)) {
165: sXML += "<calls/>";
166: } else {
167:
168: DBBind oDBB = (DBBind) getPortletContext().getAttribute(
169: "GlobalDBBind");
170:
171: DBSubset oCalls = new DBSubset(DB.k_phone_calls,
172: DB.gu_phonecall + "," + DB.tp_phonecall + ","
173: + DB.dt_start + "," + DB.dt_end + ","
174: + DB.gu_contact + "," + DB.contact_person
175: + "," + DB.tx_phone + "," + DB.tx_comments,
176: DB.gu_workarea + "=? AND " + DB.gu_user + "=? AND "
177: + DB.id_status + "=0 ORDER BY 3 DESC", 10);
178:
179: JDCConnection oCon = null;
180:
181: try {
182: oCon = oDBB.getConnection("CallsTab");
183:
184: iCalls = oCalls.load(oCon, new Object[] { sWorkAreaId,
185: sUserId });
186:
187: for (int c = 0; c < iCalls; c++) {
188: if (oCalls.isNull(2, c))
189: oCalls.setElementAt("", 2, c);
190: else {
191: Date dtStart = oCalls.getDate(2, c);
192:
193: oCalls.setElementAt(Gadgets.leftPad(String
194: .valueOf(dtStart.getHours()), '0', 2)
195: + ":"
196: + Gadgets.leftPad(String
197: .valueOf(dtStart.getMinutes()),
198: '0', 2), 2, c);
199: }
200: } // next (c)
201:
202: oCon.close("CallsTab");
203: oCon = null;
204:
205: sXML += "<calls>\n" + oCalls.toXML("", "call")
206: + "</calls>";
207: } catch (SQLException e) {
208: sXML += "<calls/>";
209:
210: try {
211: if (null != oCon)
212: if (!oCon.isClosed())
213: oCon.close("CallsTab");
214: } catch (SQLException ignore) {
215: }
216: }
217: }
218:
219: try {
220: if (DebugFile.trace)
221: DebugFile.writeln("new ByteArrayInputStream("
222: + String.valueOf(sXML.length()) + ")");
223:
224: if (sEncoding == null)
225: oInStream = new ByteArrayInputStream(sXML.getBytes());
226: else
227: oInStream = new ByteArrayInputStream(sXML
228: .getBytes(sEncoding));
229:
230: oOutStream = new ByteArrayOutputStream(4000);
231:
232: Properties oProps = new Properties();
233:
234: Enumeration oKeys = req.getPropertyNames();
235: while (oKeys.hasMoreElements()) {
236: String sKey = (String) oKeys.nextElement();
237: oProps.setProperty(sKey, req.getProperty(sKey));
238: } // wend
239:
240: if (req.getWindowState().equals(WindowState.MINIMIZED))
241: oProps.setProperty("windowstate", "MINIMIZED");
242: else
243: oProps.setProperty("windowstate", "NORMAL");
244:
245: StylesheetCache.transform(sTemplatePath, oInStream,
246: oOutStream, oProps);
247:
248: if (sEncoding == null)
249: sOutput = oOutStream.toString();
250: else
251: sOutput = oOutStream.toString("UTF-8");
252:
253: oOutStream.close();
254:
255: oInStream.close();
256: oInStream = null;
257:
258: oFS.writefilestr(sFileDir + File.separator + sCachedFile,
259: sOutput, sEncoding == null ? "ISO8859_1"
260: : sEncoding);
261: } catch (TransformerConfigurationException tce) {
262: if (DebugFile.trace) {
263: DebugFile.writeln("TransformerConfigurationException "
264: + tce.getMessageAndLocation());
265: try {
266: DebugFile
267: .write("--------------------------------------------------------------------------------\n");
268: DebugFile.write(FileSystem.readfile(sTemplatePath));
269: DebugFile
270: .write("\n--------------------------------------------------------------------------------\n");
271: DebugFile.write(sXML);
272: DebugFile
273: .write("\n--------------------------------------------------------------------------------\n");
274: } catch (java.io.IOException ignore) {
275: } catch (com.enterprisedt.net.ftp.FTPException ignore) {
276: }
277:
278: DebugFile.decIdent();
279: }
280: throw new PortletException(
281: "TransformerConfigurationException "
282: + tce.getMessage(), tce);
283: } catch (TransformerException tex) {
284: if (DebugFile.trace) {
285: DebugFile.writeln("TransformerException "
286: + tex.getMessageAndLocation());
287:
288: try {
289: DebugFile
290: .write("--------------------------------------------------------------------------------\n");
291: DebugFile.write(FileSystem.readfile(sTemplatePath));
292: DebugFile
293: .write("\n--------------------------------------------------------------------------------\n");
294: DebugFile.write(sXML);
295: DebugFile
296: .write("\n--------------------------------------------------------------------------------\n");
297: } catch (java.io.IOException ignore) {
298: } catch (com.enterprisedt.net.ftp.FTPException ignore) {
299: }
300:
301: DebugFile.decIdent();
302: }
303: throw new PortletException("TransformerException "
304: + tex.getMessage(), tex);
305: }
306:
307: if (DebugFile.trace) {
308: DebugFile.decIdent();
309: DebugFile.writeln("End CallsTab.render()");
310: }
311: return sOutput;
312: }
313:
314: // --------------------------------------------------------------------------
315:
316: public void render(RenderRequest req, RenderResponse res)
317: throws PortletException, IOException, IllegalStateException {
318: res.getWriter().write(render(req, res.getCharacterEncoding()));
319: }
320:
321: }
|