CCM NG/ccm-cms: CreateForm for NewsItem

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4697 8810af33-2d31-482b-a856-94f89814c4df
pull/2/head
jensp 2017-05-01 15:33:07 +00:00
parent fc3471d972
commit f1aa5bc54d
8 changed files with 162 additions and 19 deletions

View File

@ -43,6 +43,7 @@ import com.arsdigita.util.Assert;
import org.arsdigita.cms.CMSConfig;
import org.libreccm.cdi.utils.CdiUtil;
import org.librecms.CmsConstants;
import org.librecms.contentsection.ContentItemInitializer;
import org.librecms.contentsection.ContentItemManager;
import org.librecms.contentsection.ContentType;
@ -240,16 +241,18 @@ public abstract class BasicPageForm extends BasicItemForm {
* @param name
* @param section
* @param folder
* @param initializer
*
* @return the new content item (or a proper subclass thereof)
*
* @throws com.arsdigita.bebop.FormProcessException
*/
public ContentItem createContentPage(final PageState state,
public ContentItem createContentPage(
final PageState state,
final String name,
final ContentSection section,
final Folder folder)
throws FormProcessException {
final Folder folder,
final ContentItemInitializer initializer) throws FormProcessException {
final ItemSelectionModel selectionModel = getItemSelectionModel();
final ContentType contentType = selectionModel.getContentType();
@ -265,7 +268,11 @@ public abstract class BasicPageForm extends BasicItemForm {
final Class<? extends ContentItem> clazz
= (Class<? extends ContentItem>) Class
.forName(contentType.getContentItemClass());
item = itemManager.createContentItem(name, section, folder, clazz);
item = itemManager.createContentItem(name,
section,
folder,
clazz,
initializer);
} catch (ClassNotFoundException ex) {
throw new FormProcessException(
"Couldn't create contentpage",
@ -276,7 +283,6 @@ public abstract class BasicPageForm extends BasicItemForm {
}
// Create new item
// Make sure the item will be remembered across requests
selectionModel.setSelectedKey(state, item.getObjectId());

View File

@ -23,7 +23,6 @@ import com.arsdigita.bebop.FormData;
import com.arsdigita.bebop.FormProcessException;
import com.arsdigita.bebop.Label;
import com.arsdigita.bebop.PageState;
import com.arsdigita.bebop.Text;
import com.arsdigita.bebop.event.FormSectionEvent;
import com.arsdigita.bebop.event.FormSubmissionListener;
import com.arsdigita.bebop.event.PrintEvent;
@ -35,13 +34,13 @@ import org.librecms.contentsection.Folder;
import com.arsdigita.cms.ItemSelectionModel;
import com.arsdigita.globalization.GlobalizedMessage;
import com.arsdigita.kernel.KernelConfig;
import com.arsdigita.util.Assert;
import org.libreccm.cdi.utils.CdiUtil;
import org.libreccm.l10n.GlobalizationHelper;
import org.librecms.CmsConstants;
import org.librecms.contentsection.ContentItem;
import org.librecms.contentsection.ContentItemInitializer;
import org.librecms.contenttypes.ContentTypeInfo;
import org.librecms.contenttypes.ContentTypesManager;
@ -120,7 +119,8 @@ public class PageCreateForm
/* content type */
add(new Label(new GlobalizedMessage("cms.ui.authoring.content_type",
CmsConstants.CMS_BUNDLE)));
final Label typeOutput = new Label(new ContentTypePrintListener(typeInfo));
final Label typeOutput = new Label(
new ContentTypePrintListener(typeInfo));
add(typeOutput);
/* language selection */
add(new Label(new GlobalizedMessage("cms.ui.language.field",
@ -205,7 +205,7 @@ public class PageCreateForm
* @throws FormProcessException
*/
@Override
public void process(final FormSectionEvent event)
public final void process(final FormSectionEvent event)
throws FormProcessException {
final FormData data = event.getFormData();
@ -218,7 +218,9 @@ public class PageCreateForm
final ContentItem item = createContentPage(state,
(String) data.get(NAME),
section,
folder);
folder,
getItemInitializer(data,
state));
final Locale locale = new Locale((String) data.get(LANGUAGE));
item.getName().addValue(locale, (String) data.get(NAME));
item.getTitle().addValue(locale, (String) data.get(TITLE));
@ -228,6 +230,12 @@ public class PageCreateForm
creationSelector.editItem(state, item);
}
protected ContentItemInitializer<?> getItemInitializer(
final FormData data, final PageState state) {
return item -> {};
}
private class ContentTypePrintListener implements PrintListener {
private final ContentTypeInfo typeInfo;

View File

@ -0,0 +1,36 @@
/*
* 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.contentsection;
/**
*
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
* @param <T>
*/
public interface ContentItemInitializer<T extends ContentItem> {
/**
* Initialise the provided item.
*
* @param item The item to initialise.
*/
void initializeValues(T item);
}

View File

@ -123,7 +123,7 @@ public class ContentItemManager {
/**
* Creates a new content item in the provided content section and folder
* with the workflow.
* with the default workflow for the content type of the item.
*
* The folder must be a subfolder of the
* {@link ContentSection#rootDocumentsFolder} of the provided content
@ -146,6 +146,43 @@ public class ContentItemManager {
final Folder folder,
final Class<T> type) {
return createContentItem(name,
section,
folder,
type,
item -> {
});
}
/**
* Creates a new content item in the provided content section and folder
* with the default workflow for the content type of the item.
*
* The folder must be a subfolder of the
* {@link ContentSection#rootDocumentsFolder} of the provided content
* section. Otherwise an {@link IllegalArgumentException} is thrown.
*
* @param <T> The type of the content item.
* @param name The name (URL stub) of the new content item.
* @param section The content section in which the item is generated.
* @param folder The folder in which in the item is stored.
* @param type The type of the new content item.
* @param initalizer A {@link ContentItemInitializer} for setting mandatory
* values
*
* @return The new content item.
*/
@AuthorizationRequired
@Transactional(Transactional.TxType.REQUIRED)
public <T extends ContentItem> T createContentItem(
final String name,
final ContentSection section,
@RequiresPrivilege(ItemPrivileges.CREATE_NEW)
final Folder folder,
final Class<T> type,
final ContentItemInitializer<T> initalizer) {
final Optional<ContentType> contentType = typeRepo
.findByContentSectionAndClass(section, type);
@ -160,7 +197,8 @@ public class ContentItemManager {
section,
folder,
contentType.get().getDefaultWorkflow(),
type);
type,
initalizer);
}
/**
@ -196,6 +234,52 @@ public class ContentItemManager {
final WorkflowTemplate workflowTemplate,
final Class<T> type) {
return createContentItem(name,
section,
folder,
workflowTemplate,
type,
item -> {
});
}
/**
* Creates a new content item in the provided content section and folder
* with specific workflow.
*
* The folder must be a subfolder of the
* {@link ContentSection#rootDocumentsFolder} of the provided content
* section. Otherwise an {@link IllegalArgumentException} is thrown.
*
* Likewise the provided {@link WorkflowTemplate} must be defined in the
* provided content section. Otherwise an {@link IllegalArgumentException}
* is thrown.
*
* @param <T> The type of the content item.
* @param name The name (URL stub) of the new content item.
* @param section The content section in which the item is
* generated.
* @param folder The folder in which in the item is stored.
* @param workflowTemplate The template for the workflow to apply to the new
* item.
* @param type The type of the new content item.
* @param initializer Initialiser implementation for setting mandatory
* properties of the new item.
*
* @return The new content item.
*/
@AuthorizationRequired
@Transactional(Transactional.TxType.REQUIRED)
public <T extends ContentItem> T createContentItem(
final String name,
final ContentSection section,
@RequiresPrivilege(ItemPrivileges.CREATE_NEW)
final Folder folder,
final WorkflowTemplate workflowTemplate,
final Class<T> type,
final ContentItemInitializer<T> initializer) {
final Optional<ContentType> contentType = typeRepo
.findByContentSectionAndClass(section, type);
@ -242,6 +326,10 @@ public class ContentItemManager {
item.setWorkflow(workflow);
}
if (initializer != null) {
initializer.initializeValues(item);
}
contentItemRepo.save(item);
categoryManager.addObjectToCategory(

View File

@ -18,7 +18,8 @@
*/
package org.librecms.contenttypes;
import com.arsdigita.cms.ui.authoring.PageCreateForm;
import com.arsdigita.cms.ui.contenttypes.NewsCreateForm;
import org.hibernate.envers.Audited;
import org.hibernate.validator.constraints.NotEmpty;
import org.libreccm.l10n.LocalizedString;
@ -37,6 +38,7 @@ import javax.persistence.JoinTable;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import static org.librecms.CmsConstants.*;
@ -49,7 +51,7 @@ import static org.librecms.CmsConstants.*;
@Table(name = "NEWS", schema = DB_SCHEMA)
@ContentTypeDescription(labelBundle = "org.librecms.contenttypes.News",
descriptionBundle = "org.librecms.contenttypes.News")
@AuthoringKit(createComponent = PageCreateForm.class,
@AuthoringKit(createComponent = NewsCreateForm.class,
steps = {})
public class News extends ContentItem implements Serializable {
@ -72,7 +74,7 @@ public class News extends ContentItem implements Serializable {
* Release date of the news
*/
@Column(name = "NEWS_DATE", nullable = false)
@NotEmpty
@NotNull
@Temporal(TemporalType.DATE)
private Date releaseDate;

View File

@ -279,3 +279,4 @@ cms.ui.language.field=Sprache
cms.ui.authoring.workflow=Select a workflow
cms.ui.create=Create
cms.contenttypes.ui.summary=Summary
cms.contenttypes.ui.newsitem.date\ =Release date

View File

@ -277,3 +277,4 @@ cms.ui.language.field=Sprache
cms.ui.authoring.workflow=Arbeitsablauf ausw\u00e4hlen
cms.ui.create=Anlegen
cms.contenttypes.ui.summary=Zusammenfassung
cms.contenttypes.ui.newsitem.date\ =Erscheinungsdatum

View File

@ -236,3 +236,4 @@ cms.ui.language.field=Sprache
cms.ui.authoring.workflow=Select a workflow
cms.ui.create=Create
cms.contenttypes.ui.summary=Summary
cms.contenttypes.ui.newsitem.date\ =Release date