From e12c9ee66e910e3e6af7059b4eb297d7542b7929 Mon Sep 17 00:00:00 2001 From: jensp Date: Mon, 8 May 2017 09:28:20 +0000 Subject: [PATCH] CCM NG/ccm-cms: Some bugfixes for the ContentItemManager fixing test failures git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4712 8810af33-2d31-482b-a856-94f89814c4df --- .../contentsection/ContentItemManager.java | 186 +++++++++--------- .../after-republish.xml | 2 +- 2 files changed, 89 insertions(+), 99 deletions(-) diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemManager.java b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemManager.java index 38fb71b14..38a424f3d 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemManager.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemManager.java @@ -863,7 +863,6 @@ public class ContentItemManager { if (isLive(item)) { liveItem = getLiveVersion(item, ContentItem.class).get(); -// unpublish(oldLiveItem); } else { try { liveItem = draftItem.getClass().newInstance(); @@ -872,7 +871,6 @@ public class ContentItemManager { } } - liveItem.setVersion(ContentItemVersion.PUBLISHING); liveItem.setItemUuid(draftItem.getItemUuid()); liveItem.setContentType(draftItem.getContentType()); @@ -882,102 +880,7 @@ public class ContentItemManager { liveItem.setLifecycle(lifecycle); liveItem.setWorkflow(draftItem.getWorkflow()); - contentItemRepo.save(liveItem); - - final List oldCategories = liveItem - .getCategories() - .stream() - .map(categorization -> categorization.getCategory()) - .collect(Collectors.toList()); - oldCategories.forEach(category -> { - try { - categoryManager.removeObjectFromCategory(liveItem, category); - } catch (ObjectNotAssignedToCategoryException ex) { - throw new RuntimeException(ex); - } - }); - - contentItemRepo.save(liveItem); - - draftItem.getCategories().forEach(categorization -> categoryManager - .addObjectToCategory(liveItem, - categorization.getCategory(), - categorization.getType())); - - contentItemRepo.save(liveItem); - - for (int i = 0; i < draftItem.getAttachments().size(); i++) { - final AttachmentList sourceList = draftItem.getAttachments().get(i); - - final AttachmentList targetList; - if (liveItem.getAttachments().size() < i + 1) { - targetList = new AttachmentList(); - copyLocalizedString(sourceList.getDescription(), - targetList.getDescription()); - - targetList.setItem(liveItem); - liveItem.addAttachmentList(targetList); - targetList.setName(sourceList.getName()); - copyLocalizedString(sourceList.getTitle(), - targetList.getTitle()); - targetList.setOrder(sourceList.getOrder()); - targetList.setUuid(UUID.randomUUID().toString()); - } else { - targetList = liveItem.getAttachments().get(i); - } - - for (int j = 0; j < sourceList.getAttachments().size(); j++) { - final ItemAttachment sourceAttachment = sourceList. - getAttachments().get(j); - final ItemAttachment targetAttachment; - if (targetList.getAttachments().size() < j + 1) { - targetAttachment = new ItemAttachment<>(); - } else { - targetAttachment = (ItemAttachment) targetList. - getAttachments().get(j); - } - - if (!sourceAttachment.getAsset().equals(targetAttachment)) { - final Asset oldTargetAsset = targetAttachment.getAsset(); - if (oldTargetAsset != null - && !assetManager.isShared(oldTargetAsset)) { - targetAttachment.setAsset(null); - oldTargetAsset.removeItemAttachment(targetAttachment); - entityManager.remove(oldTargetAsset); - } - - final Asset sourceAsset = sourceAttachment.getAsset(); - final Asset targetAsset; - if (assetManager.isShared(sourceAttachment.getAsset())) { - targetAsset = sourceAttachment.getAsset(); - } else { - try { - targetAsset = sourceAttachment.getAsset().getClass() - .newInstance(); - } catch (InstantiationException | IllegalAccessException ex) { - throw new UnexpectedErrorException(ex); - } - copyAsset(sourceAsset, targetAsset); - - entityManager.persist(targetAsset); - } - - targetAttachment.setAsset(targetAsset); - targetAttachment.setAttachmentList(targetList); - targetAttachment.setSortKey(sourceAttachment.getSortKey()); - targetAttachment.setUuid(UUID.randomUUID().toString()); - } - - targetList.addAttachment(targetAttachment); - - entityManager.persist(targetAttachment); - entityManager.merge(targetList); - } - } - - contentItemRepo.save(liveItem); - - final BeanInfo beanInfo; + final BeanInfo beanInfo; try { beanInfo = Introspector.getBeanInfo(item.getClass()); } catch (IntrospectionException ex) { @@ -1098,6 +1001,93 @@ public class ContentItemManager { liveItem.setVersion(ContentItemVersion.LIVE); contentItemRepo.save(liveItem); + final List oldCategories = liveItem + .getCategories() + .stream() + .map(categorization -> categorization.getCategory()) + .collect(Collectors.toList()); + oldCategories.forEach(category -> { + try { + categoryManager.removeObjectFromCategory(liveItem, category); + } catch (ObjectNotAssignedToCategoryException ex) { + throw new RuntimeException(ex); + } + }); + + draftItem.getCategories().forEach(categorization -> categoryManager + .addObjectToCategory(liveItem, + categorization.getCategory(), + categorization.getType())); + + for (int i = 0; i < draftItem.getAttachments().size(); i++) { + final AttachmentList sourceList = draftItem.getAttachments().get(i); + + final AttachmentList targetList; + if (liveItem.getAttachments().size() < i + 1) { + targetList = new AttachmentList(); + copyLocalizedString(sourceList.getDescription(), + targetList.getDescription()); + + targetList.setItem(liveItem); + liveItem.addAttachmentList(targetList); + targetList.setName(sourceList.getName()); + copyLocalizedString(sourceList.getTitle(), + targetList.getTitle()); + targetList.setOrder(sourceList.getOrder()); + targetList.setUuid(UUID.randomUUID().toString()); + } else { + targetList = liveItem.getAttachments().get(i); + } + + for (int j = 0; j < sourceList.getAttachments().size(); j++) { + final ItemAttachment sourceAttachment = sourceList. + getAttachments().get(j); + final ItemAttachment targetAttachment; + if (targetList.getAttachments().size() < j + 1) { + targetAttachment = new ItemAttachment<>(); + } else { + targetAttachment = (ItemAttachment) targetList. + getAttachments().get(j); + } + + if (!sourceAttachment.getAsset().equals(targetAttachment)) { + final Asset oldTargetAsset = targetAttachment.getAsset(); + if (oldTargetAsset != null + && !assetManager.isShared(oldTargetAsset)) { + targetAttachment.setAsset(null); + oldTargetAsset.removeItemAttachment(targetAttachment); + entityManager.remove(oldTargetAsset); + } + + final Asset sourceAsset = sourceAttachment.getAsset(); + final Asset targetAsset; + if (assetManager.isShared(sourceAttachment.getAsset())) { + targetAsset = sourceAttachment.getAsset(); + } else { + try { + targetAsset = sourceAttachment.getAsset().getClass() + .newInstance(); + } catch (InstantiationException | IllegalAccessException ex) { + throw new UnexpectedErrorException(ex); + } + copyAsset(sourceAsset, targetAsset); + + entityManager.persist(targetAsset); + } + + targetAttachment.setAsset(targetAsset); + targetAttachment.setAttachmentList(targetList); + targetAttachment.setSortKey(sourceAttachment.getSortKey()); + targetAttachment.setUuid(UUID.randomUUID().toString()); + } + + targetList.addAttachment(targetAttachment); + + entityManager.persist(targetAttachment); + entityManager.merge(targetList); + } + } + return liveItem; } diff --git a/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/after-republish.xml b/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/after-republish.xml index acdd6b63b..8b8e50539 100644 --- a/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/after-republish.xml +++ b/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/after-republish.xml @@ -629,7 +629,7 @@