1 32 package com.knowgate.http.portlets; 33 34 import java.io.File ; 35 import java.io.IOException ; 36 import java.io.ByteArrayOutputStream ; 37 import java.io.ByteArrayInputStream ; 38 import java.io.OutputStreamWriter ; 39 import java.io.PrintWriter ; 40 41 import java.util.Date ; 42 import java.util.Properties ; 43 import java.util.Enumeration ; 44 45 import java.sql.SQLException ; 46 import java.sql.PreparedStatement ; 47 import java.sql.ResultSet ; 48 import java.sql.Timestamp ; 49 50 import javax.xml.transform.TransformerException ; 51 import javax.xml.transform.TransformerConfigurationException ; 52 53 import javax.portlet.*; 54 55 import com.knowgate.debug.DebugFile; 56 import com.knowgate.jdc.JDCConnection; 57 import com.knowgate.dataobjs.*; 58 import com.knowgate.dataxslt.StylesheetCache; 59 import com.knowgate.misc.Gadgets; 60 import com.knowgate.dfs.FileSystem; 61 62 66 67 public class RecentContactsTab extends GenericPortlet { 68 public RecentContactsTab() { } 69 70 public RecentContactsTab(HipergatePortletConfig oConfig) 71 throws javax.portlet.PortletException { 72 73 init(oConfig); 74 } 75 76 78 public String render(RenderRequest req, String sEncoding) 79 throws PortletException, IOException , IllegalStateException { 80 81 ByteArrayInputStream oInStream; 82 ByteArrayOutputStream oOutStream; 83 84 if (DebugFile.trace) { 85 DebugFile.writeln("Begin RecentContactsTab.render()"); 86 DebugFile.incIdent(); 87 } 88 89 final int iMaxRecent = 8; 90 91 FileSystem oFS = new FileSystem(FileSystem.OS_PUREJAVA); 92 93 String sOutput; 94 String sDomainId = req.getProperty("domain"); 95 String sWorkAreaId = req.getProperty("workarea"); 96 String sUserId = req.getProperty("user"); 97 String sZone = req.getProperty("zone"); 98 String sLang = req.getProperty("language"); 99 String sTemplatePath = req.getProperty("template"); 100 String sStorage = req.getProperty("storage"); 101 String sFileDir = "file://" + sStorage + "domains" + File.separator + sDomainId + File.separator + "workareas" + File.separator + sWorkAreaId + File.separator + "cache" + File.separator + sUserId; 102 String sCachedFile = "recentcontactstab_" + 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 oDtModified = (Date ) req.getAttribute("modified"); 113 114 if (null!=oDtModified) { 115 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 RecentContactsTab.render()"); 135 } 136 137 return sOutput; 138 } } } 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 += "<contacts/>"; 152 } 153 else { 154 155 DBBind oDBB = (DBBind) getPortletContext().getAttribute("GlobalDBBind"); 156 157 JDCConnection oCon = null; 158 159 try { 160 oCon = oDBB.getConnection("RecentContactsTab"); 161 162 PreparedStatement oStm = oCon.prepareStatement("(SELECT dt_last_visit,gu_company,NULL AS gu_contact,nm_company,'' AS full_name,work_phone,tx_email FROM k_companies_recent WHERE gu_user=? UNION SELECT dt_last_visit,NULL AS gu_company,gu_contact,nm_company,full_name,work_phone,tx_email FROM k_contacts_recent WHERE gu_user=?) ORDER BY dt_last_visit DESC", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 163 oStm.setString (1, sUserId); 164 oStm.setString (2, sUserId); 165 ResultSet oRSet = oStm.executeQuery(); 166 167 int iRecentCount = 0; 168 StringBuffer oXML = new StringBuffer (); 169 String sStr; 170 171 while (oRSet.next() && iRecentCount<iMaxRecent) { 172 oXML.append("<contact>"); 173 174 sStr = oRSet.getString(2); 175 if (oRSet.wasNull()) oXML.append("<gu_company/>"); else oXML.append("<gu_company>"+oRSet.getString(2)+"</gu_company>"); 176 177 sStr = oRSet.getString(3); 178 if (oRSet.wasNull()) oXML.append("<gu_contact/>"); else oXML.append("<gu_contact>"+oRSet.getString(3)+"</gu_contact>"); 179 180 sStr = oRSet.getString(4); 181 if (oRSet.wasNull()) oXML.append("<nm_company/>"); else oXML.append("<nm_company><![CDATA["+oRSet.getString(4)+"]]></nm_company>"); 182 183 sStr = oRSet.getString(5); 184 if (oRSet.wasNull()) oXML.append("<full_name/>"); else oXML.append("<full_name><![CDATA["+oRSet.getString(5)+"]]></full_name>"); 185 186 sStr = oRSet.getString(6); 187 if (oRSet.wasNull()) oXML.append("<work_phone/>"); else oXML.append("<work_phone><![CDATA["+oRSet.getString(6)+"]]></work_phone>"); 188 189 sStr = oRSet.getString(7); 190 if (oRSet.wasNull()) oXML.append("<tx_email/>"); else oXML.append("<tx_email>"+oRSet.getString(7)+"</tx_email>"); 191 192 oXML.append("</contact>"); 193 194 iRecentCount++; 195 } 197 oCon.close("RecentContactsTab"); 198 oCon = null; 199 200 sXML += "<contacts>\n"+oXML.toString()+"</contacts>"; 201 } 202 catch (SQLException e) { 203 sXML += "<contacts/>"; 204 205 try { 206 if (null != oCon) 207 if (!oCon.isClosed()) 208 oCon.close("RecentContactsTab"); 209 } catch (SQLException ignore) { } 210 } 211 } 212 213 try { 214 if (DebugFile.trace) DebugFile.writeln("new ByteArrayInputStream(" + String.valueOf(sXML.length()) + ")"); 215 216 if (sEncoding==null) 217 oInStream = new ByteArrayInputStream (sXML.getBytes()); 218 else 219 oInStream = new ByteArrayInputStream (sXML.getBytes(sEncoding)); 220 221 oOutStream = new ByteArrayOutputStream (4000); 222 223 Properties oProps = new Properties (); 224 225 Enumeration oKeys = req.getPropertyNames(); 226 while (oKeys.hasMoreElements()) { 227 String sKey = (String ) oKeys.nextElement(); 228 oProps.setProperty(sKey, req.getProperty(sKey)); 229 } 231 if (req.getWindowState().equals(WindowState.MINIMIZED)) 232 oProps.setProperty("windowstate", "MINIMIZED"); 233 else 234 oProps.setProperty("windowstate", "NORMAL"); 235 236 StylesheetCache.transform (sTemplatePath, oInStream, oOutStream, oProps); 237 238 if (sEncoding==null) 239 sOutput = oOutStream.toString(); 240 else 241 sOutput = oOutStream.toString("UTF-8"); 242 243 oOutStream.close(); 244 245 oInStream.close(); 246 oInStream = null; 247 248 oFS.writefilestr (sFileDir+File.separator+sCachedFile, sOutput, sEncoding==null ? "ISO8859_1" : sEncoding); 249 } 250 catch (TransformerConfigurationException tce) { 251 if (DebugFile.trace) { 252 DebugFile.writeln("TransformerConfigurationException " + tce.getMessageAndLocation()); 253 try { 254 DebugFile.write("--------------------------------------------------------------------------------\n"); 255 DebugFile.write(FileSystem.readfile(sTemplatePath)); 256 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 257 DebugFile.write(sXML); 258 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 259 } 260 catch (java.io.IOException ignore) { } 261 catch (com.enterprisedt.net.ftp.FTPException ignore) { } 262 263 DebugFile.decIdent(); 264 } 265 throw new PortletException("TransformerConfigurationException " + tce.getMessage(), tce); 266 } 267 catch (TransformerException tex) { 268 if (DebugFile.trace) { 269 DebugFile.writeln("TransformerException " + tex.getMessageAndLocation()); 270 271 try { 272 DebugFile.write("--------------------------------------------------------------------------------\n"); 273 DebugFile.write(FileSystem.readfile(sTemplatePath)); 274 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 275 DebugFile.write(sXML); 276 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 277 } 278 catch (java.io.IOException ignore) { } 279 catch (com.enterprisedt.net.ftp.FTPException ignore) { } 280 281 DebugFile.decIdent(); 282 } 283 throw new PortletException("TransformerException " + tex.getMessage(), tex); 284 } 285 286 if (DebugFile.trace) { 287 DebugFile.decIdent(); 288 DebugFile.writeln("End RecentContactsTab.render()"); 289 } 290 return sOutput; 291 } 292 293 295 public void render(RenderRequest req, RenderResponse res) 296 throws PortletException, IOException , IllegalStateException { 297 res.getWriter().write(render(req,res.getCharacterEncoding())); 298 } 299 } 300 | Popular Tags |