75469: The REST API should return the entitytype property

This commit is contained in:
Peter Nijs
2020-12-29 13:26:19 +01:00
parent 250c87dc16
commit 088bcb64ce
3 changed files with 203 additions and 0 deletions

View File

@@ -10,17 +10,23 @@ package org.dspace.app.rest.converter;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import org.apache.logging.log4j.Logger;
import org.dspace.app.rest.model.ItemRest;
import org.dspace.app.rest.model.MetadataValueList;
import org.dspace.app.rest.projection.Projection;
import org.dspace.content.Entity;
import org.dspace.content.EntityType;
import org.dspace.content.Item;
import org.dspace.content.MetadataField;
import org.dspace.content.MetadataValue;
import org.dspace.content.service.EntityService;
import org.dspace.content.service.ItemService;
import org.dspace.core.Context;
import org.dspace.discovery.IndexableObject;
import org.dspace.xoai.services.api.context.ContextService;
import org.dspace.xoai.services.api.context.ContextServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -41,6 +47,12 @@ public class ItemConverter
@Autowired
private ItemService itemService;
@Autowired
private EntityService entityService;
@Autowired
private ContextService contextService;
private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(ItemConverter.class);
@Override
@@ -51,6 +63,18 @@ public class ItemConverter
item.setWithdrawn(obj.isWithdrawn());
item.setLastModified(obj.getLastModified());
try {
Entity entity = entityService.findByItemId(contextService.getContext(), UUID.fromString(item.getId()));
EntityType entityType = entityService.getType(contextService.getContext(), entity);
if (entityType != null) {
item.setEntityType(entityType.getLabel());
}
} catch (SQLException e) {
log.error("Error getting item's entity type", e);
} catch (ContextServiceException e) {
log.error("Error getting context", e);
}
return item;
}

View File

