From c1bcd77b03b0bb311bfa606596af7157752f2c54 Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Tue, 2 Feb 2021 21:37:57 +0100 Subject: [PATCH] Create subfolders Former-commit-id: 4b4666ce91268b94bc56134bbc8695d3fbab6c4a --- .../ContentSectionController.java | 54 +++++++ .../DocumentFolderController.java | 100 +++++++++++- .../contentsections/DocumentFolderModel.java | 25 ++- .../documentfolder/documentfolder.xhtml | 149 ++++++++++-------- .../org/librecms/CmsAdminMessages.properties | 12 +- .../librecms/CmsAdminMessages_de.properties | 12 +- 6 files changed, 272 insertions(+), 80 deletions(-) create mode 100644 ccm-cms/src/main/java/org/librecms/ui/contentsections/ContentSectionController.java diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/ContentSectionController.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/ContentSectionController.java new file mode 100644 index 000000000..9a55bd453 --- /dev/null +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/ContentSectionController.java @@ -0,0 +1,54 @@ +package org.librecms.ui.contentsections; + +import org.libreccm.security.AuthorizationRequired; + +import java.net.URI; +import java.net.URISyntaxException; + +import javax.inject.Inject; +import javax.mvc.Controller; +import javax.servlet.http.HttpServletRequest; +import javax.transaction.Transactional; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; + +/** + * + * @author Jens Pelzetter + */ +@Controller +@Path("/") +public class ContentSectionController { + + @Inject + private HttpServletRequest request; + + @GET + @Path("/") + @AuthorizationRequired + @Transactional(Transactional.TxType.REQUIRED) + public Response redirectToContentSectionsList() { + try { + return Response + .seeOther( + new URI( + request.getScheme(), + "", + request.getServerName(), + request.getServerPort(), + String.format( + "%s/@cms/contentsections", + request.getContextPath() + ), + "", + "" + ) + ).build(); + } catch (URISyntaxException ex) { + throw new WebApplicationException(ex); + } + } + +} diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/DocumentFolderController.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/DocumentFolderController.java index 46ba9464b..a201c8262 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/DocumentFolderController.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/DocumentFolderController.java @@ -5,6 +5,8 @@ */ package org.librecms.ui.contentsections; +import com.arsdigita.cms.ui.folder.FolderPath; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.libreccm.api.Identifier; @@ -56,6 +58,9 @@ import javax.ws.rs.QueryParam; import org.librecms.ui.CmsAdminMessages; +import javax.ws.rs.FormParam; +import javax.ws.rs.POST; + /** * * @author Jens Pelzetter @@ -65,7 +70,8 @@ import org.librecms.ui.CmsAdminMessages; @Path("/{sectionIdentifier}/documentfolders") public class DocumentFolderController { - private static final Logger LOGGER = LogManager.getLogger(DocumentFolderController.class + private static final Logger LOGGER = LogManager.getLogger( + DocumentFolderController.class ); @Inject @@ -127,7 +133,7 @@ public class DocumentFolderController { sectionIdentifier, "", filterTerm, firstResult, maxResults ); } - + @GET @Path("/{folderPath:(.+)?}") @AuthorizationRequired @@ -254,6 +260,13 @@ public class DocumentFolderController { System.currentTimeMillis() - rowsStart ); + documentFolderModel.setPath(folderPath); + documentFolderModel.setCanCreateSubFolders( + permissionChecker.isPermitted( + ItemPrivileges.CREATE_NEW, folder + ) + ); + return "org/librecms/ui/contentsection/documentfolder/documentfolder.xhtml"; } else { models.put("sectionidentifier", sectionIdentifier); @@ -359,6 +372,89 @@ public class DocumentFolderController { } } + @POST + @Path("/") + @AuthorizationRequired + @Transactional(Transactional.TxType.REQUIRED) + public String newSubFolder( + @PathParam("sectionIdentifier") final String sectionIdentifier, + @FormParam("folderName") final String folderName + ) { + return newSubFolder( + sectionIdentifier, "", folderName + ); + } + + @POST + @Path("/{parentFolderPath:(.+)?}") + @AuthorizationRequired + @Transactional(Transactional.TxType.REQUIRED) + public String newSubFolder( + @PathParam("sectionIdentifier") final String sectionIdentifier, + @PathParam("parentFolderPath") final String parentFolderPath, + @FormParam("folderName") final String folderName + ) { + 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; + } + + if (sectionResult.isPresent()) { + final ContentSection section = sectionResult.get(); + + final Folder parentFolder; + if (parentFolderPath.isEmpty()) { + parentFolder = section.getRootDocumentsFolder(); + } else { + final Optional parentFolderResult = folderRepo + .findByPath(section, parentFolderPath, + FolderType.DOCUMENTS_FOLDER); + if (parentFolderResult.isPresent()) { + parentFolder = parentFolderResult.get(); + } else { + models.put("contentSection", section.getLabel()); + models.put("folderPath", parentFolderPath); + return "org/librecms/ui/contentsection/documentfolder/documentfolder-not-found.xhtml"; + } + } + + if (permissionChecker.isPermitted( + ItemPrivileges.CREATE_NEW, parentFolder + )) { + folderManager.createFolder(folderName, parentFolder); + + return String.format( + "redirect:/%s/documentfolders/%s", + sectionIdentifier, + parentFolderPath + ); + } else { + models.put("sectionidentifier", sectionIdentifier); + models.put("folderPath", parentFolderPath); + return "org/librecms/ui/contentsection/access-denied.xhtml"; + } + + } else { + models.put("sectionIdentifier", sectionIdentifier); + return "org/librecms/ui/contentsection/contentsection-not-found.xhtml"; + } + } + private List buildFolderTree( final ContentSection section, final Folder currentFolder ) { diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/DocumentFolderModel.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/DocumentFolderModel.java index 5a96b6a2a..c44cedfa6 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/DocumentFolderModel.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/DocumentFolderModel.java @@ -31,6 +31,11 @@ public class DocumentFolderModel { private List breadcrumbs; + private String path; + + private boolean canCreateSubFolders; + + public long getCount() { return count; } @@ -43,7 +48,6 @@ public class DocumentFolderModel { return firstResult; } - protected void setFirstResult(final int firstResult) { this.firstResult = firstResult; } @@ -71,8 +75,7 @@ public class DocumentFolderModel { protected void setRows(final List rows) { this.rows = new ArrayList<>(rows); } - - + public List getBreadcrumbs() { return Collections.unmodifiableList(breadcrumbs); } @@ -83,4 +86,20 @@ public class DocumentFolderModel { this.breadcrumbs = new ArrayList<>(breadcrumbs); } + public String getPath() { + return path; + } + + protected void setPath(final String path) { + this.path = path; + } + + public boolean isCanCreateSubFolders() { + return canCreateSubFolders; + } + + protected void setCanCreateSubFolders(final boolean canCreateSubFolders) { + this.canCreateSubFolders = canCreateSubFolders; + } + } diff --git a/ccm-cms/src/main/resources/WEB-INF/views/org/librecms/ui/contentsection/documentfolder/documentfolder.xhtml b/ccm-cms/src/main/resources/WEB-INF/views/org/librecms/ui/contentsection/documentfolder/documentfolder.xhtml index a9fce8df7..b8de4e3a0 100644 --- a/ccm-cms/src/main/resources/WEB-INF/views/org/librecms/ui/contentsection/documentfolder/documentfolder.xhtml +++ b/ccm-cms/src/main/resources/WEB-INF/views/org/librecms/ui/contentsection/documentfolder/documentfolder.xhtml @@ -5,7 +5,7 @@ xmlns:cms="http://xmlns.jcp.org/jsf/composite/components/cms" xmlns:libreccm="http://xmlns.jcp.org/jsf/composite/components/libreccm" xmlns:ui="http://xmlns.jcp.org/jsf/facelets"> - + @@ -49,30 +49,34 @@
-
- -
- -
-
+ + +
+ +
+
@@ -149,7 +104,7 @@
@@ -159,10 +114,66 @@ ${CmsAdminMessages.getMessage("contentsection.documentfolder.pageof", [DocumentFolderModel.currentPage, DocumentFolderModel.numberOfPages])}

- +