Commit e7317fe2 authored by Matija Obreza's avatar Matija Obreza

Accession API v1 updates

- properly remove blanks on insert and update
- improved error reporting
- reduced logging for known exceptions
parent bd8b0e31
......@@ -16,6 +16,8 @@
package org.genesys.catalog.api;
import com.fasterxml.jackson.annotation.JsonView;
import org.apache.commons.lang.StringUtils;
import org.genesys.blocks.model.JsonViews;
/**
......@@ -35,7 +37,11 @@ public class ApiError<T extends Throwable> {
* @param ex the ex
*/
public ApiError(final T ex) {
this.error = ex.getMessage();
if (StringUtils.isBlank(ex.getMessage())) {
this.error = ex.getClass().getSimpleName();
} else {
this.error = ex.getMessage();
}
this.localizedError = ex.getLocalizedMessage();
}
......
......@@ -21,6 +21,7 @@ import javax.servlet.http.HttpServletRequest;
import org.genesys.catalog.exceptions.InvalidApiUsageException;
import org.genesys.catalog.exceptions.NotFoundElement;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.ConcurrencyFailureException;
......@@ -132,7 +133,7 @@ public class ApiExceptionHandler {
@ExceptionHandler({ InvalidApiUsageException.class, ConcurrencyFailureException.class })
@ResponseBody
public ApiError<Exception> handleInvalidApiUsage(final Exception e, final HttpServletRequest request) {
LOG.warn("{} for {} {}", e.getMessage(), request.getMethod(), request.getRequestURL(), e);
LOG.warn("{} for {} {}", e.getMessage(), request.getMethod(), request.getRequestURL()); //, e);
return new ApiError<>(e);
}
......@@ -144,7 +145,7 @@ public class ApiExceptionHandler {
* @return the api error
*/
@ResponseStatus(code = HttpStatus.NOT_FOUND)
@ExceptionHandler(value = { NotFoundElement.class, org.genesys2.server.exception.NotFoundElement.class })
@ExceptionHandler(value = { NoSuchRepositoryFileException.class, NotFoundElement.class, org.genesys2.server.exception.NotFoundElement.class })
@ResponseBody
public ApiError<Exception> handleNotFound(final Exception e, final HttpServletRequest request) {
LOG.warn("Element not found {} {}", request.getMethod(), request.getRequestURL());
......
......@@ -36,6 +36,7 @@ import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import org.apache.commons.lang3.StringUtils;
......@@ -140,6 +141,7 @@ public class AccessionId extends AuditedVersionedModel implements IdUUID {
private Set<String> breederName;
@PrePersist
@PreUpdate
private void prepersist() {
if (uuid == null) {
uuid = UUID.randomUUID();
......@@ -157,26 +159,33 @@ public class AccessionId extends AuditedVersionedModel implements IdUUID {
// Remove empty AccessionAlias
if (this.aliases != null && !this.aliases.isEmpty()) {
List<AccessionAlias> empties = this.aliases.stream().filter(alias -> alias.isEmpty()).collect(Collectors.toList());
List<AccessionAlias> empties = this.aliases.stream().filter(alias -> alias == null || alias.isEmpty()).collect(Collectors.toList());
this.aliases.removeAll(empties);
}
// Remove empty AccessionRemark
if (this.remarks != null && !this.remarks.isEmpty()) {
List<AccessionRemark> empties = this.remarks.stream().filter(remark -> !remark.isEmpty()).collect(Collectors.toList());
List<AccessionRemark> empties = this.remarks.stream().filter(remark -> remark == null || remark.isEmpty()).collect(Collectors.toList());
this.remarks.removeAll(empties);
}
this.breederCode = removeEmptyStrings(this.breederCode);
this.breederName = removeEmptyStrings(this.breederName);
this.duplSite = removeEmptyStrings(this.duplSite);
this.storage = removeEmpty(this.storage);
this.duplSite = removeEmpty(this.duplSite);
this.breederCode = removeEmpty(this.breederCode);
this.breederName = removeEmpty(this.breederName);
}
private static Set<String> removeEmptyStrings(Set<String> set) {
private static <T> Set<T> removeEmpty(Set<T> set) {
if (set == null || set.isEmpty()) {
return set;
}
return set.stream().filter(s -> StringUtils.isNotBlank(s)).collect(Collectors.toSet());
return set.stream().filter(s -> {
if (s != null && s instanceof String) {
return StringUtils.isNotBlank((String) s);
} else {
return s != null;
}
}).collect(Collectors.toSet());
}
@Override
......
......@@ -380,7 +380,7 @@ public class AccessionUploader implements InitializingBean {
if (remarks == null || remarks.isEmpty()) {
existing.clear();
} else {
List<AccessionRemark> toAdd = remarks.stream().filter(rem -> existing.stream().filter(ex -> ex.equalTo(rem)).count() == 0).collect(Collectors.toList());
List<AccessionRemark> toAdd = remarks.stream().filter(rem -> rem != null).filter(rem -> existing.stream().filter(ex -> rem.equalTo(ex)).count() == 0).collect(Collectors.toList());
List<AccessionRemark> toRemove = existing.stream().filter(ex -> remarks.stream().filter(rem -> ex.equalTo(rem)).count() == 0).collect(Collectors.toList());
if (!toRemove.isEmpty() || !toAdd.isEmpty()) {
LOG.info("Removing: {} Adding: {}", toRemove, toAdd);
......
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