Author: jleroux
Date: Fri Mar 23 06:32:23 2012 New Revision: 1304205 URL: http://svn.apache.org/viewvc?rev=1304205&view=rev Log: A patch from Paul Pipper "Categories - calculated trails" https://issues.apache.org/jira/browse/OFBIZ-4580 I noticed that currently OFBiz misses a simple function to generate a category trail. Generating a trail, however, is often useful when generating breadcrums, facetted search results, and proper category trees in general. Hence I created getCategoryTrail Modified: ofbiz/trunk/applications/product/servicedef/services_maint.xml ofbiz/trunk/applications/product/src/org/ofbiz/product/category/CategoryWorker.java Modified: ofbiz/trunk/applications/product/servicedef/services_maint.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/servicedef/services_maint.xml?rev=1304205&r1=1304204&r2=1304205&view=diff ============================================================================== --- ofbiz/trunk/applications/product/servicedef/services_maint.xml (original) +++ ofbiz/trunk/applications/product/servicedef/services_maint.xml Fri Mar 23 06:32:23 2012 @@ -114,6 +114,16 @@ under the License. <attribute name="parentProductCategoryId" type="String" mode="IN" optional="false"/> <attribute name="categories" type="java.util.List" mode="INOUT" optional="true"/> </service> + + <service name="getCategoryTrail" engine="java" + location="org.ofbiz.product.category.CategoryWorker" invoke="getCategoryTrail"> + <description> Returns a complete category trail - can be used for exporting proper category trees. + This is mostly useful when used in combination with breadcrumbs, for building a facetted index tree, + or to export a category tree for migration to another system. + Will create the tree from root point to categoryId. </description> + <attribute name="productCategoryId" type="String" mode="IN" optional="false"/> + <attribute name="trail" type="java.util.List" mode="OUT" optional="true"/> + </service> <service name="checkImageUrlForCategoryAndProduct" engine="simple" location="component://product/script/org/ofbiz/product/catalog/CatalogServices.xml" invoke="checkImageUrlForCategoryAndProduct"> Modified: ofbiz/trunk/applications/product/src/org/ofbiz/product/category/CategoryWorker.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/category/CategoryWorker.java?rev=1304205&r1=1304204&r2=1304205&view=diff ============================================================================== --- ofbiz/trunk/applications/product/src/org/ofbiz/product/category/CategoryWorker.java (original) +++ ofbiz/trunk/applications/product/src/org/ofbiz/product/category/CategoryWorker.java Fri Mar 23 06:32:23 2012 @@ -19,7 +19,9 @@ package org.ofbiz.product.category; import java.util.Collection; +import java.util.Collections; import java.util.List; +import java.util.Locale; import java.util.Map; import javax.servlet.ServletRequest; @@ -34,14 +36,18 @@ import org.ofbiz.base.util.UtilFormatOut import org.ofbiz.base.util.UtilGenerics; import org.ofbiz.base.util.UtilHttp; import org.ofbiz.base.util.UtilMisc; +import org.ofbiz.base.util.UtilProperties; import org.ofbiz.base.util.UtilValidate; import org.ofbiz.entity.Delegator; +import org.ofbiz.entity.GenericDelegator; import org.ofbiz.entity.GenericEntityException; import org.ofbiz.entity.GenericValue; import org.ofbiz.entity.condition.EntityCondition; import org.ofbiz.entity.condition.EntityOperator; import org.ofbiz.entity.util.EntityUtil; import org.ofbiz.product.product.ProductWorker; +import org.ofbiz.service.DispatchContext; +import org.ofbiz.service.ServiceUtil; /** * CategoryWorker - Worker class to reduce code in JSPs. @@ -412,4 +418,54 @@ public class CategoryWorker { } } } + + /** + * Returns a complete category trail - can be used for exporting proper category trees. + * This is mostly useful when used in combination with bread-crumbs, for building a + * faceted index tree, or to export a category tree for migration to another system. + * Will create the tree from root point to categoryId. + * + * @param productCategoryId id of category the trail should be generated for + * @returns List organized trail from root point to categoryId. + * */ + public static Map getCategoryTrail(DispatchContext dctx, Map context) { + String productCategoryId = (String) context.get("productCategoryId"); + Map<String, Object> results = ServiceUtil.returnSuccess(); + GenericDelegator delegator = (GenericDelegator) dctx.getDelegator(); + List<String> trailElements = FastList.newInstance(); + trailElements.add(productCategoryId); + String parentProductCategoryId = productCategoryId; + while (UtilValidate.isNotEmpty(parentProductCategoryId)) { + // find product category rollup + try { + List<EntityCondition> rolllupConds = FastList.newInstance(); + rolllupConds.add(EntityCondition.makeCondition("productCategoryId", parentProductCategoryId)); + rolllupConds.add(EntityUtil.getFilterByDateExpr()); + List<GenericValue> productCategoryRollups = delegator.findList("ProductCategoryRollup", + EntityCondition.makeCondition(rolllupConds), null, UtilMisc.toList("sequenceNum"), null, true); + if (UtilValidate.isNotEmpty(productCategoryRollups)) { + // add only categories that belong to the top category to trail + for (GenericValue productCategoryRollup : productCategoryRollups) { + String trailCategoryId = productCategoryRollup.getString("parentProductCategoryId"); + parentProductCategoryId = trailCategoryId; + if (trailElements.contains(trailCategoryId)) { + break; + } else { + trailElements.add(trailCategoryId); + } + } + } else { + parentProductCategoryId = null; + } + } catch (GenericEntityException e) { + Map<String, String> messageMap = UtilMisc.toMap("errMessage", ". Cannot generate trail from product category. "); + String errMsg = UtilProperties.getMessage("CommonUiLabels", "CommonDatabaseProblem", messageMap, (Locale) context.get("locale")); + Debug.logError(e, errMsg, module); + return ServiceUtil.returnError(errMsg); + } + } + Collections.reverse(trailElements); + results.put("trail", trailElements); + return results; + } } |
Free forum by Nabble | Edit this page |