1 32 33 package com.knowgate.http.portlets; 34 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.Properties ; 42 import java.util.Enumeration ; 43 44 import java.sql.SQLException ; 45 46 import javax.xml.transform.TransformerException ; 47 import javax.xml.transform.TransformerConfigurationException ; 48 49 import javax.portlet.*; 50 51 import com.knowgate.debug.DebugFile; 52 import com.knowgate.jdc.JDCConnection; 53 import com.knowgate.dfs.FileSystem; 54 import com.knowgate.dataobjs.*; 55 import com.knowgate.dataxslt.StylesheetCache; 56 import com.knowgate.misc.Gadgets; 57 import com.knowgate.hipergate.Product; 58 59 64 65 public class ProductList extends GenericPortlet { 66 67 public ProductList() { } 68 69 public ProductList(HipergatePortletConfig oConfig) 70 throws javax.portlet.PortletException { 71 72 init(oConfig); 73 } 74 75 public String render(RenderRequest req, String sEncoding) 76 throws PortletException, IOException , IllegalStateException { 77 78 DBBind dbb; 79 DBSubset dbs, img; 80 JDCConnection con = null; 81 ByteArrayInputStream oInStream; 82 ByteArrayOutputStream oOutStream; 83 String sOutput, sCategoryId, sTemplatePath, sLimit, sOffset, sWrkArGet, sWorkAreaId, sImagePath; 84 int iOffset=0, iLimit=2147483647, iProdCount=0, iImgCount=0; 85 86 if (DebugFile.trace) { 87 DebugFile.writeln("Begin ProductList.render()"); 88 DebugFile.incIdent(); 89 } 90 91 sOffset = req.getParameter("offset"); 92 sLimit = req.getParameter("limit"); 93 94 sCategoryId = (String ) req.getAttribute("category"); 95 sTemplatePath = (String ) req.getAttribute("template"); 96 97 sWorkAreaId = req.getProperty("workarea"); 98 sWrkArGet = req.getProperty("workareasget"); 99 100 if (DebugFile.trace) { 101 DebugFile.writeln("template=" + sTemplatePath); 102 DebugFile.writeln("category=" + sCategoryId); 103 DebugFile.writeln("workarea=" + sWorkAreaId); 104 DebugFile.writeln("workareasget=" + sWrkArGet); 105 } 106 107 try { 108 if (null!=sOffset) 109 iOffset = Integer.parseInt(sOffset); 110 } catch (java.lang.NumberFormatException nfe) { 111 if (DebugFile.trace) DebugFile.decIdent(); 112 throw new PortletException("NumberFormatException parameter offset is not a valid integer value", nfe); 113 } 114 115 try { 116 if (null!=sLimit) 117 iLimit = Integer.parseInt(sLimit); 118 } catch (java.lang.NumberFormatException nfe) { 119 if (DebugFile.trace) DebugFile.decIdent(); 120 throw new PortletException("NumberFormatException parameter limit is not a valid integer value", nfe); 121 } 122 123 try { 124 dbb = (DBBind) getPortletContext().getAttribute("GlobalDBBind"); 125 126 dbs = new DBSubset (DB.k_products + " p," + DB.k_x_cat_objs + " x", 127 "p." + DB.gu_product + ",p." + DB.nm_product + ",p." + DB.de_product + 128 ",p." + DB.pr_list + ",p." + DB.pr_sale + ",p." + DB.id_currency + 129 ",p." + DB.pct_tax_rate + ",p." + DB.is_tax_included + 130 ",p." + DB.dt_start + ",p." + DB.dt_end + ",p." + DB.tag_product + ",p." + DB.id_ref, 131 "p." + DB.gu_product + "=x." + DB.gu_object + " AND x." + DB.id_class + "=15 AND x." + DB.gu_category + "=? ORDER BY x." + DB.od_position, 20); 132 133 con = dbb.getConnection("ProductList"); 134 135 if (null!=sLimit) dbs.setMaxRows(iLimit); 136 137 if (sOffset==null) 138 iProdCount = dbs.load(con, new Object []{sCategoryId}); 139 else 140 iProdCount = dbs.load(con, new Object []{sCategoryId}, iOffset); 141 142 } 143 catch (SQLException sqle) { 144 if (DebugFile.trace) DebugFile.writeln("SQLException " + sqle.getMessage()); 145 if (con!=null) { 146 try { if (!con.isClosed()) con.close("ProductList"); } catch (SQLException ignore) { } 147 } 148 if (DebugFile.trace) DebugFile.decIdent(); 149 throw new PortletException("SQLException " + sqle.getMessage(), sqle); 150 } 151 152 if (DebugFile.trace) DebugFile.writeln(String.valueOf(iProdCount) + " products found"); 153 154 StringBuffer oXML = new StringBuffer (8192); 155 156 oXML.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-stylesheet type=\"text/xsl\"?>\n<products count=\"" + String.valueOf(iProdCount) + "\">\n"); 157 158 Product oCurrentProd = new Product(); 159 160 for (int c=0; c<iProdCount; c++) { 161 oXML.append(" <product>"); 162 oXML.append("<gu_product>"+dbs.getString(0,c)+"</gu_product><nm_product>"+dbs.getString(1,c)+"</nm_product><tr_product><![CDATA["+dbs.getStringNull(2,c,"")+"]]></tr_product><de_product><![CDATA["+dbs.getStringNull(3,c,"")+"]]></de_product>"); 163 164 oCurrentProd.replace(DB.gu_product, dbs.getString(0,c)); 165 166 oXML.append("<images>"); 167 168 try { 169 img = oCurrentProd.getImages(con); 170 iImgCount = img.getRowCount(); 171 172 for (int i=0; i<iImgCount; i++) { 173 oXML.append("<image tp=\"" + img.getString(DB.tp_image,i) + "\"><gu_image>"+img.getString(DB.gu_image,i)+"</gu_image>"); 174 175 sImagePath = img.getString(DB.path_image,i); 176 177 oXML.append("<src_image><![CDATA["+sWrkArGet+"/"+sWorkAreaId+"/apps/Shop/"+sImagePath.substring(sImagePath.indexOf(sWorkAreaId)+43)+"]]></src_image>"); 178 179 oXML.append("<nm_image><![CDATA["+img.getStringNull(DB.nm_image,i,"")+"]]></nm_image>"); 180 if (img.isNull(DB.dm_width,i)) oXML.append("<dm_width></dm_width>"); else oXML.append("<dm_width>" + img.get(DB.dm_width,i).toString() + "</dm_width>"); 181 if (img.isNull(DB.dm_height,i)) oXML.append("<dm_height></dm_height>"); else oXML.append("<dm_height>" + img.get(DB.dm_height,i).toString() + "</dm_height>"); 182 oXML.append("<tl_image>" + img.getStringNull(DB.tl_image,i,"") + "</tl_image></image>"); 183 } } 185 catch (SQLException sqle) { } 186 catch (NullPointerException npe) { } 187 188 oXML.append("</images></product>\n"); 189 } 191 try { 192 con.close("ProductList"); 193 con = null; 194 } 195 catch (SQLException sqle) { 196 if (DebugFile.trace) DebugFile.writeln("SQLException " + sqle.getMessage()); 197 } 198 199 oXML.append("</categories>"); 200 201 try { 202 if (DebugFile.trace) DebugFile.writeln("new ByteArrayInputStream(" + String.valueOf(oXML.length()) + ")"); 203 204 oInStream = new ByteArrayInputStream (oXML.toString().getBytes("UTF-8")); 205 206 oOutStream = new ByteArrayOutputStream (40000); 207 208 Properties oProps = new Properties (); 209 Enumeration oKeys = req.getPropertyNames(); 210 while (oKeys.hasMoreElements()) { 211 String sKey = (String ) oKeys.nextElement(); 212 oProps.setProperty(sKey, req.getProperty(sKey)); 213 } 215 StylesheetCache.transform (sTemplatePath, oInStream, oOutStream, oProps); 216 217 sOutput = oOutStream.toString("UTF-8"); 218 219 oOutStream.close(); 220 221 oInStream.close(); 222 oInStream = null; 223 } 224 catch (TransformerConfigurationException tce) { 225 if (DebugFile.trace) { 226 DebugFile.writeln("TransformerConfigurationException " + tce.getMessageAndLocation()); 227 try { 228 DebugFile.write("--------------------------------------------------------------------------------\n"); 229 DebugFile.write(FileSystem.readfile(sTemplatePath)); 230 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 231 DebugFile.write(oXML.toString()); 232 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 233 } 234 catch (java.io.IOException ignore) { } 235 catch (com.enterprisedt.net.ftp.FTPException ignore) { } 236 237 DebugFile.decIdent(); 238 } 239 throw new PortletException("TransformerConfigurationException " + tce.getMessage(), tce); 240 } 241 catch (TransformerException tex) { 242 if (DebugFile.trace) { 243 DebugFile.writeln("TransformerException " + tex.getMessageAndLocation()); 244 245 try { 246 DebugFile.write("--------------------------------------------------------------------------------\n"); 247 DebugFile.write(FileSystem.readfile(sTemplatePath)); 248 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 249 DebugFile.write(oXML.toString()); 250 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 251 } 252 catch (java.io.IOException ignore) { } 253 catch (com.enterprisedt.net.ftp.FTPException ignore) { } 254 255 DebugFile.decIdent(); 256 } 257 throw new PortletException("TransformerException " + tex.getMessage(), tex); 258 } 259 260 if (DebugFile.trace) { 261 DebugFile.decIdent(); 262 DebugFile.writeln("End ProductList.render()"); 263 } 264 return sOutput; 265 } 267 269 public void render(RenderRequest req, RenderResponse res) 270 throws PortletException, IOException , IllegalStateException { 271 res.getWriter().write(render(req,res.getCharacterEncoding())); 272 } 273 } 274 | Popular Tags |