From 44e378e05471950d07738f43183620e18db4bfb2 Mon Sep 17 00:00:00 2001 From: jensp Date: Thu, 22 Mar 2018 14:14:10 +0000 Subject: [PATCH] CCM NG/ccm-cms: Some bugfixes for the items RESTful service git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@5353 8810af33-2d31-482b-a856-94f89814c4df --- .../cms/ui/assets/ItemSearchWidget.java | 2 + .../librecms/contentsection/ContentItem.java | 96 ++++++++++++++++++- .../contentsection/ContentItemRepository.java | 76 ++++++++++++++- .../contentsection/rs/ContentItems.java | 27 ++++-- .../foundry/foundry/scripts/cms-admin.js | 2 +- 5 files changed, 191 insertions(+), 12 deletions(-) diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/ItemSearchWidget.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/ItemSearchWidget.java index 5c876c1aa..c43804d81 100644 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/ItemSearchWidget.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/assets/ItemSearchWidget.java @@ -69,6 +69,8 @@ public class ItemSearchWidget extends Widget { CMS.CMS_XML_NS); widget.addAttribute("name", getName()); + widget.addAttribute("content-section", + CMS.getContext().getContentSection().getLabel()); if (type != null) { widget.addAttribute("asset-type", type.getName()); diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/ContentItem.java b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItem.java index a34d6503c..1c87a186a 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/ContentItem.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItem.java @@ -193,6 +193,28 @@ import static org.librecms.CmsConstants.*; + " OR true = :isSystemUser OR true = :isAdmin" + ")") , + @NamedQuery( + name = "ContentItem.findByContentSectionAndVersion", + query = "SELECT DISTINCT i " + + "FROM ContentItem i " + + "JOIN i.contentType t " + + "LEFT JOIN i.permissions p " + + "WHERE t.contentSection = :section " + + "AND version = :version " + + "AND (" + + " (" + + " p.grantee IN :roles " + + " AND p.grantedPrivilege = " + + " (CASE WHEN i.version = 'DRAFT' " + + " THEN '" + ItemPrivileges.PREVIEW + "' " + + " ELSE '" + ItemPrivileges.VIEW_PUBLISHED + + "' " + + " END" + + " )" + + " )" + + " OR true = :isSystemUser OR true = :isAdmin" + + ")") + , @NamedQuery( name = "ContentItem.findByNameAndContentSection", query = "SELECT DISTINCT i " @@ -215,6 +237,30 @@ import static org.librecms.CmsConstants.*; + " OR true = :isSystemUser OR true = :isAdmin" + ")") , + @NamedQuery( + name = "ContentItem.findNameAndContentSectionAndVersion", + query = "SELECT DISTINCT i " + + "FROM ContentItem i " + + "JOIN i.contentType t " + + "LEFT JOIN i.permissions p " + + "WHERE t.contentSection = :section " + + "AND lower(i.displayName) LIKE CONCAT('%', :name, '%s') " + + "AND version = :version " + + "AND (" + + " (" + + " p.grantee IN :roles " + + " AND p.grantedPrivilege = " + + " (CASE WHEN i.version = 'DRAFT' " + + " THEN '" + ItemPrivileges.PREVIEW + "' " + + " ELSE '" + ItemPrivileges.VIEW_PUBLISHED + + "' " + + " END" + + " )" + + " )" + + " OR true = :isSystemUser OR true = :isAdmin" + + ")") + + , @NamedQuery( name = "ContentItem.findByTypeAndContentSection", query = "SELECT DISTINCT i " @@ -237,7 +283,30 @@ import static org.librecms.CmsConstants.*; + " OR true = :isSystemUser OR true = :isAdmin" + ")") , - @NamedQuery( + @NamedQuery( + name = "ContentItem.findByTypeAndContentSectionAndVersion", + query = "SELECT DISTINCT i " + + "FROM ContentItem i " + + "JOIN i.contentType t " + + "LEFT JOIN i.permissions p " + + "WHERE t.contentSection = :section " + + "AND TYPE(i) = :type " + + "AND version = :version " + + "AND (" + + " (" + + " p.grantee IN :roles " + + " AND p.grantedPrivilege = " + + " (CASE WHEN i.version = 'DRAFT' " + + " THEN '" + ItemPrivileges.PREVIEW + "' " + + " ELSE '" + ItemPrivileges.VIEW_PUBLISHED + + "' " + + " END" + + " )" + + " )" + + " OR true = :isSystemUser OR true = :isAdmin" + + ")") + , + @NamedQuery( name = "ContentItem.findByNameAndTypeAndContentSection", query = "SELECT DISTINCT i " + "FROM ContentItem i " @@ -260,6 +329,31 @@ import static org.librecms.CmsConstants.*; + " OR true = :isSystemUser OR true = :isAdmin" + ")") , + @NamedQuery( + name = "ContentItem.findByNameAndTypeAndContentSectionAndVersion", + query = "SELECT DISTINCT i " + + "FROM ContentItem i " + + "JOIN i.contentType t " + + "LEFT JOIN i.permissions p " + + "WHERE t.contentSection = :section " + + "AND TYPE(i) = :type " + + "AND lower(i.displayName) LIKE CONCAT('%', :name, '%s') " + + "AND version = :version " + + "AND (" + + " (" + + " p.grantee IN :roles " + + " AND p.grantedPrivilege = " + + " (CASE WHEN i.version = 'DRAFT' " + + " THEN '" + ItemPrivileges.PREVIEW + "' " + + " ELSE '" + ItemPrivileges.VIEW_PUBLISHED + + "' " + + " END" + + " )" + + " )" + + " OR true = :isSystemUser OR true = :isAdmin" + + ")") + , + @NamedQuery( name = "ContentItem.findByFolder", query diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemRepository.java b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemRepository.java index 901a02793..55f3855fa 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemRepository.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemRepository.java @@ -97,9 +97,6 @@ public class ContentItemRepository @Inject private TaskManager taskManager; - @Inject - private TaskRepository taskRepo; - @Inject private UserRepository userRepository; @@ -270,6 +267,21 @@ public class ContentItemRepository return (List) query.getResultList(); } + @Transactional(Transactional.TxType.REQUIRED) + @SuppressWarnings("unchecked") + public List findByContentSection( + final ContentSection section, final ContentItemVersion version) { + + final TypedQuery query = getEntityManager() + .createNamedQuery("ContentItem.findByContentSectionAndVersion", + ContentItem.class); + query.setParameter("section", section); + query.setParameter("version", version.toString()); + setAuthorizationParameters(query); + + return (List) query.getResultList(); + } + @Transactional(Transactional.TxType.REQUIRED) @SuppressWarnings("unchecked") public List findByNameAndContentSection( @@ -285,6 +297,25 @@ public class ContentItemRepository return (List) query.getResultList(); } + @Transactional(Transactional.TxType.REQUIRED) + @SuppressWarnings("unchecked") + public List findByNameAndContentSection( + final String name, + final ContentSection section, + final ContentItemVersion version) { + + final TypedQuery query = getEntityManager() + .createNamedQuery( + "ContentItem.findByNameAndContentSectionAndVersion", + ContentItem.class); + query.setParameter("section", section); + query.setParameter("name", name); + query.setParameter("version", version.toString()); + setAuthorizationParameters(query); + + return (List) query.getResultList(); + } + @Transactional(Transactional.TxType.REQUIRED) @SuppressWarnings("unchecked") public List findByTypeAndContentSection( @@ -300,6 +331,25 @@ public class ContentItemRepository return (List) query.getResultList(); } + @Transactional(Transactional.TxType.REQUIRED) + @SuppressWarnings("unchecked") + public List findByTypeAndContentSection( + final Class type, + final ContentSection section, + final ContentItemVersion version) { + + final TypedQuery query = getEntityManager() + .createNamedQuery( + "ContentItem.findByNameAndContentSectionAndVersion", + ContentItem.class); + query.setParameter("section", section); + query.setParameter("type", type); + query.setParameter("version", version.toString()); + setAuthorizationParameters(query); + + return (List) query.getResultList(); + } + @Transactional(Transactional.TxType.REQUIRED) @SuppressWarnings("unchecked") public List findByNameAndTypeAndContentSection( @@ -318,6 +368,26 @@ public class ContentItemRepository return (List) query.getResultList(); } + @Transactional(Transactional.TxType.REQUIRED) + @SuppressWarnings("unchecked") + public List findByNameAndTypeAndContentSection( + final String name, + final Class type, + final ContentSection section, + final ContentItemVersion version) { + + final TypedQuery query = getEntityManager() + .createNamedQuery("ContentItem.findByNameAndContentSectionAndVersion", + ContentItem.class); + query.setParameter("section", section); + query.setParameter("name", name); + query.setParameter("type", type); + query.setParameter("version", version.toString()); + setAuthorizationParameters(query); + + return (List) query.getResultList(); + } + /** * Finds all content items of a specific type. * diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/rs/ContentItems.java b/ccm-cms/src/main/java/org/librecms/contentsection/rs/ContentItems.java index 7453492e1..53fffbea4 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/rs/ContentItems.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/rs/ContentItems.java @@ -20,8 +20,10 @@ package org.librecms.contentsection.rs; import org.libreccm.l10n.GlobalizationHelper; import org.librecms.contentsection.ContentItem; +import org.librecms.contentsection.ContentItemL10NManager; import org.librecms.contentsection.ContentItemManager; import org.librecms.contentsection.ContentItemRepository; +import org.librecms.contentsection.ContentItemVersion; import org.librecms.contentsection.ContentSection; import org.librecms.contentsection.ContentSectionRepository; import org.librecms.contentsection.Folder; @@ -131,8 +133,8 @@ public class ContentItems { result.put("name", item.getDisplayName()); result.put("title", - item.getTitle().getValue(globalizationHelper - .getNegotiatedLocale())); + globalizationHelper + .getValueFromLocalizedString(item.getTitle())); result.put("type", item.getClass().getName()); @@ -162,38 +164,49 @@ public class ContentItems { public List> findItems( @PathParam("content-section") final String section, @QueryParam("query") final String query, - @QueryParam("type") final String type) { + @QueryParam("type") final String type, + @QueryParam("version") final String version) { final ContentSection contentSection = sectionRepo .findByLabel(section) .orElseThrow(() -> new NotFoundException( String.format("No content section '%s' found.", section))); + final ContentItemVersion itemVersion; + if (version != null) { + itemVersion = ContentItemVersion.valueOf(version.toUpperCase()); + } else { + itemVersion = ContentItemVersion.LIVE; + } + final List items; if ((query == null || query.trim().isEmpty()) && (type == null || type.trim().isEmpty())) { items = itemRepo - .findByContentSection(contentSection); + .findByContentSection(contentSection, itemVersion); } else if ((query != null && !query.trim().isEmpty()) && (type == null || type.trim().isEmpty())) { items = itemRepo.findByNameAndContentSection(query, - contentSection); + contentSection, + itemVersion); } else if ((query == null || query.trim().isEmpty()) && (type != null && !type.trim().isEmpty())) { final Class itemType = toContentItemTypeClass(type); items = itemRepo.findByTypeAndContentSection(itemType, - contentSection); + contentSection, + itemVersion); } else { final Class itemType = toContentItemTypeClass(type); items = itemRepo.findByNameAndTypeAndContentSection( query, itemType, - contentSection); + contentSection, + itemVersion); } return items diff --git a/ccm-theme-foundry/src/main/resources/themes/foundry/foundry/scripts/cms-admin.js b/ccm-theme-foundry/src/main/resources/themes/foundry/foundry/scripts/cms-admin.js index 50089f272..3a9505a9a 100644 --- a/ccm-theme-foundry/src/main/resources/themes/foundry/foundry/scripts/cms-admin.js +++ b/ccm-theme-foundry/src/main/resources/themes/foundry/foundry/scripts/cms-admin.js @@ -102,7 +102,7 @@ function getItemsForSelectItemDialog(dialogId) { var dispatcherPrefix = dialog.getAttribute('data-dispatcherPrefix'); var request = new XMLHttpRequest(); - var url = dispatcherPrefix.substring(0, dispatcherPrefix.length - "/ccm".length) + "/content-sections/" + contentSection + "/items/"; + var url = dispatcherPrefix.substring(0, dispatcherPrefix.length - "/ccm".length) + "/content-sections/" + contentSection + "/items/?version=DRAFT"; if (type !== null && type.length > 0) { url = url + "?type=" + type; }