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
...@@ -165,6 +165,10 @@ public class AccessionAlias extends BasicModel implements AccessionRelated, Self ...@@ -165,6 +165,10 @@ public class AccessionAlias extends BasicModel implements AccessionRelated, Self
public void setUsedBy(final String usedBy) { public void setUsedBy(final String usedBy) {
this.usedBy = usedBy; this.usedBy = usedBy;
} }
public boolean isEmpty() {
return StringUtils.isBlank(name);
}
@Override @Override
public String toString() { public String toString() {
......
...@@ -19,6 +19,7 @@ package org.genesys2.server.model.genesys; ...@@ -19,6 +19,7 @@ package org.genesys2.server.model.genesys;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.CollectionTable; import javax.persistence.CollectionTable;
...@@ -37,6 +38,7 @@ import javax.persistence.OrderBy; ...@@ -37,6 +38,7 @@ import javax.persistence.OrderBy;
import javax.persistence.PrePersist; import javax.persistence.PrePersist;
import javax.persistence.Table; import javax.persistence.Table;
import org.elasticsearch.common.lang3.StringUtils;
import org.genesys.blocks.auditlog.annotations.Audited; import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.model.AuditedVersionedModel; import org.genesys.blocks.model.AuditedVersionedModel;
import org.genesys.blocks.model.IdUUID; import org.genesys.blocks.model.IdUUID;
...@@ -136,6 +138,29 @@ public class AccessionId extends AuditedVersionedModel implements IdUUID { ...@@ -136,6 +138,29 @@ public class AccessionId extends AuditedVersionedModel implements IdUUID {
if (this.coll != null && this.coll.isEmpty()) { if (this.coll != null && this.coll.isEmpty()) {
this.coll = null; 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 @Override
......
...@@ -138,4 +138,8 @@ public class AccessionRemark extends BasicModel implements AccessionRelated, Sel ...@@ -138,4 +138,8 @@ public class AccessionRemark extends BasicModel implements AccessionRelated, Sel
public String toString() { public String toString() {
return "" + this.fieldName + ":" + this.remark; return "" + this.fieldName + ":" + this.remark;
} }
public boolean isEmpty() {
return StringUtils.isBlank(this.remark);
}
} }
...@@ -267,19 +267,21 @@ public class AccessionUploader implements InitializingBean { ...@@ -267,19 +267,21 @@ public class AccessionUploader implements InitializingBean {
final List<AccessionAlias> existing = accession.getAliases() != null ? accession.getAliases() : new ArrayList<>(); final List<AccessionAlias> existing = accession.getAliases() != null ? accession.getAliases() : new ArrayList<>();
AliasType aliasType = AccessionAlias.AliasType.valueOf(fieldName.toUpperCase()); AliasType aliasType = AccessionAlias.AliasType.valueOf(fieldName.toUpperCase());
List<AccessionAlias> names = new ArrayList<>(); final List<AccessionAlias> newaliases = new ArrayList<>();
if (jsonNode.isArray()) { if (jsonNode.isArray()) {
ArrayNode ns = (ArrayNode) jsonNode; 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()) { } else if (jsonNode.isTextual()) {
names.add(new AccessionAlias(aliasType, jsonNode.textValue())); newaliases.add(new AccessionAlias(aliasType, jsonNode.textValue()));
} else if (jsonNode.isNull()) { } else if (jsonNode.isNull()) {
// NOOP // NOOP
} else { } else {
throw new InvalidApiUsageException("Don't know what to do with " + fieldName + "=" + jsonNode); 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> 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)) List<AccessionAlias> toRemove = existing.stream().filter(alias -> alias.getAliasType() == aliasType).filter(alias -> names.stream().filter(name -> alias.equalTo(name))
......
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