Author: pboy Date: 2012-12-17 11:16:00 +0000 (Mon, 17 Dec 2012) New Revision: 2407 Added: trunk/ccm-cms/src/com/arsdigita/cms/ui/category/OrderedCategorizedObjectsList.java.nolongerinuse Removed: trunk/ccm-cms/src/com/arsdigita/cms/ui/category/OrderedCategorizedObjectsList.java Modified: trunk/ccm-cms/src/com/arsdigita/cms/ContentBundle.java trunk/ccm-cms/src/com/arsdigita/cms/ContentItem.java trunk/ccm-cms/src/com/arsdigita/cms/dispatcher/BaseImage.java trunk/ccm-cms/src/com/arsdigita/cms/ui/category/CategorizedObjectsList.java trunk/ccm-cms/src/com/arsdigita/cms/ui/category/SortableCategoryList.java trunk/ccm-core/src/com/arsdigita/categorization/Category.java trunk/ccm-core/src/com/arsdigita/categorization/CategoryCollection.java Log: Fixed sort order in object lists. (on behalf of JensP) Modified: trunk/ccm-cms/src/com/arsdigita/cms/ContentBundle.java =================================================================== --- trunk/ccm-cms/src/com/arsdigita/cms/ContentBundle.java 2012-12-17 11:10:34 UTC (rev 2406) +++ trunk/ccm-cms/src/com/arsdigita/cms/ContentBundle.java 2012-12-17 11:16:00 UTC (rev 2407) @@ -670,7 +670,8 @@ CategoryCollection categories = source.getCategoryCollection(); while (categories.next()) { final Category category = categories.getCategory(); - category.addChild(this); + + category.addChild(this, categories.getSortKey()); category.save(); // XXX remove me } categories.close(); Modified: trunk/ccm-cms/src/com/arsdigita/cms/ContentItem.java =================================================================== --- trunk/ccm-cms/src/com/arsdigita/cms/ContentItem.java 2012-12-17 11:10:34 UTC (rev 2406) +++ trunk/ccm-cms/src/com/arsdigita/cms/ContentItem.java 2012-12-17 11:16:00 UTC (rev 2407) @@ -1893,6 +1893,7 @@ } // If live Bundle already exists, recategorize. + // jensp 2012: Behavior changed. The ContentBundle will also be republished. if (PARENT.equals(attribute)) { ACSObject parent = ((ContentItem) source).getParent(); if (parent != null && copier.getCopyType() @@ -1906,9 +1907,7 @@ //published because the ContentBundle was not republished. //Moved the next lines out of the if below to enable //republishing of the ContentBundle - final ContentBundle liveBundle = - (ContentBundle) bundle. - createPendingVersion(null); + final ContentBundle liveBundle = (ContentBundle) bundle.createPendingVersion(null); /* * if (liveBundle == null) { } else { Set liveCatSet = new * HashSet(); Set draftCatSet = new HashSet(); Modified: trunk/ccm-cms/src/com/arsdigita/cms/dispatcher/BaseImage.java =================================================================== --- trunk/ccm-cms/src/com/arsdigita/cms/dispatcher/BaseImage.java 2012-12-17 11:10:34 UTC (rev 2406) +++ trunk/ccm-cms/src/com/arsdigita/cms/dispatcher/BaseImage.java 2012-12-17 11:16:00 UTC (rev 2407) @@ -71,7 +71,7 @@ CMS.getConfig().getImageCacheMaxSize()); } } - private final bool IMAGE_CACHE_PREFETCH = CMS.getConfig().getImageCachePrefetchEnabled(); + private final boolean IMAGE_CACHE_PREFETCH = CMS.getConfig().getImageCachePrefetchEnabled(); private static final Logger s_log = Logger.getLogger(BaseImage.class); /** Modified: trunk/ccm-cms/src/com/arsdigita/cms/ui/category/CategorizedObjectsList.java =================================================================== --- trunk/ccm-cms/src/com/arsdigita/cms/ui/category/CategorizedObjectsList.java 2012-12-17 11:10:34 UTC (rev 2406) +++ trunk/ccm-cms/src/com/arsdigita/cms/ui/category/CategorizedObjectsList.java 2012-12-17 11:16:00 UTC (rev 2407) @@ -35,7 +35,6 @@ import java.math.BigDecimal; import javax.servlet.ServletException; - /** * A List of all objects currently categorized under this category * @@ -52,25 +51,36 @@ setModelBuilder(new CategorizedObjectsModelBuilder()); - Label label = new Label(GlobalizationUtil.globalize - ("cms.ui.category.item.none")); + Label label = new Label(GlobalizationUtil.globalize("cms.ui.category.item.none")); label.setFontWeight(Label.ITALIC); setEmptyView(label); } /** - * This actually performs the sorting + * This actually performs the sorting */ public void respond(PageState ps) throws ServletException { - String event = ps.getControlEventName(); + final String event = ps.getControlEventName(); if (NEXT_EVENT.equals(event) || PREV_EVENT.equals(event)) { - BigDecimal selectedID = new BigDecimal(ps.getControlEventValue()); - Category parent = getCategory(ps); + final BigDecimal selectedID = new BigDecimal(ps.getControlEventValue()); + final Category parent = getCategory(ps); + + final ContentItem selectedItem = new ContentItem(selectedID); + final BigDecimal selectedDraftId = selectedItem.getDraftVersion().getID(); - if (CMS.getContext().getSecurityManager().canAccess - (SecurityManager.CATEGORY_ADMIN)) { - parent.swapSortKeys(selectedID, getSwapID(parent,selectedID,event)); + if (CMS.getContext().getSecurityManager().canAccess(SecurityManager.CATEGORY_ADMIN)) { + final BigDecimal swapId = getSwapID(parent, selectedID, event); + parent.swapSortKeys(selectedID, swapId); + final ContentItem swapItem = new ContentItem(swapId); + final BigDecimal swapDraftId = swapItem.getDraftVersion().getID(); + + final BigDecimal sortKey1 = parent.getSortKey(selectedItem); + final BigDecimal sortKey2 = parent.getSortKey(swapItem); + + parent.setSortKey(new ContentItem(selectedDraftId), sortKey1); + parent.setSortKey(new ContentItem(swapDraftId), sortKey2); + } } else { super.respond(ps); @@ -83,9 +93,8 @@ boolean foundSelectedID = false; if (category != null && category.hasChildObjects()) { - CategorizedCollection items = category.getObjects - (ContentItem.BASE_DATA_OBJECT_TYPE); - items.addEqualsFilter(ContentItem.VERSION,ContentItem.LIVE); + CategorizedCollection items = category.getObjects(ContentItem.BASE_DATA_OBJECT_TYPE); + items.addEqualsFilter(ContentItem.VERSION, ContentItem.LIVE); items.sort(true); while (items.next()) { BigDecimal thisID = items.getACSObject().getID(); @@ -111,14 +120,14 @@ private class CategorizedObjectsModelBuilder extends LockableImpl implements ListModelBuilder { + public final ListModel makeModel(final List list, final PageState state) { final Category category = getCategory(state); if (category != null && category.hasChildObjects()) { - CategorizedCollection items = category.getObjects - (ContentItem.BASE_DATA_OBJECT_TYPE); - items.addEqualsFilter(ContentItem.VERSION,ContentItem.LIVE); + CategorizedCollection items = category.getObjects(ContentItem.BASE_DATA_OBJECT_TYPE); + items.addEqualsFilter(ContentItem.VERSION, ContentItem.LIVE); items.sort(true); return new CategorizedCollectionListModel(items); } else { @@ -131,6 +140,7 @@ * A {@link ListModel} that iterates over categorized objects via an iterator */ private static class CategorizedCollectionListModel implements ListModel { + private CategorizedCollection m_objs; private ACSObject m_object; @@ -140,7 +150,7 @@ } public boolean next() { - if ( m_objs.next() ) { + if (m_objs.next()) { m_object = (ACSObject) m_objs.getDomainObject(); return true; } else { Deleted: trunk/ccm-cms/src/com/arsdigita/cms/ui/category/OrderedCategorizedObjectsList.java =================================================================== --- trunk/ccm-cms/src/com/arsdigita/cms/ui/category/OrderedCategorizedObjectsList.java 2012-12-17 11:10:34 UTC (rev 2406) +++ trunk/ccm-cms/src/com/arsdigita/cms/ui/category/OrderedCategorizedObjectsList.java 2012-12-17 11:16:00 UTC (rev 2407) @@ -1,171 +0,0 @@ -/* - * Copyright (C) 2002-2004 Red Hat Inc. All Rights Reserved. - * - * 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.cms.ui.category; - -import com.arsdigita.bebop.Component; -import com.arsdigita.bebop.ControlLink; -import com.arsdigita.bebop.Label; -import com.arsdigita.bebop.PageState; -import com.arsdigita.bebop.list.ListModel; -import com.arsdigita.bebop.util.BebopConstants; -import com.arsdigita.categorization.Category; -import com.arsdigita.cms.CMS; -import com.arsdigita.cms.ContentItem; -import com.arsdigita.domain.DataObjectNotFoundException; -import com.arsdigita.util.Assert; -import com.arsdigita.xml.Element; -import java.io.IOException; -import java.math.BigDecimal; -import javax.servlet.ServletException; - -/** - * This list offers the option for the code to provide the developer - * with links to sort the given categories. - * - * NOTE: This UI currently does not scale well with large numbers of items - * since it just lists all of them. It would probably be nice to integrate - * a paginator as well to as to allow the user to move an item in large - * distances and to insert an item in the middle. Right now, when you add - * an item it is just placed at the end. However, if you want the item to - * appear in the middle then you must hit the "up" arrow n/2 times where - * n is the number of items in the list. This clearly is not a good setup. - * - * @author Randy Graebner (ra...@al...) - * @version $Revision: #11 $ $DateTime: 2004/08/17 23:15:09 $ - * @version $Id$ - */ -public class OrderedCategorizedObjectsList extends CategorizedObjectsList { - - private static final org.apache.log4j.Logger s_log = - org.apache.log4j.Logger.getLogger(OrderedCategorizedObjectsList.class); - - // It would be really nice if this used the save variable as is - // used by List but because List has it as private, we cannot do that. - private static final String SELECT_EVENT = "s"; - private static final String PREV_EVENT = "prev"; - private static final String NEXT_EVENT = "next"; - - /** - * This just makes a standard - * {@link OrderedCategorizedObjectsList} - */ - public OrderedCategorizedObjectsList(final CategoryRequestLocal category) { - super(category); - } - - - /** - * This geneates the XML as specified by the arguments pass in to - * the constructor. - */ - public void generateXML(PageState state, Element parent) { - if ( ! isVisible(state) ) { - return; - } - - // They want the special sort items - ListModel m = getModel(state); - - if (!m.next()) { - super.generateXML(state, parent); - return; - } - - // because m.next() returned true, we know there are items - // in the list - Element list = parent.newChildElement - ("cms:orderedCategorizedObjectList", CMS.CMS_XML_NS); - exportAttributes(list); - - Component c; - Object selKey = getSelectedKey(state); - int i = 0; - boolean hasNext; - do { - Element item = list.newChildElement - (BebopConstants.BEBOP_CELL, BEBOP_XML_NS); - item.addAttribute("configure", "true"); - - String key = m.getKey(); - Assert.exists(key); - - // Converting both keys to String for comparison - // since ListModel.getKey returns a String - boolean selected = (selKey != null) && - key.equals(selKey.toString()); - - if ( selected ) { - item.addAttribute("selected", "selected"); - } - state.setControlEvent(this, SELECT_EVENT, key); - Label l = new Label(m.getElement().toString()); - c = new ControlLink(l); - c.generateXML(state, item); - - hasNext = m.next(); - - // Add attributes containing URLs that fire control events - // for various portlet actions - try { - // Maybe add attribute containing URL for "move up" link - if (i > 0) { - state.setControlEvent(this, PREV_EVENT, key); - item.addAttribute("prevURL", state.stateAsURL()); - } - - // Maybe add attribute containing URL for "move down" link - if (hasNext) { - state.setControlEvent(this, NEXT_EVENT, key); - item.addAttribute("nextURL", state.stateAsURL()); - } - - } catch (IOException ex) { - throw new IllegalStateException("Caught IOException: " + - ex.getMessage()); - } - i++; - } while (hasNext); - - state.clearControlEvent(); - } - - public void respond(PageState ps) throws ServletException { - String event = ps.getControlEventName(); - - if (NEXT_EVENT.equals(event) || PREV_EVENT.equals(event)) { - try { - ContentItem child = - new ContentItem(new BigDecimal(ps.getControlEventValue())); - final Category parent = getCategory(ps); - - if (NEXT_EVENT.equals(event)) { - parent.swapWithNext(child); - } else { - parent.swapWithPrevious(child); - } - parent.save(); - } catch (DataObjectNotFoundException e) { - s_log.error("Trying to create categories with state = " + ps, e); - throw new ServletException(e); - } - } else { - super.respond(ps); - } - } -} Added: trunk/ccm-cms/src/com/arsdigita/cms/ui/category/OrderedCategorizedObjectsList.java.nolongerinuse =================================================================== --- trunk/ccm-cms/src/com/arsdigita/cms/ui/category/OrderedCategorizedObjectsList.java.nolongerinuse (rev 0) +++ trunk/ccm-cms/src/com/arsdigita/cms/ui/category/OrderedCategorizedObjectsList.java.nolongerinuse 2012-12-17 11:16:00 UTC (rev 2407) @@ -0,0 +1,181 @@ +/* + * Copyright (C) 2002-2004 Red Hat Inc. All Rights Reserved. + * + * 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.cms.ui.category; + +import com.arsdigita.bebop.Component; +import com.arsdigita.bebop.ControlLink; +import com.arsdigita.bebop.Label; +import com.arsdigita.bebop.PageState; +import com.arsdigita.bebop.list.ListModel; +import com.arsdigita.bebop.util.BebopConstants; +import com.arsdigita.categorization.Category; +import com.arsdigita.cms.CMS; +import com.arsdigita.cms.ContentItem; +import com.arsdigita.domain.DataObjectNotFoundException; +import com.arsdigita.util.Assert; +import com.arsdigita.xml.Element; +import java.io.IOException; +import java.math.BigDecimal; +import javax.servlet.ServletException; + +/** + * This list offers the option for the code to provide the developer + * with links to sort the given categories. + * + * NOTE: This UI currently does not scale well with large numbers of items + * since it just lists all of them. It would probably be nice to integrate + * a paginator as well to as to allow the user to move an item in large + * distances and to insert an item in the middle. Right now, when you add + * an item it is just placed at the end. However, if you want the item to + * appear in the middle then you must hit the "up" arrow n/2 times where + * n is the number of items in the list. This clearly is not a good setup. + * + * @author Randy Graebner (ra...@al...) + * @version $Revision: #11 $ $DateTime: 2004/08/17 23:15:09 $ + * @version $Id: OrderedCategorizedObjectsList.java 2090 2010-04-17 08:04:14Z pboy $ + */ +public class OrderedCategorizedObjectsList extends CategorizedObjectsList { + + private static final org.apache.log4j.Logger s_log = + org.apache.log4j.Logger.getLogger(OrderedCategorizedObjectsList.class); + + // It would be really nice if this used the save variable as is + // used by List but because List has it as private, we cannot do that. + private static final String SELECT_EVENT = "s"; + private static final String PREV_EVENT = "prev"; + private static final String NEXT_EVENT = "next"; + + /** + * This just makes a standard + * {@link OrderedCategorizedObjectsList} + */ + public OrderedCategorizedObjectsList(final CategoryRequestLocal category) { + super(category); + } + + + /** + * This geneates the XML as specified by the arguments pass in to + * the constructor. + */ + public void generateXML(PageState state, Element parent) { + if ( ! isVisible(state) ) { + return; + } + + // They want the special sort items + ListModel m = getModel(state); + + if (!m.next()) { + super.generateXML(state, parent); + return; + } + + // because m.next() returned true, we know there are items + // in the list + Element list = parent.newChildElement + ("cms:orderedCategorizedObjectList", CMS.CMS_XML_NS); + exportAttributes(list); + + Component c; + Object selKey = getSelectedKey(state); + int i = 0; + boolean hasNext; + do { + Element item = list.newChildElement + (BebopConstants.BEBOP_CELL, BEBOP_XML_NS); + item.addAttribute("configure", "true"); + + String key = m.getKey(); + Assert.exists(key); + + // Converting both keys to String for comparison + // since ListModel.getKey returns a String + boolean selected = (selKey != null) && + key.equals(selKey.toString()); + + if ( selected ) { + item.addAttribute("selected", "selected"); + } + state.setControlEvent(this, SELECT_EVENT, key); + Label l = new Label(m.getElement().toString()); + c = new ControlLink(l); + c.generateXML(state, item); + + hasNext = m.next(); + + // Add attributes containing URLs that fire control events + // for various portlet actions + try { + // Maybe add attribute containing URL for "move up" link + if (i > 0) { + state.setControlEvent(this, PREV_EVENT, key); + item.addAttribute("prevURL", state.stateAsURL()); + } + + // Maybe add attribute containing URL for "move down" link + if (hasNext) { + state.setControlEvent(this, NEXT_EVENT, key); + item.addAttribute("nextURL", state.stateAsURL()); + } + + } catch (IOException ex) { + throw new IllegalStateException("Caught IOException: " + + ex.getMessage()); + } + i++; + } while (hasNext); + + state.clearControlEvent(); + } + + public void respond(PageState ps) throws ServletException { + String event = ps.getControlEventName(); + + if (NEXT_EVENT.equals(event) || PREV_EVENT.equals(event)) { + try { + ContentItem child = new ContentItem(new BigDecimal(ps.getControlEventValue())); + ContentItem draft = null; + if (ContentItem.LIVE.equals(child.getVersion())) { + draft = child.getDraftVersion(); + } + final Category parent = getCategory(ps); + + if (NEXT_EVENT.equals(event)) { + parent.swapWithNext(child); + if (draft != null) { + parent.swapWithNext(draft); + } + } else { + parent.swapWithPrevious(child); + if (draft != null) { + parent.swapWithPrevious(draft); + } + } + parent.save(); + + } catch (DataObjectNotFoundException e) { + s_log.error("Trying to create categories with state = " + ps, e); + throw new ServletException(e); + } + } else { + super.respond(ps); + } + } +} Modified: trunk/ccm-cms/src/com/arsdigita/cms/ui/category/SortableCategoryList.java =================================================================== --- trunk/ccm-cms/src/com/arsdigita/cms/ui/category/SortableCategoryList.java 2012-12-17 11:10:34 UTC (rev 2406) +++ trunk/ccm-cms/src/com/arsdigita/cms/ui/category/SortableCategoryList.java 2012-12-17 11:16:00 UTC (rev 2407) @@ -23,6 +23,7 @@ import com.arsdigita.bebop.parameters.BigDecimalParameter; import com.arsdigita.categorization.Category; import com.arsdigita.cms.CMS; +import com.arsdigita.cms.ContentItem; import com.arsdigita.cms.ContentSection; import com.arsdigita.cms.SecurityManager; import com.arsdigita.cms.ui.SortableList; @@ -100,7 +101,7 @@ } parent.save(); - } + } } catch (DataObjectNotFoundException e) { s_log.error("Trying to create categories with state = " + ps, e); throw new ServletException(e); Modified: trunk/ccm-core/src/com/arsdigita/categorization/Category.java =================================================================== --- trunk/ccm-core/src/com/arsdigita/categorization/Category.java 2012-12-17 11:10:34 UTC (rev 2406) +++ trunk/ccm-core/src/com/arsdigita/categorization/Category.java 2012-12-17 11:16:00 UTC (rev 2407) @@ -109,9 +109,9 @@ * */ public static final String BASE_DATA_OBJECT_TYPE = - "com.arsdigita.categorization.Category"; + "com.arsdigita.categorization.Category"; private static final String BASE_DATA_OBJECT_PACKAGE = - "com.arsdigita.categorization"; + "com.arsdigita.categorization"; /** * The * <code>PrivilegeDescriptor corresponding</code> to the privilege to @@ -119,7 +119,7 @@ * */ public static final PrivilegeDescriptor MAP_DESCRIPTOR = - new PrivilegeDescriptor( + new PrivilegeDescriptor( "map_to_category"); // Quasimodo: Begin private static CategorizationConfig s_config = CategorizationConfig @@ -185,7 +185,7 @@ * An attribute name for the underlying data object. */ public static final String IGNORE_PARENT_INDEX_ITEM = - "ignoreParentIndexItem"; + "ignoreParentIndexItem"; /** * An attribute name for the underlying data object. */ @@ -197,7 +197,7 @@ public static final String LOCALIZATIONS = "localizations"; // some named queries in the pdl files private static final String CHILD_CATEGORY_IDS = - "com.arsdigita.categorization.childCategoryIDs"; + "com.arsdigita.categorization.childCategoryIDs"; private static final String CURRENT_SORT_KEY = "currentSortKey"; private HierarchyDenormalization m_hierarchy; // Quasimodo: Begin @@ -416,7 +416,8 @@ public String getName(String locale) { // Test for localized version - if (locale != null && !locale.isEmpty() && m_categoryLocalizationCollection != null && m_categoryLocalizationCollection. + if (locale != null && !locale.isEmpty() && m_categoryLocalizationCollection != null + && m_categoryLocalizationCollection. localizationExists(locale)) { // Return value of isEnabled from localized version, so categories could be disabled depending on locale @@ -517,7 +518,7 @@ * @return category path */ public String getPreferredQualifiedName(String delimiter, - boolean includeRoot) { + boolean includeRoot) { // is this a synonym? CategoryCollection cc = new CategoryCollection(getRelatedCategories( PREFERRED)); @@ -525,7 +526,7 @@ Category preferred = cc.getCategory(); cc.close(); String preferredPath = preferred.getQualifiedName(delimiter, - includeRoot); + includeRoot); return preferredPath + " (" + getName() + ")"; } else { return getQualifiedName(delimiter, includeRoot); @@ -602,7 +603,8 @@ // Test for localized version // HACK - if (locale != null && !locale.isEmpty() && m_categoryLocalizationCollection != null && m_categoryLocalizationCollection. + if (locale != null && !locale.isEmpty() && m_categoryLocalizationCollection != null + && m_categoryLocalizationCollection. localizationExists(locale)) { // Return value of isEnabled from localized version, so categories could be disabled depending on locale @@ -679,7 +681,8 @@ public String getURL(String locale) { // Test for localized version - if (locale != null && !locale.isEmpty() && m_categoryLocalizationCollection != null && m_categoryLocalizationCollection. + if (locale != null && !locale.isEmpty() && m_categoryLocalizationCollection != null + && m_categoryLocalizationCollection. localizationExists(locale)) { // Return value of isEnabled from localized version, so categories could be disabled depending on locale @@ -751,7 +754,8 @@ } // Test for localized version - if (locale != null && !locale.isEmpty() && m_categoryLocalizationCollection != null && m_categoryLocalizationCollection. + if (locale != null && !locale.isEmpty() && m_categoryLocalizationCollection != null + && m_categoryLocalizationCollection. localizationExists(locale)) { // Return value of isEnabled from localized version, so categories could be disabled depending on locale @@ -950,8 +954,8 @@ try { if (children.next()) { throw new CategorizationException("This category is the default parent of another category." - + " You must explicitly delete the child categories first. " - + "Child category: " + children. + + " You must explicitly delete the child categories first. " + + "Child category: " + children. getDataObject()); } } finally { @@ -959,13 +963,13 @@ } DataAssociationCursor objects = - ((DataAssociation) get(CHILD_OBJECTS)).cursor(); + ((DataAssociation) get(CHILD_OBJECTS)).cursor(); if (objects != null) { try { if (objects.next()) { throw new CategorizationException("This category has child objects. You must delete " - + " any such objects explicitly, before deleting the " - + " category. Child object: " + objects. + + " any such objects explicitly, before deleting the " + + " category. Child object: " + objects. getDataObject()); } } finally { @@ -999,7 +1003,7 @@ public void deleteCategorySubtree() { // we get the association between this category and its children DataAssociationCursor cursor = - ((DataAssociation) get(RELATED_CATEGORIES)).cursor(); + ((DataAssociation) get(RELATED_CATEGORIES)).cursor(); while (cursor.next()) { DataObject link = cursor.getLink(); @@ -1038,7 +1042,7 @@ } DataAssociationCursor cursor = - ((DataAssociation) get(RELATED_CATEGORIES)).cursor(); + ((DataAssociation) get(RELATED_CATEGORIES)).cursor(); while (cursor.next()) { DataObject link = cursor.getLink(); @@ -1047,7 +1051,7 @@ remove(RELATED_CATEGORIES, cursor.getDataObject()); if ("child".equals(relationType)) { Category category = new Category(cursor.getDataObject()); - parent.addChild(category); + parent.addChild(category, null); if (Boolean.TRUE.equals(isDefault)) { category.setDefaultParentCategory(parent); } @@ -1104,13 +1108,18 @@ * categories and becomes a CHILD category.</p> * * @param object the domain object to categorize + * @param sortKey optional sort key. May be <code>null</code> * * @pre !isAbstract() * @pre canMap() * */ + public void addChild(ACSObject object, BigDecimal sortKey) { + addMapping(object, "child", sortKey); + } + public void addChild(ACSObject object) { - addMapping(object, "child"); + addChild(object, null); } /** @@ -1149,21 +1158,25 @@ /** * Adds the passed in object to the correct association. + * + * @param acsObj Object to add to the category + * @param relationType Type of the relation + * @param sortKey Optional sort key. May be <code>null</code>. * * @pre canMap() */ - private void addMapping(ACSObject acsObj, String relationType) { + private void addMapping(ACSObject acsObj, String relationType, BigDecimal sortKey) { if (acsObj instanceof Category) { addMapping((Category) acsObj, relationType); return; } Assert.isFalse(isAbstract(), - "You cannot categorize an object " - + "within an abstract category. If you are " - + "seeing this message then your UI is " - + "allowing you to do something that you " - + "are not allowed to do and you " - + "should email your site administrator."); + "You cannot categorize an object " + + "within an abstract category. If you are " + + "seeing this message then your UI is " + + "allowing you to do something that you " + + "are not allowed to do and you " + + "should email your site administrator."); if (RELATED.equals(relationType)) { throw new CategorizationException( @@ -1172,21 +1185,24 @@ } DataAssociationCursor cursor = - ((DataAssociation) get(CHILD_OBJECTS)).cursor(); + ((DataAssociation) get(CHILD_OBJECTS)).cursor(); cursor.addEqualsFilter(ID, acsObj.getID()); if (cursor.size() == 0) { // if the cursor.size() > 0 then the object is already // a child and does not need to be added again. - add(CHILD_OBJECTS, acsObj); + DataObject link = add(CHILD_OBJECTS, acsObj); + if (sortKey != null) { + link.set("sortKey", sortKey); + } Categorization.triggerMapEvent(this, acsObj); if (s_log.isDebugEnabled()) { s_log.debug(acsObj + " added to " + CHILD_OBJECTS + " of catID=" - + getID() + " type=" + relationType + " (ignored)"); + + getID() + " type=" + relationType + " (ignored)"); } } else { if (s_log.isDebugEnabled()) { s_log.debug(acsObj + " is already related to catID=" + getID() - + " type=" + relationType + " (ignored)"); + + " type=" + relationType + " (ignored)"); } } } @@ -1195,8 +1211,8 @@ // Let's check for loops. if ("child".equals(relationType) && category.isMemberOfSubtree(this)) { throw new CategorizationException("The object that you are " - + "trying to add as a child is already " - + "a member of the subtree."); + + "trying to add as a child is already " + + "a member of the subtree."); } @@ -1204,7 +1220,7 @@ // Otherwise, we add and then update. DataAssociationCursor cursor = - ((DataAssociation) get(RELATED_CATEGORIES)).cursor(); + ((DataAssociation) get(RELATED_CATEGORIES)).cursor(); cursor.addEqualsFilter(ID, category.getID()); DataObject link; if (cursor.next()) { @@ -1223,7 +1239,7 @@ cursor.close(); if (s_log.isDebugEnabled()) { s_log.debug("updating related catID=" + category.getID() + " type=" - + relationType + " default=false"); + + relationType + " default=false"); } link.set(REL_TYPE, relationType); @@ -1400,7 +1416,7 @@ */ public long getParentCategoryCount() { DataAssociationCursor cursor = - ((DataAssociation) get(PARENTS)).cursor(); + ((DataAssociation) get(PARENTS)).cursor(); try { return cursor.size(); } finally { @@ -1432,7 +1448,7 @@ boolean found = false; DataAssociationCursor cursor = - ((DataAssociation) get(PARENTS)).cursor(); + ((DataAssociation) get(PARENTS)).cursor(); while (cursor.next()) { DataObject category = cursor.getDataObject(); DataObject link = cursor.getLink(); @@ -1468,7 +1484,7 @@ } DataAssociationCursor cursor = - ((DataAssociation) get(CHILD_OBJECTS)).cursor(); + ((DataAssociation) get(CHILD_OBJECTS)).cursor(); cursor.addEqualsFilter(ID, child.getID()); if (cursor.next()) { @@ -1534,7 +1550,7 @@ } DataAssociationCursor cursor = - ((DataAssociation) get(CHILD_OBJECTS)).cursor(); + ((DataAssociation) get(CHILD_OBJECTS)).cursor(); cursor.addEqualsFilter(ID, child.getID()); if (cursor.next()) { @@ -1590,7 +1606,7 @@ private void swapCategoryKeys(int key, int nextKey) { swapKeys(getSession().retrieveDataOperation( "com.arsdigita.categorization.swapCategoryWithNextCategory"), - key, nextKey); + key, nextKey); } /** @@ -1599,7 +1615,7 @@ private void swapObjectKeys(int key, int nextKey) { swapKeys(getSession().retrieveDataOperation( "com.arsdigita.categorization.swapObjectWithNextObject"), - key, nextKey); + key, nextKey); } /** @@ -1628,6 +1644,24 @@ } } + public BigDecimal getSortKey(final ACSObject object) { + return getSortKey(object.getID()); + } + + public BigDecimal getSortKey(final BigDecimal objId) { + final DataAssociationCursor cursor = ((DataAssociation) get(CHILD_OBJECTS)).cursor(); + cursor.addEqualsFilter(ID, objId); + if (cursor.next()) { + return (BigDecimal) cursor.getLink().get(SORT_KEY); + } + + return null; + } + + public void setSortKey(ACSObject object, int key) { + setSortKey(object, new BigDecimal(key)); + } + /** * Explicitly sets the sort key for this child object (category or * otherwise). @@ -1635,19 +1669,19 @@ * @param child The child object or category to set the sortKey for * @param key The integer to use for the sortKey */ - public void setSortKey(ACSObject child, int key) { + public void setSortKey(ACSObject child, BigDecimal key) { if (isCategory(child)) { setSortKey((Category) child, key); return; } DataAssociationCursor cursor = - ((DataAssociation) get(CHILD_OBJECTS)).cursor(); + ((DataAssociation) get(CHILD_OBJECTS)).cursor(); cursor.addEqualsFilter(ID, child.getID()); if (cursor.next()) { DataObject link = cursor.getLink(); - link.set(SORT_KEY, new BigDecimal(key)); + link.set(SORT_KEY, key); } cursor.close(); } @@ -1668,7 +1702,7 @@ BigDecimal key2; DataAssociationCursor cursor = - ((DataAssociation) get(CHILD_OBJECTS)).cursor(); + ((DataAssociation) get(CHILD_OBJECTS)).cursor(); cursor.addEqualsFilter(ID, childID1); if (cursor.next()) { link1 = cursor.getLink(); @@ -1714,10 +1748,10 @@ */ public DataAssociationCursor getRelatedCategories(String relation) { Assert.isTrue(relation.equals(CHILD) || relation.equals(RELATED) - || relation.equals(PREFERRED), - " invalid relation {" + relation + "}"); + || relation.equals(PREFERRED), + " invalid relation {" + relation + "}"); DataAssociationCursor cursor = - ((DataAssociation) get(RELATED_CATEGORIES)).cursor(); + ((DataAssociation) get(RELATED_CATEGORIES)).cursor(); cursor.addEqualsFilter("link.relationType", relation); return cursor; } @@ -1799,7 +1833,7 @@ } StringBuilder sb = - new StringBuilder(path.length() + pathExtension.length() + 1); + new StringBuilder(path.length() + pathExtension.length() + 1); sb.append(path).append(".").append(pathExtension); return sb.toString(); } @@ -1813,7 +1847,7 @@ */ public long getNumberOfChildObjects() { DataAssociationCursor association = - ((DataAssociation) get(CHILD_OBJECTS)).cursor(); + ((DataAssociation) get(CHILD_OBJECTS)).cursor(); if (association == null) { return 0; } else { @@ -1838,7 +1872,7 @@ */ public Category getDefaultParentCategory() { DataAssociationCursor cursor = - ((DataAssociation) get(PARENTS)).cursor(); + ((DataAssociation) get(PARENTS)).cursor(); cursor.addEqualsFilter("link.isDefault", Boolean.TRUE); try { @@ -1850,7 +1884,7 @@ } throw new CategoryNotFoundException("The Category " + this + " does " - + "not have a default parent"); + + "not have a default parent"); } /** @@ -1901,7 +1935,7 @@ */ public CategoryCollection getDefaultAscendants() { DataCollection collection = - getSession().retrieve(BASE_DATA_OBJECT_TYPE); + getSession().retrieve(BASE_DATA_OBJECT_TYPE); String ids = (String) get(DEFAULT_ANCESTORS); if (ids == null) { @@ -1958,7 +1992,7 @@ */ public CategorizedCollection getDescendantObjects() { return getDescendantObjects(ACSObject.BASE_DATA_OBJECT_TYPE, - "categories.roTransParents"); + "categories.roTransParents"); } private static String appendID(String path) { @@ -1968,7 +2002,7 @@ } public CategorizedCollection getDescendantObjects(String objectType, - String path) { + String path) { s_log.info("retrieving objectType=" + objectType + "; path=" + path); final CategorizedCollection result = new CategorizedCollection(getSession(). retrieve(objectType)); @@ -2041,6 +2075,7 @@ public String getToken() { return m_token; } + } @Override @@ -2059,7 +2094,7 @@ DataCollection dc = SessionManager.getSession().retrieve( "com.arsdigita.categorization.UseContext"); dc.addFilter("categoryOwner.id = :ownerID").set("ownerID", - acsObj.getID()); + acsObj.getID()); return dc; } @@ -2106,9 +2141,9 @@ DataObject secondRoot = cats.getDataObject(); cats.close(); throw new IllegalStateException("there is more than one root for object:\n" - + object + "\nfirst root: " - + triple + "\nsecond root: " - + secondRoot); + + object + "\nfirst root: " + + triple + "\nsecond root: " + + secondRoot); } cats.close(); } @@ -2149,14 +2184,14 @@ * @param root the root category for the object */ public static void setRootForObject(ACSObject acsObj, Category rootCat, - String context) { + String context) { DataCollection rootCats = getRootCategoriesAssoc(acsObj); rootCats.addEqualsFilter(USE_CONTEXT, context); if (rootCats.next()) { DataObject triple = rootCats.getDataObject(); triple.set(ROOT_CATEGORY, - DomainServiceInterfaceExposer.getDataObject(rootCat)); + DomainServiceInterfaceExposer.getDataObject(rootCat)); rootCats.close(); return; } @@ -2173,9 +2208,9 @@ throw new UncheckedWrapperException(ex); } triple.set(CATEGORY_OWNER, - DomainServiceInterfaceExposer.getDataObject(acsObj)); + DomainServiceInterfaceExposer.getDataObject(acsObj)); triple.set(ROOT_CATEGORY, - DomainServiceInterfaceExposer.getDataObject(rootCat)); + DomainServiceInterfaceExposer.getDataObject(rootCat)); triple.set(USE_CONTEXT, context); } @@ -2292,7 +2327,7 @@ * Add a new language set to this category */ public boolean addLanguage(String locale, String name, String description, - String url) { + String url) { // If locale don't exist if (!locale.isEmpty() && m_categoryLocalizationCollection != null && !m_categoryLocalizationCollection. @@ -2344,4 +2379,5 @@ return false; } + } Modified: trunk/ccm-core/src/com/arsdigita/categorization/CategoryCollection.java =================================================================== --- trunk/ccm-core/src/com/arsdigita/categorization/CategoryCollection.java 2012-12-17 11:10:34 UTC (rev 2406) +++ trunk/ccm-core/src/com/arsdigita/categorization/CategoryCollection.java 2012-12-17 11:16:00 UTC (rev 2407) @@ -21,6 +21,7 @@ import com.arsdigita.kernel.ACSObject; import com.arsdigita.kernel.ACSObjectCollection; import com.arsdigita.persistence.DataCollection; +import java.math.BigDecimal; /** * Represents a collection of categories. @@ -84,6 +85,10 @@ return getCategory(); } + public BigDecimal getSortKey() { + return (BigDecimal) get("link.sortKey"); + } + /** * Sorts the category collection by the category sort key. * |