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 CalendarTab extends GenericPortlet { 70 public CalendarTab() { } 71 72 public CalendarTab(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 CalendarTab.render()"); 88 DebugFile.incIdent(); 89 } 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 = "calendartab_" + 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 try { 116 117 File oCached = new File (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 (FileSystem.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 CalendarTab.render()"); 129 } 130 131 return sOutput; 132 } 133 } 134 catch (Exception xcpt) { 135 DebugFile.writeln(xcpt.getClass().getName() + " " + xcpt.getMessage()); 136 } 137 } 138 139 String sXML; 140 141 int iToDo = 0, iMeetings = 0; 142 143 if (req.getWindowState().equals(WindowState.MINIMIZED)) { 144 sXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet type=\"text/xsl\"?><calendar><todo/><today/></calendar>"; 145 } 146 else { 147 148 String sTodayXML, sToDoXML; 149 150 Date dt00 = new Date (); 151 Date dt23 = new Date (); 152 153 dt00.setHours(0); 154 dt00.setMinutes(0); 155 dt00.setSeconds(0); 156 157 dt23.setHours(23); 158 dt23.setMinutes(59); 159 dt23.setSeconds(59); 160 161 DBBind oDBB = (DBBind) getPortletContext().getAttribute("GlobalDBBind"); 162 163 DBSubset oToDo = new DBSubset (DB.k_to_do, DB.gu_to_do + "," + DB.od_priority + "," + DB.tl_to_do, 164 DB.gu_user + "=? AND (" + DB.tx_status + "='PENDING' OR " + DB.tx_status + " IS NULL) ORDER BY 2 DESC", 10); 165 166 DBSubset oMeetings = new DBSubset (DB.k_meetings + " m," + DB.k_x_meeting_fellow + " f", 167 "m." + DB.gu_meeting + ",m." + DB.gu_fellow + ",m." + DB.tp_meeting + ",m." + DB.tx_meeting + ", m." + DB.dt_start + ",m." + DB.dt_end, 168 "m." + DB.gu_meeting + "=f." + DB.gu_meeting + " AND f." + DB.gu_fellow + "=? AND m." + DB.dt_start + " BETWEEN ? AND ? ORDER BY m." + DB.dt_start, 10); 169 170 JDCConnection oCon = null; 171 172 try { 173 oCon = oDBB.getConnection("CalendarTab_today"); 174 175 oToDo.setMaxRows(10); 176 iToDo = oToDo.load (oCon, new Object []{sUserId}); 177 178 for (int a=0; a<iToDo; a++) { 179 if (oToDo.getStringNull(2, a,"").length()>40) 180 oToDo.setElementAt(oToDo.getString(2, a).substring(0, 40) +"...", 2,a); 181 } 182 183 sToDoXML = oToDo.toXML("", "activity"); 184 185 iMeetings = oMeetings.load(oCon, new Object []{sUserId, new Timestamp (dt00.getTime()), new Timestamp (dt23.getTime())}); 186 187 for (int m=0; m<iMeetings; m++) { 188 189 if (oMeetings.isNull(3,m)) oMeetings.setElementAt("untitled", 3,m); 190 191 Date oFrom = oMeetings.getDate(4,m); 192 oMeetings.setElementAt(String.valueOf(oFrom.getHours())+":"+Gadgets.leftPad(String.valueOf(oFrom.getMinutes()),'0',2), 4, m); 193 194 Date oTo = oMeetings.getDate(5,m); 195 oMeetings.setElementAt(String.valueOf(oTo.getHours())+":"+Gadgets.leftPad(String.valueOf(oTo.getMinutes()),'0',2), 5, m); 196 } 197 198 oCon.close("CalendarTab_today"); 199 oCon = null; 200 201 sTodayXML = oMeetings.toXML("","meeting"); 202 } 203 catch (SQLException e) { 204 sToDoXML = "<todo></todo>"; 205 sTodayXML = "<today></today>"; 206 207 try { 208 if (null != oCon) 209 if (!oCon.isClosed()) 210 oCon.close("CalendarTab_today"); 211 } catch (SQLException ignore) { } 212 } 213 214 sXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-stylesheet type=\"text/xsl\"?>\n<calendar>\n"; 215 if (iToDo>0) 216 sXML += "<todo>\n"+sToDoXML+"</todo>\n"; 217 else 218 sXML += "<todo/>\n"; 219 220 if (iMeetings>0) 221 sXML += "<today>\n"+sTodayXML+"</today>\n</calendar>"; 222 else 223 sXML += "<today/>\n</calendar>"; 224 } 225 226 try { 227 if (DebugFile.trace) DebugFile.writeln("new ByteArrayInputStream(" + String.valueOf(sXML.length()) + ")"); 228 229 if (sEncoding==null) 230 oInStream = new ByteArrayInputStream (sXML.getBytes()); 231 else 232 oInStream = new ByteArrayInputStream (sXML.getBytes(sEncoding)); 233 234 oOutStream = new ByteArrayOutputStream (4000); 235 236 Properties oProps = new Properties (); 237 238 Enumeration oKeys = req.getPropertyNames(); 239 while (oKeys.hasMoreElements()) { 240 String sKey = (String ) oKeys.nextElement(); 241 oProps.setProperty(sKey, req.getProperty(sKey)); 242 } 244 if (req.getWindowState().equals(WindowState.MINIMIZED)) 245 oProps.setProperty("windowstate", "MINIMIZED"); 246 else 247 oProps.setProperty("windowstate", "NORMAL"); 248 249 StylesheetCache.transform (sTemplatePath, oInStream, oOutStream, oProps); 250 251 if (sEncoding==null) 252 sOutput = oOutStream.toString(); 253 else 254 sOutput = oOutStream.toString("UTF-8"); 255 256 oOutStream.close(); 257 258 oInStream.close(); 259 oInStream = null; 260 261 oFS.writefilestr (sFileDir+File.separator+sCachedFile, sOutput, sEncoding==null ? "ISO8859_1" : sEncoding); 262 } 263 catch (TransformerConfigurationException tce) { 264 if (DebugFile.trace) { 265 DebugFile.writeln("TransformerConfigurationException " + tce.getMessageAndLocation()); 266 try { 267 DebugFile.write("--------------------------------------------------------------------------------\n"); 268 DebugFile.write(FileSystem.readfile(sTemplatePath)); 269 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 270 DebugFile.write(sXML); 271 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 272 } 273 catch (java.io.IOException ignore) { } 274 catch (com.enterprisedt.net.ftp.FTPException ignore) { } 275 276 DebugFile.decIdent(); 277 } 278 throw new PortletException("TransformerConfigurationException " + tce.getMessage(), tce); 279 } 280 catch (TransformerException tex) { 281 if (DebugFile.trace) { 282 DebugFile.writeln("TransformerException " + tex.getMessageAndLocation()); 283 284 try { 285 DebugFile.write("--------------------------------------------------------------------------------\n"); 286 DebugFile.write(FileSystem.readfile(sTemplatePath)); 287 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 288 DebugFile.write(sXML); 289 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 290 } 291 catch (java.io.IOException ignore) { } 292 catch (com.enterprisedt.net.ftp.FTPException ignore) { } 293 294 DebugFile.decIdent(); 295 } 296 throw new PortletException("TransformerException " + tex.getMessage(), tex); 297 } 298 299 if (DebugFile.trace) { 300 DebugFile.decIdent(); 301 DebugFile.writeln("End CalendarTab.render()"); 302 } 303 return sOutput; 304 } 305 306 308 public void render(RenderRequest req, RenderResponse res) 309 throws PortletException, IOException , IllegalStateException { 310 res.getWriter().write(render(req,res.getCharacterEncoding())); 311 } 312 } 313 | Popular Tags |