@@ -58,6 +58,7 @@ public class ItemRest extends DSpaceObjectRest {
private boolean discoverable = false;
private boolean withdrawn = false;
private Date lastModified = new Date();
private String entityType = null;
@Override
public String getCategory() {
@@ -101,4 +102,14 @@ public class ItemRest extends DSpaceObjectRest {
public void setLastModified(Date lastModified) {
this.lastModified = lastModified;
}
public String getEntityType() {
return entityType;
}
public void setEntityType(String entityType) {
if (this.entityType == null) {
this.entityType = entityType;
}
}
}

View File

@@ -12,6 +12,7 @@ import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
import static org.dspace.app.rest.matcher.MetadataMatcher.matchMetadata;
import static org.dspace.app.rest.matcher.MetadataMatcher.matchMetadataDoesNotExist;
import static org.dspace.core.Constants.WRITE;
import static org.hamcrest.Matchers.emptyOrNullString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
@@ -25,6 +26,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@@ -41,6 +43,7 @@ import org.dspace.app.rest.matcher.ItemMatcher;
import org.dspace.app.rest.model.ItemRest;
import org.dspace.app.rest.model.MetadataRest;
import org.dspace.app.rest.model.MetadataValueRest;
import org.dspace.app.rest.model.patch.AddOperation;
import org.dspace.app.rest.model.patch.Operation;
import org.dspace.app.rest.model.patch.ReplaceOperation;
import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
@@ -49,6 +52,7 @@ import org.dspace.builder.BitstreamBuilder;
import org.dspace.builder.CollectionBuilder;
import org.dspace.builder.CommunityBuilder;
import org.dspace.builder.EPersonBuilder;
import org.dspace.builder.EntityTypeBuilder;
import org.dspace.builder.GroupBuilder;
import org.dspace.builder.ItemBuilder;
import org.dspace.builder.ResourcePolicyBuilder;
@@ -56,6 +60,7 @@ import org.dspace.builder.WorkspaceItemBuilder;
import org.dspace.content.Bitstream;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.EntityType;
import org.dspace.content.Item;
import org.dspace.content.WorkspaceItem;
import org.dspace.content.service.CollectionService;
@@ -2740,5 +2745,168 @@ public class ItemRestRepositoryIT extends AbstractControllerIntegrationTest {
}
@Test
public void testEntityTypePerson() throws Exception {
context.turnOffAuthorisationSystem();
EntityType person = EntityTypeBuilder.createEntityTypeBuilder(context, "Person").build();
Community community = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Collection collection = CollectionBuilder.createCollection(context, community).withName("Collection").build();
Item item = ItemBuilder.createItem(context, collection)
.withTitle("Author1")
.withIssueDate("2017-10-17")
.withAuthor("Smith, Donald")
.withPersonIdentifierLastName("Smith")
.withPersonIdentifierFirstName("Donald")
.withRelationshipType("Person")
.build();
context.restoreAuthSystemState();
String token = getAuthToken(eperson.getEmail(), password);
getClient(token).perform(get("/api/core/items/" + item.getID()))
.andExpect(status().isOk())
.andExpect(jsonPath("$", ItemMatcher.matchItemProperties(item)))
.andExpect(jsonPath("$.entityType", is("Person")));
}
@Test
public void testEntityTypePublication() throws Exception {
context.turnOffAuthorisationSystem();
EntityType publication = EntityTypeBuilder.createEntityTypeBuilder(context, "Publication").build();
Community community = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Collection collection = CollectionBuilder.createCollection(context, community).withName("Collection").build();
Item item = ItemBuilder.createItem(context, collection)
.withTitle("Publication1")
.withAuthor("Testy, TEst")
.withIssueDate("2015-01-01")
.withRelationshipType("Publication")
.build();
context.restoreAuthSystemState();
String token = getAuthToken(eperson.getEmail(), password);
getClient(token).perform(get("/api/core/items/" + item.getID()))
.andExpect(status().isOk())
.andExpect(jsonPath("$", ItemMatcher.matchItemProperties(item)))
.andExpect(jsonPath("$.entityType", is("Publication")));
}
@Test
public void testEntityTypeOrgUnit() throws Exception {
context.turnOffAuthorisationSystem();
EntityType orgUnit = EntityTypeBuilder.createEntityTypeBuilder(context, "OrgUnit").build();
Community community = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Collection collection = CollectionBuilder.createCollection(context, community).withName("Collection").build();
Item item = ItemBuilder.createItem(context, collection)
.withTitle("OrgUnit1")
.withAuthor("Testy, TEst")
.withIssueDate("2015-01-01")
.withRelationshipType("OrgUnit")
.build();
context.restoreAuthSystemState();
String token = getAuthToken(eperson.getEmail(), password);
getClient(token).perform(get("/api/core/items/" + item.getID()))
.andExpect(status().isOk())
.andExpect(jsonPath("$", ItemMatcher.matchItemProperties(item)))
.andExpect(jsonPath("$.entityType", is("OrgUnit")));
}
@Test
public void testEntityTypeProject() throws Exception {
context.turnOffAuthorisationSystem();
EntityType project = EntityTypeBuilder.createEntityTypeBuilder(context, "Project").build();
Community community = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Collection collection = CollectionBuilder.createCollection(context, community).withName("Collection").build();
Item item = ItemBuilder.createItem(context, collection)
.withTitle("Project1")
.withAuthor("Testy, TEst")
.withIssueDate("2015-01-01")
.withRelationshipType("Project")
.build();
context.restoreAuthSystemState();
String token = getAuthToken(eperson.getEmail(), password);
getClient(token).perform(get("/api/core/items/" + item.getID()))
.andExpect(status().isOk())
.andExpect(jsonPath("$", ItemMatcher.matchItemProperties(item)))
.andExpect(jsonPath("$.entityType", is("Project")));
}
@Test
public void testEntityTypeNull() throws Exception {
context.turnOffAuthorisationSystem();
Community community = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Collection collection = CollectionBuilder.createCollection(context, community).withName("Collection").build();
Item item = ItemBuilder.createItem(context, collection)
.withTitle("Unclassified item")
.withAuthor("Testy, TEst")
.withIssueDate("2015-01-01")
.build();
context.restoreAuthSystemState();
String token = getAuthToken(eperson.getEmail(), password);
getClient(token).perform(get("/api/core/items/" + item.getID()))
.andExpect(status().isOk())
.andExpect(jsonPath("$", ItemMatcher.matchItemProperties(item)))
.andExpect(jsonPath("$.entityType", is(emptyOrNullString())));
}
@Test
public void testEntityTypeModification() throws Exception {
context.turnOffAuthorisationSystem();
EntityType publication = EntityTypeBuilder.createEntityTypeBuilder(context, "Publication").build();
Community community = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Collection collection = CollectionBuilder.createCollection(context, community).withName("Collection").build();
Item item = ItemBuilder.createItem(context, collection)
.withTitle("Publication1")
.withAuthor("Testy, TEst")
.withIssueDate("2015-01-01")
.build();
context.restoreAuthSystemState();
// Verify there is no entityType yet
String token = getAuthToken(admin.getEmail(), password);
getClient(token).perform(get("/api/core/items/" + item.getID()))
.andExpect(status().isOk())
.andExpect(jsonPath("$", ItemMatcher.matchItemProperties(item)))
.andExpect(jsonPath("$.entityType", is(emptyOrNullString())));
// Modify the entityType and verify the response already contains this modification
List<Operation> ops = new ArrayList<>();
List<Map<String, String>> values = new ArrayList<>();
Map<String, String> value = new HashMap<>();
value.put("value", "Publication");
values.add(value);
AddOperation addOperation = new AddOperation("/metadata/relationship.type", values);
ops.add(addOperation);
String patchBody = getPatchContent(ops);
getClient(token).perform(patch("/api/core/items/" + item.getID())
.content(patchBody)
.contentType(contentType))
.andExpect(status().isOk())
.andExpect(jsonPath("$", ItemMatcher.matchItemProperties(item)))
.andExpect(jsonPath("$.entityType", is("Publication")));
// Verify this modification is permanent
getClient(token).perform(get("/api/core/items/" + item.getID()))
.andExpect(status().isOk())
.andExpect(jsonPath("$", ItemMatcher.matchItemProperties(item)))
.andExpect(jsonPath("$.entityType", is("Publication")));
}
}