Commit 73caf02a authored by Matija Obreza's avatar Matija Obreza

Remove blank elements before saving accessions

- Filter blank BREDCODE, BREDNAME, DUPLSITE
- Filter out empty AccessionRemark, AccessionAlias
parent 1d6580ea
......@@ -166,6 +166,10 @@ public class AccessionAlias extends BasicModel implements AccessionRelated, Self
this.usedBy = usedBy;
}
public boolean isEmpty() {
return StringUtils.isBlank(name);
}
@Override
public String toString() {
return "name=" + name + " usedBy=" + usedBy + " type=" + aliasType;
......
......@@ -19,6 +19,7 @@ package org.genesys2.server.model.genesys;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
......@@ -37,6 +38,7 @@ import javax.persistence.OrderBy;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import org.elasticsearch.common.lang3.StringUtils;
import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.model.AuditedVersionedModel;
import org.genesys.blocks.model.IdUUID;
......@@ -136,6 +138,29 @@ public class AccessionId extends AuditedVersionedModel implements IdUUID {
if (this.coll != null && this.coll.isEmpty()) {
this.coll = null;
}
// Remove empty AccessionAlias
if (this.aliases != null && !this.aliases.isEmpty()) {
List<AccessionAlias> empties = this.aliases.stream().filter(alias -> 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());
this.remarks.removeAll(empties);
}
this.breederCode = removeEmptyStrings(this.breederCode);
this.breederName = removeEmptyStrings(this.breederName);
this.duplSite = removeEmptyStrings(this.duplSite);
}
private static Set<String> removeEmptyStrings(Set<String> set) {
if (set == null || set.isEmpty()) {
return set;
}
return set.stream().filter(s -> StringUtils.isNotBlank(s)).collect(Collectors.toSet());
}
@Override
......
......@@ -138,4 +138,8 @@ public class AccessionRemark extends BasicModel implements AccessionRelated, Sel
public String toString() {
return "" + this.fieldName + ":" + this.remark;
}
public boolean isEmpty() {
return StringUtils.isBlank(this.remark);
}
}
......@@ -267,20 +267,22 @@ public class AccessionUploader implements InitializingBean {
final List<AccessionAlias> existing = accession.getAliases() != null ? accession.getAliases() : new ArrayList<>();
AliasType aliasType = AccessionAlias.AliasType.valueOf(fieldName.toUpperCase());
List<AccessionAlias> names = new ArrayList<>();
final List<AccessionAlias> newaliases = new ArrayList<>();
if (jsonNode.isArray()) {
ArrayNode ns = (ArrayNode) jsonNode;
ns.forEach(n -> names.add(new AccessionAlias(aliasType, n.textValue())));
ns.forEach(n -> newaliases.add(new AccessionAlias(aliasType, n.textValue())));
} else if (jsonNode.isTextual()) {
names.add(new AccessionAlias(aliasType, jsonNode.textValue()));
newaliases.add(new AccessionAlias(aliasType, jsonNode.textValue()));
} else if (jsonNode.isNull()) {
// NOOP
} else {
throw new InvalidApiUsageException("Don't know what to do with " + fieldName + "=" + jsonNode);
}
List<AccessionAlias> names = newaliases.stream().filter(alias -> ! alias.isEmpty()).collect(Collectors.toList());
List<AccessionAlias> toAdd = names.stream().filter(name -> existing.stream().filter(ex -> ex.equalTo(name)).count() == 0).collect(Collectors.toList());
List<AccessionAlias> toRemove = existing.stream().filter(alias -> alias.getAliasType() == aliasType).filter(alias -> names.stream().filter(name -> alias.equalTo(name))
.count() == 0).collect(Collectors.toList());
......
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