From b52ce4649a51b36dfea5ea04ea0ac8a2cce7fafb Mon Sep 17 00:00:00 2001 From: jensp Date: Thu, 18 May 2017 17:39:40 +0000 Subject: [PATCH] CCM NG/ccm-cms: ArticlePropertiesStep and dependencies git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4743 8810af33-2d31-482b-a856-94f89814c4df --- ...p.java.TODO => ArticlePropertiesStep.java} | 61 +++--- .../contenttypes/ui/ArticlePropertyForm.java | 170 +++++++++++++++ .../ui/GenericArticlePropertiesStep.java | 128 ++++++++++++ .../ui/GenericArticlePropertiesStep.java.TODO | 109 ---------- .../ui/GenericArticlePropertyForm.java | 142 +++++++++++++ .../cms/ui/SecurityPropertyEditor.java | 4 +- ...EditStep.java.TODO => SimpleEditStep.java} | 101 +++++---- .../com/arsdigita/domain/DomainService.java | 197 ++++++++++++++++++ ...va.TODO => DomainObjectPropertySheet.java} | 83 ++++++-- 9 files changed, 802 insertions(+), 193 deletions(-) rename ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/{ArticlePropertiesStep.java.TODO => ArticlePropertiesStep.java} (59%) create mode 100755 ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/ArticlePropertyForm.java create mode 100755 ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/GenericArticlePropertiesStep.java delete mode 100755 ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/GenericArticlePropertiesStep.java.TODO create mode 100755 ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/GenericArticlePropertyForm.java rename ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/{SimpleEditStep.java.TODO => SimpleEditStep.java} (76%) create mode 100755 ccm-core/src/main/java/com/arsdigita/domain/DomainService.java rename ccm-core/src/main/java/com/arsdigita/toolbox/ui/{DomainObjectPropertySheet.java.TODO => DomainObjectPropertySheet.java} (87%) diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/ArticlePropertiesStep.java.TODO b/ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/ArticlePropertiesStep.java similarity index 59% rename from ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/ArticlePropertiesStep.java.TODO rename to ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/ArticlePropertiesStep.java index 9c08cabd2..98494a87c 100755 --- a/ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/ArticlePropertiesStep.java.TODO +++ b/ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/ArticlePropertiesStep.java @@ -20,65 +20,72 @@ package com.arsdigita.cms.contenttypes.ui; import com.arsdigita.bebop.Component; import com.arsdigita.cms.ItemSelectionModel; + import org.librecms.contenttypes.Article; + import com.arsdigita.cms.ui.authoring.AuthoringKitWizard; import com.arsdigita.cms.ui.authoring.BasicPageForm; import com.arsdigita.cms.ui.workflow.WorkflowLockedComponentAccess; +import com.arsdigita.globalization.GlobalizedMessage; import com.arsdigita.toolbox.ui.DomainObjectPropertySheet; +import org.librecms.CmsConstants; + /** - * Authoring step to edit the simple attributes of the Article content - * type (and its subclasses). The attributes edited are 'name', 'title', - * 'article date', 'location', 'lead', and 'article type'. - * This authoring step replaces - * the com.arsdigita.ui.authoring.PageEdit step for this type. + * Authoring step to edit the simple attributes of the Article content type (and + * its subclasses). The attributes edited are 'name', 'title', 'article date', + * 'location', 'lead', and 'article type'. This authoring step replaces the + * {@code com.arsdigita.ui.authoring.PageEdit} step for this type. */ public class ArticlePropertiesStep extends GenericArticlePropertiesStep { - /** The name of the editing sheet added to this step */ - public static String EDIT_SHEET_NAME = "edit"; - /** - * Constructor. - * - * @param itemModel - * @param parent + * The name of the editing sheet added to this step */ - public ArticlePropertiesStep(ItemSelectionModel itemModel, + public final static String EDIT_SHEET_NAME = "edit"; + + public ArticlePropertiesStep(ItemSelectionModel itemModel, AuthoringKitWizard parent) { super(itemModel, parent); } @Override - protected void createEditSheet(ItemSelectionModel itemModel) { + protected void createEditSheet(final ItemSelectionModel itemModel) { BasicPageForm editSheet; editSheet = new ArticlePropertyForm(itemModel, this); - add(EDIT_SHEET_NAME, - GlobalizationUtil.globalize("cms.ui.edit"), - new WorkflowLockedComponentAccess(editSheet, itemModel), + add(EDIT_SHEET_NAME, + new GlobalizedMessage("cms.ui.edit", CmsConstants.CMS_BUNDLE), + new WorkflowLockedComponentAccess(editSheet, itemModel), editSheet.getSaveCancelSection().getCancelButton()); } @Override - protected void setDisplayComponent(ItemSelectionModel itemModel) { + protected void setDisplayComponent(final ItemSelectionModel itemModel) { setDisplayComponent(getArticlePropertySheet(itemModel)); } /** - * Returns a component that displays the properties of the - * Article specified by the ItemSelectionModel passed in. + * Returns a component that displays the properties of the Article specified + * by the ItemSelectionModel passed in. + * * @param itemModel The ItemSelectionModel to use + * * @pre itemModel != null - * @return A component to display the state of the basic properties - * of the release + * @return A component to display the state of the basic properties of the + * release */ - public static Component getArticlePropertySheet(ItemSelectionModel itemModel) { - DomainObjectPropertySheet sheet = (DomainObjectPropertySheet) - getGenericArticlePropertySheet(itemModel); + public static Component getArticlePropertySheet( + final ItemSelectionModel itemModel) { - sheet.add(GlobalizationUtil.globalize("cms.contenttypes.ui.lead"), - Article.LEAD); + final DomainObjectPropertySheet sheet + = (DomainObjectPropertySheet) getGenericArticlePropertySheet( + itemModel); + + sheet.add(new GlobalizedMessage("cms.contenttypes.ui.lead", + CmsConstants.CMS_BUNDLE), + "lead"); return sheet; } + } diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/ArticlePropertyForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/ArticlePropertyForm.java new file mode 100755 index 000000000..c9853b985 --- /dev/null +++ b/ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/ArticlePropertyForm.java @@ -0,0 +1,170 @@ +/* + * 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.contenttypes.ui; + +import com.arsdigita.bebop.FormData; +import com.arsdigita.bebop.FormProcessException; +import com.arsdigita.bebop.event.FormInitListener; +import com.arsdigita.bebop.event.FormProcessListener; +import com.arsdigita.bebop.event.FormSubmissionListener; +import com.arsdigita.bebop.event.FormSectionEvent; +import com.arsdigita.bebop.form.TextArea; +import com.arsdigita.bebop.parameters.NotEmptyValidationListener; +import com.arsdigita.bebop.parameters.ParameterModel; +import com.arsdigita.bebop.parameters.StringInRangeValidationListener; +import com.arsdigita.bebop.parameters.StringParameter; + +import org.librecms.contentsection.ContentSection; + +import com.arsdigita.cms.ItemSelectionModel; +import com.arsdigita.globalization.GlobalizedMessage; +import com.arsdigita.kernel.KernelConfig; + +import org.arsdigita.cms.CMSConfig; +import org.libreccm.cdi.utils.CdiUtil; +import org.librecms.CmsConstants; +import org.librecms.contentsection.ContentItemRepository; +import org.librecms.contenttypes.Article; + +/** + * Form to edit the basic properties of an article. This form can be extended to + * create forms for Article subclasses. + */ +public class ArticlePropertyForm extends GenericArticlePropertyForm + implements FormProcessListener, FormInitListener, FormSubmissionListener { + + public static final String LEAD = "lead"; + + private ArticlePropertiesStep propertyStep; + + /** + * Creates a new form to edit the Article object specified by the item + * selection model passed in. + * + * @param itemModel The ItemSelectionModel to use to obtain the Article to + * work on + */ + public ArticlePropertyForm(final ItemSelectionModel itemModel) { + this(itemModel, null); + } + + /** + * Creates a new form to edit the Article object specified by the item + * selection model passed in. + * + * @param itemModel The ItemSelectionModel to use to obtain the Article to + * work on + * @param step The ArticlePropertiesStep which controls this form. + */ + public ArticlePropertyForm(final ItemSelectionModel itemModel, + final ArticlePropertiesStep step) { + super(itemModel, step); + propertyStep = step; + addSubmissionListener(this); + } + + /** + * Adds widgets to the form. + */ + @Override + protected void addWidgets() { + super.addWidgets(); + + ParameterModel leadParam = new StringParameter(LEAD); + if (CMSConfig.getConfig().isMandatoryDescriptions()) { + leadParam.addParameterListener( + new NotEmptyValidationListener( + new GlobalizedMessage( + "cms.contenttypes.ui.description_missing", + CmsConstants.CMS_BUNDLE))); + } + leadParam + .addParameterListener(new StringInRangeValidationListener(0, + 1000)); + TextArea lead = new TextArea(leadParam); + lead.setLabel(new GlobalizedMessage("cms.contenttypes.ui.lead", + CmsConstants.CMS_BUNDLE)); + lead.setHint(new GlobalizedMessage("cms.contenttypes.ui.lead_hint", + CmsConstants.CMS_BUNDLE)); + lead.setCols(40); + lead.setRows(5); + add(lead); + } + + @Override + public void validate(final FormSectionEvent event) throws + FormProcessException { + super.validate(event); + } + + /** + * Form initialisation hook. Fills widgets with data. + * + * @param event + */ + @Override + public void init(final FormSectionEvent event) { + // Do some initialization hook stuff + FormData data = event.getFormData(); + final Article article = (Article) super.initBasicWidgets(event); + + data.put(LEAD, article.getDescription()); + } + + /** + * Cancels streamlined editing. + */ + @Override + public void submitted(final FormSectionEvent event) { + if (propertyStep != null && getSaveCancelSection().getCancelButton() + .isSelected(event.getPageState())) { + propertyStep.cancelStreamlinedCreation(event.getPageState()); + } + } + + /** + * Form processing hook. Saves Event object. + * + * @param event + */ + @Override + public void process(final FormSectionEvent event) { + FormData data = event.getFormData(); + + Article article = (Article) super.processBasicWidgets(event); + + // save only if save button was pressed + if (article != null + && getSaveCancelSection().getSaveButton() + .isSelected(event.getPageState())) { + + article.getDescription().addValue(KernelConfig.getConfig() + .getDefaultLocale(), (String) data.get(LEAD)); + + final ContentItemRepository itemRepo = CdiUtil + .createCdiUtil() + .findBean(ContentItemRepository.class); + itemRepo.save(article); + } + if (propertyStep != null) { + propertyStep.maybeForwardToNextStep(event.getPageState()); + } + } + +} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/GenericArticlePropertiesStep.java b/ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/GenericArticlePropertiesStep.java new file mode 100755 index 000000000..5cd61cc6c --- /dev/null +++ b/ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/GenericArticlePropertiesStep.java @@ -0,0 +1,128 @@ +/* + * 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.contenttypes.ui; + +import com.arsdigita.bebop.Component; +import com.arsdigita.bebop.PageState; + +import org.librecms.contentsection.ContentSection; + +import com.arsdigita.cms.ItemSelectionModel; +import com.arsdigita.toolbox.ui.DomainObjectPropertySheet; +import com.arsdigita.cms.ui.authoring.AuthoringKitWizard; +import com.arsdigita.cms.ui.authoring.BasicPageForm; +import com.arsdigita.cms.ui.authoring.SimpleEditStep; +import com.arsdigita.cms.ui.workflow.WorkflowLockedComponentAccess; +import com.arsdigita.globalization.GlobalizedMessage; + +import org.arsdigita.cms.CMSConfig; +import org.librecms.CmsConstants; +import org.librecms.contentsection.ContentItem; + +import java.text.DateFormat; + +/** + * Authoring step to edit the simple attributes of the GenericArticle content + * type (and its subclasses). The attributes edited are 'name', 'title', + * 'article date', 'location', and 'article type'. This authoring step replaces + * the {@code com.arsdigita.ui.authoring.PageEdit} step for this type. + */ +public class GenericArticlePropertiesStep extends SimpleEditStep { + + /** + * The name of the editing sheet added to this step + */ + public static final String EDIT_SHEET_NAME = "edit"; + + private DomainObjectPropertySheet domainObjectPropertySheet; + + public GenericArticlePropertiesStep(final ItemSelectionModel itemModel, + final AuthoringKitWizard parent) { + + super(itemModel, parent); + + setDefaultEditKey(EDIT_SHEET_NAME); + createEditSheet(itemModel); + + setDisplayComponent(itemModel); + } + + protected void createEditSheet(final ItemSelectionModel itemModel) { + BasicPageForm editSheet; + editSheet = new GenericArticlePropertyForm(itemModel, this); + add(EDIT_SHEET_NAME, + new GlobalizedMessage("cms.ui.edit", CmsConstants.CMS_BUNDLE), + new WorkflowLockedComponentAccess(editSheet, itemModel), + editSheet.getSaveCancelSection().getCancelButton()); + } + + protected void setDisplayComponent(ItemSelectionModel itemModel) { + setDisplayComponent(getGenericArticlePropertySheet(itemModel)); + } + + /** + * Returns a component that displays the properties of the Article specified + * by the ItemSelectionModel passed in. + * + * @param itemModel The ItemSelectionModel to use + * + * @pre itemModel != null + * @return A component to display the state of the basic properties of the + * release + */ + public static Component getGenericArticlePropertySheet( + ItemSelectionModel itemModel) { + DomainObjectPropertySheet sheet = new DomainObjectPropertySheet( + itemModel); + + sheet.add(new GlobalizedMessage("cms.contenttypes.ui.title", + CmsConstants.CMS_BUNDLE), + "title"); + sheet.add(new GlobalizedMessage("cms.contenttypes.ui.name", + CmsConstants.CMS_BUNDLE), + "name"); + if (!CMSConfig.getConfig().isHideLaunchDate()) { + sheet.add(new GlobalizedMessage("cms.contenttypes.ui.launch_date", + CmsConstants.CMS_BUNDLE), + "launchDate", + new DomainObjectPropertySheet.AttributeFormatter() { + + @Override + public String format(final Object item, + final String attribute, + final PageState state) { + final ContentItem page = (ContentItem) item; + if (page.getLaunchDate() != null) { + return DateFormat + .getDateInstance(DateFormat.LONG) + .format(page.getLaunchDate()); + } else { + return (String) new GlobalizedMessage( + "cms.ui.unknown", + CmsConstants.CMS_BUNDLE) + .localize(); + } + } + + }); + } + return sheet; + } + +} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/GenericArticlePropertiesStep.java.TODO b/ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/GenericArticlePropertiesStep.java.TODO deleted file mode 100755 index 992b064b0..000000000 --- a/ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/GenericArticlePropertiesStep.java.TODO +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (C) 2002-2004 Red Hat Inc. All Rights Reserved. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - */ -package com.arsdigita.cms.contenttypes.ui; - -import com.arsdigita.bebop.Component; -import com.arsdigita.bebop.PageState; -import com.arsdigita.cms.ContentPage; -import com.arsdigita.cms.ContentSection; -import com.arsdigita.cms.ItemSelectionModel; -import com.arsdigita.cms.contenttypes.GenericArticle; -import com.arsdigita.domain.DomainObject; -import com.arsdigita.toolbox.ui.DomainObjectPropertySheet; -import com.arsdigita.cms.ui.authoring.AuthoringKitWizard; -import com.arsdigita.cms.ui.authoring.BasicPageForm; -import com.arsdigita.cms.ui.authoring.SimpleEditStep; -import com.arsdigita.cms.ui.workflow.WorkflowLockedComponentAccess; -import com.arsdigita.cms.util.GlobalizationUtil; -import java.text.DateFormat; - -/** - * Authoring step to edit the simple attributes of the GenericArticle content - * type (and its subclasses). The attributes edited are 'name', 'title', - * 'article date', 'location', and 'article type'. - * This authoring step replaces - * the com.arsdigita.ui.authoring.PageEdit step for this type. - */ -public class GenericArticlePropertiesStep extends SimpleEditStep { - - /** The name of the editing sheet added to this step */ - public static String EDIT_SHEET_NAME = "edit"; - DomainObjectPropertySheet get; - - public GenericArticlePropertiesStep(ItemSelectionModel itemModel, - AuthoringKitWizard parent) { - super(itemModel, parent); - - setDefaultEditKey(EDIT_SHEET_NAME); - createEditSheet(itemModel); - - setDisplayComponent(itemModel); - } - - protected void createEditSheet(ItemSelectionModel itemModel) { - BasicPageForm editSheet; - editSheet = new GenericArticlePropertyForm(itemModel, this); - add(EDIT_SHEET_NAME, - GlobalizationUtil.globalize("cms.ui.edit"), - new WorkflowLockedComponentAccess(editSheet, itemModel), - editSheet.getSaveCancelSection().getCancelButton()); - } - - protected void setDisplayComponent(ItemSelectionModel itemModel) { - setDisplayComponent(getGenericArticlePropertySheet(itemModel)); - } - - /** - * Returns a component that displays the properties of the - * Article specified by the ItemSelectionModel passed in. - * @param itemModel The ItemSelectionModel to use - * @pre itemModel != null - * @return A component to display the state of the basic properties - * of the release - */ - public static Component getGenericArticlePropertySheet(ItemSelectionModel itemModel) { - DomainObjectPropertySheet sheet = new DomainObjectPropertySheet(itemModel); - - sheet.add(GlobalizationUtil.globalize("cms.contenttypes.ui.title"), - GenericArticle.TITLE); - sheet.add(GlobalizationUtil.globalize("cms.contenttypes.ui.name"), - GenericArticle.NAME); - if (!ContentSection.getConfig().getHideLaunchDate()) { - sheet.add(GlobalizationUtil.globalize("cms.contenttypes.ui.launch_date"), - ContentPage.LAUNCH_DATE, - new DomainObjectPropertySheet.AttributeFormatter() { - - @Override - public String format(DomainObject item, - String attribute, - PageState state) { - ContentPage page = (ContentPage) item; - if (page.getLaunchDate() != null) { - return DateFormat.getDateInstance(DateFormat.LONG) - .format(page.getLaunchDate()); - } else { - return (String) GlobalizationUtil.globalize( - "cms.ui.unknown").localize(); - } - } - }); - } - return sheet; - } -} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/GenericArticlePropertyForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/GenericArticlePropertyForm.java new file mode 100755 index 000000000..5a7f997bf --- /dev/null +++ b/ccm-cms/src/main/java/com/arsdigita/cms/contenttypes/ui/GenericArticlePropertyForm.java @@ -0,0 +1,142 @@ +/* + * 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.contenttypes.ui; + +import com.arsdigita.bebop.FormData; +import com.arsdigita.bebop.FormProcessException; +import com.arsdigita.bebop.event.FormInitListener; +import com.arsdigita.bebop.event.FormProcessListener; +import com.arsdigita.bebop.event.FormSubmissionListener; +import com.arsdigita.bebop.event.FormSectionEvent; +import com.arsdigita.cms.ItemSelectionModel; + +import org.librecms.contenttypes.Article; + +import com.arsdigita.cms.ui.authoring.BasicPageForm; + +import org.apache.logging.log4j.LogManager; +import org.libreccm.cdi.utils.CdiUtil; +import org.librecms.contentsection.ContentItemRepository; + +import javax.enterprise.inject.spi.CDI; + +/** + * Form to edit the basic properties of an article. This form can be extended to + * create forms for Article subclasses. + */ +public class GenericArticlePropertyForm extends BasicPageForm + implements FormProcessListener, FormInitListener, FormSubmissionListener { + + private GenericArticlePropertiesStep propertiesStep; + + /** + * Creates a new form to edit the Article object specified by the item + * selection model passed in. + * + * @param itemModel The ItemSelectionModel to use to obtain the Article to + * work on + */ + public GenericArticlePropertyForm(final ItemSelectionModel itemModel) { + this(itemModel, null); + } + + /** + * Creates a new form to edit the GenericArticle object specified by the + * item selection model passed in. + * + * @param itemModel The ItemSelectionModel to use to obtain the + * GenericArticle to work on + * @param step The GenericArticlePropertiesStep which controls this + * form. + */ + public GenericArticlePropertyForm(final ItemSelectionModel itemModel, + final GenericArticlePropertiesStep step) { + super(ID, itemModel); + propertiesStep = step; + addSubmissionListener(this); + } + + /** + * Adds widgets to the form. + */ + @Override + protected void addWidgets() { + super.addWidgets(); + } + + @Override + public void validate(final FormSectionEvent event) + throws FormProcessException { + + super.validate(event); + } + + /** + * Form initialisation hook. Fills widgets with data. + * + * @param event + */ + @Override + public void init(final FormSectionEvent event) { + // Do some initialization hook stuff + final FormData data = event.getFormData(); + final Article article = (Article) super.initBasicWidgets(event); + } + + /** + * Cancels streamlined editing. + * + * @param event + */ + @Override + public void submitted(final FormSectionEvent event) { + if (propertiesStep != null + && getSaveCancelSection().getCancelButton().isSelected(event + .getPageState())) { + propertiesStep.cancelStreamlinedCreation(event.getPageState()); + } + } + + /** + * Form processing hook. Saves Event object. + * + * @param event + */ + @Override + public void process(final FormSectionEvent event) { + + final Article article = (Article) super.processBasicWidgets(event); + + // save only if save button was pressed + if (article != null + && getSaveCancelSection().getSaveButton().isSelected(event + .getPageState())) { + + final ContentItemRepository itemRepo = CdiUtil + .createCdiUtil() + .findBean(ContentItemRepository.class); + + itemRepo.save(article); + } + if (propertiesStep != null) { + propertiesStep.maybeForwardToNextStep(event.getPageState()); + } + } + +} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/SecurityPropertyEditor.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/SecurityPropertyEditor.java index 48b126a43..91fd9ecb3 100755 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/SecurityPropertyEditor.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/SecurityPropertyEditor.java @@ -28,7 +28,6 @@ import com.arsdigita.bebop.PropertyEditorModel; import com.arsdigita.bebop.event.FormSectionEvent; import com.arsdigita.bebop.event.FormSubmissionListener; import com.arsdigita.bebop.form.Submit; -import com.arsdigita.cms.dispatcher.Utilities; import com.arsdigita.globalization.GlobalizedMessage; import com.arsdigita.toolbox.ui.ComponentAccess; import com.arsdigita.util.Assert; @@ -36,8 +35,7 @@ import com.arsdigita.util.Assert; import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import org.libreccm.cdi.utils.CdiUtil; -import org.libreccm.security.PermissionChecker; + import org.librecms.CmsConstants; /** diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/SimpleEditStep.java.TODO b/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/SimpleEditStep.java similarity index 76% rename from ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/SimpleEditStep.java.TODO rename to ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/SimpleEditStep.java index f0cc02112..f92223541 100755 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/SimpleEditStep.java.TODO +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/authoring/SimpleEditStep.java @@ -20,8 +20,6 @@ package com.arsdigita.cms.ui.authoring; import com.arsdigita.bebop.Page; import com.arsdigita.bebop.PageState; -import com.arsdigita.bebop.event.ActionEvent; -import com.arsdigita.bebop.event.ActionListener; import com.arsdigita.bebop.event.RequestEvent; import com.arsdigita.bebop.event.RequestListener; import com.arsdigita.bebop.parameters.StringParameter; @@ -29,22 +27,30 @@ import com.arsdigita.bebop.parameters.StringParameter; import org.librecms.contentsection.ContentItem; import com.arsdigita.cms.ItemSelectionModel; -import com.arsdigita.cms.dispatcher.Utilities; import com.arsdigita.cms.ui.ContentItemPage; import com.arsdigita.cms.ui.SecurityPropertyEditor; +import com.arsdigita.globalization.GlobalizedMessage; import com.arsdigita.toolbox.ui.ComponentAccess; import com.arsdigita.toolbox.ui.DomainObjectPropertySheet; -import org.apache.logging.log4j.LogManager; - import java.text.DateFormat; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; -import org.apache.logging.log4j.Logger; -import org.libreccm.core.CcmObject; +import org.libreccm.cdi.utils.CdiUtil; +import org.libreccm.core.UnexpectedErrorException; +import org.libreccm.l10n.GlobalizationHelper; +import org.librecms.CmsConstants; + +import java.beans.BeanInfo; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Optional; /** * A simple implementation of an Authoring Kit editing step. Extends @@ -130,16 +136,9 @@ public class SimpleEditStep extends SecurityPropertyEditor authoringKitWizard.getContentType().getContentItemClass().getName() + "_properties_done" + parameterSuffix); - authoringKitWizard.getList().addActionListener(new ActionListener() { - - @Override - public void actionPerformed(final ActionEvent event) { - - final PageState state = event.getPageState(); - showDisplayPane(state); - } - - }); + authoringKitWizard + .getList() + .addActionListener(event -> showDisplayPane(event.getPageState())); additionalDisplayComponents .stream() @@ -151,6 +150,8 @@ public class SimpleEditStep extends SecurityPropertyEditor /** * Registers global state parameter for cancelling streamlined creation + * + * @param page */ @Override public void register(final Page page) { @@ -269,7 +270,7 @@ public class SimpleEditStep extends SecurityPropertyEditor * Note: the format method has to be executed at each page request. Take * care to properly adjust globalisation and localisation here! * - * @param object Object containing the attribute to format. + * @param object Object containing the attribute to format. * @param attribute Name of the attribute to retrieve and format * @param state PageState of the request * @@ -277,33 +278,61 @@ public class SimpleEditStep extends SecurityPropertyEditor * the domain object. */ @Override - public String format(final CcmObject object, - final String attribute, + public String format(final Object object, + final String attribute, final PageState state) { if (object != null && object instanceof ContentItem) { final ContentItem page = (ContentItem) object; - final Object field = page.get(attribute); + final BeanInfo beanInfo; + try { + beanInfo = Introspector + .getBeanInfo(page.getClass()); + } catch (IntrospectionException ex) { + throw new UnexpectedErrorException(ex); + } + final Optional propertyDescriptor = Arrays + .stream(beanInfo.getPropertyDescriptors()) + .filter(propDesc -> attribute.equals(propDesc.getName())) + .findAny(); + if (propertyDescriptor.isPresent()) { + final Method readMethod = propertyDescriptor + .get() + .getReadMethod(); + final Object value; + try { + value = readMethod.invoke(object); + } catch (IllegalAccessException | InvocationTargetException ex) { + throw new IllegalArgumentException(ex); + } + if (value == null) { + return (String) new GlobalizedMessage("cms.ui.unknown", + CmsConstants.CMS_BUNDLE) + .localize(); + } else { + final GlobalizationHelper globalizationHelper = CdiUtil + .createCdiUtil() + .findBean(GlobalizationHelper.class); - if (field != null) { - // Note: No type safety here! We relay that it is - // attached to a date property! - return DateFormat.getDateInstance( - DateFormat.LONG, - GlobalizationHelper.getNegotiatedLocale() - ) - .format(field); + // Note: No type safety here! We relay that it is + // attached to a date property! + return DateFormat.getDateInstance( + DateFormat.LONG, + globalizationHelper.getNegotiatedLocale()) + .format(value); + } } else { - return (String) GlobalizationUtil - .globalize("cms.ui.unknown") + return (String) new GlobalizedMessage("cms.ui.unknown", + CmsConstants.CMS_BUNDLE) .localize(); } - } - return (String) GlobalizationUtil - .globalize("cms.ui.unknown") - .localize(); + } else { + return (String) new GlobalizedMessage("cms.ui.unknown", + CmsConstants.CMS_BUNDLE) + .localize(); + } } } diff --git a/ccm-core/src/main/java/com/arsdigita/domain/DomainService.java b/ccm-core/src/main/java/com/arsdigita/domain/DomainService.java new file mode 100755 index 000000000..5e0a47dbf --- /dev/null +++ b/ccm-core/src/main/java/com/arsdigita/domain/DomainService.java @@ -0,0 +1,197 @@ +/* + * 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.domain; + +import org.libreccm.core.CcmObject; +import org.libreccm.core.UnexpectedErrorException; + +import java.beans.BeanInfo; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Optional; + +/** + * + * This class should be extended by domain object services that need privileged + * access to the DataObject encapsulated by a DomainObject or the DataCollection + * encapsulated by a DomainCollection. + * + * Note: This class was tightly coupled with the old PDL based persistence + * system. The class has been adapted to use standard Bean/Reflection methods + * instead, but some the results may differ. + * + * @author Oumi Mehrotra + * @author Jens Pelzetter + */ +public abstract class DomainService { + + /** + * + * Get a property of the specified domain object. + * + * @param domainObject + * @param attr + * + * @return + * + */ + protected static Object get(final Object domainObject, + final String attr) { + + final BeanInfo beanInfo; + try { + beanInfo = Introspector.getBeanInfo(domainObject.getClass()); + } catch (IntrospectionException ex) { + throw new UnexpectedErrorException(ex); + } + + final Optional property = Arrays + .stream(beanInfo.getPropertyDescriptors()) + .filter(current -> attr.equals(current.getName())) + .findAny(); + + if (property.isPresent()) { + + final Method readMethod = property.get().getReadMethod(); + + final Object result; + try { + result = readMethod.invoke(domainObject); + } catch (IllegalAccessException | InvocationTargetException ex) { + throw new UnexpectedErrorException(ex); + } + return result; + } else { + return null; + } + } + + // These methods modify domain objects and should be used cautiously. + /** + * + * Set a property of the DomainObjects DataObject. + * + * + * @param domainObject + * @param attr + * @param value + */ + protected static void set(final Object domainObject, + final String attr, + final Object value) { + + final BeanInfo beanInfo; + try { + beanInfo = Introspector.getBeanInfo(domainObject.getClass()); + } catch (IntrospectionException ex) { + throw new UnexpectedErrorException(ex); + } + + final Optional property = Arrays + .stream(beanInfo.getPropertyDescriptors()) + .filter(current -> attr.equals(current.getName())) + .findAny(); + + if (property.isPresent()) { + + final Method writeMethod = property.get().getWriteMethod(); + + try { + writeMethod.invoke(domainObject, value); + } catch (IllegalAccessException | InvocationTargetException ex) { + throw new UnexpectedErrorException(ex); + } + + } + } + + /** + * + * Set an association DomainObjects DataObject. + * + * @see DomainObject#set(String, Object) + * + */ +// protected static void setAssociation(DomainObject domainObject, +// String attr, DomainObject dobj) { +// domainObject.set(attr, dobj); +// } + + /** + * Adds data object specified by value to the specified role (with + * multiplicity>1) of the specified domain object. + * + * @see DomainObject#add(String, DataObject) + */ +// protected static DataObject add(DomainObject domainObject, +// String roleName, DataObject value) { +// return domainObject.add(roleName, value); +// } + + /** + * Adds domain object specified by value to the specified role (with + * multiplicity>1) of the specified domain object. + * + * @see DomainObject#add(String, DomainObject) + */ +// protected static DataObject add(DomainObject domainObject, +// String roleName, DomainObject value) { +// return domainObject.add(roleName, value); +// } + + /** + * + * Removes data object specified by value from the specified role + * (with multiplicity>1) of the specified domain object. + * + * @see DomainObject#remove(String, DataObject) + */ +// protected static void remove(DomainObject domainObject, +// String roleName, DataObject value) { +// domainObject.remove(roleName, value); +// } + + /** + * + * Removes domain object specified by value from the specified role + * (with multiplicity>1) of the specified domain object. + * + * @see DomainObject#remove(String, DomainObject) + */ +// protected static void remove(DomainObject domainObject, +// String roleName, DomainObject value) { +// domainObject.remove(roleName, value); +// } + + /** + * + * Clears specified role (with multiplicity > 1) of specified domain object. + * + * @see DomainObject#clear(String) + */ +// protected static void clear(DomainObject domainObject, String roleName) { +// domainObject.clear(roleName); +// } + +} diff --git a/ccm-core/src/main/java/com/arsdigita/toolbox/ui/DomainObjectPropertySheet.java.TODO b/ccm-core/src/main/java/com/arsdigita/toolbox/ui/DomainObjectPropertySheet.java similarity index 87% rename from ccm-core/src/main/java/com/arsdigita/toolbox/ui/DomainObjectPropertySheet.java.TODO rename to ccm-core/src/main/java/com/arsdigita/toolbox/ui/DomainObjectPropertySheet.java index 8cfd79e72..f629c8d50 100755 --- a/ccm-core/src/main/java/com/arsdigita/toolbox/ui/DomainObjectPropertySheet.java.TODO +++ b/ccm-core/src/main/java/com/arsdigita/toolbox/ui/DomainObjectPropertySheet.java @@ -28,10 +28,19 @@ import com.arsdigita.ui.CcmObjectSelectionModel; import com.arsdigita.util.LockableImpl; import org.libreccm.core.CcmObject; +import org.libreccm.core.UnexpectedErrorException; +import java.beans.BeanInfo; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Arrays; import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import java.util.Optional; import java.util.StringTokenizer; /** @@ -232,7 +241,7 @@ public class DomainObjectPropertySheet extends PropertySheet { * @return A String representation of the retrieved attribute of the * domain object. */ - String format(CcmObject obj, String attribute, PageState state); + String format(Object obj, String attribute, PageState state); } @@ -397,7 +406,6 @@ public class DomainObjectPropertySheet extends PropertySheet { * formatted to a String by the default format method. */ private static abstract class DefaultAttributeFormatter - extends DomainService implements AttributeFormatter { private GlobalizedMessage m_default; @@ -431,7 +439,7 @@ public class DomainObjectPropertySheet extends PropertySheet { * A simple attribute formatter that calls get on the object with the * specified attribute. */ - private static class SimpleAttributeFormatter + private class SimpleAttributeFormatter extends DefaultAttributeFormatter { /** @@ -461,8 +469,9 @@ public class DomainObjectPropertySheet extends PropertySheet { * * @return */ - public String format(final CcmObject obj, - final String attribute, + @Override + public String format(final Object obj, + final String attribute, final PageState state) { /* Determine the default value */ @@ -471,12 +480,13 @@ public class DomainObjectPropertySheet extends PropertySheet { if (obj == null) { return (String) defaultMsg.localize(); } - Object value = get(obj, attribute); - if (value == null) { - return (String) defaultMsg.localize(); + final Optional value = getPropertyValue(obj, attribute); + + if (value.isPresent()) { + return value.get().toString(); } else { - return value.toString(); + return (String) defaultMsg.localize(); } } @@ -488,8 +498,7 @@ public class DomainObjectPropertySheet extends PropertySheet { * says "foo.bar.baz", the formatter will attempt to call * obj.get("foo").get("bar").get("baz"); */ - private static class RecursiveAttributeFormatter - extends DefaultAttributeFormatter { + private class RecursiveAttributeFormatter extends DefaultAttributeFormatter { /** * Constructor, simply calls the super class. Uses a default value for @@ -518,33 +527,71 @@ public class DomainObjectPropertySheet extends PropertySheet { * * @return */ - public String format(DomainObject obj, String attribute, PageState state) { + @Override + public String format(final Object obj, + final String attribute, + final PageState state) { if (obj == null) { return (String) getDefaultValue().localize(); } - StringTokenizer tokenizer = new StringTokenizer(attribute, "."); + final StringTokenizer tokenizer + = new StringTokenizer(attribute, "."); String token = null; - Object value = getDataObject(obj); + Object currentObject = obj; while (tokenizer.hasMoreTokens()) { token = tokenizer.nextToken(); // Null check - value = ((DataObject) value).get(token); - if (value == null) { + currentObject = getPropertyValue(currentObject, token); + if (currentObject == null) { return (String) getDefaultValue().localize(); } } // Extract leaf value - if (token == null || value == null) { + if (token == null) { return (String) getDefaultValue().localize(); } - return value.toString(); + return currentObject.toString(); } } + private Optional getPropertyValue(final Object obj, + final String property) { + + final BeanInfo beanInfo; + try { + beanInfo = Introspector.getBeanInfo(obj.getClass()); + } catch (IntrospectionException ex) { + throw new UnexpectedErrorException(ex); + } + + final Optional propertyDescriptor = Arrays + .stream(beanInfo.getPropertyDescriptors()) + .filter(current -> property.equals(current.getName())) + .findAny(); + + if (propertyDescriptor.isPresent()) { + + final Method readMethod = propertyDescriptor + .get() + .getReadMethod(); + + final Object value; + try { + value = readMethod.invoke(obj); + } catch (IllegalAccessException | InvocationTargetException ex) { + throw new UnexpectedErrorException(ex); + } + return Optional.of(value); + + } else { + return Optional.empty(); + } + } + }