KickJava   Java API By Example, From Geeks To Geeks.

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


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

68
69 public class MyIncidencesTab extends GenericPortlet {
70   public MyIncidencesTab() { }
71
72   public MyIncidencesTab(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 MyIncidencesTab.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 = "myincidencestab_" + 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(oFS.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 MyIncidencesTab.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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet type=\"text/xsl\"?>";
140
141     int iBugs = 0;
142
143     if (req.getWindowState().equals(WindowState.MINIMIZED)) {
144       sXML += "<bugs/>";
145     }
146     else {
147
148       DBBind oDBB = (DBBind) getPortletContext().getAttribute("GlobalDBBind");
149
150       DBSubset oBugs = new DBSubset (DB.k_bugs, DB.gu_bug + "," + DB.tl_bug,
151                                      "(" + DB.tx_status + " IS NULL OR " + DB.tx_status + " IN ('EN ESPERA', 'ASIGNADO', 'VERIFICADO')) AND (" + DB.nm_assigned + "=? OR " + DB.tx_rep_mail + " IN (SELECT " + DB.tx_main_email + " FROM " + DB.k_users + " WHERE " + DB.gu_user + "=?)) ORDER BY " + DB.od_priority + " DESC", 10);
152
153       JDCConnection oCon = null;
154
155       try {
156         oCon = oDBB.getConnection("MyIncidencesTab");
157
158         iBugs = oBugs.load (oCon, new Object JavaDoc[]{sUserId,sUserId});
159
160         oCon.close("MyIncidencesTab");
161         oCon = null;
162
163         sXML += "<bugs>\n"+oBugs.toXML("","bug")+"</bugs>";
164       }
165       catch (SQLException JavaDoc e) {
166         sXML += "<bugs/>";
167
168         try {
169           if (null != oCon)
170             if (!oCon.isClosed())
171               oCon.close("MyIncidencesTab");
172         } catch (SQLException JavaDoc ignore) { }
173       }
174     }
175
176     try {
177        if (DebugFile.trace) DebugFile.writeln("new ByteArrayInputStream(" + String.valueOf(sXML.length()) + ")");
178
179        if (sEncoding==null)
180          oInStream = new ByteArrayInputStream JavaDoc(sXML.getBytes());
181        else
182          oInStream = new ByteArrayInputStream JavaDoc(sXML.getBytes(sEncoding));
183
184        oOutStream = new ByteArrayOutputStream JavaDoc(4000);
185
186        Properties JavaDoc oProps = new Properties JavaDoc();
187
188        Enumeration JavaDoc oKeys = req.getPropertyNames();
189        while (oKeys.hasMoreElements()) {
190          String JavaDoc sKey = (String JavaDoc) oKeys.nextElement();
191          oProps.setProperty(sKey, req.getProperty(sKey));
192        } // wend
193

194        if (req.getWindowState().equals(WindowState.MINIMIZED))
195          oProps.setProperty("windowstate", "MINIMIZED");
196        else
197          oProps.setProperty("windowstate", "NORMAL");
198
199        StylesheetCache.transform (sTemplatePath, oInStream, oOutStream, oProps);
200
201        if (sEncoding==null)
202          sOutput = oOutStream.toString();
203        else
204          sOutput = oOutStream.toString("UTF-8");
205
206        oOutStream.close();
207
208        oInStream.close();
209        oInStream = null;
210
211        oFS.writefilestr (sFileDir+File.separator+sCachedFile, sOutput, sEncoding==null ? "ISO8859_1" : sEncoding);
212      }
213      catch (TransformerConfigurationException JavaDoc tce) {
214        if (DebugFile.trace) {
215          DebugFile.writeln("TransformerConfigurationException " + tce.getMessageAndLocation());
216          try {
217            DebugFile.write("--------------------------------------------------------------------------------\n");
218            DebugFile.write(FileSystem.readfile(sTemplatePath));
219            DebugFile.write("\n--------------------------------------------------------------------------------\n");
220            DebugFile.write(sXML);
221            DebugFile.write("\n--------------------------------------------------------------------------------\n");
222          }
223          catch (java.io.IOException JavaDoc ignore) { }
224          catch (com.enterprisedt.net.ftp.FTPException ignore) { }
225
226          DebugFile.decIdent();
227        }
228        throw new PortletException("TransformerConfigurationException " + tce.getMessage(), tce);
229      }
230      catch (TransformerException JavaDoc tex) {
231        if (DebugFile.trace) {
232          DebugFile.writeln("TransformerException " + tex.getMessageAndLocation());
233
234          try {
235            DebugFile.write("--------------------------------------------------------------------------------\n");
236            DebugFile.write(FileSystem.readfile(sTemplatePath));
237            DebugFile.write("\n--------------------------------------------------------------------------------\n");
238            DebugFile.write(sXML);
239            DebugFile.write("\n--------------------------------------------------------------------------------\n");
240          }
241          catch (java.io.IOException JavaDoc ignore) { }
242          catch (com.enterprisedt.net.ftp.FTPException ignore) { }
243
244          DebugFile.decIdent();
245        }
246        throw new PortletException("TransformerException " + tex.getMessage(), tex);
247      }
248
249      if (DebugFile.trace) {
250        DebugFile.decIdent();
251        DebugFile.writeln("End MyIncidencesTab.render()");
252      }
253      return sOutput;
254    }
255
256    // --------------------------------------------------------------------------
257

258    public void render(RenderRequest req, RenderResponse res)
259      throws PortletException, IOException JavaDoc, IllegalStateException JavaDoc {
260      res.getWriter().write(render(req,res.getCharacterEncoding()));
261     }
262
263 }
264
Popular Tags