Grouped options in the Category filter (optional)

git-svn-id: https://svn.libreccm.org/ccm/trunk@2492 8810af33-2d31-482b-a856-94f89814c4df
master
jensp 2014-01-17 13:59:35 +00:00
parent fb97ab3bb1
commit 1ecc9a57f1
1 changed files with 60 additions and 44 deletions

View File

@ -15,39 +15,39 @@ import java.util.Map;
/**
* This filter allows it to filter the objects in a customisable object list by categories assigned
* to them. This filter is for example useful to filter objects after keywords. The keywords
* are managed as an additional category system. The include the filter into a customisable object
* list are special JSP template is required.
*
* to them. This filter is for example useful to filter objects after keywords. The keywords are
* managed as an additional category system. The include the filter into a customisable object list
* are special JSP template is required.
*
* First you have to add a customisable object list to the page by
*
*
* <pre>
* {@code
* <define:component name="itemList"
* classname="com.arsdigita.navigation.ui.object.CustomizableObjectList"/>
* }
* </pre>
*
* In a scriptlet block following your component definitions you have to set several parameters
* for the object list (see {@link CustomizableObjectList}) To add and configure a category filter
* to the list add these lines:
*
* classname="com.arsdigita.navigation.ui.object.CustomizableObjectList"/> }
* <
* /pre>
*
* In a scriptlet block following your component definitions you have to set several parameters for
* the object list (see {@link CustomizableObjectList}) To add and configure a category filter to
* the list add these lines:
*
* <pre>
* {@code
* CustomizableObjectList objList = (CustomizableObjectList) itemList;
*
*
* ...
*
*
* CategoryFilter catFilter = objList.addCategoryFilter("labelOfCatFilter", "rootCategory");
* catFilter.setSeparator(";");
* }
* </pre>
*
* {@link CustomizableObjectList#addCategoryFilter(java.lang.String, java.lang.String)} adds a
*
* {@link CustomizableObjectList#addCategoryFilter(java.lang.String, java.lang.String)} adds a
* category filter to the object list. The method requires two parameters: An identifier for the
* label of the category filter (localisation for the label has to done in theme at the moment) and
* the name of the root category of the category system to use for the filter.
*
*
* @author Jens Pelzetter <jens@jp-digital.de>
* @version $Id$
*/
@ -80,12 +80,12 @@ public class CategoryFilter {
/**
* Factory method for creating a category filter.
*
*
* @param label
* @param categoryName
* @return
* @return
*/
public static CategoryFilter createCategoryFilter(final String label,
public static CategoryFilter createCategoryFilter(final String label,
final String categoryName) {
final DataCollection collection = SessionManager.getSession().retrieve(
Category.BASE_DATA_OBJECT_TYPE);
@ -115,21 +115,21 @@ public class CategoryFilter {
}
/**
* Apply the filter the {@link DataCollection} of the object list. The solution for the query
* is a bit weird, due to limitations of PDL. The simple solution would be to use the
* predefined query {@code objectIDsInMultipleSubtrees} from the {@code Category.pdl} of the
* module as a subquery in the {@code where} clause, like this:
* {@code WHERE parent.id ALL ($objectIDsInMultipleSubtrees)}. But PDL does not support
* the {@code ALL} operator. So we have to use another solution. First we retrieve the IDs
* of <emph>all</emph> objects assigned to each selected category using the
* Apply the filter the {@link DataCollection} of the object list. The solution for the query is
* a bit weird, due to limitations of PDL. The simple solution would be to use the predefined
* query {@code objectIDsInMultipleSubtrees} from the {@code Category.pdl} of the module as a
* subquery in the {@code where} clause, like this:
* {@code WHERE parent.id ALL ($objectIDsInMultipleSubtrees)}. But PDL does not support the
* {@code ALL} operator. So we have to use another solution. First we retrieve the IDs of
* <emph>all</emph> objects assigned to each selected category using the
* {@code objectIDsInSubtree} query. Using this IDs we build a long filter. For each selected
* category there is an segment like this: {@code (parent.id IN (1,2,3,4)} The IDs for the
* {@code IN} operator a the ones with the IDs of all objects in category. All segments are
* combined with {@code AND}. If no items are assigned to a category, a segment with {@code 0}
* as ID is added for this category. Because there will never be an object in the database
* with the ID {@code 0} this works.
*
* @param objects
* category there is an segment like this: {@code (parent.id IN (1,2,3,4)} The IDs for the
* {@code IN} operator a the ones with the IDs of all objects in category. All segments are
* combined with {@code AND}. If no items are assigned to a category, a segment with {@code 0}
* as ID is added for this category. Because there will never be an object in the database with
* the ID {@code 0} this works.
*
* @param objects
*/
public void applyFilter(final DataCollection objects) {
if (!values.isEmpty()) {
@ -191,8 +191,8 @@ public class CategoryFilter {
/**
* Creates the XML for the category filter.
*
* @return
*
* @return
*/
public Element getXml() {
final Element filter = new Element("filter");
@ -200,7 +200,7 @@ public class CategoryFilter {
final Element invalid = new Element("invalid");
boolean invalidFound = false;
final StringBuffer searchString = new StringBuffer();
final StringBuffer categoriesStr = new StringBuffer();
//final StringBuffer categoriesStr = new StringBuffer();
filter.addAttribute("type", "categoryFilter");
filter.addAttribute("label", label);
@ -211,15 +211,19 @@ public class CategoryFilter {
Category category;
while (categories.next()) {
category = categories.getCategory();
addCategoryToFilter(categoriesElem, category, searchString);
if (categoriesStr.length() > 0) {
categoriesStr.append("; ");
if (category.hasChildCategories()) {
addCategoryGroupToFilter(categoriesElem, category, searchString);
} else {
addCategoryToFilter(categoriesElem, category, searchString);
// if (categoriesStr.length() > 0) {
// categoriesStr.append("; ");
// }
// categoriesStr.append('"').append(category.getName()).append('"');
}
categoriesStr.append('"').append(category.getName()).append('"');
}
filter.newChildElement("searchString").setText(searchString.toString());
filter.newChildElement("categoriesStr").setText(categoriesStr.toString());
//filter.newChildElement("categoriesStr").setText(categoriesStr.toString());
filter.newChildElement("separator").setText(separator);
final Element multipleElem = filter.newChildElement("multiple");
if (multiple) {
@ -245,6 +249,19 @@ public class CategoryFilter {
return filter;
}
private void addCategoryGroupToFilter(final Element parent,
final Category category,
final StringBuffer searchString) {
final Element elem = parent.newChildElement("categoryGroup");
elem.addAttribute("label", category.getName());
final CategoryCollection childs = category.getChildren();
while(childs.next()) {
final Category child = childs.getCategory();
addCategoryToFilter(elem, child, searchString);
}
}
private void addCategoryToFilter(final Element parent,
final Category category,
final StringBuffer searchString) {
@ -290,5 +307,4 @@ public class CategoryFilter {
}
}
}
}