diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/CategoriesController.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/CategoriesController.java index 71db5db33..32a66c48e 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/CategoriesController.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/CategoriesController.java @@ -18,8 +18,6 @@ */ package org.librecms.ui.contentsections; -import com.arsdigita.cms.ui.authoring.multipartarticle.SectionPreviewPanel; - import org.libreccm.api.Identifier; import org.libreccm.api.IdentifierParser; import org.libreccm.categorization.Categorization; @@ -33,8 +31,12 @@ import org.libreccm.core.CcmObject; import org.libreccm.l10n.GlobalizationHelper; import org.libreccm.security.AuthorizationRequired; import org.libreccm.security.PermissionChecker; +import org.librecms.contentsection.ContentItem; +import org.librecms.contentsection.ContentItemManager; +import org.librecms.contentsection.ContentItemVersion; import org.librecms.contentsection.ContentSection; import org.librecms.contentsection.ContentSectionRepository; +import org.librecms.contentsection.Folder; import org.librecms.contentsection.privileges.AdminPrivileges; import java.time.ZoneId; @@ -87,6 +89,12 @@ public class CategoriesController { @Inject private CategorySystemModel categorySystemModel; + /** + * The {@link ContentItemManager}. + */ + @Inject + private ContentItemManager itemManager; + /** * The {@link ContentSectionModel} which stores the data of the current * content section for the view. @@ -252,7 +260,7 @@ public class CategoriesController { "sectionIdentifier", sectionIdentifier ); } - + final Optional domainResult = section .getDomains() .stream() @@ -824,6 +832,41 @@ public class CategoriesController { } } + /** + * Sets the index element of the root category. + * + * @param sectionIdentifier The identifier of the current + * {@link ContentSection}. + * @param context The mapping context of the assigned category + * system. + * @param indexElementUuid The UUID of the new index element. + * + * @return A redirect to the category page or the template for generating an + * error view. + */ + @POST + @Path( + "/{context}/categories/@index-element/{indexElementUuid}") + @AuthorizationRequired + @Transactional(Transactional.TxType.REQUIRED) + public String setIndexElement( + @PathParam("sectionIdentifier") final String sectionIdentifier, + @PathParam("context") final String context, + @PathParam("indexElementUuid") final String indexElementUuid + ) { + final RetrieveResult result = retrieveCategory( + sectionIdentifier, context, "/" + ); + + return setIndexObject( + result, + sectionIdentifier, + context, + context, + indexElementUuid + ); + } + /** * Sets the index element of a category. * @@ -837,7 +880,7 @@ public class CategoriesController { * @return A redirect to the category page or the template for generating an * error view. */ - @GET + @POST @Path( "/{context}/categories/{categoryPath:(.+)?}/@index-element/{indexElementUuid}") @AuthorizationRequired @@ -851,41 +894,95 @@ public class CategoriesController { final RetrieveResult result = retrieveCategory( sectionIdentifier, context, categoryPath ); + + return setIndexObject( + result, + sectionIdentifier, + context, + categoryPath, + indexElementUuid + ); +// if (result.isSuccessful()) { +// final Category category = result.getResult(); +// final Optional categorizationResult = category +// .getObjects() +// .stream() +// .filter( +// categorization -> Objects.equals( +// categorization.getUuid(), indexElementUuid +// ) +// ).findAny(); +// if (categorizationResult.isPresent()) { +// final CcmObject object = categorizationResult +// .get() +// .getCategorizedObject(); +// try { +// categoryManager.setIndexObject(category, object); +// if (object instanceof ContentItem) { +// final ContentItem item = (ContentItem) object; +// final ContentItem live = itemManager.getDraftVersion( +// item, +// ContentItem.class +// ); +// categoryManager.setIndexObject(category, live); +// } +// } catch (ObjectNotAssignedToCategoryException ex) { +// models.put("sectionIdentifier", sectionIdentifier); +// models.put("context", context); +// models.put("categoryPath", categoryPath); +// models.put("categorizationUuid", indexElementUuid); +// return "org/librecms/ui/contentsection/categorysystems/categorization-not-found.xhtml"; +// } +// } else { +// models.put("sectionIdentifier", sectionIdentifier); +// models.put("context", context); +// models.put("categoryPath", categoryPath); +// models.put("categorizationUuid", indexElementUuid); +// return "org/librecms/ui/contentsection/categorysystems/categorization-not-found.xhtml"; +// } +// return String.format( +// "redirect:/%s/categorysystems/%s/categories/%s#objects-sections", +// sectionIdentifier, +// context, +// categoryPath +// ); +// } else { +// return result.getFailedResponseTemplate(); +// } + } + + /** + * Resets the index object of the root category. The object is still + * associated with the category after this, but no longer the index object + * of the category. + * + * @param sectionIdentifier The identifier of the current + * {@link ContentSection}. + * @param context The mapping context of the assigned category + * system. + * + * @return A redirect to the category page or the template for generating an + * error view. + */ + @POST + @Path("/{context}/categories/@index-element/reset") + @AuthorizationRequired + @Transactional(Transactional.TxType.REQUIRED) + public String resetIndexElement( + @PathParam("sectionIdentifier") final String sectionIdentifier, + @PathParam("context") final String context + ) { + final RetrieveResult result = retrieveCategory( + sectionIdentifier, context, "/" + ); + if (result.isSuccessful()) { final Category category = result.getResult(); - final Optional categorizationResult = category - .getObjects() - .stream() - .filter( - categorization -> Objects.equals( - categorization.getUuid(), indexElementUuid - ) - ).findAny(); - if (categorizationResult.isPresent()) { - final CcmObject object = categorizationResult - .get() - .getCategorizedObject(); - try { - categoryManager.setIndexObject(category, object); - } catch (ObjectNotAssignedToCategoryException ex) { - models.put("sectionIdentifier", sectionIdentifier); - models.put("context", context); - models.put("categoryPath", categoryPath); - models.put("categorizationUuid", indexElementUuid); - return "org/librecms/ui/contentsection/categorysystems/categorization-not-found.xhtml"; - } - } else { - models.put("sectionIdentifier", sectionIdentifier); - models.put("context", context); - models.put("categoryPath", categoryPath); - models.put("categorizationUuid", indexElementUuid); - return "org/librecms/ui/contentsection/categorysystems/categorization-not-found.xhtml"; - } + categoryManager.resetIndexObject(category); return String.format( - "redirect:/%s/categorysystems/%s/categories/%s#objects-sections", + "redirect:/%s/categorysystems/%s/categories/", sectionIdentifier, - context, - categoryPath + context ); } else { return result.getFailedResponseTemplate(); @@ -893,7 +990,9 @@ public class CategoriesController { } /** - * Rests (removes) the index element of a category. + * Resets the index object of a category. The object is still associated + * with the category after this, but no longer the index object of the + * category. * * @param sectionIdentifier The identifier of the current * {@link ContentSection}. @@ -904,7 +1003,7 @@ public class CategoriesController { * @return A redirect to the category page or the template for generating an * error view. */ - @GET + @POST @Path("/{context}/categories/{categoryPath:(.+)?}/@index-element/reset") @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) @@ -920,7 +1019,7 @@ public class CategoriesController { final Category category = result.getResult(); categoryManager.resetIndexObject(category); return String.format( - "redirect:/%s/categorysystems/%s/categories/%s#objects-sections", + "redirect:/%s/categorysystems/%s/categories/%s", sectionIdentifier, context, categoryPath @@ -1220,7 +1319,47 @@ public class CategoriesController { } /** - * Reorders subcategories. + * Reorders objects assigned to the root category. + * + * @param sectionIdentifier The identifier of the current + * {@link ContentSection}. + * @param context The mapping context of the assigned category + * system. + * @param objectIdentifier The identifier of the object to move. + * @param direction The direction of the move. + * + * @return A redirect to the details page of the parent category or the + * template for building an error message. + */ + @POST + @Path( + "/{context}/categories/@objects/{objectIdentifier}/order" + ) + @AuthorizationRequired + @Transactional(Transactional.TxType.REQUIRED) + public String orderObjects( + @PathParam("sectionIdentifier") final String sectionIdentifier, + @PathParam("context") final String context, + @PathParam("objectIdentifier") final String objectIdentifier, + @FormParam("direction") final String direction + ) { + final RetrieveResult result = retrieveCategory( + sectionIdentifier, + context, + "/" + ); + + return orderObjects( + result, + sectionIdentifier, + context, + objectIdentifier, + direction + ); + } + + /** + * Reorders objects assigned to a category. * * @param sectionIdentifier The identifier of the current * {@link ContentSection}. @@ -1246,9 +1385,147 @@ public class CategoriesController { @FormParam("direction") final String direction ) { final RetrieveResult result = retrieveCategory( - sectionIdentifier, context, categoryPath + sectionIdentifier, + context, + categoryPath ); + return orderObjects( + result, + sectionIdentifier, + context, + objectIdentifier, + direction + ); +// +// if (result.isSuccessful()) { +// final Category category = result.getResult(); +// +// final Optional categorizationResult = category +// .getObjects() +// .stream() +// .filter( +// categorization -> Objects.equals( +// categorization.getUuid(), objectIdentifier +// ) +// ).findAny(); +// if (categorizationResult.isPresent()) { +// final CcmObject object = categorizationResult +// .get() +// .getCategorizedObject(); +// try { +// switch (direction) { +// case "DECREASE": +// categoryManager.decreaseObjectOrder( +// object, category +// ); +// break; +// case "INCREASE": +// categoryManager.increaseObjectOrder( +// object, category +// ); +// break; +// default: +// // Nothing +// break; +// } +// } catch (ObjectNotAssignedToCategoryException ex) { +// return String.format( +// "redirect:/%s/categorysystems/%s/categories/%s#objects-sections", +// sectionIdentifier, +// context, +// categoryManager.getCategoryPath(category) +// ); +// } +// } +// +// return String.format( +// "redirect:/%s/categorysystems/%s/categories/%s", +// sectionIdentifier, +// context, +// categoryManager.getCategoryPath(category) +// ); +// } else { +// return result.getFailedResponseTemplate(); +// } + } + + private String setIndexObject( + final RetrieveResult result, + @PathParam("sectionIdentifier") final String sectionIdentifier, + @PathParam("context") final String context, + @PathParam("categoryPath") final String categoryPath, + @PathParam("indexElementUuid") final String indexElementUuid + ) { + if (result.isSuccessful()) { + final Category category = result.getResult(); + final List categorizationResult = category + .getObjects() + .stream() + .filter( + categorization -> Objects.equals( + categorization.getCategorizedObject().getUuid(), + indexElementUuid + ) + ) + .collect(Collectors.toList()); + if (!categorizationResult.isEmpty()) { + for (final Categorization categorization : categorizationResult) { + final CcmObject object = categorization + .getCategorizedObject(); + try { + categoryManager.setIndexObject(category, object); + if (object instanceof ContentItem) { + final ContentItem item = (ContentItem) object; + final ContentItem live = itemManager + .getDraftVersion( + item, + ContentItem.class + ); + categoryManager.setIndexObject(category, live); + } + } catch (ObjectNotAssignedToCategoryException ex) { + models.put("sectionIdentifier", sectionIdentifier); + models.put("context", context); + models.put("categoryPath", categoryPath); + models.put("categorizationUuid", indexElementUuid); + return "org/librecms/ui/contentsection/categorysystems/categorization-not-found.xhtml"; + } + } + } else { + models.put("sectionIdentifier", sectionIdentifier); + models.put("context", context); + models.put("categoryPath", categoryPath); + models.put("categorizationUuid", indexElementUuid); + return "org/librecms/ui/contentsection/categorysystems/categorization-not-found.xhtml"; + } + if ("/".equals(categoryPath) + || category.getParentCategory() == null) { + return String.format( + "redirect:/%s/categorysystems/%s/categories/", + sectionIdentifier, + context + ); + } else { + return String.format( + "redirect:/%s/categorysystems/%s/categories/%s", + sectionIdentifier, + context, + categoryPath + ); + } + } else { + return result.getFailedResponseTemplate(); + } + } + + private String orderObjects( + final RetrieveResult result, + final String sectionIdentifier, + final String context, + final String objectIdentifier, + final String direction + ) { if (result.isSuccessful()) { final Category category = result.getResult(); @@ -1358,7 +1635,7 @@ public class CategoriesController { ); } final ContentSection section = sectionResult.get(); - if (permissionChecker.isPermitted( + if (!permissionChecker.isPermitted( AdminPrivileges.ADMINISTER_CATEGORIES, section )) { return RetrieveResult.failed( @@ -1477,6 +1754,15 @@ public class CategoriesController { category .getObjects() .stream() + .filter( + categorization -> categorization + .getCategorizedObject() instanceof ContentItem + ) + .filter( + categorization -> ((ContentItem) categorization + .getCategorizedObject()) + .getVersion() == ContentItemVersion.DRAFT + ) .map(this::buildCategorizedObjectModel) .collect(Collectors.toList()) ); diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/CategoryModel.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/CategoryModel.java index 4e4f258e3..f8f6cbf55 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/CategoryModel.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/CategoryModel.java @@ -223,7 +223,7 @@ public class CategoryModel { } public void setObjects(final List objects) { - this.objects = new ArrayList<>(); + this.objects = new ArrayList<>(objects); } public long getCategoryOrder() { diff --git a/ccm-cms/src/main/resources/WEB-INF/views/org/librecms/ui/contentsection/categorysystems/categorysystem.xhtml b/ccm-cms/src/main/resources/WEB-INF/views/org/librecms/ui/contentsection/categorysystems/categorysystem.xhtml index 2b4e5be4a..0eb11b699 100644 --- a/ccm-cms/src/main/resources/WEB-INF/views/org/librecms/ui/contentsection/categorysystems/categorysystem.xhtml +++ b/ccm-cms/src/main/resources/WEB-INF/views/org/librecms/ui/contentsection/categorysystems/categorysystem.xhtml @@ -451,23 +451,15 @@ #{CmsAdminMessages['contentsections.categorystems.category.objects.name']} - - #{CmsAdminMessages['contentsections.categorystems.category.objects.title']} - - #{CmsAdminMessages['contentsections.categorystems.category.objects.type']} - - #{CmsAdminMessages['contentsections.categorystems.category.objects.index']} - - #{CmsAdminMessages['contentsections.categorystems.category.objects.actions']} @@ -498,7 +490,7 @@ -
- - - - #{CmsAdminMessages['contentsections.categorystems.category.objects.index_object.reset']} - + + + - - - #{CmsAdminMessages['contentsections.categorystems.category.objects.index_object.set']} - +
+ +