Commit cb449215 authored by Gerrit Hübbers's avatar Gerrit Hübbers 🃏
Browse files

WIP. Implement removeMetadataFromItem. Remove that updateMetadata method, as...

WIP. Implement removeMetadataFromItem. Remove that updateMetadata method, as its semantics were broken wrt reoccuring metadata such as dc.contributor.author
parent 8b085f67
......@@ -2,7 +2,6 @@ package org.gesis.dda.feeder.ssoar;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.inject.Inject;
......@@ -27,9 +26,6 @@ import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
@Service
public class SsoarRestService {
......@@ -118,51 +114,47 @@ public class SsoarRestService {
public void addMetadataToItem(Item itemSpecification) {
HttpHeaders headers = new HttpHeaders();
headers.set("rest-dspace-token", authenticationToken);
HttpEntity<?> httpEntity = new HttpEntity<>( itemSpecification.getMetadata(), headers );
restTemplate.postForEntity( endpoint + "/items/{itemId}/metadata", httpEntity, null, itemSpecification.getId() );
HttpEntity<?> additionalMetadataHttpEntity = new HttpEntity<>( itemSpecification.getMetadata(), headers );
restTemplate.postForEntity( endpoint + "/items/{itemId}/metadata", additionalMetadataHttpEntity, Void.class, itemSpecification.getId() );
}
public void updateMetadataOfItem(Item itemSpecification) {
public void replaceAllMetadataOfItem(Item itemWithNewMetadataSpecification) {
HttpHeaders headers = new HttpHeaders();
headers.set("rest-dspace-token", authenticationToken);
HttpEntity<?> httpEntity = new HttpEntity<>(headers);
ResponseEntity<List<MetadataEntry>> itemsResponseEntity = restTemplate.exchange( endpoint + "/items/{itemId}/metadata", HttpMethod.GET, httpEntity, new ParameterizedTypeReference<List<MetadataEntry>>() {}, itemSpecification.getId() );
List<MetadataEntry> oldMetadata = itemsResponseEntity.getBody();
log.debug("old metadata: {}", oldMetadata);
Table<String, String, String> table = HashBasedTable.create();
for (MetadataEntry oldMetadatum : oldMetadata) {
// Table does not like null row indices
String language = oldMetadatum.getLanguage() != null ? oldMetadatum.getLanguage() : "null";
table.put(oldMetadatum.getKey(), language, oldMetadatum.getValue());
}
List<MetadataEntry> toBeUpdatedMetadata = itemSpecification.getMetadata();
for (MetadataEntry toBeUpdatedMetadatum : toBeUpdatedMetadata) {
// add metadata, overwriting any old value
// Table does not like null row indices
String language = toBeUpdatedMetadatum.getLanguage() != null ? toBeUpdatedMetadatum.getLanguage() : "null";
table.put(toBeUpdatedMetadatum.getKey(), language, toBeUpdatedMetadatum.getValue() );
}
List<MetadataEntry> updatedMetadata = table
.cellSet()
.stream()
.map(c -> new MetadataEntry(
c.getRowKey(),
c.getColumnKey().equals("null") ? null : c.getColumnKey(),
c.getValue()
)
)
.collect(Collectors.toList());
log.debug("updated Metadata");
HttpEntity<List<MetadataEntry>> newHttpEntity = new HttpEntity<>( updatedMetadata, headers );
HttpEntity<List<MetadataEntry>> newMetadataHttpEntity = new HttpEntity<>( itemWithNewMetadataSpecification.getMetadata(), headers );
restTemplate.exchange(
endpoint + "/items/{itemId}/metadata",
HttpMethod.PUT,
newHttpEntity,
newMetadataHttpEntity,
Void.class, // doesn't matter
itemSpecification.getId() );
itemWithNewMetadataSpecification.getId() );
}
/**
* From the Item specified by itemWithMetadataToBeRemovedSpecification,
* remove all occurences of MetadataEntries specified by itemWithMetadataToBeRemovedSpecification.getMetadata().
* @param itemWithMetadataToBeRemovedSpecification
*/
public void removeMetadataFromItem(Item itemWithMetadataToBeRemovedSpecification) {
int targetItemId = itemWithMetadataToBeRemovedSpecification.getId();
HttpHeaders headers = new HttpHeaders();
headers.set("rest-dspace-token", authenticationToken);
HttpEntity<?> httpEntity = new HttpEntity<>(headers);
ResponseEntity<List<MetadataEntry>> oldMetadataResponseEntity = restTemplate.exchange( endpoint + "/items/{itemId}/metadata", HttpMethod.GET, httpEntity, new ParameterizedTypeReference<List<MetadataEntry>>() {}, targetItemId );
List<MetadataEntry> oldMetadata = oldMetadataResponseEntity.getBody();
List<MetadataEntry> metadataToBeRemoved = itemWithMetadataToBeRemovedSpecification.getMetadata();
oldMetadata.removeAll(metadataToBeRemoved);
List<MetadataEntry> updatedMetadata = oldMetadata; // redundant variable to make intent clear;
Item updatedItem = new Item();
updatedItem.setId(targetItemId);
updatedItem.setMetadata(updatedMetadata);
replaceAllMetadataOfItem(updatedItem);
}
}
......@@ -55,7 +55,7 @@ public class MetadataEntry {
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
@Override
public int hashCode() {
final int prime = 31;
......@@ -65,7 +65,10 @@ public class MetadataEntry {
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
/** Two MetadataEntry objects are considered equal iff their key, language, and value are all mutually equal.
*
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
......
......@@ -8,10 +8,10 @@ import static org.junit.Assert.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.core.IsNot.not;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
......@@ -130,59 +130,43 @@ public class SsoarRestServiceIntTest {
}
@Test
public void updateMetadataOfItem() {
String randomString = UUID.randomUUID().toString();
MetadataEntry toBeUpdatedMetadatum = new MetadataEntry("dc.title", null, "Posted to Social Problems Collection, " + randomString);
Item beforeItemSpecification = new Item();
beforeItemSpecification.setId(39939);
beforeItemSpecification.setExpand(Arrays.asList("all"));
Item beforeItem = ssoarRestService.getItem(beforeItemSpecification);
assertNotNull(beforeItem);
log.debug("Item: {}", beforeItem);
assertEquals("document/45701", beforeItem.getHandle() );
List<MetadataEntry> beforeMetadata = beforeItem.getMetadata();
assertNotNull(beforeMetadata);
int beforeMetadataSize = beforeMetadata.size();
assertThat( beforeMetadata, not( hasItem(toBeUpdatedMetadatum) ) );
MetadataEntry toBeReplacedMetadatum = null;
for (MetadataEntry someBeforeMetadatum : beforeMetadata) {
if ( someBeforeMetadatum.getKey().equals( toBeUpdatedMetadatum.getKey() ) ){
toBeReplacedMetadatum = someBeforeMetadatum;
break;
}
}
public void removeMetadataFromItem() {
Item itemSpecification = new Item();
itemSpecification.setId(39939);
itemSpecification.setExpand(Arrays.asList("all"));
log.debug("metadatum old value: {}", toBeReplacedMetadatum);
log.debug("metadatum to-be value: {}", toBeUpdatedMetadatum);
Item beforeManipulationItem = ssoarRestService.getItem(itemSpecification);
List<MetadataEntry> beforeUpdateMetadataEntry = beforeManipulationItem.getMetadata();
List<MetadataEntry> toBeUpdatedMetadata = Arrays.asList(toBeUpdatedMetadatum);
Item toBeUpdatedItemSpecification = new Item();
toBeUpdatedItemSpecification.setId(39939);
toBeUpdatedItemSpecification.setMetadata(toBeUpdatedMetadata);
MetadataEntry toBeUpdatedMetadatum = new MetadataEntry("dc.contributor.author", null, "Mustermann, Max");
assertThat( beforeUpdateMetadataEntry, not( hasItem(toBeUpdatedMetadatum) ) );
ssoarRestService.updateMetadataOfItem(toBeUpdatedItemSpecification);
MetadataEntry yetAnotherIdenticalMetadatum = new MetadataEntry("dc.contributor.author", null, "Mustermann, Max"); // redundant to make intent clear
Item updatedItemSpecification = new Item();
updatedItemSpecification.setId(39939);
updatedItemSpecification.setExpand(Arrays.asList("all"));
List<MetadataEntry> toBeAddedMetadata = new ArrayList<>();
toBeAddedMetadata.add(toBeUpdatedMetadatum);
toBeAddedMetadata.add(yetAnotherIdenticalMetadatum);
Item actualUpdatedItem = ssoarRestService.getItem(updatedItemSpecification);
assertNotNull(actualUpdatedItem);
Item sameItemWithAdditionalMetadataSpecification = new Item();
sameItemWithAdditionalMetadataSpecification.setId(39939);
sameItemWithAdditionalMetadataSpecification.setMetadata(toBeAddedMetadata);
List<MetadataEntry> actualUpdatedMetadata = actualUpdatedItem.getMetadata();
assertNotNull(actualUpdatedMetadata);
int actualUpdatedMetadataSize = actualUpdatedMetadata.size();
ssoarRestService.addMetadataToItem(sameItemWithAdditionalMetadataSpecification);
assertEquals(beforeMetadataSize, actualUpdatedMetadataSize);
Item afterMetadataAdditionItem = ssoarRestService.getItem(itemSpecification);
List<MetadataEntry> afterAdditionMetadata = afterMetadataAdditionItem.getMetadata();
int maxMustermannContributorOccurencesAfterAddition = Collections.frequency(afterAdditionMetadata, toBeUpdatedMetadatum);
assertEquals(2, maxMustermannContributorOccurencesAfterAddition);
List<MetadataEntry> expectedAfterMetadata = new ArrayList<>(beforeMetadata);
expectedAfterMetadata.remove(toBeReplacedMetadatum);
expectedAfterMetadata.add(toBeUpdatedMetadatum);
MetadataEntry toBeRemovedMetadatum = new MetadataEntry("dc.contributor.author", null, "Mustermann, Max"); // redundant to make intent clear
Item itemWithMetadataToBeRemovedSpecification = new Item();
itemWithMetadataToBeRemovedSpecification.setId(39939);
itemWithMetadataToBeRemovedSpecification.setMetadata( Arrays.asList(toBeRemovedMetadatum) );
ssoarRestService.removeMetadataFromItem(itemWithMetadataToBeRemovedSpecification);
assertThat( actualUpdatedMetadata, containsInAnyOrder( expectedAfterMetadata.toArray() ) );
Item afterMetadataRemovalItem = ssoarRestService.getItem(itemSpecification);
List<MetadataEntry> afterRemovalMetadata = afterMetadataRemovalItem.getMetadata();
int maxMustermannContributorOccurencesAfterRemoval = Collections.frequency(afterRemovalMetadata, toBeRemovedMetadatum);
assertEquals(0, maxMustermannContributorOccurencesAfterRemoval);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment