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:
033: package com.knowgate.http.portlets;
034:
035: import java.io.File;
036: import java.io.IOException;
037: import java.io.ByteArrayOutputStream;
038: import java.io.ByteArrayInputStream;
039: import java.io.OutputStreamWriter;
040: import java.io.PrintWriter;
041:
042: import java.util.Date;
043: import java.util.Properties;
044: import java.util.Enumeration;
045:
046: import java.sql.SQLException;
047: import java.sql.PreparedStatement;
048: import java.sql.ResultSet;
049: import java.sql.Timestamp;
050:
051: import javax.xml.transform.TransformerException;
052: import javax.xml.transform.TransformerConfigurationException;
053:
054: import javax.portlet.*;
055:
056: import com.knowgate.debug.DebugFile;
057: import com.knowgate.jdc.JDCConnection;
058: import com.knowgate.dataobjs.*;
059: import com.knowgate.dataxslt.StylesheetCache;
060: import com.knowgate.misc.Gadgets;
061: import com.knowgate.dfs.FileSystem;
062:
063: /**
064: * Calendar Tabbed Dialog Portlet
065: * @author Sergio Montoro Ten
066: * @version 2.2
067: */
068:
069: public class CalendarTab extends GenericPortlet {
070: public CalendarTab() {
071: }
072:
073: public CalendarTab(HipergatePortletConfig oConfig)
074: throws javax.portlet.PortletException {
075:
076: init(oConfig);
077: }
078:
079: // ---------------------------------------------------------------------------
080:
081: public String render(RenderRequest req, String sEncoding)
082: throws PortletException, IOException, IllegalStateException {
083:
084: ByteArrayInputStream oInStream;
085: ByteArrayOutputStream oOutStream;
086:
087: if (DebugFile.trace) {
088: DebugFile.writeln("Begin CalendarTab.render()");
089: DebugFile.incIdent();
090: }
091:
092: FileSystem oFS = new FileSystem(FileSystem.OS_PUREJAVA);
093:
094: String sOutput;
095: String sDomainId = req.getProperty("domain");
096: String sWorkAreaId = req.getProperty("workarea");
097: String sUserId = req.getProperty("user");
098: String sZone = req.getProperty("zone");
099: String sLang = req.getProperty("language");
100: String sTemplatePath = req.getProperty("template");
101: String sStorage = req.getProperty("storage");
102: String sFileDir = "file://" + sStorage + "domains"
103: + File.separator + sDomainId + File.separator
104: + "workareas" + File.separator + sWorkAreaId
105: + File.separator + "cache" + File.separator + sUserId;
106: String sCachedFile = "calendartab_"
107: + req.getWindowState().toString() + ".xhtm";
108:
109: if (DebugFile.trace) {
110: DebugFile.writeln("user=" + sUserId);
111: DebugFile.writeln("template=" + sTemplatePath);
112: DebugFile.writeln("cache dir=" + sFileDir);
113: DebugFile.writeln("modified="
114: + req.getAttribute("modified"));
115: DebugFile.writeln("encoding=" + sEncoding);
116: }
117:
118: Date oDtModified = (Date) req.getAttribute("modified");
119:
120: if (null != oDtModified) {
121: try {
122:
123: File oCached = new File(sFileDir.substring(7)
124: + File.separator + sCachedFile);
125:
126: if (!oCached.exists()) {
127: oFS.mkdirs(sFileDir);
128: } else if (oCached.lastModified() > oDtModified
129: .getTime()) {
130: sOutput = new String(FileSystem
131: .readfile(sFileDir + File.separator
132: + sCachedFile,
133: sEncoding == null ? "ISO8859_1"
134: : sEncoding));
135:
136: if (DebugFile.trace) {
137: DebugFile.writeln("cache hit " + sFileDir
138: + File.separator + sCachedFile);
139: DebugFile.decIdent();
140: DebugFile.writeln("End CalendarTab.render()");
141: }
142:
143: return sOutput;
144: }
145: } catch (Exception xcpt) {
146: DebugFile.writeln(xcpt.getClass().getName() + " "
147: + xcpt.getMessage());
148: }
149: }
150:
151: String sXML;
152:
153: int iToDo = 0, iMeetings = 0;
154:
155: if (req.getWindowState().equals(WindowState.MINIMIZED)) {
156: sXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet type=\"text/xsl\"?><calendar><todo/><today/></calendar>";
157: } else {
158:
159: String sTodayXML, sToDoXML;
160:
161: Date dt00 = new Date();
162: Date dt23 = new Date();
163:
164: dt00.setHours(0);
165: dt00.setMinutes(0);
166: dt00.setSeconds(0);
167:
168: dt23.setHours(23);
169: dt23.setMinutes(59);
170: dt23.setSeconds(59);
171:
172: DBBind oDBB = (DBBind) getPortletContext().getAttribute(
173: "GlobalDBBind");
174:
175: DBSubset oToDo = new DBSubset(DB.k_to_do, DB.gu_to_do + ","
176: + DB.od_priority + "," + DB.tl_to_do, DB.gu_user
177: + "=? AND (" + DB.tx_status + "='PENDING' OR "
178: + DB.tx_status + " IS NULL) ORDER BY 2 DESC", 10);
179:
180: DBSubset oMeetings = new DBSubset(DB.k_meetings + " m,"
181: + DB.k_x_meeting_fellow + " f", "m."
182: + DB.gu_meeting + ",m." + DB.gu_fellow + ",m."
183: + DB.tp_meeting + ",m." + DB.tx_meeting + ", m."
184: + DB.dt_start + ",m." + DB.dt_end, "m."
185: + DB.gu_meeting + "=f." + DB.gu_meeting + " AND f."
186: + DB.gu_fellow + "=? AND m." + DB.dt_start
187: + " BETWEEN ? AND ? ORDER BY m." + DB.dt_start, 10);
188:
189: JDCConnection oCon = null;
190:
191: try {
192: oCon = oDBB.getConnection("CalendarTab_today");
193:
194: oToDo.setMaxRows(10);
195: iToDo = oToDo.load(oCon, new Object[] { sUserId });
196:
197: for (int a = 0; a < iToDo; a++) {
198: if (oToDo.getStringNull(2, a, "").length() > 40)
199: oToDo.setElementAt(oToDo.getString(2, a)
200: .substring(0, 40)
201: + "...", 2, a);
202: }
203:
204: sToDoXML = oToDo.toXML("", "activity");
205:
206: iMeetings = oMeetings.load(oCon, new Object[] {
207: sUserId, new Timestamp(dt00.getTime()),
208: new Timestamp(dt23.getTime()) });
209:
210: for (int m = 0; m < iMeetings; m++) {
211:
212: if (oMeetings.isNull(3, m))
213: oMeetings.setElementAt("untitled", 3, m);
214:
215: Date oFrom = oMeetings.getDate(4, m);
216: oMeetings.setElementAt(String.valueOf(oFrom
217: .getHours())
218: + ":"
219: + Gadgets.leftPad(String.valueOf(oFrom
220: .getMinutes()), '0', 2), 4, m);
221:
222: Date oTo = oMeetings.getDate(5, m);
223: oMeetings.setElementAt(String.valueOf(oTo
224: .getHours())
225: + ":"
226: + Gadgets.leftPad(String.valueOf(oTo
227: .getMinutes()), '0', 2), 5, m);
228: }
229:
230: oCon.close("CalendarTab_today");
231: oCon = null;
232:
233: sTodayXML = oMeetings.toXML("", "meeting");
234: } catch (SQLException e) {
235: sToDoXML = "<todo></todo>";
236: sTodayXML = "<today></today>";
237:
238: try {
239: if (null != oCon)
240: if (!oCon.isClosed())
241: oCon.close("CalendarTab_today");
242: } catch (SQLException ignore) {
243: }
244: }
245:
246: sXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-stylesheet type=\"text/xsl\"?>\n<calendar>\n";
247: if (iToDo > 0)
248: sXML += "<todo>\n" + sToDoXML + "</todo>\n";
249: else
250: sXML += "<todo/>\n";
251:
252: if (iMeetings > 0)
253: sXML += "<today>\n" + sTodayXML
254: + "</today>\n</calendar>";
255: else
256: sXML += "<today/>\n</calendar>";
257: }
258:
259: try {
260: if (DebugFile.trace)
261: DebugFile.writeln("new ByteArrayInputStream("
262: + String.valueOf(sXML.length()) + ")");
263:
264: if (sEncoding == null)
265: oInStream = new ByteArrayInputStream(sXML.getBytes());
266: else
267: oInStream = new ByteArrayInputStream(sXML
268: .getBytes(sEncoding));
269:
270: oOutStream = new ByteArrayOutputStream(4000);
271:
272: Properties oProps = new Properties();
273:
274: Enumeration oKeys = req.getPropertyNames();
275: while (oKeys.hasMoreElements()) {
276: String sKey = (String) oKeys.nextElement();
277: oProps.setProperty(sKey, req.getProperty(sKey));
278: } // wend
279:
280: if (req.getWindowState().equals(WindowState.MINIMIZED))
281: oProps.setProperty("windowstate", "MINIMIZED");
282: else
283: oProps.setProperty("windowstate", "NORMAL");
284:
285: StylesheetCache.transform(sTemplatePath, oInStream,
286: oOutStream, oProps);
287:
288: if (sEncoding == null)
289: sOutput = oOutStream.toString();
290: else
291: sOutput = oOutStream.toString("UTF-8");
292:
293: oOutStream.close();
294:
295: oInStream.close();
296: oInStream = null;
297:
298: oFS.writefilestr(sFileDir + File.separator + sCachedFile,
299: sOutput, sEncoding == null ? "ISO8859_1"
300: : sEncoding);
301: } catch (TransformerConfigurationException tce) {
302: if (DebugFile.trace) {
303: DebugFile.writeln("TransformerConfigurationException "
304: + tce.getMessageAndLocation());
305: try {
306: DebugFile
307: .write("--------------------------------------------------------------------------------\n");
308: DebugFile.write(FileSystem.readfile(sTemplatePath));
309: DebugFile
310: .write("\n--------------------------------------------------------------------------------\n");
311: DebugFile.write(sXML);
312: DebugFile
313: .write("\n--------------------------------------------------------------------------------\n");
314: } catch (java.io.IOException ignore) {
315: } catch (com.enterprisedt.net.ftp.FTPException ignore) {
316: }
317:
318: DebugFile.decIdent();
319: }
320: throw new PortletException(
321: "TransformerConfigurationException "
322: + tce.getMessage(), tce);
323: } catch (TransformerException tex) {
324: if (DebugFile.trace) {
325: DebugFile.writeln("TransformerException "
326: + tex.getMessageAndLocation());
327:
328: try {
329: DebugFile
330: .write("--------------------------------------------------------------------------------\n");
331: DebugFile.write(FileSystem.readfile(sTemplatePath));
332: DebugFile
333: .write("\n--------------------------------------------------------------------------------\n");
334: DebugFile.write(sXML);
335: DebugFile
336: .write("\n--------------------------------------------------------------------------------\n");
337: } catch (java.io.IOException ignore) {
338: } catch (com.enterprisedt.net.ftp.FTPException ignore) {
339: }
340:
341: DebugFile.decIdent();
342: }
343: throw new PortletException("TransformerException "
344: + tex.getMessage(), tex);
345: }
346:
347: if (DebugFile.trace) {
348: DebugFile.decIdent();
349: DebugFile.writeln("End CalendarTab.render()");
350: }
351: return sOutput;
352: }
353:
354: // --------------------------------------------------------------------------
355:
356: public void render(RenderRequest req, RenderResponse res)
357: throws PortletException, IOException, IllegalStateException {
358: res.getWriter().write(render(req, res.getCharacterEncoding()));
359: }
360: }
|