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: * Recent Incidents Tabbed Dialog Portlet
065: * @author Sergio Montoro Ten
066: * @version 2.2
067: */
068:
069: public class MyIncidencesTab extends GenericPortlet {
070: public MyIncidencesTab() {
071: }
072:
073: public MyIncidencesTab(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 MyIncidencesTab.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 = "myincidencestab_"
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(oFS
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
141: .writeln("End MyIncidencesTab.render()");
142: }
143:
144: return sOutput;
145: }
146: } catch (Exception xcpt) {
147: DebugFile.writeln(xcpt.getClass().getName() + " "
148: + xcpt.getMessage());
149: }
150: }
151:
152: String sXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet type=\"text/xsl\"?>";
153:
154: int iBugs = 0;
155:
156: if (req.getWindowState().equals(WindowState.MINIMIZED)) {
157: sXML += "<bugs/>";
158: } else {
159:
160: DBBind oDBB = (DBBind) getPortletContext().getAttribute(
161: "GlobalDBBind");
162:
163: DBSubset oBugs = new DBSubset(
164: DB.k_bugs,
165: DB.gu_bug + "," + DB.tl_bug,
166: "("
167: + DB.tx_status
168: + " IS NULL OR "
169: + DB.tx_status
170: + " IN ('EN ESPERA', 'ASIGNADO', 'VERIFICADO')) AND ("
171: + DB.nm_assigned + "=? OR "
172: + DB.tx_rep_mail + " IN (SELECT "
173: + DB.tx_main_email + " FROM " + DB.k_users
174: + " WHERE " + DB.gu_user + "=?)) ORDER BY "
175: + DB.od_priority + " DESC", 10);
176:
177: JDCConnection oCon = null;
178:
179: try {
180: oCon = oDBB.getConnection("MyIncidencesTab");
181:
182: iBugs = oBugs.load(oCon, new Object[] { sUserId,
183: sUserId });
184:
185: oCon.close("MyIncidencesTab");
186: oCon = null;
187:
188: sXML += "<bugs>\n" + oBugs.toXML("", "bug") + "</bugs>";
189: } catch (SQLException e) {
190: sXML += "<bugs/>";
191:
192: try {
193: if (null != oCon)
194: if (!oCon.isClosed())
195: oCon.close("MyIncidencesTab");
196: } catch (SQLException ignore) {
197: }
198: }
199: }
200:
201: try {
202: if (DebugFile.trace)
203: DebugFile.writeln("new ByteArrayInputStream("
204: + String.valueOf(sXML.length()) + ")");
205:
206: if (sEncoding == null)
207: oInStream = new ByteArrayInputStream(sXML.getBytes());
208: else
209: oInStream = new ByteArrayInputStream(sXML
210: .getBytes(sEncoding));
211:
212: oOutStream = new ByteArrayOutputStream(4000);
213:
214: Properties oProps = new Properties();
215:
216: Enumeration oKeys = req.getPropertyNames();
217: while (oKeys.hasMoreElements()) {
218: String sKey = (String) oKeys.nextElement();
219: oProps.setProperty(sKey, req.getProperty(sKey));
220: } // wend
221:
222: if (req.getWindowState().equals(WindowState.MINIMIZED))
223: oProps.setProperty("windowstate", "MINIMIZED");
224: else
225: oProps.setProperty("windowstate", "NORMAL");
226:
227: StylesheetCache.transform(sTemplatePath, oInStream,
228: oOutStream, oProps);
229:
230: if (sEncoding == null)
231: sOutput = oOutStream.toString();
232: else
233: sOutput = oOutStream.toString("UTF-8");
234:
235: oOutStream.close();
236:
237: oInStream.close();
238: oInStream = null;
239:
240: oFS.writefilestr(sFileDir + File.separator + sCachedFile,
241: sOutput, sEncoding == null ? "ISO8859_1"
242: : sEncoding);
243: } catch (TransformerConfigurationException tce) {
244: if (DebugFile.trace) {
245: DebugFile.writeln("TransformerConfigurationException "
246: + tce.getMessageAndLocation());
247: try {
248: DebugFile
249: .write("--------------------------------------------------------------------------------\n");
250: DebugFile.write(FileSystem.readfile(sTemplatePath));
251: DebugFile
252: .write("\n--------------------------------------------------------------------------------\n");
253: DebugFile.write(sXML);
254: DebugFile
255: .write("\n--------------------------------------------------------------------------------\n");
256: } catch (java.io.IOException ignore) {
257: } catch (com.enterprisedt.net.ftp.FTPException ignore) {
258: }
259:
260: DebugFile.decIdent();
261: }
262: throw new PortletException(
263: "TransformerConfigurationException "
264: + tce.getMessage(), tce);
265: } catch (TransformerException tex) {
266: if (DebugFile.trace) {
267: DebugFile.writeln("TransformerException "
268: + tex.getMessageAndLocation());
269:
270: try {
271: DebugFile
272: .write("--------------------------------------------------------------------------------\n");
273: DebugFile.write(FileSystem.readfile(sTemplatePath));
274: DebugFile
275: .write("\n--------------------------------------------------------------------------------\n");
276: DebugFile.write(sXML);
277: DebugFile
278: .write("\n--------------------------------------------------------------------------------\n");
279: } catch (java.io.IOException ignore) {
280: } catch (com.enterprisedt.net.ftp.FTPException ignore) {
281: }
282:
283: DebugFile.decIdent();
284: }
285: throw new PortletException("TransformerException "
286: + tex.getMessage(), tex);
287: }
288:
289: if (DebugFile.trace) {
290: DebugFile.decIdent();
291: DebugFile.writeln("End MyIncidencesTab.render()");
292: }
293: return sOutput;
294: }
295:
296: // --------------------------------------------------------------------------
297:
298: public void render(RenderRequest req, RenderResponse res)
299: throws PortletException, IOException, IllegalStateException {
300: res.getWriter().write(render(req, res.getCharacterEncoding()));
301: }
302:
303: }
|