Merge branch 'w2p-59343_support-ordered-metadata' into w2p-58898_place-column-calculation-error

# Conflicts:
#	dspace-spring-rest/src/main/java/org/dspace/app/rest/repository/DSpaceRestRepository.java
This commit is contained in:
Ben Bosman
2019-01-29 10:41:05 +01:00
6 changed files with 146 additions and 10 deletions

View File

@@ -27,7 +27,6 @@ import org.dspace.content.authority.Choices;
import org.dspace.content.authority.service.ChoiceAuthorityService;
import org.dspace.content.authority.service.MetadataAuthorityService;
import org.dspace.content.service.DSpaceObjectService;
import org.dspace.content.service.ItemService;
import org.dspace.content.service.MetadataFieldService;
import org.dspace.content.service.MetadataValueService;
import org.dspace.content.service.RelationshipService;
@@ -65,8 +64,6 @@ public abstract class DSpaceObjectServiceImpl<T extends DSpaceObject> implements
@Autowired(required = true)
protected MetadataAuthorityService metadataAuthorityService;
@Autowired(required = true)
protected ItemService itemService;
@Autowired(required = true)
protected RelationshipService relationshipService;
public DSpaceObjectServiceImpl() {
@@ -559,7 +556,7 @@ public abstract class DSpaceObjectServiceImpl<T extends DSpaceObject> implements
Map<MetadataField, Integer> fieldToLastPlace = new HashMap<>();
List<MetadataValue> metadataValues = new LinkedList<>();
if (dso.getType() == Constants.ITEM) {
metadataValues = itemService.getMetadata((Item) dso, Item.ANY, Item.ANY, Item.ANY, Item.ANY);
metadataValues = getMetadata(dso, Item.ANY, Item.ANY, Item.ANY, Item.ANY);
} else {
metadataValues = dso.getMetadata();
}

View File

@@ -11,6 +11,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
@@ -18,6 +19,8 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@@ -1329,6 +1332,8 @@ prevent the generation of resource policy entry values with null dspace_object a
listToReturn.add(metadataValue);
}
}
listToReturn = sortMetadataValueList(listToReturn);
return listToReturn;
} else {
@@ -1346,11 +1351,28 @@ prevent the generation of resource policy entry values with null dspace_object a
finalList.add(metadataValue);
}
}
finalList = sortMetadataValueList(finalList);
return finalList;
}
}
private List<MetadataValue> sortMetadataValueList(List<MetadataValue> listToReturn) {
Comparator<MetadataValue> comparator = Comparator.comparing(
metadataValue -> metadataValue.getMetadataField().getMetadataSchema().getName(),
Comparator.nullsFirst(Comparator.naturalOrder()));
comparator = comparator.thenComparing(metadataValue -> metadataValue.getMetadataField().getElement(),
Comparator.nullsFirst(Comparator.naturalOrder()));
comparator = comparator.thenComparing(metadataValue -> metadataValue.getMetadataField().getQualifier(),
Comparator.nullsFirst(Comparator.naturalOrder()));
comparator = comparator.thenComparing(metadataValue -> metadataValue.getPlace(),
Comparator.nullsFirst(Comparator.naturalOrder()));
Stream<MetadataValue> metadataValueStream = listToReturn.stream().sorted(comparator);
listToReturn = metadataValueStream.collect(Collectors.toList());
return listToReturn;
}
private List<RelationshipMetadataValue> handleItemRelationship(Context context, Item item, String entityType,
Relationship relationship,
boolean enableVirtualMetadata)

View File

@@ -122,10 +122,11 @@ public class RelationshipServiceImpl implements RelationshipService {
}
public void updateItem(Context context, Item leftItem)
@Override
public void updateItem(Context context, Item relatedItem)
throws SQLException, AuthorizeException {
leftItem.setMetadataModified();
itemService.update(context, leftItem);
relatedItem.setMetadataModified();
itemService.update(context, relatedItem);
}

