From 64e9b6097b6e2a787239cdfa9fe32c791c9b2dd0 Mon Sep 17 00:00:00 2001 From: jensp Date: Mon, 24 Jul 2017 15:22:10 +0000 Subject: [PATCH] CCM NG/ccm-cms: - Privileges from the folder are now applied to new content items - Privilieges from the folder are not applied to new assets - Some improvments for the structure of the asset forms git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4883 8810af33-2d31-482b-a856-94f89814c4df --- .../src/main/resources/log4j2.xml | 3 + ...{AssetForm.java => AbstractAssetForm.java} | 139 +++--- .../cms/ui/assets/AssetFormController.java | 4 +- .../arsdigita/cms/ui/assets/AssetPane.java | 2 +- ...Form.java => AbstractBinaryAssetForm.java} | 52 +-- .../ui/assets/forms/AbstractBookmarkForm.java | 171 ++++++++ .../cms/ui/assets/forms/AudioForm.java | 43 +- .../cms/ui/assets/forms/BookmarkForm.java | 275 ++++++------ .../assets/forms/ExternalAudioAssetForm.java | 44 +- .../assets/forms/ExternalVideoAssetForm.java | 37 +- .../cms/ui/assets/forms/FileAssetForm.java | 11 +- .../cms/ui/assets/forms/ImageForm.java | 58 ++- .../ui/assets/forms/LegalMetadataForm.java | 56 +-- .../cms/ui/assets/forms/SideNoteForm.java | 53 ++- .../cms/ui/assets/forms/VideoForm.java | 50 ++- .../java/org/librecms/assets/AssetType.java | 4 +- .../org/librecms/assets/AssetTypeInfo.java | 8 +- .../org/librecms/contentsection/Asset.java | 19 + .../librecms/contentsection/AssetManager.java | 49 ++- .../contentsection/AssetRepository.java | 90 ++++ .../librecms/contentsection/ContentItem.java | 6 +- .../contentsection/ContentItemManager.java | 96 ++-- .../contentsection/ContentItemRepository.java | 2 +- .../org/librecms/contentsection/PathUtil.java | 7 +- .../contentsection/rs/ContentSections.java | 1 + .../librecms/contentsection/rs/Images.java | 110 +++++ .../ContentItemManagerTest.java | 6 + ...after-create-contentitem-with-workflow.xml | 409 +++++++++++++++--- .../after-create-contentitem.xml | 409 +++++++++++++++--- .../ContentItemManagerTest/data.xml | 204 ++++++--- .../libreccm/security/PermissionManager.java | 53 ++- 31 files changed, 1822 insertions(+), 649 deletions(-) rename ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/{AssetForm.java => AbstractAssetForm.java} (76%) rename ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/{BinaryAssetForm.java => AbstractBinaryAssetForm.java} (87%) create mode 100644 ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/AbstractBookmarkForm.java create mode 100644 ccm-cms/src/main/java/org/librecms/contentsection/rs/Images.java diff --git a/ccm-bundle-devel-wildfly-web/src/main/resources/log4j2.xml b/ccm-bundle-devel-wildfly-web/src/main/resources/log4j2.xml index 849d8d913..22a97f7e2 100644 --- a/ccm-bundle-devel-wildfly-web/src/main/resources/log4j2.xml +++ b/ccm-bundle-devel-wildfly-web/src/main/resources/log4j2.xml @@ -77,6 +77,9 @@ + + diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AssetForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AbstractAssetForm.java similarity index 76% rename from ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AssetForm.java rename to ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AbstractAssetForm.java index ca802d1c2..9c79e61b0 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AssetForm.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AbstractAssetForm.java @@ -47,25 +47,29 @@ import org.librecms.contentsection.AssetRepository; import java.util.Optional; -import org.libreccm.categorization.CategoryManager; import org.libreccm.core.UnexpectedErrorException; import org.librecms.assets.AssetL10NManager; -import org.librecms.contentsection.Folder; +import org.librecms.contentsection.AssetManager; import java.util.ArrayList; import java.util.List; import java.util.Locale; +import java.util.Objects; import java.util.TooManyListenersException; /** + * Basic Form for manipulating assets. * * @author Jens Pelzetter + * @param The type of the asset. */ -public abstract class AssetForm extends Form implements FormInitListener, - FormProcessListener, - FormSubmissionListener { +public abstract class AbstractAssetForm + extends Form implements FormInitListener, + FormProcessListener, + FormSubmissionListener { - private static final String ASSET_TITLE = "asset-title"; + private static final String ASSET_TITLE = "asset-name"; + private static final String ASSET_NAME = "asset-title"; private final AssetPane assetPane; private final SingleSelectionModel selectionModel; @@ -78,10 +82,11 @@ public abstract class AssetForm extends Form implements FormInitListener, private SingleSelect addLocaleSelect; private Submit addLocaleSubmit; + private TextField name; private TextField title; private SaveCancelSection saveCancelSection; - public AssetForm(final AssetPane assetPane) { + public AbstractAssetForm(final AssetPane assetPane) { super("asset-form", new ColumnPanel(1)); this.assetPane = assetPane; @@ -98,7 +103,7 @@ public abstract class AssetForm extends Form implements FormInitListener, @Override public void prepare(final PrintEvent event) { final PageState state = event.getPageState(); - final Optional selectedAsset = getSelectedAsset(state); + final Optional selectedAsset = getSelectedAsset(state); final Label target = (Label) event.getTarget(); if (selectedAsset.isPresent()) { target.setLabel(new GlobalizedMessage( @@ -121,8 +126,7 @@ public abstract class AssetForm extends Form implements FormInitListener, public void prepare(final PrintEvent event) { final PageState state = event.getPageState(); - final Optional selectedAsset - = getSelectedAsset(state); + final Optional selectedAsset = getSelectedAsset(state); if (selectedAsset.isPresent()) { final SingleSelect target = (SingleSelect) event .getTarget(); @@ -196,8 +200,7 @@ public abstract class AssetForm extends Form implements FormInitListener, public void prepare(final PrintEvent event) { final PageState state = event.getPageState(); - final Optional selectedAsset - = getSelectedAsset(state); + final Optional selectedAsset = getSelectedAsset(state); if (selectedAsset.isPresent()) { final SingleSelect target = (SingleSelect) event .getTarget(); @@ -232,6 +235,11 @@ public abstract class AssetForm extends Form implements FormInitListener, addLocalePanel.add(addLocaleSubmit); add(addLocalePanel); + add(new Label(new GlobalizedMessage("cms.ui.asset.name", + CmsConstants.CMS_BUNDLE))); + name = new TextField(ASSET_NAME); + add(name); + add(new Label(new GlobalizedMessage("cms.ui.asset.title", CmsConstants.CMS_BUNDLE))); title = new TextField(ASSET_TITLE); @@ -255,7 +263,8 @@ public abstract class AssetForm extends Form implements FormInitListener, return (String) title.getValue(state); } - protected Optional getSelectedAsset(final PageState state) { + @SuppressWarnings("unchecked") + protected Optional getSelectedAsset(final PageState state) { if (selectionModel.getSelectedKey(state) == null) { return Optional.empty(); @@ -268,7 +277,17 @@ public abstract class AssetForm extends Form implements FormInitListener, .orElseThrow(() -> new IllegalArgumentException(String. format("No asset with ID %d in the database.", selectionModel.getSelectedKey(state)))); - return Optional.of(asset); + if (getAssetClass().isAssignableFrom(asset.getClass())) { + return Optional.of((T) asset); + } else { + throw new UnexpectedErrorException(String + .format( + "The requested asset is not of the type expected by " + + "this form. Expected type is \"%s\". " + + "Type of asset: \"%s\".", + getAssetClass().getName(), + asset.getClass().getName())); + } } } @@ -277,7 +296,7 @@ public abstract class AssetForm extends Form implements FormInitListener, final PageState state = event.getPageState(); - final Optional selectedAsset = getSelectedAsset(state); + final Optional selectedAsset = getSelectedAsset(state); if (selectedAsset.isPresent()) { @@ -312,8 +331,12 @@ public abstract class AssetForm extends Form implements FormInitListener, } } + protected String getTitleValue(final PageState state) { + return (String) title.getValue(state); + } + protected void initForm(final PageState state, - final Optional selectedAsset) { + final Optional selectedAsset) { if (selectedAsset.isPresent()) { @@ -336,18 +359,9 @@ public abstract class AssetForm extends Form implements FormInitListener, if (showLocaleSubmit.isSelected(state)) { - final Optional selectedAsset = getSelectedAsset(state); + final Optional selectedAsset = getSelectedAsset(state); initForm(state, selectedAsset); -// if (selectedAsset.isPresent()) { -// -// title.setValue(state, -// selectedAsset -// .get() -// .getTitle() -// .getValue(getSelectedLocale(state))); -// showLocale(state); -// } return; } @@ -356,52 +370,77 @@ public abstract class AssetForm extends Form implements FormInitListener, .findBean(AssetL10NManager.class); final Locale add = new Locale((String) addLocaleSelect .getValue(state)); - final Optional selectedAsset = getSelectedAsset(state); + final Optional selectedAsset = getSelectedAsset(state); l10nManager.addLanguage(selectedAsset.get(), add); } if (saveCancelSection.getSaveButton().isSelected(state)) { - final Optional selectedAsset = getSelectedAsset(state); + final Optional selectedAsset = getSelectedAsset(state); final Asset asset; if (selectedAsset.isPresent()) { asset = selectedAsset.get(); - updateAsset(asset, event); +// updateAsset(asset, event); + + asset.setDisplayName((String) name.getValue(state)); + asset.getTitle().addValue(getSelectedLocale(state), + (String) title.getValue(state)); + +// final AssetRepository assetRepo = cdiUtil +// .findBean(AssetRepository.class); +// assetRepo.save(asset); } else { asset = createAsset(event); + updateAsset(asset, event); } - asset.getTitle().addValue(getSelectedLocale(state), - (String) title.getValue(state)); + updateAsset(asset, event); final AssetRepository assetRepo = cdiUtil .findBean(AssetRepository.class); assetRepo.save(asset); - if (!selectedAsset.isPresent()) { - //Set display name - asset.setDisplayName((String) title.getValue(state)); - assetRepo.save(asset); - - //Add new asset to currently selected folder - final Folder selectedFolder = assetPane - .getFolderSelectionModel() - .getSelectedObject(state); - final CategoryManager categoryManager = cdiUtil - .findBean(CategoryManager.class); - categoryManager.addObjectToCategory( - asset, - selectedFolder, - CmsConstants.CATEGORIZATION_TYPE_FOLDER); - } - +// if (!selectedAsset.isPresent()) { +// //Set display name +// asset.setDisplayName((String) title.getValue(state)); +// assetRepo.save(asset); +// +// //Add new asset to currently selected folder +// final Folder selectedFolder = assetPane +// .getFolderSelectionModel() +// .getSelectedObject(state); +// final CategoryManager categoryManager = cdiUtil +// .findBean(CategoryManager.class); +// categoryManager.addObjectToCategory( +// asset, +// selectedFolder, +// CmsConstants.CATEGORIZATION_TYPE_FOLDER); +// } assetPane.browseMode(state); } } + protected abstract Class getAssetClass(); + protected abstract void showLocale(final PageState state); - protected abstract Asset createAsset(final FormSectionEvent event) - throws FormProcessException; + protected final T createAsset(final FormSectionEvent event) + throws FormProcessException { + + Objects.requireNonNull(event); + + final PageState state = event.getPageState(); + + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final AssetManager assetManager = cdiUtil.findBean(AssetManager.class); + + return assetManager.createAsset((String) name.getValue(state), + (String) title.getValue(state), + getSelectedLocale(state), + assetPane + .getFolderSelectionModel() + .getSelectedObject(state), + getAssetClass()); + } protected abstract void updateAsset(final Asset asset, final FormSectionEvent event) diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AssetFormController.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AssetFormController.java index 268258d11..6ddf9cf85 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AssetFormController.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AssetFormController.java @@ -22,10 +22,12 @@ import javax.enterprise.context.RequestScoped; /** * + * Controller for the asset forms + * * @author Jens Pelzetter */ @RequestScoped -public class AssetFormController { +class AssetFormController { diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AssetPane.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AssetPane.java index a3d3eb8e5..7812edefd 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AssetPane.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/AssetPane.java @@ -536,7 +536,7 @@ public class AssetPane extends LayoutPanel implements Resettable { .findBean(AssetTypesManager.class); final AssetTypeInfo typeInfo = typesManager .getAssetTypeInfo(selectedAssetType); - final Class assetForm = typeInfo + final Class assetForm = typeInfo .getAssetForm(); try { return assetForm diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/BinaryAssetForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/AbstractBinaryAssetForm.java similarity index 87% rename from ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/BinaryAssetForm.java rename to ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/AbstractBinaryAssetForm.java index eddbeb513..69df27c11 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/BinaryAssetForm.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/AbstractBinaryAssetForm.java @@ -26,7 +26,7 @@ import com.arsdigita.bebop.Text; import com.arsdigita.bebop.event.FormSectionEvent; import com.arsdigita.bebop.form.TextArea; import com.arsdigita.cms.ui.FileUploadSection; -import com.arsdigita.cms.ui.assets.AssetForm; +import com.arsdigita.cms.ui.assets.AbstractAssetForm; import com.arsdigita.cms.ui.assets.AssetPane; import com.arsdigita.globalization.GlobalizedMessage; @@ -45,8 +45,10 @@ import org.librecms.contentsection.Asset; * Base form for assets which extend {@link BinaryAsset}. * * @author Jens Pelzetter + * @param Type of binary asset */ -public abstract class BinaryAssetForm extends AssetForm { +public abstract class AbstractBinaryAssetForm + extends AbstractAssetForm { private TextArea description; private Text fileName; @@ -54,7 +56,7 @@ public abstract class BinaryAssetForm extends AssetForm { private Text size; private FileUploadSection fileUpload; - public BinaryAssetForm(final AssetPane assetPane) { + public AbstractBinaryAssetForm(final AssetPane assetPane) { super(assetPane); } @@ -101,7 +103,7 @@ public abstract class BinaryAssetForm extends AssetForm { @Override protected void initForm(final PageState state, - final Optional selectedAsset) { + final Optional selectedAsset) { super.initForm(state, selectedAsset); @@ -140,7 +142,7 @@ public abstract class BinaryAssetForm extends AssetForm { @Override protected void showLocale(final PageState state) { - final Optional selectedAsset = getSelectedAsset(state); + final Optional selectedAsset = getSelectedAsset(state); if (selectedAsset.isPresent()) { if (!(getSelectedAsset(state).get() instanceof BinaryAsset)) { @@ -158,27 +160,27 @@ public abstract class BinaryAssetForm extends AssetForm { } - @Override - protected Asset createAsset(final FormSectionEvent event) - throws FormProcessException { +// @Override +// protected Asset createAsset(final FormSectionEvent event) +// throws FormProcessException { +// +// Objects.requireNonNull(event); +// +// final PageState state = event.getPageState(); +// +// final BinaryAsset binaryAsset = createBinaryAsset(state); +// +// binaryAsset +// .getDescription() +// .addValue(getSelectedLocale(state), +// (String) description.getValue(state)); +// +// setFileData(event, binaryAsset); +// +// return binaryAsset; +// } - Objects.requireNonNull(event); - - final PageState state = event.getPageState(); - - final BinaryAsset binaryAsset = createBinaryAsset(state); - - binaryAsset - .getDescription() - .addValue(getSelectedLocale(state), - (String) description.getValue(state)); - - setFileData(event, binaryAsset); - - return binaryAsset; - } - - protected abstract BinaryAsset createBinaryAsset(final PageState state); +// protected abstract BinaryAsset createBinaryAsset(final PageState state); @Override protected void updateAsset(final Asset asset, diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/AbstractBookmarkForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/AbstractBookmarkForm.java new file mode 100644 index 000000000..d8e8a1cb0 --- /dev/null +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/AbstractBookmarkForm.java @@ -0,0 +1,171 @@ +/* + * 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.assets.forms; + +import com.arsdigita.bebop.FormData; +import com.arsdigita.bebop.FormProcessException; +import com.arsdigita.bebop.Label; +import com.arsdigita.bebop.PageState; +import com.arsdigita.bebop.event.FormSectionEvent; +import com.arsdigita.bebop.event.FormValidationListener; +import com.arsdigita.bebop.form.TextArea; +import com.arsdigita.bebop.form.TextField; +import com.arsdigita.cms.ui.assets.AbstractAssetForm; +import com.arsdigita.cms.ui.assets.AssetPane; +import com.arsdigita.globalization.GlobalizedMessage; + +import org.librecms.CmsConstants; +import org.librecms.assets.Bookmark; +import org.librecms.contentsection.Asset; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Objects; +import java.util.Optional; + +/** + * Abstract base form for all forms for {@link BookmarkAsset}s. + * + * @author Jens Pelzetter + * @param Type of the Bookmark Asset. + */ +public abstract class AbstractBookmarkForm + extends AbstractAssetForm { + + private TextArea description; + private TextField url; + + public AbstractBookmarkForm(final AssetPane assetPane) { + super(assetPane); + } + + @Override + protected void addWidgets() { + + add(new Label( + new GlobalizedMessage("cms.ui.assets.bookmark.description", + CmsConstants.CMS_BUNDLE))); + description = new TextArea("bookmark-description"); + add(description); + + add(new Label(new GlobalizedMessage("cms.ui.assets.bookmark.url", + CmsConstants.CMS_BUNDLE))); + url = new TextField("bookmark-url"); + add(url); + + addValidationListener(new FormValidationListener() { + + @Override + public void validate(final FormSectionEvent event) + throws FormProcessException { + + final PageState state = event.getPageState(); + final FormData data = event.getFormData(); + + try { + new URL((String) url.getValue(state)); + } catch (MalformedURLException ex) { + data.addError(new GlobalizedMessage( + "cms.ui.assets.bookmark.url.malformed", + CmsConstants.CMS_BUNDLE)); + } + } + + }); + + } + + @Override + protected void initForm(final PageState state, + final Optional selectedAsset) { + + if (selectedAsset.isPresent()) { + + if (!(selectedAsset.get() instanceof Bookmark)) { + throw new IllegalArgumentException(String.format( + "The provided asset must be an instanceof of class '%s' or " + + "an subclass but is an instanceof of class '%s'.", + Bookmark.class.getName(), + selectedAsset.get().getClass().getName())); + } + + final Bookmark bookmark = selectedAsset.get(); + + description.setValue(state, + bookmark + .getDescription() + .getValue(getSelectedLocale(state))); + url.setValue(state, bookmark.getUrl()); + + } + } + + @Override + protected void showLocale(final PageState state) { + final Optional selectedAsset = getSelectedAsset(state); + + if (selectedAsset.isPresent()) { + if (!(getSelectedAsset(state).get() instanceof Bookmark)) { + throw new IllegalArgumentException( + "Selected asset is not a bookmark"); + } + + final Bookmark bookmark = selectedAsset.get(); + + description.setValue(state, + bookmark + .getDescription() + .getValue(getSelectedLocale(state))); + } + } + + protected void updateData(final Bookmark bookmark, + final PageState state) { + bookmark + .getDescription() + .addValue(getSelectedLocale(state), + (String) description.getValue(state)); + + bookmark.setUrl((String) url.getValue(state)); + } + + @Override + protected void updateAsset(final Asset asset, + final FormSectionEvent event) + throws FormProcessException { + + Objects.requireNonNull(asset); + Objects.requireNonNull(event); + + final PageState state = event.getPageState(); + + if (!(asset instanceof Bookmark)) { + throw new IllegalArgumentException(String.format( + "Provided asset is not an instance of class (or sub class of) " + + "'%s' but is an instance of class '%s'", + Bookmark.class.getName(), + asset.getClass().getName())); + } + + final Bookmark bookmark = (Bookmark) asset; + + updateData(bookmark, state); + } + +} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/AudioForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/AudioForm.java index a9d750019..ac09ac21f 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/AudioForm.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/AudioForm.java @@ -25,11 +25,11 @@ import com.arsdigita.bebop.event.FormSectionEvent; import com.arsdigita.cms.ui.assets.AssetPane; import com.arsdigita.cms.ui.assets.AssetSearchWidget; import com.arsdigita.globalization.GlobalizedMessage; + import org.libreccm.cdi.utils.CdiUtil; import org.librecms.CmsConstants; import org.librecms.assets.AudioAsset; import org.librecms.assets.BinaryAsset; -import org.librecms.assets.Image; import org.librecms.assets.LegalMetadata; import org.librecms.contentsection.Asset; import org.librecms.contentsection.AssetRepository; @@ -39,8 +39,9 @@ import java.util.Optional; /** * * @author Yannick Bülter + * @author Jens Pelzetter */ -public class AudioForm extends BinaryAssetForm { +public class AudioForm extends AbstractBinaryAssetForm { private AssetSearchWidget assetSearchWidget; @@ -61,13 +62,14 @@ public class AudioForm extends BinaryAssetForm { } @Override - protected void initForm(PageState state, Optional selectedAsset) { + protected void initForm(final PageState state, + final Optional selectedAsset) { super.initForm(state, selectedAsset); if (selectedAsset.isPresent()) { - final AudioAsset audioAsset = (AudioAsset) selectedAsset.get(); + final AudioAsset audioAsset = selectedAsset.get(); final LegalMetadata legalMetadata = audioAsset .getLegalMetadata(); @@ -78,17 +80,22 @@ public class AudioForm extends BinaryAssetForm { } @Override - protected Asset createAsset(final FormSectionEvent event) - throws FormProcessException { - - final AudioAsset audioAsset = (AudioAsset) super.createAsset(event); - - final PageState state = event.getPageState(); - - updateData(audioAsset, state); - - return audioAsset; + protected Class getAssetClass() { + return AudioAsset.class; } + +// @Override +// protected Asset createAsset(final FormSectionEvent event) +// throws FormProcessException { +// +// final AudioAsset audioAsset = (AudioAsset) super.createAsset(event); +// +// final PageState state = event.getPageState(); +// +// updateData(audioAsset, state); +// +// return audioAsset; +// } @Override protected void updateAsset(final Asset asset, @@ -122,8 +129,8 @@ public class AudioForm extends BinaryAssetForm { } } - @Override - protected BinaryAsset createBinaryAsset(final PageState state) { - return new AudioAsset(); - } +// @Override +// protected BinaryAsset createBinaryAsset(final PageState state) { +// return new AudioAsset(); +// } } diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/BookmarkForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/BookmarkForm.java index 31eab23d5..0c6dc2df5 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/BookmarkForm.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/BookmarkForm.java @@ -18,168 +18,159 @@ */ package com.arsdigita.cms.ui.assets.forms; -import com.arsdigita.bebop.FormData; -import com.arsdigita.bebop.FormProcessException; -import com.arsdigita.bebop.Label; -import com.arsdigita.bebop.PageState; -import com.arsdigita.bebop.event.FormSectionEvent; -import com.arsdigita.bebop.event.FormValidationListener; import com.arsdigita.bebop.form.TextArea; import com.arsdigita.bebop.form.TextField; -import com.arsdigita.cms.ui.assets.AssetForm; import com.arsdigita.cms.ui.assets.AssetPane; -import com.arsdigita.globalization.GlobalizedMessage; -import org.librecms.CmsConstants; import org.librecms.assets.Bookmark; -import org.librecms.contentsection.Asset; - -import java.net.MalformedURLException; -import java.net.URL; -import java.util.Objects; -import java.util.Optional; /** * * @author Jens Pelzetter */ -public class BookmarkForm extends AssetForm { +public class BookmarkForm extends AbstractBookmarkForm { - private TextArea description; - private TextField url; +// private TextArea description; +// private TextField url; public BookmarkForm(final AssetPane assetPane) { super(assetPane); } - @Override - protected void addWidgets() { - - add(new Label( - new GlobalizedMessage("cms.ui.assets.bookmark.description", - CmsConstants.CMS_BUNDLE))); - description = new TextArea("bookmark-description"); - add(description); - - add(new Label(new GlobalizedMessage("cms.ui.assets.bookmark.url", - CmsConstants.CMS_BUNDLE))); - url = new TextField("bookmark-url"); - add(url); - - addValidationListener(new FormValidationListener() { - - @Override - public void validate(final FormSectionEvent event) - throws FormProcessException { - - final PageState state = event.getPageState(); - final FormData data = event.getFormData(); - - try { - new URL((String) url.getValue(state)); - } catch (MalformedURLException ex) { - data.addError(new GlobalizedMessage( - "cms.ui.assets.bookmark.url.malformed", - CmsConstants.CMS_BUNDLE)); - } - } - - }); - - } +// @Override +// protected void addWidgets() { +// +// add(new Label( +// new GlobalizedMessage("cms.ui.assets.bookmark.description", +// CmsConstants.CMS_BUNDLE))); +// description = new TextArea("bookmark-description"); +// add(description); +// +// add(new Label(new GlobalizedMessage("cms.ui.assets.bookmark.url", +// CmsConstants.CMS_BUNDLE))); +// url = new TextField("bookmark-url"); +// add(url); +// +// addValidationListener(new FormValidationListener() { +// +// @Override +// public void validate(final FormSectionEvent event) +// throws FormProcessException { +// +// final PageState state = event.getPageState(); +// final FormData data = event.getFormData(); +// +// try { +// new URL((String) url.getValue(state)); +// } catch (MalformedURLException ex) { +// data.addError(new GlobalizedMessage( +// "cms.ui.assets.bookmark.url.malformed", +// CmsConstants.CMS_BUNDLE)); +// } +// } +// +// }); +// +// } +// +// @Override +// protected void initForm(final PageState state, +// final Optional selectedAsset) { +// +// if (selectedAsset.isPresent()) { +// +// if (!(selectedAsset.get() instanceof Bookmark)) { +// throw new IllegalArgumentException(String.format( +// "The provided asset must be an instanceof of class '%s' or " +// + "an subclass but is an instanceof of class '%s'.", +// Bookmark.class.getName(), +// selectedAsset.get().getClass().getName())); +// } +// +// final Bookmark bookmark = selectedAsset.get(); +// +// description.setValue(state, +// bookmark +// .getDescription() +// .getValue(getSelectedLocale(state))); +// url.setValue(state, bookmark.getUrl()); +// +// } +// +// } +// +// @Override +// protected void showLocale(final PageState state) { +// final Optional selectedAsset = getSelectedAsset(state); +// +// if (selectedAsset.isPresent()) { +// if (!(getSelectedAsset(state).get() instanceof Bookmark)) { +// throw new IllegalArgumentException( +// "Selected asset is not a bookmark"); +// } +// +// final Bookmark bookmark = selectedAsset.get(); +// +// description.setValue(state, +// bookmark +// .getDescription() +// .getValue(getSelectedLocale(state))); +// } +// } @Override - protected void initForm(final PageState state, - final Optional selectedAsset) { - - if (selectedAsset.isPresent()) { - - if (!(selectedAsset.get() instanceof Bookmark)) { - throw new IllegalArgumentException(String.format( - "The provided asset must be an instanceof of class '%s' or " - + "an subclass but is an instanceof of class '%s'.", - Bookmark.class.getName(), - selectedAsset.get().getClass().getName())); - } - - final Bookmark bookmark = (Bookmark) selectedAsset.get(); - - description.setValue(state, - bookmark - .getDescription() - .getValue(getSelectedLocale(state))); - url.setValue(state, bookmark.getUrl()); - - } - - } - - @Override - protected void showLocale(final PageState state) { - final Optional selectedAsset = getSelectedAsset(state); - - if (selectedAsset.isPresent()) { - if (!(getSelectedAsset(state).get() instanceof Bookmark)) { - throw new IllegalArgumentException( - "Selected asset is not a bookmark"); - } - - final Bookmark bookmark = (Bookmark) selectedAsset.get(); - - description.setValue(state, - bookmark - .getDescription() - .getValue(getSelectedLocale(state))); - } - } - - @Override - protected Asset createAsset(final FormSectionEvent event) - throws FormProcessException { - - Objects.requireNonNull(event); - - final PageState state = event.getPageState(); - - final Bookmark bookmark = new Bookmark(); - - updateData(bookmark, state); - - return bookmark; - } - - protected void updateData(final Bookmark bookmark, - final PageState state) { - bookmark - .getDescription() - .addValue(getSelectedLocale(state), - (String) description.getValue(state)); - - bookmark.setUrl((String) url.getValue(state)); + @SuppressWarnings("unchecked") + protected Class getAssetClass() { + return Bookmark.class; } - - @Override - protected void updateAsset(final Asset asset, - final FormSectionEvent event) - throws FormProcessException { +// @Override +// protected Asset createAsset(final FormSectionEvent event) +// throws FormProcessException { +// +// Objects.requireNonNull(event); +// +// final PageState state = event.getPageState(); +// +// final Bookmark bookmark = new Bookmark(); +// +// updateData(bookmark, state); +// +// return bookmark; +// } - Objects.requireNonNull(asset); - Objects.requireNonNull(event); - - final PageState state = event.getPageState(); - - if (!(asset instanceof Bookmark)) { - throw new IllegalArgumentException(String.format( - "Provided asset is not an instance of class (or sub class of) " - + "'%s' but is an instance of class '%s'", - Bookmark.class.getName(), - asset.getClass().getName())); - } - - final Bookmark bookmark = (Bookmark) asset; - - updateData(bookmark, state); - } +// protected void updateData(final Bookmark bookmark, +// final PageState state) { +// bookmark +// .getDescription() +// .addValue(getSelectedLocale(state), +// (String) description.getValue(state)); +// +// bookmark.setUrl((String) url.getValue(state)); +// } +// +// +// @Override +// protected void updateAsset(final Asset asset, +// final FormSectionEvent event) +// throws FormProcessException { +// +// Objects.requireNonNull(asset); +// Objects.requireNonNull(event); +// +// final PageState state = event.getPageState(); +// +// if (!(asset instanceof Bookmark)) { +// throw new IllegalArgumentException(String.format( +// "Provided asset is not an instance of class (or sub class of) " +// + "'%s' but is an instance of class '%s'", +// Bookmark.class.getName(), +// asset.getClass().getName())); +// } +// +// final Bookmark bookmark = (Bookmark) asset; +// +// updateData(bookmark, state); +// } } diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/ExternalAudioAssetForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/ExternalAudioAssetForm.java index 45542527b..a90806853 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/ExternalAudioAssetForm.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/ExternalAudioAssetForm.java @@ -18,13 +18,13 @@ */ package com.arsdigita.cms.ui.assets.forms; -import com.arsdigita.bebop.FormProcessException; import com.arsdigita.bebop.Label; import com.arsdigita.bebop.PageState; import com.arsdigita.bebop.event.FormSectionEvent; import com.arsdigita.cms.ui.assets.AssetPane; import com.arsdigita.cms.ui.assets.AssetSearchWidget; import com.arsdigita.globalization.GlobalizedMessage; + import org.libreccm.cdi.utils.CdiUtil; import org.librecms.CmsConstants; import org.librecms.assets.Bookmark; @@ -41,7 +41,8 @@ import java.util.Optional; * @author Jens Pelzetter * @author Yannick Bülter */ -public class ExternalAudioAssetForm extends BookmarkForm { +public class ExternalAudioAssetForm + extends AbstractBookmarkForm { private AssetSearchWidget assetSearchWidget; @@ -63,13 +64,12 @@ public class ExternalAudioAssetForm extends BookmarkForm { @Override protected void initForm(final PageState state, - final Optional selectedAsset) { + final Optional selectedAsset) { super.initForm(state, selectedAsset); if (selectedAsset.isPresent()) { final ExternalAudioAsset externalAudioAsset - = (ExternalAudioAsset) selectedAsset - .get(); + = (ExternalAudioAsset) selectedAsset.get(); final LegalMetadata legalMetadata = externalAudioAsset .getLegalMetadata(); @@ -80,21 +80,25 @@ public class ExternalAudioAssetForm extends BookmarkForm { } @Override - protected Asset createAsset(final FormSectionEvent event) - throws FormProcessException { - - Objects.requireNonNull(event); - - final PageState state = event.getPageState(); - - final ExternalAudioAsset externalAudioAsset = new ExternalAudioAsset(); - - updateData((Bookmark) externalAudioAsset, state); - updateData(externalAudioAsset, state); - - return externalAudioAsset; + protected Class getAssetClass() { + return ExternalAudioAsset.class; } +// @Override +// protected Asset createAsset(final FormSectionEvent event) +// throws FormProcessException { +// +// Objects.requireNonNull(event); +// +// final PageState state = event.getPageState(); +// +// final ExternalAudioAsset externalAudioAsset = new ExternalAudioAsset(); +// +// updateData((Bookmark) externalAudioAsset, state); +// updateData(externalAudioAsset, state); +// +// return externalAudioAsset; +// } protected void updateData(final ExternalAudioAsset externalAudioAsset, final PageState state) { @@ -114,14 +118,14 @@ public class ExternalAudioAssetForm extends BookmarkForm { } @Override - protected void updateAsset(final Asset asset, + protected void updateAsset(final Asset asset, final FormSectionEvent event) { Objects.requireNonNull(asset); Objects.requireNonNull(event); final PageState state = event.getPageState(); - + if (!(asset instanceof ExternalAudioAsset)) { throw new IllegalArgumentException(String.format( "Provided asset is not an instance of class (or sub class of) " diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/ExternalVideoAssetForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/ExternalVideoAssetForm.java index 582fda6dc..8b5f3d26e 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/ExternalVideoAssetForm.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/ExternalVideoAssetForm.java @@ -18,7 +18,6 @@ */ package com.arsdigita.cms.ui.assets.forms; -import com.arsdigita.bebop.FormProcessException; import com.arsdigita.bebop.Label; import com.arsdigita.bebop.PageState; import com.arsdigita.bebop.event.FormSectionEvent; @@ -42,7 +41,8 @@ import org.librecms.contentsection.AssetRepository; * * @author Jens Pelzetter */ -public class ExternalVideoAssetForm extends BookmarkForm { +public class ExternalVideoAssetForm + extends AbstractBookmarkForm { private AssetSearchWidget assetSearchWidget; @@ -64,7 +64,7 @@ public class ExternalVideoAssetForm extends BookmarkForm { @Override protected void initForm(final PageState state, - final Optional selectedAsset) { + final Optional selectedAsset) { super.initForm(state, selectedAsset); if (selectedAsset.isPresent()) { @@ -81,20 +81,25 @@ public class ExternalVideoAssetForm extends BookmarkForm { } @Override - protected Asset createAsset(final FormSectionEvent event) - throws FormProcessException { - - Objects.requireNonNull(event); - - final PageState state = event.getPageState(); - - final ExternalVideoAsset externalVideoAsset = new ExternalVideoAsset(); - - updateData((Bookmark) externalVideoAsset, state); - updateData(externalVideoAsset, state); - - return externalVideoAsset; + protected Class getAssetClass() { + return ExternalVideoAsset.class; } + +// @Override +// protected Asset createAsset(final FormSectionEvent event) +// throws FormProcessException { +// +// Objects.requireNonNull(event); +// +// final PageState state = event.getPageState(); +// +// final ExternalVideoAsset externalVideoAsset = new ExternalVideoAsset(); +// +// updateData((Bookmark) externalVideoAsset, state); +// updateData(externalVideoAsset, state); +// +// return externalVideoAsset; +// } protected void updateData(final ExternalVideoAsset externalVideoAsset, final PageState state) { diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/FileAssetForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/FileAssetForm.java index c0937cb26..a71a7317f 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/FileAssetForm.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/FileAssetForm.java @@ -27,17 +27,22 @@ import org.librecms.assets.FileAsset; * * @author Jens Pelzetter */ -public class FileAssetForm extends BinaryAssetForm { +public class FileAssetForm extends AbstractBinaryAssetForm { public FileAssetForm(final AssetPane assetPane) { super(assetPane); } @Override - protected BinaryAsset createBinaryAsset(final PageState state) { - return new FileAsset(); + protected Class getAssetClass() { + return FileAsset.class; } +// @Override +// protected BinaryAsset createBinaryAsset(final PageState state) { +// return new FileAsset(); +// } + } diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/ImageForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/ImageForm.java index d7f02130e..f6c3d5c3b 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/ImageForm.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/ImageForm.java @@ -40,8 +40,9 @@ import java.util.Optional; /** * * @author Yannick Bülter + * @author Jens Pelzetter */ -public class ImageForm extends BinaryAssetForm { +public class ImageForm extends AbstractBinaryAssetForm { private TextField width; private TextField height; @@ -55,7 +56,7 @@ public class ImageForm extends BinaryAssetForm { protected void addWidgets() { super.addWidgets(); - + width = new TextField("width-text"); height = new TextField("height-text"); assetSearchWidget = new AssetSearchWidget("legal-metadata", @@ -81,14 +82,14 @@ public class ImageForm extends BinaryAssetForm { } @Override - protected void initForm(final PageState state, - final Optional selectedAsset) { + protected void initForm(final PageState state, + final Optional selectedAsset) { super.initForm(state, selectedAsset); if (selectedAsset.isPresent()) { - Image image = (Image) selectedAsset.get(); + final Image image = selectedAsset.get(); width.setValue(state, Long.toString(image.getWidth())); @@ -103,22 +104,35 @@ public class ImageForm extends BinaryAssetForm { } } } - + @Override - protected Asset createAsset(final FormSectionEvent event) - throws FormProcessException { - - final Image image = (Image) super.createAsset(event); - - final PageState state = event.getPageState(); - - image.setHeight(Long.parseLong((String) height.getValue(state))); - image.setWidth(Long.parseLong((String) width.getValue(state))); - updateData(image, state); - - return image; + protected Class getAssetClass() { + return Image.class; } +// @Override +// protected Asset createAsset(final FormSectionEvent event) +// throws FormProcessException { +// +// final Image image = (Image) super.createAsset(event); +// +// final PageState state = event.getPageState(); +// +// image.setDisplayName(getTitleValue(state) +// .toLowerCase() +// .replace(" ", "-")); +// +// if (height.getValue(state) != null) { +// image.setHeight(Long.parseLong((String) height.getValue(state))); +// } +// if (width.getValue(state) != null) { +// image.setWidth(Long.parseLong((String) width.getValue(state))); +// } +// updateData(image, state); +// +// return image; +// } + @Override protected void updateAsset(final Asset asset, final FormSectionEvent event) @@ -154,9 +168,9 @@ public class ImageForm extends BinaryAssetForm { } } - @Override - protected BinaryAsset createBinaryAsset(final PageState state) { - return new Image(); - } +// @Override +// protected BinaryAsset createBinaryAsset(final PageState state) { +// return new Image(); +// } } diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/LegalMetadataForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/LegalMetadataForm.java index 0225a9cfb..2795282a6 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/LegalMetadataForm.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/LegalMetadataForm.java @@ -24,7 +24,7 @@ import com.arsdigita.bebop.Label; import com.arsdigita.bebop.PageState; import com.arsdigita.bebop.event.FormSectionEvent; import com.arsdigita.bebop.form.TextArea; -import com.arsdigita.cms.ui.assets.AssetForm; +import com.arsdigita.cms.ui.assets.AbstractAssetForm; import com.arsdigita.cms.ui.assets.AssetPane; import com.arsdigita.globalization.GlobalizedMessage; @@ -39,7 +39,7 @@ import java.util.Optional; * * @author Jens Pelzetter */ -public class LegalMetadataForm extends AssetForm { +public class LegalMetadataForm extends AbstractAssetForm { private TextArea rightsHolder; private TextArea rights; @@ -84,7 +84,7 @@ public class LegalMetadataForm extends AssetForm { @Override protected void initForm(final PageState state, - final Optional selectedAsset) { + final Optional selectedAsset) { if (selectedAsset.isPresent()) { @@ -96,8 +96,7 @@ public class LegalMetadataForm extends AssetForm { selectedAsset.get().getClass().getName())); } - final LegalMetadata legalMetadata = (LegalMetadata) selectedAsset - .get(); + final LegalMetadata legalMetadata = selectedAsset.get(); rightsHolder.setValue(state, legalMetadata.getRightsHolder()); rights.setValue(state, @@ -112,7 +111,7 @@ public class LegalMetadataForm extends AssetForm { @Override protected void showLocale(final PageState state) { - final Optional selectedAsset = getSelectedAsset(state); + final Optional selectedAsset = getSelectedAsset(state); if (selectedAsset.isPresent()) { if (!(getSelectedAsset(state).get() instanceof LegalMetadata)) { @@ -120,8 +119,7 @@ public class LegalMetadataForm extends AssetForm { "Selected asset is not a legal metadata"); } - final LegalMetadata legalMetadata = (LegalMetadata) selectedAsset. - get(); + final LegalMetadata legalMetadata = selectedAsset.get(); rights.setValue(state, legalMetadata @@ -131,33 +129,37 @@ public class LegalMetadataForm extends AssetForm { } @Override - protected Asset createAsset(final FormSectionEvent event) - throws FormProcessException { - - Objects.requireNonNull(event); - - final PageState state = event.getPageState(); - - final LegalMetadata legalMetadata = new LegalMetadata(); - - legalMetadata.setRightsHolder((String) rightsHolder.getValue(state)); - legalMetadata.getRights().addValue(getSelectedLocale(state), - (String) rights.getValue(state)); - - legalMetadata.setPublisher((String) publisher.getValue(state)); - legalMetadata.setCreator((String) creator.getValue(state)); - - return legalMetadata; + protected Class getAssetClass() { + return LegalMetadata.class; } +// @Override +// protected Asset createAsset(final FormSectionEvent event) +// throws FormProcessException { +// +// Objects.requireNonNull(event); +// +// final PageState state = event.getPageState(); +// +// final LegalMetadata legalMetadata = new LegalMetadata(); +// +// legalMetadata.setRightsHolder((String) rightsHolder.getValue(state)); +// legalMetadata.getRights().addValue(getSelectedLocale(state), +// (String) rights.getValue(state)); +// +// legalMetadata.setPublisher((String) publisher.getValue(state)); +// legalMetadata.setCreator((String) creator.getValue(state)); +// +// return legalMetadata; +// } @Override - protected void updateAsset(final Asset asset, + protected void updateAsset(final Asset asset, final FormSectionEvent event) throws FormProcessException { Objects.requireNonNull(asset); Objects.requireNonNull(event); - + final PageState state = event.getPageState(); if (!(asset instanceof LegalMetadata)) { diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/SideNoteForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/SideNoteForm.java index 19a3aa7bf..7887aa3ed 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/SideNoteForm.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/SideNoteForm.java @@ -23,11 +23,13 @@ import com.arsdigita.bebop.Label; import com.arsdigita.bebop.PageState; import com.arsdigita.bebop.event.FormSectionEvent; import com.arsdigita.bebop.form.TextArea; -import com.arsdigita.cms.ui.assets.AssetForm; +import com.arsdigita.cms.ui.assets.AbstractAssetForm; import com.arsdigita.cms.ui.assets.AssetPane; import com.arsdigita.globalization.GlobalizedMessage; + import java.util.Objects; import java.util.Optional; + import org.librecms.CmsConstants; import org.librecms.assets.SideNote; import org.librecms.contentsection.Asset; @@ -36,7 +38,7 @@ import org.librecms.contentsection.Asset; * * @author Jens Pelzetter */ -public class SideNoteForm extends AssetForm { +public class SideNoteForm extends AbstractAssetForm { private TextArea text; @@ -55,7 +57,7 @@ public class SideNoteForm extends AssetForm { @Override protected void initForm(final PageState state, - final Optional selectedAsset) { + final Optional selectedAsset) { if (selectedAsset.isPresent()) { if (!(selectedAsset.get() instanceof SideNote)) { @@ -66,8 +68,8 @@ public class SideNoteForm extends AssetForm { selectedAsset.get().getClass().getName())); } - final SideNote sideNote = (SideNote) selectedAsset.get(); - + final SideNote sideNote = selectedAsset.get(); + text.setValue(state, sideNote .getText() @@ -79,7 +81,7 @@ public class SideNoteForm extends AssetForm { @Override protected void showLocale(final PageState state) { - final Optional selectedAsset = getSelectedAsset(state); + final Optional selectedAsset = getSelectedAsset(state); if (selectedAsset.isPresent()) { if (!(selectedAsset.get() instanceof SideNote)) { @@ -90,7 +92,7 @@ public class SideNoteForm extends AssetForm { selectedAsset.get().getClass().getName())); } - final SideNote sideNote = (SideNote) selectedAsset.get(); + final SideNote sideNote =selectedAsset.get(); text.setValue(state, sideNote @@ -100,23 +102,30 @@ public class SideNoteForm extends AssetForm { } @Override - protected Asset createAsset(final FormSectionEvent event) throws - FormProcessException { - - Objects.requireNonNull(event); - - final PageState state = event.getPageState(); - - final SideNote sideNote = new SideNote(); - - sideNote - .getText() - .addValue(getSelectedLocale(state), - (String) text.getValue(state)); - - return sideNote; + protected Class getAssetClass() { + return SideNote.class; } + + +// @Override +// protected Asset createAsset(final FormSectionEvent event) throws +// FormProcessException { +// +// Objects.requireNonNull(event); +// +// final PageState state = event.getPageState(); +// +// final SideNote sideNote = new SideNote(); +// +// sideNote +// .getText() +// .addValue(getSelectedLocale(state), +// (String) text.getValue(state)); +// +// return sideNote; +// } + @Override protected void updateAsset(final Asset asset, final FormSectionEvent event) diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/VideoForm.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/VideoForm.java index bbb12eceb..7131d6c7e 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/VideoForm.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/forms/VideoForm.java @@ -26,9 +26,9 @@ import com.arsdigita.bebop.form.TextField; import com.arsdigita.cms.ui.assets.AssetPane; import com.arsdigita.cms.ui.assets.AssetSearchWidget; import com.arsdigita.globalization.GlobalizedMessage; + import org.libreccm.cdi.utils.CdiUtil; import org.librecms.CmsConstants; -import org.librecms.assets.BinaryAsset; import org.librecms.assets.VideoAsset; import org.librecms.assets.LegalMetadata; import org.librecms.contentsection.Asset; @@ -39,8 +39,10 @@ import java.util.Optional; /** * * @author Yannick Bülter + * @author Jens Pelzetter + * */ -public class VideoForm extends BinaryAssetForm { +public class VideoForm extends AbstractBinaryAssetForm { private TextField width; private TextField height; @@ -77,13 +79,14 @@ public class VideoForm extends BinaryAssetForm { } @Override - protected void initForm(PageState state, Optional selectedAsset) { + protected void initForm(final PageState state, + final Optional selectedAsset) { super.initForm(state, selectedAsset); if (selectedAsset.isPresent()) { - VideoAsset video = (VideoAsset) selectedAsset.get(); + final VideoAsset video = selectedAsset.get(); width.setValue(state, Long.toString(video.getWidth())); @@ -98,20 +101,27 @@ public class VideoForm extends BinaryAssetForm { } @Override - protected Asset createAsset(final FormSectionEvent event) - throws FormProcessException { - - final VideoAsset video = (VideoAsset) super.createAsset(event); - - final PageState state = event.getPageState(); - - video.setHeight(Long.parseLong((String) height.getValue(state))); - video.setWidth(Long.parseLong((String) width.getValue(state))); - updateData(video, state); - - return video; + protected Class getAssetClass() { + return VideoAsset.class; } + + +// @Override +// protected Asset createAsset(final FormSectionEvent event) +// throws FormProcessException { +// +// final VideoAsset video = (VideoAsset) super.createAsset(event); +// +// final PageState state = event.getPageState(); +// +// video.setHeight(Long.parseLong((String) height.getValue(state))); +// video.setWidth(Long.parseLong((String) width.getValue(state))); +// updateData(video, state); +// +// return video; +// } + @Override protected void updateAsset(final Asset asset, final FormSectionEvent event) @@ -147,8 +157,8 @@ public class VideoForm extends BinaryAssetForm { } } - @Override - protected BinaryAsset createBinaryAsset(final PageState state) { - return new VideoAsset(); - } +// @Override +// protected BinaryAsset createBinaryAsset(final PageState state) { +// return new VideoAsset(); +// } } diff --git a/ccm-cms/src/main/java/org/librecms/assets/AssetType.java b/ccm-cms/src/main/java/org/librecms/assets/AssetType.java index 4512e47a5..b1bda857f 100644 --- a/ccm-cms/src/main/java/org/librecms/assets/AssetType.java +++ b/ccm-cms/src/main/java/org/librecms/assets/AssetType.java @@ -18,7 +18,7 @@ */ package org.librecms.assets; -import com.arsdigita.cms.ui.assets.AssetForm; +import com.arsdigita.cms.ui.assets.AbstractAssetForm; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; @@ -50,7 +50,7 @@ public @interface AssetType { * @return The form for editing and creating assets of the annotated sub * class {@link org.librecms.contentsection.Asset}. */ - Class assetForm(); + Class assetForm(); /** * The key for the localised label of the asset type. If not set the default diff --git a/ccm-cms/src/main/java/org/librecms/assets/AssetTypeInfo.java b/ccm-cms/src/main/java/org/librecms/assets/AssetTypeInfo.java index 19c8a036b..4d0012106 100644 --- a/ccm-cms/src/main/java/org/librecms/assets/AssetTypeInfo.java +++ b/ccm-cms/src/main/java/org/librecms/assets/AssetTypeInfo.java @@ -18,7 +18,7 @@ */ package org.librecms.assets; -import com.arsdigita.cms.ui.assets.AssetForm; +import com.arsdigita.cms.ui.assets.AbstractAssetForm; import org.librecms.contentsection.Asset; @@ -59,7 +59,7 @@ public class AssetTypeInfo { /** * The form for editing and creating asset of the type described. */ - private Class assetForm; + private Class assetForm; public String getLabelBundle() { return labelBundle; @@ -101,11 +101,11 @@ public class AssetTypeInfo { this.assetClass = assetClass; } - public Class getAssetForm() { + public Class getAssetForm() { return assetForm; } - public void setAssetForm(final Class assetForm) { + public void setAssetForm(final Class assetForm) { this.assetForm = assetForm; } diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/Asset.java b/ccm-cms/src/main/java/org/librecms/contentsection/Asset.java index 1335cff34..7ae983f53 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/Asset.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/Asset.java @@ -259,6 +259,25 @@ import static org.librecms.CmsConstants.*; + " OR true = :isSystemUser OR true = :isAdmin" + ")") , + @NamedQuery( + name = "Asset.findByNameInFolder", + query = "SELECT DISTINCT a " + + "FROM Asset a " + + "JOIN a.categories c " + + "LEFT JOIN a.permissions p " + + "WHERE c.category = :folder " + + "AND c.type = '" + CATEGORIZATION_TYPE_FOLDER + "' " + + "AND a.displayName = :name " + + "AND (" + + " (" + + " p.grantee IN :roles " + + " AND p.grantedPrivilege = '" + + AssetPrivileges.VIEW + "'" + + " ) " + + " OR true = :isSystemUser OR true = :isAdmin" + + " )" + ) + , @NamedQuery( name = "Asset.countInFolder", query = "SELECT COUNT(DISTINCT a) " diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/AssetManager.java b/ccm-cms/src/main/java/org/librecms/contentsection/AssetManager.java index 6a4a9ab67..3d36b4ec9 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/AssetManager.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/AssetManager.java @@ -45,6 +45,7 @@ import java.util.Objects; import org.libreccm.categorization.ObjectNotAssignedToCategoryException; import org.libreccm.core.UnexpectedErrorException; import org.libreccm.l10n.LocalizedString; +import org.libreccm.security.PermissionManager; import java.beans.BeanInfo; import java.beans.IntrospectionException; @@ -52,6 +53,7 @@ import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.Locale; import java.util.Map; import java.util.Set; @@ -69,9 +71,6 @@ public class AssetManager { private static final Logger LOGGER = LogManager. getLogger(AssetManager.class); - @Inject - private EntityManager entityManager; - @Inject private AssetRepository assetRepo; @@ -82,7 +81,47 @@ public class AssetManager { private FolderRepository folderRepo; @Inject - private FolderManager folderManager; + private PermissionManager permissionManager; + + @Transactional(Transactional.TxType.REQUIRED) + public T createAsset(final String name, + final String title, + final Locale locale, + final Folder folder, + final Class assetType) { + + Objects.requireNonNull(name); + Objects.requireNonNull(title); + Objects.requireNonNull(locale); + Objects.requireNonNull(folder); + Objects.requireNonNull(assetType); + + if (name.trim().isEmpty()) { + throw new IllegalArgumentException( + "The name of an asset can't be empty."); + } + + if (title.trim().isEmpty()) { + throw new IllegalArgumentException( + "The title of an asset can't be empty."); + } + + final T asset; + try { + asset = assetType.newInstance(); + } catch (IllegalAccessException | InstantiationException ex) { + throw new UnexpectedErrorException(ex); + } + + asset.setDisplayName(name); + asset.getTitle().addValue(locale, title); + assetRepo.save(asset); + + categoryManager.addObjectToCategory(asset, folder); + permissionManager.copyPermissions(folder, asset, true); + + return asset; + } /** * Makes an {@link Asset} a shared {@code Asset} by adding it to an asset @@ -468,7 +507,7 @@ public class AssetManager { } else { sectionName = "?"; } - + return String.format("%s:/%s", sectionName, path); } else { return String.format("/%s", path); diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/AssetRepository.java b/ccm-cms/src/main/java/org/librecms/contentsection/AssetRepository.java index 3d3209571..5eca5f75a 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/AssetRepository.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/AssetRepository.java @@ -18,10 +18,14 @@ */ package org.librecms.contentsection; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.libreccm.auditing.AbstractAuditedEntityRepository; +import org.libreccm.categorization.Categorization; import org.libreccm.categorization.Category; import org.libreccm.categorization.CategoryManager; import org.libreccm.categorization.ObjectNotAssignedToCategoryException; +import org.libreccm.core.CcmObject; import org.libreccm.core.CcmObjectRepository; import org.libreccm.core.UnexpectedErrorException; import org.libreccm.security.AuthorizationRequired; @@ -54,6 +58,9 @@ import javax.transaction.Transactional; public class AssetRepository extends AbstractAuditedEntityRepository { + private static final Logger LOGGER = LogManager + .getLogger(AssetRepository.class); + @Inject private Shiro shiro; @@ -69,6 +76,9 @@ public class AssetRepository @Inject private CategoryManager categoryManager; + @Inject + private FolderRepository folderRepo; + @Inject private AssetManager assetManager; @@ -523,6 +533,86 @@ public class AssetRepository return query.getResultList(); } + @Transactional(Transactional.TxType.REQUIRED) + public Optional findByNameInFolder(final Folder folder, + final String name) { + + final TypedQuery query = getEntityManager() + .createNamedQuery("Asset.findByNameInFolder", + Asset.class) + .setParameter("folder", folder) + .setParameter("name", name); +// setAuthorizationParameters(query); + + try { + return Optional.of(query.getSingleResult()); + } catch (NoResultException ex) { + return Optional.empty(); + } + } + + @Transactional(Transactional.TxType.REQUIRED) + public Optional findByPath(final String path) { + + //The last token is the name of the asset itself. Remove this part and + //get the folder containing the asset using the FolderRepository. + final String normalizedPath = PathUtil.normalizePath(path); + final int lastTokenStart = normalizedPath.lastIndexOf('/'); + final String folderPath = normalizedPath.substring(0, lastTokenStart); + final String assetName = normalizedPath.substring(lastTokenStart + 1); + + final Optional folder = folderRepo.findByPath( + folderPath, FolderType.ASSETS_FOLDER); + + if (folder.isPresent()) { + return findByNameInFolder(folder.get(), assetName); + } else { + return Optional.empty(); + } + } + + @Transactional(Transactional.TxType.REQUIRED) + public Optional findByPath(final ContentSection section, + final String path) { + + //The last token is the name of the asset itself. Remove this part an get + //the folder containing the asset using the FolderRepository. + final String normalizedPath = PathUtil.normalizePath(path); + final int lastTokenStart = normalizedPath.lastIndexOf('/'); + final String assetName; + final Optional folder; + if (lastTokenStart < 0) { + assetName = normalizedPath; + folder = folderRepo + .findById(section.getRootAssetsFolder().getObjectId()); + } else { + final String folderPath = normalizedPath + .substring(0, lastTokenStart); + assetName = normalizedPath.substring(lastTokenStart + 1); + folder = folderRepo + .findByPath(section, folderPath, FolderType.ASSETS_FOLDER); + } + + if (folder.isPresent()) { + LOGGER.debug("transaction is active? {}", + entityManager.isJoinedToTransaction()); + LOGGER.debug("Folder for path {} found...", path); +// LOGGER.debug("Assets in the folder:"); +// final Folder theFolder = folderRepo +// .findById(folder.get().getObjectId()) +// .orElseThrow(() -> new IllegalArgumentException()); + for (final Categorization categorization : folder.get().getObjects()) { + LOGGER.debug(" {}", + categorization.getCategorizedObject() + .getDisplayName()); + } + + return findByNameInFolder(folder.get(), assetName); + } else { + return Optional.empty(); + } + } + /** * Counts the assets of a specific type which name starts with a provided * string in a specific folder. diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/ContentItem.java b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItem.java index dba0aac62..8eb1b1c92 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/ContentItem.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItem.java @@ -233,9 +233,9 @@ import static org.librecms.CmsConstants.*; + " ELSE '" + ItemPrivileges.VIEW_PUBLISHED + "' " + " END" + " )" - + " )" - + " OR true = :isSystemUser OR true = :isAdmin" - + " )") + + " )" + + " OR true = :isSystemUser OR true = :isAdmin" + + " )") , @NamedQuery( name = "ContentItem.countByNameInFolder", diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemManager.java b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemManager.java index 9aea00137..af0dd7f79 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemManager.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemManager.java @@ -18,8 +18,6 @@ */ package org.librecms.contentsection; -import com.arsdigita.kernel.KernelConfig; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -70,6 +68,7 @@ import javax.persistence.TypedQuery; import javax.transaction.Transactional; import org.libreccm.security.PermissionChecker; +import org.libreccm.security.PermissionManager; import org.librecms.contentsection.privileges.TypePrivileges; /** @@ -77,48 +76,48 @@ import org.librecms.contentsection.privileges.TypePrivileges; * * @author Jens Pelzetter */ - @RequestScoped +@RequestScoped public class ContentItemManager { private static final Logger LOGGER = LogManager.getLogger( ContentItemManager.class); @Inject - private EntityManager entityManager; - - @Inject - private ConfigurationManager confManager; + private AssetManager assetManager; @Inject private CategoryManager categoryManager; @Inject - private FolderManager folderManager; + private ContentItemRepository contentItemRepo; @Inject private ContentSectionManager sectionManager; - @Inject - private ContentItemRepository contentItemRepo; - @Inject private ContentTypeRepository typeRepo; @Inject - private LifecycleManager lifecycleManager; + private EntityManager entityManager; @Inject - private WorkflowManager workflowManager; + private FolderManager folderManager; @Inject private FolderRepository folderRepo; @Inject - private AssetManager assetManager; + private LifecycleManager lifecycleManager; @Inject private PermissionChecker permissionChecker; + @Inject + private PermissionManager permissionManager; + + @Inject + private WorkflowManager workflowManager; + /** * Creates a new content item in the provided content section and folder * with the default workflow for the content type of the item. @@ -319,9 +318,6 @@ public class ContentItemManager { throw new RuntimeException(ex); } - final KernelConfig kernelConfig = confManager.findConfiguration( - KernelConfig.class); - item.setDisplayName(name); item.getName().addValue(locale, name); @@ -351,6 +347,8 @@ public class ContentItemManager { if (item.getWorkflow() != null) { workflowManager.start(item.getWorkflow()); } + + permissionManager.copyPermissions(folder, item, true); return item; } @@ -537,8 +535,8 @@ public class ContentItemManager { source = (LocalizedString) readMethod.invoke(draftItem); target = (LocalizedString) readMethod.invoke(copy); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } @@ -550,8 +548,8 @@ public class ContentItemManager { try { linkedItem = (ContentItem) readMethod.invoke(draftItem); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } @@ -561,8 +559,8 @@ public class ContentItemManager { try { writeMethod.invoke(copy, linkedDraftItem); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } } else if (propType != null @@ -573,8 +571,8 @@ public class ContentItemManager { source = (List) readMethod.invoke(draftItem); target = (List) readMethod.invoke(copy); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } @@ -588,8 +586,8 @@ public class ContentItemManager { source = (Map) readMethod.invoke(draftItem); target = (Map) readMethod.invoke(copy); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } @@ -603,8 +601,8 @@ public class ContentItemManager { source = (Set) readMethod.invoke(draftItem); target = (Set) readMethod.invoke(copy); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } @@ -615,8 +613,8 @@ public class ContentItemManager { value = readMethod.invoke(draftItem); writeMethod.invoke(copy, value); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } } @@ -785,8 +783,8 @@ public class ContentItemManager { sourceStr = (LocalizedString) readMethod.invoke(source); targetStr = (LocalizedString) readMethod.invoke(target); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new UnexpectedErrorException(ex); } @@ -797,8 +795,8 @@ public class ContentItemManager { value = readMethod.invoke(source); writeMethod.invoke(target, value); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new UnexpectedErrorException(ex); } } @@ -922,8 +920,8 @@ public class ContentItemManager { source = (LocalizedString) readMethod.invoke(draftItem); target = (LocalizedString) readMethod.invoke(liveItem); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } @@ -934,8 +932,8 @@ public class ContentItemManager { try { linkedItem = (ContentItem) readMethod.invoke(draftItem); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } @@ -949,8 +947,8 @@ public class ContentItemManager { linkedDraftItem, ContentItem.class); writeMethod.invoke(liveItem, linkedLiveItem); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } } @@ -962,8 +960,8 @@ public class ContentItemManager { source = (List) readMethod.invoke(draftItem); target = (List) readMethod.invoke(liveItem); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } @@ -977,8 +975,8 @@ public class ContentItemManager { source = (Map) readMethod.invoke(draftItem); target = (Map) readMethod.invoke(liveItem); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } @@ -992,8 +990,8 @@ public class ContentItemManager { source = (Set) readMethod.invoke(draftItem); target = (Set) readMethod.invoke(liveItem); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } @@ -1004,8 +1002,8 @@ public class ContentItemManager { value = readMethod.invoke(item); writeMethod.invoke(liveItem, value); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } } diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemRepository.java b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemRepository.java index 7bde65ee4..ed21394df 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemRepository.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemRepository.java @@ -410,7 +410,7 @@ public class ContentItemRepository @Transactional(Transactional.TxType.REQUIRED) public Optional findByPath(final String path) { - //The last token is the name of the item itself. Remove this part an get + //The last token is the name of the item itself. Remove this part and get //the folder containing the item using the FolderRepository. final String normalizedPath = PathUtil.normalizePath(path); final int lastTokenStart = normalizedPath.lastIndexOf('/'); diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/PathUtil.java b/ccm-cms/src/main/java/org/librecms/contentsection/PathUtil.java index 0ced0fa93..a756bf79d 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/PathUtil.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/PathUtil.java @@ -34,16 +34,15 @@ public final class PathUtil { * * The method does the following: *
    - *
  • Replace all "." in the path with a slash.
  • *
  • If the first character is a slash remove the character.
  • *
  • If the last character is a slash remove the character.
  • *
* - * @param path - * @return + * @param path The path to normalise. + * @return The normalised path */ protected static final String normalizePath(final String path) { - String normalizedPath = path.replace('.', '/'); + String normalizedPath = path; if (normalizedPath.charAt(0) == '/') { normalizedPath = normalizedPath.substring(1); } diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/rs/ContentSections.java b/ccm-cms/src/main/java/org/librecms/contentsection/rs/ContentSections.java index bcfa9cff0..129a6287e 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/rs/ContentSections.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/rs/ContentSections.java @@ -37,6 +37,7 @@ public class ContentSections extends Application{ final Set> classes = new HashSet<>(); classes.add(Assets.class); + classes.add(Images.class); return classes; } diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/rs/Images.java b/ccm-cms/src/main/java/org/librecms/contentsection/rs/Images.java new file mode 100644 index 000000000..9cf49d7ce --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/contentsection/rs/Images.java @@ -0,0 +1,110 @@ +/* + * 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.rs; + +import org.librecms.assets.Image; +import org.librecms.contentsection.Asset; +import org.librecms.contentsection.AssetRepository; +import org.librecms.contentsection.ContentSection; +import org.librecms.contentsection.ContentSectionRepository; + +import java.util.Optional; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.core.Response; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +@Path("/{content-section}/images/") +public class Images { + + @Inject + private ContentSectionRepository sectionRepo; + + @Inject + private AssetRepository assetRepo; + + @GET + @Path("/{path:.+}") + public Response getImage( + @PathParam("content-section") final String sectionName, + @PathParam("path") final String path) { + + final Optional section = sectionRepo + .findByLabel(sectionName); + if (!section.isPresent()) { + return Response + .status(Response.Status.NOT_FOUND) + .entity(String.format("No content section \"%s\" available.", + sectionName)) + .build(); + } + + final Optional asset = assetRepo.findByPath(section.get(), + path); + + if (asset.isPresent()) { + if (asset.get() instanceof Image) { + final Image image = (Image) asset.get(); + final byte[] data = image.getData(); + + return Response + .ok(String.format( + "Requested image \"%s\" in content section \"%s\"", + path, + section.get().getLabel()), + "text/plain") + .build(); + } else { + return Response + .status(Response.Status.NOT_FOUND) + .entity(String + .format("The asset found at the requested path \"%s\" " + + "is not an image.", + path)) + .build(); + } + } else { + return Response + .status(Response.Status.NOT_FOUND) + .entity(String + .format("The requested image \"%s\" does not exist.", + path)) + .build(); + } + +// final Response.ResponseBuilder builder = Response +// .ok(String.format( +// "Requested image \"%s\" from folder \"%s\" in content section \"%s\"", +// imageName, +// folderPath, +// section.get().getLabel()), +// "text/plain"); +// +// return builder.build(); + } + +} diff --git a/ccm-cms/src/test/java/org/librecms/contentsection/ContentItemManagerTest.java b/ccm-cms/src/test/java/org/librecms/contentsection/ContentItemManagerTest.java index f190b85f9..be87031b6 100644 --- a/ccm-cms/src/test/java/org/librecms/contentsection/ContentItemManagerTest.java +++ b/ccm-cms/src/test/java/org/librecms/contentsection/ContentItemManagerTest.java @@ -221,11 +221,14 @@ public class ContentItemManagerTest { value = "datasets/org/librecms/contentsection/" + "ContentItemManagerTest/after-create-contentitem.xml", excludeColumns = {"categorization_id", + "creation_date", "id", + "inherited_from_id", "item_uuid", "lifecycle_id", "object_id", "object_order", + "permission_id", "phase_id", "rev", "task_assignment_id", @@ -391,11 +394,14 @@ public class ContentItemManagerTest { + "ContentItemManagerTest/" + "after-create-contentitem-with-workflow.xml", excludeColumns = {"categorization_id", + "creation_date", "id", + "inherited_from_id", "item_uuid", "lifecycle_id", "object_id", "object_order", + "permission_id", "phase_id", "rev", "task_id", diff --git a/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/after-create-contentitem-with-workflow.xml b/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/after-create-contentitem-with-workflow.xml index 8951ebd33..3015464f5 100644 --- a/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/after-create-contentitem-with-workflow.xml +++ b/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/after-create-contentitem-with-workflow.xml @@ -749,341 +749,614 @@ granted_privilege="categorize_items" object_id="-2100" grantee_id="-3200" - creation_date="2016-07-15"/> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/after-create-contentitem.xml b/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/after-create-contentitem.xml index f9511a7c9..5dc79d79a 100644 --- a/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/after-create-contentitem.xml +++ b/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/after-create-contentitem.xml @@ -765,341 +765,612 @@ granted_privilege="categorize_items" object_id="-2100" grantee_id="-3200" - creation_date="2016-07-15"/> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> - + creation_date="2016-07-15" + inherited="false" /> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/data.xml b/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/data.xml index 1d89becf0..f662ab21f 100644 --- a/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/data.xml +++ b/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/data.xml @@ -686,341 +686,409 @@ granted_privilege="categorize_items" object_id="-2100" grantee_id="-3200" - creation_date="2016-07-15"/> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> + creation_date="2016-07-15" + inherited="false" /> diff --git a/ccm-core/src/main/java/org/libreccm/security/PermissionManager.java b/ccm-core/src/main/java/org/libreccm/security/PermissionManager.java index a18af9201..948bd1a71 100644 --- a/ccm-core/src/main/java/org/libreccm/security/PermissionManager.java +++ b/ccm-core/src/main/java/org/libreccm/security/PermissionManager.java @@ -129,14 +129,18 @@ public class PermissionManager { * @param grantee The role to which the privilege is granted. * @param object The object on which the privilege is granted. * + * @return The newly granted permission. If there is also none inherited + * permission granting the provided {@code privilege} on the + * provided {@code object} the return value is {@code null}. + * * @see RecursivePermissions */ @AuthorizationRequired @RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN) @Transactional(Transactional.TxType.REQUIRED) - public void grantPrivilege(final String privilege, - final Role grantee, - final CcmObject object) { + public Permission grantPrivilege(final String privilege, + final Role grantee, + final CcmObject object) { if (privilege == null || privilege.isEmpty()) { throw new IllegalArgumentException( "Can't grant a permission without a privilege."); @@ -156,7 +160,9 @@ public class PermissionManager { revokePrivilege(privilege, grantee, object); } - if (!existsPermission(privilege, grantee, object)) { + if (existsPermission(privilege, grantee, object)) { + return null; + } else { final Permission permission = new Permission(); permission.setGrantee(grantee); permission.setGrantedPrivilege(privilege); @@ -166,6 +172,8 @@ public class PermissionManager { entityManager.persist(permission); grantRecursive(privilege, grantee, object, object.getClass(), object); + + return permission; } } @@ -485,14 +493,35 @@ public class PermissionManager { * permissions. * * - * @param source - * @param target + * @param source The source object from which the permissions are copied. + * @param target The target object to which the permissions are copied. */ @AuthorizationRequired @RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN) @Transactional(Transactional.TxType.REQUIRED) public void copyPermissions(final CcmObject source, final CcmObject target) { + copyPermissions(source, target, false); + } + + /** + * Copy the permissions from on {@link CcmObject} to another. The + * permissions granted on the {@code target} object will not be removed. + * Instead the permissions from {@code source} object are added the the + * permissions. This variant is should only be used to copy permissions from + * a parent object (like a category) to a new or newly assigned object. + * + * @param source The source object from which the permissions are copied. + * @param target The target object to which the permissions are copied. + * @param inherited Value for the {@link Permission#inherited} field of the + * copied permission. + */ + @AuthorizationRequired + @RequiresPrivilege(CoreConstants.PRIVILEGE_ADMIN) + @Transactional(Transactional.TxType.REQUIRED) + public void copyPermissions(final CcmObject source, + final CcmObject target, + final boolean inherited) { if (source == null) { throw new IllegalArgumentException( "Can't copy permissions from source NULL."); @@ -509,9 +538,15 @@ public class PermissionManager { final List result = query.getResultList(); for (final Permission permission : result) { - grantPrivilege(permission.getGrantedPrivilege(), - permission.getGrantee(), - target); + final Permission granted = grantPrivilege( + permission.getGrantedPrivilege(), + permission.getGrantee(), + target); + granted.setInherited(inherited); + if (inherited) { + granted.setInheritedFrom(source); + } + entityManager.merge(granted); } }