Commit 44e87fab authored by Matija Obreza's avatar Matija Obreza

Fix: Accession API upsert fails for "storage" and conversion to array does not handle null values

- Added JUnit test for /upsert
- Fixed AccessionId#removeEmpty (it must reuse existing collection)
parent 4d56b721
......@@ -79,11 +79,11 @@ import io.swagger.annotations.Api;
@RestController("accessionApi0")
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = { AccessionController.CONTROLLER_URL, "/json/v0/acn" })
@RequestMapping(value = { AccessionController.API_BASE, "/json/v0/acn" })
@Api(tags = { "accession" })
public class AccessionController extends ApiBaseController {
private static final int UPLOAD_RETRIES = 10;
protected static final String CONTROLLER_URL = ApiBaseController.APIv0_BASE + "/acn";
public static final String API_BASE = ApiBaseController.APIv0_BASE + "/acn";
private final ObjectMapper mapper = new ObjectMapper();
......@@ -449,11 +449,13 @@ public class AccessionController extends ApiBaseController {
private void upgradeToArray(ObjectNode obj, String fieldName) {
JsonNode node = obj.get(fieldName);
if (node != null && !node.isArray()) {
ArrayNode arrNode = obj.putArray(fieldName);
for (String x : node.asText().split(";")) {
if (StringUtils.isNotBlank(x)) {
arrNode.add(StringUtils.trim(x));
if (node != null) {
if (! node.isNull() && ! node.isArray()) {
ArrayNode arrNode = obj.putArray(fieldName);
for (String x : node.asText().split(";")) {
if (StringUtils.isNotBlank(x)) {
arrNode.add(StringUtils.trim(x));
}
}
}
}
......
......@@ -100,7 +100,7 @@ public class AccessionId extends AuditedVersionedModel implements IdUUID {
@CollectionTable(name = "accession_storage", joinColumns = @JoinColumn(name = "accessionId", referencedColumnName = "id"))
@OrderBy("storage")
@Field(type = FieldType.Auto, index = FieldIndex.not_analyzed)
private Set<Integer> storage = null;
private Set<Integer> storage;
@Column(name = "duplSite", nullable = false, length = 9)
@ElementCollection(fetch = FetchType.LAZY)
......@@ -179,13 +179,15 @@ public class AccessionId extends AuditedVersionedModel implements IdUUID {
if (set == null || set.isEmpty()) {
return set;
}
return set.stream().filter(s -> {
if (s != null && s instanceof String) {
return StringUtils.isNotBlank((String) s);
set.removeAll(set.stream().filter(s -> {
if (s instanceof String) {
return StringUtils.isBlank((String) s);
} else {
return s != null;
return s == null;
}
}).collect(Collectors.toSet());
}).collect(Collectors.toSet()));
return set;
}
@Override
......
......@@ -398,7 +398,7 @@ public class AccessionUploader implements InitializingBean {
}
}
@SuppressWarnings("rawtypes")
@SuppressWarnings({ "unchecked" })
private <T> void copy(Class<T> clazz, T source, T target, Iterator<String> fieldNames) throws IllegalArgumentException, IllegalAccessException {
String fieldName = null;
while (fieldNames.hasNext() && (fieldName = fieldNames.next()) != null) {
......@@ -409,10 +409,14 @@ public class AccessionUploader implements InitializingBean {
final Object srcValue = field.get(source);
final Object dest = field.get(target);
// handle collections better
if (srcValue instanceof Collection && dest != null) {
Collection collection = (Collection) dest;
if (dest != null && dest instanceof Collection) {
Collection<Object> collection = (Collection<Object>) dest;
collection.clear();
collection.addAll((Collection) srcValue);
if (srcValue != null && srcValue instanceof Collection) {
collection.addAll(((Collection<Object>) srcValue).stream().filter(o -> o != null).collect(Collectors.toList()));
} else {
LOG.trace("Clearing {}, source was empty", fieldName);
}
} else {
field.set(target, srcValue);
}
......
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