diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/AssetFolderController.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/AssetFolderController.java index 843191aa3..a7f479e2c 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/AssetFolderController.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/AssetFolderController.java @@ -34,6 +34,7 @@ import org.librecms.contentsection.FolderManager; import org.librecms.contentsection.FolderRepository; import org.librecms.contentsection.FolderType; import org.librecms.contentsection.privileges.AssetPrivileges; +import org.librecms.ui.contentsections.assets.MvcAssetCreateStep; import java.util.ArrayList; import java.util.Arrays; @@ -44,6 +45,8 @@ import java.util.Optional; import java.util.stream.Collectors; import javax.enterprise.context.RequestScoped; +import javax.enterprise.inject.Any; +import javax.enterprise.inject.Instance; import javax.inject.Inject; import javax.mvc.Controller; import javax.mvc.Models; @@ -154,6 +157,10 @@ public class AssetFolderController { @Inject private Models models; + @Inject + @Any + private Instance> assetCreateSteps; + /** * Performs operations on permissions. */ @@ -267,6 +274,17 @@ public class AssetFolderController { assetFolderTree.buildFolderTree(section, folder) ); + contentSectionModel.setAvailableAssetTypes( + assetCreateSteps + .stream() + .collect( + Collectors.toMap( + step -> step.getAssetType(), + step -> step.getLabel() + ) + ) + ); + assetFolderModel.setRows( folderEntries .stream() diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/AssetFolderModel.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/AssetFolderModel.java index 9fdc445cb..2332ae305 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/AssetFolderModel.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/AssetFolderModel.java @@ -26,9 +26,9 @@ import javax.enterprise.context.RequestScoped; import javax.inject.Named; /** - * Model for the current asset folder. Provides data about the folder for the + * Model for the current asset folder. Provides data about the folder for the * view template. - * + * * @author Jens Pelzetter */ @RequestScoped @@ -145,6 +145,14 @@ public class AssetFolderModel { this.path = path; } + public String getPathWithTrailingSlash() { + if (path.isEmpty()) { + return ""; + } else { + return String.format("%s/", path); + } + } + public boolean isCanCreateSubFolders() { return canCreateSubFolders; } diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/AssetPermissionsChecker.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/AssetPermissionsChecker.java index 906e7f3e9..bc7532e29 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/AssetPermissionsChecker.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/AssetPermissionsChecker.java @@ -202,7 +202,7 @@ public class AssetPermissionsChecker { * {@link AssetPrivileges#EDIT} privilege for the provided * {@link Asset} to the current user, {@code false} otherwise. */ - public boolean canEditAssets(final Asset asset) { + public boolean canEditAsset(final Asset asset) { return permissionChecker.isPermitted( AssetPrivileges.EDIT, asset ); diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/AssetPermissionsModelProvider.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/AssetPermissionsModelProvider.java index f623cd212..0a3895254 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/AssetPermissionsModelProvider.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/AssetPermissionsModelProvider.java @@ -91,7 +91,7 @@ public class AssetPermissionsModelProvider { permissionChecker.canDeleteAssets(asset) ); model.setGrantedEdit( - permissionChecker.canEditAssets(asset) + permissionChecker.canEditAsset(asset) ); model.setGrantedUse( permissionChecker.canUseAssets(asset) diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/ContentSectionModel.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/ContentSectionModel.java index 5e3d382e2..5aec790e5 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/ContentSectionModel.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/ContentSectionModel.java @@ -70,6 +70,8 @@ public class ContentSectionModel { private List documentFolders; private Map availableDocumentTypes; + + private Map availableAssetTypes; /** * @@ -128,6 +130,16 @@ public class ContentSectionModel { this.availableDocumentTypes = new LinkedHashMap<>(availableDocumentTypes); } + + public Map getAvailableAssetTypes() { + return Collections.unmodifiableMap(availableAssetTypes); + } + + protected void setAvailableAssetTypes( + final Map availableAssetTypes + ) { + this.availableAssetTypes = new LinkedHashMap<>(availableAssetTypes); + } /** * Can the current user administer the categories of the domains/category diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/ContentSectionNotFoundException.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/ContentSectionNotFoundException.java similarity index 92% rename from ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/ContentSectionNotFoundException.java rename to ccm-cms/src/main/java/org/librecms/ui/contentsections/ContentSectionNotFoundException.java index 438dfcc0d..fbbadca5c 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/ContentSectionNotFoundException.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/ContentSectionNotFoundException.java @@ -16,7 +16,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301 USA */ -package org.librecms.ui.contentsections.documents; +package org.librecms.ui.contentsections; /** * Used by the {@link MvcAuthoringStepService} to indicate that the requested @@ -39,7 +39,7 @@ public class ContentSectionNotFoundException extends Exception { * * @param errorMessageTemplate Template for the error message. */ - ContentSectionNotFoundException(final String errorMessageTemplate) { + public ContentSectionNotFoundException(final String errorMessageTemplate) { super(); this.errorMessageTemplate = errorMessageTemplate; } @@ -51,7 +51,7 @@ public class ContentSectionNotFoundException extends Exception { * @param msg The detail message. * @param errorMessageTemplate Template for the error message. */ - ContentSectionNotFoundException( + public ContentSectionNotFoundException( final String errorMessageTemplate, final String msg ) { super(msg); diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/AbstractMvcAssetEditStep.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/AbstractMvcAssetEditStep.java new file mode 100644 index 000000000..263a25993 --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/AbstractMvcAssetEditStep.java @@ -0,0 +1,315 @@ +/* + * Copyright (C) 2021 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.ui.contentsections.assets; + +import org.libreccm.l10n.GlobalizationHelper; +import org.librecms.contentsection.Asset; +import org.librecms.contentsection.AssetManager; +import org.librecms.contentsection.AssetRepository; +import org.librecms.contentsection.ContentSection; +import org.librecms.ui.contentsections.AssetPermissionsChecker; +import org.librecms.ui.contentsections.ContentSectionModel; +import org.librecms.ui.contentsections.ContentSectionsUi; +import org.librecms.ui.contentsections.ContentSectionNotFoundException; +import org.librecms.ui.contentsections.documents.MvcAuthoringSteps; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import javax.inject.Inject; +import javax.mvc.Models; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.UriBuilder; + +/** + * + * @author Jens Pelzetter + */ +public abstract class AbstractMvcAssetEditStep implements MvcAssetEditStep { + + @Inject + private AssetUi assetUi; + + @Inject + private AssetManager assetManager; + + @Inject + private AssetPermissionsChecker assetPermissionsChecker; + + @Inject + private AssetRepository assetRepo; + + @Inject + private GlobalizationHelper globalizationHelper; + + @Inject + private ContentSectionModel sectionModel; + + @Inject + private ContentSectionsUi sectionsUi; + + @Inject + private HttpServletRequest request; + + @Inject + private Models models; + + @Inject + private SelectedAssetModel assetModel; + + @PathParam(MvcAuthoringSteps.SECTION_IDENTIFIER_PATH_PARAM) + private String sectionIdentifier; + + @PathParam(MvcAuthoringSteps.DOCUMENT_PATH_PATH_PARAM_NAME) + private String assetPathParam; + + private ContentSection contentSection; + + private Asset asset; + + private String assetPath; + + private String stepPath; + + protected void init() throws ContentSectionNotFoundException, + AssetNotFoundException { + contentSection = sectionsUi + .findContentSection(sectionIdentifier) + .orElseThrow( + () -> new ContentSectionNotFoundException( + sectionsUi.showContentSectionNotFound(sectionIdentifier), + String.format( + "ContentSection %s not found.", + sectionIdentifier + ) + ) + ); + sectionModel.setSection(contentSection); + + asset = assetRepo + .findByPath(contentSection, assetPathParam) + .orElseThrow( + () -> new AssetNotFoundException( + assetUi.showAssetNotFound( + contentSection, assetPathParam + ), + String.format( + "No asset for path %s found in section %s.", + assetPathParam, + contentSection.getLabel() + ) + ) + ); + assetModel.setAsset(asset); + + assetPath = assetManager.getAssetPath(asset); + final Map values = new HashMap<>(); + values.put( + MvcAssetEditSteps.SECTION_IDENTIFIER_PATH_PARAM, + contentSection.getLabel() + ); + values.put( + MvcAssetEditSteps.ASSET_PATH_PATH_PARAM_NAME, + assetPath + ); + + stepPath = Optional + .ofNullable(getStepClass().getAnnotation(Path.class)) + .map(Path::value) + .map( + path -> UriBuilder + .fromPath(path) + .buildFromMap(values) + .toString() + ) + .orElse(""); + + models.put("activeAssetTab", "editTab"); + models.put("stepPath", stepPath); + } + + @Override + public ContentSection getContentSection() { + return Optional + .ofNullable(contentSection) + .orElseThrow( + () -> new WebApplicationException( + String.format( + "Edit step %s was not initialized properly. " + + "Did you forget to call %s#init()?", + getStepClass().getName(), + AbstractMvcAssetEditStep.class.getName() + ) + ) + ); + } + + @Override + public Asset getAsset() { + return Optional + .ofNullable(asset) + .orElseThrow( + () -> new WebApplicationException( + String.format( + "Edit step %s was not initialized properly. " + + "Did you forget to call %s#init()?", + getStepClass().getName(), + AbstractMvcAssetEditStep.class.getName() + ) + ) + ); + } + + @Override + public String getAssetPath() { + return Optional + .ofNullable(assetPath) + .orElseThrow( + () -> new WebApplicationException( + String.format( + "Edit step %s was not initialized properly. " + + "Did you forget to call %s#init()?", + getStepClass().getName(), + AbstractMvcAssetEditStep.class.getName() + ) + ) + ); + } + + @Override + public boolean getCanEdit() { + return assetPermissionsChecker.canEditAsset(asset); + } + + @Override + public void updateAssetPath() { + assetPath = assetManager.getAssetPath(asset).substring(1); // Without leading slash + } + + @Override + public String getStepPath() { + return stepPath; + } + + @Override + public String buildRedirectPathForStep() { + final ContentSection section = Optional + .ofNullable(contentSection) + .orElseThrow( + () -> new WebApplicationException( + String.format( + "Edit Step %s was not initalized properly. " + + "Did you forget to call %s#init()?", + getStepClass().getName(), + AbstractMvcAssetEditStep.class.getName() + ) + ) + ); + final String assetPathNonNull = Optional + .ofNullable(assetPath) + .orElseThrow( + () -> new WebApplicationException( + String.format( + "Edit step %s was not initialized properly. " + + "Did you forget to call %s#init()?", + getStepClass().getName(), + AbstractMvcAssetEditStep.class.getName() + ) + ) + ); + + final Map values = new HashMap<>(); + values.put( + MvcAssetEditSteps.SECTION_IDENTIFIER_PATH_PARAM, + section.getLabel() + ); + values.put( + MvcAssetEditSteps.ASSET_PATH_PATH_PARAM_NAME, + assetPathNonNull + ); + + return Optional + .ofNullable(getStepClass().getAnnotation(Path.class)) + .map(Path::value) + .map( + path -> UriBuilder + .fromPath(path) + .buildFromMap(values) + .toString() + ) + .map(path -> String.format("redirect:%s", path)) + .orElse(""); + } + + @Override + public String buildRedirectPathForStep(final String subPath) { + final ContentSection section = Optional + .ofNullable(contentSection) + .orElseThrow( + () -> new WebApplicationException( + String.format( + "Edit Step %s was not initalized properly. " + + "Did you forget to call %s#init()?", + getStepClass().getName(), + AbstractMvcAssetEditStep.class.getName() + ) + ) + ); + final String assetPathNonNull = Optional + .ofNullable(assetPath) + .orElseThrow( + () -> new WebApplicationException( + String.format( + "Edit step %s was not initialized properly. " + + "Did you forget to call %s#init()?", + getStepClass().getName(), + AbstractMvcAssetEditStep.class.getName() + ) + ) + ); + + final Map values = new HashMap<>(); + values.put( + MvcAssetEditSteps.SECTION_IDENTIFIER_PATH_PARAM, + section.getLabel() + ); + values.put( + MvcAssetEditSteps.ASSET_PATH_PATH_PARAM_NAME, + assetPathNonNull + ); + + return Optional + .ofNullable(getStepClass().getAnnotation(Path.class)) + .map(Path::value) + .map( + path -> UriBuilder + .fromPath(path) + .path(subPath) + .buildFromMap(values) + .toString() + ) + .map(path -> String.format("redirect:%s", path)) + .orElse(""); + } + +} diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/AssetEditStepsValidator.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/AssetEditStepsValidator.java index cc931a324..0cd1483bf 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/AssetEditStepsValidator.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/AssetEditStepsValidator.java @@ -75,12 +75,12 @@ public class AssetEditStepsValidator { return false; } - if (stepClass.getAnnotation(MvcAssetEditStep.class) == null) { + if (stepClass.getAnnotation(MvcAssetEditStepDef.class) == null) { LOGGER.warn( "Class {} is part of a set of asset edit steps, but is not " + "annotated with {}. The class will be ignored.", stepClass.getName(), - MvcAssetEditStep.class + MvcAssetEditStepDef.class ); } @@ -89,7 +89,7 @@ public class AssetEditStepsValidator { public boolean supportsAsset(final Class stepClass, final Asset asset) { return Optional - .ofNullable(stepClass.getAnnotation(MvcAssetEditStep.class)) + .ofNullable(stepClass.getAnnotation(MvcAssetEditStepDef.class)) .map( stepAnnotation -> asset.getClass().isAssignableFrom( stepAnnotation.supportedAssetType() @@ -97,8 +97,8 @@ public class AssetEditStepsValidator { ) .orElse(false); -// final MvcAssetEditStep stepAnnotation = stepClass.getAnnotation( -// MvcAssetEditStep.class +// final MvcAssetEditStepDef stepAnnotation = stepClass.getAnnotation( +// MvcAssetEditStepDef.class // ); // // if (stepAnnotation == null) { diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/AssetNotFoundException.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/AssetNotFoundException.java new file mode 100644 index 000000000..03bc2565a --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/AssetNotFoundException.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2021 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.ui.contentsections.assets; + +/** + * + * @author Jens Pelzetter + */ +public class AssetNotFoundException extends Exception { + + private static final long serialVersionUID = 1L; + + private final String errorMessageTemplate; + + public AssetNotFoundException(final String errorMessageTemplate) { + super(); + this.errorMessageTemplate = errorMessageTemplate; + } + + public AssetNotFoundException( + final String errorMessageTemplate, final String msg + ) { + super(msg); + this.errorMessageTemplate = errorMessageTemplate; + } + + public String showErrorMessage() { + return errorMessageTemplate; + } + + +} diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/MvcAssetCreateStep.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/MvcAssetCreateStep.java index f513f5fd8..ea85ef6f1 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/MvcAssetCreateStep.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/MvcAssetCreateStep.java @@ -67,6 +67,15 @@ public interface MvcAssetCreateStep { */ String getAssetType(); + /** + * Localized label of the asset type. The current locale as returned by + * {@link GlobalizationHelper#getNegotiatedLocale()} should be used to + * select the language variant to return. + * + * @return The localized label of the asset type. + */ + String getLabel(); + /** * Localized description of the create step. The current locale as returned * by {@link GlobalizationHelper#getNegotiatedLocale()} should be used to diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/MvcAssetEditStep.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/MvcAssetEditStep.java index 37b982faa..4663c7c81 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/MvcAssetEditStep.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/MvcAssetEditStep.java @@ -19,50 +19,51 @@ package org.librecms.ui.contentsections.assets; import org.librecms.contentsection.Asset; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; +import org.librecms.contentsection.ContentSection; +import org.librecms.ui.contentsections.ContentSectionNotFoundException; /** - * Metadata of an edit step for assets. * * @author Jens Pelzetter */ -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.RUNTIME) -public @interface MvcAssetEditStep { +public interface MvcAssetEditStep { + + Class getStepClass(); + + ContentSection getContentSection() throws ContentSectionNotFoundException; + + Asset getAsset() throws ContentSectionNotFoundException, + AssetNotFoundException; + + String getAssetPath() throws ContentSectionNotFoundException, + AssetNotFoundException; /** - * The name of the resource bundle providing the localized values for - * {@link #labelKey} and {@link descriptionKey}. + * Can the current user edit the asset. This method MUST only return * - * @return The resource bundle providing the localized labelKey and - * descriptionKey. + * @return {@code true} if the current user can edit the asset, { + * + * @false} otherwise. */ - String bundle(); + boolean getCanEdit(); /** - * The key for the localized description of the step. + * If an edit step alters the name of the asset and therefore the path of + * the asset, the step MUST call this method to update the asset path used + * by the step. * - * @return The key for the localized description of the step. + * @throws ContentSectionNotFoundException + * @throws AssetNotFoundException */ - String descriptionKey(); + void updateAssetPath() throws ContentSectionNotFoundException, + AssetNotFoundException; - /** - * The key for the localized label of the authoring step.. - * - * @return The key for the localized label of the authoring step... - */ - String labelKey(); + String getStepPath(); - /** - * Edit steps only support a specific type, and all subtypes. - * - * @return The asset type supported by the edit step. - */ + String buildRedirectPathForStep() throws ContentSectionNotFoundException, + AssetNotFoundException; - Class supportedAssetType(); + String buildRedirectPathForStep(final String subPath) + throws ContentSectionNotFoundException, AssetNotFoundException; } diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/MvcAssetEditStepDef.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/MvcAssetEditStepDef.java new file mode 100644 index 000000000..87bcfedb4 --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/MvcAssetEditStepDef.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2021 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.ui.contentsections.assets; + +import org.librecms.contentsection.Asset; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Metadata of an edit step for assets. + * + * @author Jens Pelzetter + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface MvcAssetEditStepDef { + + /** + * The name of the resource bundle providing the localized values for + * {@link #labelKey} and {@link descriptionKey}. + * + * @return The resource bundle providing the localized labelKey and + * descriptionKey. + */ + String bundle(); + + /** + * The key for the localized description of the step. + * + * @return The key for the localized description of the step. + */ + String descriptionKey(); + + /** + * The key for the localized label of the authoring step.. + * + * @return The key for the localized label of the authoring step... + */ + String labelKey(); + + /** + * Edit steps only support a specific type, and all subtypes. + * + * @return The asset type supported by the edit step. + */ + + Class supportedAssetType(); + +} diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/PostalAddressCreateStep.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/PostalAddressCreateStep.java new file mode 100644 index 000000000..596c6bfe7 --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/PostalAddressCreateStep.java @@ -0,0 +1,174 @@ +/* + * Copyright (C) 2021 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.ui.contentsections.assets; + +import org.libreccm.l10n.GlobalizationHelper; +import org.libreccm.security.AuthorizationRequired; +import org.librecms.assets.PostalAddress; +import org.librecms.contentsection.AssetManager; +import org.librecms.contentsection.AssetRepository; + +import java.util.Locale; +import java.util.Map; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.inject.Named; +import javax.transaction.Transactional; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +@Named("CmsPostalAddressCreateStep") +public class PostalAddressCreateStep + extends AbstractMvcAssetCreateStep { + + private static final String FORM_PARAMS_NAME = "name"; + + private static final String FORM_PARAMS_TITLE = "title"; + + private static final String FORM_PARAM_INITIAL_LOCALE = "locale"; + + @Inject + private AssetManager assetManager; + + @Inject + private AssetRepository assetRepo; + + @Inject + private GlobalizationHelper globalizationHelper; + + @Override + public String showCreateStep() { + return "/org/librecms/ui/assets/postaladdress/create-postaladdress.xhtml"; + } + + @AuthorizationRequired + @Transactional(Transactional.TxType.REQUIRED) + @Override + public String createAsset(final Map formParams) { + if (!formParams.containsKey(FORM_PARAMS_NAME) + || formParams.get(FORM_PARAMS_NAME) == null + || formParams.get(FORM_PARAMS_NAME).length == 0) { + addMessage( + "danger", + globalizationHelper + .getLocalizedTextsUtil(getBundle()) + .getText("postaladdress.createstep.name.error.missing") + ); + return showCreateStep(); + } + + final String name = formParams.get(FORM_PARAMS_NAME)[0]; + if (!name.matches("^([a-zA-Z0-9_-]*)$")) { + addMessage( + "danger", + globalizationHelper + .getLocalizedTextsUtil(getBundle()) + .getText("postaladdress.createstep.name.error.invalid") + ); + return showCreateStep(); + } + + if (!formParams.containsKey(FORM_PARAMS_TITLE) + || formParams.get(FORM_PARAMS_TITLE) == null + || formParams.get(FORM_PARAMS_TITLE).length == 0) { + addMessage( + "danger", + globalizationHelper + .getLocalizedTextsUtil(getBundle()) + .getText("postaladdress.createstep.title.error.missing") + ); + return showCreateStep(); + } + final String title = formParams.get(FORM_PARAMS_TITLE)[0]; + + if (!formParams.containsKey(FORM_PARAM_INITIAL_LOCALE) + || formParams.get(FORM_PARAM_INITIAL_LOCALE) == null + || formParams.get(FORM_PARAM_INITIAL_LOCALE).length == 0) { + addMessage( + "danger", + globalizationHelper.getLocalizedTextsUtil( + getBundle() + ).getText("createstep.initial_locale.error.missing") + ); + return showCreateStep(); + } + final Locale locale = new Locale( + formParams.get(FORM_PARAM_INITIAL_LOCALE)[0] + ); + + final String address = formParams.get("address")[0]; + final String postalCode = formParams.get("postalCode")[0]; + final String city = formParams.get("city")[0]; + final String state = formParams.get("state")[0]; + final String isoCountryCode = formParams.get("isoCountryCode")[0]; + + final PostalAddress postalAddress = assetManager.createAsset( + name, + title, + locale, + getFolder(), + PostalAddress.class + ); + + postalAddress.setAddress(address); + postalAddress.setPostalCode(postalCode); + postalAddress.setCity(city); + postalAddress.setState(state); + postalAddress.setIsoCountryCode(isoCountryCode); + + assetRepo.save(postalAddress); + + return String.format( + "redirect:/%s/assets/%s/%s/@postaladdress-edit", + getContentSectionLabel(), + getFolderPath(), + name + ); + } + + + @Override + public String getAssetType() { + return PostalAddress.class.getName(); + } + + @Override + public String getLabel() { + return globalizationHelper + .getLocalizedTextsUtil(getBundle()) + .getText("postal_address.label"); + } + + @Override + public String getDescription() { + return globalizationHelper + .getLocalizedTextsUtil(getBundle()) + .getText("postal_address.description"); + } + + @Override + public String getBundle() { + return MvcAssetStepsConstants.BUNDLE; + } + +} diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/PostalAddressEditStep.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/PostalAddressEditStep.java new file mode 100644 index 000000000..483eee044 --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/PostalAddressEditStep.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2021 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.ui.contentsections.assets; + +import org.librecms.assets.PostalAddress; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Named; +import javax.mvc.Controller; +import javax.ws.rs.Path; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +@Path(MvcAssetEditSteps.PATH_PREFIX + "postaladdress-edit") +@Controller +@Named("CmsPostalAddressEditStep") +@MvcAssetEditStepDef( + bundle = MvcAssetStepsConstants.BUNDLE, + descriptionKey = "postaladdress.editstep.description", + labelKey = "postaladdress.editstep.label", + supportedAssetType = PostalAddress.class +) +public class PostalAddressEditStep { + +} diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/AbstractMvcAuthoringStep.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/AbstractMvcAuthoringStep.java index 46446278f..4d4924fee 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/AbstractMvcAuthoringStep.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/AbstractMvcAuthoringStep.java @@ -18,6 +18,7 @@ */ package org.librecms.ui.contentsections.documents; +import org.librecms.ui.contentsections.ContentSectionNotFoundException; import org.hibernate.LazyInitializationException; import org.libreccm.l10n.GlobalizationHelper; import org.librecms.contentsection.ContentItem; @@ -87,7 +88,7 @@ public abstract class AbstractMvcAuthoringStep implements MvcAuthoringStep { private ContentItem document; private String documentPath; - + private String stepPath; /** @@ -109,7 +110,8 @@ public abstract class AbstractMvcAuthoringStep implements MvcAuthoringStep { sectionsUi.showContentSectionNotFound(sectionIdentifier), String.format( "ContentSection %s not found.", - sectionIdentifier) + sectionIdentifier + ) ) ); sectionModel.setSection(contentSection); @@ -119,9 +121,10 @@ public abstract class AbstractMvcAuthoringStep implements MvcAuthoringStep { .orElseThrow( () -> new DocumentNotFoundException( documentUi.showDocumentNotFound( - contentSection, documentPathParam), + contentSection, documentPathParam + ), String.format( - "Not document for path %s in section %s.", + "No document for path %s in section %s.", documentPathParam, contentSection.getLabel() ) @@ -129,8 +132,8 @@ public abstract class AbstractMvcAuthoringStep implements MvcAuthoringStep { ); documentModel.setContentItem(document); - this.documentPath = itemManager.getItemPath(document); - final Map values = new HashMap<>(); + documentPath = itemManager.getItemPath(document); + final Map values = new HashMap<>(); values.put( MvcAuthoringSteps.SECTION_IDENTIFIER_PATH_PARAM, contentSection.getLabel() @@ -356,11 +359,4 @@ public abstract class AbstractMvcAuthoringStep implements MvcAuthoringStep { .orElse(""); } - private String withoutLeadingSlash(final String path) { - if (path.startsWith("/")) { - return path.substring(1); - } else { - return path; - } - } } diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/CategorizationStep.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/CategorizationStep.java index ab05662cd..1eb481beb 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/CategorizationStep.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/CategorizationStep.java @@ -18,6 +18,7 @@ */ package org.librecms.ui.contentsections.documents; +import org.librecms.ui.contentsections.ContentSectionNotFoundException; import org.libreccm.api.Identifier; import org.libreccm.api.IdentifierParser; import org.libreccm.categorization.Category; diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/MvcAuthoringStep.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/MvcAuthoringStep.java index 32c7b599e..3d7dfc037 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/MvcAuthoringStep.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/MvcAuthoringStep.java @@ -18,6 +18,7 @@ */ package org.librecms.ui.contentsections.documents; +import org.librecms.ui.contentsections.ContentSectionNotFoundException; import org.librecms.contentsection.ContentItem; import org.librecms.contentsection.ContentSection; diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/PublishStep.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/PublishStep.java index 688c830f3..9b3098152 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/PublishStep.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/PublishStep.java @@ -18,6 +18,7 @@ */ package org.librecms.ui.contentsections.documents; +import org.librecms.ui.contentsections.ContentSectionNotFoundException; import org.libreccm.l10n.GlobalizationHelper; import org.librecms.contentsection.ContentItem; import org.librecms.contentsection.ContentItemManager; diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/RelatedInfoStep.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/RelatedInfoStep.java index 8ff1e939c..90665f5c9 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/RelatedInfoStep.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/RelatedInfoStep.java @@ -18,6 +18,7 @@ */ package org.librecms.ui.contentsections.documents; +import org.librecms.ui.contentsections.ContentSectionNotFoundException; import org.libreccm.api.Identifier; import org.libreccm.api.IdentifierParser; import org.libreccm.l10n.GlobalizationHelper; diff --git a/ccm-cms/src/main/java/org/librecms/ui/contenttypes/MvcArticleCreateStep.java b/ccm-cms/src/main/java/org/librecms/ui/contenttypes/MvcArticleCreateStep.java index 1b48c9cc3..eb53fddbc 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contenttypes/MvcArticleCreateStep.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contenttypes/MvcArticleCreateStep.java @@ -179,9 +179,9 @@ public class MvcArticleCreateStep || formParams.get(FORM_PARAM_NAME).length == 0) { addMessage( "danger", - globalizationHelper.getLocalizedTextsUtil( - getBundle() - ).getText("createstep.name.error.missing") + globalizationHelper + .getLocalizedTextsUtil(getBundle()) + .getText("createstep.name.error.missing") ); return showCreateStep(); } @@ -190,9 +190,9 @@ public class MvcArticleCreateStep if (!name.matches("^([a-zA-Z0-9_-]*)$")) { addMessage( "danger", - globalizationHelper.getLocalizedTextsUtil( - getBundle() - ).getText("createstep.name.error.invalid") + globalizationHelper + .getLocalizedTextsUtil(getBundle()) + .getText("createstep.name.error.invalid") ); return showCreateStep(); } @@ -202,9 +202,9 @@ public class MvcArticleCreateStep || formParams.get(FORM_PARAM_TITLE).length == 0) { addMessage( "danger", - globalizationHelper.getLocalizedTextsUtil( - getBundle() - ).getText("createstep.title.error.missing") + globalizationHelper + .getLocalizedTextsUtil(getBundle()) + .getText("createstep.title.error.missing") ); return showCreateStep(); } @@ -215,9 +215,9 @@ public class MvcArticleCreateStep || formParams.get(FORM_PARAM_SUMMARY).length == 0) { addMessage( "danger", - globalizationHelper.getLocalizedTextsUtil( - getBundle() - ).getText("createstep.summary.error.missing") + globalizationHelper + .getLocalizedTextsUtil(getBundle()) + .getText("createstep.summary.error.missing") ); return showCreateStep(); } @@ -292,6 +292,4 @@ public class MvcArticleCreateStep ); } - - } diff --git a/ccm-cms/src/main/java/org/librecms/ui/contenttypes/MvcArticlePropertiesStep.java b/ccm-cms/src/main/java/org/librecms/ui/contenttypes/MvcArticlePropertiesStep.java index 02d397372..8c375a299 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contenttypes/MvcArticlePropertiesStep.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contenttypes/MvcArticlePropertiesStep.java @@ -26,7 +26,7 @@ import org.librecms.contentsection.FolderManager; import org.librecms.contenttypes.Article; import org.librecms.ui.contentsections.ItemPermissionChecker; import org.librecms.ui.contentsections.documents.AbstractMvcAuthoringStep; -import org.librecms.ui.contentsections.documents.ContentSectionNotFoundException; +import org.librecms.ui.contentsections.ContentSectionNotFoundException; import org.librecms.ui.contentsections.documents.DocumentNotFoundException; import org.librecms.ui.contentsections.documents.DocumentUi; import org.librecms.ui.contentsections.documents.MvcAuthoringStepDef; diff --git a/ccm-cms/src/main/java/org/librecms/ui/contenttypes/MvcArticleTextBodyStep.java b/ccm-cms/src/main/java/org/librecms/ui/contenttypes/MvcArticleTextBodyStep.java index 6ac35fcf9..8ba577b10 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contenttypes/MvcArticleTextBodyStep.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contenttypes/MvcArticleTextBodyStep.java @@ -30,7 +30,7 @@ import org.librecms.contenttypes.Article; import org.librecms.ui.contentsections.ItemPermissionChecker; import org.librecms.ui.contentsections.documents.AbstractMvcAuthoringStep; import org.librecms.ui.contentsections.documents.CmsEditorLocaleVariantRow; -import org.librecms.ui.contentsections.documents.ContentSectionNotFoundException; +import org.librecms.ui.contentsections.ContentSectionNotFoundException; import org.librecms.ui.contentsections.documents.DocumentNotFoundException; import org.librecms.ui.contentsections.documents.DocumentUi; diff --git a/ccm-cms/src/main/resources/WEB-INF/views/org/librecms/ui/contentsection/assetfolder/assetfolder.xhtml b/ccm-cms/src/main/resources/WEB-INF/views/org/librecms/ui/contentsection/assetfolder/assetfolder.xhtml index 06287497b..c2069c121 100644 --- a/ccm-cms/src/main/resources/WEB-INF/views/org/librecms/ui/contentsection/assetfolder/assetfolder.xhtml +++ b/ccm-cms/src/main/resources/WEB-INF/views/org/librecms/ui/contentsection/assetfolder/assetfolder.xhtml @@ -188,10 +188,55 @@ - +