CCM NG/ccm-cms: NewsItemPropertiesStep and NewsItemProperty from old system

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4757 8810af33-2d31-482b-a856-94f89814c4df

Former-commit-id: 3447dbe9e2
pull/2/head
jensp 2017-06-06 09:19:09 +00:00
parent 8d2db36b11
commit eb3de37948
6 changed files with 500 additions and 9 deletions

View File

@ -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.contenttypes.ui;
import com.arsdigita.bebop.Component;
import com.arsdigita.bebop.PageState;
import com.arsdigita.bebop.parameters.StringParameter;
import com.arsdigita.cms.ItemSelectionModel;
import org.librecms.contenttypes.News;
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.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.text.DateFormat;
import java.util.Arrays;
import java.util.Optional;
/**
* Authoring step to edit the simple attributes of the News content type (and
* its subclasses). The attributes edited are {@code name}, {@code title},
* {@code lead} and {@code item date}. This authoring step replaces the
* {@link com.arsdigita.ui.authoring.PageEdit} step for this type.
*
* @see com.arsdigita.cms.contenttypes.NewsItem
*
*/
public class NewsItemPropertiesStep extends SimpleEditStep {
/**
* The name of the editing sheet added to this step
*/
public static String EDIT_SHEET_NAME = "edit";
public NewsItemPropertiesStep(final ItemSelectionModel itemModel,
final AuthoringKitWizard parent,
final StringParameter selectedLanguageParam) {
super(itemModel, parent, selectedLanguageParam);
setDefaultEditKey(EDIT_SHEET_NAME);
BasicPageForm editSheet;
editSheet = new NewsItemPropertyForm(itemModel, this);
add(EDIT_SHEET_NAME,
new GlobalizedMessage("cms.ui.edit", CmsConstants.CMS_BUNDLE),
new WorkflowLockedComponentAccess(editSheet, itemModel),
editSheet.getSaveCancelSection().getCancelButton());
setDisplayComponent(getNewsDomainObjectPropertySheet(itemModel));
}
/**
* Returns a component that displays the properties of the NewsItem
* 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
* item
*
*/
public static Component getNewsDomainObjectPropertySheet(
ItemSelectionModel itemModel) {
final 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");
sheet.add(new GlobalizedMessage("cms.contenttypes.ui.newsitem.lead",
CmsConstants.CMS_BUNDLE),
"lead");
// Show news item on homepage?
sheet.add(new GlobalizedMessage(
"cms.contenttypes.ui.newsitem.news_date",
CmsConstants.CMS_BUNDLE),
"releaseDate",
new NewsItemDateAttributeFormatter());
return sheet;
}
/**
* Private class which implements an AttributeFormatter interface for
* NewsItem's date values. Its format(...) class returns a string
* representation for either a false or a true value.
*/
private static class NewsItemDateAttributeFormatter
implements DomainObjectPropertySheet.AttributeFormatter {
/**
* Constructor, does nothing.
*/
public NewsItemDateAttributeFormatter() {
}
/**
* Formatter for the value of an attribute.
*
* It currently relays on the prerequisite that the passed in property
* attribute is in fact a date property. No type checking yet!
*
* Note: the format method has to be executed at each page request. Take
* care to properly adjust globalization and localization here!
*
* @param obj Object containing the attribute to format.
* @param attribute Name of the attribute to retrieve and format
* @param state PageState of the request
*
* @return A String representation of the retrieved boolean attribute of
* the domain object.
*/
public String format(final Object obj,
final String attribute,
final PageState state) {
if (obj != null && obj instanceof News) {
final News newsItem = (News) obj;
final BeanInfo beanInfo;
try {
beanInfo = Introspector.getBeanInfo(obj.getClass());
} catch (IntrospectionException ex) {
throw new UnexpectedErrorException(ex);
}
final Optional<PropertyDescriptor> propertyDescriptor = Arrays
.stream(beanInfo.getPropertyDescriptors())
.filter(current -> attribute.equals(current.getName()))
.findAny();
if (propertyDescriptor.isPresent()) {
final GlobalizationHelper globalizationHelper = CdiUtil
.createCdiUtil().findBean(GlobalizationHelper.class);
final Method readMethod = propertyDescriptor
.get()
.getReadMethod();
final Object result;
try {
result = readMethod.invoke(obj);
} catch (IllegalAccessException
| IllegalArgumentException
| InvocationTargetException ex) {
throw new UnexpectedErrorException(ex);
}
return DateFormat
.getDateInstance(
DateFormat.LONG,
globalizationHelper.getNegotiatedLocale())
.format(result);
} else {
return (String) new GlobalizedMessage(
"cms.ui.unknown",
CmsConstants.CMS_BUNDLE)
.localize();
}
} else {
return (String) new GlobalizedMessage("cms.ui.unknown",
CmsConstants.CMS_BUNDLE)
.localize();
}
}
}
}

