KickJava   Java API By Example, From Geeks To Geeks.

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


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.Category;
58
59 /**
60  * Categories List for Microsites
61  * @author Sergio Montoro Ten
62  * @version 2.2
63  */

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   // --------------------------------------------------------------------------
76

77   public String JavaDoc render(RenderRequest req, String JavaDoc sEncoding)
78     throws PortletException, IOException JavaDoc, IllegalStateException JavaDoc {
79
80     DBBind dbb;
81     DBSubset dbs, img;
82     JDCConnection con = null;
83     ByteArrayInputStream JavaDoc oInStream;
84     ByteArrayOutputStream JavaDoc oOutStream;
85     String JavaDoc 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 JavaDoc) req.getAttribute("catalog");
101     sTemplatePath = (String JavaDoc) 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 JavaDoc 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 JavaDoc 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 JavaDoc[]{sParentId,sLanguageId});
146       else
147         iCatCount = dbs.load(con, new Object JavaDoc[]{sParentId,sLanguageId}, iOffset);
148
149       iImgCount = img.load(con, new Object JavaDoc[]{sParentId});
150     }
151     catch (SQLException JavaDoc sqle) {
152       if (DebugFile.trace) DebugFile.writeln("SQLException " + sqle.getMessage());
153       if (con!=null) {
154         try { if (!con.isClosed()) con.close("CategoryList"); } catch (SQLException JavaDoc 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 JavaDoc oXML = new StringBuffer JavaDoc(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 JavaDoc 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       } // next (i)
198
oXML.append("</images></category>\n");
199     } // next (c)
200

201     try {
202       con.close("CategoryList");
203       con = null;
204     }
205     catch (SQLException JavaDoc 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 JavaDoc(oXML.toString().getBytes("UTF-8"));
215
216       oOutStream = new ByteArrayOutputStream JavaDoc(40000);
217
218       Properties JavaDoc oProps = new Properties JavaDoc();
219       Enumeration JavaDoc oKeys = req.getPropertyNames();
220       while (oKeys.hasMoreElements()) {
221         String JavaDoc sKey = (String JavaDoc) oKeys.nextElement();
222         oProps.setProperty(sKey, req.getProperty(sKey));
223       } // wend
224

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

278   // --------------------------------------------------------------------------
279

280   public void render(RenderRequest req, RenderResponse res)
281     throws PortletException, IOException JavaDoc, IllegalStateException JavaDoc {
282     res.getWriter().write(render(req,res.getCharacterEncoding()));
283    }
284 }
285
Popular Tags