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
pull/2/head
jensp 2018-03-22 14:14:10 +00:00
parent 51e083c13b
commit 44e378e054
5 changed files with 191 additions and 12 deletions

View File

@ -69,6 +69,8 @@ public class ItemSearchWidget extends Widget {
CMS.CMS_XML_NS); CMS.CMS_XML_NS);
widget.addAttribute("name", getName()); widget.addAttribute("name", getName());
widget.addAttribute("content-section",
CMS.getContext().getContentSection().getLabel());
if (type != null) { if (type != null) {
widget.addAttribute("asset-type", type.getName()); widget.addAttribute("asset-type", type.getName());

View File

@ -193,6 +193,28 @@ import static org.librecms.CmsConstants.*;
+ " OR true = :isSystemUser OR true = :isAdmin" + " 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( @NamedQuery(
name = "ContentItem.findByNameAndContentSection", name = "ContentItem.findByNameAndContentSection",
query = "SELECT DISTINCT i " query = "SELECT DISTINCT i "
@ -215,6 +237,30 @@ import static org.librecms.CmsConstants.*;
+ " OR true = :isSystemUser OR true = :isAdmin" + " 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( @NamedQuery(
name = "ContentItem.findByTypeAndContentSection", name = "ContentItem.findByTypeAndContentSection",
query = "SELECT DISTINCT i " query = "SELECT DISTINCT i "
@ -237,7 +283,30 @@ import static org.librecms.CmsConstants.*;
+ " OR true = :isSystemUser OR true = :isAdmin" + " 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", name = "ContentItem.findByNameAndTypeAndContentSection",
query = "SELECT DISTINCT i " query = "SELECT DISTINCT i "
+ "FROM ContentItem i " + "FROM ContentItem i "
@ -260,6 +329,31 @@ import static org.librecms.CmsConstants.*;
+ " OR true = :isSystemUser OR true = :isAdmin" + " 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( @NamedQuery(
name = "ContentItem.findByFolder", name = "ContentItem.findByFolder",
query query

View File

@ -97,9 +97,6 @@ public class ContentItemRepository
@Inject @Inject
private TaskManager taskManager; private TaskManager taskManager;
@Inject
private TaskRepository taskRepo;
@Inject @Inject
private UserRepository userRepository; private UserRepository userRepository;
@ -270,6 +267,21 @@ public class ContentItemRepository
return (List<T>) query.getResultList(); return (List<T>) query.getResultList();
} }
@Transactional(Transactional.TxType.REQUIRED)
@SuppressWarnings("unchecked")
public <T extends ContentItem> List<T> findByContentSection(
final ContentSection section, final ContentItemVersion version) {
final TypedQuery<ContentItem> query = getEntityManager()
.createNamedQuery("ContentItem.findByContentSectionAndVersion",
ContentItem.class);
query.setParameter("section", section);
query.setParameter("version", version.toString());
setAuthorizationParameters(query);
return (List<T>) query.getResultList();
}
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends ContentItem> List<T> findByNameAndContentSection( public <T extends ContentItem> List<T> findByNameAndContentSection(
@ -285,6 +297,25 @@ public class ContentItemRepository
return (List<T>) query.getResultList(); return (List<T>) query.getResultList();
} }
@Transactional(Transactional.TxType.REQUIRED)
@SuppressWarnings("unchecked")
public <T extends ContentItem> List<T> findByNameAndContentSection(
final String name,
final ContentSection section,
final ContentItemVersion version) {
final TypedQuery<ContentItem> query = getEntityManager()
.createNamedQuery(
"ContentItem.findByNameAndContentSectionAndVersion",
ContentItem.class);
query.setParameter("section", section);
query.setParameter("name", name);
query.setParameter("version", version.toString());
setAuthorizationParameters(query);
return (List<T>) query.getResultList();
}
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends ContentItem> List<T> findByTypeAndContentSection( public <T extends ContentItem> List<T> findByTypeAndContentSection(
@ -300,6 +331,25 @@ public class ContentItemRepository
return (List<T>) query.getResultList(); return (List<T>) query.getResultList();
} }
@Transactional(Transactional.TxType.REQUIRED)
@SuppressWarnings("unchecked")
public <T extends ContentItem> List<T> findByTypeAndContentSection(
final Class<? extends ContentItem> type,
final ContentSection section,
final ContentItemVersion version) {
final TypedQuery<ContentItem> query = getEntityManager()
.createNamedQuery(
"ContentItem.findByNameAndContentSectionAndVersion",
ContentItem.class);
query.setParameter("section", section);
query.setParameter("type", type);
query.setParameter("version", version.toString());
setAuthorizationParameters(query);
return (List<T>) query.getResultList();
}
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends ContentItem> List<T> findByNameAndTypeAndContentSection( public <T extends ContentItem> List<T> findByNameAndTypeAndContentSection(
@ -318,6 +368,26 @@ public class ContentItemRepository
return (List<T>) query.getResultList(); return (List<T>) query.getResultList();
} }
@Transactional(Transactional.TxType.REQUIRED)
@SuppressWarnings("unchecked")
public <T extends ContentItem> List<T> findByNameAndTypeAndContentSection(
final String name,
final Class<? extends ContentItem> type,
final ContentSection section,
final ContentItemVersion version) {
final TypedQuery<ContentItem> 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<T>) query.getResultList();
}
/** /**
* Finds all content items of a specific type. * Finds all content items of a specific type.
* *

View File

@ -20,8 +20,10 @@ package org.librecms.contentsection.rs;
import org.libreccm.l10n.GlobalizationHelper; import org.libreccm.l10n.GlobalizationHelper;
import org.librecms.contentsection.ContentItem; import org.librecms.contentsection.ContentItem;
import org.librecms.contentsection.ContentItemL10NManager;
import org.librecms.contentsection.ContentItemManager; import org.librecms.contentsection.ContentItemManager;
import org.librecms.contentsection.ContentItemRepository; import org.librecms.contentsection.ContentItemRepository;
import org.librecms.contentsection.ContentItemVersion;
import org.librecms.contentsection.ContentSection; import org.librecms.contentsection.ContentSection;
import org.librecms.contentsection.ContentSectionRepository; import org.librecms.contentsection.ContentSectionRepository;
import org.librecms.contentsection.Folder; import org.librecms.contentsection.Folder;
@ -131,8 +133,8 @@ public class ContentItems {
result.put("name", item.getDisplayName()); result.put("name", item.getDisplayName());
result.put("title", result.put("title",
item.getTitle().getValue(globalizationHelper globalizationHelper
.getNegotiatedLocale())); .getValueFromLocalizedString(item.getTitle()));
result.put("type", result.put("type",
item.getClass().getName()); item.getClass().getName());
@ -162,38 +164,49 @@ public class ContentItems {
public List<Map<String, String>> findItems( public List<Map<String, String>> findItems(
@PathParam("content-section") final String section, @PathParam("content-section") final String section,
@QueryParam("query") final String query, @QueryParam("query") final String query,
@QueryParam("type") final String type) { @QueryParam("type") final String type,
@QueryParam("version") final String version) {
final ContentSection contentSection = sectionRepo final ContentSection contentSection = sectionRepo
.findByLabel(section) .findByLabel(section)
.orElseThrow(() -> new NotFoundException( .orElseThrow(() -> new NotFoundException(
String.format("No content section '%s' found.", section))); 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<ContentItem> items; final List<ContentItem> items;
if ((query == null || query.trim().isEmpty()) if ((query == null || query.trim().isEmpty())
&& (type == null || type.trim().isEmpty())) { && (type == null || type.trim().isEmpty())) {
items = itemRepo items = itemRepo
.findByContentSection(contentSection); .findByContentSection(contentSection, itemVersion);
} else if ((query != null && !query.trim().isEmpty()) } else if ((query != null && !query.trim().isEmpty())
&& (type == null || type.trim().isEmpty())) { && (type == null || type.trim().isEmpty())) {
items = itemRepo.findByNameAndContentSection(query, items = itemRepo.findByNameAndContentSection(query,
contentSection); contentSection,
itemVersion);
} else if ((query == null || query.trim().isEmpty()) } else if ((query == null || query.trim().isEmpty())
&& (type != null && !type.trim().isEmpty())) { && (type != null && !type.trim().isEmpty())) {
final Class<? extends ContentItem> itemType final Class<? extends ContentItem> itemType
= toContentItemTypeClass(type); = toContentItemTypeClass(type);
items = itemRepo.findByTypeAndContentSection(itemType, items = itemRepo.findByTypeAndContentSection(itemType,
contentSection); contentSection,
itemVersion);
} else { } else {
final Class<? extends ContentItem> itemType final Class<? extends ContentItem> itemType
= toContentItemTypeClass(type); = toContentItemTypeClass(type);
items = itemRepo.findByNameAndTypeAndContentSection( items = itemRepo.findByNameAndTypeAndContentSection(
query, query,
itemType, itemType,
contentSection); contentSection,
itemVersion);
} }
return items return items

View File

@ -102,7 +102,7 @@ function getItemsForSelectItemDialog(dialogId) {
var dispatcherPrefix = dialog.getAttribute('data-dispatcherPrefix'); var dispatcherPrefix = dialog.getAttribute('data-dispatcherPrefix');
var request = new XMLHttpRequest(); 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) { if (type !== null && type.length > 0) {
url = url + "?type=" + type; url = url + "?type=" + type;
} }