mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-17 15:03:18 +00:00
Merge branch 'DS-3910' of https://github.com/ppmdo/DSpace into DS-3910
This commit is contained in:
@@ -14,11 +14,11 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.dspace.authorize.AuthorizeException;
|
||||
import org.dspace.app.rest.exception.UnprocessableEntityException;
|
||||
import org.dspace.app.rest.converter.ItemConverter;
|
||||
import org.dspace.app.rest.exception.UnprocessableEntityException;
|
||||
import org.dspace.app.rest.model.ItemRest;
|
||||
import org.dspace.app.rest.model.hateoas.ItemResource;
|
||||
import org.dspace.authorize.AuthorizeException;
|
||||
import org.dspace.content.Item;
|
||||
import org.dspace.content.service.ItemService;
|
||||
import org.dspace.core.Context;
|
||||
@@ -43,7 +43,6 @@ public class ItemRestRepository extends DSpaceRestRepository<ItemRest, UUID> {
|
||||
@Autowired
|
||||
ItemConverter converter;
|
||||
|
||||
|
||||
public ItemRestRepository() {
|
||||
System.out.println("Repository initialized by Spring");
|
||||
}
|
||||
@@ -97,17 +96,18 @@ public class ItemRestRepository extends DSpaceRestRepository<ItemRest, UUID> {
|
||||
try {
|
||||
item = is.find(context, id);
|
||||
if (is.isInProgressSubmission(context, item)) {
|
||||
throw new UnprocessableEntityException("The item cannot be deleted. It's part of a in-progress submission.");
|
||||
throw new UnprocessableEntityException("The item cannot be deleted. "
|
||||
+ "It's part of a in-progress submission.");
|
||||
}
|
||||
if (item.getTemplateItemOf() != null) {
|
||||
throw new UnprocessableEntityException("The item cannot be deleted. It's a template for a collection");
|
||||
throw new UnprocessableEntityException("The item cannot be deleted. "
|
||||
+ "It's a template for a collection");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
try {
|
||||
is.delete(context, item);
|
||||
is.removeAllBundles(context, item);
|
||||
} catch (SQLException | IOException e) {
|
||||
throw new RuntimeException(e.getMessage(), e);
|
||||
}
|
||||
|
@@ -23,6 +23,7 @@ import org.dspace.app.rest.builder.BitstreamBuilder;
|
||||
import org.dspace.app.rest.builder.CollectionBuilder;
|
||||
import org.dspace.app.rest.builder.CommunityBuilder;
|
||||
import org.dspace.app.rest.builder.ItemBuilder;
|
||||
import org.dspace.app.rest.builder.WorkspaceItemBuilder;
|
||||
import org.dspace.app.rest.matcher.ItemMatcher;
|
||||
import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
|
||||
import org.dspace.content.Bitstream;
|
||||
@@ -73,7 +74,6 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
|
||||
.withSubject("ExtraEntry")
|
||||
.build();
|
||||
|
||||
|
||||
getClient().perform(get("/api/core/items"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$._embedded.items", Matchers.containsInAnyOrder(
|
||||
@@ -197,7 +197,6 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
|
||||
.withSubject("ExtraEntry")
|
||||
.build();
|
||||
|
||||
|
||||
getClient().perform(get("/api/core/items/" + publicItem1.getID()))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$", Matchers.is(
|
||||
@@ -292,7 +291,6 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void findOneTestWrongUUID() throws Exception {
|
||||
context.turnOffAuthorisationSystem();
|
||||
@@ -341,34 +339,33 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
|
||||
.withMimeType("text/plain")
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
// Check publicItem creation
|
||||
getClient().perform(get("/api/core/items/" + publicItem.getID()))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
|
||||
// Check publicItem bitstream creatino
|
||||
getClient().perform(get("/api/core/items/" + publicItem.getID() + "/bitstreams"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(contentType))
|
||||
.andExpect(jsonPath("$._links.self.href", Matchers
|
||||
.containsString("/api/core/items/" + publicItem.getID() + "/bitstreams")));
|
||||
|
||||
|
||||
String token = getAuthToken(admin.getEmail(), password);
|
||||
|
||||
|
||||
//Delete public item
|
||||
getClient(token).perform(delete("/api/core/items/" + publicItem.getID()))
|
||||
.andExpect(status().is(204));
|
||||
|
||||
|
||||
//Trying to get deleted item should fail with 404
|
||||
getClient().perform(get("/api/core/items/" + publicItem.getID()))
|
||||
.andExpect(status().is(404));
|
||||
|
||||
|
||||
//Trying to get deleted item bitstream should fail with 404
|
||||
getClient().perform(get("/api/core/biststreams/" + bitstream.getID()))
|
||||
.andExpect(status().is(404));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void deleteOneTemplateTest() throws Exception {
|
||||
context.turnOffAuthorisationSystem();
|
||||
|
||||
@@ -381,16 +378,42 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
|
||||
Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build();
|
||||
|
||||
//2. One template item.
|
||||
Item templateItem = ItemBuilder.createTemplateItem(context, col1);
|
||||
|
||||
Item templateItem = CollectionBuilder.createTemplateItem(context, col1);
|
||||
|
||||
String token = getAuthToken(admin.getEmail(), password);
|
||||
|
||||
//Check templateItem creation
|
||||
getClient().perform(get("/api/core/items/" + templateItem.getID()))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
|
||||
//Trying to delete a templateItem should fail with 422
|
||||
getClient(token).perform(delete("/api/core/items/" + templateItem.getID()))
|
||||
.andExpect(status().is(422));
|
||||
|
||||
//Check templateItem is available after failed deletion
|
||||
getClient().perform(get("/api/core/items/" + templateItem.getID()))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
public void deleteOneWorkspaceTest() throws Exception {
|
||||
context.turnOffAuthorisationSystem();
|
||||
|
||||
//** GIVEN **
|
||||
//1. A community with one collection.
|
||||
parentCommunity = CommunityBuilder.createCommunity(context)
|
||||
.withName("Parent Community")
|
||||
.build();
|
||||
|
||||
Collection col1 = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection 1").build();
|
||||
|
||||
//2. One workspace item.
|
||||
WorkspaceItem workspaceItem = WorkspaceItemBuilder.createWorkspaceItem(context, col1)
|
||||
.build();
|
||||
|
||||
String token = getAuthToken(admin.getEmail(), password);
|
||||
|
||||
//Trying to delete a workspaceItem should fail with 422
|
||||
getClient(token).perform(delete("/api/core/items/" + workspaceItem.getItem().getID()))
|
||||
.andExpect(status().is(422));
|
||||
|
||||
//Check templateItem is available after failed deletion
|
||||
getClient().perform(get("/api/core/items/" + workspaceItem.getID()))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
}
|
||||
|
@@ -16,6 +16,7 @@ import org.apache.commons.lang3.CharEncoding;
|
||||
import org.dspace.authorize.AuthorizeException;
|
||||
import org.dspace.content.Collection;
|
||||
import org.dspace.content.Community;
|
||||
import org.dspace.content.Item;
|
||||
import org.dspace.content.MetadataSchema;
|
||||
import org.dspace.content.service.DSpaceObjectService;
|
||||
import org.dspace.core.Context;
|
||||
@@ -29,7 +30,7 @@ import org.dspace.core.Context;
|
||||
public class CollectionBuilder extends AbstractDSpaceObjectBuilder<Collection> {
|
||||
|
||||
private Collection collection;
|
||||
|
||||
|
||||
protected CollectionBuilder(Context context) {
|
||||
super(context);
|
||||
|
||||
@@ -78,6 +79,20 @@ public class CollectionBuilder extends AbstractDSpaceObjectBuilder<Collection> {
|
||||
return collection;
|
||||
}
|
||||
|
||||
public static Item createTemplateItem(final Context context, final Collection collection) {
|
||||
CollectionBuilder builder = new CollectionBuilder(context);
|
||||
return builder.create_template_item(collection);
|
||||
}
|
||||
|
||||
private Item create_template_item(final Collection collection) {
|
||||
try {
|
||||
Item item = itemService.createTemplateItem(context, collection);
|
||||
return item;
|
||||
} catch (Exception e) {
|
||||
return handleException(e);
|
||||
}
|
||||
}
|
||||
|
||||
protected void cleanup() throws Exception {
|
||||
delete(collection);
|
||||
}
|
||||
|
@@ -36,22 +36,8 @@ public class ItemBuilder extends AbstractDSpaceObjectBuilder<Item> {
|
||||
ItemBuilder builder = new ItemBuilder(context);
|
||||
return builder.create(context, col);
|
||||
}
|
||||
|
||||
public static Item createTemplateItem(final Context context, final Collection collection) {
|
||||
ItemBuilder builder = new ItemBuilder(context);
|
||||
return builder.create_template_item(collection);
|
||||
}
|
||||
|
||||
private Item create_template_item(final Collection collection) {
|
||||
try {
|
||||
item = itemService.createTemplateItem(context, collection);
|
||||
return item;
|
||||
} catch (Exception e) {
|
||||
return handleException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private ItemBuilder create(final Context context, final Collection col) {
|
||||
|
||||
private ItemBuilder create(final Context context, final Collection col) {
|
||||
this.context = context;
|
||||
|
||||
try {
|
||||
@@ -84,7 +70,7 @@ public class ItemBuilder extends AbstractDSpaceObjectBuilder<Item> {
|
||||
item.setDiscoverable(false);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public ItemBuilder withEmbargoPeriod(String embargoPeriod) {
|
||||
return setEmbargo(embargoPeriod, item);
|
||||
}
|
||||
@@ -113,7 +99,7 @@ public class ItemBuilder extends AbstractDSpaceObjectBuilder<Item> {
|
||||
return handleException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected void cleanup() throws Exception {
|
||||
delete(item);
|
||||
}
|
||||
|
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* The contents of this file are subject to the license and copyright
|
||||
* detailed in the LICENSE and NOTICE files at the root of the source
|
||||
* tree and available online at
|
||||
*
|
||||
* http://www.dspace.org/license/
|
||||
*/
|
||||
package org.dspace.app.rest.builder;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.dspace.content.Collection;
|
||||
import org.dspace.content.Item;
|
||||
import org.dspace.content.WorkspaceItem;
|
||||
import org.dspace.content.service.DSpaceObjectService;
|
||||
import org.dspace.content.service.WorkspaceItemService;
|
||||
import org.dspace.core.Context;
|
||||
import org.dspace.service.DSpaceCRUDService;
|
||||
|
||||
/**
|
||||
* Builder to construct WorkspaceItem objects
|
||||
*
|
||||
**/
|
||||
public class WorkspaceItemBuilder extends AbstractBuilder<WorkspaceItem, WorkspaceItemService> {
|
||||
|
||||
/* Log4j logger*/
|
||||
private static final Logger log = Logger.getLogger(AbstractDSpaceObjectBuilder.class);
|
||||
|
||||
private WorkspaceItem workspaceItem;
|
||||
private WorkspaceItemService workspaceItemService;
|
||||
|
||||
protected WorkspaceItemBuilder(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public static WorkspaceItemBuilder createWorkspaceItem(final Context context, final Collection col) {
|
||||
WorkspaceItemBuilder builder = new WorkspaceItemBuilder(context);
|
||||
return builder.create(context, col);
|
||||
}
|
||||
|
||||
private WorkspaceItemBuilder create(final Context context, final Collection col) {
|
||||
this.context = context;
|
||||
|
||||
try {
|
||||
workspaceItem = workspaceItemService.create(context, col, false);
|
||||
} catch (Exception e) {
|
||||
return handleException(e);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
protected <B> B handleException(final Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WorkspaceItem build() {
|
||||
try {
|
||||
return workspaceItem;
|
||||
} catch (Exception e) {
|
||||
return handleException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(WorkspaceItem dso) throws Exception {
|
||||
try (Context c = new Context()) {
|
||||
c.turnOffAuthorisationSystem();
|
||||
WorkspaceItem attachedDso = c.reloadEntity(dso);
|
||||
if (attachedDso != null) {
|
||||
getService().deleteAll(c, attachedDso);
|
||||
}
|
||||
c.complete();
|
||||
}
|
||||
|
||||
indexingService.commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void cleanup() throws Exception {
|
||||
delete(workspaceItem);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WorkspaceItemService getService() {
|
||||
return workspaceItemService;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user