From aa95d070922f1df1909c804a76fb22bcaabe3ceb Mon Sep 17 00:00:00 2001 From: jensp Date: Thu, 3 Nov 2016 11:58:59 +0000 Subject: [PATCH] CCM NG/ccm-cms: Optimised ContentItemManger#publish git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4429 8810af33-2d31-482b-a856-94f89814c4df --- .../librecms/contentsection/AssetManager.java | 191 ++--- .../contentsection/ContentItemManager.java | 683 ++++++++++-------- .../ContentItemManagerTest/after-publish.xml | 6 +- .../after-republish.xml | 54 +- 4 files changed, 508 insertions(+), 426 deletions(-) diff --git a/ccm-cms/src/main/java/org/librecms/contentsection/AssetManager.java b/ccm-cms/src/main/java/org/librecms/contentsection/AssetManager.java index 29b3035a6..961714930 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/AssetManager.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/AssetManager.java @@ -68,7 +68,7 @@ import static org.librecms.CmsConstants.*; public class AssetManager { private static final Logger LOGGER = LogManager. - getLogger(AssetManager.class); + getLogger(AssetManager.class); @Inject private EntityManager entityManager; @@ -94,15 +94,15 @@ public class AssetManager { * thrown. * * - * @param asset The {@link Asset} to share. + * @param asset The {@link Asset} to share. * @param folder The {@link Folder} in which the {@link Asset} is created. */ @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) public void shareAsset( - final Asset asset, - @RequiresPrivilege(AssetPrivileges.CREATE_NEW) - final Folder folder) { + final Asset asset, + @RequiresPrivilege(AssetPrivileges.CREATE_NEW) + final Folder folder) { if (asset == null) { throw new IllegalArgumentException("Can't share asset null."); @@ -114,14 +114,14 @@ public class AssetManager { if (isShared(asset)) { throw new IllegalArgumentException(String.format( - "The asset %s is already shared.", - Objects.toString(asset))); + "The asset %s is already shared.", + Objects.toString(asset))); } categoryManager.addObjectToCategory( - asset, - folder, - CmsConstants.CATEGORIZATION_TYPE_FOLDER); + asset, + folder, + CmsConstants.CATEGORIZATION_TYPE_FOLDER); } /** @@ -132,9 +132,14 @@ public class AssetManager { * @param asset The asset the check. * * @return {@code true} is the {@link Asset} is shared, - * {@code false if not}. + * {@code false if not}. */ public boolean isShared(final Asset asset) { + if (asset == null) { + throw new IllegalArgumentException( + "Can't determine if null is a shared asset."); + } + return getAssetFolder(asset).isPresent(); } @@ -148,9 +153,9 @@ public class AssetManager { final List assets = assetRepo.findAll(); final List orphaned = assets.stream() - .filter(asset -> asset.getCategories().isEmpty() - && asset.getItemAttachments().isEmpty()) - .collect(Collectors.toList()); + .filter(asset -> asset.getCategories().isEmpty() + && asset.getItemAttachments().isEmpty()). + collect(Collectors.toList()); orphaned.forEach(orphan -> assetRepo.delete(orphan)); } @@ -158,17 +163,17 @@ public class AssetManager { /** * Moves an {@link Asset} to an folder. * - * @param asset The {@link Asset} to move. + * @param asset The {@link Asset} to move. * @param targetFolder The folder to which the {@link Asset} is moved. Must - * be an asset folder. + * be an asset folder. */ @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) public void move( - @RequiresPrivilege(AssetPrivileges.EDIT) - final Asset asset, - @RequiresPrivilege(AssetPrivileges.CREATE_NEW) - final Folder targetFolder) { + @RequiresPrivilege(AssetPrivileges.EDIT) + final Asset asset, + @RequiresPrivilege(AssetPrivileges.CREATE_NEW) + final Folder targetFolder) { if (asset == null) { throw new IllegalArgumentException("No asset to move provided."); @@ -180,8 +185,8 @@ public class AssetManager { if (targetFolder.getType() != FolderType.ASSETS_FOLDER) { throw new IllegalArgumentException(String.format( - "The provided target folder %s is not an asset folder.", - Objects.toString(targetFolder))); + "The provided target folder %s is not an asset folder.", + Objects.toString(targetFolder))); } final Optional currentFolder = getAssetFolder(asset); @@ -203,7 +208,7 @@ public class AssetManager { /** * Copies an {@link Asset}. * - * @param asset The {@link Asset} to copy. + * @param asset The {@link Asset} to copy. * @param targetFolder The folder to which the {@link Asset} is copied. * * @return The copy of the {@code asset}. @@ -225,8 +230,8 @@ public class AssetManager { if (targetFolder.getType() != FolderType.ASSETS_FOLDER) { throw new IllegalArgumentException(String.format( - "The provided target folder %s is not an asset folder.", - Objects.toString(targetFolder))); + "The provided target folder %s is not an asset folder.", + Objects.toString(targetFolder))); } final Asset copy; @@ -244,7 +249,7 @@ public class AssetManager { } for (final PropertyDescriptor propertyDescriptor : beanInfo - .getPropertyDescriptors()) { + .getPropertyDescriptors()) { if (propertyIsExcluded(propertyDescriptor.getName())) { continue; } @@ -264,49 +269,49 @@ public class AssetManager { source = (LocalizedString) readMethod.invoke(asset); target = (LocalizedString) readMethod.invoke(copy); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } source.getAvailableLocales().forEach( - locale -> target.addValue(locale, - source.getValue(locale))); + locale -> target.addValue(locale, + source.getValue(locale))); } else if (propType != null - && propType.isAssignableFrom(Asset.class)) { + && propType.isAssignableFrom(Asset.class)) { final Asset linkedAsset; try { linkedAsset = (Asset) readMethod.invoke(asset); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new UncheckedWrapperException(ex); } try { writeMethod.invoke(copy, linkedAsset); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new UncheckedWrapperException(ex); } } else if (propType != null - && propType.isAssignableFrom(List.class)) { + && propType.isAssignableFrom(List.class)) { final List source; final List target; try { source = (List) readMethod.invoke(asset); target = (List) readMethod.invoke(copy); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new UncheckedWrapperException(ex); } target.addAll(source); } else if (propType != null - && propType.isAssignableFrom(Map.class)) { + && propType.isAssignableFrom(Map.class)) { final Map source; final Map target; @@ -314,14 +319,14 @@ public class AssetManager { source = (Map) readMethod.invoke(asset); target = (Map) readMethod.invoke(copy); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } source.forEach((key, value) -> target.put(key, value)); } else if (propType != null - && propType.isAssignableFrom(Set.class)) { + && propType.isAssignableFrom(Set.class)) { final Set source; final Set target; @@ -329,8 +334,8 @@ public class AssetManager { source = (Set) readMethod.invoke(asset); target = (Set) readMethod.invoke(copy); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } @@ -341,8 +346,8 @@ public class AssetManager { value = readMethod.invoke(asset); writeMethod.invoke(copy, value); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new UncheckedWrapperException(ex); } } @@ -350,8 +355,8 @@ public class AssetManager { if (targetFolder.equals(getAssetFolder(asset).orElse(null))) { final long number = assetRepo.countFilterByFolderAndName( - targetFolder, String.format("%s_copy", - asset.getDisplayName())); + targetFolder, String.format("%s_copy", + asset.getDisplayName())); final long index = number + 1; copy.setDisplayName(String.format("%s_copy%d", copy.getDisplayName(), @@ -359,7 +364,7 @@ public class AssetManager { } assetRepo.save(asset); - + categoryManager.addObjectToCategory(copy, targetFolder, CATEGORIZATION_TYPE_FOLDER); @@ -390,10 +395,14 @@ public class AssetManager { * @param asset The {@link Asset} to check for usage. * * @return {@code true} if the {@link Asset} is in use, {@link false} if - * not. + * not. */ @Transactional(Transactional.TxType.REQUIRED) public boolean isAssetInUse(final Asset asset) { + if (asset == null) { + throw new IllegalArgumentException("Can't verify if null is in use."); + } + return !asset.getItemAttachments().isEmpty(); } @@ -405,7 +414,7 @@ public class AssetManager { * @param asset The {@link Assset} for which the path is generated. * * @return The path of the {@link Asset}. If the {@link Asset} is a non - * shared asset the path is empty. + * shared asset the path is empty. * * @see #getAssetPath(org.librecms.assets.Asset, boolean) */ @@ -416,24 +425,23 @@ public class AssetManager { /** * Returns the path of an item as String. * - * @param asset The {@link Asset} for which the path is - * generated. + * @param asset The {@link Asset} for which the path is generated. * @param withContentSection Whether to include the content section into the - * path or not. + * path or not. * * @return The path of the asset. For non shared assets this is an empty - * string. + * string. * * @see #getAssetPath(org.librecms.assets.Asset) */ public String getAssetPath(final Asset asset, final boolean withContentSection) { final List result = asset.getCategories().stream() - .filter(categorization -> { - return CATEGORIZATION_TYPE_FOLDER.equals( - categorization.getType()); - }) - .collect(Collectors.toList()); + .filter(categorization -> { + return CATEGORIZATION_TYPE_FOLDER.equals( + categorization.getType()); + }) + .collect(Collectors.toList()); if (result.isEmpty()) { return ""; @@ -452,8 +460,8 @@ public class AssetManager { if (withContentSection) { final String sectionName - = ((Folder) result.get(0).getCategory()). - getSection().getDisplayName(); + = ((Folder) result.get(0).getCategory()). + getSection().getDisplayName(); return String.format("%s:/%s", sectionName, path); } else { return String.format("/%s", path); @@ -467,15 +475,15 @@ public class AssetManager { * @param asset * * @return A list of the folders which form the path of the asset. For non - * shared assets an empty list is returned. + * shared assets an empty list is returned. */ public List getAssetFolders(final Asset asset) { final List result = asset.getCategories().stream() - .filter(categorization -> { - return CATEGORIZATION_TYPE_FOLDER.equals(categorization - .getType()); - }) - .collect(Collectors.toList()); + .filter(categorization -> { + return CATEGORIZATION_TYPE_FOLDER.equals(categorization + .getType()); + }) + .collect(Collectors.toList()); final List folders = new ArrayList<>(); if (!result.isEmpty()) { @@ -484,12 +492,12 @@ public class AssetManager { folders.add((Folder) current); } else { throw new IllegalArgumentException(String.format( - "The asset %s is assigned to the category %s with the" - + "categorization type \"%s\", but the Category is not" + "The asset %s is assigned to the category %s with the" + + "categorization type \"%s\", but the Category is not" + "a folder. This is no supported.", - asset.getUuid(), - current.getUuid(), - CATEGORIZATION_TYPE_FOLDER)); + asset.getUuid(), + current.getUuid(), + CATEGORIZATION_TYPE_FOLDER)); } while (current.getParentCategory() != null) { @@ -498,12 +506,12 @@ public class AssetManager { folders.add((Folder) current); } else { throw new IllegalArgumentException(String.format( - "The asset %s is assigned to the category %s with the" + "The asset %s is assigned to the category %s with the" + "categorization type \"%s\", but the Category is not" - + "a folder. This is no supported.", - asset.getUuid(), - current.getUuid(), - CATEGORIZATION_TYPE_FOLDER)); + + "a folder. This is no supported.", + asset.getUuid(), + current.getUuid(), + CATEGORIZATION_TYPE_FOLDER)); } } } @@ -518,18 +526,27 @@ public class AssetManager { * @param asset The asset. * * @return The folder in which the asset is placed. If the asset is a non - * shared asset an empty {@link Optional} is returned. + * shared asset an empty {@link Optional} is returned. */ public Optional getAssetFolder(final Asset asset) { + if (asset == null) { + throw new IllegalArgumentException( + "Can't retrieve the folder for asset null."); + } + + if (asset.getCategories() == null) { + return Optional.empty(); + } + return asset.getCategories().stream() - .filter(categorization -> { - return CATEGORIZATION_TYPE_FOLDER.equals( - categorization.getType()); - }) - .map(categorization -> { - return (Folder) categorization.getCategory(); - }) - .findFirst(); + .filter(categorization -> { + return CATEGORIZATION_TYPE_FOLDER.equals( + categorization.getType()); + }) + .map(categorization -> { + return (Folder) categorization.getCategory(); + }) + .findFirst(); } } 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 e681738bd..b0dd3347a 100644 --- a/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemManager.java +++ b/ccm-cms/src/main/java/org/librecms/contentsection/ContentItemManager.java @@ -64,7 +64,6 @@ import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.UUID; -import java.util.logging.Level; import javax.persistence.EntityManager; import javax.persistence.TypedQuery; @@ -79,7 +78,7 @@ import javax.transaction.Transactional; public class ContentItemManager { private static final Logger LOGGER = LogManager.getLogger( - ContentItemManager.class); + ContentItemManager.class); @Inject private EntityManager entityManager; @@ -122,31 +121,31 @@ public class ContentItemManager { * {@link ContentSection#rootDocumentsFolder} of the provided content * section. Otherwise an {@link IllegalArgumentException} is thrown. * - * @param The type of the content item. - * @param name The name (URL stub) of the new content item. + * @param The type of the content item. + * @param name The name (URL stub) of the new content item. * @param section The content section in which the item is generated. - * @param folder The folder in which in the item is stored. - * @param type The type of the new content item. + * @param folder The folder in which in the item is stored. + * @param type The type of the new content item. * * @return The new content item. */ @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) public T createContentItem( - final String name, - final ContentSection section, - @RequiresPrivilege(ItemPrivileges.CREATE_NEW) - final Folder folder, - final Class type) { + final String name, + final ContentSection section, + @RequiresPrivilege(ItemPrivileges.CREATE_NEW) + final Folder folder, + final Class type) { final Optional contentType = typeRepo - .findByContentSectionAndClass(section, type); + .findByContentSectionAndClass(section, type); if (!contentType.isPresent()) { throw new IllegalArgumentException(String.format( - "ContentSection \"%s\" has no content type for \"%s\".", - section.getLabel(), - type.getName())); + "ContentSection \"%s\" has no content type for \"%s\".", + section.getLabel(), + type.getName())); } return createContentItem(name, @@ -168,40 +167,39 @@ public class ContentItemManager { * provided content section. Otherwise an {@link IllegalArgumentException} * is thrown. * - * @param The type of the content item. - * @param name The name (URL stub) of the new content item. - * @param section The content section in which the item is - * generated. - * @param folder The folder in which in the item is stored. + * @param The type of the content item. + * @param name The name (URL stub) of the new content item. + * @param section The content section in which the item is generated. + * @param folder The folder in which in the item is stored. * @param workflowTemplate The template for the workflow to apply to the new - * item. - * @param type The type of the new content item. + * item. + * @param type The type of the new content item. * * @return The new content item. */ @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) public T createContentItem( - final String name, - final ContentSection section, - @RequiresPrivilege(ItemPrivileges.CREATE_NEW) - final Folder folder, - final WorkflowTemplate workflowTemplate, - final Class type) { + final String name, + final ContentSection section, + @RequiresPrivilege(ItemPrivileges.CREATE_NEW) + final Folder folder, + final WorkflowTemplate workflowTemplate, + final Class type) { final Optional contentType = typeRepo - .findByContentSectionAndClass(section, type); + .findByContentSectionAndClass(section, type); if (!contentType.isPresent()) { throw new IllegalArgumentException(String.format( - "ContentSection \"%s\" has no content type for \"%s\".", - section.getLabel(), - type.getName())); + "ContentSection \"%s\" has no content type for \"%s\".", + section.getLabel(), + type.getName())); } if (name == null || name.trim().isEmpty()) { throw new IllegalArgumentException( - "The name of a content item can't be blank."); + "The name of a content item can't be blank."); } final T item; @@ -209,14 +207,14 @@ public class ContentItemManager { item = type.newInstance(); } catch (InstantiationException | IllegalAccessException ex) { LOGGER.error("Failed to create new content item of type \"{}\" " - + "in content section \"{}\".", + + "in content section \"{}\".", type.getName(), section.getLabel()); throw new RuntimeException(ex); } final KernelConfig kernelConfig = confManager.findConfiguration( - KernelConfig.class); + KernelConfig.class); item.setDisplayName(name); item.getName().addValue(kernelConfig.getDefaultLocale(), @@ -227,16 +225,16 @@ public class ContentItemManager { if (workflowTemplate != null) { final Workflow workflow = workflowManager.createWorkflow( - workflowTemplate); + workflowTemplate); item.setWorkflow(workflow); } contentItemRepo.save(item); categoryManager.addObjectToCategory( - item, - folder, - CATEGORIZATION_TYPE_FOLDER); + item, + folder, + CATEGORIZATION_TYPE_FOLDER); contentItemRepo.save(item); @@ -254,23 +252,23 @@ public class ContentItemManager { * only moves the draft version of the item. The live version is moved after * a the item is republished. * - * @param item The item to move. + * @param item The item to move. * @param targetFolder The folder to which the item is moved. */ @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) public void move( - @RequiresPrivilege(ItemPrivileges.EDIT) - final ContentItem item, - @RequiresPrivilege(ItemPrivileges.CREATE_NEW) - final Folder targetFolder) { + @RequiresPrivilege(ItemPrivileges.EDIT) + final ContentItem item, + @RequiresPrivilege(ItemPrivileges.CREATE_NEW) + final Folder targetFolder) { if (item == null) { throw new IllegalArgumentException("The item to move can't be null."); } if (targetFolder == null) { throw new IllegalArgumentException( - "The target folder can't be null."); + "The target folder can't be null."); } final ContentItem draftItem = getDraftVersion(item, item.getClass()); @@ -279,18 +277,18 @@ public class ContentItemManager { if (!sectionManager.hasContentType(draftItem.getClass(), targetFolder.getSection())) { throw new IllegalArgumentException(String.format( - "Can't move item %d:\"%s\" to folder \"%s\"." - + "The target folder %d:\"%s\" belongs to content section " + "Can't move item %d:\"%s\" to folder \"%s\"." + + "The target folder %d:\"%s\" belongs to content section " + "%d:\"%s\". The content type \"%s\" has not registered" - + "for this section.", - draftItem.getObjectId(), - draftItem.getDisplayName(), - folderManager.getFolderPath(targetFolder, true), - targetFolder.getObjectId(), - targetFolder.getDisplayName(), - targetFolder.getSection().getObjectId(), - targetFolder.getSection().getDisplayName(), - draftItem.getClass().getName())); + + "for this section.", + draftItem.getObjectId(), + draftItem.getDisplayName(), + folderManager.getFolderPath(targetFolder, true), + targetFolder.getObjectId(), + targetFolder.getDisplayName(), + targetFolder.getSection().getObjectId(), + targetFolder.getSection().getDisplayName(), + draftItem.getClass().getName())); } if (currentFolder.isPresent()) { @@ -303,9 +301,9 @@ public class ContentItemManager { } categoryManager.addObjectToCategory( - draftItem, - targetFolder, - CATEGORIZATION_TYPE_FOLDER); + draftItem, + targetFolder, + CATEGORIZATION_TYPE_FOLDER); } @@ -319,11 +317,10 @@ public class ContentItemManager { * section and the type of the item is not registered for the target section * an {@link IllegalArgumentException} is thrown. * - * @param item The item to copy. + * @param item The item to copy. * @param targetFolder The folder in which the copy is created. If the - * target folder is the same folder as the folder of the - * original item an index is appended to the name of the - * item. + * target folder is the same folder as the folder of the original item an + * index is appended to the name of the item. * * @return The copy of the item */ @@ -331,9 +328,9 @@ public class ContentItemManager { @AuthorizationRequired @SuppressWarnings("unchecked") public ContentItem copy( - final ContentItem item, - @RequiresPrivilege(ItemPrivileges.CREATE_NEW) - final Folder targetFolder) { + final ContentItem item, + @RequiresPrivilege(ItemPrivileges.CREATE_NEW) + final Folder targetFolder) { if (item == null) { throw new IllegalArgumentException("The item to copy can't be null."); @@ -341,36 +338,36 @@ public class ContentItemManager { if (targetFolder == null) { throw new IllegalArgumentException( - "The target folder to which the item is copied can't be null"); + "The target folder to which the item is copied can't be null"); } final Optional contentType = typeRepo - .findByContentSectionAndClass( - targetFolder.getSection(), item.getClass()); + .findByContentSectionAndClass( + targetFolder.getSection(), item.getClass()); if (!contentType.isPresent()) { throw new IllegalArgumentException(String.format( - "ContentSection \"%s\" has no content type for \"%s\".", - item.getContentType().getContentSection(), - item.getClass().getName())); + "ContentSection \"%s\" has no content type for \"%s\".", + item.getContentType().getContentSection(), + item.getClass().getName())); } final ContentItem draftItem = getDraftVersion(item, item.getClass()); if (!sectionManager.hasContentType(draftItem.getClass(), targetFolder.getSection())) { throw new IllegalArgumentException(String.format( - "Can't copy item %d:\"%s\" to folder \"%s\"." - + "The target folder %d:\"%s\" belongs to content section " + "Can't copy item %d:\"%s\" to folder \"%s\"." + + "The target folder %d:\"%s\" belongs to content section " + "%d:\"%s\". The content type \"%s\" has not registered" - + "for this section.", - draftItem.getObjectId(), - draftItem.getDisplayName(), - folderManager.getFolderPath(targetFolder, true), - targetFolder.getObjectId(), - targetFolder.getDisplayName(), - targetFolder.getSection().getObjectId(), - targetFolder.getSection().getDisplayName(), - draftItem.getClass().getName())); + + "for this section.", + draftItem.getObjectId(), + draftItem.getDisplayName(), + folderManager.getFolderPath(targetFolder, true), + targetFolder.getObjectId(), + targetFolder.getDisplayName(), + targetFolder.getSection().getObjectId(), + targetFolder.getSection().getDisplayName(), + draftItem.getClass().getName())); } final ContentItem copy; @@ -384,31 +381,31 @@ public class ContentItemManager { if (draftItem.getWorkflow() != null) { final WorkflowTemplate template = draftItem.getWorkflow() - .getTemplate(); + .getTemplate(); final Workflow copyWorkflow = workflowManager.createWorkflow( - template); + template); copy.setWorkflow(copyWorkflow); } contentItemRepo.save(copy); draftItem.getCategories().forEach(categorization -> categoryManager - .addObjectToCategory(copy, categorization.getCategory())); + .addObjectToCategory(copy, categorization.getCategory())); final Optional itemFolder = getItemFolder(draftItem); if (itemFolder.isPresent()) { try { categoryManager.removeObjectFromCategory( - copy, getItemFolder(draftItem).get()); + copy, getItemFolder(draftItem).get()); } catch (ObjectNotAssignedToCategoryException ex) { throw new RuntimeException(ex); } } categoryManager.addObjectToCategory( - copy, - targetFolder, - CATEGORIZATION_TYPE_FOLDER); + copy, + targetFolder, + CATEGORIZATION_TYPE_FOLDER); for (AttachmentList attachmentList : item.getAttachments()) { copyAttachmentList(attachmentList, copy); @@ -422,7 +419,7 @@ public class ContentItemManager { } for (final PropertyDescriptor propertyDescriptor : beanInfo - .getPropertyDescriptors()) { + .getPropertyDescriptors()) { if (propertyIsExcluded(propertyDescriptor.getName())) { continue; } @@ -442,52 +439,50 @@ public class ContentItemManager { source = (LocalizedString) readMethod.invoke(draftItem); target = (LocalizedString) readMethod.invoke(copy); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } - source.getAvailableLocales().forEach( - locale -> target.addValue(locale, - source.getValue(locale))); + copyLocalizedString(source, target); } else if (propType != null - && propType.isAssignableFrom(ContentItem.class)) { + && propType.isAssignableFrom(ContentItem.class)) { final ContentItem linkedItem; try { linkedItem = (ContentItem) readMethod.invoke(draftItem); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } final ContentItem linkedDraftItem = getDraftVersion( - linkedItem, linkedItem.getClass()); + linkedItem, linkedItem.getClass()); try { writeMethod.invoke(copy, linkedDraftItem); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } } else if (propType != null - && propType.isAssignableFrom(List.class)) { + && propType.isAssignableFrom(List.class)) { final List source; final List target; try { source = (List) readMethod.invoke(draftItem); target = (List) readMethod.invoke(copy); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } target.addAll(source); } else if (propType != null - && propType.isAssignableFrom(Map.class)) { + && propType.isAssignableFrom(Map.class)) { final Map source; final Map target; @@ -495,14 +490,14 @@ public class ContentItemManager { source = (Map) readMethod.invoke(draftItem); target = (Map) readMethod.invoke(copy); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } source.forEach((key, value) -> target.put(key, value)); } else if (propType != null - && propType.isAssignableFrom(Set.class)) { + && propType.isAssignableFrom(Set.class)) { final Set source; final Set target; @@ -510,8 +505,8 @@ public class ContentItemManager { source = (Set) readMethod.invoke(draftItem); target = (Set) readMethod.invoke(copy); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } @@ -522,8 +517,8 @@ public class ContentItemManager { value = readMethod.invoke(draftItem); writeMethod.invoke(copy, value); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } } @@ -531,8 +526,8 @@ public class ContentItemManager { if (targetFolder.equals(getItemFolder(item).orElse(null))) { final long number = contentItemRepo.countFilterByFolderAndName( - targetFolder, String.format("%s_copy", - item.getDisplayName())); + targetFolder, String.format("%s_copy", + item.getDisplayName())); final long index = number + 1; copy.setDisplayName(String.format("%s_copy%d", copy.getDisplayName(), @@ -564,25 +559,19 @@ public class ContentItemManager { return result; } - private void copyAttachmentList(final AttachmentList list, + private void copyAttachmentList(final AttachmentList sourceList, final ContentItem target) { final AttachmentList targetList = new AttachmentList(); - for (final Locale locale : list.getDescription().getAvailableLocales()) { - targetList.getDescription().addValue( - locale, list.getDescription().getValue(locale)); - } + copyLocalizedString(sourceList.getDescription(), targetList.getDescription()); targetList.setItem(target); - targetList.setName(list.getName()); - targetList.setOrder(list.getOrder()); - for (Map.Entry title : list.getTitle().getValues() - .entrySet()) { - targetList.getTitle().addValue(title.getKey(), title.getValue()); - } + targetList.setName(sourceList.getName()); + targetList.setOrder(sourceList.getOrder()); + copyLocalizedString(sourceList.getTitle(), targetList.getTitle()); targetList.setUuid(UUID.randomUUID().toString()); - entityManager.persist(list); + entityManager.persist(sourceList); - for (ItemAttachment attachment : list.getAttachments()) { + for (ItemAttachment attachment : sourceList.getAttachments()) { if (assetManager.isShared(attachment.getAsset())) { copySharedAssetAttachment(attachment, targetList); } else { @@ -590,7 +579,7 @@ public class ContentItemManager { } } - entityManager.merge(list); + entityManager.merge(sourceList); } private void copySharedAssetAttachment(final ItemAttachment attachment, @@ -616,6 +605,39 @@ public class ContentItemManager { throw new UncheckedWrapperException(ex); } + copyAsset(source, target); + + entityManager.persist(target); + final ItemAttachment targetAttachment = new ItemAttachment<>(); + targetAttachment.setAsset(target); + targetAttachment.setSortKey(attachment.getSortKey()); + targetAttachment.setUuid(UUID.randomUUID().toString()); + entityManager.persist(targetAttachment); + + targetAttachment.setAttachmentList(targetList); + targetList.addAttachment(targetAttachment); + entityManager.merge(targetAttachment); + } + + public void copyAsset(final Asset source, final Asset target) { + if (source == null) { + throw new IllegalArgumentException("Source Asset can't be null."); + } + + if (target == null) { + throw new IllegalArgumentException("Target Asset can't be null."); + } + + if (!source.getClass().equals(target.getClass())) { + throw new IllegalArgumentException(String.format( + "Asset belong to different classes. source is instance of " + + "\"%s\", target is instance of \"%s\". It is not " + + "possible to use assets of diffierent classes " + + "with this method", + source.getClass().getName(), + target.getClass().getName())); + } + final BeanInfo beanInfo; try { beanInfo = Introspector.getBeanInfo(source.getClass()); @@ -623,13 +645,13 @@ public class ContentItemManager { throw new UncheckedWrapperException(ex); } - for (final PropertyDescriptor propertyDescriptor : beanInfo - .getPropertyDescriptors()) { + for (final PropertyDescriptor propertyDescriptor : beanInfo. + getPropertyDescriptors()) { final String propertyName = propertyDescriptor.getName(); if ("objectId".equals(propertyName) - || "uuid".equals(propertyName) - || "itemAttachments".equals(propertyName) - || "categories".equals(propertyName)) { + || "uuid".equals(propertyName) + || "itemAttachments".equals(propertyName) + || "categories".equals(propertyName)) { continue; } @@ -648,39 +670,35 @@ public class ContentItemManager { sourceStr = (LocalizedString) readMethod.invoke(source); targetStr = (LocalizedString) readMethod.invoke(target); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new UncheckedWrapperException(ex); } - sourceStr.getAvailableLocales().forEach( - locale -> targetStr.addValue(locale, - sourceStr.getValue(locale))); + copyLocalizedString(sourceStr, targetStr); } else { final Object value; try { value = readMethod.invoke(source); writeMethod.invoke(target, value); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new UncheckedWrapperException(ex); } } } - target.setUuid(UUID.randomUUID().toString()); + if (target.getUuid() == null || target.getUuid().isEmpty()) { + target.setUuid(UUID.randomUUID().toString()); + } + } - entityManager.persist(target); - final ItemAttachment targetAttachment = new ItemAttachment<>(); - targetAttachment.setAsset(target); - targetAttachment.setSortKey(attachment.getSortKey()); - targetAttachment.setUuid(UUID.randomUUID().toString()); - entityManager.persist(targetAttachment); - - targetAttachment.setAttachmentList(targetList); - targetList.addAttachment(targetAttachment); - entityManager.merge(targetAttachment); + private void copyLocalizedString(final LocalizedString source, + final LocalizedString target) { + for (final Locale locale : source.getAvailableLocales()) { + target.addValue(locale, source.getValue(locale)); + } } /** @@ -695,16 +713,16 @@ public class ContentItemManager { @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) public ContentItem publish( - @RequiresPrivilege(ItemPrivileges.PUBLISH) - final ContentItem item) { + @RequiresPrivilege(ItemPrivileges.PUBLISH) + final ContentItem item) { if (item == null) { throw new IllegalArgumentException( - "The item to publish can't be null."); + "The item to publish can't be null."); } final LifecycleDefinition lifecycleDefinition = item.getContentType() - .getDefaultLifecycle(); + .getDefaultLifecycle(); return publish(item, lifecycleDefinition); } @@ -713,9 +731,9 @@ public class ContentItemManager { * Creates a live version of content item or updates the live version of a * content item if there already a live version. * - * @param item The content item to publish. + * @param item The content item to publish. * @param lifecycleDefinition The definition of the lifecycle to use for the - * new item. + * new item. * * @return The published content item. */ @@ -723,51 +741,50 @@ public class ContentItemManager { @Transactional(Transactional.TxType.REQUIRED) @SuppressWarnings("unchecked") public ContentItem publish( - @RequiresPrivilege(ItemPrivileges.PUBLISH) - final ContentItem item, - final LifecycleDefinition lifecycleDefinition) { + @RequiresPrivilege(ItemPrivileges.PUBLISH) + final ContentItem item, + final LifecycleDefinition lifecycleDefinition) { + if (item == null) { throw new IllegalArgumentException( - "The item to publish can't be null."); + "The item to publish can't be null."); } if (lifecycleDefinition == null) { throw new IllegalArgumentException( - "The lifecycle definition for the " - + "lifecycle of the item to publish can't be null."); + "The lifecycle definition for the " + + "lifecycle of the item to publish can't be null."); } final ContentItem draftItem = getDraftVersion(item, ContentItem.class); final ContentItem liveItem; if (isLive(item)) { - final ContentItem oldLiveItem = getLiveVersion( - item, ContentItem.class).get(); - unpublish(oldLiveItem); + liveItem = getLiveVersion(item, ContentItem.class).get(); +// unpublish(oldLiveItem); + } else { + try { + liveItem = draftItem.getClass().newInstance(); + } catch (InstantiationException | IllegalAccessException ex) { + throw new RuntimeException(ex); + } } -// else { - try { - liveItem = draftItem.getClass().newInstance(); - } catch (InstantiationException | IllegalAccessException ex) { - throw new RuntimeException(ex); - } -// } liveItem.setVersion(ContentItemVersion.PUBLISHING); liveItem.setItemUuid(draftItem.getItemUuid()); liveItem.setContentType(draftItem.getContentType()); final Lifecycle lifecycle = lifecycleManager.createLifecycle( - lifecycleDefinition); + lifecycleDefinition); liveItem.setLifecycle(lifecycle); liveItem.setWorkflow(draftItem.getWorkflow()); final List oldCategories = liveItem - .getCategories() - .stream() - .map(categorization -> categorization.getCategory()) - .collect(Collectors.toList()); + .getCategories() + .stream() + .map(categorization -> categorization.getCategory()) + .collect(Collectors.toList()); oldCategories.forEach(category -> { try { categoryManager.removeObjectFromCategory(liveItem, category); @@ -777,14 +794,82 @@ public class ContentItemManager { }); draftItem.getCategories().forEach(categorization -> categoryManager - .addObjectToCategory(liveItem, - categorization.getCategory(), - categorization.getType())); + .addObjectToCategory(liveItem, + categorization.getCategory(), + categorization.getType())); - for (AttachmentList attachmentList : item.getAttachments()) { - copyAttachmentList(attachmentList, 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 UncheckedWrapperException(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); + } } +// for (AttachmentList attachmentList : item.getAttachments()) { +// copyAttachmentList(attachmentList, liveItem); +// } final BeanInfo beanInfo; try { beanInfo = Introspector.getBeanInfo(item.getClass()); @@ -793,7 +878,7 @@ public class ContentItemManager { } for (final PropertyDescriptor propertyDescriptor : beanInfo - .getPropertyDescriptors()) { + .getPropertyDescriptors()) { if (propertyIsExcluded(propertyDescriptor.getName())) { continue; @@ -814,56 +899,54 @@ public class ContentItemManager { source = (LocalizedString) readMethod.invoke(draftItem); target = (LocalizedString) readMethod.invoke(liveItem); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } - source.getAvailableLocales().forEach( - locale -> target.addValue(locale, source. - getValue(locale))); + copyLocalizedString(source, target); } else if (propType != null - && propType.isAssignableFrom(ContentItem.class)) { + && propType.isAssignableFrom(ContentItem.class)) { final ContentItem linkedItem; try { linkedItem = (ContentItem) readMethod.invoke(draftItem); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } final ContentItem linkedDraftItem = getDraftVersion( - linkedItem, linkedItem.getClass()); + linkedItem, linkedItem.getClass()); if (isLive(linkedDraftItem)) { try { final Optional linkedLiveItem - = getLiveVersion( - linkedDraftItem, ContentItem.class); + = getLiveVersion( + linkedDraftItem, ContentItem.class); writeMethod.invoke(liveItem, linkedLiveItem); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } } } else if (propType != null - && propType.isAssignableFrom(List.class)) { + && propType.isAssignableFrom(List.class)) { final List source; final List target; try { source = (List) readMethod.invoke(draftItem); target = (List) readMethod.invoke(liveItem); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } target.addAll(source); } else if (propType != null - && propType.isAssignableFrom(Map.class)) { + && propType.isAssignableFrom(Map.class)) { final Map source; final Map target; @@ -871,14 +954,14 @@ public class ContentItemManager { source = (Map) readMethod.invoke(draftItem); target = (Map) readMethod.invoke(liveItem); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } source.forEach((key, value) -> target.put(key, value)); } else if (propType != null - && propType.isAssignableFrom(Set.class)) { + && propType.isAssignableFrom(Set.class)) { final Set source; final Set target; @@ -886,8 +969,8 @@ public class ContentItemManager { source = (Set) readMethod.invoke(draftItem); target = (Set) readMethod.invoke(liveItem); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } @@ -898,8 +981,8 @@ public class ContentItemManager { value = readMethod.invoke(item); writeMethod.invoke(liveItem, value); } catch (IllegalAccessException - | IllegalArgumentException - | InvocationTargetException ex) { + | IllegalArgumentException + | InvocationTargetException ex) { throw new RuntimeException(ex); } } @@ -921,8 +1004,8 @@ public class ContentItemManager { @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) public void publish( - @RequiresPrivilege(ItemPrivileges.PUBLISH) - final Folder folder) { + @RequiresPrivilege(ItemPrivileges.PUBLISH) + final Folder folder) { // Ensure that we are using a fresh folder and that the folder was // retrieved in this transaction to avoid problems with lazy fetched @@ -930,10 +1013,10 @@ public class ContentItemManager { final Folder theFolder = folderRepo.findById(folder.getObjectId()); theFolder.getObjects() - .stream() - .map(categorization -> categorization.getCategorizedObject()) - .filter(object -> object instanceof ContentItem) - .forEach(item -> publish((ContentItem) item)); + .stream() + .map(categorization -> categorization.getCategorizedObject()) + .filter(object -> object instanceof ContentItem) + .forEach(item -> publish((ContentItem) item)); } /** @@ -945,17 +1028,17 @@ public class ContentItemManager { @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) public void unpublish( - @RequiresPrivilege(ItemPrivileges.PUBLISH) - final ContentItem item) { + @RequiresPrivilege(ItemPrivileges.PUBLISH) + final ContentItem item) { if (item == null) { throw new IllegalArgumentException( - "The item to unpublish can't be null"); + "The item to unpublish can't be null"); } LOGGER.debug("Unpublishing item {}...", item.getItemUuid()); final Optional liveItem = getLiveVersion( - item, ContentItem.class); + item, ContentItem.class); if (!liveItem.isPresent()) { LOGGER.info("ContentItem {} has no live version.", @@ -964,20 +1047,20 @@ public class ContentItemManager { } final List attachmentLists = liveItem.get() - .getAttachments(); + .getAttachments(); for (final AttachmentList attachmentList : attachmentLists) { attachmentList.getAttachments().forEach( - attachment -> { - unpublishAttachment(attachment); - }); + attachment -> { + unpublishAttachment(attachment); + }); } final List categories = liveItem - .get() - .getCategories() - .stream() - .map(categorization -> categorization.getCategory()) - .collect(Collectors.toList()); + .get() + .getCategories() + .stream() + .map(categorization -> categorization.getCategory()) + .collect(Collectors.toList()); categories.forEach(category -> { try { @@ -1018,8 +1101,8 @@ public class ContentItemManager { @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) public void unpublish( - @RequiresPrivilege(ItemPrivileges.PUBLISH) - final Folder folder) { + @RequiresPrivilege(ItemPrivileges.PUBLISH) + final Folder folder) { // Ensure that we are using a fresh folder and that the folder was // retrieved in this transaction to avoid problems with lazy fetched @@ -1027,12 +1110,12 @@ public class ContentItemManager { final Folder theFolder = folderRepo.findById(folder.getObjectId()); theFolder.getObjects() - .stream() - .map(categorization -> categorization.getCategorizedObject()) - .filter(object -> object instanceof ContentItem) - .map(object -> (ContentItem) object) - .filter(item -> isLive(item)) - .forEach(item -> unpublish(item)); + .stream() + .map(categorization -> categorization.getCategorizedObject()) + .filter(object -> object instanceof ContentItem) + .map(object -> (ContentItem) object) + .filter(item -> isLive(item)) + .forEach(item -> unpublish(item)); } /** @@ -1041,12 +1124,12 @@ public class ContentItemManager { * @param item The item * * @return {@code true} if the content item has a live version, - * {@code false} if not. + * {@code false} if not. */ @Transactional(Transactional.TxType.REQUIRED) public boolean isLive(final ContentItem item) { final TypedQuery query = entityManager.createNamedQuery( - "ContentItem.hasLiveVersion", Boolean.class); + "ContentItem.hasLiveVersion", Boolean.class); query.setParameter("uuid", item.getItemUuid()); return query.getSingleResult(); @@ -1055,35 +1138,35 @@ public class ContentItemManager { /** * Retrieves the live version of the provided content item if any. * - * @param Type of the content item. + * @param Type of the content item. * @param item The item of which the live version should be retrieved. * @param type Type of the content item. * * @return The live version of an item. If the item provided is already the - * live version the provided item is returned, otherwise the live - * version is returned. If there is no live version an empty - * {@link Optional} is returned. + * live version the provided item is returned, otherwise the live version is + * returned. If there is no live version an empty {@link Optional} is + * returned. */ @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) @SuppressWarnings({"unchecked"}) public Optional getLiveVersion( - @RequiresPrivilege(ItemPrivileges.VIEW_PUBLISHED) - final ContentItem item, - final Class type) { + @RequiresPrivilege(ItemPrivileges.VIEW_PUBLISHED) + final ContentItem item, + final Class type) { if (!ContentItem.class.isAssignableFrom(type)) { throw new IllegalArgumentException(String.format( - "The provided type \"%s\" does match the type of the provided " + "The provided type \"%s\" does match the type of the provided " + "item (\"%s\").", - type.getName(), - item.getClass().getName())); + type.getName(), + item.getClass().getName())); } if (isLive(item)) { final TypedQuery query = entityManager - .createNamedQuery( - "ContentItem.findLiveVersion", ContentItem.class); + .createNamedQuery( + "ContentItem.findLiveVersion", ContentItem.class); query.setParameter("uuid", item.getItemUuid()); final ContentItem result = query.getSingleResult(); @@ -1100,50 +1183,50 @@ public class ContentItemManager { /** * Retrieves the pending versions of an item if there are any. * - * @param Type of the content item to retrieve. + * @param Type of the content item to retrieve. * @param item The item of which the pending versions are retrieved. * @param type Type of the content item to retrieve. * * @return A list of the pending versions of the item. */ public List getPendingVersions( - final ContentItem item, - final Class type) { + final ContentItem item, + final Class type) { throw new UnsupportedOperationException(); } /** * Retrieves the draft version * - * @param Type of the item. + * @param Type of the item. * @param item The item of which the draft version is retrieved. * @param type Type of the item. * * @return The draft version of the provided content item. If the provided - * item is the draft version the provided item is simply returned. - * Otherwise the draft version is retrieved from the database and is - * returned. Each content item has a draft version (otherwise - * something is seriously wrong with the database) this method will + * item is the draft version the provided item is simply returned. Otherwise + * the draft version is retrieved from the database and is returned. Each + * content item has a draft version (otherwise something is seriously wrong + * with the database) this method will * never return {@code null}. */ @AuthorizationRequired @Transactional(Transactional.TxType.REQUIRED) @SuppressWarnings("unchecked") public T getDraftVersion( - @RequiresPrivilege(ItemPrivileges.PREVIEW) - final ContentItem item, - final Class type) { + @RequiresPrivilege(ItemPrivileges.PREVIEW) + final ContentItem item, + final Class type) { if (!ContentItem.class.isAssignableFrom(type)) { throw new IllegalArgumentException(String.format( - "The provided type \"%s\" does match the type of the provided " + "The provided type \"%s\" does match the type of the provided " + "item (\"%s\").", - type.getName(), - item.getClass().getName())); + type.getName(), + item.getClass().getName())); } final TypedQuery query = entityManager.createNamedQuery( - "ContentItem.findDraftVersion", ContentItem.class); + "ContentItem.findDraftVersion", ContentItem.class); query.setParameter("uuid", item.getItemUuid()); return (T) query.getSingleResult(); @@ -1182,9 +1265,9 @@ public class ContentItemManager { * {@code info}, the path including the content section would be * {@code info:/research/computer-science/artificial-intelligence/neural-nets}. * - * @param item The item whose path is generated. + * @param item The item whose path is generated. * @param withContentSection Whether to include the content section into the - * path. + * path. * * @return The path of the content item * @@ -1193,11 +1276,11 @@ public class ContentItemManager { public String getItemPath(final ContentItem item, final boolean withContentSection) { final List result = item.getCategories().stream() - .filter(categorization -> { - return CATEGORIZATION_TYPE_FOLDER.equals( - categorization.getType()); - }) - .collect(Collectors.toList()); + .filter(categorization -> { + return CATEGORIZATION_TYPE_FOLDER.equals( + categorization.getType()); + }) + .collect(Collectors.toList()); if (result.isEmpty()) { return item.getDisplayName(); @@ -1216,9 +1299,9 @@ public class ContentItemManager { if (withContentSection) { final String sectionName = item.getContentType(). - getContentSection().getDisplayName(); + getContentSection().getDisplayName(); return String.format( - "%s:/%s", sectionName, path); + "%s:/%s", sectionName, path); } else { return String.format("/%s", path); } @@ -1234,11 +1317,11 @@ public class ContentItemManager { */ public List getItemFolders(final ContentItem item) { final List result = item.getCategories().stream() - .filter(categorization -> { - return CATEGORIZATION_TYPE_FOLDER.equals( - categorization.getType()); - }) - .collect(Collectors.toList()); + .filter(categorization -> { + return CATEGORIZATION_TYPE_FOLDER.equals( + categorization.getType()); + }) + .collect(Collectors.toList()); final List folders = new ArrayList<>(); if (!result.isEmpty()) { @@ -1247,12 +1330,12 @@ public class ContentItemManager { folders.add((Folder) current); } else { throw new IllegalArgumentException(String.format( - "The item %s is assigned to the category %s with the" - + "categorization type \"%s\", but the Category is not" + "The item %s is assigned to the category %s with the" + + "categorization type \"%s\", but the Category is not" + "a folder. This is no supported.", - item.getUuid(), - current.getUuid(), - CATEGORIZATION_TYPE_FOLDER)); + item.getUuid(), + current.getUuid(), + CATEGORIZATION_TYPE_FOLDER)); } while (current.getParentCategory() != null) { @@ -1261,12 +1344,12 @@ public class ContentItemManager { folders.add((Folder) current); } else { throw new IllegalArgumentException(String.format( - "The item %s is assigned to the category %s with the" + "The item %s is assigned to the category %s with the" + "categorization type \"%s\", but the Category is not" - + "a folder. This is no supported.", - item.getUuid(), - current.getUuid(), - CATEGORIZATION_TYPE_FOLDER)); + + "a folder. This is no supported.", + item.getUuid(), + current.getUuid(), + CATEGORIZATION_TYPE_FOLDER)); } } @@ -1284,15 +1367,15 @@ public class ContentItemManager { * @param item The item * * @return An {@link Optional} containing the folder of the item if the item - * is part of a folder. + * is part of a folder. */ public Optional getItemFolder(final ContentItem item) { final List result = item.getCategories().stream() - .filter(categorization -> { - return CATEGORIZATION_TYPE_FOLDER. - equals(categorization.getType()); - }) - .collect(Collectors.toList()); + .filter(categorization -> { + return CATEGORIZATION_TYPE_FOLDER. + equals(categorization.getType()); + }) + .collect(Collectors.toList()); if (result.size() > 0) { final Category category = result.get(0).getCategory(); @@ -1300,12 +1383,12 @@ public class ContentItemManager { return Optional.of((Folder) category); } else { throw new IllegalArgumentException(String.format( - "The item %s is assigned to the category %s with the" - + "categorization type \"%s\", but the Category is not" + "The item %s is assigned to the category %s with the" + + "categorization type \"%s\", but the Category is not" + "a folder. This is no supported.", - item.getUuid(), - category.getUuid(), - CATEGORIZATION_TYPE_FOLDER)); + item.getUuid(), + category.getUuid(), + CATEGORIZATION_TYPE_FOLDER)); } } else { return Optional.empty(); diff --git a/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/after-publish.xml b/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/after-publish.xml index ce360e363..25d3eeec0 100644 --- a/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/after-publish.xml +++ b/ccm-cms/src/test/resources/datasets/org/librecms/contentsection/ContentItemManagerTest/after-publish.xml @@ -152,8 +152,8 @@ display_name="org.librecms.contenttypes.Article" uuid="bd061ab6-9c4f-45ff-ab69-f521008eeac3" /> + display_name="nonSharedAsset" + uuid="00000000-0000-0000-0000-000000000000" /> - 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 95d621ae0..b2cc2500f 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 @@ -157,12 +157,12 @@ - - - @@ -424,7 +419,7 @@ object_id="-10200" localized_value="article2" locale="en" - revend="6" + revend="5" revtype="0" /> - + - - - - - - - - -