KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > hipergate > Categories


1 /*
2   Copyright (C) 2003 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.hipergate;
34
35 import java.sql.SQLException JavaDoc;
36 import java.sql.PreparedStatement JavaDoc;
37 import java.sql.ResultSet JavaDoc;
38
39 import com.knowgate.debug.DebugFile;
40 import com.knowgate.jdc.JDCConnection;
41 import com.knowgate.dataobjs.DB;
42 import com.knowgate.dataobjs.DBBind;
43 import com.knowgate.dataobjs.DBSubset;
44
45 /**
46  * Singleton manager for Categories Tree
47  * @author Sergio Montoro Ten
48  * @version 2.1
49  */

50 public class Categories {
51
52   public Categories() {
53     iRootsCount = -1;
54     sRootsNamedTables = DB.k_categories + " c, " + DB.k_cat_labels + " n," + DB.k_cat_root + " r";
55     sRootsNamedFields = "c." + DB.gu_category + ", c." + DB.nm_category + ", " + DBBind.Functions.ISNULL + "(n." + DB.tr_category + ",''), c." + DB.nm_icon + ", c." + DB.nm_icon2;
56     sRootsNamedFilter = "n." + DB.gu_category + "=c." + DB.gu_category + " AND c." + DB.gu_category + "=r." + DB.gu_category + " AND n." + DB.id_language + "=?";
57
58     sChildNamedTables = DB.v_cat_tree_labels ;
59     sChildNamedFields = DB.gu_category + "," + DB.nm_category + "," + DB.tr_category + ", " + DB.nm_icon + ", " + DB.nm_icon2;
60     sChildNamedFilter = DB.gu_parent_cat + "=? AND (" + DB.id_language + "=? OR " + DB.id_language + " IS NULL)";
61   }
62
63   //----------------------------------------------------------------------------
64

65   /**
66    * Clear root categories cache.
67    * Root category names are loaded once and then cached into a static variable.
68    * Use this method for forcing reload of categories from database on next call
69    * to getRoots() or getRootsNamed().
70    */

71   public void clearCache() {
72     oRootsLoaded = false;
73   }
74
75   // ----------------------------------------------------------
76

77   /**
78    * <p>Expand Category Childs into k_cat_expand table</p>
79    * @param oConn Database Connection
80    * @param sRootCategoryId GUID of Category to expand.
81    * @throws SQLException
82    */

83   public static void expand (JDCConnection oConn, String JavaDoc sRootCategoryId) throws SQLException JavaDoc {
84     Category oRoot = new Category(sRootCategoryId);
85     oRoot.expand(oConn);
86   }
87
88   //----------------------------------------------------------------------------
89

90   /**
91    * <p>Get root category for a given Domain</p>
92    * The root Category for a Domain will be the one such that nm_category=nm_domain
93    * @param oConn Database Connection
94    * @param iDomain Domain Numeric Identifier
95    * @return Category GUID or <b>null</b> if root Category for Domain was not found.
96    * @throws SQLException
97    */

98   public Category forDomain(JDCConnection oConn, int iDomain) throws SQLException JavaDoc {
99     PreparedStatement JavaDoc oStmt;
100     ResultSet JavaDoc oRSet;
101     Category oRetVal;
102
103     if (DebugFile.trace) {
104       DebugFile.writeln("Begin Categories.forDomain([Connection], " + String.valueOf(iDomain) + ")");
105       DebugFile.incIdent();
106       DebugFile.writeln("Connection.prepareStatement(SELECT " + DB.gu_category + " FROM " + DB.k_categories + " WHERE " + DB.nm_category + "=(SELECT " + DB.nm_domain + " FROM " + DB.k_domains + " WHERE " + DB.id_domain + "=?)");
107     }
108
109     oStmt = oConn.prepareStatement("SELECT " + DB.gu_category + " FROM " + DB.k_categories + " WHERE " + DB.nm_category + "=(SELECT " + DB.nm_domain + " FROM " + DB.k_domains + " WHERE " + DB.id_domain + "=?)", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
110     oStmt.setInt(1, iDomain);
111
112     oRSet = oStmt.executeQuery();
113
114     if (oRSet.next())
115       oRetVal = new Category(oConn, oRSet.getString(1));
116     else
117       oRetVal = null;
118
119     oRSet.close();
120     oStmt.close();
121
122     if (DebugFile.trace) {
123       DebugFile.decIdent();
124       DebugFile.writeln("End Categories.forDomain() : " + (oRetVal==null ? "null" : "[Category]"));
125     }
126
127     return oRetVal;
128   } // forDomain()
129

130   //----------------------------------------------------------------------------
131

132   /**
133    * <p>Get root categories as a DBSubset.</p>
134    * Root categories are those present at k_cat_root table.<br>
135    * It is recommended to use this criteria instead of seeking those categories
136    * not present as childs at k_cat_tree. Selecting from k_cat_root is much faster
137    * than scanning the k_cat_tree table.
138    * @param oConn Database Connection
139    * @return A single column DBSubset containing th GUID of root categories.
140    * @throws SQLException
141    */

142   public DBSubset getRoots(JDCConnection oConn) throws SQLException JavaDoc {
143
144     if (DebugFile.trace) {
145       DebugFile.writeln("Begin Categories.getRoots([Connection])");
146       DebugFile.incIdent();
147     }
148
149     oRoots = new DBSubset(DB.k_cat_root,DB.gu_category,"",10);
150     iRootsCount = oRoots.load (oConn);
151
152     if (DebugFile.trace) {
153       DebugFile.decIdent();
154       DebugFile.writeln("End Categories.getRoots()");
155     }
156
157     return oRoots;
158   } // getRoots
159

160   //----------------------------------------------------------------------------
161

162   /**
163    * Get root categories count.
164    * @throws IllegalStateException If getRoots() or getRootsNamed() have not
165    * been called prior to getRootsCount()
166    */

167   public int getRootsCount() throws IllegalStateException JavaDoc {
168     if (-1==iRootsCount) throw new IllegalStateException JavaDoc("Must call getRoots() or getRootsNamed() prior to getRootsCount()");
169
170     return iRootsCount;
171   }
172
173   //----------------------------------------------------------------------------
174

175   /**
176    * <p>Get Root Caetgories and their names as a DBSubset</p>
177    * Categories not having any translation at k_cat_labels will not be retrieved.<br>
178    * Root Category Names are loaded once and then cached internally as a static object.<br>
179    * Use clearCahce() method for refreshing root categories from database.
180    * @param oConn Database Connection
181    * @param sLanguage Language for category label retrieval.
182    * @param iOrderBy Column for order by { ORDER_BY_NONE, ORDER_BY_NEUTRAL_NAME, ORDER_BY_LOCALE_NAME }
183    * @return A DBSubset with the following columns:<br>
184    * <table border=1 cellpadding=4>
185    * <tr><td><b>gu_category</b></td><td><b>nm_category</b></td><td><b>tr_category</b></td><td><b>nm_icon</b></td><td><b>nm_icon2</b></td></tr>
186    * <tr><td>Category GUID</td><td>Category Internal Name</td><td>Category Translated Label</td><td>Icon for Closed Folder</td><td>Icon for Opened Folder</td></tr>
187    * </table>
188    * @throws SQLException
189    */

190   public DBSubset getRootsNamed(JDCConnection oConn, String JavaDoc sLanguage, int iOrderBy) throws SQLException JavaDoc {
191
192     sRootsNamedFields = "c." + DB.gu_category + ", c." + DB.nm_category + ", " + DBBind.Functions.ISNULL + "(n." + DB.tr_category + ",''), c." + DB.nm_icon + ", c." + DB.nm_icon2;
193
194     if (DebugFile.trace) {
195       DebugFile.writeln("Begin Categories.getRootsNamed([Connection], " + sLanguage + String.valueOf(iOrderBy) + ")");
196       DebugFile.incIdent();
197     }
198
199     if (!oRootsLoaded) {
200       Object JavaDoc[] aLang = { sLanguage };
201
202       if (iOrderBy>0)
203         oRootsNamed = new DBSubset (sRootsNamedTables, sRootsNamedFields, sRootsNamedFilter + " ORDER BY " + iOrderBy, 16);
204       else
205         oRootsNamed = new DBSubset (sRootsNamedTables, sRootsNamedFields, sRootsNamedFilter, 16);
206
207       iRootsCount = oRootsNamed.load(oConn, aLang);
208       oRootsLoaded = true;
209     }
210
211     if (DebugFile.trace) {
212       DebugFile.decIdent();
213       DebugFile.writeln("End Categories.getRootsNamed()");
214     }
215
216     return oRootsNamed;
217   } // getRootsNamed()
218

219   //----------------------------------------------------------------------------
220

221   /**
222    * <p>Get first level childs for a given category.</p>
223    * Categories not having any translation at k_cat_labels will not be retrieved.
224    * @param oConn Database Connection
225    * @param idParent Parent Category
226    * @param sLanguage Language for label retrieval
227    * @param iOrderBy Column for order by { ORDER_BY_NONE, ORDER_BY_NEUTRAL_NAME, ORDER_BY_LOCALE_NAME }
228    * @return A DBSubset with the following columns:<br>
229    * <table border=1 cellpadding=4>
230    * <tr><td><b>gu_category</b></td><td><b>nm_category</b></td><td><b>tr_category</b></td><td><b>nm_icon</b></td><td><b>nm_icon2</b></td></tr>
231    * <tr><td>Category GUID</td><td>Category Internal Name</td><td>Category Translated Label</td><td>Icon for Closed Folder</td><td>Icon for Opened Folder</td></tr>
232    * </table>
233    * @throws SQLException
234    */

235   public DBSubset getChildsNamed(JDCConnection oConn, String JavaDoc idParent, String JavaDoc sLanguage, int iOrderBy) throws SQLException JavaDoc {
236
237     long lElapsed = 0;
238
239     if (DebugFile.trace) {
240       lElapsed = System.currentTimeMillis();
241       DebugFile.writeln("Begin Categories.getChildsNamed([Connection], " + (idParent==null ? "null" : idParent) + "," + (sLanguage==null ? "null" : sLanguage) + "," + String.valueOf(iOrderBy) + ")");
242       DebugFile.incIdent();
243     }
244
245     Object JavaDoc[] aParams = { idParent, sLanguage, idParent, idParent, sLanguage };
246     DBSubset oChilds;
247
248     if (iOrderBy>0)
249       oChilds = new DBSubset (sChildNamedTables, sChildNamedFields, sChildNamedFilter +
250                               " UNION SELECT " +
251                               "c." + DB.gu_category + ",c." + DB.nm_category + ",c." + DB.nm_category + "," +
252                               "c." + DB.nm_icon + ",c." + DB.nm_icon2 + " FROM " + DB.k_categories + " c, " +
253                               DB.k_cat_tree + " t WHERE c." + DB.gu_category + "=t." + DB.gu_child_cat + " AND " +
254                               "t." + DB.gu_parent_cat + "=? AND c." + DB.gu_category + " NOT IN " +
255                               "(SELECT " + DB.gu_category + " FROM " + sChildNamedTables + " WHERE " + sChildNamedFilter + ") ORDER BY " + iOrderBy, 32);
256     else
257       oChilds = new DBSubset (sChildNamedTables, sChildNamedFields, sChildNamedFilter +
258                               " UNION SELECT " +
259                               "c." + DB.gu_category + ",c." + DB.nm_category + ",c." + DB.nm_category + ", " +
260                               "c." + DB.nm_icon + ",c." + DB.nm_icon2 + " FROM " + DB.k_categories + " c, " +
261                               DB.k_cat_tree + " t WHERE c." + DB.gu_category + "=t." + DB.gu_child_cat + " AND " +
262                               "t." + DB.gu_parent_cat + "=? AND c." + DB.gu_category + " NOT IN " +
263                               "(SELECT " + DB.gu_category + " FROM " + sChildNamedTables + " WHERE " + sChildNamedFilter + ")", 32);
264
265     int iChilds = oChilds.load(oConn, aParams);
266
267     if (DebugFile.trace) {
268       DebugFile.writeln(String.valueOf(iChilds) + " childs readed in " + String.valueOf(System.currentTimeMillis()-lElapsed) + " ms");
269       DebugFile.decIdent();
270       DebugFile.writeln("End Categories.getChildsNamed()");
271     }
272
273     return oChilds;
274   } // getChildsNamed()
275

276   //----------------------------------------------------------------------------
277

278   private DBSubset oRoots;
279
280   private DBSubset oRootsNamed;
281   private boolean oRootsLoaded;
282   private int iRootsCount;
283   private String JavaDoc sRootsNamedTables;
284   private String JavaDoc sRootsNamedFields;
285   private String JavaDoc sRootsNamedFilter;
286
287   private String JavaDoc sChildNamedTables;
288   private String JavaDoc sChildNamedFields;
289   private String JavaDoc sChildNamedFilter;
290   private String JavaDoc sChildNamedNoLang;
291
292   public static final int ORDER_BY_NONE = 0;
293   public static final int ORDER_BY_ID = 1;
294   public static final int ORDER_BY_NEUTRAL_NAME = 2;
295   public static final int ORDER_BY_LOCALE_NAME = 3;
296 }
Popular Tags