diff --git a/ccm-cms/package-lock.json b/ccm-cms/package-lock.json index 97fea6b7a..19e6bf84a 100644 --- a/ccm-cms/package-lock.json +++ b/ccm-cms/package-lock.json @@ -1,12 +1,12 @@ { "name": "@librecms/ccm-cms", - "version": "7.0.0-SNAPSHOT.2023-03-21T165148", + "version": "7.0.0-SNAPSHOT.2023-03-23T131710", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@librecms/ccm-cms", - "version": "7.0.0-SNAPSHOT.2023-03-21T165148", + "version": "7.0.0-SNAPSHOT.2023-03-23T131710", "license": "LGPL-3.0-or-later", "dependencies": { "@tiptap/core": "^2.0.0-beta.127", diff --git a/ccm-cms/package.json b/ccm-cms/package.json index 488caad80..b91767d19 100644 --- a/ccm-cms/package.json +++ b/ccm-cms/package.json @@ -1,6 +1,6 @@ { "name": "@librecms/ccm-cms", - "version": "7.0.0-SNAPSHOT.2023-03-21T165148", + "version": "7.0.0-SNAPSHOT.2023-03-23T131710", "description": "JavaScript stuff for ccm-cms", "main": "target/generated-resources/assets/@content-sections/cms-admin.js", "types": "target/generated-resources/assets/@content-sections/cms-admin.d.ts", diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/ContentSection.java b/ccm-cms/src/main/java/org/librecms/contentsection/ContentSection.java index ed5c2320c..827116380 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/ContentSection.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/ContentSection.java @@ -54,6 +54,8 @@ import org.librecms.lifecycle.LifecycleDefinition; import java.util.HashSet; import java.util.Set; +import javax.persistence.NamedNativeQueries; +import javax.persistence.NamedNativeQuery; import javax.persistence.OrderBy; import static org.librecms.CmsConstants.*; @@ -118,6 +120,48 @@ import static org.librecms.CmsConstants.*; + " OR p.object = :rootAssetsFolder) " + "AND p.grantee = :role") }) +@NamedNativeQueries({ + @NamedNativeQuery( + name = "ContentSection.countAssetSubFoldersOfRootFolder", + query = "SELECT COUNT(*) " + + "FROM ccm_core.categories " + + "WHERE parent_category_id = (" + + " SELECT root_assets_folder_id " + + " FROM ccm_cms.content_sections " + + " WHERE object_id = :sectionId" + + ")" + ), + @NamedNativeQuery( + name = "ContentSection.countDocumentSubFoldersOfRootFolder", + query = "SELECT COUNT(*) " + + "FROM ccm_core.categories " + + "WHERE parent_category_id = (" + + " SELECT root_documents_folder_id " + + " FROM ccm_cms.content_sections " + + " WHERE object_id = :sectionId" + + ")" + ), + @NamedNativeQuery( + name = "ContentSection.countObjectsInRootAssetsFolder", + query = "SELECT COUNT(*) " + + "FROM ccm_core.categorizations " + + "WHERE category_id = (" + + " SELECT root_assets_folder_id " + + " FROM ccm_cms.content_sections " + + " WHERE object_id = :sectionId" + + ");" + ), + @NamedNativeQuery( + name = "ContentSection.countObjectsInRootDocumentsFolder", + query = "SELECT COUNT(*) " + + "FROM ccm_core.categorizations " + + "WHERE category_id = (" + + " SELECT root_documents_folder_id " + + " FROM ccm_cms.content_sections " + + " WHERE object_id = :sectionId" + + ");" + ) +}) //@ApplicationType( // name = CONTENT_SECTION_APP_TYPE, // descBundle = "org.librecms.contentsection.ContentSectionResources", diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/ContentSectionRepository.java b/ccm-cms/src/main/java/org/librecms/contentsection/ContentSectionRepository.java index adf389f12..c00daa40a 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/ContentSectionRepository.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/ContentSectionRepository.java @@ -23,7 +23,9 @@ import org.libreccm.core.CoreConstants; import org.libreccm.security.AuthorizationRequired; import org.libreccm.security.RequiresPrivilege; +import java.math.BigInteger; import java.util.List; +import java.util.Objects; import java.util.Optional; import java.util.UUID; @@ -48,7 +50,7 @@ public class ContentSectionRepository .createNamedQuery("ContentSection.findAll", ContentSection.class) .getResultList(); } - + public Optional findByUuid(final String uuid) { try { return Optional.of( @@ -72,8 +74,10 @@ public class ContentSectionRepository } final TypedQuery query = getEntityManager() - .createNamedQuery("ContentSection.findByLabel", - ContentSection.class); + .createNamedQuery( + "ContentSection.findByLabel", + ContentSection.class + ); query.setParameter("label", label); try { @@ -83,6 +87,60 @@ public class ContentSectionRepository } } + public long countAssetSubFoldersOfRootFolder(final ContentSection section) { + Objects.requireNonNull(section); + + final BigInteger result = (BigInteger) getEntityManager() + .createNamedQuery( + "ContentSection.countAssetSubFoldersOfRootFolder" + ) + .setParameter("sectionId", section.getObjectId()) + .getSingleResult(); + + return result.longValue(); + } + + public long countDocumentSubFoldersOfRootFolder( + final ContentSection section + ) { + Objects.requireNonNull(section); + + final BigInteger result = (BigInteger) getEntityManager() + .createNamedQuery( + "ContentSection.countDocumentSubFoldersOfRootFolder" + ) + .setParameter("sectionId", section.getObjectId()) + .getSingleResult(); + + return result.longValue(); + } + + public long countObjectsInRootAssetFolder(final ContentSection section) { + Objects.requireNonNull(section); + + final BigInteger result = (BigInteger) getEntityManager() + .createNamedQuery( + "ContentSection.countObjectsInRootAssetsFolder" + ) + .setParameter("sectionId", section.getObjectId()) + .getSingleResult(); + + return result.longValue(); + } + + public long countObjectsInRootDocumentsFolder(final ContentSection section) { + Objects.requireNonNull(section); + + final BigInteger result = (BigInteger) getEntityManager() + .createNamedQuery( + "ContentSection.countObjectsInRootDocumentsFolder" + ) + .setParameter("sectionId", section.getObjectId()) + .getSingleResult(); + + return result.longValue(); + } + @Override public Class getEntityClass() { return ContentSection.class; diff --git a/ccm-cms/src/main/java/org/librecms/ui/ContentSectionsController.java b/ccm-cms/src/main/java/org/librecms/ui/ContentSectionsController.java index ce3e70582..d819cd783 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/ContentSectionsController.java +++ b/ccm-cms/src/main/java/org/librecms/ui/ContentSectionsController.java @@ -244,13 +244,29 @@ public class ContentSectionsController { * {@code false} is not. */ protected boolean canDelete(final ContentSection section) { - final Folder rootAssetsFolder = section.getRootAssetsFolder(); - final Folder rootDocumentsFolder = section.getRootDocumentsFolder(); + final long rootAssetFolders = sectionRepo + .countAssetSubFoldersOfRootFolder(section); + final long rootDocumentFolders = sectionRepo + .countDocumentSubFoldersOfRootFolder(section); + final long assetsInRootFolder = sectionRepo + .countObjectsInRootAssetFolder(section); + final long documentsInRootFolder = sectionRepo + .countObjectsInRootDocumentsFolder(section); - return rootAssetsFolder.getSubFolders().isEmpty() - && rootAssetsFolder.getObjects().isEmpty() - && rootDocumentsFolder.getSubFolders().isEmpty() - && rootDocumentsFolder.getObjects().isEmpty(); + final long result = rootAssetFolders + + rootDocumentFolders + + assetsInRootFolder + + documentsInRootFolder; + + return result == 0; + +// final Folder rootAssetsFolder = section.getRootAssetsFolder(); +// final Folder rootDocumentsFolder = section.getRootDocumentsFolder(); +// +// return rootAssetsFolder.getSubFolders().isEmpty() +// && rootAssetsFolder.getObjects().isEmpty() +// && rootDocumentsFolder.getSubFolders().isEmpty() +// && rootDocumentsFolder.getObjects().isEmpty(); } } diff --git a/ccm-cms/src/main/java/org/librecms/ui/ContentSectionsTableRow.java b/ccm-cms/src/main/java/org/librecms/ui/ContentSectionsTableRow.java index e1f1f4cd3..347b74bcf 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/ContentSectionsTableRow.java +++ b/ccm-cms/src/main/java/org/librecms/ui/ContentSectionsTableRow.java @@ -48,6 +48,20 @@ public class ContentSectionsTableRow implements * Is the section empty and can be deleted? */ private boolean deletable; + + public ContentSectionsTableRow() { + // Nothing + } + + public ContentSectionsTableRow( + final long sectionId, + final String label, + final boolean deletable + ) { + this.sectionId = sectionId; + this.label = label; + this.deletable = deletable; + } public long getSectionId() { return sectionId;