1 package com.dotmarketing.portlets.product.factories; 2 3 import java.util.ArrayList ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 7 import javax.servlet.http.HttpServletRequest ; 8 9 import org.apache.struts.action.ActionForm; 10 11 import com.dotmarketing.beans.Identifier; 12 import com.dotmarketing.beans.Inode; 13 import com.dotmarketing.db.DbConnectionFactory; 14 import com.dotmarketing.db.DotHibernate; 15 import com.dotmarketing.exception.DotRuntimeException; 16 import com.dotmarketing.factories.InodeFactory; 17 import com.dotmarketing.portlets.categories.model.Category; 18 import com.dotmarketing.portlets.entities.factories.EntityFactory; 19 import com.dotmarketing.portlets.entities.model.Entity; 20 import com.dotmarketing.util.Config; 21 import com.dotmarketing.util.Logger; 22 import com.dotmarketing.util.UtilMethods; 23 import com.dotmarketing.util.WebKeys; 24 import com.dotmarketing.cms.product.struts.ProductsForm; 25 import com.dotmarketing.portlets.product.model.Product; 26 import com.dotmarketing.portlets.product.model.ProductFormat; 27 31 public class ProductFactory { 32 33 public static java.util.List getAllProducts() { 34 DotHibernate dh = new DotHibernate(Product.class); 35 dh.setQuery( 36 "from inode in class com.dotmarketing.portlets.product.model.Product order by title"); 37 return dh.list(); 38 } 39 40 public static java.util.List getAllProducts(String orderby) { 41 DotHibernate dh = new DotHibernate(Product.class); 42 dh.setQuery( 43 "from inode in class com.dotmarketing.portlets.product.model.Product order by " + orderby); 44 return dh.list(); 45 } 46 47 public static java.util.List getAllProducts(String [] productType,String keyword,String orderBy,String direction) { 48 String inProduct = ""; 49 if(productType != null) 50 { 51 for(int i = 0;i < productType.length;i++) 52 { 53 if (!(productType[i].equals("0"))) 54 { 55 inProduct += "'" + productType[i] + "',"; 56 } 57 } 58 if (!inProduct.equals("")) 59 { 60 inProduct = inProduct.substring(0,inProduct.lastIndexOf(",")); 61 } 62 } 63 64 DotHibernate dh = new DotHibernate(Product.class); 65 String query = ""; 66 if (inProduct.equals("")) 67 { 68 query = "select {ecom_product.*} from ecom_product, inode ecom_product_1_ where ecom_product.inode = ecom_product_1_.inode"; 69 } 70 else 71 { 72 query = "select {ecom_product.*} from ecom_product, inode ecom_product_1_, tree where " + 73 "ecom_product.inode = ecom_product_1_.inode and tree.child = ecom_product.inode"; 74 } 75 76 if (UtilMethods.isSet(keyword)) 77 { 78 keyword = "%" + keyword + "%"; 79 query += " and title like '" + keyword + "'"; 80 } 81 if (!inProduct.equals("")) 82 { 83 query += " and tree.parent in (" + inProduct + ") "; 84 } 85 if (UtilMethods.isSet(orderBy) && UtilMethods.isSet(direction)) 86 { 87 query += " order by " + orderBy + " " + direction; 88 } 89 dh.setSQLQuery(query); 90 return dh.list(); 91 } 92 93 94 public static Product newInstance() { 95 Product dc = new Product(); 96 return dc; 97 } 98 99 public static void saveProduct(Product product) { 100 InodeFactory.saveInode(product); 101 } 102 103 public static void deleteProduct(Product product) { 104 List <ProductFormat> formats = ProductFormatFactory.getAllFormatsByProduct(product); 105 for (ProductFormat format : formats) { 106 ProductFormatFactory.deleteProductFormat(format); 107 } 108 InodeFactory.deleteInode(product); 109 } 110 111 public static void copyProduct(Product product) { 112 Product copyProduct = new Product(); 114 try 115 { 116 copyProduct.setTitle(product.getTitle()); 118 copyProduct.setShortDescription(product.getShortDescription()); 119 copyProduct.setLongDescription(product.getLongDescription()); 120 copyProduct.setReqShipping(product.getReqShipping()); 121 copyProduct.setFeatured(product.getFeatured()); 122 copyProduct.setComments(product.getComments()); 123 } 124 catch(Exception ex) 125 { 126 Logger.debug(ProductFactory.class,ex.toString()); 127 } 128 String title = copyProduct.getTitle(); 129 title += " COPY"; 130 copyProduct.setTitle(title); 131 copyProduct.setInode(0); 132 ProductFactory.saveProduct(copyProduct); 133 134 List <ProductFormat> formats = ProductFormatFactory.getAllFormatsByProduct(product); 136 for (ProductFormat format : formats) { 137 ProductFormatFactory.copyProductFormat(copyProduct,format); 138 } 139 Identifier identifier = (Identifier) InodeFactory.getChildOfClassByRelationType(product,Identifier.class,WebKeys.PRODUCT_SMALL_IMAGE); 143 copyProduct.addChild(identifier,WebKeys.PRODUCT_SMALL_IMAGE); 144 145 identifier = (Identifier) InodeFactory.getChildOfClassByRelationType(product,Identifier.class,WebKeys.PRODUCT_MEDIUM_IMAGE); 147 copyProduct.addChild(identifier,WebKeys.PRODUCT_MEDIUM_IMAGE); 148 149 150 identifier = (Identifier) InodeFactory.getChildOfClassByRelationType(product,Identifier.class,WebKeys.PRODUCT_LARGE_IMAGE); 152 copyProduct.addChild(identifier,WebKeys.PRODUCT_LARGE_IMAGE); 153 154 List identifiers = InodeFactory.getChildrenClassByRelationType(product,Identifier.class,WebKeys.PRODUCT_FILES); 156 Iterator it = identifiers.iterator(); 157 while (it.hasNext()) { 158 identifier = (Identifier)it.next(); 159 copyProduct.addChild(identifier,WebKeys.PRODUCT_FILES); 160 } 161 java.util.List categories = InodeFactory.getParentsOfClassByRelationType(product,Category.class,WebKeys.PRODUCT_PRODUCTS_TYPE); 163 for(int i = 0;i < categories.size();i++) 164 { 165 Category category = (Category) categories.get(i); 166 copyProduct.addParent(category,WebKeys.PRODUCT_PRODUCTS_TYPE); 167 } 168 categories = InodeFactory.getParentsOfClassByRelationType(product,Category.class,WebKeys.PRODUCT_CATEGORIES); 170 for(int i = 0;i < categories.size();i++) 171 { 172 Category category = (Category) categories.get(i); 173 copyProduct.addParent(category,WebKeys.PRODUCT_CATEGORIES); 174 } 175 List <Product> relatedProducts = InodeFactory.getChildrenClassByRelationType(product,Product.class,WebKeys.PRODUCT_RELATED); 177 for(Product relatedProduct : relatedProducts) 178 { 179 copyProduct.addChild(relatedProduct,WebKeys.PRODUCT_RELATED); 180 } 181 } 182 183 public static Product getProduct(String inode) { 184 return (Product) InodeFactory.getInode(inode,Product.class); 185 } 186 187 public static Product getProduct(long inode) { 188 return (Product) InodeFactory.getInode(inode,Product.class); 189 } 190 191 public static List <Product> getAllProductsByCategory(int categoryInode,String orderBy,String filter,int page,int pageSize,String direction, String condition) 192 { 193 List <Product> listProducts = null; 194 try { 195 String tableName = ((Inode) Product.class.newInstance()).getType(); 196 DotHibernate dh = new DotHibernate(Product.class); 197 String sql = "SELECT {" + tableName + ".*} " 198 + "from " + tableName + " " + tableName + ", " 199 + "tree tree, " 200 + "tree tree1, " 201 + "tree tree2, " 202 + "entity entity, " 203 + "inode " + tableName + "_1_, " 204 + "category category " 205 + "where tree.child = " + tableName + ".inode " 206 + "and " + tableName + "_1_.inode = " + tableName + ".inode " 207 + "and tree.parent = category.inode " 208 + "and category.inode = tree1.child " 209 + "and tree1.parent = tree2.parent " 210 + "and tree2.child = entity.inode " 211 + "and entity.entity_name = ? "; 212 213 if (UtilMethods.isSet(condition)) { 214 sql += "and " + condition; 215 } 216 217 String sqlCategory = ""; 219 if (categoryInode > 0) 220 { 221 sqlCategory = "and category.inode = ? "; 222 } 223 224 String sqlFilter = ""; 226 if(UtilMethods.isSet(filter)) 227 { 228 sqlFilter = "and " + tableName + ".title like '%" + filter + "%' "; 229 } 230 231 String sqlOrder = ""; 233 if (UtilMethods.isSet(orderBy)) 234 { 235 direction = (UtilMethods.isSet(direction) ? direction : "ASC"); 236 sqlOrder = "order by " + tableName + "." + orderBy + " " + direction + " "; 237 } 238 239 sql = sql + sqlCategory + sqlFilter + sqlOrder; 241 242 if(pageSize > 0 && page >= 0) 244 { 245 dh.setFirstResult(page * pageSize); 246 dh.setMaxResults(pageSize); 247 } 248 249 dh.setSQLQuery(sql); 251 dh.setParam(WebKeys.PRODUCT_PRODUCTS_TYPE); 252 if (categoryInode > 0) 253 { 254 dh.setParam(categoryInode); 255 } 256 listProducts = dh.list(); 257 } catch (Exception e) { 258 Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e); 259 throw new DotRuntimeException(e.toString()); 260 } 261 return listProducts; 262 } 263 264 public static List <Product> getAllFeaturedProductsByCategory(int categoryInode,String orderBy,String filter,int page,int pageSize,String direction) 265 { 266 List <Product> listProducts = null; 268 try { 269 String tableName = ((Inode) Product.class.newInstance()).getType(); 270 DotHibernate dh = new DotHibernate(Product.class); 271 String sql = "SELECT {" + tableName + ".*} " 272 + "from " + tableName + " " + tableName + ", " 273 + "tree tree, " 274 + "tree tree1, " 275 + "tree tree2, " 276 + "entity entity, " 277 + "inode " + tableName + "_1_, " 278 + "category category " 279 + "where " + tableName + ".featured = " + DbConnectionFactory.getDBTrue() + " " 280 + "and tree.child = " + tableName + ".inode " 281 + "and " + tableName + "_1_.inode = " + tableName + ".inode " 282 + "and tree.parent = category.inode " 283 + "and category.inode = tree1.child " 284 + "and tree1.parent = tree2.parent " 285 + "and tree2.child = entity.inode " 286 + "and entity.entity_name = ? " 287 + "and showOnWeb = " + DbConnectionFactory.getDBTrue() + " "; 288 289 String sqlCategory = ""; 291 if (categoryInode > 0) 292 { 293 sqlCategory = "and category.inode = ? "; 294 } 295 296 String sqlFilter = ""; 298 if(UtilMethods.isSet(filter)) 299 { 300 sqlFilter = "and " + tableName + ".title like '%" + filter + "%' "; 301 } 302 303 String sqlOrder = ""; 305 if (UtilMethods.isSet(orderBy)) 306 { 307 sqlOrder = "order by " + orderBy + " " + direction + " "; 308 } 309 310 sql = sql + sqlCategory + sqlFilter + sqlOrder; 312 313 if(pageSize > 0 && page >= 0) 315 { 316 dh.setFirstResult(page * pageSize); 317 dh.setMaxResults(pageSize); 318 } 319 320 dh.setSQLQuery(sql); 322 dh.setParam(WebKeys.PRODUCT_PRODUCTS_TYPE); 324 if (categoryInode > 0) 325 { 326 dh.setParam(categoryInode); 327 } 328 listProducts = dh.list(); 329 } catch (Exception e) { 330 Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e); 331 throw new DotRuntimeException(e.toString()); 332 } 333 return listProducts; 334 } 335 336 @SuppressWarnings ("unchecked") 337 public static List <Product> getAllProductsByCategoryList(List <Category> categories,int topProductNumber,String orderBy,String direction,String filter,int page,int pageSize) 338 { 339 List <Product> listProducts = null; 340 try { 341 String tableName = ((Inode) Product.class.newInstance()).getType(); 342 DotHibernate dh = new DotHibernate(Product.class); 343 String sql = "SELECT"; 344 if(topProductNumber > 0){ 345 sql = sql + " top "+ topProductNumber; 346 } 347 sql = sql + " {" + tableName + ".*} " 348 + "from " + tableName + " " + tableName + ", " 349 + "tree tree, " 350 + "tree tree1, " 351 + "tree tree2, " 352 + "entity entity, " 353 + "inode " + tableName + "_1_, " 354 + "category category " 355 + "where tree.child = " + tableName + ".inode " 356 + "and " + tableName + "_1_.inode = " + tableName + ".inode " 357 + "and tree.parent = category.inode " 358 + "and category.inode = tree1.child " 359 + "and tree1.parent = tree2.parent " 360 + "and tree2.child = entity.inode " 361 + "and showOnWeb = " + DbConnectionFactory.getDBTrue() + " "; 362 364 String sqlCategory = ""; 366 if (categories != null && categories.size() > 0) 367 { 368 sqlCategory = "and category.inode in ("; 369 if(categories.size() >1){ 370 for(int i = 0 ; i < categories.size() - 1 ; i++ ){ 371 372 sqlCategory = sqlCategory + categories.get(i).getInode() + ","; 373 } 374 } 375 sqlCategory = sqlCategory + categories.get(0).getInode() + ") "; 376 } 377 378 String sqlFilter = ""; 380 if(UtilMethods.isSet(filter)) 381 { 382 sqlFilter = "and " + tableName + ".title like '%" + filter + "%' "; 383 } 384 385 String sqlOrder = ""; 387 if (UtilMethods.isSet(orderBy)) 388 { 389 sqlOrder = "order by " + orderBy + " " + direction + " "; 390 } 391 392 sql = sql + sqlCategory + sqlFilter + sqlOrder; 394 395 if(pageSize > 0 && page >= 0) 397 { 398 dh.setFirstResult(page * pageSize); 399 dh.setMaxResults(pageSize); 400 } 401 402 dh.setSQLQuery(sql); 404 405 listProducts = dh.list(); 406 } catch (Exception e) { 407 Logger.error(InodeFactory.class, "getChildrenClass failed:" + e, e); 408 throw new DotRuntimeException(e.toString()); 409 } 410 return listProducts; 411 } 412 413 public static void loadFeaturedProductByCategory(ActionForm lf,HttpServletRequest request) 414 { 415 ProductsForm productsForm = (ProductsForm) lf; 416 int categoryInode = productsForm.getCategoryInode(); 417 String orderBy = productsForm.getOrderBy(); 418 if (!UtilMethods.isSet(orderBy)) { 419 orderBy = "ecom_product.sort_order"; 420 } 421 int page = productsForm.getPage(); 422 int pageSize = productsForm.getPageSize(); 423 String direction = productsForm.getDirection(); 424 if (!UtilMethods.isSet(direction)) { 425 direction = "asc"; 426 } 427 String filter = productsForm.getFilter(); 428 List <Product> listProducts = ProductFactory.getAllFeaturedProductsByCategory(categoryInode,orderBy,filter,page,pageSize,direction); 430 431 productsForm.setListProducts(listProducts); 433 } 434 435 public static void loadProductByCategory(ActionForm lf,HttpServletRequest request) 436 { 437 ProductsForm productsForm = (ProductsForm) lf; 438 int categoryInode = productsForm.getCategoryInode(); 439 String orderBy = productsForm.getOrderBy(); 440 int page = productsForm.getPage(); 441 int pageSize = productsForm.getPageSize(); 442 String direction = productsForm.getDirection(); 443 String filter = productsForm.getFilter(); 444 445 orderBy = (UtilMethods.isSet(orderBy) ? orderBy : "sort_order"); 447 String condition = "showOnWeb = " + DbConnectionFactory.getDBTrue() + " "; 448 List <Product> listProducts = ProductFactory.getAllProductsByCategory(categoryInode,orderBy,filter,page,pageSize,direction,condition); 449 450 productsForm.setListProducts(listProducts); 452 } 453 454 public static void loadProduct(ActionForm lf,HttpServletRequest request) 455 { 456 ProductsForm productsForm = (ProductsForm) lf; 457 long productInode = productsForm.getInode(); 458 459 List <Product> listProducts = new ArrayList (); 461 Product product = ProductFactory.getProduct(productInode); 462 listProducts.add(product); 463 464 productsForm.setListProducts(listProducts); 466 } 467 468 public static void loadTypeProducts(ActionForm lf,HttpServletRequest request) 469 { 470 ProductsForm productsForm = (ProductsForm) lf; 471 472 String typeProductEntityName = WebKeys.PRODUCT_PRODUCTS_TYPE; 473 Entity entity = EntityFactory.getEntity(typeProductEntityName); 474 475 List <Category> listTypeProducts = InodeFactory.getParentsOfClass(entity,Category.class); 477 Category masterCategory = listTypeProducts.get(0); 479 listTypeProducts = InodeFactory.getChildrenClass(masterCategory,Category.class,"sort_order"); 480 481 productsForm.setListTypeProducts(listTypeProducts); 483 } 484 } 485 | Popular Tags |