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 public class RecentPostsTab extends GenericPortlet { 70 public RecentPostsTab() { } 71 72 public RecentPostsTab(HipergatePortletConfig oConfig) 73 throws javax.portlet.PortletException { 74 75 init(oConfig); 76 } 77 78 80 public String render(RenderRequest req, String sEncoding) 81 throws PortletException, IOException , IllegalStateException { 82 83 ByteArrayInputStream oInStream; 84 ByteArrayOutputStream oOutStream; 85 86 if (DebugFile.trace) { 87 DebugFile.writeln("Begin RecentPosts.render()"); 88 DebugFile.incIdent(); 89 } 90 91 final int iMaxRecent = 10; 92 93 FileSystem oFS = new FileSystem(FileSystem.OS_PUREJAVA); 94 95 String sOutput; 96 String sDomainId = req.getProperty("domain"); 97 String sWorkAreaId = req.getProperty("workarea"); 98 String sUserId = req.getProperty("user"); 99 String sZone = req.getProperty("zone"); 100 String sLang = req.getProperty("language"); 101 String sTemplatePath = req.getProperty("template"); 102 String sStorage = req.getProperty("storage"); 103 String sFileDir = "file://" + sStorage + "domains" + File.separator + sDomainId + File.separator + "workareas" + File.separator + sWorkAreaId + File.separator + "cache" + File.separator + sUserId; 104 String sCachedFile = "recentpoststab_" + req.getWindowState().toString() + ".xhtm"; 105 106 if (DebugFile.trace) { 107 DebugFile.writeln ("user=" + sUserId); 108 DebugFile.writeln ("template=" + sTemplatePath); 109 DebugFile.writeln ("cache dir=" + sFileDir); 110 DebugFile.writeln ("modified=" + req.getAttribute("modified")); 111 DebugFile.writeln ("encoding=" + sEncoding); 112 } 113 114 Date oDtModified = (Date ) req.getAttribute("modified"); 115 116 if (null!=oDtModified) { 117 try { 118 119 File oCached = new File (sFileDir.substring(7)+File.separator+sCachedFile); 120 121 if (!oCached.exists()) { 122 oFS.mkdirs(sFileDir); 123 } 124 else if (oCached.lastModified()>oDtModified.getTime()) { 125 sOutput = new String (oFS.readfile(sFileDir+File.separator+sCachedFile, sEncoding==null ? "ISO8859_1" : sEncoding)); 126 127 if (DebugFile.trace) { 128 DebugFile.writeln("cache hit " + sFileDir+File.separator+sCachedFile); 129 DebugFile.decIdent(); 130 DebugFile.writeln("End RecentPosts.render()"); 131 } 132 133 return sOutput; 134 } 135 } 136 catch (Exception xcpt) { 137 DebugFile.writeln(xcpt.getClass().getName() + " " + xcpt.getMessage()); 138 } 139 } 140 141 String sXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet type=\"text/xsl\"?>"; 142 143 int iPosts = 0; 144 145 if (req.getWindowState().equals(WindowState.MINIMIZED)) { 146 sXML += "<posts/>"; 147 } 148 else { 149 150 DBBind oDBB = (DBBind) getPortletContext().getAttribute("GlobalDBBind"); 151 152 DBSubset oPosts = new DBSubset (DB.k_newsmsgs + " m," + DB.k_x_cat_objs + " x," + DB.k_newsgroups + " g", 153 "x." + DB.gu_category + ",m." + DB.gu_msg + ",m." + DB.gu_product + ",m." + DB.nm_author + ",m." + DB.tx_subject + 154 ",m." + DB.dt_published + ",m." + DB.tx_email + ",m." + DB.nu_thread_msgs + ",m." + DB.gu_thread_msg, 155 "m." + DB.id_status + "=0 AND x." + DB.gu_category + "=" + "g." + DB.gu_newsgrp + " AND m." + 156 DB.gu_msg + "=x." + DB.gu_object + " AND m." + DB.gu_parent_msg + " IS NULL AND g." + DB.gu_workarea + 157 "=? ORDER BY 6 DESC", iMaxRecent); 158 159 JDCConnection oCon = null; 160 161 try { 162 oCon = oDBB.getConnection("RecentPostsTab"); 163 164 iPosts = oPosts.load (oCon, new Object []{sWorkAreaId}); 165 166 oCon.close("RecentPostsTab"); 167 oCon = null; 168 169 Date dtPub; 170 for (int p=0; p<iPosts; p++) { 171 dtPub = oPosts.getDate(5,p); 172 oPosts.setElementAt(Gadgets.leftPad(String.valueOf(dtPub.getMonth()+1),'0',2)+"-"+Gadgets.leftPad(String.valueOf(dtPub.getDate()),'0',2)+" "+Gadgets.leftPad(String.valueOf(dtPub.getHours()),'0',2)+":"+Gadgets.leftPad(String.valueOf(dtPub.getMinutes()),'0',2), 5,p); 173 } 174 175 sXML += "<posts>\n"+oPosts.toXML("","newsmsg")+"</posts>"; 176 } 177 catch (SQLException e) { 178 sXML += "<posts/>"; 179 180 try { 181 if (null != oCon) 182 if (!oCon.isClosed()) 183 oCon.close("RecentPostsTab"); 184 } catch (SQLException ignore) { } 185 } 186 } 187 188 try { 189 if (DebugFile.trace) DebugFile.writeln("new ByteArrayInputStream(" + String.valueOf(sXML.length()) + ")"); 190 191 if (sEncoding==null) 192 oInStream = new ByteArrayInputStream (sXML.getBytes()); 193 else 194 oInStream = new ByteArrayInputStream (sXML.getBytes(sEncoding)); 195 196 oOutStream = new ByteArrayOutputStream (4000); 197 198 Properties oProps = new Properties (); 199 200 Enumeration oKeys = req.getPropertyNames(); 201 while (oKeys.hasMoreElements()) { 202 String sKey = (String ) oKeys.nextElement(); 203 oProps.setProperty(sKey, req.getProperty(sKey)); 204 } 206 if (req.getWindowState().equals(WindowState.MINIMIZED)) 207 oProps.setProperty("windowstate", "MINIMIZED"); 208 else 209 oProps.setProperty("windowstate", "NORMAL"); 210 211 StylesheetCache.transform (sTemplatePath, oInStream, oOutStream, oProps); 212 213 if (sEncoding==null) 214 sOutput = oOutStream.toString(); 215 else 216 sOutput = oOutStream.toString("UTF-8"); 217 218 oOutStream.close(); 219 220 oInStream.close(); 221 oInStream = null; 222 223 oFS.writefilestr (sFileDir+File.separator+sCachedFile, sOutput, sEncoding==null ? "ISO8859_1" : sEncoding); 224 } 225 catch (TransformerConfigurationException tce) { 226 if (DebugFile.trace) { 227 DebugFile.writeln("TransformerConfigurationException " + tce.getMessageAndLocation()); 228 try { 229 DebugFile.write("--------------------------------------------------------------------------------\n"); 230 DebugFile.write(FileSystem.readfile(sTemplatePath)); 231 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 232 DebugFile.write(sXML); 233 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 234 } 235 catch (java.io.IOException ignore) { } 236 catch (com.enterprisedt.net.ftp.FTPException ignore) { } 237 238 DebugFile.decIdent(); 239 } 240 throw new PortletException("TransformerConfigurationException " + tce.getMessage(), tce); 241 } 242 catch (TransformerException tex) { 243 if (DebugFile.trace) { 244 DebugFile.writeln("TransformerException " + tex.getMessageAndLocation()); 245 246 try { 247 DebugFile.write("--------------------------------------------------------------------------------\n"); 248 DebugFile.write(FileSystem.readfile(sTemplatePath)); 249 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 250 DebugFile.write(sXML); 251 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 252 } 253 catch (java.io.IOException ignore) { } 254 catch (com.enterprisedt.net.ftp.FTPException ignore) { } 255 256 DebugFile.decIdent(); 257 } 258 throw new PortletException("TransformerException " + tex.getMessage(), tex); 259 } 260 261 if (DebugFile.trace) { 262 DebugFile.decIdent(); 263 DebugFile.writeln("End RecentPosts.render()"); 264 } 265 return sOutput; 266 } 267 268 270 public void render(RenderRequest req, RenderResponse res) 271 throws PortletException, IOException , IllegalStateException { 272 res.getWriter().write(render(req,res.getCharacterEncoding())); 273 } 274 } 275 | Popular Tags |