Modifed the menu component of navigation. It is now possible to configure

the properties for showing grand children from the JSP. Settings in the JSP
will override the settings from configuration.


git-svn-id: https://svn.libreccm.org/ccm/trunk@2831 8810af33-2d31-482b-a856-94f89814c4df
master
jensp 2014-09-03 10:49:51 +00:00
parent 4648c842e8
commit cdc551736f
1 changed files with 165 additions and 123 deletions

View File

@ -15,13 +15,13 @@
* 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.navigation.ui.category;
import com.arsdigita.categorization.Category;
import com.arsdigita.categorization.CategoryCollection;
import com.arsdigita.domain.DomainObjectFactory;
import com.arsdigita.navigation.Navigation;
import com.arsdigita.navigation.NavigationConfig;
import com.arsdigita.persistence.DataCollection;
import com.arsdigita.persistence.SessionManager;
import com.arsdigita.xml.Element;
@ -34,45 +34,35 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
/**
* CategoryTree component displays children of all categories
* in the current path.
* CategoryTree component displays children of all categories in the current path.
*
* Updated cg - TreeCatProvider interface introduced to
* provide the categories to be included in the menu. The
* TreeCatProvider returns a collection of all categories
* that should be included in the tree - the AbstractTree
* organises children under their correct parents. This class
* provides the default implementation of TreeCatProvider.
* A different default provider may be set in config and
* Updated cg - TreeCatProvider interface introduced to provide the categories to be included in the
* menu. The TreeCatProvider returns a collection of all categories that should be included in the
* tree - the AbstractTree organises children under their correct parents. This class provides the
* default implementation of TreeCatProvider. A different default provider may be set in config and
* providers may be registered for specific categories.
*
* Note - the final tree will only include categories where there
* is a complete trail from a category in the path to the
* category output by the provider
* Note - the final tree will only include categories where there is a complete trail from a
* category in the path to the category output by the provider
*
* an alternative provider - PopulatedSubCategoryTreeCatProvider
* is included in this package - it avoids access denied errors
* and blank index pages by restricting the categories under
* the current categories to those that contain at least one item
* visible to the current user
* an alternative provider - PopulatedSubCategoryTreeCatProvider is included in this package - it
* avoids access denied errors and blank index pages by restricting the categories under the current
* categories to those that contain at least one item visible to the current user
*
* Note 2 - changes do not affect the configurable display of
* nephews & grandchildren as these are added to the path list
* Note 2 - changes do not affect the configurable display of nephews & grandchildren as these are
* added to the path list
*/
public class Menu extends AbstractTree implements TreeCatProvider {
/**
*Text for the additional xml attribute for categorys having more children than supposed to be shown.
* Text for the additional xml attribute for categorys having more children than supposed to be
* shown.
*/
public static final String MORE_ATTRIB = "showMore";
@ -84,11 +74,27 @@ public class Menu extends AbstractTree implements TreeCatProvider {
private static Map treeCatProviders = new HashMap();
public static TreeCatProvider defaultProvider = Navigation.getConfig().getDefaultMenuCatProvider();
public static TreeCatProvider defaultProvider = Navigation.getConfig().
getDefaultMenuCatProvider();
private String showGrandChildren;
private long showGrandChildrenMax;
private long showGrandChildrenMin;
private long showGrandChildrenLimit;
private boolean showNephews;
public Menu() {
super();
final NavigationConfig config = Navigation.getConfig();
showGrandChildren = config.getCategoryMenuShowGrandChildren();
showGrandChildrenMin = config.getCategoryMenuShowGrandChildrenMin();
showGrandChildrenMax = config.getCategoryMenuShowGrandChildrenMax();
showGrandChildrenLimit = config.getCategoryMenuShowGrandChildrenLimit();
showNephews = config.getCategoryMenuShowNephews();
}
/**
* enables a different strategy to be used to retrieve categories
* for a specific category
* enables a different strategy to be used to retrieve categories for a specific category
*/
public static void registerTreeCatProvider(Category cat, TreeCatProvider provider) {
s_log.debug("registering "
@ -98,9 +104,48 @@ public class Menu extends AbstractTree implements TreeCatProvider {
treeCatProviders.put(cat, provider);
}
public String getShowGrandChildren() {
return showGrandChildren;
}
public Element generateXML(HttpServletRequest request,
HttpServletResponse response) {
public void setShowGrandChildren(final String showGrandChildren) {
this.showGrandChildren = showGrandChildren;
}
public long getShowGrandChildrenMax() {
return showGrandChildrenMax;
}
public void setShowGrandChildrenMax(final long showGrandChildrenMax) {
this.showGrandChildrenMax = showGrandChildrenMax;
}
public long getShowGrandChildrenMin() {
return showGrandChildrenMin;
}
public void setShowGrandChildrenMin(final long showGrandChildrenMin) {
this.showGrandChildrenMin = showGrandChildrenMin;
}
public long getShowGrandChildrenLimit() {
return showGrandChildrenLimit;
}
public void setShowGrandChildrenLimit(final long showGrandChildrenLimit) {
this.showGrandChildrenLimit = showGrandChildrenLimit;
}
public boolean isShowNephews() {
return showNephews;
}
public void setShowNephews(final boolean showNephews) {
this.showNephews = showNephews;
}
public Element generateXML(final HttpServletRequest request,
final HttpServletResponse response) {
//stores how deep we are inside the menu structure
Category[] path = getModel().getCategoryPath();
@ -115,30 +160,29 @@ public class Menu extends AbstractTree implements TreeCatProvider {
selectedIDsList.add(path[i].getID());
}
BigDecimal[] selectedIDs =
(BigDecimal[]) selectedIDsList.toArray(
BigDecimal[] selectedIDs
= (BigDecimal[]) selectedIDsList.toArray(
new BigDecimal[selectedIDsList.size()]);
// Quasimodo: Begin
// If show_grand_children is set to "true" or "adaptive"
if (Navigation.getConfig().getCategoryMenuShowGrandChildren().equals("true") ||
Navigation.getConfig().getCategoryMenuShowGrandChildren().equals("adaptive")) {
if (showGrandChildren.equals("true") || showGrandChildren.equals("adaptive")) {
// should add the children categories as potential parents
if (path.length > 0) {
// Adaptive Mode
if(Navigation.getConfig().getCategoryMenuShowGrandChildren().equals("adaptive")) {
if (showGrandChildren.equals("adaptive")) {
// Show grand children of the first n levels
for (int path_it = 0;
path_it < path.length && path_it < Navigation.getConfig().getCategoryMenuShowGrandChildrenLimit();
path_it < path.length && path_it < showGrandChildrenLimit;
path_it++) {
// add children of all the catgories along the path until limit is reached
BigDecimal categoryID = path[path_it].getID();
addChildrenOfToList(catIDs, categoryID, categoryID);
if (Navigation.getConfig().getCategoryMenuShowNephews()) {
if (showNephews) {
// should add the sibling categories as potential parents
if (path_it > 1) {
BigDecimal parentID = path[path_it - 1].getID();
@ -158,7 +202,7 @@ public class Menu extends AbstractTree implements TreeCatProvider {
}
// Quasimodo: End
if (Navigation.getConfig().getCategoryMenuShowNephews()) {
if (showNephews) {
// should add the sibling categories as potential parents
if (path.length > 1) { //> 1
BigDecimal categoryID = path[path.length - 1].getID();
@ -176,8 +220,8 @@ public class Menu extends AbstractTree implements TreeCatProvider {
if (s_log.isDebugEnabled()) {
while (treeCats.next()) {
Category cat =
(Category) DomainObjectFactory.newInstance(
Category cat
= (Category) DomainObjectFactory.newInstance(
treeCats.getDataObject());
s_log.debug("treecats - " + cat.getID() + " " + cat.getName());
}
@ -225,16 +269,16 @@ public class Menu extends AbstractTree implements TreeCatProvider {
}
/**
* retrieve data collection of all categories that are to appear in the menu
* Default retrieves all children of categories included in catList.
* retrieve data collection of all categories that are to appear in the menu Default retrieves
* all children of categories included in catList.
*
* @param catIDs
* @param selectedIDs
* @return
*/
public DataCollection getTreeCats(List catIDs, BigDecimal[] selectedIDs) {
DataCollection treeCats =
SessionManager.getSession().retrieve(
DataCollection treeCats
= SessionManager.getSession().retrieve(
Category.BASE_DATA_OBJECT_TYPE);
// Filter to all children of :ids
treeCats.addFilter("parents.id in :ids").set("ids", catIDs);
@ -243,22 +287,21 @@ public class Menu extends AbstractTree implements TreeCatProvider {
return treeCats;
}
/**
* Adds the IDs for the menu entrys to the menu..
*
* @param List catIDs
* @param BigDecimal categoryID
* @param BigDecimal parentID ID of the parent menu entry...
* only those entrys will be added who have this ID as parent id
* @param BigDecimal parentID ID of the parent menu entry... only those entrys will be added who
* have this ID as parent id
*/
protected void addChildrenOfToList(
List catIDs,
BigDecimal categoryID,
BigDecimal parentID) {
BigDecimal childID;
DataCollection childCats =
SessionManager.getSession().retrieve(
DataCollection childCats
= SessionManager.getSession().retrieve(
Category.BASE_DATA_OBJECT_TYPE);
childCats.addFilter("parents.id = :id").set("id", parentID);
childCats.addEqualsFilter("parents.link.relationType", Category.CHILD);
@ -280,11 +323,10 @@ public class Menu extends AbstractTree implements TreeCatProvider {
// com.arsdigita.navigation.category_menu_show_grand_children is not set to adaptive or
// com.arsdigita.navigation.category_menu_show_nephews is set to true and this category has the same parents as the active category or
// this category is part of the current path
if(!(Navigation.getConfig().getCategoryMenuShowGrandChildren().equals("adaptive")) ||
(Navigation.getConfig().getCategoryMenuShowNephews() &&
compareCategoryCollection(cat.getParents(), new Category(selected[selected.length - 1]).getParents())
) ||
Arrays.asList(selected).contains(cat.getID())) {
if (!(showGrandChildren.equals("adaptive"))
|| (showNephews && compareCategoryCollection(
cat.getParents(), new Category(selected[selected.length - 1]).getParents()))
|| Arrays.asList(selected).contains(cat.getID())) {
return new Long(Long.MAX_VALUE).longValue();
@ -294,7 +336,7 @@ public class Menu extends AbstractTree implements TreeCatProvider {
if ((new Category(selected[selected.length - 1])).isMemberOfSubtree(cat)) {
// If com.arsdigita.navigation.category_menu_show_grand_children_max = 0
if(Navigation.getConfig().getCategoryMenuShowGrandChildrenMax() == 0) {
if (showGrandChildrenMax == 0) {
// Show all children
return new Long(Long.MAX_VALUE).longValue();
@ -302,14 +344,14 @@ public class Menu extends AbstractTree implements TreeCatProvider {
} else {
// Show com.arsdigita.navigation.category_menu_show_grand_children_max children
return Navigation.getConfig().getCategoryMenuShowGrandChildrenMax();
return showGrandChildrenMax;
}
} else {
// Show com.arsdigita.navigation.category_menu_show_grand_children_min children
return Navigation.getConfig().getCategoryMenuShowGrandChildrenMin();
return showGrandChildrenMin;
}
}