From: <ssk...@vh...> - 2005-11-30 16:30:09
|
Author: sskracic Date: 2005-11-30 17:28:00 +0100 (Wed, 30 Nov 2005) New Revision: 1020 Added: trunk/ccm-ldn-search/src/com/arsdigita/london/search/ui/CategoryHierarchyFilterWidget.java Modified: trunk/ccm-ldn-search/src/com/arsdigita/london/search/ui/AdvancedQueryComponent.java Log: Integrated sourceforge patch [ 1369317 ] Search by category can include subcats. Modified: trunk/ccm-ldn-search/src/com/arsdigita/london/search/ui/AdvancedQueryComponent.java =================================================================== --- trunk/ccm-ldn-search/src/com/arsdigita/london/search/ui/AdvancedQueryComponent.java 2005-11-30 16:01:55 UTC (rev 1019) +++ trunk/ccm-ldn-search/src/com/arsdigita/london/search/ui/AdvancedQueryComponent.java 2005-11-30 16:28:00 UTC (rev 1020) @@ -5,12 +5,12 @@ * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA @@ -18,35 +18,29 @@ package com.arsdigita.london.search.ui; -import com.arsdigita.bebop.parameters.StringParameter; -import com.arsdigita.bebop.PageState; import com.arsdigita.bebop.Form; -import com.arsdigita.bebop.FormModel; import com.arsdigita.bebop.FormData; +import com.arsdigita.bebop.FormModel; +import com.arsdigita.bebop.PageState; import com.arsdigita.bebop.parameters.ParameterData; - +import com.arsdigita.bebop.parameters.StringParameter; +import com.arsdigita.categorization.Category; +import com.arsdigita.cms.ui.search.ContentTypeFilterWidget; +import com.arsdigita.cms.ui.search.VersionFilterComponent; import com.arsdigita.kernel.permissions.PrivilegeDescriptor; +import com.arsdigita.search.FilterSpecification; +import com.arsdigita.search.Search; +import com.arsdigita.search.filters.ContentSectionFilterSpecification; import com.arsdigita.search.ui.BaseQueryComponent; import com.arsdigita.search.ui.filters.PermissionFilterComponent; -import com.arsdigita.search.ui.filters.SimpleCategoryFilterWidget; -import com.arsdigita.search.Search; -import com.arsdigita.categorization.Category; import com.arsdigita.web.Application; import com.arsdigita.web.Web; -import com.arsdigita.cms.ui.search.VersionFilterComponent; -import com.arsdigita.cms.ui.search.ContentTypeFilterWidget; -import com.arsdigita.cms.SecurityManager; - -import com.arsdigita.search.FilterSpecification; -import com.arsdigita.search.filters.ContentSectionFilterSpecification; - -import org.apache.log4j.Logger; - import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.StringTokenizer; +import org.apache.log4j.Logger; public class AdvancedQueryComponent extends BaseQueryComponent { @@ -59,14 +53,15 @@ public AdvancedQueryComponent(String context) { - // XXX READ instead of CMS_READ_ITEM if (Search.getConfig().isIntermediaEnabled()) { add(new PermissionFilterComponent( PrivilegeDescriptor.READ)); - + Application app = Web.getContext().getApplication(); Category root = Category.getRootForObject(app); - add(new SimpleCategoryFilterWidget(root)); + // amended chr...@we... 29/11/05 + // changed from new SimpleCategoryFilterWidget + add(new CategoryHierarchyFilterWidget(root)); } if (Search.getConfig().isIntermediaEnabled() || @@ -78,11 +73,11 @@ } public void register(Form form, FormModel model) { - s_log.debug("Adding " + m_hiddenAllowedContentSectionsList.getName() + " to form model"); - m_hiddenAllowedContentSectionsList.setPassIn(true); - model.addFormParam(m_hiddenAllowedContentSectionsList); - super.register(form, model); - m_form = form; + s_log.debug("Adding " + m_hiddenAllowedContentSectionsList.getName() + " to form model"); + m_hiddenAllowedContentSectionsList.setPassIn(true); + model.addFormParam(m_hiddenAllowedContentSectionsList); + super.register(form, model); + m_form = form; } @@ -92,16 +87,16 @@ * If present the search will only return content items from these content sections. **/ protected String getContentSections(PageState state) { - FormData formData = m_form.getFormData(state); - if (formData != null) { - ParameterData contentSectionListParam = formData.getParameter(m_hiddenAllowedContentSectionsList.getName()); - String paramValue = (String)contentSectionListParam.getValue(); - m_paramValue = paramValue; - s_log.debug("content sections list is " + paramValue); - is_restricted = (m_paramValue != null && !"".equals(m_paramValue)); - return (String)contentSectionListParam.getValue(); - } - return null; + FormData formData = m_form.getFormData(state); + if (formData != null) { + ParameterData contentSectionListParam = formData.getParameter(m_hiddenAllowedContentSectionsList.getName()); + String paramValue = (String)contentSectionListParam.getValue(); + m_paramValue = paramValue; + s_log.debug("content sections list is " + paramValue); + is_restricted = (m_paramValue != null && !"".equals(m_paramValue)); + return (String)contentSectionListParam.getValue(); + } + return null; } @@ -109,17 +104,17 @@ * Gets an array of content section titles for creation of an inclusion filter. **/ public String[] getContentSectionsArray(PageState state) { - String contentSections = getContentSections(state); - if (contentSections == null) { return null; } - StringTokenizer st = new StringTokenizer(getContentSections(state), ","); - contentSectionTitles = new String[st.countTokens()]; - int index = 0; + String contentSections = getContentSections(state); + if (contentSections == null) { return null; } + StringTokenizer st = new StringTokenizer(getContentSections(state), ","); + contentSectionTitles = new String[st.countTokens()]; + int index = 0; while (st.hasMoreTokens()) { contentSectionTitles[index] = st.nextToken().trim(); s_log.info("Restricting to content section : "+contentSectionTitles[index]); index++; } - return contentSectionTitles; + return contentSectionTitles; } @@ -127,26 +122,26 @@ * Adds the content section filter to any existing filters. **/ protected FilterSpecification[] getFilters(PageState state) { - FilterSpecification[] existingfilters = super.getFilters(state); - List n = new ArrayList(); - try { - List filters = Arrays.asList(existingfilters); // this will throw a NullPointerException if there are no existing filters - n.addAll(filters); - } catch (NullPointerException e) { - // do we need to catch it if we're doing nothing with it? - } - String[] contentSections = getContentSectionsArray(state); - if (contentSections == null) { return existingfilters; } - ContentSectionFilterSpecification csfs = new ContentSectionFilterSpecification(contentSections); - n.add(csfs); - FilterSpecification[] newFilters = new FilterSpecification[n.size()]; - Iterator i = n.iterator(); - int c = 0; - while (i.hasNext()) { - newFilters[c] = (FilterSpecification)i.next(); - c++; - } + FilterSpecification[] existingfilters = super.getFilters(state); + List n = new ArrayList(); + try { + List filters = Arrays.asList(existingfilters); // this will throw a NullPointerException if there are no existing filters + n.addAll(filters); + } catch (NullPointerException e) { + // do we need to catch it if we're doing nothing with it? + } + String[] contentSections = getContentSectionsArray(state); + if (contentSections == null) { return existingfilters; } + ContentSectionFilterSpecification csfs = new ContentSectionFilterSpecification(contentSections); + n.add(csfs); + FilterSpecification[] newFilters = new FilterSpecification[n.size()]; + Iterator i = n.iterator(); + int c = 0; + while (i.hasNext()) { + newFilters[c] = (FilterSpecification)i.next(); + c++; + } - return newFilters; + return newFilters; } } Added: trunk/ccm-ldn-search/src/com/arsdigita/london/search/ui/CategoryHierarchyFilterWidget.java =================================================================== --- trunk/ccm-ldn-search/src/com/arsdigita/london/search/ui/CategoryHierarchyFilterWidget.java 2005-11-30 16:01:55 UTC (rev 1019) +++ trunk/ccm-ldn-search/src/com/arsdigita/london/search/ui/CategoryHierarchyFilterWidget.java 2005-11-30 16:28:00 UTC (rev 1020) @@ -0,0 +1,115 @@ +/* + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ +package com.arsdigita.london.search.ui; + +import com.arsdigita.bebop.Form; +import com.arsdigita.bebop.FormData; +import com.arsdigita.bebop.FormModel; +import com.arsdigita.bebop.PageState; +import com.arsdigita.bebop.parameters.ParameterData; +import com.arsdigita.bebop.parameters.StringParameter; +import com.arsdigita.categorization.Category; +import com.arsdigita.domain.DomainObjectFactory; +import com.arsdigita.persistence.OID; +import com.arsdigita.search.FilterSpecification; +import com.arsdigita.search.Search; +import com.arsdigita.search.filters.CategoryFilterSpecification; +import com.arsdigita.search.ui.filters.SimpleCategoryFilterWidget; +import com.arsdigita.xml.Element; +import java.util.HashSet; +import java.util.Set; + +/** + * @author chr...@we... + * + * Category filter widget that includes an extra parameter that can be used to + * search all the subcategories of the selected categories + * + * + * + */ +public class CategoryHierarchyFilterWidget extends SimpleCategoryFilterWidget { + + private Form m_form; + private StringParameter includeCategoryHierarchy; + /** + * @param root the root category for categories available for selection + */ + public CategoryHierarchyFilterWidget(Category root) { + this(new Category[] { root }); + + } + + public CategoryHierarchyFilterWidget(Category[] roots) { + super(roots); + includeCategoryHierarchy = new StringParameter("subcats"); + + } + + /** + * adds additional parameter to the underlying form model + */ + public void register(Form form, FormModel model) { + super.register(form, model); + model.addFormParam(includeCategoryHierarchy); + m_form = form; + } + + /** + * + * returns a FilterSpecification that includes the selected categories + * and if subcats parameter has a value, then includes all descendent + * categories of the selected categories + */ + public FilterSpecification getFilter(PageState state) { + OID[] oids = (OID[]) getValue(state); + if (oids == null) { + oids = new OID[0]; + } + FormData fd = m_form.getFormData(state); + boolean includeSubCats = false; + if (fd != null) { + ParameterData data = + fd.getParameter(includeCategoryHierarchy.getName()); + includeSubCats = data.getValue() != null; + } + Set includedCats = new HashSet(); + + for (int i = 0; i < oids.length; i++) { + Category cat = (Category) DomainObjectFactory.newInstance(oids[i]); + includedCats.add(cat); + } + Category[] cats = (Category []) includedCats.toArray(new Category[0]); + return new CategoryFilterSpecification(cats, includeSubCats); + } + + /** + * + * adds xml for the extra parameter to the <search:filter param="category" type="category"> element. + * The xml for the parameter is <search:includeSubCats name="subcats" value="true" /> which can be + * transformed into a checkbox or a hidden input + */ + public void generateBodyXML(PageState state, Element parent) { + super.generateBodyXML(state, parent); + Element includeSubCats = Search.newElement("includeSubCats"); + includeSubCats.addAttribute("name", includeCategoryHierarchy.getName()); + includeSubCats.addAttribute("value", "true"); + + parent.addContent(includeSubCats); + } + +} Property changes on: trunk/ccm-ldn-search/src/com/arsdigita/london/search/ui/CategoryHierarchyFilterWidget.java ___________________________________________________________________ Name: svn:keywords + Id Author URL |