diff --git a/ccm-cms/src/main/java/org/librecms/assets/AssetManager.java b/ccm-cms/src/main/java/org/librecms/assets/AssetManager.java index 3efd70867..bee092f32 100644 --- a/ccm-cms/src/main/java/org/librecms/assets/AssetManager.java +++ b/ccm-cms/src/main/java/org/librecms/assets/AssetManager.java @@ -37,13 +37,15 @@ import org.libreccm.categorization.CategoryManager; import org.libreccm.core.CoreConstants; import org.libreccm.security.AuthorizationRequired; import org.libreccm.security.RequiresPrivilege; +import org.librecms.CmsConstants; import org.librecms.attachments.AttachmentList; import org.librecms.contentsection.ContentSection; import org.librecms.contentsection.Folder; import org.librecms.contentsection.FolderManager; import org.librecms.contentsection.FolderRepository; import org.librecms.contentsection.privileges.AssetPrivileges; -import org.librecms.contentsection.privileges.ItemPrivileges; + +import java.util.Objects; import static org.librecms.CmsConstants.*; @@ -75,83 +77,56 @@ public class AssetManager { private FolderManager folderManager; /** - * Creates a new, non shared {@link Asset} and adds it to the provided - * {@link AttachmentList}. - * - * @param Type variable for the type of the new {@link Asset}. - * @param name The name of the new {@link Asset}. - * @param attachments The {@link AttachmentList} to which the new - * {@link Asset} is added. - * @param type The type of the new {@link Asset}. Must be a subclass - * of the {@link Asset} class. - * - * @return The new {@link Asset}. - */ - @AuthorizationRequired - @Transactional(Transactional.TxType.REQUIRED) - public T createAsset( - final String name, - @RequiresPrivilege(ItemPrivileges.EDIT) - final AttachmentList attachments, - final Class type) { - throw new UnsupportedOperationException("Not implemented yet."); - } - - /** - * Creates a new shared {@link Asset} in the provided {@link Folder}. + * Makes an {@link Asset} a shared {@code Asset} by adding it to an asset + * folder. This action can't be undone. * * The folder must be a subfolder {@link ContentSection#rootAssetsFolder} of * a content section. Otherwise an {@link IllegalArgumentException} is * thrown. * - * @param Type variable for the type of the {@link Asset} to create. - * @param name The name of the new {@link Asset}. - * @param folder The {@link Folder} in which the {@link Asset} is created. - * @param type The type of the new {@link Asset}. Must be a subclass of - * the {@link Asset} class. * - * @return The new {@link Asset}. + * @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 T createAsset( - final String name, + public void shareAsset( + final Asset asset, @RequiresPrivilege(AssetPrivileges.CREATE_NEW) - final Folder folder, - final Class type) { - throw new UnsupportedOperationException("Not implemented yet."); + final Folder folder) { + + if (asset == null) { + throw new IllegalArgumentException("Can't share asset null."); + } + + if (folder == null) { + throw new IllegalArgumentException("No folder provided"); + } + + if (isShared(asset)) { + throw new IllegalArgumentException(String.format( + "The asset %s is already shared.", + Objects.toString(asset))); + } + + categoryManager.addObjectToCategory( + asset, + folder, + CmsConstants.CATEGORIZATION_TYPE_FOLDER); } /** - * Creates a new {@link Asset}. If a folder is provided a sharable - * {@link Asset} is created. Otherwise a non shared asset is created. This - * method implements the common logic for - * {@link #createAsset(java.lang.String, org.librecms.attachments.AttachmentList, java.lang.Class)} - * and - * {@link #createAsset(java.lang.String, org.librecms.contentsection.Folder, java.lang.Class)}. - * Users of this class usually should use these methods. This method has - * been made public for special cases. Please note that this class does - * not - * perform any authorisation checks. This is up to the caller. Also if no - * folder is provided and the caller does not add the created asset to an - * {@link AttachmentList} the asset will become orphaned can't be accessed. + * Checks of an {@link Asset} is shared (associated with an {@link Folder}. + * The folder in which the asset is stored can be retrieved using + * {@link #getAssetFolder(org.librecms.assets.Asset)}. * - * @param Type variable for the type of the {@link Asset}. - * @param name The name of the new {@link Asset}. - * @param folder Optional folder in which the new {@link Asset} is placed. - * @param type The type of the new {@link Asset}. Must be a subclass of - * the {@link Asset} class. + * @param asset The asset the check. * - * @return The new {@link Asset}. Note: If no {@link Folder} is provided and - * the and the returned {@link Asset} is not added to an - * {@link AttachmentList} the new {@code Asset} will become orphaned - * and can't be accessed by any method. + * @return {@code true} is the {@link Asset} is shared, + * {@code false if not}. */ - @Transactional(Transactional.TxType.REQUIRED) - public T createAsset(final String name, - final Optional folder, - final Class type) { - throw new UnsupportedOperationException("Not implemented yet."); + public boolean isShared(final Asset asset) { + return getAssetFolder(asset).isPresent(); } /** @@ -271,7 +246,7 @@ public class AssetManager { if (withContentSection) { final String sectionName - = ((Folder) result.get(0).getCategory()). + = ((Folder) result.get(0).getCategory()). getSection().getDisplayName(); return String.format("%s:/%s", sectionName, path); } else { diff --git a/ccm-cms/src/main/java/org/librecms/assets/BinaryAsset.java b/ccm-cms/src/main/java/org/librecms/assets/BinaryAsset.java index 8a60767c5..4af597560 100644 --- a/ccm-cms/src/main/java/org/librecms/assets/BinaryAsset.java +++ b/ccm-cms/src/main/java/org/librecms/assets/BinaryAsset.java @@ -38,6 +38,7 @@ import org.libreccm.jpa.utils.MimeTypeConverter; import org.libreccm.l10n.LocalizedString; import javax.persistence.Convert; +import javax.validation.constraints.NotNull; import static org.librecms.CmsConstants.*; @@ -71,7 +72,7 @@ public class BinaryAsset extends Asset implements Serializable { @Column(name = "MIME_TYPE", length = 512, nullable = false) @Convert(converter = MimeTypeConverter.class) - @NotEmpty + @NotNull private MimeType mimeType; @Column(name = "ASSET_DATA") diff --git a/ccm-cms/src/test/java/org/librecms/assets/AssetManagerTest.java b/ccm-cms/src/test/java/org/librecms/assets/AssetManagerTest.java index 5af4ad082..837ac0b07 100644 --- a/ccm-cms/src/test/java/org/librecms/assets/AssetManagerTest.java +++ b/ccm-cms/src/test/java/org/librecms/assets/AssetManagerTest.java @@ -45,11 +45,19 @@ import org.junit.runner.RunWith; import org.libreccm.security.Shiro; import org.libreccm.tests.categories.IntegrationTest; import org.librecms.attachments.AttachmentList; +import org.librecms.contentsection.ContentItem; +import org.librecms.contentsection.ContentItemRepository; import org.librecms.contentsection.FolderRepository; import javax.inject.Inject; + import org.librecms.contentsection.Folder; +import java.util.Locale; + +import javax.activation.MimeType; +import javax.activation.MimeTypeParseException; + import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; @@ -65,6 +73,9 @@ import static org.junit.Assert.*; @CreateSchema({"create_ccm_cms_schema.sql"}) public class AssetManagerTest { + @Inject + private ContentItemRepository itemRepo; + @Inject private AssetRepository assetRepo; @@ -179,27 +190,45 @@ public class AssetManagerTest { } /** - * Tries to generate various non shared {@link Asset}s of different types - * using - * {@link AssetManager#createAsset(java.lang.String, org.librecms.attachments.AttachmentList, java.lang.Class)}. - * and puts them into an {@link AttachmentList}. + * Tries to share an {@link Asset} using + * {@link AssetManager#shareAsset(org.librecms.assets.Asset, org.librecms.contentsection.Folder)}. + * + * @throws javax.activation.MimeTypeParseException */ @Test @InSequence(100) @UsingDataSet("datasets/org/librecms/assets/AssetManagerTest/data.xml") @ShouldMatchDataSet( - value = "datasets/org/librecms/assets/AssetManagerTest/" - + "after-create-nonshared.xml", - excludeColumns = {"object_id", + value = "datasets/org/librecms/assets/AssetManagerTest/after-share.xml", + excludeColumns = {"asset_id", + "categorization_id", + "id", + "object_id", + "object_order", + "rev", + "timestamp", "uuid"}) - public void createNonSharedAssets() { - fail(); + public void shareAsset() throws MimeTypeParseException { + final Folder folder = folderRepo.findById(-420L); + assertThat(folder, is(not(nullValue()))); + + final File file = new File(); + file.setDisplayName("datasheet.pdf"); + file.setFileName("datasheet.pdf"); + file.setMimeType(new MimeType("application/pdf")); + file.getTitle().addValue(Locale.ENGLISH, "datasheet.pdf"); + assetRepo.save(file); + + assetManager.shareAsset(file, folder); + + assertThat(file, is(not(nullValue()))); + assertThat(file.getDisplayName(), is(equalTo("datasheet.pdf"))); } /** * Verifies that - * {@link AssetManager#createAsset(java.lang.String, org.librecms.attachments.AttachmentList, java.lang.Class)} - * throws an {@link IllegalArgumentException} if the provided {@code name} + * {@link AssetManager#shareAsset(org.librecms.assets.Asset, org.librecms.contentsection.Folder)} + * throws an {@link IllegalArgumentException} if the provided {@code asset} * is {@code null}. */ @Test(expected = IllegalArgumentException.class) @@ -207,129 +236,54 @@ public class AssetManagerTest { @UsingDataSet("datasets/org/librecms/assets/AssetManagerTest/data.xml") @ShouldMatchDataSet("datasets/org/librecms/assets/AssetManagerTest/data.xml") @ShouldThrowException(IllegalArgumentException.class) - public void createNonSharedAssetNameIsNull() { + public void shareAssetNull() { + final Folder folder = folderRepo.findById(-420L); + assertThat(folder, is(not(nullValue()))); + assetManager.shareAsset(null, folder); } /** * Verifies that - * {@link AssetManager#createAsset(java.lang.String, org.librecms.attachments.AttachmentList, java.lang.Class)} - * throws an {@link IllegalArgumentException} if the provided {@code name} - * is empty. + * {@link AssetManager#shareAsset(org.librecms.assets.Asset, org.librecms.contentsection.Folder)} + * throws an {@link IllegalArgumentException} if the provided {@code folder} + * is null. + * + * @throws javax.activation.MimeTypeParseException */ @Test(expected = IllegalArgumentException.class) @InSequence(120) @UsingDataSet("datasets/org/librecms/assets/AssetManagerTest/data.xml") @ShouldMatchDataSet("datasets/org/librecms/assets/AssetManagerTest/data.xml") @ShouldThrowException(IllegalArgumentException.class) - public void createNonSharedAssetNameIsEmpty() { - fail(); + public void shareAssetFolderIsNull() throws MimeTypeParseException { + final File file = new File(); + file.setDisplayName("datasheet.pdf"); + file.setFileName("datasheet.pdf"); + file.setMimeType(new MimeType("application/pdf")); + + assetManager.shareAsset(file, null); } /** * Verifies that - * {@link AssetManager#createAsset(java.lang.String, org.librecms.attachments.AttachmentList, java.lang.Class)} - * throws an {@link IllegalArgumentException} if the provided - * {@code attachmentList} is empty. + * {@link AssetManager#shareAsset(org.librecms.assets.Asset, org.librecms.contentsection.Folder)} + * throws an {@link IllegalArgumentException} if the provided {@code asset} + * is already shared. */ @Test(expected = IllegalArgumentException.class) @InSequence(130) @UsingDataSet("datasets/org/librecms/assets/AssetManagerTest/data.xml") @ShouldMatchDataSet("datasets/org/librecms/assets/AssetManagerTest/data.xml") @ShouldThrowException(IllegalArgumentException.class) - public void createNonSharedAssetAttachmentListNull() { - fail(); - } + public void shareAlreadySharedAsset() { + final Folder folder = folderRepo.findById(-420L); + assertThat(folder, is(not(nullValue()))); - /** - * Verifies that - * {@link AssetManager#createAsset(java.lang.String, org.librecms.attachments.AttachmentList, java.lang.Class)} - * throws an {@link IllegalArgumentException} if the provided {@code type} - * is empty. - */ - @Test(expected = IllegalArgumentException.class) - @InSequence(140) - @UsingDataSet("datasets/org/librecms/assets/AssetManagerTest/data.xml") - @ShouldMatchDataSet("datasets/org/librecms/assets/AssetManagerTest/data.xml") - @ShouldThrowException(IllegalArgumentException.class) - public void createNonSharedAssetTypeIsNull() { - fail(); - } + final Asset asset = assetRepo.findById(-700L); + assertThat(asset, is(not(nullValue()))); - /** - * Tries to generate various shared {@link Asset}s of different types using - * {@link AssetManager#createAsset(java.lang.String, org.librecms.contentsection.Folder, java.lang.Class)}. - */ - @Test - @InSequence(200) - @UsingDataSet("datasets/org/librecms/assets/AssetManagerTest/data.xml") - @ShouldMatchDataSet( - value = "datasets/org/librecms/assets/AssetManagerTest/" - + "after-create-shared.xml", - excludeColumns = {"object_id", - "uuid"}) - public void createSharedAssets() { - fail(); - } - - /** - * Verifies that - * {@link AssetManager#createAsset(java.lang.String, org.librecms.contentsection.Folder, java.lang.Class)} - * throws an {@link IllegalArgumentException} if the provided {@code name} - * is {@code null}. - */ - @Test(expected = IllegalArgumentException.class) - @InSequence(210) - @UsingDataSet("datasets/org/librecms/assets/AssetManagerTest/data.xml") - @ShouldMatchDataSet("datasets/org/librecms/assets/AssetManagerTest/data.xml") - @ShouldThrowException(IllegalArgumentException.class) - public void createSharedAssetNameIsNull() { - fail(); - } - - /** - * Verifies that - * {@link AssetManager#createAsset(java.lang.String, org.librecms.contentsection.Folder, java.lang.Class)} - * throws an {@link IllegalArgumentException} if the provided {@code name} - * is empty. - */ - @Test(expected = IllegalArgumentException.class) - @InSequence(220) - @UsingDataSet("datasets/org/librecms/assets/AssetManagerTest/data.xml") - @ShouldMatchDataSet("datasets/org/librecms/assets/AssetManagerTest/data.xml") - @ShouldThrowException(IllegalArgumentException.class) - public void createSharedAssetNameIsEmpty() { - fail(); - } - - /** - * Verifies that - * {@link AssetManager#createAsset(java.lang.String, org.librecms.contentsection.Folder, java.lang.Class)} - * throws an {@link IllegalArgumentException} if the provided {@code folder} - * is {@code null}. - */ - @Test(expected = IllegalArgumentException.class) - @InSequence(230) - @UsingDataSet("datasets/org/librecms/assets/AssetManagerTest/data.xml") - @ShouldMatchDataSet("datasets/org/librecms/assets/AssetManagerTest/data.xml") - @ShouldThrowException(IllegalArgumentException.class) - public void createSharedAssetFolderIsNull() { - fail(); - } - - /** - * Verifies that - * {@link AssetManager#createAsset(java.lang.String, org.librecms.contentsection.Folder, java.lang.Class)} - * throws an {@link IllegalArgumentException} if the provided {@code type} - * is {@code null}. - */ - @Test(expected = IllegalArgumentException.class) - @InSequence(240) - @UsingDataSet("datasets/org/librecms/assets/AssetManagerTest/data.xml") - @ShouldMatchDataSet("datasets/org/librecms/assets/AssetManagerTest/data.xml") - @ShouldThrowException(IllegalArgumentException.class) - public void createSharedAssetTypeIsNull() { - fail(); + assetManager.shareAsset(asset, folder); } /** @@ -342,8 +296,7 @@ public class AssetManagerTest { @ShouldMatchDataSet( value = "datasets/org/librecms/assets/AssetManagerTest/" + "after-clean-orphaned.xml", - excludeColumns = {"timestamp"}, - orderBy = {"asset_titles_aud.asset_id"}) + excludeColumns = {"timestamp", "object_order"}) public void cleanOrphanedAssets() { assetManager.cleanOrphanedAssets(); } diff --git a/ccm-cms/src/test/java/org/librecms/assets/DatasetsTest.java b/ccm-cms/src/test/java/org/librecms/assets/DatasetsTest.java index 9e93c2a28..2321d49b5 100644 --- a/ccm-cms/src/test/java/org/librecms/assets/DatasetsTest.java +++ b/ccm-cms/src/test/java/org/librecms/assets/DatasetsTest.java @@ -52,8 +52,7 @@ public class DatasetsTest extends DatasetsVerifier { "/datasets/org/librecms/assets/AssetManagerTest/after-copy-to-other-contentsection.xml", "/datasets/org/librecms/assets/AssetManagerTest/after-copy-to-other-folder.xml", "/datasets/org/librecms/assets/AssetManagerTest/after-copy-to-same-folder.xml", - "/datasets/org/librecms/assets/AssetManagerTest/after-create-nonshared.xml", - "/datasets/org/librecms/assets/AssetManagerTest/after-create-shared.xml", + "/datasets/org/librecms/assets/AssetManagerTest/after-share.xml", "/datasets/org/librecms/assets/AssetManagerTest/after-move-to-other-contentsection.xml", "/datasets/org/librecms/assets/AssetManagerTest/after-move-to-other-folder.xml", }); diff --git a/ccm-cms/src/test/resources/datasets/org/librecms/assets/AssetManagerTest/after-clean-orphaned.xml b/ccm-cms/src/test/resources/datasets/org/librecms/assets/AssetManagerTest/after-clean-orphaned.xml index 269f3babd..2882d0dda 100644 --- a/ccm-cms/src/test/resources/datasets/org/librecms/assets/AssetManagerTest/after-clean-orphaned.xml +++ b/ccm-cms/src/test/resources/datasets/org/librecms/assets/AssetManagerTest/after-clean-orphaned.xml @@ -415,40 +415,40 @@ - - - - - - - + + + + --> + + + @@ -65,6 +73,9 @@ + + + + + + @@ -196,6 +237,10 @@ type="ASSETS_FOLDER" /> + + + + + @@ -261,6 +311,8 @@ rev="0" /> + + + + + + + + - - + + - diff --git a/ccm-cms/src/test/resources/datasets/org/librecms/assets/AssetRepositoryTest/after-delete.xml b/ccm-cms/src/test/resources/datasets/org/librecms/assets/AssetRepositoryTest/after-delete.xml index e4b381345..ef4f4ecf0 100644 --- a/ccm-cms/src/test/resources/datasets/org/librecms/assets/AssetRepositoryTest/after-delete.xml +++ b/ccm-cms/src/test/resources/datasets/org/librecms/assets/AssetRepositoryTest/after-delete.xml @@ -286,7 +286,6 @@ localized_value="the-phb.png" locale="en" /> -