Categories - calculated trails
------------------------------
Key: OFBIZ-4580
URL: https://issues.apache.org/jira/browse/OFBIZ-4580
Project: OFBiz
Issue Type: Improvement
Reporter: Paul Piper
Priority: Minor
Hey folks,
been a while since I contributed. 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 the following as a timesaver:
{code:title=getCategoryTrail|borderStyle=solid}
public static List getCategoryTrail(String productCategoryId,DispatchContext
dctx){
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("-fromDate"), 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) {
Debug.logError(e, "Cannot generate trail from product
category", module);
}
}
Collections.reverse(trailElements);
return trailElements;
}
{code}
I suggest to add this to the CategoryWorker.java
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators:
https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira