KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Calendar Tabbed Dialog Portlet
65  * @author Sergio Montoro Ten
66  * @version 2.2
67  */

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

80   public String JavaDoc render(RenderRequest req, String JavaDoc sEncoding)
81     throws PortletException, IOException JavaDoc, IllegalStateException JavaDoc {
82
83     ByteArrayInputStream JavaDoc oInStream;
84     ByteArrayOutputStream JavaDoc oOutStream;
85
86     if (DebugFile.trace) {
87       DebugFile.writeln("Begin CalendarTab.render()");
88       DebugFile.incIdent();
89     }
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 = "calendartab_" + 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       try {
116
117         File JavaDoc oCached = new File JavaDoc(sFileDir.substring(7)+File.separator+sCachedFile);
118
119         if (!oCached.exists()) {
120           oFS.mkdirs(sFileDir);
121         }
122         else if (oCached.lastModified()>oDtModified.getTime()) {
123           sOutput = new String JavaDoc(FileSystem.readfile(sFileDir+File.separator+sCachedFile, sEncoding==null ? "ISO8859_1" : sEncoding));
124
125           if (DebugFile.trace) {
126             DebugFile.writeln("cache hit " + sFileDir+File.separator+sCachedFile);
127             DebugFile.decIdent();
128             DebugFile.writeln("End CalendarTab.render()");
129           }
130
131           return sOutput;
132         }
133       }
134       catch (Exception JavaDoc xcpt) {
135         DebugFile.writeln(xcpt.getClass().getName() + " " + xcpt.getMessage());
136       }
137     }
138
139     String JavaDoc sXML;
140
141     int iToDo = 0, iMeetings = 0;
142
143     if (req.getWindowState().equals(WindowState.MINIMIZED)) {
144       sXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet type=\"text/xsl\"?><calendar><todo/><today/></calendar>";
145     }
146     else {
147
148       String JavaDoc sTodayXML, sToDoXML;
149
150       Date JavaDoc dt00 = new Date JavaDoc();
151       Date JavaDoc dt23 = new Date JavaDoc();
152
153       dt00.setHours(0);
154       dt00.setMinutes(0);
155       dt00.setSeconds(0);
156
157       dt23.setHours(23);
158       dt23.setMinutes(59);
159       dt23.setSeconds(59);
160
161       DBBind oDBB = (DBBind) getPortletContext().getAttribute("GlobalDBBind");
162
163       DBSubset oToDo = new DBSubset (DB.k_to_do, DB.gu_to_do + "," + DB.od_priority + "," + DB.tl_to_do,
164                                      DB.gu_user + "=? AND (" + DB.tx_status + "='PENDING' OR " + DB.tx_status + " IS NULL) ORDER BY 2 DESC", 10);
165
166       DBSubset oMeetings = new DBSubset (DB.k_meetings + " m," + DB.k_x_meeting_fellow + " f",
167                                          "m." + DB.gu_meeting + ",m." + DB.gu_fellow + ",m." + DB.tp_meeting + ",m." + DB.tx_meeting + ", m." + DB.dt_start + ",m." + DB.dt_end,
168                                          "m." + DB.gu_meeting + "=f." + DB.gu_meeting + " AND f." + DB.gu_fellow + "=? AND m." + DB.dt_start + " BETWEEN ? AND ? ORDER BY m." + DB.dt_start, 10);
169
170       JDCConnection oCon = null;
171
172       try {
173         oCon = oDBB.getConnection("CalendarTab_today");
174
175         oToDo.setMaxRows(10);
176         iToDo = oToDo.load (oCon, new Object JavaDoc[]{sUserId});
177
178         for (int a=0; a<iToDo; a++) {
179           if (oToDo.getStringNull(2, a,"").length()>40)
180             oToDo.setElementAt(oToDo.getString(2, a).substring(0, 40) +"...", 2,a);
181         }
182
183         sToDoXML = oToDo.toXML("", "activity");
184
185         iMeetings = oMeetings.load(oCon, new Object JavaDoc[]{sUserId, new Timestamp JavaDoc(dt00.getTime()), new Timestamp JavaDoc(dt23.getTime())});
186
187         for (int m=0; m<iMeetings; m++) {
188
189           if (oMeetings.isNull(3,m)) oMeetings.setElementAt("untitled", 3,m);
190
191           Date JavaDoc oFrom = oMeetings.getDate(4,m);
192           oMeetings.setElementAt(String.valueOf(oFrom.getHours())+":"+Gadgets.leftPad(String.valueOf(oFrom.getMinutes()),'0',2), 4, m);
193
194           Date JavaDoc oTo = oMeetings.getDate(5,m);
195           oMeetings.setElementAt(String.valueOf(oTo.getHours())+":"+Gadgets.leftPad(String.valueOf(oTo.getMinutes()),'0',2), 5, m);
196         }
197
198         oCon.close("CalendarTab_today");
199         oCon = null;
200
201         sTodayXML = oMeetings.toXML("","meeting");
202       }
203       catch (SQLException JavaDoc e) {
204         sToDoXML = "<todo></todo>";
205         sTodayXML = "<today></today>";
206
207         try {
208           if (null != oCon)
209             if (!oCon.isClosed())
210               oCon.close("CalendarTab_today");
211         } catch (SQLException JavaDoc ignore) { }
212       }
213
214       sXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-stylesheet type=\"text/xsl\"?>\n<calendar>\n";
215       if (iToDo>0)
216         sXML += "<todo>\n"+sToDoXML+"</todo>\n";
217       else
218         sXML += "<todo/>\n";
219
220       if (iMeetings>0)
221         sXML += "<today>\n"+sTodayXML+"</today>\n</calendar>";
222       else
223         sXML += "<today/>\n</calendar>";
224      }
225
226      try {
227        if (DebugFile.trace) DebugFile.writeln("new ByteArrayInputStream(" + String.valueOf(sXML.length()) + ")");
228
229        if (sEncoding==null)
230          oInStream = new ByteArrayInputStream JavaDoc(sXML.getBytes());
231        else
232          oInStream = new ByteArrayInputStream JavaDoc(sXML.getBytes(sEncoding));
233
234        oOutStream = new ByteArrayOutputStream JavaDoc(4000);
235
236        Properties JavaDoc oProps = new Properties JavaDoc();
237
238        Enumeration JavaDoc oKeys = req.getPropertyNames();
239        while (oKeys.hasMoreElements()) {
240          String JavaDoc sKey = (String JavaDoc) oKeys.nextElement();
241          oProps.setProperty(sKey, req.getProperty(sKey));
242        } // wend
243

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

308    public void render(RenderRequest req, RenderResponse res)
309      throws PortletException, IOException JavaDoc, IllegalStateException JavaDoc {
310      res.getWriter().write(render(req,res.getCharacterEncoding()));
311    }
312 }
313
Popular Tags