001: /*
002: Copyright (C) 2003-2006 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.http.portlets;
034:
035: import java.io.IOException;
036: import java.io.ByteArrayOutputStream;
037: import java.io.ByteArrayInputStream;
038: import java.io.OutputStreamWriter;
039: import java.io.PrintWriter;
040:
041: import java.util.Properties;
042: import java.util.Enumeration;
043:
044: import java.sql.SQLException;
045:
046: import javax.xml.transform.TransformerException;
047: import javax.xml.transform.TransformerConfigurationException;
048:
049: import javax.portlet.*;
050:
051: import com.knowgate.debug.DebugFile;
052: import com.knowgate.jdc.JDCConnection;
053: import com.knowgate.dfs.FileSystem;
054: import com.knowgate.dataobjs.*;
055: import com.knowgate.dataxslt.StylesheetCache;
056: import com.knowgate.misc.Gadgets;
057: import com.knowgate.hipergate.Product;
058:
059: /**
060: * Product List for Microsites
061: * @author Sergio Montoro Ten
062: * @version 1.0
063: */
064:
065: public class ProductList extends GenericPortlet {
066:
067: public ProductList() {
068: }
069:
070: public ProductList(HipergatePortletConfig oConfig)
071: throws javax.portlet.PortletException {
072:
073: init(oConfig);
074: }
075:
076: public String render(RenderRequest req, String sEncoding)
077: throws PortletException, IOException, IllegalStateException {
078:
079: DBBind dbb;
080: DBSubset dbs, img;
081: JDCConnection con = null;
082: ByteArrayInputStream oInStream;
083: ByteArrayOutputStream oOutStream;
084: String sOutput, sCategoryId, sTemplatePath, sLimit, sOffset, sWrkArGet, sWorkAreaId, sImagePath;
085: int iOffset = 0, iLimit = 2147483647, iProdCount = 0, iImgCount = 0;
086:
087: if (DebugFile.trace) {
088: DebugFile.writeln("Begin ProductList.render()");
089: DebugFile.incIdent();
090: }
091:
092: sOffset = req.getParameter("offset");
093: sLimit = req.getParameter("limit");
094:
095: sCategoryId = (String) req.getAttribute("category");
096: sTemplatePath = (String) req.getAttribute("template");
097:
098: sWorkAreaId = req.getProperty("workarea");
099: sWrkArGet = req.getProperty("workareasget");
100:
101: if (DebugFile.trace) {
102: DebugFile.writeln("template=" + sTemplatePath);
103: DebugFile.writeln("category=" + sCategoryId);
104: DebugFile.writeln("workarea=" + sWorkAreaId);
105: DebugFile.writeln("workareasget=" + sWrkArGet);
106: }
107:
108: try {
109: if (null != sOffset)
110: iOffset = Integer.parseInt(sOffset);
111: } catch (java.lang.NumberFormatException nfe) {
112: if (DebugFile.trace)
113: DebugFile.decIdent();
114: throw new PortletException(
115: "NumberFormatException parameter offset is not a valid integer value",
116: nfe);
117: }
118:
119: try {
120: if (null != sLimit)
121: iLimit = Integer.parseInt(sLimit);
122: } catch (java.lang.NumberFormatException nfe) {
123: if (DebugFile.trace)
124: DebugFile.decIdent();
125: throw new PortletException(
126: "NumberFormatException parameter limit is not a valid integer value",
127: nfe);
128: }
129:
130: try {
131: dbb = (DBBind) getPortletContext().getAttribute(
132: "GlobalDBBind");
133:
134: dbs = new DBSubset(DB.k_products + " p," + DB.k_x_cat_objs
135: + " x", "p." + DB.gu_product + ",p."
136: + DB.nm_product + ",p." + DB.de_product + ",p."
137: + DB.pr_list + ",p." + DB.pr_sale + ",p."
138: + DB.id_currency + ",p." + DB.pct_tax_rate + ",p."
139: + DB.is_tax_included + ",p." + DB.dt_start + ",p."
140: + DB.dt_end + ",p." + DB.tag_product + ",p."
141: + DB.id_ref, "p." + DB.gu_product + "=x."
142: + DB.gu_object + " AND x." + DB.id_class
143: + "=15 AND x." + DB.gu_category + "=? ORDER BY x."
144: + DB.od_position, 20);
145:
146: con = dbb.getConnection("ProductList");
147:
148: if (null != sLimit)
149: dbs.setMaxRows(iLimit);
150:
151: if (sOffset == null)
152: iProdCount = dbs
153: .load(con, new Object[] { sCategoryId });
154: else
155: iProdCount = dbs.load(con,
156: new Object[] { sCategoryId }, iOffset);
157:
158: } catch (SQLException sqle) {
159: if (DebugFile.trace)
160: DebugFile.writeln("SQLException " + sqle.getMessage());
161: if (con != null) {
162: try {
163: if (!con.isClosed())
164: con.close("ProductList");
165: } catch (SQLException ignore) {
166: }
167: }
168: if (DebugFile.trace)
169: DebugFile.decIdent();
170: throw new PortletException("SQLException "
171: + sqle.getMessage(), sqle);
172: }
173:
174: if (DebugFile.trace)
175: DebugFile.writeln(String.valueOf(iProdCount)
176: + " products found");
177:
178: StringBuffer oXML = new StringBuffer(8192);
179:
180: oXML
181: .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-stylesheet type=\"text/xsl\"?>\n<products count=\""
182: + String.valueOf(iProdCount) + "\">\n");
183:
184: Product oCurrentProd = new Product();
185:
186: for (int c = 0; c < iProdCount; c++) {
187: oXML.append(" <product>");
188: oXML.append("<gu_product>" + dbs.getString(0, c)
189: + "</gu_product><nm_product>" + dbs.getString(1, c)
190: + "</nm_product><tr_product><![CDATA["
191: + dbs.getStringNull(2, c, "")
192: + "]]></tr_product><de_product><![CDATA["
193: + dbs.getStringNull(3, c, "") + "]]></de_product>");
194:
195: oCurrentProd.replace(DB.gu_product, dbs.getString(0, c));
196:
197: oXML.append("<images>");
198:
199: try {
200: img = oCurrentProd.getImages(con);
201: iImgCount = img.getRowCount();
202:
203: for (int i = 0; i < iImgCount; i++) {
204: oXML.append("<image tp=\""
205: + img.getString(DB.tp_image, i)
206: + "\"><gu_image>"
207: + img.getString(DB.gu_image, i)
208: + "</gu_image>");
209:
210: sImagePath = img.getString(DB.path_image, i);
211:
212: oXML.append("<src_image><![CDATA["
213: + sWrkArGet
214: + "/"
215: + sWorkAreaId
216: + "/apps/Shop/"
217: + sImagePath.substring(sImagePath
218: .indexOf(sWorkAreaId) + 43)
219: + "]]></src_image>");
220:
221: oXML.append("<nm_image><![CDATA["
222: + img.getStringNull(DB.nm_image, i, "")
223: + "]]></nm_image>");
224: if (img.isNull(DB.dm_width, i))
225: oXML.append("<dm_width></dm_width>");
226: else
227: oXML.append("<dm_width>"
228: + img.get(DB.dm_width, i).toString()
229: + "</dm_width>");
230: if (img.isNull(DB.dm_height, i))
231: oXML.append("<dm_height></dm_height>");
232: else
233: oXML.append("<dm_height>"
234: + img.get(DB.dm_height, i).toString()
235: + "</dm_height>");
236: oXML.append("<tl_image>"
237: + img.getStringNull(DB.tl_image, i, "")
238: + "</tl_image></image>");
239: } // next (i)
240: } catch (SQLException sqle) {
241: } catch (NullPointerException npe) {
242: }
243:
244: oXML.append("</images></product>\n");
245: } // next (c)
246:
247: try {
248: con.close("ProductList");
249: con = null;
250: } catch (SQLException sqle) {
251: if (DebugFile.trace)
252: DebugFile.writeln("SQLException " + sqle.getMessage());
253: }
254:
255: oXML.append("</categories>");
256:
257: try {
258: if (DebugFile.trace)
259: DebugFile.writeln("new ByteArrayInputStream("
260: + String.valueOf(oXML.length()) + ")");
261:
262: oInStream = new ByteArrayInputStream(oXML.toString()
263: .getBytes("UTF-8"));
264:
265: oOutStream = new ByteArrayOutputStream(40000);
266:
267: Properties oProps = new Properties();
268: Enumeration oKeys = req.getPropertyNames();
269: while (oKeys.hasMoreElements()) {
270: String sKey = (String) oKeys.nextElement();
271: oProps.setProperty(sKey, req.getProperty(sKey));
272: } // wend
273:
274: StylesheetCache.transform(sTemplatePath, oInStream,
275: oOutStream, oProps);
276:
277: sOutput = oOutStream.toString("UTF-8");
278:
279: oOutStream.close();
280:
281: oInStream.close();
282: oInStream = null;
283: } catch (TransformerConfigurationException tce) {
284: if (DebugFile.trace) {
285: DebugFile.writeln("TransformerConfigurationException "
286: + tce.getMessageAndLocation());
287: try {
288: DebugFile
289: .write("--------------------------------------------------------------------------------\n");
290: DebugFile.write(FileSystem.readfile(sTemplatePath));
291: DebugFile
292: .write("\n--------------------------------------------------------------------------------\n");
293: DebugFile.write(oXML.toString());
294: DebugFile
295: .write("\n--------------------------------------------------------------------------------\n");
296: } catch (java.io.IOException ignore) {
297: } catch (com.enterprisedt.net.ftp.FTPException ignore) {
298: }
299:
300: DebugFile.decIdent();
301: }
302: throw new PortletException(
303: "TransformerConfigurationException "
304: + tce.getMessage(), tce);
305: } catch (TransformerException tex) {
306: if (DebugFile.trace) {
307: DebugFile.writeln("TransformerException "
308: + tex.getMessageAndLocation());
309:
310: try {
311: DebugFile
312: .write("--------------------------------------------------------------------------------\n");
313: DebugFile.write(FileSystem.readfile(sTemplatePath));
314: DebugFile
315: .write("\n--------------------------------------------------------------------------------\n");
316: DebugFile.write(oXML.toString());
317: DebugFile
318: .write("\n--------------------------------------------------------------------------------\n");
319: } catch (java.io.IOException ignore) {
320: } catch (com.enterprisedt.net.ftp.FTPException ignore) {
321: }
322:
323: DebugFile.decIdent();
324: }
325: throw new PortletException("TransformerException "
326: + tex.getMessage(), tex);
327: }
328:
329: if (DebugFile.trace) {
330: DebugFile.decIdent();
331: DebugFile.writeln("End ProductList.render()");
332: }
333: return sOutput;
334: } // render
335:
336: // --------------------------------------------------------------------------
337:
338: public void render(RenderRequest req, RenderResponse res)
339: throws PortletException, IOException, IllegalStateException {
340: res.getWriter().write(render(req, res.getCharacterEncoding()));
341: }
342: }
|