From 632b81a0cf6e7b0161594f3944ef259d10e87477 Mon Sep 17 00:00:00 2001 From: baka Date: Mon, 12 Dec 2016 15:26:31 +0000 Subject: [PATCH] Adds most of the missing files. Still turned off, because they still contain small errors git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4469 8810af33-2d31-482b-a856-94f89814c4df --- .../category/CategorizedObjectsList.java.off | 210 ++++++ .../cms/ui/category/CategoryItemPane.java.off | 653 ++++++++++++++++++ .../cms/ui/category/CategoryMoveForm.java.off | 181 +++++ .../CategoryTreeModelBuilder.java.off | 73 ++ .../CategoryUseContextModelBuilder.java.off | 78 +++ .../category/IndexItemSelectionForm.java.off | 222 ++++++ .../cms/ui/category/LinkForm.java.off | 128 ++++ 7 files changed, 1545 insertions(+) create mode 100755 ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategorizedObjectsList.java.off create mode 100755 ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryItemPane.java.off create mode 100644 ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryMoveForm.java.off create mode 100755 ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryTreeModelBuilder.java.off create mode 100755 ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryUseContextModelBuilder.java.off create mode 100755 ccm-cms/src/main/java/com/arsdigita/cms/ui/category/IndexItemSelectionForm.java.off create mode 100755 ccm-cms/src/main/java/com/arsdigita/cms/ui/category/LinkForm.java.off diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategorizedObjectsList.java.off b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategorizedObjectsList.java.off new file mode 100755 index 000000000..b0e74639f --- /dev/null +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategorizedObjectsList.java.off @@ -0,0 +1,210 @@ +/* + * 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.*; +import com.arsdigita.bebop.list.ListModel; +import com.arsdigita.bebop.list.ListModelBuilder; +import com.arsdigita.bebop.util.GlobalizationUtil; +import com.arsdigita.cms.CMS; +import com.arsdigita.cms.dispatcher.ItemResolver; +import com.arsdigita.globalization.GlobalizedMessage; +import com.arsdigita.util.LockableImpl; +import com.arsdigita.xml.Element; +import org.libreccm.categorization.Categorization; +import org.libreccm.categorization.Category; +import org.libreccm.cdi.utils.CdiUtil; +import org.libreccm.core.CcmObject; +import org.libreccm.security.PermissionChecker; +import org.librecms.CmsConstants; +import org.librecms.contentsection.ContentItem; +import org.librecms.contentsection.ContentItemRepository; +import org.librecms.contentsection.privileges.AdminPrivileges; + +import java.math.BigDecimal; +import java.util.Collection; +import java.util.Iterator; +import javax.servlet.ServletException; + +/** + * A List of all objects currently categorized under this category + * + * @author Randy Graebner (randyg@redhat.com) + * @version $Revision: #18 $ $DateTime: 2004/08/17 23:15:09 $ + * @version $Revision: #18 $Id: CategorizedObjectsList.java 2090 2010-04-17 + * 08:04:14Z pboy $ + */ +public class CategorizedObjectsList extends SortableCategoryList { + + public final static String CATEGORIZED_OBJECTS = "co"; + + public CategorizedObjectsList(final CategoryRequestLocal category) { + super(category); + + setModelBuilder(new CategorizedObjectsModelBuilder()); + Label label = new Label(GlobalizationUtil.globalize("cms.ui.category.item.none")); + label.setFontWeight(Label.ITALIC); + setEmptyView(label); + } + + /** + * This actually performs the sorting + */ + public void respond(PageState ps) throws ServletException { + final String event = ps.getControlEventName(); + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final ContentItemRepository contentItemRepository = cdiUtil.findBean(ContentItemRepository.class); + + /* + if (NEXT_EVENT.equals(event) || PREV_EVENT.equals(event)) { + final long selectedID = Long.parseLong(ps.getControlEventValue()); + final Category parent = getCategory(ps); + + final ContentItem selectedItem = contentItemRepository.findById(selectedID).get(); + final BigDecimal selectedDraftId = selectedItem.getDraftVersion().getID(); + + 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); + }*/ + } + + + protected long getSwapID(Category category, long selectedID, String event) { + long priorID = -1; + long swapID = -1; + boolean foundSelectedID = false; + + if (category != null && !category.getObjects().isEmpty()) { + Iterator items = category.getObjects().iterator(); + //items.addEqualsFilter(ContentItem.VERSION, ContentItem.LIVE); TODO + //items.sort(true); + while (items.hasNext()) { + long thisID = items.next().getCategorizationId(); + if (foundSelectedID && NEXT_EVENT.equals(event)) { + swapID = thisID; + break; + } + + if (thisID == selectedID) { + foundSelectedID = true; + if (PREV_EVENT.equals(event)) { + swapID = priorID; + break; + } + } + + priorID = thisID; + } + } + return swapID; + } + + @Override + protected void generateLabelXML(PageState state, Element parent, Label label, String key, Object element) { + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final PermissionChecker permissionChecker = cdiUtil.findBean(PermissionChecker.class); + + ContentBundle item = (ContentBundle) element; + + boolean canEdit = permissionChecker.isPermitted(AdminPrivileges.ADMINISTER_CATEGORIES); + + if (canEdit) { + + ContentSection section = item.getContentSection(); + ItemResolver resolver = section.getItemResolver(); + + Link link = new Link( + item.getDisplayName(), + resolver.generateItemURL( + state, + ((ContentBundle) item.getDraftVersion()).getPrimaryInstance(), + section, + ((ContentBundle) item.getDraftVersion()).getPrimaryInstance().getVersion())); + Component c = link; + c.generateXML(state, parent); + } + } + + 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(); + items.addEqualsFilter(ContentItem.VERSION, ContentItem.LIVE); + items.sort(true); + return new CategorizedCollectionListModel(items); + } else { + return List.EMPTY_MODEL; + } + } + } + + /** + * A {@link ListModel} that iterates over categorized objects via an + * iterator + */ + private static class CategorizedCollectionListModel implements ListModel { + + private Iterator m_objs; + private Categorization m_object; + + CategorizedCollectionListModel(Collection coll) { + m_objs = coll.iterator(); + m_object = null; + + } + + @Override + public boolean next() { + if (m_objs.hasNext()) { + m_object = m_objs.next(); + return true; + } else { + return false; + } + } + + @Override + public Object getElement() { + return m_object; + } + + @Override + public String getKey() { + return Long.toString(m_object.getCategorizationId()); + } + } +} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryItemPane.java.off b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryItemPane.java.off new file mode 100755 index 000000000..25e16fec6 --- /dev/null +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryItemPane.java.off @@ -0,0 +1,653 @@ +/* + * Copyright (C) 2003-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.ActionLink; +import com.arsdigita.bebop.BaseLink; +import com.arsdigita.bebop.Component; +import com.arsdigita.bebop.Form; +import com.arsdigita.bebop.Label; +import com.arsdigita.bebop.Link; +import com.arsdigita.bebop.Page; +import com.arsdigita.bebop.PageState; +import com.arsdigita.bebop.ParameterSingleSelectionModel; +import com.arsdigita.bebop.SimpleContainer; +import com.arsdigita.bebop.SingleSelectionModel; +import com.arsdigita.bebop.event.ActionEvent; +import com.arsdigita.bebop.event.ActionListener; +import com.arsdigita.bebop.event.ChangeEvent; +import com.arsdigita.bebop.event.ChangeListener; +import com.arsdigita.bebop.form.Submit; +import com.arsdigita.bebop.parameters.StringParameter; + +import com.arsdigita.bebop.util.GlobalizationUtil; +import com.arsdigita.cms.ui.BaseItemPane; +import com.arsdigita.cms.ui.CMSForm; +import com.arsdigita.cms.ui.VisibilityComponent; +import com.arsdigita.kernel.ui.ACSObjectSelectionModel; +import com.arsdigita.toolbox.ui.ActionGroup; +import com.arsdigita.toolbox.ui.PropertyList; +import com.arsdigita.toolbox.ui.Section; +import com.arsdigita.util.Assert; +import com.arsdigita.web.Web; +import com.arsdigita.xml.Element; + +import java.math.BigDecimal; +import java.net.URLEncoder; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Optional; + +import org.apache.log4j.Logger; +import org.libreccm.categorization.Category; +import org.libreccm.categorization.CategoryManager; +import org.libreccm.categorization.CategoryRepository; +import org.libreccm.cdi.utils.CdiUtil; +import org.libreccm.security.Permission; +import org.libreccm.security.PermissionChecker; +import org.libreccm.security.User; +import org.librecms.contentsection.ContentItem; +import org.librecms.contentsection.privileges.AdminPrivileges; + +/** + * Edits a single category. + * + * @author Justin Ross <jross@redhat.com> + * @author Sören Bernstein + * @version $Id: CategoryItemPane.java 1967 2009-08-29 21:05:51Z pboy $ + */ +class CategoryItemPane extends BaseItemPane { + + private static final Logger s_log = Logger.getLogger(CategoryItemPane.class); + private final SingleSelectionModel m_model; + private final CategoryRequestLocal m_category; + private final SimpleContainer m_detailPane; + + public CategoryItemPane(final SingleSelectionModel model, + final SingleSelectionModel contextModel, + final CategoryRequestLocal category, + final ActionLink addLink, + final ActionLink editLink, + final ActionLink deleteLink) { + m_model = model; + m_category = category; + + // Details + m_detailPane = new SimpleContainer(); + add(m_detailPane); + setDefault(m_detailPane); + + final ActionLink orderItemsLink = new ActionLink(new Label( + gz("cms.ui.category.categorized_objects"))) { + @Override + public boolean isVisible(PageState state) { + // update for live items only + if (!super.isVisible(state)) { + return false; + } + return !m_category.getCategory(state).getObjects().isEmpty(); + } + + }; + + final Form orderItemsForm = new OrderItemsForm(m_category); + final Form orderItemsForm2 = new OrderItemsForm(m_category); + add(orderItemsForm); + add(orderItemsForm2); + + // Change index item + final ActionLink indexLink = new ActionLink(new Label(gz( + "cms.ui.category.change_index_item"))); + final Form indexForm = new IndexItemSelectionForm(m_category); + add(indexForm); + + //Move link + final ActionLink moveLink = new MoveLink(new Label(gz("cms.ui.category.move"))); + final Form moveForm = new CategoryMoveForm(m_category, contextModel); + add(moveForm); + + ViewItemLink viewIndexLink = new ViewItemLink(new Label(gz( + "cms.ui.category.view_index_item")), ""); + EditItemLink editIndexLink = new EditItemLink(new Label(gz( + "cms.ui.category.edit_index_item")), ""); + + // Summary + m_detailPane.add(new SummarySection(editLink, + deleteLink, + indexLink, + moveLink, + viewIndexLink, + editIndexLink, + orderItemsLink)); + + // Quasimodo: BEGIN + // Localizations + /* + ActionLink addCategoryLocalizationLink = new ActionLink(new Label(gz( TODO + "cms.ui.category.localization_add"))) { + @Override + public boolean isVisible(PageState state) { + // Only show addLanguage button, if there are langauges to add + int countSupportedLanguages = (Kernel.getConfig()).getSupportedLanguagesTokenizer() + .countTokens(); + long countLanguages = + m_category.getCategory(state) + .getCategoryLocalizationCollection().size(); + + if (m_category.getCategory(state).canEdit() + && countLanguages < countSupportedLanguages) { + return true; + } else { + return false; + } + } + + };*/ + /* + CategoryLocalizationAddForm addCategoryLocalizationForm = + new CategoryLocalizationAddForm(m_category); + m_detailPane.add(new CategoryLocalizationSection(addCategoryLocalizationLink)); + add(addCategoryLocalizationForm); + connect(addCategoryLocalizationLink, addCategoryLocalizationForm); + connect(addCategoryLocalizationForm);*/ + // Quasimodo: END + + // Subcategories + m_detailPane.add(new SubcategorySection(addLink)); + + // Linked categories + final ActionLink linkAddLink = new ActionLink(new Label(gz("cms.ui.category.linked_add"))); + + //final Form linkForm = new LinkForm(m_category); + //add(linkForm); + + //linkAddLink.addActionListener(new NavigationListener(linkForm)); + //linkForm.addSubmissionListener(new CancelListener(linkForm)); + + m_detailPane.add(new LinkedCategorySection(linkAddLink)); + + // Templates + m_detailPane.add(new AdminVisible(new CategoryTemplateSection())); + + // Permissions + m_detailPane.add(new PermissionsSection()); + + connect(indexLink, indexForm); + connect(indexForm); + + connect(moveLink, moveForm); + connect(moveForm); + + connect(orderItemsLink, orderItemsForm); + connect(orderItemsForm); + + } + + private class EditVisible extends VisibilityComponent { + + EditVisible(final Component child) { + super(child, null); + } + + @Override + public boolean hasPermission(PageState ps) { + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final PermissionChecker permissionChecker = cdiUtil.findBean(PermissionChecker.class); + return permissionChecker.isPermitted(AdminPrivileges.ADMINISTER_CATEGORIES); + } + + } + + private class AdminVisible extends VisibilityComponent { + + AdminVisible(final Component child) { + super(child, null); + } + + @Override + public boolean hasPermission(PageState ps) { + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final PermissionChecker permissionChecker = cdiUtil.findBean(PermissionChecker.class); + return permissionChecker.isPermitted(AdminPrivileges.ADMINISTER_CATEGORIES); + } + + } + + private class SummarySection extends Section { + + SummarySection(final ActionLink editLink, + final ActionLink deleteLink, + final ActionLink indexLink, + final ActionLink moveLink, + final ActionLink orderItemsLink) { + setHeading(new Label(gz("cms.ui.category.details"))); + + final ActionGroup group = new ActionGroup(); + setBody(group); + + group.setSubject(new Properties()); + + group.addAction(new EditVisible(editLink), ActionGroup.EDIT); + group.addAction(new EditVisible(orderItemsLink)); + group.addAction(new EditVisible(moveLink)); + group.addAction(new EditVisible(indexLink)); + group.addAction(new AdminVisible(deleteLink), ActionGroup.DELETE); + } + + /* + * This alternative constructor sets two additional links, allowing + * the user to view and edit the content index item. + */ + SummarySection(final ActionLink editLink, + final ActionLink deleteLink, + final ActionLink indexLink, + final ActionLink moveLink, + final BaseLink viewIndexItem, + final BaseLink editIndexItem, + final ActionLink orderItemsLink) { + setHeading(new Label(gz("cms.ui.category.details"))); + + final ActionGroup group = new ActionGroup(); + setBody(group); + + group.setSubject(new Properties()); + + group.addAction(new EditVisible(editLink), ActionGroup.EDIT); + group.addAction(new EditVisible(orderItemsLink)); + group.addAction(new EditVisible(indexLink)); + group.addAction(new EditVisible(moveLink)); + group.addAction(new EditVisible(viewIndexItem)); + group.addAction(new EditVisible(editIndexItem)); + group.addAction(new AdminVisible(deleteLink), ActionGroup.DELETE); + } + + private class Properties extends PropertyList { + + @Override + protected final java.util.List properties(final PageState state) { + final java.util.List props = super.properties(state); + final Category category = m_category.getCategory(state); + + String itemTitle = "None"; + + if (category != null) { + itemTitle = category.getDisplayName(); + } + + props.add(new Property(gz("cms.ui.name"), + category.getName())); + props.add(new Property(gz("cms.ui.description"), + category.getDescription().getValue())); + props.add(new Property(gz("cms.ui.category.is_not_abstract"), + category.isAbstractCategory() + ? gz("cms.ui.no") + : gz("cms.ui.yes"))); + props.add(new Property(gz("cms.ui.cateogry.is_visible"), + category.isVisible() + ? gz("cms.ui.yes") + : gz("cms.ui.no"))); + props.add(new Property(gz("cms.ui.category.is_enabled"), + category.isEnabled() + ? gz("cms.ui.yes") + : gz("cms.ui.no"))); + props.add(new Property(gz("cms.ui.category.index_item"), + itemTitle)); + + return props; + } + + } + } + + /* + // Quasimodo: BEGIN + // CategoryLocalizationSection + private class CategoryLocalizationSection extends Section { + + private CategoryLocalizationTable m_catLocalizationTable; + private CategoryLocalizationEditForm m_editCategoryLocalizationForm; + private StringParameter m_catLocaleParam; + private ParameterSingleSelectionModel m_catLocale; + + CategoryLocalizationSection(ActionLink addLink) { + setHeading(new Label(gz("cms.ui.category.localizations"))); + m_catLocaleParam = new StringParameter("catLocale"); + m_catLocale = new ParameterSingleSelectionModel(m_catLocaleParam); + + final ActionGroup group = new ActionGroup(); + setBody(group); + m_catLocalizationTable = new CategoryLocalizationTable(m_category, m_model, m_catLocale); + group.setSubject(m_catLocalizationTable); + group.addAction(new AdminVisible(addLink), ActionGroup.ADD); + + m_editCategoryLocalizationForm = new CategoryLocalizationEditForm(m_category, + m_catLocale); + add(m_editCategoryLocalizationForm); + connect(m_editCategoryLocalizationForm); + connect(m_catLocalizationTable, 0, m_editCategoryLocalizationForm); + } + + @Override + public void register(Page page) { + super.register(page); + page.addComponentStateParam(m_editCategoryLocalizationForm, m_catLocaleParam); + } + + }*/ + + private class SubcategorySection extends Section { + + SubcategorySection(final ActionLink addLink) { + setHeading(new Label(gz("cms.ui.category.subcategories"))); + + final ActionGroup group = new ActionGroup(); + setBody(group); + + group.setSubject(new SubcategoryList(m_category, m_model)); + group.addAction(new AdminVisible(addLink), ActionGroup.ADD); + } + + } + + private class LinkedCategorySection extends Section { + + LinkedCategorySection(final ActionLink linkAddLink) { + setHeading(new Label(gz("cms.ui.category.linked"))); + + final ActionGroup group = new ActionGroup(); + setBody(group); + + group.setSubject(new CategoryLinks(m_category, m_model)); + group.addAction(new EditVisible(linkAddLink), ActionGroup.EDIT); + } + + @Override + public final boolean isVisible(final PageState state) { + return m_category.getCategory(state).getParent().isPresent(); + } + + } + + private class CategoryTemplateSection extends Section { + + CategoryTemplateSection() { + setHeading(new Label(gz("cms.ui.category.templates"))); + + final ActionGroup group = new ActionGroup(); + setBody(group); + + //group.setSubject(new CategoryTemplates(m_category)); TODO + // XXX secvis + //group.addAction(link); + } + + } + + private class PermissionsSection extends Section { + + @Override + public boolean isVisible(PageState ps) { + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final PermissionChecker permissionChecker = cdiUtil.findBean(PermissionChecker.class); + Category cat = m_category.getCategory(ps); + return cat.getParent().isPresent() && permissionChecker.isPermitted(AdminPrivileges.ADMINISTER_CATEGORIES); + } + + PermissionsSection() { + setHeading(new Label(gz("cms.ui.permissions"))); + /* + final ActionGroup group = new ActionGroup(); + setBody(group); + + PrivilegeDescriptor[] privs = new PrivilegeDescriptor[]{ + PrivilegeDescriptor.EDIT, + Category.MAP_DESCRIPTOR, + PrivilegeDescriptor.DELETE, + PrivilegeDescriptor.ADMIN + }; + + HashMap privMap = new HashMap(); + privMap.put("edit", "Edit"); + privMap.put("delete", "Delete"); + privMap.put(Category.MAP_DESCRIPTOR.getName(), "Categorize Items"); + privMap.put("admin", "Admin"); + + final CMSPermissionsPane permPane = new CMSPermissionsPane(privs, privMap, + new ACSObjectSelectionModel( + m_model)) { + @Override + public void showAdmin(PageState ps) { + Assert.exists(m_model.getSelectedKey(ps)); + + super.showAdmin(ps); + getAdminListingPanel().setVisible(ps, false); + } + + }; + + final ActionLink restoreDefault = new ActionLink(new Label(gz( + "cms.ui.restore_default_permissions"))) { + @Override + public boolean isVisible(PageState ps) { + Category cat = m_category.getCategory(ps); + return PermissionService.getContext(cat) == null; + } + + }; + + final ActionLink useCustom = new ActionLink(new Label(gz( + "cms.ui.use_custom_permissions"))) { + @Override + public boolean isVisible(PageState ps) { + Category cat = m_category.getCategory(ps); + return PermissionService.getContext(cat) != null; + } + + }; + + ActionListener al = new ActionListener() { + public void actionPerformed(ActionEvent event) { + PageState state = event.getPageState(); + // if this is the root then we cannot revert to anything + // since there is not a parent + Category cat = m_category.getCategory(state); + if (!cat.canAdmin()) { + throw new com.arsdigita.cms.dispatcher.AccessDeniedException(); + } + DataObject context = PermissionService.getContext(cat); + if (context != null) { + PermissionService.clonePermissions(cat); + } else { + ACSObject parent; + try { + parent = cat.getDefaultParentCategory(); + } catch (CategoryNotFoundException ce) { + throw new IllegalStateException( + "link shouldn't exist for root categories"); + } + PermissionService.setContext(cat, parent); + + // revoke all direct permissions so category will only + // have inherited permissions + ObjectPermissionCollection perms = + PermissionService.getDirectGrantedPermissions( + cat.getOID()); + while (perms.next()) { + PermissionService.revokePermission( + new PermissionDescriptor( + perms.getPrivilege(), cat.getOID(), + perms.getGranteeOID())); + } + } + permPane.reset(state); + } + + } + + restoreDefault.addActionListener(al); + useCustom.addActionListener(al); + + SimpleContainer links = new SimpleContainer(); + links.add(restoreDefault); + links.add(useCustom); + + group.setSubject(permPane); + group.addAction(links); + + m_model.addChangeListener(new ChangeListener() { + public void stateChanged(ChangeEvent e) { + PageState ps = e.getPageState(); + } + + }); + */ + } + + } + + private static class OrderItemsForm extends CMSForm { + + public OrderItemsForm(CategoryRequestLocal category) { + super("orderItems", new SimpleContainer()); + Label header = new Label(gz("cms.ui.category.categorized_objects")); + header.setFontWeight(Label.BOLD); + add(header); + add(new CategorizedObjectsList(category)); + + add(new Submit("Done")); + + } + + } + + /* + * This private class creates a link to the index item for a category. + */ + private class ViewItemLink extends Link { + + ViewItemLink(Component c, String s) { + super(c, s); + } + + // Build the preview link. This uses a standard redirect link to find + // the content. The prepareURL method is called by the printwriter + @Override + protected String prepareURL(final PageState state, String location) { + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final CategoryManager manager = cdiUtil.findBean(CategoryManager.class); + + ContentItem indexItem = (ContentItem) manager.getIndexObject(m_category.getCategory(state)).get(); + return "/redirect/?oid=" + URLEncoder.encode(Long.toString(indexItem.getObjectId())); + } + + // We only show this link when an index item exists for this category + @Override + public boolean isVisible(PageState state) { + if (!super.isVisible(state)) { + return false; + } + Category /*ACSObject*/ indexItem = m_category.getCategory(state);//.getDirectIndexObject(); + if (indexItem == null) { + return false; + } else { + return true; + } + } + + }; + + private class EditItemLink extends Link { + + EditItemLink(Component c, String s) { + super(c, s); + } + + /** + * Build the preview link. This is based on code in the + * ContentSoonExpiredPane class. The prepareURL method of the parent is + * overwritten. This method is called by the printwriter + */ + @Override + protected String prepareURL(final PageState state, String location) { + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final CategoryManager manager = cdiUtil.findBean(CategoryManager.class); + final PermissionChecker permissionChecker = cdiUtil.findBean(PermissionChecker.class); + + + boolean canEdit = false; + Optional indexItem = manager.getIndexObject(m_category.getCategory(state); + if (!indexItem.isPresent()) { + return ""; + } + if (!permissionChecker.isPermitted("", indexItem.get())) { + return ""; + } else { + BigDecimal draftID = indexItem.getDraftVersion().getID(); + return "item.jsp?item_id=" + draftID + "&set_tab=" + + ContentItemPage.AUTHORING_TAB; + } + } + + /** + * We only show this link when an index item exists for this category + * and the user is allowed to edit this item. + * + * @param state + * + * @return + */ + @Override + public boolean isVisible(PageState state) { + if (!super.isVisible(state)) { + return false; + } + ACSObject indexItem = m_category.getCategory(state).getDirectIndexObject(); + if (indexItem == null) { + return false; + } else { + return isItemEditable((ContentItem) indexItem, state); + } + } + }; + + private class MoveLink extends ActionLink { + + private final Label alternativeLabel; + + public MoveLink(final Label label) { + super(label); + alternativeLabel = new Label(GlobalizationUtil.globalize("cms.ui.category.cantmoved")); + } + + @Override + public void generateXML(final PageState state, final Element parent) { + if (!isVisible(state)) { + return; + } + + final Category category = m_category.getCategory(state); + if (!category.getParent().isPresent()) { + alternativeLabel.generateXML(state, parent); + } else { + super.generateXML(state, parent); + } + } + + } +} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryMoveForm.java.off b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryMoveForm.java.off new file mode 100644 index 000000000..52c984dae --- /dev/null +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryMoveForm.java.off @@ -0,0 +1,181 @@ +/* + * Copyright (C) 2013 Jens Pelzetter 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.ColumnPanel; +import com.arsdigita.bebop.Form; +import com.arsdigita.bebop.FormProcessException; +import com.arsdigita.bebop.Label; +import com.arsdigita.bebop.PageState; +import com.arsdigita.bebop.ParameterSingleSelectionModel; +import com.arsdigita.bebop.SaveCancelSection; +import com.arsdigita.bebop.SingleSelectionModel; +import com.arsdigita.bebop.Tree; +import com.arsdigita.bebop.event.ChangeEvent; +import com.arsdigita.bebop.event.ChangeListener; +import com.arsdigita.bebop.event.FormInitListener; +import com.arsdigita.bebop.event.FormProcessListener; +import com.arsdigita.bebop.event.FormSectionEvent; +import com.arsdigita.bebop.event.PrintEvent; +import com.arsdigita.bebop.event.PrintListener; +import com.arsdigita.bebop.form.Submit; +import com.arsdigita.bebop.parameters.ParameterModel; +import com.arsdigita.bebop.parameters.StringParameter; +import com.arsdigita.bebop.util.GlobalizationUtil; +import com.arsdigita.cms.ui.BaseTree; +import com.arsdigita.cms.ui.CMSForm; +import com.arsdigita.cms.ui.FormSecurityListener; +import com.arsdigita.util.Assert; +import org.libreccm.categorization.Category; +import org.libreccm.categorization.CategoryRepository; +import org.libreccm.cdi.utils.CdiUtil; + +import java.math.BigDecimal; + +/** + * + * @author Jens Pelzetter + */ +public class CategoryMoveForm extends CMSForm { + + public static final String CONTEXT_SELECTED = "sel_context"; + private static final String DEFAULT_USE_CONTEXT = + CategoryUseContextModelBuilder.DEFAULT_USE_CONTEXT; + private final CategoryRequestLocal selectedCategory; + private final SaveCancelSection saveCancelSection; + private final ChangeListener changeListener; + //private final SingleSelectionModel selectionModel; + private final Tree categoryTree; + + public CategoryMoveForm(final CategoryRequestLocal selectedCategory, + final SingleSelectionModel contextModel) { + + super("MoveCategory"); + setMethod(Form.POST); + this.selectedCategory = selectedCategory; + + //final Label header = new Label(GlobalizationUtil.globalize("cms.ui.category.move")); + final Label header = new Label(); + header.addPrintListener(new PrintListener() { + @Override + public void prepare(final PrintEvent event) { + final String[] args = new String[1]; + args[0] = selectedCategory.getCategory(event.getPageState()).getName(); + + final Label target = (Label) event.getTarget(); + target.setLabel(GlobalizationUtil.globalize("cms.ui.move.category", args)); + } + + }); + + header.setFontWeight(Label.BOLD); + add(header, ColumnPanel.FULL_WIDTH); + + changeListener = new TreeChangeListener(); + //selectionModel = new ParameterSingleSelectionModel(new StringParameter("selectedCategory")); + categoryTree = new BaseTree(new CategoryTreeModelBuilder(contextModel)); + categoryTree.addChangeListener(changeListener); + + add(categoryTree); + + saveCancelSection = new SaveCancelSection(); + add(saveCancelSection); + + addInitListener(new InitListener()); + addProcessListener(new ProcessListener()); + addSubmissionListener(new FormSecurityListener( + com.arsdigita.cms.SecurityManager.CATEGORY_ADMIN)); + + } + + protected Submit getCancelButton() { + return saveCancelSection.getCancelButton(); + } + + protected Category getCategory(final PageState state) { + final Category category = selectedCategory.getCategory(state); + Assert.exists(category); + return category; + } + + private class TreeChangeListener implements ChangeListener { + + public TreeChangeListener() { + //Nothing + } + + @Override + public void stateChanged(final ChangeEvent event) { + //Nothing for now + } + + } + + private class InitListener implements FormInitListener { + + public InitListener() { + //Nothing + } + + @Override + public void init(final FormSectionEvent event) throws FormProcessException { + //Nothing + } + + } + + private class ProcessListener implements FormProcessListener { + + public ProcessListener() { + //Nothing + } + + @Override + public void process(final FormSectionEvent event) throws FormProcessException { + final PageState state = event.getPageState(); + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final CategoryRepository categoryRepository = cdiUtil.findBean(CategoryRepository.class); + + if (saveCancelSection.getSaveButton().isSelected(state) + && !(categoryTree.getSelectedKey(state).equals(selectedCategory.getCategory(state).getUniqueId()))) { + + final Category categoryToMove = selectedCategory.getCategory(state); + final String targetKey = (String) categoryTree.getSelectedKey(state); + final Category target = categoryRepository.findById(Long.parseLong(targetKey)); + + final CategoryCollection parents = categoryToMove.getParents(); + while (parents.next()) { + final Category parent = parents.getCategory(); + parent.removeChild(categoryToMove); + parent.save(); + } + + target.addChild(categoryToMove); + categoryToMove.setDefaultParentCategory(target); + + target.save(); + categoryToMove.save(); + } + + categoryTree.clearSelection(state); + categoryTree.clearExpansionState(state); + } + + } +} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryTreeModelBuilder.java.off b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryTreeModelBuilder.java.off new file mode 100755 index 000000000..54c75daf5 --- /dev/null +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryTreeModelBuilder.java.off @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2003-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.PageState; +import com.arsdigita.bebop.SingleSelectionModel; +import com.arsdigita.bebop.Tree; +import com.arsdigita.bebop.tree.TreeModel; +import com.arsdigita.bebop.tree.TreeModelBuilder; +import com.arsdigita.categorization.Category; +import com.arsdigita.categorization.CategoryTreeModelLite; +import com.arsdigita.cms.CMS; +import com.arsdigita.cms.ContentSection; +import com.arsdigita.util.LockableImpl; + +/** + * Lists category tree. + * + * @author Tri Tran (tri@arsdigita.com) + * @version $Id: CategoryTreeModelBuilder.java 1942 2009-05-29 07:53:23Z terry $ + */ +class CategoryTreeModelBuilder extends LockableImpl + implements TreeModelBuilder { + + private SingleSelectionModel m_contextModel = null; + + public CategoryTreeModelBuilder() { + this(null); + } + + public CategoryTreeModelBuilder(SingleSelectionModel contextModel) { + super(); + m_contextModel = contextModel; + } + + public final TreeModel makeModel(final Tree tree, final PageState state) { + final ContentSection section = CMS.getContext().getContentSection(); + final Category root = Category.getRootForObject(section, + getUseContext(state)); + String order = ContentSection.getConfig().getCategoryTreeOrder(); + final CategoryTreeModelLite model = new CategoryTreeModelLite(root, order); + + return model; + } + + private String getUseContext(PageState state) { + String context = null; + if (m_contextModel != null) { + context = (String) m_contextModel.getSelectedKey(state); + if ((CategoryUseContextModelBuilder.DEFAULT_USE_CONTEXT).equals(context)) { + context = null; + } + } + return context; + } + +} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryUseContextModelBuilder.java.off b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryUseContextModelBuilder.java.off new file mode 100755 index 000000000..6e0f9dff8 --- /dev/null +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryUseContextModelBuilder.java.off @@ -0,0 +1,78 @@ +/* + * Copyright (C) 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.List; +import com.arsdigita.bebop.PageState; +import com.arsdigita.bebop.list.AbstractListModelBuilder; +import com.arsdigita.bebop.list.ListModel; +import com.arsdigita.cms.CMS; +import org.apache.log4j.Logger; +import org.libreccm.categorization.Category; +import org.libreccm.categorization.CategoryManager; +import org.libreccm.cdi.utils.CdiUtil; +import org.librecms.contentsection.ContentSection; + +/** + * Builds a list of category use contexts for the current + * content section. + * + * @author Scott Seago + * @version $Id: CategoryUseContextModelBuilder.java 2090 2010-04-17 08:04:14Z pboy $ + */ +class CategoryUseContextModelBuilder extends AbstractListModelBuilder { + + public static String DEFAULT_USE_CONTEXT = ""; + + private static final Logger s_log = Logger.getLogger + (CategoryUseContextModelBuilder.class); + + public final ListModel makeModel(final List list, final PageState state) { + return new Model(); + } + + private class Model implements ListModel { + private final RootCategoryCollection m_roots; + + public Model() { + final ContentSection section = + CMS.getContext().getContentSection(); + + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final CategoryManager manager = cdiUtil.findBean(CategoryManager.class); + + m_roots = manager.get + m_roots.addOrder(Category.USE_CONTEXT); + } + + public boolean next() { + return m_roots.next(); + } + + public Object getElement() { + String useContext = m_roots.getUseContext(); + return useContext == null ? DEFAULT_USE_CONTEXT : useContext; + } + + public String getKey() { + String useContext = m_roots.getUseContext(); + return useContext == null ? DEFAULT_USE_CONTEXT : useContext; + } + } +} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/IndexItemSelectionForm.java.off b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/IndexItemSelectionForm.java.off new file mode 100755 index 000000000..6d7d838ef --- /dev/null +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/IndexItemSelectionForm.java.off @@ -0,0 +1,222 @@ +/* + * 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.ColumnPanel; +import com.arsdigita.bebop.Component; +import com.arsdigita.bebop.Form; +import com.arsdigita.bebop.FormData; +import com.arsdigita.bebop.FormProcessException; +import com.arsdigita.bebop.Label; +import com.arsdigita.bebop.Link; +import com.arsdigita.bebop.PageState; +import com.arsdigita.bebop.SaveCancelSection; +import com.arsdigita.bebop.event.FormProcessListener; +import com.arsdigita.bebop.event.FormSectionEvent; +import com.arsdigita.bebop.event.PrintEvent; +import com.arsdigita.bebop.event.PrintListener; +import com.arsdigita.bebop.form.FormErrorDisplay; +import com.arsdigita.bebop.form.Option; +import com.arsdigita.bebop.form.RadioGroup; +import com.arsdigita.bebop.form.Submit; +import com.arsdigita.bebop.parameters.ParameterData; +import com.arsdigita.bebop.parameters.StringParameter; +import com.arsdigita.categorization.Category; +import com.arsdigita.categorization.CategorizedCollection; +import com.arsdigita.cms.ContentBundle; +import com.arsdigita.cms.ContentItem; +import com.arsdigita.cms.ContentSection; +import com.arsdigita.cms.SecurityManager; +import com.arsdigita.cms.dispatcher.ItemResolver; +import com.arsdigita.cms.ui.CMSForm; +import com.arsdigita.cms.ui.FormSecurityListener; +import com.arsdigita.cms.util.GlobalizationUtil; +import com.arsdigita.kernel.ACSObject; +import com.arsdigita.util.Assert; +import com.arsdigita.util.UncheckedWrapperException; + +import java.math.BigDecimal; + +/** + * Allows the user to select an index item to display when the front end user is + * browsing by Category + * + * @author Randy Graebner (randyg@alum.mit.edu) + * @version $Revision: #18 $ $DateTime: 2004/08/17 23:15:09 $ + */ +public class IndexItemSelectionForm extends CMSForm { + + private static org.apache.log4j.Logger s_log + = org.apache.log4j.Logger.getLogger( + IndexItemSelectionForm.class); + private final CategoryRequestLocal m_category; + private RadioGroup m_options; + private static final String NULL_OPTION_VALUE = ""; + private static final String NONE_OPTION_VALUE = "None"; + private FormErrorDisplay m_errors; + private SaveCancelSection m_saveCancelSection; + + public IndexItemSelectionForm(CategoryRequestLocal m) { + super("EditCategory"); + setMethod(Form.POST); + + m_category = m; + + // Form header + Label header = new Label(GlobalizationUtil.globalize("cms.ui.category.select_index_item")); + header.setFontWeight(Label.BOLD); + add(header, ColumnPanel.FULL_WIDTH); + + // Form errors + m_errors = new FormErrorDisplay(this); + add(m_errors, ColumnPanel.FULL_WIDTH); + + // Option Group + m_options = new RadioGroup(new StringParameter("items")); + try { + m_options.addPrintListener(new PrintListener() { + public void prepare(PrintEvent event) { + RadioGroup group = (RadioGroup) event.getTarget(); + PageState state = event.getPageState(); + Category category = getCategory(event.getPageState()); + CategorizedCollection children = category.getObjects( + ContentItem.BASE_DATA_OBJECT_TYPE); + + group.clearOptions(); + + // option for NO index Object + group.addOption(new Option(NONE_OPTION_VALUE, + new Label(NONE_OPTION_VALUE))); + + // option for inheriting from the parent category + if (category.getParentCategoryCount() > 0) { + group.addOption(new Option(NULL_OPTION_VALUE, + new Label("Inherit Index from Parent Category"))); + } + + while (children.next()) { + ACSObject item + = (ACSObject) children.getDomainObject(); + + if ((item instanceof ContentItem) && ((ContentItem) item).getVersion(). + equals(ContentItem.DRAFT)) { + + //create a link to the item: + ContentBundle bundleItem = (ContentBundle) item; + ContentSection section = bundleItem.getContentSection(); + ItemResolver resolver = section.getItemResolver(); + + Link link = new Link( + bundleItem.getDisplayName(), + resolver.generateItemURL(state, + ((ContentBundle) bundleItem.getDraftVersion()).getPrimaryInstance(), + section, + ((ContentBundle) bundleItem.getDraftVersion()).getPrimaryInstance().getVersion())); + Component linkComponent = link; + //add the option with the link + group.addOption(new Option(item.getID().toString(), + linkComponent)); + } + } + // get currently selected item + ACSObject indexItem = category.getDirectIndexObject(); + if (indexItem != null && indexItem instanceof ContentItem) { + group.setValue(state, ((ContentItem) indexItem) + .getWorkingVersion() + .getID().toString()); + } else { + String value = NONE_OPTION_VALUE; + if (!category.ignoreParentIndexItem() + && category.getParentCategoryCount() > 0) { + value = NULL_OPTION_VALUE; + } + group.setValue(state, value); + } + } + + }); + } catch (java.util.TooManyListenersException e) { + s_log.error("Error adding init listener to Radio Group", e); + throw new UncheckedWrapperException(e); + } + m_options.setLayout(RadioGroup.VERTICAL); + add(m_options); + + // Save and cancel buttons + m_saveCancelSection = new SaveCancelSection(); + add(m_saveCancelSection, ColumnPanel.FULL_WIDTH | ColumnPanel.LEFT); + + addSubmissionListener(new FormSecurityListener(SecurityManager.CATEGORY_ADMIN)); + + // Process listener + addProcessListener(new FormProcessListener() { + public void process(FormSectionEvent event) + throws FormProcessException { + PageState state = event.getPageState(); + FormData data = event.getFormData(); + ParameterData param = data.getParameter(m_options.getParameterModel().getName()); + String selectedValue = (String) param.getValue(); + + Category category + = getCategory(event.getPageState()); + + ContentItem item = null; + if (selectedValue != null) { + if (NULL_OPTION_VALUE.equals(selectedValue)) { + category.setIgnoreParentIndexItem(false); + selectedValue = null; + } else if (NONE_OPTION_VALUE.equals(selectedValue)) { + category.setIgnoreParentIndexItem(true); + selectedValue = null; + + } else { + item = new ContentItem(new BigDecimal(selectedValue)); + item = item.getWorkingVersion(); + } + } + category.setIndexObject(item); + category.save(); + } + + }); + } + + /** + * Get the cancel button. + * + * @return The cancel button + */ + protected Submit getCancelButton() { + return m_saveCancelSection.getCancelButton(); + } + + /** + * Fetch the selected category. + * + * @param state The page state + * @return The selected category + * @pre ( state != null ) + */ + protected Category getCategory(PageState state) { + Category category = m_category.getCategory(state); + Assert.exists(category); + return category; + } + +} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/LinkForm.java.off b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/LinkForm.java.off new file mode 100755 index 000000000..54b6c0133 --- /dev/null +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/LinkForm.java.off @@ -0,0 +1,128 @@ +/* + * Copyright (C) 2001-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.ColumnPanel; +import com.arsdigita.bebop.PageState; +import com.arsdigita.bebop.form.Submit; +import com.arsdigita.categorization.Category; +import com.arsdigita.categorization.CategoryCollection; +import com.arsdigita.cms.SecurityManager; +import com.arsdigita.cms.ui.CategoryForm; +import com.arsdigita.cms.ui.FormSecurityListener; +import com.arsdigita.kernel.ACSObject; +import com.arsdigita.toolbox.ui.Cancellable; + +import java.math.BigDecimal; + +import org.apache.log4j.Logger; + +/** + * A form which edits secondary parents + * + * @author Michael Pih + * @author Stanislav Freidin + * @version $Revision: #18 $ $DateTime: 2004/08/17 23:15:09 $ + */ +public class LinkForm extends CategoryForm implements Cancellable { + private final static Logger s_log = Logger.getLogger(LinkForm.class); + + private final CategoryRequestLocal m_category; + private final Submit m_cancelButton; + + public LinkForm(final CategoryRequestLocal category) { + super("LinkForm"); + + m_category = category; + + m_cancelButton = new Submit("Finish"); + add(m_cancelButton, ColumnPanel.FULL_WIDTH | ColumnPanel.LEFT); + + setAssignedCaption("Linked Categories"); + + addSubmissionListener + (new FormSecurityListener(SecurityManager.CATEGORY_ADMIN)); + } + + public final boolean isCancelled(final PageState state) { + return m_cancelButton.isSelected(state); + } + + /** + * Load all categories which are assigned to the current item. + */ + protected void initAssignedCategories(PageState state, CategoryMap m) { + final Category category = m_category.getCategory(state); + + final BigDecimal parentID = category.getDefaultParentCategory().getID(); + CategoryCollection links = category.getParents(); + + while ( links.next() ) { + Category cat = links.getCategory(); + + if ( !cat.getID().equals(parentID) ) { + m.add(cat); + } + } + links.close(); + } + + /** + * Assign a secondary parent. + */ + public void assignCategory(PageState state, Category category) { + final Category child = m_category.getCategory(state); + if (category.canEdit()) { + category.addChild(child); + category.save(); + } + } + + /** + * Unassign a secondary parent. + */ + public void unassignCategory(PageState state, Category category) { + final Category child = m_category.getCategory(state); + + if (category.canEdit()) { + category.removeChild(child); + category.save(); + } + } + + /** + * The category cannot be its own parent. Its children cannot + * be parents either. + */ + public Category getExcludedCategory(PageState state) { + return m_category.getCategory(state); + } + + /** + * This method returns the URL for the given item to make sure that + * there are not two objects in the same category with the same URL. + */ + protected final String getItemURL(final PageState state) { + return m_category.getCategory(state).getURL(); + } + + protected final ACSObject getObject(final PageState state) { + return (Category) m_category.getCategory(state); + } +}