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.
*
* @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.
*
* @return The published content item.
@ -1209,7 +1210,8 @@ public class ContentItemManager {
@Transactional(Transactional.TxType.REQUIRED)
public void unpublish(
@RequiresPrivilege(ItemPrivileges.PUBLISH)
final ContentItem item) {
final ContentItem item
) {
if (item == null) {
throw new IllegalArgumentException(
"The item to unpublish can't be null");
@ -1217,44 +1219,53 @@ public class ContentItemManager {
LOGGER.debug("Unpublishing item {}...", item.getItemUuid());
final Optional<ContentItem> liveItem = getLiveVersion(
item, ContentItem.class);
final Optional<ContentItem> liveItemResult = getLiveVersion(
item, ContentItem.class
);
if (!liveItem.isPresent()) {
if (!liveItemResult.isPresent()) {
LOGGER.info("ContentItem {} has no live version.",
item.getItemUuid());
return;
}
final List<AttachmentList> attachmentLists = liveItem.get()
.getAttachments();
final ContentItem liveItem = liveItemResult.get();
final List<AttachmentList> attachmentLists = new ArrayList<>(
liveItem.getAttachments()
);
for (final AttachmentList attachmentList : attachmentLists) {
attachmentList.getAttachments().forEach(
attachment -> {
unpublishAttachment(attachment);
});
unpublishAttachmentList(attachmentList);
}
final List<Category> categories = liveItem
.get()
.getCategories()
.stream()
.map(categorization -> categorization.getCategory())
.collect(Collectors.toList());
categories.forEach(category -> {
for (final Category category : categories) {
try {
categoryManager.removeObjectFromCategory(liveItem.get(),
category);
categoryManager.removeObjectFromCategory(
liveItem, category
);
} catch (ObjectNotAssignedToCategoryException 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) {

View File

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