View File

@ -0,0 +1,217 @@
/*
* 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.Label;
import com.arsdigita.bebop.event.FormInitListener;
import com.arsdigita.bebop.event.FormProcessListener;
import com.arsdigita.bebop.event.FormSectionEvent;
import com.arsdigita.bebop.event.FormSubmissionListener;
import com.arsdigita.bebop.form.Option;
import com.arsdigita.bebop.form.RadioGroup;
import com.arsdigita.bebop.form.TextArea;
import com.arsdigita.bebop.parameters.DateParameter;
import com.arsdigita.bebop.parameters.NotNullValidationListener;
import com.arsdigita.bebop.parameters.ParameterModel;
import com.arsdigita.bebop.parameters.StringParameter;
import com.arsdigita.cms.ItemSelectionModel;
import org.librecms.contenttypes.News;
import com.arsdigita.cms.ui.authoring.BasicPageForm;
import com.arsdigita.globalization.GlobalizedMessage;
import com.arsdigita.kernel.KernelConfig;
import org.libreccm.cdi.utils.CdiUtil;
import org.libreccm.configuration.ConfigurationManager;
import org.librecms.CmsConstants;
import org.librecms.contentsection.ContentItemRepository;
import org.librecms.contenttypes.NewsConfig;
import java.util.Calendar;
import java.util.GregorianCalendar;
/**
* Form to edit the basic properties of a {@link News} item. These are name,
* title, item date and reference code. Used by {@link NewsItemPropertiesStep}
* authoring kit step.
*
* This form can be extended to create forms for NewsItem subclasses.
*
*/
public class NewsItemPropertyForm extends BasicPageForm
implements FormProcessListener, FormInitListener, FormSubmissionListener {
private NewsItemPropertiesStep propertiesStep;
/**
* lead parameter name
*/
public static final String LEAD = "lead";
/**
* Item date parameter name
*/
public static final String NEWS_DATE = "news_date";
public static final String IS_HOMEPAGE = "isHomepage";
/**
* Name of this form
*/
public static final String ID = "news_item_edit";
private com.arsdigita.bebop.form.Date releaseDateSelector;
/**
* Creates a new form to edit the NewsItem object specified by the item
* selection model passed in.
*
* @param itemSelectionModel The ItemSelectionModel to use to obtain the
* NewsItem to work on
*/
public NewsItemPropertyForm(final ItemSelectionModel itemSelectionModel) {
this(itemSelectionModel, null);
}
/**
* Creates a new form to edit the NewsItem object specified by the item
* selection model passed in.
*
* @param itemSelectionModel The ItemSelectionModel to use to obtain the
* NewsItem to work on
* @param propertiesStep The NewsItemPropertiesStep which controls this
* form.
*/
public NewsItemPropertyForm(final ItemSelectionModel itemSelectionModel,
final NewsItemPropertiesStep propertiesStep) {
super(ID, itemSelectionModel);
this.propertiesStep = propertiesStep;
addSubmissionListener(this);
}
/**
* Adds widgets to the form.
*/
@Override
protected void addWidgets() {
super.addWidgets();
final ParameterModel leadParam = new StringParameter(LEAD);
final TextArea lead = new TextArea(leadParam);
lead.setLabel(new GlobalizedMessage(
"cms.contenttypes.ui.newsitem.lead",
CmsConstants.CMS_BUNDLE));
lead.setCols(50);
lead.setRows(5);
add(lead);
final ConfigurationManager confManager = CdiUtil
.createCdiUtil()
.findBean(ConfigurationManager.class);
final NewsConfig newsConfig = confManager
.findConfiguration(NewsConfig.class);
final int startYear = newsConfig.getStartYear();
final int endYearDelta = newsConfig.getEndYearDelta();
final int currentYear = GregorianCalendar
.getInstance()
.get(Calendar.YEAR);
final int endYear = currentYear + endYearDelta;
final ParameterModel newsDateParam = new DateParameter(NEWS_DATE);
newsDateParam.addParameterListener(new NotNullValidationListener());
releaseDateSelector = new com.arsdigita.bebop.form.Date(newsDateParam);
releaseDateSelector.setYearRange(startYear, endYear);
releaseDateSelector.setLabel(new GlobalizedMessage(
"cms.contenttypes.ui.newsitem.date",
CmsConstants.CMS_BUNDLE));
add(releaseDateSelector);
}
/**
* Form initialisation hook. Fills widgets with data.
*
* @param event
*/
@Override
public void init(final FormSectionEvent event) {
final FormData data = event.getFormData();
final News item = (News) super.initBasicWidgets(event);
// set a default item date, if none set
java.util.Date releaseDate = item.getReleaseDate();
if (releaseDate == null) {
// new Date is initialised to current time
releaseDate = new java.util.Date();
}
releaseDateSelector.addYear(releaseDate);
data.put(NEWS_DATE, releaseDate);
data.put(LEAD, item.getDescription());
}
/**
* 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 NewsItem object.
*
* @param event
*/
@Override
public void process(final FormSectionEvent event) {
FormData data = event.getFormData();
final News item = (News) super.processBasicWidgets(event);
// save only if save button was newsed
if (item != null
&& getSaveCancelSection()
.getSaveButton()
.isSelected(event.getPageState())) {
item.setReleaseDate((java.util.Date) data.get(NEWS_DATE));
item.getDescription().addValue(
KernelConfig.getConfig().getDefaultLocale(),
(String) data.get(LEAD));
final ContentItemRepository itemRepo = CdiUtil
.createCdiUtil()
.findBean(ContentItemRepository.class);
itemRepo.save(item);
}
if (propertiesStep != null) {
propertiesStep.maybeForwardToNextStep(event.getPageState());
}
}
}

