From 9bded5ad648c642eb140eb500aa639142a79b684 Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Sat, 7 Aug 2021 19:49:23 +0200 Subject: [PATCH] PublishStep: Publishing works, unpublishing not, republishing not tested yet. Some finetuning is also required. Also not all error messages set by the controller are displayed yet. --- .../documents/CmsMvcAuthoringSteps.java | 1 + .../documents/PublishStep.java | 195 ++++++++++++------ .../documents/PublishStepModel.java | 28 ++- .../ui/contentsection/documents/publish.xhtml | 38 ++-- .../ui/DefaultAuthoringStepsBundle.properties | 4 +- .../DefaultAuthoringStepsBundle_de.properties | 4 +- 6 files changed, 190 insertions(+), 80 deletions(-) diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/CmsMvcAuthoringSteps.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/CmsMvcAuthoringSteps.java index 5d16b6f31..178ecd970 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/CmsMvcAuthoringSteps.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/CmsMvcAuthoringSteps.java @@ -43,6 +43,7 @@ public class CmsMvcAuthoringSteps implements MvcAuthoringSteps { ExampleAuthoringStep.class, CategorizationStep.class, MediaStep.class, + PublishStep.class, RelatedInfoStep.class, MvcArticlePropertiesStep.class, MvcArticleTextBodyStep.class diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/PublishStep.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/PublishStep.java index 995191f0e..81796b194 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/PublishStep.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/PublishStep.java @@ -18,6 +18,7 @@ */ package org.librecms.ui.contentsections.documents; +import org.libreccm.core.UnexpectedErrorException; import org.librecms.ui.contentsections.ContentSectionNotFoundException; import org.libreccm.l10n.GlobalizationHelper; import org.librecms.contentsection.ContentItem; @@ -36,6 +37,7 @@ import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.util.Date; import java.util.Optional; +import java.util.stream.Collectors; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; @@ -69,7 +71,7 @@ import javax.ws.rs.PathParam; public class PublishStep extends AbstractMvcAuthoringStep { private static final String TEMPLATE - = "org/librecms/ui/documenttypes/publish.xhtml"; + = "org/librecms/ui/contentsection/documents/publish.xhtml"; /** * The path fragment of the publish step. @@ -110,24 +112,51 @@ public class PublishStep extends AbstractMvcAuthoringStep { DocumentNotFoundException { super.init(); - publishStepModel.setAssignedLifecycleLabel( - Optional - .ofNullable(getDocument().getLifecycle()) - .map(Lifecycle::getDefinition) - .map(LifecycleDefinition::getLabel) - .map(globalizationHelper::getValueFromLocalizedString) - .orElse("") + publishStepModel.setAvailableLifecycles( + getContentSection() + .getLifecycleDefinitions() + .stream() + .map(this::buildLifecycleListEntry) + .collect(Collectors.toList()) ); - publishStepModel.setAssignedLifecycleDescription( - Optional - .ofNullable(getDocument().getLifecycle()) - .map(Lifecycle::getDefinition) - .map(LifecycleDefinition::getDescription) - .map(globalizationHelper::getValueFromLocalizedString) - .orElse("") - ); - + final ContentItem document = getDocument(); + + if (itemManager.isLive(document)) { + final ContentItem live = itemManager + .getLiveVersion(document, document.getClass()) + .orElseThrow( + () -> new UnexpectedErrorException( + String.format( + "ContentItem %s is reported as live by " + + "ContentItemManager#isLive" + + "but has no live version.", + document.getUuid() + ) + ) + ); + publishStepModel.setAssignedLifecycleLabel( + Optional + .ofNullable(live.getLifecycle()) + .map(Lifecycle::getDefinition) + .map(LifecycleDefinition::getLabel) + .map(globalizationHelper::getValueFromLocalizedString) + .orElse("") + ); + + publishStepModel.setAssignedLifecycleDescription( + Optional + .ofNullable(live.getLifecycle()) + .map(Lifecycle::getDefinition) + .map(LifecycleDefinition::getDescription) + .map(globalizationHelper::getValueFromLocalizedString) + .orElse("") + ); + } else { + publishStepModel.setAssignedLifecycleDescription(""); + publishStepModel.setAssignedLifecycleLabel(""); + } + publishStepModel.setLive(itemManager.isLive(getDocument())); } @@ -152,7 +181,18 @@ public class PublishStep extends AbstractMvcAuthoringStep { if (itemPermissionChecker.canPublishItems(getDocument())) { final String lifecycleDefUuid; if (itemManager.isLive(document)) { - lifecycleDefUuid = document + lifecycleDefUuid = itemManager + .getLiveVersion(document, document.getClass()) + .orElseThrow( + () -> new UnexpectedErrorException( + String.format( + "ContentItem %s is reported as live by " + + "ContentItemManager#isLive" + + "but has no live version.", + document.getUuid() + ) + ) + ) .getLifecycle() .getDefinition() .getUuid(); @@ -279,49 +319,54 @@ public class PublishStep extends AbstractMvcAuthoringStep { final LocalDateTime startLocalDateTime = LocalDateTime.of( localStartDate, localStartTime ); + final Date startDateTime = Date.from( - startLocalDateTime.toInstant( - ZoneOffset.from(startLocalDateTime) - ) + startLocalDateTime.atZone(ZoneId.systemDefault()) + .toInstant() ); - final LocalDate localEndDate; - try { - localEndDate = LocalDate.parse(endDateParam, isoDateFormatter); - } catch (DateTimeParseException ex) { - models.put("invalidEndDate", true); - models.put("lifecycleDefinitionUuid", selectedLifecycleDefUuid); - models.put("startDate", startDateParam); - models.put("startTime", startTimeParam); - models.put("endDate", endDateParam); - models.put("endTime", endTimeParam); + final Date endDateTime; + if (endDateParam == null || endDateParam.isBlank()) { + endDateTime = null; + } else { + final LocalDate localEndDate; + try { + localEndDate = LocalDate.parse(endDateParam, isoDateFormatter); + } catch (DateTimeParseException ex) { + models.put("invalidEndDate", true); + models.put("lifecycleDefinitionUuid", selectedLifecycleDefUuid); + models.put("startDate", startDateParam); + models.put("startTime", startTimeParam); + models.put("endDate", endDateParam); + models.put("endTime", endTimeParam); - return TEMPLATE; + return TEMPLATE; + } + + final LocalTime localEndTime; + try { + localEndTime = LocalTime.parse(endTimeParam, isoTimeFormatter); + } catch (DateTimeParseException ex) { + models.put("invalidEndTime", true); + models.put("lifecycleDefinitionUuid", selectedLifecycleDefUuid); + models.put("startDate", startDateParam); + models.put("startTime", startTimeParam); + models.put("endDate", endDateParam); + models.put("endTime", endTimeParam); + + return TEMPLATE; + } + + final LocalDateTime endLocalDateTime = LocalDateTime.of( + localEndDate, localEndTime + ); + endDateTime = Date.from( + endLocalDateTime + .atZone(ZoneId.systemDefault()) + .toInstant() + ); } - final LocalTime localEndTime; - try { - localEndTime = LocalTime.parse(endTimeParam, isoTimeFormatter); - } catch (DateTimeParseException ex) { - models.put("invalidEndTime", true); - models.put("lifecycleDefinitionUuid", selectedLifecycleDefUuid); - models.put("startDate", startDateParam); - models.put("startTime", startTimeParam); - models.put("endDate", endDateParam); - models.put("endTime", endTimeParam); - - return TEMPLATE; - } - - final LocalDateTime endLocalDateTime = LocalDateTime.of( - localEndDate, localEndTime - ); - final Date endDateTime = Date.from( - endLocalDateTime.toInstant( - ZoneOffset.from(endLocalDateTime) - ) - ); - if (!itemPermissionChecker.canPublishItems(document)) { return documentUi.showAccessDenied( getContentSection(), @@ -333,7 +378,20 @@ public class PublishStep extends AbstractMvcAuthoringStep { if (selectedLifecycleDefUuid.isEmpty()) { if (itemManager.isLive(document)) { final LifecycleDefinition definition; - definition = document.getLifecycle().getDefinition(); + definition = itemManager + .getLiveVersion(document, document.getClass()) + .orElseThrow( + () -> new UnexpectedErrorException( + String.format( + "ContentItem %s is reported as live by " + + "ContentItemManager#isLive" + + "but has no live version.", + document.getUuid() + ) + ) + ) + .getLifecycle() + .getDefinition(); itemManager.publish( document, definition, startDateTime, endDateTime ); @@ -366,10 +424,8 @@ public class PublishStep extends AbstractMvcAuthoringStep { * * @return A redirect to the publish step. */ - @MvcAuthoringAction( - method = MvcAuthoringActionMethod.POST, - path = "/@unpublish" - ) + @POST + @Path("/unpublish") @Transactional(Transactional.TxType.REQUIRED) public String unpublish() { final ContentItem document = getDocument(); @@ -386,4 +442,23 @@ public class PublishStep extends AbstractMvcAuthoringStep { return buildRedirectPathForStep(); } + private LifecycleListEntry buildLifecycleListEntry( + final LifecycleDefinition definition + ) { + final LifecycleListEntry entry = new LifecycleListEntry(); + entry.setDescription( + globalizationHelper.getValueFromLocalizedString( + definition.getDescription() + ) + ); + entry.setLabel( + globalizationHelper.getValueFromLocalizedString( + definition.getLabel() + ) + ); + entry.setUuid(definition.getUuid()); + + return entry; + } + } diff --git a/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/PublishStepModel.java b/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/PublishStepModel.java index 0018088d0..4230dab1b 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/PublishStepModel.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contentsections/documents/PublishStepModel.java @@ -20,6 +20,10 @@ package org.librecms.ui.contentsections.documents; import org.libreccm.l10n.GlobalizationHelper; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -50,7 +54,7 @@ public class PublishStepModel { /** * A list of the available lifecycles. */ - private List availableListcycles; + private List availableLivecycles; /** * Get the label of the lifecycle assigned to the current content item. The @@ -99,18 +103,18 @@ public class PublishStepModel { } public PublishStepModel() { - availableListcycles = new ArrayList<>(); + availableLivecycles = new ArrayList<>(); phases = new ArrayList<>(); } public List getAvailableLifecycles() { - return Collections.unmodifiableList(availableListcycles); + return Collections.unmodifiableList(availableLivecycles); } public void setAvailableLifecycles( final List availableListcycles ) { - this.availableListcycles = new ArrayList<>(availableListcycles); + this.availableLivecycles = new ArrayList<>(availableListcycles); } public List getPhases() { @@ -121,4 +125,20 @@ public class PublishStepModel { this.phases = new ArrayList<>(phases); } + public String getToday() { + return LocalDateTime + .now() + .format(DateTimeFormatter.ISO_DATE.withZone(ZoneId.systemDefault())); + } + + public String getNow() { + return LocalTime + .now() + .format( + DateTimeFormatter + .ofPattern("HH:mm") + .withZone(ZoneId.systemDefault()) + ); + } + } diff --git a/ccm-cms/src/main/resources/WEB-INF/views/org/librecms/ui/contentsection/documents/publish.xhtml b/ccm-cms/src/main/resources/WEB-INF/views/org/librecms/ui/contentsection/documents/publish.xhtml index b2cf8354f..4e771401d 100644 --- a/ccm-cms/src/main/resources/WEB-INF/views/org/librecms/ui/contentsection/documents/publish.xhtml +++ b/ccm-cms/src/main/resources/WEB-INF/views/org/librecms/ui/contentsection/documents/publish.xhtml @@ -4,20 +4,29 @@ xmlns:c="http://xmlns.jcp.org/jsp/jstl/core" xmlns:libreccm="http://xmlns.jcp.org/jsf/composite/components/libreccm" xmlns:ui="http://xmlns.jcp.org/jsf/facelets"> - + - + - + - + - + + +

#{CmsDefaultStepsMessageBundle.getMessage('publish.title', [CmsSelectedDocumentModel.itemTitle])}

+ + +
+ #{CmsDefaultStepsMessageBundle['publish.no_lifecycle_selected']} +
+
+

@@ -29,7 +38,8 @@

-
-
-
@@ -83,7 +93,8 @@ id="lifecycle-startdate" name="startDate" required="true" - type="date" /> + type="date" + value="#{CmsDocumentPublishStepModel.today}" />