Some refactoring

Former-commit-id: d24f46e0088ec14a1c887b41f74a629dd968a305
pull/10/head
Jens Pelzetter 2021-03-01 19:51:24 +01:00
parent 84949ba099
commit d2ddfac6e8
4 changed files with 280 additions and 93 deletions

View File

@ -7,8 +7,6 @@ package org.librecms.ui.contentsections;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.libreccm.api.Identifier;
import org.libreccm.api.IdentifierParser;
import org.libreccm.l10n.GlobalizationHelper; import org.libreccm.l10n.GlobalizationHelper;
import org.libreccm.security.AuthorizationRequired; import org.libreccm.security.AuthorizationRequired;
import org.libreccm.security.PermissionChecker; import org.libreccm.security.PermissionChecker;
@ -48,6 +46,7 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
/** /**
* *
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a> * @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
@ -78,6 +77,9 @@ public class AssetFolderController {
@Inject @Inject
private ContentSectionRepository sectionRepo; private ContentSectionRepository sectionRepo;
@Inject
private ContentSectionsUi sectionsUi;
@Inject @Inject
private CurrentUserAssetPermissions currentUserPermissions; private CurrentUserAssetPermissions currentUserPermissions;
@ -93,9 +95,6 @@ public class AssetFolderController {
@Inject @Inject
private GrantedAssetPrivileges grantedPrivileges; private GrantedAssetPrivileges grantedPrivileges;
@Inject
private IdentifierParser identifierParser;
@Inject @Inject
private Models models; private Models models;
@ -104,7 +103,7 @@ public class AssetFolderController {
@Inject @Inject
private PermissionManager permissionManager; private PermissionManager permissionManager;
@Inject @Inject
private RoleRepository roleRepo; private RoleRepository roleRepo;
@ -138,21 +137,18 @@ public class AssetFolderController {
@QueryParam("firstResult") @DefaultValue("0") final int firstResult, @QueryParam("firstResult") @DefaultValue("0") final int firstResult,
@QueryParam("maxResults") @DefaultValue("20") final int maxResults @QueryParam("maxResults") @DefaultValue("20") final int maxResults
) { ) {
final Optional<ContentSection> sectionResult = retrieveContentSection(
sectionIdentifier
);
final Optional<ContentSection> sectionResult = sectionsUi
.findContentSection(sectionIdentifier);
if (!sectionResult.isPresent()) { if (!sectionResult.isPresent()) {
models.put("sectionIdentifier", sectionIdentifier); return sectionsUi.showContentSectionNotFound(sectionIdentifier);
return "org/librecms/ui/contentsection/contentsection-not-found.xhtml";
} }
final ContentSection section = sectionResult.get(); final ContentSection section = sectionResult.get();
if (!permissionChecker.isPermitted( if (!permissionChecker.isPermitted(
AssetPrivileges.EDIT, section.getRootAssetsFolder() AssetPrivileges.EDIT, section.getRootAssetsFolder()
)) { )) {
models.put("sectionidentifier", sectionIdentifier); sectionsUi.showAccessDenied("sectionIdentifier", sectionIdentifier);
return "org/librecms/ui/contentsection/access-denied.xhtml";
} }
contentSectionModel.setSection(section); contentSectionModel.setSection(section);
@ -170,19 +166,17 @@ public class AssetFolderController {
); );
if (folderResult.isPresent()) { if (folderResult.isPresent()) {
folder = folderResult.get(); folder = folderResult.get();
assetFolderModel.setBreadcrumbs(buildBreadcrumbs(folderPath)); assetFolderModel.setBreadcrumbs(buildBreadcrumbs(folderPath));
} else { } else {
models.put("contentSection", section.getLabel()); return showAssetFolderNotFound(section, folderPath);
models.put("folderPath", folderPath);
return "org/librecms/ui/contentsection/assetfolder/asssetfolder-not-found.xhtml";
} }
} }
if (!permissionChecker.isPermitted(AssetPrivileges.EDIT, folder)) { if (!permissionChecker.isPermitted(AssetPrivileges.EDIT, folder)) {
models.put("sectionidentifier", sectionIdentifier); return sectionsUi.showAccessDenied(
models.put("folderPath", folderPath); "sectionIdentifier", sectionIdentifier,
return "org/librecms/ui/contentsection/access-denied.xhtml"; "folderPath", folderPath
);
} }
final List<AssetFolderEntry> folderEntries = folderRepo final List<AssetFolderEntry> folderEntries = folderRepo
@ -246,21 +240,22 @@ public class AssetFolderController {
@PathParam("parentFolderPath") final String parentFolderPath, @PathParam("parentFolderPath") final String parentFolderPath,
@FormParam("folderName") final String folderName @FormParam("folderName") final String folderName
) { ) {
final Optional<ContentSection> sectionResult = retrieveContentSection( final RetrieveResult<ContentSection> sectionResult = sectionsUi
sectionIdentifier .retrieveContentSection(sectionIdentifier);
);
if (!sectionResult.isPresent()) { final ContentSection section;
models.put("sectionIdentifier", sectionIdentifier); if (sectionResult.isSuccessful()) {
return "org/librecms/ui/contentsection/contentsection-not-found.xhtml"; section = sectionResult.getResult();
} else {
return sectionResult.getFailedResponseTemplate();
} }
final ContentSection section = sectionResult.get();
if (!permissionChecker.isPermitted( if (!permissionChecker.isPermitted(
AssetPrivileges.EDIT, section.getRootAssetsFolder() AssetPrivileges.EDIT, section.getRootAssetsFolder()
)) { )) {
models.put("sectionidentifier", sectionIdentifier); return sectionsUi.showAccessDenied(
return "org/librecms/ui/contentsection/access-denied.xhtml"; "sectionIdentifier", sectionIdentifier
);
} }
final Folder parentFolder; final Folder parentFolder;
@ -285,9 +280,10 @@ public class AssetFolderController {
if (!permissionChecker.isPermitted( if (!permissionChecker.isPermitted(
AssetPrivileges.CREATE_NEW, parentFolder AssetPrivileges.CREATE_NEW, parentFolder
)) { )) {
models.put("sectionidentifier", sectionIdentifier); return sectionsUi.showAccessDenied(
models.put("folderPath", parentFolderPath); "sectionIdentifier", sectionIdentifier,
return "org/librecms/ui/contentsection/access-denied.xhtml"; "folderPath", parentFolderPath
);
} }
folderManager.createFolder(folderName, parentFolder); folderManager.createFolder(folderName, parentFolder);
@ -323,15 +319,15 @@ public class AssetFolderController {
@PathParam("role") final String roleParam, @PathParam("role") final String roleParam,
@FormParam("permissions") final List<String> permissions @FormParam("permissions") final List<String> permissions
) { ) {
final Optional<ContentSection> 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<ContentSection> sectionResult = sectionsUi
.retrieveContentSection(sectionIdentifier);
final ContentSection section;
if (sectionResult.isSuccessful()) {
section = sectionResult.getResult();
} else {
return sectionResult.getFailedResponseTemplate();
}
if (!permissionChecker.isPermitted( if (!permissionChecker.isPermitted(
AssetPrivileges.EDIT, section.getRootAssetsFolder() AssetPrivileges.EDIT, section.getRootAssetsFolder()
)) { )) {
@ -409,15 +405,14 @@ public class AssetFolderController {
@PathParam("folderPath") final String folderPath, @PathParam("folderPath") final String folderPath,
@FormParam("folderName") final String folderName @FormParam("folderName") final String folderName
) { ) {
final Optional<ContentSection> sectionResult = retrieveContentSection( final RetrieveResult<ContentSection> sectionResult = sectionsUi
sectionIdentifier .retrieveContentSection(sectionIdentifier);
); final ContentSection section;
if (!sectionResult.isPresent()) { if (sectionResult.isSuccessful()) {
models.put("sectionIdentifier", sectionIdentifier); section = sectionResult.getResult();
return "org/librecms/ui/contentsection/contentsection-not-found.xhtml"; } else {
return sectionResult.getFailedResponseTemplate();
} }
final ContentSection section = sectionResult.get();
if (!permissionChecker.isPermitted( if (!permissionChecker.isPermitted(
AssetPrivileges.EDIT, section.getRootAssetsFolder() AssetPrivileges.EDIT, section.getRootAssetsFolder()
)) { )) {
@ -464,30 +459,37 @@ public class AssetFolderController {
); );
} }
private Optional<ContentSection> retrieveContentSection( // private Optional<ContentSection> retrieveContentSection(
final String sectionIdentifier // final String sectionIdentifier
// ) {
// final Identifier identifier = identifierParser.parseIdentifier(
// sectionIdentifier
// );
//
// final Optional<ContentSection> 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( models.put("contentSection", section.getLabel());
sectionIdentifier models.put("folderPath", folderPath);
); return "org/librecms/ui/contentsection/assetfolder/asssetfolder-not-found.xhtml";
final Optional<ContentSection> 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 List<FolderBreadcrumbsModel> buildBreadcrumbs( private List<FolderBreadcrumbsModel> buildBreadcrumbs(

View File

@ -251,7 +251,7 @@ public class CategoriesController {
categoryPathParam categoryPathParam
); );
} else { } else {
return result.getResponseTemplate(); return result.getFailedResponseTemplate();
} }
} }
@ -303,7 +303,7 @@ public class CategoriesController {
categoryPathParam categoryPathParam
); );
} else { } else {
return result.getResponseTemplate(); return result.getFailedResponseTemplate();
} }
} }
@ -352,7 +352,7 @@ public class CategoriesController {
categoryPathParam categoryPathParam
); );
} else { } else {
return result.getResponseTemplate(); return result.getFailedResponseTemplate();
} }
} }
@ -404,7 +404,7 @@ public class CategoriesController {
categoryPathParam categoryPathParam
); );
} else { } else {
return result.getResponseTemplate(); return result.getFailedResponseTemplate();
} }
} }
@ -458,7 +458,7 @@ public class CategoriesController {
categoryPathParam categoryPathParam
); );
} else { } else {
return result.getResponseTemplate(); return result.getFailedResponseTemplate();
} }
} }
@ -511,7 +511,7 @@ public class CategoriesController {
categoryPathParam categoryPathParam
); );
} else { } else {
return result.getResponseTemplate(); return result.getFailedResponseTemplate();
} }
} }
@ -549,7 +549,7 @@ public class CategoriesController {
categoryName categoryName
); );
} else { } else {
return result.getResponseTemplate(); return result.getFailedResponseTemplate();
} }
} }
@ -604,7 +604,7 @@ public class CategoriesController {
categoryPath categoryPath
); );
} else { } else {
return result.getResponseTemplate(); return result.getFailedResponseTemplate();
} }
} }
@ -630,7 +630,7 @@ public class CategoriesController {
categoryPath categoryPath
); );
} else { } else {
return result.getResponseTemplate(); return result.getFailedResponseTemplate();
} }
} }
@ -693,7 +693,7 @@ public class CategoriesController {
categoryPath categoryPath
); );
} else { } else {
return result.getResponseTemplate(); return result.getFailedResponseTemplate();
} }
} }
@ -730,7 +730,7 @@ public class CategoriesController {
categoryManager.getCategoryPath(parentCategory) categoryManager.getCategoryPath(parentCategory)
); );
} else { } else {
return result.getResponseTemplate(); return result.getFailedResponseTemplate();
} }
} }
@ -790,7 +790,7 @@ public class CategoriesController {
categoryManager.getCategoryPath(target) categoryManager.getCategoryPath(target)
); );
} else { } else {
return result.getResponseTemplate(); return result.getFailedResponseTemplate();
} }
} }
@ -851,7 +851,7 @@ public class CategoriesController {
pathFragment pathFragment
); );
} else { } else {
return result.getResponseTemplate(); return result.getFailedResponseTemplate();
} }
} }
@ -919,7 +919,7 @@ public class CategoriesController {
categoryManager.getCategoryPath(category) categoryManager.getCategoryPath(category)
); );
} else { } else {
return result.getResponseTemplate(); return result.getFailedResponseTemplate();
} }
} }

View File

@ -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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
class ContentSectionsUi {
@Inject
private ContentSectionRepository sectionRepo;
@Inject
private IdentifierParser identifierParser;
private Models models;
public Optional<ContentSection> 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<String, String> identifiers) {
for (final Map.Entry<String, String> 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<ContentSection> retrieveContentSection(
final String identifierParam
) {
final Identifier sectionIdentifier = identifierParser.parseIdentifier(
identifierParam
);
final Optional<ContentSection> 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"
);
}
}
}

View File

@ -8,22 +8,44 @@ package org.librecms.ui.contentsections;
import java.util.Objects; 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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a> * @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
* @param <T> * @param <T> The entity class contained by an instance of this class if it was
* retrived successfully.
*/ */
public class RetrieveResult<T> { public class RetrieveResult<T> {
/**
* The retrieved entity if the it was retrieved successfully.
*/
private T result; private T result;
private String responseTemplate; /**
* The path of the response template to show when the entity was <em>no</em>
* retrived successfully.
*/
private String failedResponseTemplate;
/**
* Indicates if the entity was retrieved succesfully.
*/
private boolean successful; private boolean successful;
private RetrieveResult() { private RetrieveResult() {
} }
/**
* Creates a successful {@code RetrieveResult} instance.
*
* @param <R> The type of the retrieved entity.
* @param result The retrieved entity. Can't be null.
*
* @return The new {@code RetrieveResult}.
*/
public static <R> RetrieveResult<R> successful(final R result) { public static <R> RetrieveResult<R> successful(final R result) {
final RetrieveResult<R> retrieveResult = new RetrieveResult<>(); final RetrieveResult<R> retrieveResult = new RetrieveResult<>();
retrieveResult.setResult(Objects.requireNonNull(result)); retrieveResult.setResult(Objects.requireNonNull(result));
@ -31,35 +53,75 @@ public class RetrieveResult<T> {
return retrieveResult; return retrieveResult;
} }
/**
* Creates a failed {@code RetrieveResult} instance.
*
* @param <R> The type of the entity to retrieve.
* @param responseTemplate The template to show.
*
* @return The new {@code RetrieveResult}.
*/
public static <R> RetrieveResult<R> failed(final String responseTemplate) { public static <R> RetrieveResult<R> failed(final String responseTemplate) {
final RetrieveResult<R> retrieveResult = new RetrieveResult<>(); final RetrieveResult<R> retrieveResult = new RetrieveResult<>();
retrieveResult.setResponseTemplate( retrieveResult.setFailedResponseTemplate(
Objects.requireNonNull(responseTemplate) Objects.requireNonNull(responseTemplate)
); );
retrieveResult.setSuccessful(false); retrieveResult.setSuccessful(false);
return retrieveResult; return retrieveResult;
} }
/**
* Gets the result.
*
* @return The retrieved entity.
*/
public T getResult() { public T getResult() {
return result; return result;
} }
/**
* Private setter for the result entity.
*
* @param result The retrieved entity.
*/
private void setResult(final T result) { private void setResult(final T result) {
this.result = 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() { public boolean isSuccessful() {
return successful; 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) { private void setSuccessful(final boolean successful) {
this.successful = successful; this.successful = successful;
} }