KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > http > portlets > ProductList


1 /*
2   Copyright (C) 2003-2006 Know Gate S.L. All rights reserved.
3                            C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.http.portlets;
34
35 import java.io.IOException JavaDoc;
36 import java.io.ByteArrayOutputStream JavaDoc;
37 import java.io.ByteArrayInputStream JavaDoc;
38 import java.io.OutputStreamWriter JavaDoc;
39 import java.io.PrintWriter JavaDoc;
40
41 import java.util.Properties JavaDoc;
42 import java.util.Enumeration JavaDoc;
43
44 import java.sql.SQLException JavaDoc;
45
46 import javax.xml.transform.TransformerException JavaDoc;
47 import javax.xml.transform.TransformerConfigurationException JavaDoc;
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 /**
60  * Product List for Microsites
61  * @author Sergio Montoro Ten
62  * @version 1.0
63  */

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 JavaDoc render(RenderRequest req, String JavaDoc sEncoding)
76     throws PortletException, IOException JavaDoc, IllegalStateException JavaDoc {
77
78     DBBind dbb;
79     DBSubset dbs, img;
80     JDCConnection con = null;
81     ByteArrayInputStream JavaDoc oInStream;
82     ByteArrayOutputStream JavaDoc oOutStream;
83     String JavaDoc 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 JavaDoc) req.getAttribute("category");
95     sTemplatePath = (String JavaDoc) 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 JavaDoc 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 JavaDoc 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 JavaDoc[]{sCategoryId});
139       else
140         iProdCount = dbs.load(con, new Object JavaDoc[]{sCategoryId}, iOffset);
141
142     }
143     catch (SQLException JavaDoc sqle) {
144       if (DebugFile.trace) DebugFile.writeln("SQLException " + sqle.getMessage());
145       if (con!=null) {
146         try { if (!con.isClosed()) con.close("ProductList"); } catch (SQLException JavaDoc 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 JavaDoc oXML = new StringBuffer JavaDoc(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         } // next (i)
184
}
185       catch (SQLException JavaDoc sqle) { }
186       catch (NullPointerException JavaDoc npe) { }
187
188       oXML.append("</images></product>\n");
189     } // next (c)
190

191     try {
192       con.close("ProductList");
193       con = null;
194     }
195     catch (SQLException JavaDoc 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 JavaDoc(oXML.toString().getBytes("UTF-8"));
205
206       oOutStream = new ByteArrayOutputStream JavaDoc(40000);
207
208       Properties JavaDoc oProps = new Properties JavaDoc();
209       Enumeration JavaDoc oKeys = req.getPropertyNames();
210       while (oKeys.hasMoreElements()) {
211         String JavaDoc sKey = (String JavaDoc) oKeys.nextElement();
212         oProps.setProperty(sKey, req.getProperty(sKey));
213       } // wend
214

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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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   } // render
266

267   // --------------------------------------------------------------------------
268

269   public void render(RenderRequest req, RenderResponse res)
270     throws PortletException, IOException JavaDoc, IllegalStateException JavaDoc {
271     res.getWriter().write(render(req,res.getCharacterEncoding()));
272     }
273 }
274
Popular Tags