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 67 68 public class OportunitiesTab extends GenericPortlet { 69 public OportunitiesTab() { } 70 71 public OportunitiesTab(HipergatePortletConfig oConfig) 72 throws javax.portlet.PortletException { 73 74 init(oConfig); 75 } 76 77 79 public String render(RenderRequest req, String sEncoding) 80 throws PortletException, IOException , IllegalStateException { 81 82 ByteArrayInputStream oInStream; 83 ByteArrayOutputStream oOutStream; 84 85 if (DebugFile.trace) { 86 DebugFile.writeln("Begin OportunitiesTab.render()"); 87 DebugFile.incIdent(); 88 } 89 90 final int iMaxRecent = 8; 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 = "oportunitiestab_" + 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 if (oCached.lastModified()>oDtModified.getTime()) { 124 sOutput = new String (oFS.readfile(sFileDir+File.separator+sCachedFile, sEncoding==null ? "ISO8859_1" : sEncoding)); 125 126 if (DebugFile.trace) { 127 DebugFile.writeln("cache hit " + sFileDir+File.separator+sCachedFile); 128 DebugFile.decIdent(); 129 DebugFile.writeln("End OportunitiesTab.render()"); 130 } 131 132 return sOutput; 133 } 134 } 135 catch (Exception xcpt) { 136 DebugFile.writeln(xcpt.getClass().getName() + " " + xcpt.getMessage()); 137 } 138 } 139 140 String sXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet type=\"text/xsl\"?>"; 141 142 int iCalls = 0; 143 144 if (req.getWindowState().equals(WindowState.MINIMIZED)) { 145 sXML += "<oportunities/>"; 146 } 147 else { 148 149 DBBind oDBB = (DBBind) getPortletContext().getAttribute("GlobalDBBind"); 150 151 JDCConnection oCon = null; 152 PreparedStatement oStm = null; 153 String sSQL, sInterval; 154 155 try { 156 int iOprtnCount = 0; 157 StringBuffer oXML = new StringBuffer (); 158 String [][] aOprtns = new String [6][iMaxRecent]; 159 160 oCon = oDBB.getConnection("OportunitiesTab"); 161 162 if (oCon.getDataBaseProduct()==JDCConnection.DBMS_POSTGRESQL) 163 sInterval = "interval '10 years'"; 164 else 165 sInterval = "3650"; 166 167 sSQL = "(SELECT "+ 168 DB.gu_oportunity+","+DB.tl_oportunity+","+DB.gu_company+","+DB.gu_contact+","+DB.tx_company+","+DB.tx_contact+","+DB.dt_modified+","+DB.dt_next_action+","+ 169 DBBind.Functions.GETDATE+"-"+DB.dt_modified+ " AS nu_elapsed FROM "+DB.k_oportunities+" WHERE "+DB.id_status+" NOT IN ('PERDIDA','GANADA','ABANDONADA') AND "+ 170 DB.dt_modified+" IS NOT NULL AND " + DB.gu_workarea+"=? AND "+DB.gu_writer+"=? UNION SELECT "+ 171 DB.gu_oportunity+","+DB.tl_oportunity+","+DB.gu_company+","+DB.gu_contact+","+DB.tx_company+","+DB.tx_contact+","+DB.dt_modified+","+DB.dt_next_action+","+ 172 DBBind.Functions.ISNULL+"("+DB.dt_next_action+","+DBBind.Functions.GETDATE+"+" + sInterval + ")-"+DBBind.Functions.GETDATE+" AS nu_elapsed "+ 173 "FROM "+DB.k_oportunities+" WHERE "+DB.id_status+" NOT IN ('PERDIDA','GANADA','ABANDONADA') AND "+ 174 DB.gu_workarea+"=? AND "+DB.gu_writer+"=?) ORDER BY nu_elapsed"; 175 176 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement("+sSQL+")"); 177 178 oStm = oCon.prepareStatement(sSQL,ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 179 180 oStm.setString (1,sWorkAreaId); 181 oStm.setString (2,sUserId); 182 oStm.setString (3,sWorkAreaId); 183 oStm.setString (4,sUserId); 184 185 if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeQuery()"); 186 187 ResultSet oRSet = oStm.executeQuery(); 188 189 while (oRSet.next() && iOprtnCount<iMaxRecent) { 190 191 boolean bListed = false; 192 for (int n=0; n<iOprtnCount && !bListed; n++) 193 bListed = oRSet.getString(1).equals(aOprtns[0][n]); 194 195 if (!bListed) { 196 aOprtns[0][iOprtnCount] = oRSet.getString(1); 197 aOprtns[1][iOprtnCount] = oRSet.getString(2); 198 aOprtns[2][iOprtnCount] = oRSet.getString(3); 199 aOprtns[3][iOprtnCount] = oRSet.getString(4); 200 aOprtns[4][iOprtnCount] = oRSet.getString(5); 201 aOprtns[5][iOprtnCount] = oRSet.getString(6); 202 203 iOprtnCount++; 204 } 205 } 207 oRSet.close(); 208 oRSet = null; 209 210 oStm.close(); 211 oStm = null; 212 213 oCon.close("OportunitiesTab"); 214 oCon = null; 215 216 for (int o=0; o<iOprtnCount; o++) 217 for (int f=0; f<6; f++) 218 if (aOprtns[f][o]==null) aOprtns[f][o]=""; 219 220 oXML.append("<oportunities>\n"); 221 for (int q=0; q<iOprtnCount; q++) { 222 oXML.append("<oportunity>\n"); 223 224 oXML.append("<gu_oportunity>"+aOprtns[0][q]+"</gu_oportunity>"); 225 oXML.append("<tl_oportunity><![CDATA["+aOprtns[1][q]+"]]></tl_oportunity>"); 226 oXML.append("<gu_company>"+aOprtns[2][q]+"</gu_company>"); 227 oXML.append("<gu_contact>"+aOprtns[3][q]+"</gu_contact>"); 228 oXML.append("<tx_company><![CDATA["+aOprtns[4][q]+"]]></tx_company>"); 229 oXML.append("<tx_contact><![CDATA["+aOprtns[5][q]+"]]></tx_contact>"); 230 oXML.append("<tx_contact_esc><![CDATA["+Gadgets.URLEncode(aOprtns[5][q])+"]]></tx_contact_esc>"); 231 oXML.append("<where><![CDATA["+Gadgets.URLEncode(" AND gu_contact='" + aOprtns[3][q] + "'")+"]]></where>"); 232 233 oXML.append("</oportunity>\n"); 234 } 235 236 oXML.append("</oportunities>"); 237 238 sXML += oXML.toString(); 239 } 240 catch (SQLException e) { 241 sXML += "<oportunities/>"; 242 243 if (DebugFile.trace) DebugFile.writeln("SQLException " + e.getMessage()); 244 try { 245 if (null != oStm) 246 oStm.close(); 247 } catch (SQLException ignore) { } 248 249 try { 250 if (null != oCon) 251 if (!oCon.isClosed()) 252 oCon.close("OportunitiesTab"); 253 } catch (SQLException ignore) { } 254 } 255 } 256 257 try { 258 if (DebugFile.trace) DebugFile.writeln("new ByteArrayInputStream(" + String.valueOf(sXML.length()) + ")"); 259 260 if (sEncoding==null) 261 oInStream = new ByteArrayInputStream (sXML.getBytes()); 262 else 263 oInStream = new ByteArrayInputStream (sXML.getBytes(sEncoding)); 264 265 oOutStream = new ByteArrayOutputStream (4000); 266 267 Properties oProps = new Properties (); 268 269 Enumeration oKeys = req.getPropertyNames(); 270 while (oKeys.hasMoreElements()) { 271 String sKey = (String ) oKeys.nextElement(); 272 oProps.setProperty(sKey, req.getProperty(sKey)); 273 } 275 if (req.getWindowState().equals(WindowState.MINIMIZED)) 276 oProps.setProperty("windowstate", "MINIMIZED"); 277 else 278 oProps.setProperty("windowstate", "NORMAL"); 279 280 StylesheetCache.transform (sTemplatePath, oInStream, oOutStream, oProps); 281 282 if (sEncoding==null) 283 sOutput = oOutStream.toString(); 284 else 285 sOutput = oOutStream.toString("UTF-8"); 286 287 oOutStream.close(); 288 289 oInStream.close(); 290 oInStream = null; 291 292 oFS.writefilestr (sFileDir+File.separator+sCachedFile, sOutput, sEncoding==null ? "ISO8859_1" : sEncoding); 293 } 294 catch (TransformerConfigurationException tce) { 295 if (DebugFile.trace) { 296 DebugFile.writeln("TransformerConfigurationException " + tce.getMessageAndLocation()); 297 try { 298 DebugFile.write("--------------------------------------------------------------------------------\n"); 299 DebugFile.write(FileSystem.readfile(sTemplatePath)); 300 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 301 DebugFile.write(sXML); 302 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 303 } 304 catch (java.io.IOException ignore) { } 305 catch (com.enterprisedt.net.ftp.FTPException ignore) { } 306 307 DebugFile.decIdent(); 308 } 309 throw new PortletException("TransformerConfigurationException " + tce.getMessage(), tce); 310 } 311 catch (TransformerException tex) { 312 if (DebugFile.trace) { 313 DebugFile.writeln("TransformerException " + tex.getMessageAndLocation()); 314 315 try { 316 DebugFile.write("--------------------------------------------------------------------------------\n"); 317 DebugFile.write(FileSystem.readfile(sTemplatePath)); 318 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 319 DebugFile.write(sXML); 320 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 321 } 322 catch (java.io.IOException ignore) { } 323 catch (com.enterprisedt.net.ftp.FTPException ignore) { } 324 325 DebugFile.decIdent(); 326 } 327 throw new PortletException("TransformerException " + tex.getMessage(), tex); 328 } 329 330 if (DebugFile.trace) { 331 DebugFile.decIdent(); 332 DebugFile.writeln("End OportunitiesTab.render()"); 333 } 334 return sOutput; 335 } 336 337 339 public void render(RenderRequest req, RenderResponse res) 340 throws PortletException, IOException , IllegalStateException { 341 res.getWriter().write(render(req,res.getCharacterEncoding())); 342 } 343 } 344 | Popular Tags |