KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2003 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  * @author Sergio Montoro Ten
64  * @version 2.2
65  */

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

78   public String JavaDoc render(RenderRequest req, String JavaDoc sEncoding)
79     throws PortletException, IOException JavaDoc, IllegalStateException JavaDoc {
80
81     ByteArrayInputStream JavaDoc oInStream;
82     ByteArrayOutputStream JavaDoc oOutStream;
83
84     if (DebugFile.trace) {
85       DebugFile.writeln("Begin RecentContactsTab.render()");
86       DebugFile.incIdent();
87     }
88
89     final int iMaxRecent = 8;
90
91     FileSystem oFS = new FileSystem(FileSystem.OS_PUREJAVA);
92
93     String JavaDoc sOutput;
94     String JavaDoc sDomainId = req.getProperty("domain");
95     String JavaDoc sWorkAreaId = req.getProperty("workarea");
96     String JavaDoc sUserId = req.getProperty("user");
97     String JavaDoc sZone = req.getProperty("zone");
98     String JavaDoc sLang = req.getProperty("language");
99     String JavaDoc sTemplatePath = req.getProperty("template");
100     String JavaDoc sStorage = req.getProperty("storage");
101     String JavaDoc sFileDir = "file://" + sStorage + "domains" + File.separator + sDomainId + File.separator + "workareas" + File.separator + sWorkAreaId + File.separator + "cache" + File.separator + sUserId;
102     String JavaDoc sCachedFile = "recentcontactstab_" + req.getWindowState().toString() + ".xhtm";
103
104     if (DebugFile.trace) {
105       DebugFile.writeln ("user=" + sUserId);
106       DebugFile.writeln ("template=" + sTemplatePath);
107       DebugFile.writeln ("cache dir=" + sFileDir);
108       DebugFile.writeln ("modified=" + req.getAttribute("modified"));
109       DebugFile.writeln ("encoding=" + sEncoding);
110     }
111
112     Date JavaDoc oDtModified = (Date JavaDoc) req.getAttribute("modified");
113
114     if (null!=oDtModified) {
115
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 {
124           if (DebugFile.trace) {
125             DebugFile.writeln ("file modified " + new Date JavaDoc(oCached.lastModified()).toString());
126             DebugFile.writeln ("last modified " + new Date JavaDoc(oDtModified.getTime()).toString());
127           }
128           if (oCached.lastModified()>oDtModified.getTime()) {
129             sOutput = new String JavaDoc(FileSystem.readfile(sFileDir+File.separator+sCachedFile, sEncoding==null ? "ISO8859_1" : sEncoding));
130
131             if (DebugFile.trace) {
132               DebugFile.writeln("cache hit " + sFileDir+File.separator+sCachedFile);
133               DebugFile.decIdent();
134               DebugFile.writeln("End RecentContactsTab.render()");
135             }
136
137             return sOutput;
138           } // fi (oCached.lastModified()>oDtModified.getTime())
139
} // fi (!oCached.exists())
140
}
141       catch (Exception JavaDoc xcpt) {
142         DebugFile.writeln(xcpt.getClass().getName() + " " + xcpt.getMessage());
143       }
144     }
145
146     String JavaDoc sXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet type=\"text/xsl\"?>";
147
148     int iCalls = 0;
149
150     if (req.getWindowState().equals(WindowState.MINIMIZED)) {
151       sXML += "<contacts/>";
152     }
153     else {
154
155       DBBind oDBB = (DBBind) getPortletContext().getAttribute("GlobalDBBind");
156
157       JDCConnection oCon = null;
158
159       try {
160         oCon = oDBB.getConnection("RecentContactsTab");
161
162         PreparedStatement JavaDoc oStm = oCon.prepareStatement("(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", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
163         oStm.setString (1, sUserId);
164         oStm.setString (2, sUserId);
165         ResultSet JavaDoc oRSet = oStm.executeQuery();
166
167         int iRecentCount = 0;
168         StringBuffer JavaDoc oXML = new StringBuffer JavaDoc();
169         String JavaDoc sStr;
170
171         while (oRSet.next() && iRecentCount<iMaxRecent) {
172           oXML.append("<contact>");
173
174           sStr = oRSet.getString(2);
175           if (oRSet.wasNull()) oXML.append("<gu_company/>"); else oXML.append("<gu_company>"+oRSet.getString(2)+"</gu_company>");
176
177           sStr = oRSet.getString(3);
178           if (oRSet.wasNull()) oXML.append("<gu_contact/>"); else oXML.append("<gu_contact>"+oRSet.getString(3)+"</gu_contact>");
179
180           sStr = oRSet.getString(4);
181           if (oRSet.wasNull()) oXML.append("<nm_company/>"); else oXML.append("<nm_company><![CDATA["+oRSet.getString(4)+"]]></nm_company>");
182
183           sStr = oRSet.getString(5);
184           if (oRSet.wasNull()) oXML.append("<full_name/>"); else oXML.append("<full_name><![CDATA["+oRSet.getString(5)+"]]></full_name>");
185
186           sStr = oRSet.getString(6);
187           if (oRSet.wasNull()) oXML.append("<work_phone/>"); else oXML.append("<work_phone><![CDATA["+oRSet.getString(6)+"]]></work_phone>");
188
189           sStr = oRSet.getString(7);
190           if (oRSet.wasNull()) oXML.append("<tx_email/>"); else oXML.append("<tx_email>"+oRSet.getString(7)+"</tx_email>");
191
192           oXML.append("</contact>");
193
194           iRecentCount++;
195         } // wend
196

197         oCon.close("RecentContactsTab");
198         oCon = null;
199
200         sXML += "<contacts>\n"+oXML.toString()+"</contacts>";
201       }
202       catch (SQLException JavaDoc e) {
203         sXML += "<contacts/>";
204
205         try {
206           if (null != oCon)
207             if (!oCon.isClosed())
208               oCon.close("RecentContactsTab");
209         } catch (SQLException JavaDoc ignore) { }
210       }
211     }
212
213     try {
214        if (DebugFile.trace) DebugFile.writeln("new ByteArrayInputStream(" + String.valueOf(sXML.length()) + ")");
215
216        if (sEncoding==null)
217          oInStream = new ByteArrayInputStream JavaDoc(sXML.getBytes());
218        else
219          oInStream = new ByteArrayInputStream JavaDoc(sXML.getBytes(sEncoding));
220
221        oOutStream = new ByteArrayOutputStream JavaDoc(4000);
222
223        Properties JavaDoc oProps = new Properties JavaDoc();
224
225        Enumeration JavaDoc oKeys = req.getPropertyNames();
226        while (oKeys.hasMoreElements()) {
227          String JavaDoc sKey = (String JavaDoc) oKeys.nextElement();
228          oProps.setProperty(sKey, req.getProperty(sKey));
229        } // wend
230

231        if (req.getWindowState().equals(WindowState.MINIMIZED))
232          oProps.setProperty("windowstate", "MINIMIZED");
233        else
234          oProps.setProperty("windowstate", "NORMAL");
235
236        StylesheetCache.transform (sTemplatePath, oInStream, oOutStream, oProps);
237
238        if (sEncoding==null)
239          sOutput = oOutStream.toString();
240        else
241          sOutput = oOutStream.toString("UTF-8");
242
243        oOutStream.close();
244
245        oInStream.close();
246        oInStream = null;
247
248        oFS.writefilestr (sFileDir+File.separator+sCachedFile, sOutput, sEncoding==null ? "ISO8859_1" : sEncoding);
249      }
250      catch (TransformerConfigurationException JavaDoc tce) {
251        if (DebugFile.trace) {
252          DebugFile.writeln("TransformerConfigurationException " + tce.getMessageAndLocation());
253          try {
254            DebugFile.write("--------------------------------------------------------------------------------\n");
255            DebugFile.write(FileSystem.readfile(sTemplatePath));
256            DebugFile.write("\n--------------------------------------------------------------------------------\n");
257            DebugFile.write(sXML);
258            DebugFile.write("\n--------------------------------------------------------------------------------\n");
259          }
260          catch (java.io.IOException JavaDoc ignore) { }
261          catch (com.enterprisedt.net.ftp.FTPException ignore) { }
262
263          DebugFile.decIdent();
264        }
265        throw new PortletException("TransformerConfigurationException " + tce.getMessage(), tce);
266      }
267      catch (TransformerException JavaDoc tex) {
268        if (DebugFile.trace) {
269          DebugFile.writeln("TransformerException " + tex.getMessageAndLocation());
270
271          try {
272            DebugFile.write("--------------------------------------------------------------------------------\n");
273            DebugFile.write(FileSystem.readfile(sTemplatePath));
274            DebugFile.write("\n--------------------------------------------------------------------------------\n");
275            DebugFile.write(sXML);
276            DebugFile.write("\n--------------------------------------------------------------------------------\n");
277          }
278          catch (java.io.IOException JavaDoc ignore) { }
279          catch (com.enterprisedt.net.ftp.FTPException ignore) { }
280
281          DebugFile.decIdent();
282        }
283        throw new PortletException("TransformerException " + tex.getMessage(), tex);
284      }
285
286      if (DebugFile.trace) {
287        DebugFile.decIdent();
288        DebugFile.writeln("End RecentContactsTab.render()");
289      }
290      return sOutput;
291    }
292
293    // --------------------------------------------------------------------------
294

295    public void render(RenderRequest req, RenderResponse res)
296      throws PortletException, IOException JavaDoc, IllegalStateException JavaDoc {
297      res.getWriter().write(render(req,res.getCharacterEncoding()));
298     }
299 }
300
Popular Tags