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

WIP. Implement updateMetadataOfItem method

parent 0a967d9e
......@@ -2,17 +2,20 @@ package org.gesis.dda.feeder.ssoar;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.inject.Inject;
import org.gesis.dda.feeder.ssoar.domain.Collection;
import org.gesis.dda.feeder.ssoar.domain.Item;
import org.gesis.dda.feeder.ssoar.domain.MetadataEntry;
import org.gesis.dda.feeder.ssoar.domain.Status;
import org.gesis.dda.feeder.ssoar.domain.User;
import org.gesis.dda.feeder.ssoar.domain.XmlWorkflowItem;
import org.gesis.dda.wizard.config.IngesterProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
......@@ -24,6 +27,9 @@ 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 {
......@@ -115,4 +121,48 @@ public class SsoarRestService {
HttpEntity<?> httpEntity = new HttpEntity<>( itemSpecification.getMetadata(), headers );
restTemplate.postForEntity( endpoint + "/items/{itemId}/metadata", httpEntity, null, itemSpecification.getId() );
}
public void updateMetadataOfItem(Item itemSpecification) {
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 );
restTemplate.exchange(
endpoint + "/items/{itemId}/metadata",
HttpMethod.PUT,
newHttpEntity,
Void.class, // doesn't matter
itemSpecification.getId() );
}
}
......@@ -8,8 +8,24 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
public class MetadataEntry {
private String key;
private String value;
private String language;
private String value;
public MetadataEntry() {
//default constructor for Jackson
}
public MetadataEntry(MetadataEntry template) {
this.key = template.key;
this.language = template.language;
this.value = template.value;
}
public MetadataEntry(String key, String language, String value) {
this.key = key;
this.language = language;
this.value = value;
}
public String getKey() {
return key;
......
......@@ -7,7 +7,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.List;
import java.util.UUID;
......@@ -84,10 +87,7 @@ public class SsoarRestServiceIntTest {
log.debug("Item: {}", actualItem);
assertEquals("Notes of a dummy editor", actualItem.getName() );
List<MetadataEntry> actualMetadata = actualItem.getMetadata();
MetadataEntry expectedMetadatum = new MetadataEntry();
expectedMetadatum.setKey("dc.contributor.author");
expectedMetadatum.setLanguage("en_US");
expectedMetadatum.setValue("Editor, Dummy");
MetadataEntry expectedMetadatum = new MetadataEntry("dc.contributor.author", "en_US", "Editor, Dummy");
assertThat( actualMetadata, hasItem(expectedMetadatum) );
}
......@@ -111,14 +111,8 @@ public class SsoarRestServiceIntTest {
public void addMetadataToItem() {
String randomString = UUID.randomUUID().toString();
MetadataEntry metadatum1 = new MetadataEntry();
metadatum1.setKey("dc.contributor.author");
metadatum1.setValue("Editor, Dummy " + randomString);
MetadataEntry metadatum2 = new MetadataEntry();
metadatum2.setKey("dc.title.alternative");
metadatum2.setLanguage("de");
metadatum2.setValue("Deutsch " + randomString);
MetadataEntry metadatum1 = new MetadataEntry("dc.contributor.author", null, "Editor, Dummy " + randomString);
MetadataEntry metadatum2 = new MetadataEntry("dc.title.alternative", "de", "Deutsch " + randomString);
List<MetadataEntry> requestedNewMetadata = Arrays.asList(metadatum1, metadatum2);
Item itemSpecification = new Item();
......@@ -134,4 +128,61 @@ public class SsoarRestServiceIntTest {
List<MetadataEntry> actualUpdatedMetadata = actualUpdatedItem.getMetadata();
assertTrue( actualUpdatedMetadata.containsAll(requestedNewMetadata) );
}
@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;
}
}
log.debug("metadatum old value: {}", toBeReplacedMetadatum);
log.debug("metadatum to-be value: {}", toBeUpdatedMetadatum);
List<MetadataEntry> toBeUpdatedMetadata = Arrays.asList(toBeUpdatedMetadatum);
Item toBeUpdatedItemSpecification = new Item();
toBeUpdatedItemSpecification.setId(39939);
toBeUpdatedItemSpecification.setMetadata(toBeUpdatedMetadata);
ssoarRestService.updateMetadataOfItem(toBeUpdatedItemSpecification);
Item updatedItemSpecification = new Item();
updatedItemSpecification.setId(39939);
updatedItemSpecification.setExpand(Arrays.asList("all"));
Item actualUpdatedItem = ssoarRestService.getItem(updatedItemSpecification);
assertNotNull(actualUpdatedItem);
List<MetadataEntry> actualUpdatedMetadata = actualUpdatedItem.getMetadata();
assertNotNull(actualUpdatedMetadata);
int actualUpdatedMetadataSize = actualUpdatedMetadata.size();
assertEquals(beforeMetadataSize, actualUpdatedMetadataSize);
List<MetadataEntry> expectedAfterMetadata = new ArrayList<>(beforeMetadata);
expectedAfterMetadata.remove(toBeReplacedMetadatum);
expectedAfterMetadata.add(toBeUpdatedMetadatum);
assertThat( actualUpdatedMetadata, containsInAnyOrder( expectedAfterMetadata.toArray() ) );
}
}
Supports Markdown
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