1 32 33 package com.knowgate.http.portlets; 34 35 import java.io.File ; 36 import java.io.IOException ; 37 import java.io.ByteArrayOutputStream ; 38 import java.io.ByteArrayInputStream ; 39 import java.io.OutputStreamWriter ; 40 import java.io.PrintWriter ; 41 42 import java.util.Date ; 43 import java.util.Properties ; 44 import java.util.Enumeration ; 45 46 import java.sql.SQLException ; 47 import java.sql.PreparedStatement ; 48 import java.sql.ResultSet ; 49 import java.sql.Timestamp ; 50 51 import javax.xml.transform.TransformerException ; 52 import javax.xml.transform.TransformerConfigurationException ; 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 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 81 public String render(RenderRequest req, String sEncoding) 82 throws PortletException, IOException , IllegalStateException { 83 84 ByteArrayInputStream oInStream; 85 ByteArrayOutputStream 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 sOutput; 95 String sDomainId = req.getProperty("domain"); 96 String sWorkAreaId = req.getProperty("workarea"); 97 String sUserId = req.getProperty("user"); 98 String sZone = req.getProperty("zone"); 99 String sLang = req.getProperty("language"); 100 String sTemplatePath = req.getProperty("template"); 101 String sStorage = req.getProperty("storage"); 102 String sFileDir = "file://" + sStorage + "domains" + File.separator + sDomainId + File.separator + "workareas" + File.separator + sWorkAreaId + File.separator + "cache" + File.separator + sUserId; 103 String 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 oDtModified = (Date ) req.getAttribute("modified"); 114 115 if (null!=oDtModified) { 116 try { 117 118 File oCached = new File (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 (oCached.lastModified()).toString()); 126 DebugFile.writeln ("last modified " + new Date (oDtModified.getTime()).toString()); 127 } 128 if (oCached.lastModified()>oDtModified.getTime()) { 129 sOutput = new String (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 xcpt) { 142 DebugFile.writeln(xcpt.getClass().getName() + " " + xcpt.getMessage()); 143 } 144 } 145 146 String 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 []{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 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 } 177 oCon.close("CallsTab"); 178 oCon = null; 179 180 sXML += "<calls>\n"+oCalls.toXML("","call")+"</calls>"; 181 } 182 catch (SQLException e) { 183 sXML += "<calls/>"; 184 185 try { 186 if (null != oCon) 187 if (!oCon.isClosed()) 188 oCon.close("CallsTab"); 189 } catch (SQLException 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 (sXML.getBytes()); 198 else 199 oInStream = new ByteArrayInputStream (sXML.getBytes(sEncoding)); 200 201 oOutStream = new ByteArrayOutputStream (4000); 202 203 Properties oProps = new Properties (); 204 205 Enumeration oKeys = req.getPropertyNames(); 206 while (oKeys.hasMoreElements()) { 207 String sKey = (String ) oKeys.nextElement(); 208 oProps.setProperty(sKey, req.getProperty(sKey)); 209 } 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 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 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 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 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 275 public void render(RenderRequest req, RenderResponse res) 276 throws PortletException, IOException , IllegalStateException { 277 res.getWriter().write(render(req,res.getCharacterEncoding())); 278 } 279 280 } 281 | Popular Tags |