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.Category; 58 59 64 65 public class CategoryList extends GenericPortlet { 66 67 public CategoryList() { } 68 69 public CategoryList(HipergatePortletConfig oConfig) 70 throws javax.portlet.PortletException { 71 72 init(oConfig); 73 } 74 75 77 public String render(RenderRequest req, String sEncoding) 78 throws PortletException, IOException , IllegalStateException { 79 80 DBBind dbb; 81 DBSubset dbs, img; 82 JDCConnection con = null; 83 ByteArrayInputStream oInStream; 84 ByteArrayOutputStream oOutStream; 85 String sOutput, sParentId, sTemplatePath, sLanguageId, sLimit, sOffset, sWrkArGet, sWorkAreaId; 86 int iOffset=0, iLimit=2147483647, iCatCount=0, iImgCount=0; 87 88 if (DebugFile.trace) { 89 DebugFile.writeln("Begin CategoryList.render()"); 90 DebugFile.incIdent(); 91 } 92 93 sOffset = req.getParameter("offset"); 94 sLimit = req.getParameter("limit"); 95 96 sLanguageId = req.getParameter("language"); 97 98 if (null==sLanguageId) sLanguageId = "es"; 99 100 sParentId = (String ) req.getAttribute("catalog"); 101 sTemplatePath = (String ) req.getAttribute("template"); 102 103 sWorkAreaId = req.getProperty("workarea"); 104 sWrkArGet = req.getProperty("workareasget"); 105 106 if (DebugFile.trace) { 107 DebugFile.writeln("template=" + sTemplatePath); 108 DebugFile.writeln("catalog=" + sParentId); 109 DebugFile.writeln("workarea=" + sWorkAreaId); 110 DebugFile.writeln("workareasget=" + sWrkArGet); 111 } 112 113 try { 114 if (null!=sOffset) 115 iOffset = Integer.parseInt(sOffset); 116 } catch (java.lang.NumberFormatException nfe) { 117 if (DebugFile.trace) DebugFile.decIdent(); 118 throw new PortletException("NumberFormatException parameter offset is not a valid integer value", nfe); 119 } 120 121 try { 122 if (null!=sLimit) 123 iLimit = Integer.parseInt(sLimit); 124 } catch (java.lang.NumberFormatException nfe) { 125 if (DebugFile.trace) DebugFile.decIdent(); 126 throw new PortletException("NumberFormatException parameter limit is not a valid integer value", nfe); 127 } 128 129 try { 130 dbb = (DBBind) getPortletContext().getAttribute("GlobalDBBind"); 131 132 dbs = new DBSubset (DB.v_cat_tree_labels + " l", 133 "l."+DB.gu_category + ",l." + DB.nm_category + ",l." + DB.tr_category + ",l." + DB.de_category, 134 "l."+DB.gu_parent_cat + "=? AND l." + DB.id_language + "=? ORDER BY 3", 10); 135 136 img = new DBSubset (DB.k_images + " i," + DB.k_x_cat_objs + " x," + DB.k_cat_tree + " c", 137 "c." + DB.gu_child_cat + ",i." + DB.gu_image + ",i." + DB.path_image + ",i." + DB.nm_image + ",i." + DB.dm_width + ",i." + DB.dm_height + ",i." + DB.tl_image, 138 "c." + DB.gu_parent_cat + "=? AND i." + DB.tp_image + " LIKE 'category%' AND i." + DB.gu_image + "=x." + DB.gu_object + " AND x." + DB.id_class + "=13 AND x." + DB.gu_category + "=c." + DB.gu_child_cat + " ORDER BY 1", 10); 139 140 con = dbb.getConnection("CategoryList"); 141 142 if (null!=sLimit) dbs.setMaxRows(iLimit); 143 144 if (sOffset==null) 145 iCatCount = dbs.load(con, new Object []{sParentId,sLanguageId}); 146 else 147 iCatCount = dbs.load(con, new Object []{sParentId,sLanguageId}, iOffset); 148 149 iImgCount = img.load(con, new Object []{sParentId}); 150 } 151 catch (SQLException sqle) { 152 if (DebugFile.trace) DebugFile.writeln("SQLException " + sqle.getMessage()); 153 if (con!=null) { 154 try { if (!con.isClosed()) con.close("CategoryList"); } catch (SQLException ignore) { } 155 } 156 if (DebugFile.trace) DebugFile.decIdent(); 157 throw new PortletException("SQLException " + sqle.getMessage(), sqle); 158 } 159 160 if (DebugFile.trace) DebugFile.writeln(String.valueOf(iCatCount) + " categories and " + String.valueOf(iImgCount) + " images found"); 161 162 StringBuffer oXML = new StringBuffer (8192); 163 164 oXML.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-stylesheet type=\"text/xsl\"?>\n<categories count=\"" + String.valueOf(iCatCount) + "\">\n"); 165 166 Category oCurrentCat = new Category(); 167 168 for (int c=0; c<iCatCount; c++) { 169 oXML.append(" <category>"); 170 oXML.append("<gu_category>"+dbs.getString(0,c)+"</gu_category><nm_category>"+dbs.getString(1,c)+"</nm_category><tr_category><![CDATA["+dbs.getStringNull(2,c,"")+"]]></tr_category><de_category><![CDATA["+dbs.getStringNull(3,c,"")+"]]></de_category>"); 171 172 oXML.append("<images>"); 173 174 for (int i=0; i<iImgCount; i++) { 175 int iCompare = dbs.getString(0,c).compareTo(img.getString(0,i)); 176 177 if (iCompare>0) 178 break; 179 else if (iCompare==0) { 180 oXML.append("<image><gu_image>"+img.getString(1,i)+"</gu_image>"); 181 182 oCurrentCat.replace(DB.gu_category, dbs.getString(0,c)); 183 184 try { 185 oXML.append("<src_image>"+sWrkArGet+"/"+sWorkAreaId+"/apps/Shop/"+oCurrentCat.getPath(con)+"/"+img.getStringNull(3,i,"")+"</src_image>"); 186 } 187 catch (SQLException sqle) { 188 if (DebugFile.trace) DebugFile.writeln("SQLException at Category.getPath(" + dbs.getString(0,c) + ") " + sqle.getMessage()); 189 oXML.append("<src_image></src_image>"); 190 } 191 192 oXML.append("<nm_image><![CDATA["+img.getStringNull(3,i,"")+"]]></nm_image>"); 193 if (img.isNull(4,i)) oXML.append("<dm_width></dm_width>"); else oXML.append("<dm_width>" + img.get(4,i).toString() + "</dm_width>"); 194 if (img.isNull(5,i)) oXML.append("<dm_height></dm_height>"); else oXML.append("<dm_height>" + img.get(5,i).toString() + "</dm_height>"); 195 oXML.append("<tl_image>" + img.getStringNull(6,i,"") + "</tl_image></image>"); 196 } 197 } oXML.append("</images></category>\n"); 199 } 201 try { 202 con.close("CategoryList"); 203 con = null; 204 } 205 catch (SQLException sqle) { 206 if (DebugFile.trace) DebugFile.writeln("SQLException " + sqle.getMessage()); 207 } 208 209 oXML.append("</categories>"); 210 211 try { 212 if (DebugFile.trace) DebugFile.writeln("new ByteArrayInputStream(" + String.valueOf(oXML.length()) + ")"); 213 214 oInStream = new ByteArrayInputStream (oXML.toString().getBytes("UTF-8")); 215 216 oOutStream = new ByteArrayOutputStream (40000); 217 218 Properties oProps = new Properties (); 219 Enumeration oKeys = req.getPropertyNames(); 220 while (oKeys.hasMoreElements()) { 221 String sKey = (String ) oKeys.nextElement(); 222 oProps.setProperty(sKey, req.getProperty(sKey)); 223 } 225 StylesheetCache.transform (sTemplatePath, oInStream, oOutStream, oProps); 226 227 sOutput = oOutStream.toString("UTF-8"); 228 229 oOutStream.close(); 230 231 oInStream.close(); 232 oInStream = null; 233 } 234 catch (TransformerConfigurationException tce) { 235 if (DebugFile.trace) { 236 DebugFile.writeln("TransformerConfigurationException " + tce.getMessageAndLocation()); 237 try { 238 DebugFile.write("--------------------------------------------------------------------------------\n"); 239 DebugFile.write(FileSystem.readfile(sTemplatePath)); 240 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 241 DebugFile.write(oXML.toString()); 242 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 243 } 244 catch (java.io.IOException ignore) { } 245 catch (com.enterprisedt.net.ftp.FTPException ignore) { } 246 247 DebugFile.decIdent(); 248 } 249 throw new PortletException("TransformerConfigurationException " + tce.getMessage(), tce); 250 } 251 catch (TransformerException tex) { 252 if (DebugFile.trace) { 253 DebugFile.writeln("TransformerException " + tex.getMessageAndLocation()); 254 255 try { 256 DebugFile.write("--------------------------------------------------------------------------------\n"); 257 DebugFile.write(FileSystem.readfile(sTemplatePath)); 258 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 259 DebugFile.write(oXML.toString()); 260 DebugFile.write("\n--------------------------------------------------------------------------------\n"); 261 } 262 catch (java.io.IOException ignore) { } 263 catch (com.enterprisedt.net.ftp.FTPException ignore) { } 264 265 DebugFile.decIdent(); 266 } 267 throw new PortletException("TransformerException " + tex.getMessage(), tex); 268 } 269 270 if (DebugFile.trace) { 271 DebugFile.decIdent(); 272 DebugFile.writeln("End CategoryList.render()"); 273 } 274 275 return sOutput; 276 } 278 280 public void render(RenderRequest req, RenderResponse res) 281 throws PortletException, IOException , IllegalStateException { 282 res.getWriter().write(render(req,res.getCharacterEncoding())); 283 } 284 } 285 | Popular Tags |