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: 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: * Open Oportunities Tabbed Dialog Portlet
064: * @author Sergio Montoro Ten
065: * @version 2.2
066: */
067:
068: public class OportunitiesTab extends GenericPortlet {
069: public OportunitiesTab() {
070: }
071:
072: public OportunitiesTab(HipergatePortletConfig oConfig)
073: throws javax.portlet.PortletException {
074:
075: init(oConfig);
076: }
077:
078: // ---------------------------------------------------------------------------
079:
080: public String render(RenderRequest req, String sEncoding)
081: throws PortletException, IOException, IllegalStateException {
082:
083: ByteArrayInputStream oInStream;
084: ByteArrayOutputStream oOutStream;
085:
086: if (DebugFile.trace) {
087: DebugFile.writeln("Begin OportunitiesTab.render()");
088: DebugFile.incIdent();
089: }
090:
091: final int iMaxRecent = 8;
092:
093: FileSystem oFS = new FileSystem(FileSystem.OS_PUREJAVA);
094:
095: String sOutput;
096: String sDomainId = req.getProperty("domain");
097: String sWorkAreaId = req.getProperty("workarea");
098: String sUserId = req.getProperty("user");
099: String sZone = req.getProperty("zone");
100: String sLang = req.getProperty("language");
101: String sTemplatePath = req.getProperty("template");
102: String sStorage = req.getProperty("storage");
103: String sFileDir = "file://" + sStorage + "domains"
104: + File.separator + sDomainId + File.separator
105: + "workareas" + File.separator + sWorkAreaId
106: + File.separator + "cache" + File.separator + sUserId;
107: String sCachedFile = "oportunitiestab_"
108: + req.getWindowState().toString() + ".xhtm";
109:
110: if (DebugFile.trace) {
111: DebugFile.writeln("user=" + sUserId);
112: DebugFile.writeln("template=" + sTemplatePath);
113: DebugFile.writeln("cache dir=" + sFileDir);
114: DebugFile.writeln("modified="
115: + req.getAttribute("modified"));
116: DebugFile.writeln("encoding=" + sEncoding);
117: }
118:
119: Date oDtModified = (Date) req.getAttribute("modified");
120:
121: if (null != oDtModified) {
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 if (oCached.lastModified() > oDtModified
130: .getTime()) {
131: sOutput = new String(oFS
132: .readfile(sFileDir + File.separator
133: + sCachedFile,
134: sEncoding == null ? "ISO8859_1"
135: : sEncoding));
136:
137: if (DebugFile.trace) {
138: DebugFile.writeln("cache hit " + sFileDir
139: + File.separator + sCachedFile);
140: DebugFile.decIdent();
141: DebugFile
142: .writeln("End OportunitiesTab.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 iCalls = 0;
156:
157: if (req.getWindowState().equals(WindowState.MINIMIZED)) {
158: sXML += "<oportunities/>";
159: } else {
160:
161: DBBind oDBB = (DBBind) getPortletContext().getAttribute(
162: "GlobalDBBind");
163:
164: JDCConnection oCon = null;
165: PreparedStatement oStm = null;
166: String sSQL, sInterval;
167:
168: try {
169: int iOprtnCount = 0;
170: StringBuffer oXML = new StringBuffer();
171: String[][] aOprtns = new String[6][iMaxRecent];
172:
173: oCon = oDBB.getConnection("OportunitiesTab");
174:
175: if (oCon.getDataBaseProduct() == JDCConnection.DBMS_POSTGRESQL)
176: sInterval = "interval '10 years'";
177: else
178: sInterval = "3650";
179:
180: sSQL = "(SELECT "
181: + DB.gu_oportunity
182: + ","
183: + DB.tl_oportunity
184: + ","
185: + DB.gu_company
186: + ","
187: + DB.gu_contact
188: + ","
189: + DB.tx_company
190: + ","
191: + DB.tx_contact
192: + ","
193: + DB.dt_modified
194: + ","
195: + DB.dt_next_action
196: + ","
197: + DBBind.Functions.GETDATE
198: + "-"
199: + DB.dt_modified
200: + " AS nu_elapsed FROM "
201: + DB.k_oportunities
202: + " WHERE "
203: + DB.id_status
204: + " NOT IN ('PERDIDA','GANADA','ABANDONADA') AND "
205: + DB.dt_modified
206: + " IS NOT NULL AND "
207: + DB.gu_workarea
208: + "=? AND "
209: + DB.gu_writer
210: + "=? UNION SELECT "
211: + DB.gu_oportunity
212: + ","
213: + DB.tl_oportunity
214: + ","
215: + DB.gu_company
216: + ","
217: + DB.gu_contact
218: + ","
219: + DB.tx_company
220: + ","
221: + DB.tx_contact
222: + ","
223: + DB.dt_modified
224: + ","
225: + DB.dt_next_action
226: + ","
227: + DBBind.Functions.ISNULL
228: + "("
229: + DB.dt_next_action
230: + ","
231: + DBBind.Functions.GETDATE
232: + "+"
233: + sInterval
234: + ")-"
235: + DBBind.Functions.GETDATE
236: + " AS nu_elapsed "
237: + "FROM "
238: + DB.k_oportunities
239: + " WHERE "
240: + DB.id_status
241: + " NOT IN ('PERDIDA','GANADA','ABANDONADA') AND "
242: + DB.gu_workarea + "=? AND " + DB.gu_writer
243: + "=?) ORDER BY nu_elapsed";
244:
245: if (DebugFile.trace)
246: DebugFile.writeln("Connection.prepareStatement("
247: + sSQL + ")");
248:
249: oStm = oCon.prepareStatement(sSQL,
250: ResultSet.TYPE_FORWARD_ONLY,
251: ResultSet.CONCUR_READ_ONLY);
252:
253: oStm.setString(1, sWorkAreaId);
254: oStm.setString(2, sUserId);
255: oStm.setString(3, sWorkAreaId);
256: oStm.setString(4, sUserId);
257:
258: if (DebugFile.trace)
259: DebugFile
260: .writeln("PreparedStatement.executeQuery()");
261:
262: ResultSet oRSet = oStm.executeQuery();
263:
264: while (oRSet.next() && iOprtnCount < iMaxRecent) {
265:
266: boolean bListed = false;
267: for (int n = 0; n < iOprtnCount && !bListed; n++)
268: bListed = oRSet.getString(1).equals(
269: aOprtns[0][n]);
270:
271: if (!bListed) {
272: aOprtns[0][iOprtnCount] = oRSet.getString(1);
273: aOprtns[1][iOprtnCount] = oRSet.getString(2);
274: aOprtns[2][iOprtnCount] = oRSet.getString(3);
275: aOprtns[3][iOprtnCount] = oRSet.getString(4);
276: aOprtns[4][iOprtnCount] = oRSet.getString(5);
277: aOprtns[5][iOprtnCount] = oRSet.getString(6);
278:
279: iOprtnCount++;
280: }
281: } // wend
282:
283: oRSet.close();
284: oRSet = null;
285:
286: oStm.close();
287: oStm = null;
288:
289: oCon.close("OportunitiesTab");
290: oCon = null;
291:
292: for (int o = 0; o < iOprtnCount; o++)
293: for (int f = 0; f < 6; f++)
294: if (aOprtns[f][o] == null)
295: aOprtns[f][o] = "";
296:
297: oXML.append("<oportunities>\n");
298: for (int q = 0; q < iOprtnCount; q++) {
299: oXML.append("<oportunity>\n");
300:
301: oXML.append("<gu_oportunity>" + aOprtns[0][q]
302: + "</gu_oportunity>");
303: oXML.append("<tl_oportunity><![CDATA["
304: + aOprtns[1][q] + "]]></tl_oportunity>");
305: oXML.append("<gu_company>" + aOprtns[2][q]
306: + "</gu_company>");
307: oXML.append("<gu_contact>" + aOprtns[3][q]
308: + "</gu_contact>");
309: oXML.append("<tx_company><![CDATA[" + aOprtns[4][q]
310: + "]]></tx_company>");
311: oXML.append("<tx_contact><![CDATA[" + aOprtns[5][q]
312: + "]]></tx_contact>");
313: oXML.append("<tx_contact_esc><![CDATA["
314: + Gadgets.URLEncode(aOprtns[5][q])
315: + "]]></tx_contact_esc>");
316: oXML.append("<where><![CDATA["
317: + Gadgets.URLEncode(" AND gu_contact='"
318: + aOprtns[3][q] + "'")
319: + "]]></where>");
320:
321: oXML.append("</oportunity>\n");
322: }
323:
324: oXML.append("</oportunities>");
325:
326: sXML += oXML.toString();
327: } catch (SQLException e) {
328: sXML += "<oportunities/>";
329:
330: if (DebugFile.trace)
331: DebugFile.writeln("SQLException " + e.getMessage());
332: try {
333: if (null != oStm)
334: oStm.close();
335: } catch (SQLException ignore) {
336: }
337:
338: try {
339: if (null != oCon)
340: if (!oCon.isClosed())
341: oCon.close("OportunitiesTab");
342: } catch (SQLException ignore) {
343: }
344: }
345: }
346:
347: try {
348: if (DebugFile.trace)
349: DebugFile.writeln("new ByteArrayInputStream("
350: + String.valueOf(sXML.length()) + ")");
351:
352: if (sEncoding == null)
353: oInStream = new ByteArrayInputStream(sXML.getBytes());
354: else
355: oInStream = new ByteArrayInputStream(sXML
356: .getBytes(sEncoding));
357:
358: oOutStream = new ByteArrayOutputStream(4000);
359:
360: Properties oProps = new Properties();
361:
362: Enumeration oKeys = req.getPropertyNames();
363: while (oKeys.hasMoreElements()) {
364: String sKey = (String) oKeys.nextElement();
365: oProps.setProperty(sKey, req.getProperty(sKey));
366: } // wend
367:
368: if (req.getWindowState().equals(WindowState.MINIMIZED))
369: oProps.setProperty("windowstate", "MINIMIZED");
370: else
371: oProps.setProperty("windowstate", "NORMAL");
372:
373: StylesheetCache.transform(sTemplatePath, oInStream,
374: oOutStream, oProps);
375:
376: if (sEncoding == null)
377: sOutput = oOutStream.toString();
378: else
379: sOutput = oOutStream.toString("UTF-8");
380:
381: oOutStream.close();
382:
383: oInStream.close();
384: oInStream = null;
385:
386: oFS.writefilestr(sFileDir + File.separator + sCachedFile,
387: sOutput, sEncoding == null ? "ISO8859_1"
388: : sEncoding);
389: } catch (TransformerConfigurationException tce) {
390: if (DebugFile.trace) {
391: DebugFile.writeln("TransformerConfigurationException "
392: + tce.getMessageAndLocation());
393: try {
394: DebugFile
395: .write("--------------------------------------------------------------------------------\n");
396: DebugFile.write(FileSystem.readfile(sTemplatePath));
397: DebugFile
398: .write("\n--------------------------------------------------------------------------------\n");
399: DebugFile.write(sXML);
400: DebugFile
401: .write("\n--------------------------------------------------------------------------------\n");
402: } catch (java.io.IOException ignore) {
403: } catch (com.enterprisedt.net.ftp.FTPException ignore) {
404: }
405:
406: DebugFile.decIdent();
407: }
408: throw new PortletException(
409: "TransformerConfigurationException "
410: + tce.getMessage(), tce);
411: } catch (TransformerException tex) {
412: if (DebugFile.trace) {
413: DebugFile.writeln("TransformerException "
414: + tex.getMessageAndLocation());
415:
416: try {
417: DebugFile
418: .write("--------------------------------------------------------------------------------\n");
419: DebugFile.write(FileSystem.readfile(sTemplatePath));
420: DebugFile
421: .write("\n--------------------------------------------------------------------------------\n");
422: DebugFile.write(sXML);
423: DebugFile
424: .write("\n--------------------------------------------------------------------------------\n");
425: } catch (java.io.IOException ignore) {
426: } catch (com.enterprisedt.net.ftp.FTPException ignore) {
427: }
428:
429: DebugFile.decIdent();
430: }
431: throw new PortletException("TransformerException "
432: + tex.getMessage(), tex);
433: }
434:
435: if (DebugFile.trace) {
436: DebugFile.decIdent();
437: DebugFile.writeln("End OportunitiesTab.render()");
438: }
439: return sOutput;
440: }
441:
442: // --------------------------------------------------------------------------
443:
444: public void render(RenderRequest req, RenderResponse res)
445: throws PortletException, IOException, IllegalStateException {
446: res.getWriter().write(render(req, res.getCharacterEncoding()));
447: }
448: }
|