From d2ddfac6e8b6f25cb48c7f6bcd0878739880c0ca Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Mon, 1 Mar 2021 19:51:24 +0100 Subject: [PATCH] Some refactoring Former-commit-id: d24f46e0088ec14a1c887b41f74a629dd968a305 --- .../AssetFolderController.java | 146 +++++++++--------- .../contentsections/CategoriesController.java | 28 ++-- .../ui/contentsections/ContentSectionsUi.java | 123 +++++++++++++++ .../ui/contentsections/RetrieveResult.java | 76 ++++++++- 4 files changed, 280 insertions(+), 93 deletions(-) create mode 100644 ccm-cms/src/main/java/org/librecms/ui/contentsections/ContentSectionsUi.java 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 35bded472..15d5cec62 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 @@ -7,8 +7,6 @@ package org.librecms.ui.contentsections; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.libreccm.api.Identifier; -import org.libreccm.api.IdentifierParser; import org.libreccm.l10n.GlobalizationHelper; import org.libreccm.security.AuthorizationRequired; import org.libreccm.security.PermissionChecker; @@ -48,6 +46,7 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; + /** * * @author Jens Pelzetter @@ -78,6 +77,9 @@ public class AssetFolderController { @Inject private ContentSectionRepository sectionRepo; + @Inject + private ContentSectionsUi sectionsUi; + @Inject private CurrentUserAssetPermissions currentUserPermissions; @@ -93,9 +95,6 @@ public class AssetFolderController { @Inject private GrantedAssetPrivileges grantedPrivileges; - @Inject - private IdentifierParser identifierParser; - @Inject private Models models; @@ -104,7 +103,7 @@ public class AssetFolderController { @Inject private PermissionManager permissionManager; - + @Inject private RoleRepository roleRepo; @@ -138,21 +137,18 @@ public class AssetFolderController { @QueryParam("firstResult") @DefaultValue("0") final int firstResult, @QueryParam("maxResults") @DefaultValue("20") final int maxResults ) { - final Optional sectionResult = retrieveContentSection( - sectionIdentifier - ); + final Optional sectionResult = sectionsUi + .findContentSection(sectionIdentifier); if (!sectionResult.isPresent()) { - models.put("sectionIdentifier", sectionIdentifier); - return "org/librecms/ui/contentsection/contentsection-not-found.xhtml"; + return sectionsUi.showContentSectionNotFound(sectionIdentifier); } - final ContentSection section = sectionResult.get(); + if (!permissionChecker.isPermitted( AssetPrivileges.EDIT, section.getRootAssetsFolder() )) { - models.put("sectionidentifier", sectionIdentifier); - return "org/librecms/ui/contentsection/access-denied.xhtml"; + sectionsUi.showAccessDenied("sectionIdentifier", sectionIdentifier); } contentSectionModel.setSection(section); @@ -170,19 +166,17 @@ public class AssetFolderController { ); if (folderResult.isPresent()) { folder = folderResult.get(); - assetFolderModel.setBreadcrumbs(buildBreadcrumbs(folderPath)); } else { - models.put("contentSection", section.getLabel()); - models.put("folderPath", folderPath); - return "org/librecms/ui/contentsection/assetfolder/asssetfolder-not-found.xhtml"; + return showAssetFolderNotFound(section, folderPath); } } if (!permissionChecker.isPermitted(AssetPrivileges.EDIT, folder)) { - models.put("sectionidentifier", sectionIdentifier); - models.put("folderPath", folderPath); - return "org/librecms/ui/contentsection/access-denied.xhtml"; + return sectionsUi.showAccessDenied( + "sectionIdentifier", sectionIdentifier, + "folderPath", folderPath + ); } final List folderEntries = folderRepo @@ -246,21 +240,22 @@ public class AssetFolderController { @PathParam("parentFolderPath") final String parentFolderPath, @FormParam("folderName") final String folderName ) { - final Optional sectionResult = retrieveContentSection( - sectionIdentifier - ); + final RetrieveResult sectionResult = sectionsUi + .retrieveContentSection(sectionIdentifier); - if (!sectionResult.isPresent()) { - models.put("sectionIdentifier", sectionIdentifier); - return "org/librecms/ui/contentsection/contentsection-not-found.xhtml"; + final ContentSection section; + if (sectionResult.isSuccessful()) { + section = sectionResult.getResult(); + } else { + return sectionResult.getFailedResponseTemplate(); } - final ContentSection section = sectionResult.get(); if (!permissionChecker.isPermitted( AssetPrivileges.EDIT, section.getRootAssetsFolder() )) { - models.put("sectionidentifier", sectionIdentifier); - return "org/librecms/ui/contentsection/access-denied.xhtml"; + return sectionsUi.showAccessDenied( + "sectionIdentifier", sectionIdentifier + ); } final Folder parentFolder; @@ -285,9 +280,10 @@ public class AssetFolderController { if (!permissionChecker.isPermitted( AssetPrivileges.CREATE_NEW, parentFolder )) { - models.put("sectionidentifier", sectionIdentifier); - models.put("folderPath", parentFolderPath); - return "org/librecms/ui/contentsection/access-denied.xhtml"; + return sectionsUi.showAccessDenied( + "sectionIdentifier", sectionIdentifier, + "folderPath", parentFolderPath + ); } folderManager.createFolder(folderName, parentFolder); @@ -323,15 +319,15 @@ public class AssetFolderController { @PathParam("role") final String roleParam, @FormParam("permissions") final List permissions ) { - final Optional sectionResult = retrieveContentSection( - sectionIdentifier - ); - if (!sectionResult.isPresent()) { - models.put("sectionIdentifier", sectionIdentifier); - return "org/librecms/ui/contentsection/contentsection-not-found.xhtml"; - } - final ContentSection section = sectionResult.get(); + final RetrieveResult sectionResult = sectionsUi + .retrieveContentSection(sectionIdentifier); + final ContentSection section; + if (sectionResult.isSuccessful()) { + section = sectionResult.getResult(); + } else { + return sectionResult.getFailedResponseTemplate(); + } if (!permissionChecker.isPermitted( AssetPrivileges.EDIT, section.getRootAssetsFolder() )) { @@ -409,15 +405,14 @@ public class AssetFolderController { @PathParam("folderPath") final String folderPath, @FormParam("folderName") final String folderName ) { - final Optional sectionResult = retrieveContentSection( - sectionIdentifier - ); - if (!sectionResult.isPresent()) { - models.put("sectionIdentifier", sectionIdentifier); - return "org/librecms/ui/contentsection/contentsection-not-found.xhtml"; + final RetrieveResult sectionResult = sectionsUi + .retrieveContentSection(sectionIdentifier); + final ContentSection section; + if (sectionResult.isSuccessful()) { + section = sectionResult.getResult(); + } else { + return sectionResult.getFailedResponseTemplate(); } - - final ContentSection section = sectionResult.get(); if (!permissionChecker.isPermitted( AssetPrivileges.EDIT, section.getRootAssetsFolder() )) { @@ -464,30 +459,37 @@ public class AssetFolderController { ); } - private Optional retrieveContentSection( - final String sectionIdentifier +// private Optional retrieveContentSection( +// final String sectionIdentifier +// ) { +// final Identifier identifier = identifierParser.parseIdentifier( +// sectionIdentifier +// ); +// +// final Optional sectionResult; +// switch (identifier.getType()) { +// case ID: +// sectionResult = sectionRepo.findById( +// Long.parseLong(identifier.getIdentifier()) +// ); +// break; +// case UUID: +// sectionResult = sectionRepo.findByUuid(identifier +// .getIdentifier()); +// break; +// default: +// sectionResult = sectionRepo.findByLabel(identifier +// .getIdentifier()); +// break; +// } +// return sectionResult; +// } + private String showAssetFolderNotFound( + final ContentSection section, final String folderPath ) { - final Identifier identifier = identifierParser.parseIdentifier( - sectionIdentifier - ); - - final Optional sectionResult; - switch (identifier.getType()) { - case ID: - sectionResult = sectionRepo.findById( - Long.parseLong(identifier.getIdentifier()) - ); - break; - case UUID: - sectionResult = sectionRepo.findByUuid(identifier - .getIdentifier()); - break; - default: - sectionResult = sectionRepo.findByLabel(identifier - .getIdentifier()); - break; - } - return sectionResult; + models.put("contentSection", section.getLabel()); + models.put("folderPath", folderPath); + return "org/librecms/ui/contentsection/assetfolder/asssetfolder-not-found.xhtml"; } private List buildBreadcrumbs( diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/CategoriesController.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/CategoriesController.java index a1f3643d7..10c12d074 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/CategoriesController.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/CategoriesController.java @@ -251,7 +251,7 @@ public class CategoriesController { categoryPathParam ); } else { - return result.getResponseTemplate(); + return result.getFailedResponseTemplate(); } } @@ -303,7 +303,7 @@ public class CategoriesController { categoryPathParam ); } else { - return result.getResponseTemplate(); + return result.getFailedResponseTemplate(); } } @@ -352,7 +352,7 @@ public class CategoriesController { categoryPathParam ); } else { - return result.getResponseTemplate(); + return result.getFailedResponseTemplate(); } } @@ -404,7 +404,7 @@ public class CategoriesController { categoryPathParam ); } else { - return result.getResponseTemplate(); + return result.getFailedResponseTemplate(); } } @@ -458,7 +458,7 @@ public class CategoriesController { categoryPathParam ); } else { - return result.getResponseTemplate(); + return result.getFailedResponseTemplate(); } } @@ -511,7 +511,7 @@ public class CategoriesController { categoryPathParam ); } else { - return result.getResponseTemplate(); + return result.getFailedResponseTemplate(); } } @@ -549,7 +549,7 @@ public class CategoriesController { categoryName ); } else { - return result.getResponseTemplate(); + return result.getFailedResponseTemplate(); } } @@ -604,7 +604,7 @@ public class CategoriesController { categoryPath ); } else { - return result.getResponseTemplate(); + return result.getFailedResponseTemplate(); } } @@ -630,7 +630,7 @@ public class CategoriesController { categoryPath ); } else { - return result.getResponseTemplate(); + return result.getFailedResponseTemplate(); } } @@ -693,7 +693,7 @@ public class CategoriesController { categoryPath ); } else { - return result.getResponseTemplate(); + return result.getFailedResponseTemplate(); } } @@ -730,7 +730,7 @@ public class CategoriesController { categoryManager.getCategoryPath(parentCategory) ); } else { - return result.getResponseTemplate(); + return result.getFailedResponseTemplate(); } } @@ -790,7 +790,7 @@ public class CategoriesController { categoryManager.getCategoryPath(target) ); } else { - return result.getResponseTemplate(); + return result.getFailedResponseTemplate(); } } @@ -851,7 +851,7 @@ public class CategoriesController { pathFragment ); } else { - return result.getResponseTemplate(); + return result.getFailedResponseTemplate(); } } @@ -919,7 +919,7 @@ public class CategoriesController { categoryManager.getCategoryPath(category) ); } else { - return result.getResponseTemplate(); + return result.getFailedResponseTemplate(); } } diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/ContentSectionsUi.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/ContentSectionsUi.java new file mode 100644 index 000000000..0bc6cfb89 --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/ContentSectionsUi.java @@ -0,0 +1,123 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.librecms.ui.contentsections; + +import org.libreccm.api.Identifier; +import org.libreccm.api.IdentifierParser; +import org.librecms.contentsection.ContentSection; +import org.librecms.contentsection.ContentSectionRepository; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.mvc.Models; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +class ContentSectionsUi { + + @Inject + private ContentSectionRepository sectionRepo; + + @Inject + private IdentifierParser identifierParser; + + private Models models; + + public Optional findContentSection( + final String identifierParam + ) { + final Identifier sectionIdentifier = identifierParser.parseIdentifier( + identifierParam + ); + + switch (sectionIdentifier.getType()) { + case ID: + return sectionRepo.findById( + Long.parseLong( + sectionIdentifier.getIdentifier() + ) + ); + case UUID: + return sectionRepo.findByUuid( + sectionIdentifier.getIdentifier() + ); + default: + return sectionRepo.findByLabel( + sectionIdentifier.getIdentifier() + ); + } + } + + public String showAccessDenied(final String... identifiers) { + if (identifiers.length % 2 != 0) { + throw new IllegalArgumentException( + "The length of the identifiers must be even." + ); + } + for (int i = 1; i < identifiers.length; i = +2) { + models.put(identifiers[i - 1], identifiers[i]); + } + return "org/librecms/ui/contentsection/contentsection-not-found.xhtml"; + } + + public String showAccessDenied(final Map identifiers) { + for (final Map.Entry entry : identifiers.entrySet()) { + models.put(entry.getKey(), entry.getValue()); + } + return "org/librecms/ui/contentsection/access-denied.xhtml"; + } + + public String showContentSectionNotFound(final String sectionIdentifier) { + models.put("sectionIdentifier", sectionIdentifier); + return "org/librecms/ui/contentsection/contentsection-not-found.xhtml"; + } + + public RetrieveResult retrieveContentSection( + final String identifierParam + ) { + final Identifier sectionIdentifier = identifierParser.parseIdentifier( + identifierParam + ); + + final Optional sectionResult; + switch (sectionIdentifier.getType()) { + case ID: + sectionResult = sectionRepo.findById( + Long.parseLong( + sectionIdentifier.getIdentifier() + ) + ); + break; + case UUID: + sectionResult = sectionRepo.findByUuid( + sectionIdentifier.getIdentifier() + ); + break; + default: + sectionResult = sectionRepo.findByLabel( + sectionIdentifier.getIdentifier() + ); + break; + } + + if (sectionResult.isPresent()) { + return RetrieveResult.successful(sectionResult.get()); + } else { + models.put("sectionIdentifier", sectionIdentifier); + return RetrieveResult.failed( + "org/librecms/ui/contentsection/contentsection-not-found.xhtml" + ); + } + } + +} diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/RetrieveResult.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/RetrieveResult.java index e83091d5a..ad5b722bf 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/RetrieveResult.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/RetrieveResult.java @@ -8,22 +8,44 @@ package org.librecms.ui.contentsections; import java.util.Objects; /** + * This class can be used by controller methods to either return an entity, for + * example a content section, or the path to an error response if the identity + * for the provided identifier was not found. * * @author Jens Pelzetter - * @param + * @param The entity class contained by an instance of this class if it was + * retrived successfully. */ public class RetrieveResult { + /** + * The retrieved entity if the it was retrieved successfully. + */ private T result; - private String responseTemplate; + /** + * The path of the response template to show when the entity was no + * retrived successfully. + */ + private String failedResponseTemplate; + /** + * Indicates if the entity was retrieved succesfully. + */ private boolean successful; private RetrieveResult() { } + /** + * Creates a successful {@code RetrieveResult} instance. + * + * @param The type of the retrieved entity. + * @param result The retrieved entity. Can't be null. + * + * @return The new {@code RetrieveResult}. + */ public static RetrieveResult successful(final R result) { final RetrieveResult retrieveResult = new RetrieveResult<>(); retrieveResult.setResult(Objects.requireNonNull(result)); @@ -31,35 +53,75 @@ public class RetrieveResult { return retrieveResult; } + /** + * Creates a failed {@code RetrieveResult} instance. + * + * @param The type of the entity to retrieve. + * @param responseTemplate The template to show. + * + * @return The new {@code RetrieveResult}. + */ public static RetrieveResult failed(final String responseTemplate) { final RetrieveResult retrieveResult = new RetrieveResult<>(); - retrieveResult.setResponseTemplate( + retrieveResult.setFailedResponseTemplate( Objects.requireNonNull(responseTemplate) ); retrieveResult.setSuccessful(false); return retrieveResult; } + /** + * Gets the result. + * + * @return The retrieved entity. + */ public T getResult() { return result; } + /** + * Private setter for the result entity. + * + * @param result The retrieved entity. + */ private void setResult(final T result) { this.result = result; } - public String getResponseTemplate() { - return responseTemplate; + /** + * Gets the failed result template. + * + * @return The path of the template to show. + */ + public String getFailedResponseTemplate() { + return failedResponseTemplate; } - private void setResponseTemplate(final String responseTemplate) { - this.responseTemplate = responseTemplate; + /** + * Private setter for the failed response template. + * + * @param failedResponseTemplate Path of the template to show. + */ + private void setFailedResponseTemplate(final String failedResponseTemplate) { + this.failedResponseTemplate = failedResponseTemplate; } + /** + * Indicates if the entity was retrieve succesfully. + * + * @return {@code true} if the enity was retrieved successfully, + * {@code false} otherwise. + */ public boolean isSuccessful() { return successful; } + /** + * Private setter for {@link #successful}. + * + * @param successful {@code true} if the enity was retrieved successfully, + * {@code false} otherwise. + */ private void setSuccessful(final boolean successful) { this.successful = successful; }