1 24 package org.ofbiz.product.category; 25 26 import java.sql.Timestamp ; 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import javolution.util.FastList; 35 36 import org.ofbiz.base.util.Debug; 37 import org.ofbiz.base.util.UtilDateTime; 38 import org.ofbiz.base.util.UtilMisc; 39 import org.ofbiz.entity.GenericDelegator; 40 import org.ofbiz.entity.GenericEntityException; 41 import org.ofbiz.entity.GenericValue; 42 import org.ofbiz.entity.condition.EntityCondition; 43 import org.ofbiz.entity.condition.EntityConditionList; 44 import org.ofbiz.entity.condition.EntityExpr; 45 import org.ofbiz.entity.condition.EntityOperator; 46 import org.ofbiz.entity.util.EntityFindOptions; 47 import org.ofbiz.entity.util.EntityListIterator; 48 import org.ofbiz.entity.util.EntityUtil; 49 import org.ofbiz.product.catalog.CatalogWorker; 50 import org.ofbiz.service.DispatchContext; 51 import org.ofbiz.service.ModelService; 52 import org.ofbiz.service.ServiceUtil; 53 54 61 public class CategoryServices { 62 63 public static final String module = CategoryServices.class.getName(); 64 65 public static Map getCategoryMembers(DispatchContext dctx, Map context) { 66 GenericDelegator delegator = dctx.getDelegator(); 67 String categoryId = (String ) context.get("categoryId"); 68 GenericValue productCategory = null; 69 List members = null; 70 71 try { 72 productCategory = delegator.findByPrimaryKeyCache("ProductCategory", UtilMisc.toMap("productCategoryId", categoryId)); 73 members = EntityUtil.filterByDate(productCategory.getRelatedCache("ProductCategoryMember", null, UtilMisc.toList("sequenceNum")), true); 74 if (Debug.verboseOn()) Debug.logVerbose("Category: " + productCategory + " Member Size: " + members.size() + " Members: " + members, module); 75 } catch (GenericEntityException e) { 76 String errMsg = "Problem reading product categories: " + e.getMessage(); 77 Debug.logError(e, errMsg, module); 78 return ServiceUtil.returnError(errMsg); 79 } 80 Map result = ServiceUtil.returnSuccess(); 81 result.put("category", productCategory); 82 result.put("categoryMembers", members); 83 return result; 84 } 85 86 public static Map getNextPreviousCategoryMembers(DispatchContext dctx, Map context) { 87 GenericDelegator delegator = dctx.getDelegator(); 88 String categoryId = (String ) context.get("categoryId"); 89 String productId = (String ) context.get("productId"); 90 Integer index = (Integer ) context.get("index"); 91 92 if (index == null && productId == null) { 93 return ServiceUtil.returnError("Both Index and ProductID cannot be null."); 94 } 95 96 Map values = getCategoryMembers(dctx, context); 97 98 if (values.containsKey(ModelService.ERROR_MESSAGE)) { 99 return values; 100 } 101 if (!values.containsKey("categoryMembers") || values.get("categoryMembers") == null) { 102 return ServiceUtil.returnError("Problem reading category data."); 103 } 104 105 Collection memberCol = (Collection ) values.get("categoryMembers"); 106 if (memberCol == null || memberCol.size() == 0) { 107 return ServiceUtil.returnSuccess("Product not found in the current category."); 109 } 110 111 List memberList = new ArrayList (memberCol); 112 113 if (productId != null && index == null) { 114 Iterator i = memberList.iterator(); 115 116 while (i.hasNext()) { 117 GenericValue v = (GenericValue) i.next(); 118 119 if (v.getString("productId").equals(productId)) 120 index = new Integer (memberList.indexOf(v)); 121 } 122 } 123 124 if (index == null) { 125 return ServiceUtil.returnSuccess("Product not found in the current category."); 127 } 128 129 Map result = ServiceUtil.returnSuccess(); 130 result.put("category", values.get("category")); 131 132 String previous = null; 133 String next = null; 134 135 if (index.intValue() - 1 >= 0 && index.intValue() - 1 < memberList.size()) { 136 previous = ((GenericValue) memberList.get(index.intValue() - 1)).getString("productId"); 137 result.put("previousProductId", previous); 138 } else { 139 previous = ((GenericValue) memberList.get(memberList.size() - 1)).getString("productId"); 140 result.put("previousProductId", previous); 141 } 142 143 if (index.intValue() + 1 < memberList.size()) { 144 next = ((GenericValue) memberList.get(index.intValue() + 1)).getString("productId"); 145 result.put("nextProductId", next); 146 } else { 147 next = ((GenericValue) memberList.get(0)).getString("productId"); 148 result.put("nextProductId", next); 149 } 150 return result; 151 } 152 153 public static Map getProductCategoryAndLimitedMembers(DispatchContext dctx, Map context) { 154 GenericDelegator delegator = dctx.getDelegator(); 155 String productCategoryId = (String ) context.get("productCategoryId"); 156 boolean limitView = ((Boolean ) context.get("limitView")).booleanValue(); 157 int defaultViewSize = ((Integer ) context.get("defaultViewSize")).intValue(); 158 159 String prodCatalogId = (String ) context.get("prodCatalogId"); 160 161 boolean useCacheForMembers = (context.get("useCacheForMembers") != null ? ((Boolean ) context.get("useCacheForMembers")).booleanValue() : true); 162 boolean activeOnly = (context.get("activeOnly") != null ? ((Boolean ) context.get("activeOnly")).booleanValue() : true); 163 boolean checkViewAllow = (context.get("checkViewAllow") != null ? ((Boolean ) context.get("checkViewAllow")).booleanValue() : false); 165 166 Timestamp nowTimestamp = UtilDateTime.nowTimestamp(); 167 168 int viewIndex = 1; 169 try { 170 viewIndex = Integer.valueOf((String ) context.get("viewIndexString")).intValue(); 171 } catch (Exception e) { 172 viewIndex = 1; 173 } 174 175 int viewSize = defaultViewSize; 176 try { 177 viewSize = Integer.valueOf((String ) context.get("viewSizeString")).intValue(); 178 } catch (Exception e) { 179 viewSize = defaultViewSize; 180 } 181 182 GenericValue productCategory = null; 183 try { 184 productCategory = delegator.findByPrimaryKeyCache("ProductCategory", UtilMisc.toMap("productCategoryId", productCategoryId)); 185 } catch (GenericEntityException e) { 186 Debug.logWarning(e.getMessage(), module); 187 productCategory = null; 188 } 189 190 int listSize = 0; 191 int lowIndex = 0; 192 int highIndex = 0; 193 194 if (limitView) { 195 lowIndex = (((viewIndex - 1) * viewSize) + 1); 197 highIndex = viewIndex * viewSize; 198 } 199 200 List productCategoryMembers = null; 201 if (productCategory != null) { 202 try { 203 if (useCacheForMembers) { 204 productCategoryMembers = productCategory.getRelatedCache("ProductCategoryMember", null, UtilMisc.toList("sequenceNum")); 205 if (activeOnly) { 206 productCategoryMembers = EntityUtil.filterByDate(productCategoryMembers, true); 207 } 208 listSize = productCategoryMembers.size(); 209 if (highIndex > listSize) { 210 highIndex = listSize; 211 } 212 213 if (limitView) { 214 productCategoryMembers = productCategoryMembers.subList(lowIndex-1, highIndex); 215 } else { 216 lowIndex = 1; 217 highIndex = listSize; 218 } 219 } else { 220 List mainCondList = UtilMisc.toList(new EntityExpr("productCategoryId", EntityOperator.EQUALS, productCategory.getString("productCategoryId"))); 221 if (activeOnly) { 222 mainCondList.add(new EntityExpr("fromDate", EntityOperator.LESS_THAN_EQUAL_TO, nowTimestamp)); 223 mainCondList.add(new EntityExpr(new EntityExpr("thruDate", EntityOperator.EQUALS, null), EntityOperator.OR, new EntityExpr("thruDate", EntityOperator.GREATER_THAN, nowTimestamp))); 224 } 225 EntityCondition mainCond = new EntityConditionList(mainCondList, EntityOperator.AND); 226 227 EntityFindOptions findOpts = new EntityFindOptions(true, EntityFindOptions.TYPE_SCROLL_INSENSITIVE, EntityFindOptions.CONCUR_READ_ONLY, true); 229 EntityListIterator pli = delegator.findListIteratorByCondition("ProductCategoryMember", mainCond, null, null, UtilMisc.toList("sequenceNum", "productId"), findOpts); 231 232 if (limitView) { 234 productCategoryMembers = pli.getPartialList(lowIndex, viewSize); 235 pli.last(); 237 listSize = pli.currentIndex(); 238 } else { 239 productCategoryMembers = pli.getCompleteList(); 240 listSize = productCategoryMembers.size(); 241 lowIndex = 1; 242 highIndex = listSize; 243 } 244 if (productCategoryMembers == null) { 245 productCategoryMembers = FastList.newInstance(); 246 } 247 248 if (highIndex > listSize) { 249 highIndex = listSize; 250 } 251 252 pli.close(); 254 } 255 256 if (checkViewAllow && prodCatalogId != null && productCategoryMembers != null && productCategoryMembers.size() > 0) { 258 String viewProductCategoryId = CatalogWorker.getCatalogViewAllowCategoryId(delegator, prodCatalogId); 259 if (viewProductCategoryId != null) { 260 productCategoryMembers = CategoryWorker.filterProductsInCategory(delegator, productCategoryMembers, viewProductCategoryId); 261 } 262 } 263 } catch (GenericEntityException e) { 264 Debug.logError(e, module); 265 } 266 } 267 268 Map result = new HashMap (); 269 result.put("viewIndex", new Integer (viewIndex)); 270 result.put("viewSize", new Integer (viewSize)); 271 result.put("lowIndex", new Integer (lowIndex)); 272 result.put("highIndex", new Integer (highIndex)); 273 result.put("listSize", new Integer (listSize)); 274 if (productCategory != null) result.put("productCategory", productCategory); 275 if (productCategoryMembers != null) result.put("productCategoryMembers", productCategoryMembers); 276 return result; 277 } 278 } 279 | Popular Tags |