View File

@ -99,12 +99,14 @@ public class SimpleEditStep extends SecurityPropertyEditor
/**
* Construct a new SimpleEditStep component
*
* @param itemModel The {@link ItemSelectionModel} which will be responsible
* for loading the current item
* @param itemModel The {@link ItemSelectionModel} which will be
* responsible for loading the current item
*
* @param parent The parent wizard which contains the form. The component
* may use the wizard's methods, such as stepForward and
* stepBack, in its process listener.
* @param parent The parent wizard which contains the form.
* The component may use the wizard's methods,
* such as stepForward and stepBack, in its
* process listener.
* @param selectedLanguageParam
*/
public SimpleEditStep(final ItemSelectionModel itemModel,
final AuthoringKitWizard parent,

View File

@ -19,7 +19,6 @@
package org.arsdigita.cms;
import com.arsdigita.bebop.form.DHTMLEditor;
import com.arsdigita.util.UncheckedWrapperException;
import org.libreccm.cdi.utils.CdiUtil;
import org.libreccm.configuration.Configuration;

View File

@ -0,0 +1,63 @@
/*
* 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 org.librecms.contenttypes;
import org.libreccm.configuration.Configuration;
import org.libreccm.configuration.Setting;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@Configuration
public class NewsConfig {
/**
* First year shown in date selector for the release date of a {@link News}.
*/
@Setting
private int startYear = 2010;
/**
* Delta to be added to the current year to get the last year shown
* in the date selector for a {@link News}.
*/
@Setting
private int endYearDelta = 10;
public int getStartYear() {
return startYear;
}
public void setStartYear(final int startYear) {
this.startYear = startYear;
}
public int getEndYearDelta() {
return endYearDelta;
}
public void setEndYearDelta(final int endYearDelta) {
this.endYearDelta = endYearDelta;
}
}