Bugfixes for unpublish

pull/10/head
Jens Pelzetter 2021-08-17 20:32:36 +02:00
parent 9bded5ad64
commit cfe8810666
2 changed files with 44 additions and 27 deletions

View File

@ -848,7 +848,8 @@ public class ContentItemManager {
* for the content type of the provided item. * for the content type of the provided item.
* *
* @param item The content item to publish. * @param item The content item to publish.
* @param startDateTime Start date and time of the lifecycle. May not ben null. * @param startDateTime Start date and time of the lifecycle. May not ben
* null.
* @param endDateTime End date/time of the lifecycle. May be null. * @param endDateTime End date/time of the lifecycle. May be null.
* *
* @return The published content item. * @return The published content item.
@ -1209,7 +1210,8 @@ public class ContentItemManager {
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
public void unpublish( public void unpublish(
@RequiresPrivilege(ItemPrivileges.PUBLISH) @RequiresPrivilege(ItemPrivileges.PUBLISH)
final ContentItem item) { final ContentItem item
) {
if (item == null) { if (item == null) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"The item to unpublish can't be null"); "The item to unpublish can't be null");
@ -1217,44 +1219,53 @@ public class ContentItemManager {
LOGGER.debug("Unpublishing item {}...", item.getItemUuid()); LOGGER.debug("Unpublishing item {}...", item.getItemUuid());
final Optional<ContentItem> liveItem = getLiveVersion( final Optional<ContentItem> liveItemResult = getLiveVersion(
item, ContentItem.class); item, ContentItem.class
);
if (!liveItem.isPresent()) { if (!liveItemResult.isPresent()) {
LOGGER.info("ContentItem {} has no live version.", LOGGER.info("ContentItem {} has no live version.",
item.getItemUuid()); item.getItemUuid());
return; return;
} }
final List<AttachmentList> attachmentLists = liveItem.get() final ContentItem liveItem = liveItemResult.get();
.getAttachments();
final List<AttachmentList> attachmentLists = new ArrayList<>(
liveItem.getAttachments()
);
for (final AttachmentList attachmentList : attachmentLists) { for (final AttachmentList attachmentList : attachmentLists) {
attachmentList.getAttachments().forEach( unpublishAttachmentList(attachmentList);
attachment -> {
unpublishAttachment(attachment);
});
} }
final List<Category> categories = liveItem final List<Category> categories = liveItem
.get()
.getCategories() .getCategories()
.stream() .stream()
.map(categorization -> categorization.getCategory()) .map(categorization -> categorization.getCategory())
.collect(Collectors.toList()); .collect(Collectors.toList());
categories.forEach(category -> { for (final Category category : categories) {
try { try {
categoryManager.removeObjectFromCategory(liveItem.get(), categoryManager.removeObjectFromCategory(
category); liveItem, category
);
} catch (ObjectNotAssignedToCategoryException ex) { } catch (ObjectNotAssignedToCategoryException ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
});
if (liveItem.isPresent()) {
entityManager.remove(liveItem.get());
} }
entityManager.remove(liveItem);
}
private void unpublishAttachmentList(final AttachmentList attachmentList) {
for(final ItemAttachment<?> attachment : attachmentList.getAttachments()) {
unpublishAttachment(attachment);
}
final ContentItem item = attachmentList.getItem();
attachmentList.setItem(null);
item.removeAttachmentList(attachmentList);
entityManager.remove(attachmentList);
} }
private void unpublishAttachment(final ItemAttachment<?> itemAttachment) { private void unpublishAttachment(final ItemAttachment<?> itemAttachment) {

View File

@ -32,7 +32,6 @@ import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.LocalTime; import java.time.LocalTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException; import java.time.format.DateTimeParseException;
import java.util.Date; import java.util.Date;
@ -41,7 +40,6 @@ import java.util.stream.Collectors;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named;
import javax.mvc.Controller; import javax.mvc.Controller;
import javax.mvc.Models; import javax.mvc.Models;
import javax.transaction.Transactional; import javax.transaction.Transactional;
@ -428,6 +426,14 @@ public class PublishStep extends AbstractMvcAuthoringStep {
@Path("/unpublish") @Path("/unpublish")
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
public String unpublish() { public String unpublish() {
try {
init();
} catch (ContentSectionNotFoundException ex) {
return ex.showErrorMessage();
} catch (DocumentNotFoundException ex) {
return ex.showErrorMessage();
}
final ContentItem document = getDocument(); final ContentItem document = getDocument();
if (!itemPermissionChecker.canPublishItems(document)) { if (!itemPermissionChecker.canPublishItems(document)) {
return documentUi.showAccessDenied( return documentUi.showAccessDenied(