diff --git a/ccm-cms/src/main/java/org/librecms/assets/Bookmark.java b/ccm-cms/src/main/java/org/librecms/assets/Bookmark.java index 1be0ce6d7..9a2f7d3aa 100644 --- a/ccm-cms/src/main/java/org/librecms/assets/Bookmark.java +++ b/ccm-cms/src/main/java/org/librecms/assets/Bookmark.java @@ -34,8 +34,12 @@ import javax.persistence.JoinTable; import javax.persistence.Table; import org.hibernate.envers.Audited; -import org.hibernate.validator.constraints.NotEmpty; import org.libreccm.l10n.LocalizedString; +import org.librecms.ui.contentsections.assets.BookmarkCreateStep; +import org.librecms.ui.contentsections.assets.BookmarkEditStep; +import org.librecms.ui.contentsections.assets.MvcAssetEditKit; + +import javax.validation.constraints.NotEmpty; import static org.librecms.CmsConstants.*; import static org.librecms.assets.AssetConstants.*; @@ -54,6 +58,10 @@ import static org.librecms.assets.AssetConstants.*; @Entity @Table(name = "BOOKMARKS", schema = DB_SCHEMA) @Audited +@MvcAssetEditKit( + createStep = BookmarkCreateStep.class, + editStep = BookmarkEditStep.class +) public class Bookmark extends Asset implements Serializable { private static final long serialVersionUID = -2077380735104791483L; diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/BookmarkCreateStep.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/BookmarkCreateStep.java new file mode 100644 index 000000000..168f2ab8d --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/BookmarkCreateStep.java @@ -0,0 +1,127 @@ +/* + * 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.assets.Bookmark; +import org.librecms.contentsection.AssetRepository; + +import java.util.Locale; +import java.util.Map; +import java.util.Optional; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.inject.Named; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +@Named("CmsBookmarkCreateStep") +public class BookmarkCreateStep extends AbstractMvcAssetCreateStep { + + @Inject + private AssetRepository assetRepository; + + @Inject + private GlobalizationHelper globalizationHelper; + + private String bookmarkDescription; + + private String url; + + @Override + public String showCreateStep() { + return "org/librecms/ui/contentsection/assets/bookmark/create-bookmark.xhtml"; + } + + @Override + public String getLabel() { + return globalizationHelper + .getLocalizedTextsUtil(getBundle()) + .getText("bookmark.label"); + } + + @Override + public String getDescription() { + return globalizationHelper + .getLocalizedTextsUtil(getBundle()) + .getText("bookmark.description"); + } + + @Override + public String getBundle() { + return MvcAssetStepsConstants.BUNDLE; + } + + public String getUrl() { + return url; + } + + public String getBookmarkDescription() { + return bookmarkDescription; + } + + @Override + protected Class getAssetClass() { + return Bookmark.class; + } + + @Override + protected String setAssetProperties( + final Bookmark bookmark, final Map formParams + ) { + url = Optional + .ofNullable(formParams.get("url")) + .filter(value -> value.length > 0) + .map(value -> value[0]) + .orElse(""); + bookmarkDescription = Optional + .ofNullable(formParams.get("description")) + .filter(value -> value.length > 0) + .map(value -> value[0]) + .orElse(""); + + if (url.isEmpty() || url.matches("\\s*")) { + addMessage( + "warning", + globalizationHelper + .getLocalizedTextsUtil(getBundle()) + .getText("bookmark.create.url.missing") + ); + return showCreateStep(); + } + + bookmark.setUrl(url); + bookmark + .getDescription() + .addValue(new Locale(getInitialLocale()), bookmarkDescription); + assetRepository.save(bookmark); + + return String.format( + "redirect:/%s/assets/%s/%s/@bookmark-edit", + getContentSectionLabel(), + getFolderPath(), + getName() + ); + } + +} diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/BookmarkEditStep.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/BookmarkEditStep.java new file mode 100644 index 000000000..ffdb04e5f --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/BookmarkEditStep.java @@ -0,0 +1,301 @@ +/* + * 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.Bookmark; +import org.librecms.contentsection.AssetRepository; +import org.librecms.ui.contentsections.AssetPermissionsChecker; +import org.librecms.ui.contentsections.ContentSectionNotFoundException; + +import java.util.Locale; +import java.util.Set; +import java.util.stream.Collectors; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.mvc.Controller; +import javax.transaction.Transactional; +import javax.ws.rs.FormParam; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +@Path(MvcAssetEditSteps.PATH_PREFIX + "bookmark-edit") +@Controller +@MvcAssetEditStepDef( + bundle = MvcAssetStepsConstants.BUNDLE, + descriptionKey = "bookmark.editstep.description", + labelKey = "bookmark.editstep.label", + supportedAssetType = Bookmark.class +) +public class BookmarkEditStep extends AbstractMvcAssetEditStep { + + @Inject + private AssetStepsDefaultMessagesBundle messageBundle; + + @Inject + private AssetUi assetUi; + + @Inject + private AssetRepository assetRepository; + + @Inject + private GlobalizationHelper globalizationHelper; + + @Inject + private AssetPermissionsChecker assetPermissionsChecker; + + @Inject + private BookmarkEditStepModel bookmarkEditStepModel; + + @Override + public Class getStepClass() { + return BookmarkEditStep.class; + } + + @Override + protected void init() throws ContentSectionNotFoundException, + AssetNotFoundException { + super.init(); + + if (getAsset() instanceof Bookmark) { + bookmarkEditStepModel.setUrl(getBookmark().getUrl()); + + bookmarkEditStepModel.setDescriptionValues( + getBookmark() + .getDescription() + .getValues() + .entrySet() + .stream() + .collect( + Collectors.toMap( + entry -> entry.getKey().toString(), + entry -> entry.getValue() + ) + ) + ); + + final Set descriptionLocales = getBookmark() + .getDescription() + .getAvailableLocales(); + bookmarkEditStepModel.setUnusedDescriptionLocales( + globalizationHelper + .getAvailableLocales() + .stream() + .filter(locale -> !descriptionLocales.contains(locale)) + .map(Locale::toString) + .collect(Collectors.toList()) + ); + } else { + throw new AssetNotFoundException( + assetUi.showAssetNotFound( + getContentSection(), getAssetPath() + ), + String.format( + "No asset for path %s found in section %s.", + getAssetPath(), + getContentSection().getLabel() + ) + ); + } + } + + @GET + @Path("/") + @AuthorizationRequired + @Transactional(Transactional.TxType.REQUIRED) + @Override + public String showStep( + @PathParam(MvcAssetEditSteps.SECTION_IDENTIFIER_PATH_PARAM) + final String sectionIdentifier, + @PathParam(MvcAssetEditSteps.ASSET_PATH_PATH_PARAM_NAME) + final String assetPath + ) { + try { + init(); + } catch (ContentSectionNotFoundException ex) { + return ex.showErrorMessage(); + } catch (AssetNotFoundException ex) { + return ex.showErrorMessage(); + } + + if (assetPermissionsChecker.canEditAsset(getAsset())) { + return "org/librecms/ui/contentsection/bookmark/bookmark/edit-bookmark.xhtml"; + } else { + return assetUi.showAccessDenied( + getContentSection(), + getAsset(), + messageBundle.get("asset.edit.denied")); + } + } + + public Bookmark getBookmark() { + return (Bookmark) getAsset(); + } + + @POST + @Path("/properties") + @AuthorizationRequired + @Transactional(Transactional.TxType.REQUIRED) + public String updateProperties( + @PathParam(MvcAssetEditSteps.SECTION_IDENTIFIER_PATH_PARAM) + final String sectionIdentifier, + @PathParam(MvcAssetEditSteps.ASSET_PATH_PATH_PARAM_NAME) + final String assetPath, + @FormParam("url") final String url + ) { + try { + init(); + } catch (ContentSectionNotFoundException ex) { + return ex.showErrorMessage(); + } catch (AssetNotFoundException ex) { + return ex.showErrorMessage(); + } + + if (assetPermissionsChecker.canEditAsset(getAsset())) { + final Bookmark bookmark = getBookmark(); + bookmark.setUrl(url); + + assetRepository.save(bookmark); + + return buildRedirectPathForStep(); + } else { + return assetUi.showAccessDenied( + getContentSection(), + getAsset(), + messageBundle.get("asset.edit.denied")); + } + } + + @POST + @Path("/description/add") + @AuthorizationRequired + @Transactional(Transactional.TxType.REQUIRED) + public String addDescription( + @PathParam(MvcAssetEditSteps.SECTION_IDENTIFIER_PATH_PARAM) + final String sectionIdentifier, + @PathParam(MvcAssetEditSteps.ASSET_PATH_PATH_PARAM_NAME) + final String assetPath, + @FormParam("locale") final String localeParam, + @FormParam("value") final String value + ) { + try { + init(); + } catch (ContentSectionNotFoundException ex) { + return ex.showErrorMessage(); + } catch (AssetNotFoundException ex) { + return ex.showErrorMessage(); + } + + if (assetPermissionsChecker.canEditAsset(getAsset())) { + final Locale locale = new Locale(localeParam); + final Bookmark bookmark = getBookmark(); + bookmark.getDescription().addValue(locale, value); + + assetRepository.save(bookmark); + + return buildRedirectPathForStep(); + } else { + return assetUi.showAccessDenied( + getContentSection(), + getAsset(), + messageBundle.get("asset.edit.denied")); + } + } + + @POST + @Path("/description/edit/{locale}") + @AuthorizationRequired + @Transactional(Transactional.TxType.REQUIRED) + public String editDescription( + @PathParam(MvcAssetEditSteps.SECTION_IDENTIFIER_PATH_PARAM) + final String sectionIdentifier, + @PathParam(MvcAssetEditSteps.ASSET_PATH_PATH_PARAM_NAME) + final String assetPath, + @PathParam("locale") final String localeParam, + @FormParam("value") final String value + ) { + try { + init(); + } catch (ContentSectionNotFoundException ex) { + return ex.showErrorMessage(); + } catch (AssetNotFoundException ex) { + return ex.showErrorMessage(); + } + + if (assetPermissionsChecker.canEditAsset(getAsset())) { + final Locale locale = new Locale(localeParam); + final Bookmark bookmark = getBookmark(); + bookmark.getDescription().addValue(locale, value); + + assetRepository.save(bookmark); + + return buildRedirectPathForStep(); + } else { + return assetUi.showAccessDenied( + getContentSection(), + getAsset(), + messageBundle.get("asset.edit.denied")); + } + } + + @POST + @Path("/description/remove/{locale}") + @AuthorizationRequired + @Transactional(Transactional.TxType.REQUIRED) + public String removeDescription( + @PathParam(MvcAssetEditSteps.SECTION_IDENTIFIER_PATH_PARAM) + final String sectionIdentifier, + @PathParam(MvcAssetEditSteps.ASSET_PATH_PATH_PARAM_NAME) + final String assetPath, + @PathParam("locale") final String localeParam + ) { + try { + init(); + } catch (ContentSectionNotFoundException ex) { + return ex.showErrorMessage(); + } catch (AssetNotFoundException ex) { + return ex.showErrorMessage(); + } + + if (assetPermissionsChecker.canEditAsset(getAsset())) { + final Locale locale = new Locale(localeParam); + final Bookmark bookmark = getBookmark(); + bookmark.getDescription().removeValue(locale); + + assetRepository.save(bookmark); + + return buildRedirectPathForStep(); + } else { + return assetUi.showAccessDenied( + getContentSection(), + getAsset(), + messageBundle.get("asset.edit.denied")); + } + } + +} diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/BookmarkEditStepModel.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/BookmarkEditStepModel.java new file mode 100644 index 000000000..2c720acea --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/BookmarkEditStepModel.java @@ -0,0 +1,74 @@ +/* + * 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 java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Named; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +@Named("CmsBookmarkEditStep") +public class BookmarkEditStepModel { + + private String url; + + private Map descriptionValues; + + private List unusedDescriptionLocales; + + public String getUrl() { + return url; + } + + protected void setUrl(final String url) { + this.url = url; + } + + public Map getDescriptionValues() { + return Collections.unmodifiableMap(descriptionValues); + } + + protected void setDescriptionValues( + final Map descriptionValues + ) { + this.descriptionValues = new HashMap<>(descriptionValues); + } + + public List getUnusedDescriptionLocales() { + return Collections.unmodifiableList(unusedDescriptionLocales); + } + + protected void setUnusedDescriptionLocales( + final List unusedDescriptionLocales + ) { + this.unusedDescriptionLocales = new ArrayList<>( + unusedDescriptionLocales + ); + } + +} diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/CmsAssetEditSteps.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/CmsAssetEditSteps.java index 68ff6730b..1c5c674ab 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/CmsAssetEditSteps.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/assets/CmsAssetEditSteps.java @@ -31,6 +31,7 @@ public class CmsAssetEditSteps implements MvcAssetEditSteps { public Set> getClasses() { final Set> classes = new HashSet<>(); + classes.add(BookmarkEditStep.class); classes.add(LegalMetadataEditStep.class); classes.add(PostalAddressEditStep.class); classes.add(SideNoteEditStep.class); diff --git a/ccm-cms/src/main/resources/WEB-INF/views/org/librecms/ui/contentsection/assets/bookmark/create-bookmark.xhtml b/ccm-cms/src/main/resources/WEB-INF/views/org/librecms/ui/contentsection/assets/bookmark/create-bookmark.xhtml new file mode 100644 index 000000000..62bfc4f11 --- /dev/null +++ b/ccm-cms/src/main/resources/WEB-INF/views/org/librecms/ui/contentsection/assets/bookmark/create-bookmark.xhtml @@ -0,0 +1,80 @@ +]> + + + + +
+

#{CmsAssetsStepsDefaultMessagesBundle["bookmark.createform.title"]}

+ + + + + +
+ + + + + + + + + + + + #{CmsAssetsStepsDefaultMessagesBundle['createform.cancel']} + + + + + +
+
+ +
+ \ No newline at end of file diff --git a/ccm-cms/src/main/resources/WEB-INF/views/org/librecms/ui/contentsection/assets/bookmark/edit-bookmark.xhtml b/ccm-cms/src/main/resources/WEB-INF/views/org/librecms/ui/contentsection/assets/bookmark/edit-bookmark.xhtml new file mode 100644 index 000000000..acd56e077 --- /dev/null +++ b/ccm-cms/src/main/resources/WEB-INF/views/org/librecms/ui/contentsection/assets/bookmark/edit-bookmark.xhtml @@ -0,0 +1,116 @@ +]> + + + + + + + + +

#{CmsAssetsStepsDefaultMessagesBundle['bookmark.editstep.url.header']}

+ + +
+ +
+ +
+
#{CmsBookmarkEditStep.url}
+ + + +
+ +
+ +