KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

81   public String JavaDoc render(RenderRequest req, String JavaDoc sEncoding)
82     throws PortletException, IOException JavaDoc, IllegalStateException JavaDoc {
83
84     ByteArrayInputStream JavaDoc oInStream;
85     ByteArrayOutputStream JavaDoc oOutStream;
86
87     if (DebugFile.trace) {
88       DebugFile.writeln("Begin CallsTab.render()");
89       DebugFile.incIdent();
90     }
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 = "callstab_" + 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 {
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 CallsTab.render()");
135             }
136
137           return sOutput;
138         }
139         }
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 += "<calls/>";
152     }
153     else {
154
155       DBBind oDBB = (DBBind) getPortletContext().getAttribute("GlobalDBBind");
156
157       DBSubset oCalls = new DBSubset (DB.k_phone_calls, DB.gu_phonecall + "," + DB.tp_phonecall + "," + DB.dt_start + "," + DB.dt_end + "," + DB.gu_contact + "," + DB.contact_person + "," + DB.tx_phone + "," + DB.tx_comments,
158                                       DB.gu_workarea + "=? AND " + DB.gu_user + "=? AND " + DB.id_status + "=0 ORDER BY 3 DESC", 10);
159
160       JDCConnection oCon = null;
161
162       try {
163         oCon = oDBB.getConnection("CallsTab");
164
165         iCalls = oCalls.load (oCon, new Object JavaDoc[]{sWorkAreaId,sUserId});
166
167         for (int c=0; c<iCalls; c++) {
168           if (oCalls.isNull(2,c))
169             oCalls.setElementAt("",2,c);
170           else {
171             Date JavaDoc dtStart = oCalls.getDate(2,c);
172
173             oCalls.setElementAt(Gadgets.leftPad(String.valueOf(dtStart.getHours()), '0', 2) + ":" + Gadgets.leftPad(String.valueOf(dtStart.getMinutes()), '0', 2), 2,c);
174           }
175         } // next (c)
176

177         oCon.close("CallsTab");
178         oCon = null;
179
180         sXML += "<calls>\n"+oCalls.toXML("","call")+"</calls>";
181       }
182       catch (SQLException JavaDoc e) {
183         sXML += "<calls/>";
184
185         try {
186           if (null != oCon)
187             if (!oCon.isClosed())
188               oCon.close("CallsTab");
189         } catch (SQLException JavaDoc ignore) { }
190       }
191     }
192
193     try {
194        if (DebugFile.trace) DebugFile.writeln("new ByteArrayInputStream(" + String.valueOf(sXML.length()) + ")");
195
196        if (sEncoding==null)
197          oInStream = new ByteArrayInputStream JavaDoc(sXML.getBytes());
198        else
199          oInStream = new ByteArrayInputStream JavaDoc(sXML.getBytes(sEncoding));
200
201        oOutStream = new ByteArrayOutputStream JavaDoc(4000);
202
203        Properties JavaDoc oProps = new Properties JavaDoc();
204
205        Enumeration JavaDoc oKeys = req.getPropertyNames();
206        while (oKeys.hasMoreElements()) {
207          String JavaDoc sKey = (String JavaDoc) oKeys.nextElement();
208          oProps.setProperty(sKey, req.getProperty(sKey));
209        } // wend
210

211        if (req.getWindowState().equals(WindowState.MINIMIZED))
212          oProps.setProperty("windowstate", "MINIMIZED");
213        else
214          oProps.setProperty("windowstate", "NORMAL");
215
216        StylesheetCache.transform (sTemplatePath, oInStream, oOutStream, oProps);
217
218        if (sEncoding==null)
219          sOutput = oOutStream.toString();
220        else
221          sOutput = oOutStream.toString("UTF-8");
222
223        oOutStream.close();
224
225        oInStream.close();
226        oInStream = null;
227
228        oFS.writefilestr (sFileDir+File.separator+sCachedFile, sOutput, sEncoding==null ? "ISO8859_1" : sEncoding);
229      }
230      catch (TransformerConfigurationException JavaDoc tce) {
231        if (DebugFile.trace) {
232          DebugFile.writeln("TransformerConfigurationException " + tce.getMessageAndLocation());
233          try {
234            DebugFile.write("--------------------------------------------------------------------------------\n");
235            DebugFile.write(FileSystem.readfile(sTemplatePath));
236            DebugFile.write("\n--------------------------------------------------------------------------------\n");
237            DebugFile.write(sXML);
238            DebugFile.write("\n--------------------------------------------------------------------------------\n");
239          }
240          catch (java.io.IOException JavaDoc ignore) { }
241          catch (com.enterprisedt.net.ftp.FTPException ignore) { }
242
243          DebugFile.decIdent();
244        }
245        throw new PortletException("TransformerConfigurationException " + tce.getMessage(), tce);
246      }
247      catch (TransformerException JavaDoc tex) {
248        if (DebugFile.trace) {
249          DebugFile.writeln("TransformerException " + tex.getMessageAndLocation());
250
251          try {
252            DebugFile.write("--------------------------------------------------------------------------------\n");
253            DebugFile.write(FileSystem.readfile(sTemplatePath));
254            DebugFile.write("\n--------------------------------------------------------------------------------\n");
255            DebugFile.write(sXML);
256            DebugFile.write("\n--------------------------------------------------------------------------------\n");
257          }
258          catch (java.io.IOException JavaDoc ignore) { }
259          catch (com.enterprisedt.net.ftp.FTPException ignore) { }
260
261          DebugFile.decIdent();
262        }
263        throw new PortletException("TransformerException " + tex.getMessage(), tex);
264      }
265
266      if (DebugFile.trace) {
267        DebugFile.decIdent();
268        DebugFile.writeln("End CallsTab.render()");
269      }
270      return sOutput;
271    }
272
273    // --------------------------------------------------------------------------
274

275    public void render(RenderRequest req, RenderResponse res)
276      throws PortletException, IOException JavaDoc, IllegalStateException JavaDoc {
277      res.getWriter().write(render(req,res.getCharacterEncoding()));
278    }
279
280 }
281
Popular Tags