1 32 33 package com.knowgate.hipergate; 34 35 import java.sql.SQLException ; 36 import java.sql.PreparedStatement ; 37 import java.sql.ResultSet ; 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 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 65 71 public void clearCache() { 72 oRootsLoaded = false; 73 } 74 75 77 83 public static void expand (JDCConnection oConn, String sRootCategoryId) throws SQLException { 84 Category oRoot = new Category(sRootCategoryId); 85 oRoot.expand(oConn); 86 } 87 88 90 98 public Category forDomain(JDCConnection oConn, int iDomain) throws SQLException { 99 PreparedStatement oStmt; 100 ResultSet 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 } 130 132 142 public DBSubset getRoots(JDCConnection oConn) throws SQLException { 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 } 160 162 167 public int getRootsCount() throws IllegalStateException { 168 if (-1==iRootsCount) throw new IllegalStateException ("Must call getRoots() or getRootsNamed() prior to getRootsCount()"); 169 170 return iRootsCount; 171 } 172 173 175 190 public DBSubset getRootsNamed(JDCConnection oConn, String sLanguage, int iOrderBy) throws SQLException { 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 [] 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 } 219 221 235 public DBSubset getChildsNamed(JDCConnection oConn, String idParent, String sLanguage, int iOrderBy) throws SQLException { 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 [] 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 } 276 278 private DBSubset oRoots; 279 280 private DBSubset oRootsNamed; 281 private boolean oRootsLoaded; 282 private int iRootsCount; 283 private String sRootsNamedTables; 284 private String sRootsNamedFields; 285 private String sRootsNamedFilter; 286 287 private String sChildNamedTables; 288 private String sChildNamedFields; 289 private String sChildNamedFilter; 290 private String 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 |