View File

@@ -102,6 +102,17 @@ public interface RelationshipService extends DSpaceCRUDService<Relationship> {
public void updatePlaceInRelationship(Context context, Relationship relationship, boolean isCreation)
throws SQLException, AuthorizeException;
public void updateItem(Context context, Item leftItem) throws SQLException, AuthorizeException;
/**
* This method will update the given item's metadata order.
* If the relationships for the item have been modified and will calculate the place based on a
* metadata field, this function will ensure the place is calculated.
* @param context The relevant DSpace context
* @param relatedItem The Item for which the list of Relationship location is calculated
* based on a metadata field
* @throws SQLException If something goes wrong
* @throws AuthorizeException
* If the user is not authorized to update the item
*/
public void updateItem(Context context, Item relatedItem) throws SQLException, AuthorizeException;
}

View File

@@ -95,8 +95,7 @@ public class ItemConverter extends DSpaceObjectConverter<org.dspace.content.Item
item.setRelationships(relationshipRestList);
List<MetadataValue> fullList = new LinkedList<>();
fullList.addAll(obj.getMetadata());
fullList.addAll(itemService.getRelationshipMetadata(obj, true));
fullList = itemService.getMetadata(obj, Item.ANY, Item.ANY, Item.ANY, Item.ANY, true);
List<MetadataEntryRest> metadata = super.convertMetadataToRest(fullList);
item.setMetadata(metadata);

View File

