📄 categoryworker.java
字号:
long rollups = categoryRollupCount(category);
//Debug.log("Category : " + category.get("productCategoryId") + " has " + rollups + " rollups", module);
if (rollups > 0) {
empty = false;
}
}
return empty;
}
public static long categoryMemberCount(GenericValue category) {
if (category == null) return 0;
GenericDelegator delegator = category.getDelegator();
long count = 0;
try {
count = delegator.findCountByCondition("ProductCategoryMember", buildCountCondition("productCategoryId", category.getString("productCategoryId")), null);
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
return count;
}
public static long categoryRollupCount(GenericValue category) {
if (category == null) return 0;
GenericDelegator delegator = category.getDelegator();
long count = 0;
try {
count = delegator.findCountByCondition("ProductCategoryRollup", buildCountCondition("parentProductCategoryId", category.getString("productCategoryId")), null);
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
return count;
}
private static EntityCondition buildCountCondition(String fieldName, String fieldValue) {
List orCondList = new ArrayList();
orCondList.add(new EntityExpr("thruDate", EntityOperator.GREATER_THAN, UtilDateTime.nowTimestamp()));
orCondList.add(new EntityExpr("thruDate", EntityOperator.EQUALS, null));
EntityCondition orCond = new EntityConditionList(orCondList, EntityOperator.OR);
List andCondList = new ArrayList();
andCondList.add(new EntityExpr("fromDate", EntityOperator.LESS_THAN, UtilDateTime.nowTimestamp()));
andCondList.add(new EntityExpr(fieldName, EntityOperator.EQUALS, fieldValue));
andCondList.add(orCond);
EntityCondition andCond = new EntityConditionList(andCondList, EntityOperator.AND);
return andCond;
}
public static void setTrail(PageContext pageContext, String currentCategory) {
setTrail(pageContext.getRequest(), currentCategory);
}
public static void setTrail(ServletRequest request, String currentCategory) {
Map requestParameters = UtilHttp.getParameterMap((HttpServletRequest) request);
String previousCategory = (String) requestParameters.get("pcategory");
if (Debug.verboseOn()) Debug.logVerbose("[CatalogHelper.setTrail] Start: previousCategory=" + previousCategory +
" currentCategory=" + currentCategory, module);
// if there is no current category, just return and do nothing to that the last settings will stay
if (currentCategory == null || currentCategory.length() <= 0)
return;
// always get the last crumb list
List crumb = getTrail(request);
if (crumb == null)
crumb = new ArrayList();
// if no previous category was specified, check to see if currentCategory is in the list
if (previousCategory == null || previousCategory.length() <= 0) {
if (crumb.contains(currentCategory)) {
// if cur category is in crumb, remove everything after it and return
int cindex = crumb.lastIndexOf(currentCategory);
if (cindex < (crumb.size() - 1)) {
for (int i = crumb.size() - 1; i > cindex; i--) {
String deadCat = (String) crumb.remove(i);
if (Debug.infoOn()) Debug.logInfo("[CatalogHelper.setTrail] Removed after current category index: " + i +
" catname: " + deadCat, module);
}
}
return;
} else {
// current category is not in the list, and no previous category was specified, go back to the beginning
previousCategory = "TOP";
crumb.clear();
crumb.add(previousCategory);
if (Debug.infoOn()) Debug.logInfo("[CatalogHelper.setTrail] Starting new list, added previousCategory: " + previousCategory, module);
}
}
if (!crumb.contains(previousCategory)) {
// previous category was NOT in the list, ERROR, start over
if (Debug.infoOn()) Debug.logInfo("[CatalogHelper.setTrail] ERROR: previousCategory (" + previousCategory +
") was not in the crumb list, position is lost, starting over with TOP", module);
previousCategory = "TOP";
crumb.clear();
crumb.add(previousCategory);
} else {
// remove all categories after the previous category, preparing for adding the current category
int index = crumb.indexOf(previousCategory);
if (index < (crumb.size() - 1)) {
for (int i = crumb.size() - 1; i > index; i--) {
String deadCat = (String) crumb.remove(i);
if (Debug.infoOn()) Debug.logInfo("[CatalogHelper.setTrail] Removed after previous category index: " + i +
" catname: " + deadCat, module);
}
}
}
// add the current category to the end of the list
crumb.add(currentCategory);
if (Debug.verboseOn()) Debug.logVerbose("[CatalogHelper.setTrail] Continuing list: Added currentCategory: " + currentCategory, module);
setTrail(request, crumb);
}
public static List getTrail(PageContext pageContext) {
return getTrail(pageContext.getRequest());
}
public static List getTrail(ServletRequest request) {
HttpSession session = ((HttpServletRequest) request).getSession();
ArrayList crumb = (ArrayList) session.getAttribute("_BREAD_CRUMB_TRAIL_");
return crumb;
}
public static List setTrail(PageContext pageContext, List crumb) {
return setTrail(pageContext.getRequest(), crumb);
}
public static List setTrail(ServletRequest request, List crumb) {
HttpSession session = ((HttpServletRequest) request).getSession();
session.setAttribute("_BREAD_CRUMB_TRAIL_", crumb);
return crumb;
}
public static boolean checkTrailItem(PageContext pageContext, String category) {
return checkTrailItem(pageContext.getRequest(), category);
}
public static boolean checkTrailItem(ServletRequest request, String category) {
List crumb = getTrail(request);
if (crumb != null && crumb.contains(category))
return true;
else
return false;
}
public static String lastTrailItem(PageContext pageContext) {
return lastTrailItem(pageContext.getRequest());
}
public static String lastTrailItem(ServletRequest request) {
List crumb = getTrail(request);
if (crumb != null && crumb.size() > 0) {
return (String) crumb.get(crumb.size() - 1);
} else {
return null;
}
}
public static boolean isProductInCategory(GenericDelegator delegator, String productId, String productCategoryId) throws GenericEntityException {
if (productCategoryId == null) return false;
if (productId == null || productId.length() == 0) return false;
List productCategoryMembers = EntityUtil.filterByDate(delegator.findByAndCache("ProductCategoryMember",
UtilMisc.toMap("productCategoryId", productCategoryId, "productId", productId)), true);
if (productCategoryMembers == null || productCategoryMembers.size() == 0) {
//before giving up see if this is a variant product, and if so look up the virtual product and check it...
GenericValue product = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", productId));
List productAssocs = ProductWorker.getVariantVirtualAssocs(product);
//this does take into account that a product could be a variant of multiple products, but this shouldn't ever really happen...
if (productAssocs != null && productAssocs.size() > 0) {
Iterator pasIter = productAssocs.iterator();
while (pasIter.hasNext()) {
GenericValue productAssoc = (GenericValue) pasIter.next();
if (isProductInCategory(delegator, productAssoc.getString("productId"), productCategoryId)) {
return true;
}
}
}
return false;
} else {
return true;
}
}
public static List filterProductsInCategory(GenericDelegator delegator, List valueObjects, String productCategoryId) throws GenericEntityException {
return filterProductsInCategory(delegator, valueObjects, productCategoryId, "productId");
}
public static List filterProductsInCategory(GenericDelegator delegator, List valueObjects, String productCategoryId, String productIdFieldName) throws GenericEntityException {
if (productCategoryId == null) return new LinkedList();
if (valueObjects == null) return null;
List newList = new ArrayList(valueObjects.size());
Iterator valIter = valueObjects.iterator();
while (valIter.hasNext()) {
GenericValue curValue = (GenericValue) valIter.next();
String productId = curValue.getString(productIdFieldName);
if (isProductInCategory(delegator, productId, productCategoryId)) {
newList.add(curValue);
}
}
return newList;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -