KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

79   public String JavaDoc render(RenderRequest req, String JavaDoc sEncoding)
80     throws PortletException, IOException JavaDoc, IllegalStateException JavaDoc {
81
82     ByteArrayInputStream JavaDoc oInStream;
83     ByteArrayOutputStream JavaDoc oOutStream;
84
85     if (DebugFile.trace) {
86       DebugFile.writeln("Begin OportunitiesTab.render()");
87       DebugFile.incIdent();
88     }
89
90     final int iMaxRecent = 8;
91
92     FileSystem oFS = new FileSystem(FileSystem.OS_PUREJAVA);
93
94     String JavaDoc sOutput;
95     String JavaDoc sDomainId = req.getProperty("domain");
96     String JavaDoc sWorkAreaId = req.getProperty("workarea");
97     String JavaDoc sUserId = req.getProperty("user");
98     String JavaDoc sZone = req.getProperty("zone");
99     String JavaDoc sLang = req.getProperty("language");
100     String JavaDoc sTemplatePath = req.getProperty("template");
101     String JavaDoc sStorage = req.getProperty("storage");
102     String JavaDoc sFileDir = "file://" + sStorage + "domains" + File.separator + sDomainId + File.separator + "workareas" + File.separator + sWorkAreaId + File.separator + "cache" + File.separator + sUserId;
103     String JavaDoc sCachedFile = "oportunitiestab_" + req.getWindowState().toString() + ".xhtm";
104
105     if (DebugFile.trace) {
106       DebugFile.writeln ("user=" + sUserId);
107       DebugFile.writeln ("template=" + sTemplatePath);
108       DebugFile.writeln ("cache dir=" + sFileDir);
109       DebugFile.writeln ("modified=" + req.getAttribute("modified"));
110       DebugFile.writeln ("encoding=" + sEncoding);
111     }
112
113     Date JavaDoc oDtModified = (Date JavaDoc) req.getAttribute("modified");
114
115     if (null!=oDtModified) {
116       try {
117
118         File JavaDoc oCached = new File JavaDoc(sFileDir.substring(7)+File.separator+sCachedFile);
119
120         if (!oCached.exists()) {
121           oFS.mkdirs(sFileDir);
122         }
123         else if (oCached.lastModified()>oDtModified.getTime()) {
124           sOutput = new String JavaDoc(oFS.readfile(sFileDir+File.separator+sCachedFile, sEncoding==null ? "ISO8859_1" : sEncoding));
125
126           if (DebugFile.trace) {
127             DebugFile.writeln("cache hit " + sFileDir+File.separator+sCachedFile);
128             DebugFile.decIdent();
129             DebugFile.writeln("End OportunitiesTab.render()");
130           }
131
132           return sOutput;
133         }
134       }
135       catch (Exception JavaDoc xcpt) {
136         DebugFile.writeln(xcpt.getClass().getName() + " " + xcpt.getMessage());
137       }
138     }
139
140     String JavaDoc sXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet type=\"text/xsl\"?>";
141
142     int iCalls = 0;
143
144     if (req.getWindowState().equals(WindowState.MINIMIZED)) {
145       sXML += "<oportunities/>";
146     }
147     else {
148
149       DBBind oDBB = (DBBind) getPortletContext().getAttribute("GlobalDBBind");
150
151       JDCConnection oCon = null;
152       PreparedStatement JavaDoc oStm = null;
153       String JavaDoc sSQL, sInterval;
154
155       try {
156         int iOprtnCount = 0;
157         StringBuffer JavaDoc oXML = new StringBuffer JavaDoc();
158         String JavaDoc[][] aOprtns = new String JavaDoc[6][iMaxRecent];
159
160         oCon = oDBB.getConnection("OportunitiesTab");
161
162         if (oCon.getDataBaseProduct()==JDCConnection.DBMS_POSTGRESQL)
163           sInterval = "interval '10 years'";
164         else
165           sInterval = "3650";
166
167         sSQL = "(SELECT "+
168                       DB.gu_oportunity+","+DB.tl_oportunity+","+DB.gu_company+","+DB.gu_contact+","+DB.tx_company+","+DB.tx_contact+","+DB.dt_modified+","+DB.dt_next_action+","+
169                       DBBind.Functions.GETDATE+"-"+DB.dt_modified+ " AS nu_elapsed FROM "+DB.k_oportunities+" WHERE "+DB.id_status+" NOT IN ('PERDIDA','GANADA','ABANDONADA') AND "+
170                       DB.dt_modified+" IS NOT NULL AND " + DB.gu_workarea+"=? AND "+DB.gu_writer+"=? UNION SELECT "+
171                       DB.gu_oportunity+","+DB.tl_oportunity+","+DB.gu_company+","+DB.gu_contact+","+DB.tx_company+","+DB.tx_contact+","+DB.dt_modified+","+DB.dt_next_action+","+
172                       DBBind.Functions.ISNULL+"("+DB.dt_next_action+","+DBBind.Functions.GETDATE+"+" + sInterval + ")-"+DBBind.Functions.GETDATE+" AS nu_elapsed "+
173                       "FROM "+DB.k_oportunities+" WHERE "+DB.id_status+" NOT IN ('PERDIDA','GANADA','ABANDONADA') AND "+
174                       DB.gu_workarea+"=? AND "+DB.gu_writer+"=?) ORDER BY nu_elapsed";
175
176         if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement("+sSQL+")");
177
178         oStm = oCon.prepareStatement(sSQL,ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
179
180         oStm.setString (1,sWorkAreaId);
181         oStm.setString (2,sUserId);
182         oStm.setString (3,sWorkAreaId);
183         oStm.setString (4,sUserId);
184
185         if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeQuery()");
186
187         ResultSet JavaDoc oRSet = oStm.executeQuery();
188
189         while (oRSet.next() && iOprtnCount<iMaxRecent) {
190
191           boolean bListed = false;
192           for (int n=0; n<iOprtnCount && !bListed; n++)
193             bListed = oRSet.getString(1).equals(aOprtns[0][n]);
194
195           if (!bListed) {
196             aOprtns[0][iOprtnCount] = oRSet.getString(1);
197             aOprtns[1][iOprtnCount] = oRSet.getString(2);
198             aOprtns[2][iOprtnCount] = oRSet.getString(3);
199             aOprtns[3][iOprtnCount] = oRSet.getString(4);
200             aOprtns[4][iOprtnCount] = oRSet.getString(5);
201             aOprtns[5][iOprtnCount] = oRSet.getString(6);
202
203             iOprtnCount++;
204           }
205         } // wend
206

207         oRSet.close();
208         oRSet = null;
209
210         oStm.close();
211         oStm = null;
212
213         oCon.close("OportunitiesTab");
214         oCon = null;
215
216         for (int o=0; o<iOprtnCount; o++)
217           for (int f=0; f<6; f++)
218             if (aOprtns[f][o]==null) aOprtns[f][o]="";
219
220         oXML.append("<oportunities>\n");
221         for (int q=0; q<iOprtnCount; q++) {
222           oXML.append("<oportunity>\n");
223
224           oXML.append("<gu_oportunity>"+aOprtns[0][q]+"</gu_oportunity>");
225           oXML.append("<tl_oportunity><![CDATA["+aOprtns[1][q]+"]]></tl_oportunity>");
226           oXML.append("<gu_company>"+aOprtns[2][q]+"</gu_company>");
227           oXML.append("<gu_contact>"+aOprtns[3][q]+"</gu_contact>");
228           oXML.append("<tx_company><![CDATA["+aOprtns[4][q]+"]]></tx_company>");
229           oXML.append("<tx_contact><![CDATA["+aOprtns[5][q]+"]]></tx_contact>");
230           oXML.append("<tx_contact_esc><![CDATA["+Gadgets.URLEncode(aOprtns[5][q])+"]]></tx_contact_esc>");
231           oXML.append("<where><![CDATA["+Gadgets.URLEncode(" AND gu_contact='" + aOprtns[3][q] + "'")+"]]></where>");
232
233           oXML.append("</oportunity>\n");
234         }
235
236         oXML.append("</oportunities>");
237
238         sXML += oXML.toString();
239       }
240       catch (SQLException JavaDoc e) {
241         sXML += "<oportunities/>";
242
243         if (DebugFile.trace) DebugFile.writeln("SQLException " + e.getMessage());
244         try {
245           if (null != oStm)
246               oStm.close();
247         } catch (SQLException JavaDoc ignore) { }
248
249         try {
250           if (null != oCon)
251             if (!oCon.isClosed())
252               oCon.close("OportunitiesTab");
253         } catch (SQLException JavaDoc ignore) { }
254       }
255     }
256
257     try {
258        if (DebugFile.trace) DebugFile.writeln("new ByteArrayInputStream(" + String.valueOf(sXML.length()) + ")");
259
260        if (sEncoding==null)
261          oInStream = new ByteArrayInputStream JavaDoc(sXML.getBytes());
262        else
263          oInStream = new ByteArrayInputStream JavaDoc(sXML.getBytes(sEncoding));
264
265        oOutStream = new ByteArrayOutputStream JavaDoc(4000);
266
267        Properties JavaDoc oProps = new Properties JavaDoc();
268
269        Enumeration JavaDoc oKeys = req.getPropertyNames();
270        while (oKeys.hasMoreElements()) {
271          String JavaDoc sKey = (String JavaDoc) oKeys.nextElement();
272          oProps.setProperty(sKey, req.getProperty(sKey));
273        } // wend
274

275        if (req.getWindowState().equals(WindowState.MINIMIZED))
276          oProps.setProperty("windowstate", "MINIMIZED");
277        else
278          oProps.setProperty("windowstate", "NORMAL");
279
280        StylesheetCache.transform (sTemplatePath, oInStream, oOutStream, oProps);
281
282        if (sEncoding==null)
283          sOutput = oOutStream.toString();
284        else
285          sOutput = oOutStream.toString("UTF-8");
286
287        oOutStream.close();
288
289        oInStream.close();
290        oInStream = null;
291
292        oFS.writefilestr (sFileDir+File.separator+sCachedFile, sOutput, sEncoding==null ? "ISO8859_1" : sEncoding);
293      }
294      catch (TransformerConfigurationException JavaDoc tce) {
295        if (DebugFile.trace) {
296          DebugFile.writeln("TransformerConfigurationException " + tce.getMessageAndLocation());
297          try {
298            DebugFile.write("--------------------------------------------------------------------------------\n");
299            DebugFile.write(FileSystem.readfile(sTemplatePath));
300            DebugFile.write("\n--------------------------------------------------------------------------------\n");
301            DebugFile.write(sXML);
302            DebugFile.write("\n--------------------------------------------------------------------------------\n");
303          }
304          catch (java.io.IOException JavaDoc ignore) { }
305          catch (com.enterprisedt.net.ftp.FTPException ignore) { }
306
307          DebugFile.decIdent();
308        }
309        throw new PortletException("TransformerConfigurationException " + tce.getMessage(), tce);
310      }
311      catch (TransformerException JavaDoc tex) {
312        if (DebugFile.trace) {
313          DebugFile.writeln("TransformerException " + tex.getMessageAndLocation());
314
315          try {
316            DebugFile.write("--------------------------------------------------------------------------------\n");
317            DebugFile.write(FileSystem.readfile(sTemplatePath));
318            DebugFile.write("\n--------------------------------------------------------------------------------\n");
319            DebugFile.write(sXML);
320            DebugFile.write("\n--------------------------------------------------------------------------------\n");
321          }
322          catch (java.io.IOException JavaDoc ignore) { }
323          catch (com.enterprisedt.net.ftp.FTPException ignore) { }
324
325          DebugFile.decIdent();
326        }
327        throw new PortletException("TransformerException " + tex.getMessage(), tex);
328      }
329
330      if (DebugFile.trace) {
331        DebugFile.decIdent();
332        DebugFile.writeln("End OportunitiesTab.render()");
333      }
334      return sOutput;
335    }
336
337    // --------------------------------------------------------------------------
338

339    public void render(RenderRequest req, RenderResponse res)
340      throws PortletException, IOException JavaDoc, IllegalStateException JavaDoc {
341      res.getWriter().write(render(req,res.getCharacterEncoding()));
342     }
343 }
344
Popular Tags