@@ -493,11 +493,17 @@ public class RelationshipRestRepositoryIT extends AbstractControllerIntegrationT
List<MetadataValue> list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
assertEquals(2, list.size());
for (MetadataValue mdv : list) {
if (StringUtils.equals(mdv.getValue(), "plain text")) {
assertEquals(1, mdv.getPlace());
}
}
MetadataValue author0MD = list.get(0);
assertEquals("Smith, Donald", author0MD.getValue());
MetadataValue author1MD = list.get(1);
assertEquals("plain text", author1MD.getValue());
getClient(adminToken).perform(get("/api/core/relationships/" + firstRelationshipIdString))
.andExpect(status().isOk())
@@ -520,10 +526,19 @@ public class RelationshipRestRepositoryIT extends AbstractControllerIntegrationT
map = mapper.readValue(content, Map.class);
String secondRelationshipIdString = String.valueOf(map.get("id"));
list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
assertEquals(3, list.size());
getClient(adminToken).perform(get("/api/core/relationships/" + secondRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("leftPlace", is(2)));
author0MD = list.get(0);
assertEquals("Smith, Donald", author0MD.getValue());
author1MD = list.get(1);
assertEquals("plain text", author1MD.getValue());
MetadataValue author2MD = list.get(2);
assertEquals("Smith, Maria", author2MD.getValue());
publication = itemService.find(context, publication.getID());
itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text two");
// itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text three");
@@ -533,12 +548,22 @@ public class RelationshipRestRepositoryIT extends AbstractControllerIntegrationT
// itemService.update(context, publication);
list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
assertEquals(4, list.size());
for (MetadataValue mdv : list) {
if (StringUtils.equals(mdv.getValue(), "plain text two")) {
assertEquals(3, mdv.getPlace());
}
}
author0MD = list.get(0);
assertEquals("Smith, Donald", author0MD.getValue());
author1MD = list.get(1);
assertEquals("plain text", author1MD.getValue());
author2MD = list.get(2);
assertEquals("Smith, Maria", author2MD.getValue());
MetadataValue author3MD = list.get(3);
assertEquals("plain text two", author3MD.getValue());
mvcResult = getClient(adminToken).perform(post("/api/core/relationships")
.param("leftItem", publication.getID().toString())
@@ -557,21 +582,102 @@ public class RelationshipRestRepositoryIT extends AbstractControllerIntegrationT
map = mapper.readValue(content, Map.class);
String thirdRelationshipIdString = String.valueOf(map.get("id"));
list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
assertEquals(5, list.size());
getClient(adminToken).perform(get("/api/core/relationships/" + thirdRelationshipIdString))
.andExpect(status().isOk())
.andExpect(jsonPath("leftPlace", is(4)));
author0MD = list.get(0);
assertEquals("Smith, Donald", author0MD.getValue());
author1MD = list.get(1);
assertEquals("plain text", author1MD.getValue());
author2MD = list.get(2);
assertEquals("Smith, Maria", author2MD.getValue());
author3MD = list.get(3);
assertEquals("plain text two", author3MD.getValue());
MetadataValue author4MD = list.get(4);
assertEquals("Maybe, Maybe", author4MD.getValue());
publication = itemService.find(context, publication.getID());
itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text three");
itemService.update(context, publication);
list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
assertEquals(6, list.size());
for (MetadataValue mdv : list) {
if (StringUtils.equals(mdv.getValue(), "plain text three")) {
assertEquals(5, mdv.getPlace());
}
}
author0MD = list.get(0);
assertEquals("Smith, Donald", author0MD.getValue());
author1MD = list.get(1);
assertEquals("plain text", author1MD.getValue());
author2MD = list.get(2);
assertEquals("Smith, Maria", author2MD.getValue());
author3MD = list.get(3);
assertEquals("plain text two", author3MD.getValue());
author4MD = list.get(4);
assertEquals("Maybe, Maybe", author4MD.getValue());
MetadataValue author5MD = list.get(5);
assertEquals("plain text three", author5MD.getValue());
publication = itemService.find(context, publication.getID());
itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text four");
itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text five");
itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text six");
itemService.addMetadata(context, publication, "dc", "contributor", "author", Item.ANY, "plain text seven");
itemService.update(context, publication);
list = itemService.getMetadata(publication, "dc", "contributor", "author", Item.ANY);
assertEquals(10, list.size());
for (MetadataValue mdv : list) {
if (StringUtils.equals(mdv.getValue(), "plain text four")) {
assertEquals(6, mdv.getPlace());
}
if (StringUtils.equals(mdv.getValue(), "plain text five")) {
assertEquals(7, mdv.getPlace());
}
if (StringUtils.equals(mdv.getValue(), "plain text six")) {
assertEquals(8, mdv.getPlace());
}
if (StringUtils.equals(mdv.getValue(), "plain text seven")) {
assertEquals(9, mdv.getPlace());
}
}
author0MD = list.get(0);
assertEquals("Smith, Donald", author0MD.getValue());
author1MD = list.get(1);
assertEquals("plain text", author1MD.getValue());
author2MD = list.get(2);
assertEquals("Smith, Maria", author2MD.getValue());
author3MD = list.get(3);
assertEquals("plain text two", author3MD.getValue());
author4MD = list.get(4);
assertEquals("Maybe, Maybe", author4MD.getValue());
author5MD = list.get(5);
assertEquals("plain text three", author5MD.getValue());
MetadataValue author6MD = list.get(6);
assertEquals("plain text four", author6MD.getValue());
MetadataValue author7MD = list.get(7);
assertEquals("plain text five", author7MD.getValue());
MetadataValue author8MD = list.get(8);
assertEquals("plain text six", author8MD.getValue());
MetadataValue author9MD = list.get(9);
assertEquals("plain text seven", author9MD.getValue());
list = itemService.getMetadata(publication, "dc", "contributor", Item.ANY, Item.ANY);
assertEquals(10, list.size()); //same size as authors
list = itemService.getMetadata(publication, "dc", Item.ANY, Item.ANY, Item.ANY);
assertEquals(16, list.size()); //also includes title, 4 date fields, uri
list = itemService.getMetadata(publication, Item.ANY, Item.ANY, Item.ANY, Item.ANY);
assertEquals(20, list.size()); //also includes type and 3 relation.isAuthorOfPublication values
}
@Test