CCM NG/ccm-cms: AttachmentListManager finished

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4427 8810af33-2d31-482b-a856-94f89814c4df
pull/2/head
jensp 2016-11-02 08:17:44 +00:00
parent 0e8fe19efd
commit 4b9f65d40b
5 changed files with 411 additions and 142 deletions

View File

@ -302,7 +302,7 @@ public class AttachmentListManager {
*
* @param attachmentList The list to move.
*/
@Transactional
@Transactional(Transactional.TxType.REQUIRED)
@AuthorizationRequired
public void moveUp(
@RequiresPrivilege(ItemPrivileges.EDIT)

View File

@ -19,11 +19,15 @@
package org.librecms.contentsection;
import java.util.List;
import org.libreccm.security.AuthorizationRequired;
import org.libreccm.security.RequiresPrivilege;
import org.librecms.contentsection.privileges.ItemPrivileges;
import java.util.Collections;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
@ -117,6 +121,15 @@ public class ItemAttachmentManager {
*/
public void unattachAsset(final Asset asset,
final AttachmentList attachmentList) {
if (asset == null) {
throw new IllegalArgumentException("Can't unattach null.");
}
if (attachmentList == null) {
throw new IllegalArgumentException(
"Can't unattach an asset from list null.");
}
final TypedQuery<Long> countQuery = entityManager.createNamedQuery(
"ItemAttachment.countByAssetIdAndList", Long.class);
countQuery.setParameter("asset", asset);
@ -128,17 +141,18 @@ public class ItemAttachmentManager {
return;
}
@SuppressWarnings("rawtypes")
final TypedQuery<ItemAttachment> query = entityManager
.createNamedQuery("ItemAttachment.findByAssetByAndList",
ItemAttachment.class);
query.setParameter("asset", asset);
query.setParameter("attachmentList", attachmentList);
@SuppressWarnings("rawtypes")
final List<ItemAttachment> attachments = query.getResultList();
attachments.forEach((attachment) -> entityManager.remove(attachment));
if (!assetManager.isShared(asset)) {
// assetRepo.delete(asset);
entityManager.remove(asset);
}
}
@ -147,28 +161,147 @@ public class ItemAttachmentManager {
* Moves the {@link Asset} one position up in the provided
* {@link AttachmentList}.
*
* @param asset The asset to move up. If the asset is not part of the
* provided {@link AttachmentList} an {@link IllegalArgumentException} is
* thrown.
* @param asset The asset to move up. If the asset is not part of
* the provided {@link AttachmentList} an
* {@link IllegalArgumentException} is thrown.
* @param attachmentList The attachment list in which the item is moved.
*/
public void moveUp(final Asset asset,
@Transactional(Transactional.TxType.REQUIRED)
@AuthorizationRequired
public void moveUp(
final Asset asset,
@RequiresPrivilege(ItemPrivileges.EDIT)
final AttachmentList attachmentList) {
throw new UnsupportedOperationException("Not implemented yet");
if (asset == null) {
throw new IllegalArgumentException("Can't move null.");
}
if (attachmentList == null) {
throw new IllegalArgumentException(
"Can't move up an asset in list null.");
}
final TypedQuery<Long> countQuery = entityManager.createNamedQuery(
"ItemAttachment.countByAssetIdAndList", Long.class);
countQuery.setParameter("asset", asset);
countQuery.setParameter("attachmentList", attachmentList);
final long count = countQuery.getSingleResult();
if (count == 0) {
return;
}
@SuppressWarnings("rawtypes")
final TypedQuery<ItemAttachment> query = entityManager.createNamedQuery(
"ItemAttachment.findByAssetByAndList", ItemAttachment.class);
query.setParameter("asset", asset);
query.setParameter("attachmentList", attachmentList);
final ItemAttachment<?> selected = query.getSingleResult();
final Optional<ItemAttachment<?>> attachment1 = attachmentList
.getAttachments().stream()
.filter(attachment -> {
return attachment.getSortKey() == selected.getSortKey();
})
.findFirst();
final Optional<ItemAttachment<?>> attachment2 = attachmentList
.getAttachments().stream()
.filter(attachment -> {
return attachment.getSortKey() >= selected.getSortKey() + 1;
})
.findFirst();
if (!attachment2.isPresent()) {
return;
}
final long sortKey1 = attachment1.get().getSortKey();
final long sortKey2 = attachment2.get().getSortKey();
attachment1.get().setSortKey(sortKey2);
attachment2.get().setSortKey(sortKey1);
entityManager.merge(attachment1.get());
entityManager.merge(attachment2.get());
}
/**
* Moves the {@link Asset} one position down in the provided
* {@link AttachmentList}.
*
* @param asset The asset to move down. If the asset is not part of the
* provided {@link AttachmentList} an {@link IllegalArgumentException} is
* thrown.
* @param asset The asset to move down. If the asset is not part of
* the provided {@link AttachmentList} an
* {@link IllegalArgumentException} is thrown.
* @param attachmentList The attachment list in which the item is moved.
*/
public void moveDown(final Asset asset,
@Transactional(Transactional.TxType.REQUIRED)
@AuthorizationRequired
public void moveDown(
final Asset asset,
@RequiresPrivilege(ItemPrivileges.EDIT)
final AttachmentList attachmentList) {
throw new UnsupportedOperationException("Not implemented yet");
if (asset == null) {
throw new IllegalArgumentException("Can't move down null.");
}
if (attachmentList == null) {
throw new IllegalArgumentException(
"Can't move down an asset in list null.");
}
final TypedQuery<Long> countQuery = entityManager.createNamedQuery(
"ItemAttachment.countByAssetIdAndList", Long.class);
countQuery.setParameter("asset", asset);
countQuery.setParameter("attachmentList", attachmentList);
final long count = countQuery.getSingleResult();
if (count == 0) {
return;
}
@SuppressWarnings("rawtypes")
final TypedQuery<ItemAttachment> query = entityManager.createNamedQuery(
"ItemAttachment.findByAssetByAndList", ItemAttachment.class);
query.setParameter("asset", asset);
query.setParameter("attachmentList", attachmentList);
final ItemAttachment<?> selected = query.getSingleResult();
final Optional<ItemAttachment<?>> attachment1 = attachmentList
.getAttachments().stream()
.filter(attachment -> {
return attachment.getSortKey() == selected.getSortKey();
})
.findFirst();
final List<ItemAttachment<?>> lower = attachmentList
.getAttachments().stream()
.filter(attachment -> {
return attachment.getSortKey() <= selected.getSortKey() - 1;
})
.collect(Collectors.toList());
Collections.sort(lower);
final Optional<ItemAttachment<?>> attachment2;
if (lower.isEmpty()) {
attachment2 = Optional.empty();
} else {
attachment2 = Optional.of(lower.get(lower.size() - 1));
}
if (!attachment2.isPresent()) {
return;
}
final long sortKey1 = attachment1.get().getSortKey();
final long sortKey2 = attachment2.get().getSortKey();
attachment1.get().setSortKey(sortKey2);
attachment2.get().setSortKey(sortKey1);
entityManager.merge(attachment1.get());
entityManager.merge(attachment2.get());
}
}

View File

@ -129,14 +129,14 @@ public class ItemAttachmentManagerTest {
.addClass(com.arsdigita.kernel.KernelConfig.class)
.addClass(com.arsdigita.runtime.CCMResourceManager.class)
.addClass(
com.arsdigita.ui.admin.applications.AbstractAppInstanceForm.class).
addClass(
com.arsdigita.ui.admin.applications.AbstractAppSettingsPane.class).
addClass(
com.arsdigita.ui.admin.applications.DefaultApplicationInstanceForm.class).
addClass(
com.arsdigita.ui.admin.applications.DefaultApplicationSettingsPane.class).
addClass(com.arsdigita.cms.dispatcher.ItemResolver.class)
com.arsdigita.ui.admin.applications.AbstractAppInstanceForm.class)
.addClass(
com.arsdigita.ui.admin.applications.AbstractAppSettingsPane.class)
.addClass(
com.arsdigita.ui.admin.applications.DefaultApplicationInstanceForm.class)
.addClass(
com.arsdigita.ui.admin.applications.DefaultApplicationSettingsPane.class)
.addClass(com.arsdigita.cms.dispatcher.ItemResolver.class)
.addPackage(com.arsdigita.util.Lockable.class.getPackage())
.addPackage(com.arsdigita.web.BaseServlet.class.getPackage())
.addPackage(org.librecms.Cms.class.getPackage())
@ -369,7 +369,15 @@ public class ItemAttachmentManagerTest {
@ShouldMatchDataSet("datasets/org/librecms/contentsection/"
+ "ItemAttachmentManagerTest/data.xml")
public void unattachAssetNotAttached() {
fail("Not implemented yet");
final Asset asset = assetRepo.findById(-720L);
final Optional<ContentItem> item = itemRepo.findById(-510L);
assertThat(asset, is(not(nullValue())));
assertThat(item.isPresent(), is(true));
final AttachmentList list = item.get().getAttachments().get(1);
attachmentManager.unattachAsset(asset, list);
}
/**
@ -386,7 +394,14 @@ public class ItemAttachmentManagerTest {
+ "ItemAttachmentManagerTest/data.xml")
@ShouldThrowException(IllegalArgumentException.class)
public void unattachAssetNull() {
fail("Not implemented yet");
final Asset asset = null;
final Optional<ContentItem> item = itemRepo.findById(-510L);
assertThat(item.isPresent(), is(true));
final AttachmentList list = item.get().getAttachments().get(0);
attachmentManager.unattachAsset(asset, list);
}
/**
@ -403,7 +418,12 @@ public class ItemAttachmentManagerTest {
+ "ItemAttachmentManagerTest/data.xml")
@ShouldThrowException(IllegalArgumentException.class)
public void unattachAssetFromListNull() {
fail("Not implemented yet");
final Asset asset = assetRepo.findById(-720L);
assertThat(asset, is(not(nullValue())));
final AttachmentList list = null;
attachmentManager.unattachAsset(asset, list);
}
/**
@ -415,10 +435,18 @@ public class ItemAttachmentManagerTest {
@InSequence(300)
@UsingDataSet("datasets/org/librecms/contentsection/"
+ "ItemAttachmentManagerTest/data.xml")
@ShouldMatchDataSet("datasets/org/librecms/contentsection/"
+ "ItemAttachmentManagerTest/after-move-up.xml")
@ShouldMatchDataSet(
value = "datasets/org/librecms/contentsection/"
+ "ItemAttachmentManagerTest/after-move-up.xml",
excludeColumns = {"timestamp"})
public void moveUp() {
fail("Not implemented yet");
final Optional<ContentItem> item = itemRepo.findById(-510L);
assertThat(item.isPresent(), is(true));
final AttachmentList list = item.get().getAttachments().get(0);
attachmentManager.moveUp(list.getAttachments().get(0).getAsset(),
list);
}
/**
@ -433,7 +461,13 @@ public class ItemAttachmentManagerTest {
@ShouldMatchDataSet("datasets/org/librecms/contentsection/"
+ "ItemAttachmentManagerTest/data.xml")
public void moveUpLast() {
fail("Not implemented yet");
final Optional<ContentItem> item = itemRepo.findById(-510L);
assertThat(item.isPresent(), is(true));
final AttachmentList list = item.get().getAttachments().get(0);
attachmentManager.moveUp(list.getAttachments().get(2).getAsset(),
list);
}
/**
@ -450,7 +484,34 @@ public class ItemAttachmentManagerTest {
+ "ItemAttachmentManagerTest/data.xml")
@ShouldThrowException(IllegalArgumentException.class)
public void moveUpNull() {
fail("Not implemented yet");
final Optional<ContentItem> item = itemRepo.findById(-510L);
assertThat(item.isPresent(), is(true));
final AttachmentList list = item.get().getAttachments().get(0);
attachmentManager.moveUp(null, list);
}
/**
* Verifies that
* {@link ItemAttachmentManager#moveUp(org.librecms.contentsection.Asset, org.librecms.contentsection.AttachmentList)}
* throws an {@link IllegalArgumentException} if called with {@code null}
* for the list.
*/
@Test(expected = IllegalArgumentException.class)
@InSequence(330)
@UsingDataSet("datasets/org/librecms/contentsection/"
+ "ItemAttachmentManagerTest/data.xml")
@ShouldMatchDataSet("datasets/org/librecms/contentsection/"
+ "ItemAttachmentManagerTest/data.xml")
@ShouldThrowException(IllegalArgumentException.class)
public void moveUpInListNull() {
final Asset asset = assetRepo.findById(-720L);
assertThat(asset, is(not(nullValue())));
final AttachmentList list = null;
attachmentManager.moveUp(asset, list);
}
/**
@ -462,10 +523,18 @@ public class ItemAttachmentManagerTest {
@InSequence(400)
@UsingDataSet("datasets/org/librecms/contentsection/"
+ "ItemAttachmentManagerTest/data.xml")
@ShouldMatchDataSet("datasets/org/librecms/contentsection/"
+ "ItemAttachmentManagerTest/after-move-down.xml")
@ShouldMatchDataSet(
value = "datasets/org/librecms/contentsection/"
+ "ItemAttachmentManagerTest/after-move-down.xml",
excludeColumns = {"timestamp"})
public void moveDown() {
fail("Not implemented yet");
final Optional<ContentItem> item = itemRepo.findById(-510L);
assertThat(item.isPresent(), is(true));
final AttachmentList list = item.get().getAttachments().get(0);
attachmentManager.moveDown(list.getAttachments().get(2).getAsset(),
list);
}
/**
@ -480,7 +549,13 @@ public class ItemAttachmentManagerTest {
@ShouldMatchDataSet("datasets/org/librecms/contentsection/"
+ "ItemAttachmentManagerTest/data.xml")
public void moveDownFirst() {
fail("Not implemented yet");
final Optional<ContentItem> item = itemRepo.findById(-510L);
assertThat(item.isPresent(), is(true));
final AttachmentList list = item.get().getAttachments().get(0);
attachmentManager.moveDown(list.getAttachments().get(0).getAsset(),
list);
}
/**
@ -497,7 +572,34 @@ public class ItemAttachmentManagerTest {
+ "ItemAttachmentManagerTest/data.xml")
@ShouldThrowException(IllegalArgumentException.class)
public void moveDownNull() {
fail("Not implemented yet");
final Optional<ContentItem> item = itemRepo.findById(-510L);
assertThat(item.isPresent(), is(true));
final AttachmentList list = item.get().getAttachments().get(0);
attachmentManager.moveDown(null, list);
}
/**
* Verifies that
* {@link ItemAttachmentManager#moveDown(org.librecms.contentsection.Asset, org.librecms.contentsection.AttachmentList)}
* throws an {@link IllegalArgumentException} if called with {@code null}
* for the list.
*/
@Test(expected = IllegalArgumentException.class)
@InSequence(430)
@UsingDataSet("datasets/org/librecms/contentsection/"
+ "ItemAttachmentManagerTest/data.xml")
@ShouldMatchDataSet("datasets/org/librecms/contentsection/"
+ "ItemAttachmentManagerTest/data.xml")
@ShouldThrowException(IllegalArgumentException.class)
public void moveDownInListNull() {
final Asset asset = assetRepo.findById(-720L);
assertThat(asset, is(not(nullValue())));
final AttachmentList list = null;
attachmentManager.moveDown(asset, list);
}
}

View File

@ -3,6 +3,8 @@
<ccm_core.ccm_revisions id="0"
timestamp="1451602800" />
<ccm_core.ccm_revisions id="1"
timestamp="1451602800" />
<ccm_core.ccm_objects object_id="-100"
display_name="info"
@ -320,12 +322,12 @@
asset_id="-710"
attachment_list_id="-510010" />
<ccm_cms.attachments attachment_id="-510120"
sort_key="2"
sort_key="3"
uuid="5a34deae-9e3a-41e8-abd8-6a7d10dd9e7d"
asset_id="-720"
attachment_list_id="-510010" />
<ccm_cms.attachments attachment_id="-510130"
sort_key="3"
sort_key="2"
uuid="9e34627c-2da9-45fe-aae3-48801bd27cbe"
asset_id="-610"
attachment_list_id="-510010" />
@ -345,6 +347,7 @@
<ccm_cms.attachments_aud attachment_id="-510120"
rev="0"
revtype="0"
revend="1"
sort_key="2"
uuid="5a34deae-9e3a-41e8-abd8-6a7d10dd9e7d"
asset_id="-720"
@ -352,6 +355,7 @@
<ccm_cms.attachments_aud attachment_id="-510130"
rev="0"
revtype="0"
revend="1"
sort_key="3"
uuid="9e34627c-2da9-45fe-aae3-48801bd27cbe"
asset_id="-610"
@ -370,7 +374,20 @@
uuid="395c43dc-4aea-43e3-a2de-13e10f3a63f7"
asset_id="-630"
attachment_list_id="-510030" />
<ccm_cms.attachments_aud attachment_id="-510120"
rev="1"
revtype="1"
sort_key="3"
uuid="5a34deae-9e3a-41e8-abd8-6a7d10dd9e7d"
asset_id="-720"
attachment_list_id="-510010" />
<ccm_cms.attachments_aud attachment_id="-510130"
rev="1"
revtype="1"
sort_key="2"
uuid="9e34627c-2da9-45fe-aae3-48801bd27cbe"
asset_id="-610"
attachment_list_id="-510010" />
</dataset>

View File

@ -3,6 +3,8 @@
<ccm_core.ccm_revisions id="0"
timestamp="1451602800" />
<ccm_core.ccm_revisions id="1"
timestamp="1451602800" />
<ccm_core.ccm_objects object_id="-100"
display_name="info"
@ -315,12 +317,12 @@
item_id="-510" />
<ccm_cms.attachments attachment_id="-510110"
sort_key="1"
sort_key="2"
uuid="de1d8531-df11-4808-9679-9ffa7537ebd1"
asset_id="-710"
attachment_list_id="-510010" />
<ccm_cms.attachments attachment_id="-510120"
sort_key="2"
sort_key="1"
uuid="5a34deae-9e3a-41e8-abd8-6a7d10dd9e7d"
asset_id="-720"
attachment_list_id="-510010" />
@ -338,6 +340,7 @@
<ccm_cms.attachments_aud attachment_id="-510110"
rev="0"
revtype="0"
revend="1"
sort_key="1"
uuid="de1d8531-df11-4808-9679-9ffa7537ebd1"
asset_id="-710"
@ -345,6 +348,7 @@
<ccm_cms.attachments_aud attachment_id="-510120"
rev="0"
revtype="0"
revend="1"
sort_key="2"
uuid="5a34deae-9e3a-41e8-abd8-6a7d10dd9e7d"
asset_id="-720"
@ -370,7 +374,20 @@
uuid="395c43dc-4aea-43e3-a2de-13e10f3a63f7"
asset_id="-630"
attachment_list_id="-510030" />
<ccm_cms.attachments_aud attachment_id="-510110"
rev="1"
revtype="1"
sort_key="2"
uuid="de1d8531-df11-4808-9679-9ffa7537ebd1"
asset_id="-710"
attachment_list_id="-510010" />
<ccm_cms.attachments_aud attachment_id="-510120"
rev="1"
revtype="1"
sort_key="1"
uuid="5a34deae-9e3a-41e8-abd8-6a7d10dd9e7d"
asset_id="-720"
attachment_list_id="-510010" />
</dataset>