diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryAdminController.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryAdminController.java index 9057abb1e..71bac9d50 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryAdminController.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryAdminController.java @@ -18,17 +18,29 @@ */ package com.arsdigita.cms.ui.category; +import org.libreccm.categorization.Categorization; +import org.libreccm.categorization.Category; +import org.libreccm.categorization.CategoryManager; +import org.libreccm.categorization.CategoryRepository; + import java.util.ArrayList; import java.util.List; import java.util.Objects; + import javax.enterprise.context.RequestScoped; import javax.inject.Inject; -import javax.persistence.EntityManager; import javax.transaction.Transactional; + import org.libreccm.categorization.DomainOwnership; +import org.libreccm.l10n.GlobalizationHelper; +import org.librecms.contentsection.ContentItem; +import org.librecms.contentsection.ContentItemManager; +import org.librecms.contentsection.ContentItemVersion; import org.librecms.contentsection.ContentSection; import org.librecms.contentsection.ContentSectionRepository; +import java.util.stream.Collectors; + /** * * @author Jens Pelzetter @@ -37,13 +49,22 @@ import org.librecms.contentsection.ContentSectionRepository; class CategoryAdminController { @Inject - private ContentSectionRepository sectionRepo; + private CategoryManager categoryManager; @Inject - private EntityManager entityManager; + private CategoryRepository categoryRepo; + + @Inject + private ContentItemManager itemManager; + + @Inject + private GlobalizationHelper globalizationHelper; + + @Inject + private ContentSectionRepository sectionRepo; @Transactional(Transactional.TxType.REQUIRED) - public List retrieveDomains(final ContentSection section) { + protected List retrieveDomains(final ContentSection section) { Objects.requireNonNull(section); @@ -57,4 +78,56 @@ class CategoryAdminController { return new ArrayList<>(contentSection.getDomains()); } + @Transactional(Transactional.TxType.REQUIRED) + protected List generateSubCategoryList( + final Category forCategory) { + + Objects.requireNonNull(forCategory); + + final Category category = categoryRepo + .findById(forCategory.getObjectId()) + .orElseThrow(() -> new IllegalArgumentException(String + .format("No Category with ID %d in the datbase.", + forCategory.getObjectId()))); + + return category + .getSubCategories() + .stream() + .map(this::createCategoryListItem) + .collect(Collectors.toList()); + } + + private CategoryListItem createCategoryListItem(final Category category) { + + final CategoryListItem item = new CategoryListItem(); + item.setCategoryId(category.getObjectId()); + + final String label = globalizationHelper + .getValueFromLocalizedString(category.getTitle(), category::getName); + item.setLabel(label); + + return item; + } + + @Transactional + protected List retrieveAssignedContentItems( + final Category fromCategory) { + + final Category category = categoryRepo + .findById(fromCategory.getObjectId()) + .orElseThrow(() -> new IllegalArgumentException(String + .format("No Category with ID %d in the datbase.", + fromCategory.getObjectId()))); + + return category + .getObjects() + .stream() + .map(Categorization::getCategorizedObject) + .filter(obj -> obj instanceof ContentItem) + .map(obj -> (ContentItem) obj) + .filter(item -> itemManager.isLive(item)) + .filter(item -> item.getVersion() == ContentItemVersion.LIVE) + .collect(Collectors.toList()); + } + } diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryAdminPane.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryAdminPane.java index decc0f0d6..4d992daa2 100755 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryAdminPane.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryAdminPane.java @@ -272,15 +272,23 @@ public final class CategoryAdminPane extends BaseAdminPane { @Override protected final Object initialValue(final PageState state) { - final String id = selectedCategory.getSelectedKey(state); + + final String selectedCatetoryIdStr = selectedCategory + .getSelectedKey(state); final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); final CategoryRepository repository = cdiUtil.findBean( CategoryRepository.class); - if (id == null) { - return null; + final Category category; + if (selectedCatetoryIdStr == null) { + category = null; } else { - return repository.findById(Long.parseLong(id)); + category = repository + .findById(Long.parseLong(selectedCatetoryIdStr)) + .orElseThrow(() -> new UnexpectedErrorException(String + .format("No Category with ID %s in the database.", + selectedCatetoryIdStr))); } + return category; } } diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryItemPane.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryItemPane.java index 910747a09..c946144fe 100755 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryItemPane.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryItemPane.java @@ -32,12 +32,12 @@ import com.arsdigita.bebop.SingleSelectionModel; 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.ContentItemPage; import com.arsdigita.cms.ui.VisibilityComponent; import com.arsdigita.cms.ui.templates.CategoryTemplates; +import com.arsdigita.globalization.GlobalizedMessage; import com.arsdigita.kernel.KernelConfig; import com.arsdigita.toolbox.ui.ActionGroup; import com.arsdigita.toolbox.ui.Property; @@ -45,7 +45,6 @@ import com.arsdigita.toolbox.ui.PropertyList; import com.arsdigita.toolbox.ui.Section; import com.arsdigita.xml.Element; -import java.net.URLEncoder; import java.util.Optional; import org.apache.logging.log4j.LogManager; @@ -56,12 +55,17 @@ import org.libreccm.categorization.CategoryManager; import org.libreccm.cdi.utils.CdiUtil; import org.libreccm.configuration.ConfigurationManager; import org.libreccm.core.CcmObject; +import org.libreccm.l10n.GlobalizationHelper; import org.libreccm.security.PermissionChecker; +import org.librecms.CmsConstants; import org.librecms.contentsection.ContentItem; import org.librecms.contentsection.ContentItemManager; import org.librecms.contentsection.privileges.AdminPrivileges; import org.librecms.contentsection.privileges.ItemPrivileges; +import java.util.List; +import java.util.Objects; + /** * Edits a single category. * @@ -100,7 +104,12 @@ class CategoryItemPane extends BaseItemPane { if (!super.isVisible(state)) { return false; } - return !m_category.getCategory(state).getObjects().isEmpty(); + + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final CategoryManager categoryManager = cdiUtil + .findBean(CategoryManager.class); + final Category category = m_category.getCategory(state); + return categoryManager.hasObjects(category); } }; @@ -276,6 +285,7 @@ class CategoryItemPane extends BaseItemPane { final BaseLink viewIndexItem, final BaseLink editIndexItem, final ActionLink orderItemsLink) { + setHeading(new Label(gz("cms.ui.category.details"))); final ActionGroup group = new ActionGroup(); @@ -295,36 +305,67 @@ class CategoryItemPane extends BaseItemPane { 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); + protected final java.util.List properties( + final PageState state) { - String itemTitle = "None"; + final java.util.List properties = super.properties( + state); + final Category category = m_category + .getCategory(state); - if (category != null) { - itemTitle = category.getDisplayName(); + final String itemTitle; + + if (category == null) { + itemTitle = "None"; + } else { + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final CategoryManager categoryManager = cdiUtil + .findBean(CategoryManager.class); + final List indexObjects = categoryManager + .getIndexObject(category); + + final Optional indexItem = indexObjects + .stream() + .filter(obj -> obj instanceof ContentItem) + .map(obj -> (ContentItem) obj) + .findFirst(); + + if (indexItem.isPresent()) { + final GlobalizationHelper globalizationHelper = cdiUtil + .findBean(GlobalizationHelper.class); + + final ContentItem item = indexItem.get(); + itemTitle = globalizationHelper + .getValueFromLocalizedString(item.getTitle(), + item::getDisplayName); + + } else { + final CcmObject indexObj = indexObjects.get(0); + itemTitle = Objects.toString(indexObj); + } } - 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)); + properties.add(new Property(gz("cms.ui.name"), + category.getName())); + properties.add(new Property(gz("cms.ui.description"), + category.getDescription().getValue())); + properties.add(new Property( + gz("cms.ui.category.is_not_abstract"), + category.isAbstractCategory() + ? gz("cms.ui.no") + : gz("cms.ui.yes"))); + properties.add(new Property(gz("cms.ui.cateogry.is_visible"), + category.isVisible() + ? gz("cms.ui.yes") + : gz("cms.ui.no"))); + properties.add(new Property(gz("cms.ui.category.is_enabled"), + category.isEnabled() + ? gz("cms.ui.yes") + : gz("cms.ui.no"))); + properties.add(new Property(gz("cms.ui.category.index_item"), + itemTitle)); - return props; + return properties; } } @@ -398,7 +439,13 @@ class CategoryItemPane extends BaseItemPane { @Override public final boolean isVisible(final PageState state) { - return m_category.getCategory(state).getParentCategory().isVisible(); + + final Category category = m_category.getCategory(state); + if (category.getParentCategory() == null) { + return false; + } else { + return category.getParentCategory().isVisible(); + } } } @@ -567,17 +614,20 @@ class CategoryItemPane extends BaseItemPane { // 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); + final Category category = m_category.getCategory(state); - ContentItem indexItem = (ContentItem) manager - .getIndexObject(m_category.getCategory(state)) + final ContentItem indexItem = (ContentItem) manager + .getIndexObject(category) .stream() .findFirst() .get(); - return "/redirect/?oid=" + URLEncoder.encode(Long.toString(indexItem - .getObjectId())); + + return String.format("/redirect/?oid=%s", + Long.toString(indexItem.getObjectId())); } // We only show this link when an index item exists for this category @@ -586,13 +636,12 @@ class CategoryItemPane extends BaseItemPane { if (!super.isVisible(state)) { return false; } - Category /*ACSObject*/ indexItem = m_category - .getCategory(state);//.getDirectIndexObject(); - if (indexItem == null) { - return false; - } else { - return true; - } + + final Category category = m_category.getCategory(state); + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final CategoryManager categoryManager = cdiUtil + .findBean(CategoryManager.class); + return categoryManager.hasIndexObject(category); } }; @@ -677,8 +726,9 @@ class CategoryItemPane extends BaseItemPane { public MoveLink(final Label label) { super(label); - alternativeLabel = new Label(GlobalizationUtil.globalize( - "cms.ui.category.cantmoved")); + alternativeLabel = new Label(new GlobalizedMessage( + "cms.ui.category.cantmoved", + CmsConstants.CMS_BUNDLE)); } @Override diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryLinks.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryLinks.java index 3c668d1d9..9f23d4811 100755 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryLinks.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryLinks.java @@ -30,10 +30,12 @@ import com.arsdigita.bebop.list.ListModelBuilder; import com.arsdigita.bebop.parameters.BigDecimalParameter; import com.arsdigita.bebop.util.GlobalizationUtil; import com.arsdigita.util.LockableImpl; -import org.libreccm.categorization.Category; -import java.util.Collection; -import java.util.HashSet; +import org.libreccm.categorization.Category; +import org.libreccm.cdi.utils.CdiUtil; +import org.libreccm.l10n.GlobalizationHelper; + +import java.util.ArrayList; /** * A List of all secondary parents of the current category. @@ -81,13 +83,27 @@ public class CategoryLinks extends List { // Since this part is for non default parents, but there is only one... this is not needed anymore, i guess private class LinkedCategoryModelBuilder extends LockableImpl implements ListModelBuilder { + + @Override public ListModel makeModel(List list, PageState state) { final Category category = m_parent.getCategory(state); if (category != null && category.getParentCategory() != null) { - java.util.List categories = new java.util.ArrayList<>(); - categories.add(category.getParentCategory()); + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final GlobalizationHelper globalizationHelper =cdiUtil + .findBean(GlobalizationHelper.class); + final Category parent = category.getParentCategory(); + + java.util.List categories = new ArrayList<>(); + final CategoryListItem parentItem = new CategoryListItem(); + parentItem.setCategoryId(parent.getObjectId()); + final String label = globalizationHelper + .getValueFromLocalizedString(parent.getTitle(), + parent::getName); + parentItem.setLabel(label); + + categories.add(parentItem); return new CategoryListModel (categories, diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryListItem.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryListItem.java new file mode 100644 index 000000000..23594b1b9 --- /dev/null +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryListItem.java @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2017 LibreCCM Foundation. + * + * 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., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package com.arsdigita.cms.ui.category; + +import java.util.Objects; + +/** + * + * @author Jens Pelzetter + */ +class CategoryListItem implements Comparable { + + private long categoryId; + private String label; + + public long getCategoryId() { + return categoryId; + } + + public void setCategoryId(final long categoryId) { + this.categoryId = categoryId; + } + + public String getLabel() { + return label; + } + + public void setLabel(final String label) { + this.label = label; + } + + @Override + public int compareTo(final CategoryListItem other) { + + final int result = label.compareTo(other.getLabel()); + if (result == 0) { + return Long.compare(categoryId, other.getCategoryId()); + } else { + return result; + } + } + + @Override + public int hashCode() { + int hash = 7; + hash = 67 * hash + (int) (categoryId ^ (categoryId >>> 32)); + hash = 67 * hash + Objects.hashCode(label); + return hash; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!(obj instanceof CategoryListItem)) { + return false; + } + final CategoryListItem other = (CategoryListItem) obj; + if (!other.canEqual(this)) { + return false; + } + + if (categoryId != other.getCategoryId()) { + return false; + } + return Objects.equals(label, other.getLabel()); + } + + public boolean canEqual(final Object obj) { + return obj instanceof CategoryListItem; + } + + @Override + public final String toString() { + return toString(""); + } + + public String toString(final String data) { + return String.format("%s{ " + + "categoryId = %d," + + "label = \"%s\"%s" + + " }", + super.toString(), + categoryId, + label, + data); + } + +} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryListModel.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryListModel.java index 1212dd542..8b0217b58 100755 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryListModel.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryListModel.java @@ -19,8 +19,8 @@ package com.arsdigita.cms.ui.category; import java.util.List; + import com.arsdigita.bebop.list.ListModel; -import org.libreccm.categorization.Category; import java.util.Iterator; @@ -28,39 +28,44 @@ import java.util.Iterator; * A {@link ListModel} that iterates over categories via a cursor. * * @author Yannick Bülter + * @author Jens Pelzetter */ -public final class CategoryListModel implements ListModel { - - private Category m_cat; - private long m_excludedID; - private Iterator iterator; +class CategoryListModel implements ListModel { + private final Iterator iterator; + private CategoryListItem currentCategory; + private Long excludedCategoryId; /** * Constructs a new CategoryListModel + * + * @param categories */ - public CategoryListModel(List coll) { - this(coll, -1); //Hopefully a decent replacement for null in BigDecimal. Negative ids would be weird... + public CategoryListModel(final List categories) { + this(categories, null); } /** * Constructs a new CategoryListModel */ - public CategoryListModel(List coll, - long excludedID) { + public CategoryListModel(final List categories, + final Long excludedCategoryId) { - m_excludedID = excludedID; - m_cat = null; - iterator = coll.iterator(); + iterator = categories.iterator(); + this.excludedCategoryId = excludedCategoryId; } + @Override public boolean next() { if (iterator.hasNext()) { - final Category category = iterator.next(); - if (Long.parseLong(category.getUniqueId()) == m_excludedID) { + + final CategoryListItem next = iterator.next(); + if (excludedCategoryId != null + && next.getCategoryId() == excludedCategoryId) { + return next(); } else { - m_cat = category; + currentCategory = next; return true; } } else { @@ -68,26 +73,19 @@ public final class CategoryListModel implements ListModel { } } - private Category getCategory() { - if ( m_cat == null ) { - throw new IllegalStateException("call next() first"); - } - return m_cat; - } - /** - * Reads the name of the category. + * Reads the label of the {@link CategoryListItem}. + * * - * Quasimodo: - * Modified to ensure that the value is read from Category (and not the - * localized version). This is necessary because we are in the admin GUI, - * a localized version would be confusing. */ + @Override public Object getElement() { - return getCategory().getName(); + return currentCategory.getLabel(); } + @Override public String getKey() { - return getCategory().getUniqueId(); + return Long.toString(currentCategory.getCategoryId()); } + } diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryLocalizationTable.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryLocalizationTable.java index 422e33620..e94010284 100755 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryLocalizationTable.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/CategoryLocalizationTable.java @@ -33,10 +33,14 @@ import com.arsdigita.bebop.table.TableModel; import com.arsdigita.bebop.table.TableModelBuilder; import com.arsdigita.bebop.util.GlobalizationUtil; import com.arsdigita.util.LockableImpl; + import org.libreccm.categorization.Category; import org.libreccm.cdi.utils.CdiUtil; +import org.libreccm.l10n.GlobalizationHelper; +import org.libreccm.l10n.GlobalizedMessagesUtil; import org.libreccm.l10n.LocalizedString; import org.libreccm.security.PermissionChecker; +import org.librecms.CmsConstants; import org.librecms.contentsection.privileges.AdminPrivileges; import java.math.BigDecimal; @@ -54,18 +58,22 @@ import java.util.Locale; * @author Sören Bernstein * @author Yannick Bülter */ -public class CategoryLocalizationTable extends Table implements TableActionListener { +public class CategoryLocalizationTable extends Table implements + TableActionListener { + + private static final String TABLE_COL_LANG = "table_col_lang"; + private static final String TABLE_COL_DEL = "table_col_del"; private final CategoryRequestLocal m_category; private final SingleSelectionModel m_model; - private final String TABLE_COL_LANG = "table_col_lang"; - private final String TABLE_COL_DEL = "table_col_del"; private final SingleSelectionModel m_catLocale; /** * Creates a new instance of CategoryLocalizationTable */ - public CategoryLocalizationTable(final CategoryRequestLocal category, final SingleSelectionModel model, SingleSelectionModel catLocale) { + public CategoryLocalizationTable(final CategoryRequestLocal category, + final SingleSelectionModel model, + final SingleSelectionModel catLocale) { super(); @@ -73,30 +81,52 @@ public class CategoryLocalizationTable extends Table implements TableActionListe m_model = model; m_catLocale = catLocale; + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final GlobalizationHelper globalizationHelper = cdiUtil + .findBean(GlobalizationHelper.class); + final GlobalizedMessagesUtil messagesUtil = globalizationHelper + .getGlobalizedMessagesUtil(CmsConstants.CMS_BUNDLE); + // if table is empty: - setEmptyView(new Label(GlobalizationUtil.globalize( - "cms.ui.category.localization_none"))); - TableColumnModel tab_model = getColumnModel(); + setEmptyView(new Label(messagesUtil + .getGlobalizedMessage("cms.ui.category.localization_none"))); + final TableColumnModel columnModel = getColumnModel(); // define columns - // XXX globalize - tab_model.add(new TableColumn(0, GlobalizationUtil.globalize( - "cms.ui.category.localization.locale").localize(), TABLE_COL_LANG)); - tab_model.add(new TableColumn(1, GlobalizationUtil.globalize( - "cms.ui.category.localization_name").localize())); - tab_model.add(new TableColumn(2, GlobalizationUtil.globalize( - "cms.ui.category.localization_description").localize())); - tab_model.add(new TableColumn(3, GlobalizationUtil.globalize( - "cms.ui.category.localization_url").localize())); - tab_model.add(new TableColumn(4, GlobalizationUtil.globalize( - "cms.ui.category.localization_action").localize(), TABLE_COL_DEL)); + columnModel.add(new TableColumn( + 0, + messagesUtil.getGlobalizedMessage( + "cms.ui.category.localization.locale") + .localize(), + TABLE_COL_LANG)); + columnModel.add(new TableColumn( + 1, + messagesUtil + .getGlobalizedMessage("cms.ui.category.localization_name") + .localize())); + columnModel.add(new TableColumn( + 2, + messagesUtil.getGlobalizedMessage( + "cms.ui.category.localization_description") + .localize())); + columnModel.add(new TableColumn( + 3, + messagesUtil + .getGlobalizedMessage("cms.ui.category.localization_url") + .localize())); + columnModel.add(new TableColumn( + 4, + messagesUtil + .getGlobalizedMessage("cms.ui.category.localization_action") + .localize(), + TABLE_COL_DEL)); - setModelBuilder(new CategoryLocalizationTableModelBuilder()); + super.setModelBuilder(new CategoryLocalizationTableModelBuilder()); - tab_model.get(0).setCellRenderer(new EditCellRenderer()); - tab_model.get(4).setCellRenderer(new DeleteCellRenderer()); + columnModel.get(0).setCellRenderer(new EditCellRenderer()); + columnModel.get(4).setCellRenderer(new DeleteCellRenderer()); - addTableActionListener(this); + super.addTableActionListener(this); } @@ -105,7 +135,7 @@ public class CategoryLocalizationTable extends Table implements TableActionListe * */ private class CategoryLocalizationTableModelBuilder extends LockableImpl - implements TableModelBuilder { + implements TableModelBuilder { public TableModel makeModel(Table table, PageState state) { final Category category = m_category.getCategory(state); @@ -116,6 +146,7 @@ public class CategoryLocalizationTable extends Table implements TableActionListe return Table.EMPTY_MODEL; } } + } /** @@ -128,7 +159,8 @@ public class CategoryLocalizationTable extends Table implements TableActionListe private ArrayList localizedStringCollection; private LocalizedString m_categoryLocalization; - private CategoryLocalizationTableModel(Table t, PageState ps, Category category) { + private CategoryLocalizationTableModel(Table t, PageState ps, + Category category) { m_table = t; localizedStringCollection = new ArrayList<>(); localizedStringCollection.add(category.getTitle()); @@ -194,41 +226,52 @@ public class CategoryLocalizationTable extends Table implements TableActionListe return null; // return m_categoryLocalization.getID(); } + } - private class EditCellRenderer extends LockableImpl implements TableCellRenderer { + private class EditCellRenderer extends LockableImpl implements + TableCellRenderer { public Component getComponent(Table table, PageState state, Object value, - boolean isSelected, final Object key, - int row, int column) { + boolean isSelected, final Object key, + int row, int column) { final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); - final PermissionChecker permissionChecker = cdiUtil.findBean(PermissionChecker.class); + final PermissionChecker permissionChecker = cdiUtil.findBean( + PermissionChecker.class); - if (permissionChecker.isPermitted(AdminPrivileges.ADMINISTER_CATEGORIES, m_category.getCategory(state))) { + if (permissionChecker.isPermitted( + AdminPrivileges.ADMINISTER_CATEGORIES, m_category.getCategory( + state))) { return new ControlLink(value.toString()); } else { return new Label(GlobalizationUtil.globalize(value.toString())); } } + } - private class DeleteCellRenderer extends LockableImpl implements TableCellRenderer { + private class DeleteCellRenderer extends LockableImpl implements + TableCellRenderer { public Component getComponent(Table table, PageState state, Object value, - boolean isSelected, Object key, - int row, int column) { + boolean isSelected, Object key, + int row, int column) { final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); - final PermissionChecker permissionChecker = cdiUtil.findBean(PermissionChecker.class); + final PermissionChecker permissionChecker = cdiUtil.findBean( + PermissionChecker.class); - if (permissionChecker.isPermitted(AdminPrivileges.ADMINISTER_CATEGORIES, m_category.getCategory(state))) { + if (permissionChecker.isPermitted( + AdminPrivileges.ADMINISTER_CATEGORIES, m_category.getCategory( + state))) { ControlLink link = new ControlLink(value.toString()); link.setConfirmation(GlobalizationUtil.globalize( - "cms.ui.category.localization_confirm_delete")); + "cms.ui.category.localization_confirm_delete")); return link; } else { return null; } } + } /** @@ -259,7 +302,6 @@ public class CategoryLocalizationTable extends Table implements TableActionListe // if (col.getHeaderKey().toString().equals(TABLE_COL_DEL)) { // category.delLanguage(categoryLocalization.getLocale()); // } - } /** @@ -269,4 +311,5 @@ public class CategoryLocalizationTable extends Table implements TableActionListe public void headSelected(TableActionEvent e) { throw new UnsupportedOperationException("Not Implemented"); } + } diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/IndexItemSelectionForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/IndexItemSelectionForm.java index daca8b3f6..c944e0062 100755 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/IndexItemSelectionForm.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/IndexItemSelectionForm.java @@ -18,9 +18,17 @@ */ package com.arsdigita.cms.ui.category; -import com.arsdigita.bebop.*; -import com.arsdigita.bebop.event.FormProcessListener; +import com.arsdigita.bebop.ColumnPanel; +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.Text; import com.arsdigita.bebop.event.FormSectionEvent; +import com.arsdigita.bebop.event.PrintEvent; import com.arsdigita.bebop.form.FormErrorDisplay; import com.arsdigita.bebop.form.Option; import com.arsdigita.bebop.form.RadioGroup; @@ -28,182 +36,223 @@ import com.arsdigita.bebop.form.Submit; import com.arsdigita.bebop.parameters.ParameterData; import com.arsdigita.bebop.parameters.StringParameter; -import com.arsdigita.bebop.util.GlobalizationUtil; import com.arsdigita.cms.CMS; import com.arsdigita.cms.ui.CMSForm; import com.arsdigita.cms.ui.FormSecurityListener; -import com.arsdigita.util.Assert; -import com.arsdigita.util.UncheckedWrapperException; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.libreccm.categorization.*; +import org.libreccm.categorization.Category; +import org.libreccm.categorization.CategoryManager; +import org.libreccm.categorization.CategoryRepository; +import org.libreccm.categorization.ObjectNotAssignedToCategoryException; + import org.libreccm.cdi.utils.CdiUtil; import org.libreccm.core.CcmObject; -import org.librecms.contentsection.*; +import org.libreccm.core.UnexpectedErrorException; +import org.libreccm.l10n.GlobalizationHelper; +import org.libreccm.l10n.GlobalizedMessagesUtil; +import org.librecms.CmsConstants; +import org.librecms.contentsection.ContentItem; +import org.librecms.contentsection.ContentItemManager; +import org.librecms.contentsection.ContentItemRepository; +import org.librecms.contentsection.ContentSection; +import org.librecms.contentsection.ContentSectionManager; + import org.librecms.contentsection.privileges.AdminPrivileges; import org.librecms.dispatcher.ItemResolver; +import java.util.List; import java.util.Optional; +import java.util.TooManyListenersException; /** * 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) - * @author Yannick Bülter + + * @author Yannick Bülter + * @author Jens Pelzetter */ public class IndexItemSelectionForm extends CMSForm { private static final Logger LOGGER = LogManager.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 SaveCancelSection m_saveCancelSection; + private final CategoryRequestLocal selectedCategory; + private RadioGroup optionsGroup; + private final SaveCancelSection saveCancelSection; + + public IndexItemSelectionForm(final CategoryRequestLocal selectedCategory) { - public IndexItemSelectionForm(CategoryRequestLocal m) { super("EditCategory"); + super.setMethod(Form.POST); + + this.selectedCategory = selectedCategory; final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); - final ContentSectionManager sectionManager = cdiUtil.findBean( - ContentSectionManager.class); - final CategoryManager categoryManager = cdiUtil.findBean( - CategoryManager.class); - final CategoryRepository categoryRepository = cdiUtil.findBean( - CategoryRepository.class); - final ContentItemManager contentItemManager = cdiUtil.findBean( - ContentItemManager.class); - final ContentItemRepository contentItemRepository = cdiUtil.findBean( - ContentItemRepository.class); - - setMethod(Form.POST); - - m_category = m; + final GlobalizationHelper globalizationHelper = cdiUtil + .findBean(GlobalizationHelper.class); + final GlobalizedMessagesUtil messagesUtil = globalizationHelper + .getGlobalizedMessagesUtil(CmsConstants.CMS_BUNDLE); // Form header - Label header = new Label(GlobalizationUtil.globalize( - "cms.ui.category.select_index_item")); + final Label header = new Label(messagesUtil + .getGlobalizedMessage("cms.ui.category.select_index_item")); header.setFontWeight(Label.BOLD); - add(header, ColumnPanel.FULL_WIDTH); + super.add(header, ColumnPanel.FULL_WIDTH); // Form errors - FormErrorDisplay m_errors = new FormErrorDisplay(this); - add(m_errors, ColumnPanel.FULL_WIDTH); + final FormErrorDisplay errorsDisplay = new FormErrorDisplay(this); + super.add(errorsDisplay, ColumnPanel.FULL_WIDTH); // Option Group - m_options = new RadioGroup(new StringParameter("items")); + optionsGroup = new RadioGroup(new StringParameter("items")); try { - m_options.addPrintListener(event -> { - RadioGroup group = (RadioGroup) event.getTarget(); - PageState state = event.getPageState(); - Category category = getCategory(event.getPageState()); - java.util.List children = category.getObjects(); - - group.clearOptions(); - - // option for NO index Object - group.addOption(new Option(NONE_OPTION_VALUE, - new Label(GlobalizationUtil - .globalize( - "cms.ui.category.non_option")))); - - // option for inheriting from the parent category - if (category.getParentCategory() != null) { - group.addOption(new Option(NULL_OPTION_VALUE, - new Label(GlobalizationUtil - .globalize( - "cms.ui.category.inherit_parent")))); - } - - final ContentSection section = CMS.getContext() - .getContentSection(); - final ItemResolver itemResolver = sectionManager - .getItemResolver( - section); - for (Categorization child : children) { - ContentItem item = (ContentItem) child - .getCategorizedObject(); - Link link = new Link( - new Text(item.getDisplayName()), - itemResolver.generateItemURL( - state, - item.getObjectId(), - item.getDisplayName(), - section, - item.getVersion().name() - ) - ); - Component linkComponent = link; - //add the option with the link - group.addOption(new Option(item.getItemUuid(), - linkComponent)); - } - - // get currently selected item - Optional optionalIndexObject = categoryManager - .getIndexObject(category) - .stream() - .findFirst(); - if (optionalIndexObject.isPresent()) { - ContentItem indexItem = (ContentItem) optionalIndexObject - .get(); - group.setValue( - state, - contentItemManager.getDraftVersion(indexItem, - ContentItem.class) - .getItemUuid() - ); - } else { - String value = category.getParentCategory() != null - ? NULL_OPTION_VALUE - : NONE_OPTION_VALUE; - group.setValue(state, value); - } - }); - } catch (java.util.TooManyListenersException e) { - LOGGER.error("Error adding init listener to Radio Group", e); - throw new UncheckedWrapperException(e); + optionsGroup.addPrintListener(this::printOptionsGroup); + } catch (TooManyListenersException ex) { + LOGGER.error("Error adding init listener to Radio Group", ex); + throw new UnexpectedErrorException(ex); } - m_options.setLayout(RadioGroup.VERTICAL); - add(m_options); + optionsGroup.setLayout(RadioGroup.VERTICAL); + super.add(optionsGroup); // Save and cancel buttons - m_saveCancelSection = new SaveCancelSection(); - add(m_saveCancelSection, ColumnPanel.FULL_WIDTH | ColumnPanel.LEFT); + saveCancelSection = new SaveCancelSection(); + super.add(saveCancelSection, ColumnPanel.FULL_WIDTH | ColumnPanel.LEFT); - addSubmissionListener(new FormSecurityListener( + super.addSubmissionListener(new FormSecurityListener( AdminPrivileges.ADMINISTER_CATEGORIES)); // Process listener - addProcessListener(event -> { - PageState state = event.getPageState(); - FormData data = event.getFormData(); - ParameterData param = data.getParameter(m_options - .getParameterModel().getName()); - String selectedValue = (String) param.getValue(); + super.addProcessListener(this::process); + } - Category category - = getCategory(event.getPageState()); + private void printOptionsGroup(final PrintEvent event) { - if (selectedValue != null) { - Optional optionalItem = contentItemRepository - .findById(Long.parseLong(selectedValue)); - if (optionalItem.isPresent()) { - ContentItem item = contentItemManager.getDraftVersion( - optionalItem.get(), ContentItem.class); - try { - categoryManager.setIndexObject(category, item); - categoryRepository.save(category); - } catch (ObjectNotAssignedToCategoryException e) { - throw new FormProcessException(e); - } + final RadioGroup group = (RadioGroup) event.getTarget(); + final PageState state = event.getPageState(); + final Category category = getCategory(event.getPageState()); + + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final CategoryAdminController controller = cdiUtil + .findBean(CategoryAdminController.class); + final ContentItemManager itemManager = cdiUtil + .findBean(ContentItemManager.class); + final CategoryManager categoryManager = cdiUtil + .findBean(CategoryManager.class); + final ContentSectionManager sectionManager = cdiUtil + .findBean(ContentSectionManager.class); + final GlobalizationHelper globalizationHelper = cdiUtil + .findBean(GlobalizationHelper.class); + final GlobalizedMessagesUtil messagesUtil = globalizationHelper + .getGlobalizedMessagesUtil(CmsConstants.CMS_BUNDLE); + + group.clearOptions(); + + // option for NO index Object + group.addOption( + new Option(NONE_OPTION_VALUE, + new Label(messagesUtil + .getGlobalizedMessage("cms.ui.category.non_option")))); + + // option for inheriting from the parent category + if (category.getParentCategory() != null) { + group.addOption( + new Option(NULL_OPTION_VALUE, + new Label(messagesUtil + .getGlobalizedMessage( + "cms.ui.category.inherit_parent")))); + } + + final ContentSection section = CMS.getContext() + .getContentSection(); + final ItemResolver itemResolver = sectionManager + .getItemResolver(section); + + final List assignedItems = controller + .retrieveAssignedContentItems(category); + for (final ContentItem item : assignedItems) { + + final Link link = new Link( + new Text(item.getDisplayName()), + itemResolver.generateItemURL( + state, + item.getObjectId(), + item.getDisplayName(), + section, + item.getVersion().name() + ) + ); + //add the option with the link + group.addOption(new Option(Long.toString(item.getObjectId()), link)); + } + + // get currently selected item + final Optional optionalIndexObject = categoryManager + .getIndexObject(category) + .stream() + .findFirst(); + if (optionalIndexObject.isPresent()) { + final ContentItem indexItem + = (ContentItem) optionalIndexObject + .get(); + final ContentItem liveItem = itemManager + .getLiveVersion(indexItem, ContentItem.class) + .get(); + group.setValue( + state, + Long.toString(liveItem.getObjectId())); + } else { + final String value; + if (category.getParentCategory() == null) { + value = NULL_OPTION_VALUE; + } else { + value = NONE_OPTION_VALUE; + } + group.setValue(state, value); + } + } + + private void process(final FormSectionEvent event) + throws FormProcessException { + + final FormData data = event.getFormData(); + final ParameterData param = data + .getParameter(optionsGroup.getParameterModel().getName()); + final String selectedValue = (String) param.getValue(); + + final Category category = getCategory(event.getPageState()); + + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final CategoryManager categoryManager = cdiUtil + .findBean(CategoryManager.class); + final CategoryRepository categoryRepository = cdiUtil + .findBean(CategoryRepository.class); + final ContentItemManager contentItemManager = cdiUtil + .findBean(ContentItemManager.class); + final ContentItemRepository contentItemRepository = cdiUtil + .findBean(ContentItemRepository.class); + + if (selectedValue != null) { + final Optional optionalItem = contentItemRepository + .findById(Long.parseLong(selectedValue)); + if (optionalItem.isPresent()) { + final ContentItem item = contentItemManager + .getLiveVersion(optionalItem.get(), + ContentItem.class) + .get(); + try { + categoryManager.setIndexObject(category, item); + categoryRepository.save(category); + } catch (ObjectNotAssignedToCategoryException ex) { + throw new FormProcessException(ex); } } - - }); + } } /** @@ -212,7 +261,7 @@ public class IndexItemSelectionForm extends CMSForm { * @return The cancel button */ protected Submit getCancelButton() { - return m_saveCancelSection.getCancelButton(); + return saveCancelSection.getCancelButton(); } /** @@ -224,9 +273,8 @@ public class IndexItemSelectionForm extends CMSForm { * * @pre ( state != null ) */ - protected Category getCategory(PageState state) { - Category category = m_category.getCategory(state); - return category; + protected Category getCategory(final PageState state) { + return selectedCategory.getCategory(state); } } diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/SubcategoryList.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/SubcategoryList.java index d90e8dc70..8716d2bd8 100755 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/SubcategoryList.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/category/SubcategoryList.java @@ -23,66 +23,113 @@ import com.arsdigita.bebop.List; import com.arsdigita.bebop.PageState; import com.arsdigita.bebop.SingleSelectionModel; import com.arsdigita.bebop.event.ActionEvent; -import com.arsdigita.bebop.event.ActionListener; import com.arsdigita.bebop.list.ListModel; import com.arsdigita.bebop.list.ListModelBuilder; import com.arsdigita.bebop.util.GlobalizationUtil; import com.arsdigita.util.LockableImpl; + import org.libreccm.categorization.Category; +import org.libreccm.categorization.CategoryManager; +import org.libreccm.cdi.utils.CdiUtil; /** * A List of all subcategories of the current category. * - * @author Yannick Bülter + * * @author Stanislav Freidin (stas@arsdigita.com) * @author Michael Pih (pihman@arsdigita.com) - * @version $Revision: #15 $ $DateTime: 2004/08/17 23:15:09 $ + * @author Yannick Bülter + * @author Jens Pelzetter */ public class SubcategoryList extends SortableCategoryList { - private final CategoryRequestLocal m_parent; - private final SingleSelectionModel m_model; - public SubcategoryList(final CategoryRequestLocal parent, - final SingleSelectionModel model) { - super(parent); + private final CategoryRequestLocal parentCategory; + private final SingleSelectionModel selectedCategoryId; - m_parent = parent; - m_model = model; + public SubcategoryList( + final CategoryRequestLocal parentCategory, + final SingleSelectionModel selectedCategoryId) { - setIdAttr("subcategories_list"); + super(parentCategory); + + this.parentCategory = parentCategory; + this.selectedCategoryId = selectedCategoryId; + + super.setIdAttr("subcategories_list"); setModelBuilder(new SubcategoryModelBuilder()); // Select the category in the main tree when the // user selects it here - addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent event) { - final PageState state = event.getPageState(); - final String id = (String) getSelectedKey(state); + super.addActionListener(this::actionPerformed); - if (id != null) { - m_model.setSelectedKey(state, id); - } - } - }); - - Label label = new Label(GlobalizationUtil.globalize - ("cms.ui.category.subcategory.none")); + Label label = new Label(GlobalizationUtil.globalize( + "cms.ui.category.subcategory.none")); label.setFontWeight(Label.ITALIC); setEmptyView(label); } - private class SubcategoryModelBuilder extends LockableImpl - implements ListModelBuilder { - public ListModel makeModel(List list, PageState state) { - final Category category = m_parent.getCategory(state); + /** + * Select the category in the main tree when the user selects it here + * + * @param event + */ + private void actionPerformed(final ActionEvent event) { + + final PageState state = event.getPageState(); + final String categoryId = (String) getSelectedKey(state); + + if (categoryId != null) { + selectedCategoryId.setSelectedKey(state, categoryId); + } + } + + public ListModel makeMake(final List list, final PageState state) { + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final CategoryManager categoryManager = cdiUtil + .findBean(CategoryManager.class); + final Category category = parentCategory.getCategory(state); + + if (category != null + && categoryManager.hasSubCategories(category)) { + + final CategoryAdminController controller = cdiUtil.findBean( + CategoryAdminController.class); + final java.util.List children = controller + .generateSubCategoryList(category); + + return new CategoryListModel(children); + } else { + return List.EMPTY_MODEL; + } + + } + + private class SubcategoryModelBuilder extends LockableImpl + implements ListModelBuilder { + + @Override + public ListModel makeModel(final List list, final PageState state) { + + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final CategoryManager categoryManager = cdiUtil + .findBean(CategoryManager.class); + final Category category = parentCategory.getCategory(state); + + if (category != null + && categoryManager.hasSubCategories(category)) { + + final CategoryAdminController controller = cdiUtil.findBean( + CategoryAdminController.class); + final java.util.List children = controller + .generateSubCategoryList(category); - if (category != null && !category.getSubCategories().isEmpty()) { - java.util.List children = category.getSubCategories(); return new CategoryListModel(children); } else { return List.EMPTY_MODEL; } } + } + }