Commit b7276698 authored by Matija Obreza's avatar Matija Obreza

Don't persist empty breed, coll, exch, geo, delete if already persisted

parent ec86e60c
......@@ -25,6 +25,7 @@ import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Version;
import org.apache.commons.lang.StringUtils;
import org.genesys2.server.model.BusinessModel;
import org.hibernate.annotations.Type;
......@@ -91,4 +92,14 @@ public class AccessionBreeding extends BusinessModel implements AccessionRelated
this.pedigree = pedigree;
}
public boolean isEmpty() {
if (StringUtils.isNotBlank(pedigree))
return false;
if (StringUtils.isNotBlank(breederCode))
return false;
return true;
}
}
......@@ -25,6 +25,7 @@ import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Version;
import org.apache.commons.lang.StringUtils;
import org.genesys2.server.model.BusinessModel;
import org.hibernate.annotations.Type;
......@@ -158,4 +159,32 @@ public class AccessionCollect extends BusinessModel implements AccessionRelated
this.collSrc = collSrc;
}
public boolean isEmpty() {
if (StringUtils.isNotBlank(collDate))
return false;
if (StringUtils.isNotBlank(collNumb))
return false;
if (StringUtils.isNotBlank(collMissId))
return false;
if (StringUtils.isNotBlank(collName))
return false;
if (StringUtils.isNotBlank(collName))
return false;
if (StringUtils.isNotBlank(collInstAddress))
return false;
if (StringUtils.isNotBlank(collSite))
return false;
if (collSrc != null)
return false;
return true;
}
}
......@@ -24,6 +24,7 @@ import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Version;
import org.apache.commons.lang.StringUtils;
import org.genesys2.server.model.BusinessModel;
/**
......@@ -97,4 +98,15 @@ public class AccessionExchange extends BusinessModel implements AccessionRelated
public void setDonorName(String donorName) {
this.donorName = donorName;
}
public boolean isEmpty() {
if (StringUtils.isNotBlank(accNumbDonor))
return false;
if (StringUtils.isNotBlank(donorName))
return false;
if (StringUtils.isNotBlank(donorInstitute))
return false;
return true;
}
}
......@@ -24,6 +24,7 @@ import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Version;
import org.apache.commons.lang.StringUtils;
import org.genesys2.server.model.BusinessModel;
import org.genesys2.server.model.impl.GeoReferencedEntity;
......@@ -116,7 +117,7 @@ public class AccessionGeo extends BusinessModel implements GeoReferencedEntity,
/**
* Set geodetic method
*
*
* @param method
*/
public void setMethod(String method) {
......@@ -126,4 +127,14 @@ public class AccessionGeo extends BusinessModel implements GeoReferencedEntity,
public String getMethod() {
return method;
}
public boolean isEmpty() {
if (StringUtils.isNotBlank(datum))
return false;
if (StringUtils.isNotBlank(method))
return false;
if (this.latitude != null || this.longitude != null || this.elevation != null || this.uncertainty != null)
return false;
return true;
}
}
......@@ -192,4 +192,12 @@ public interface GenesysService {
AccessionDetails getAccessionDetails(long accessionId);
List<AccessionCollect> removeCollecting(List<AccessionCollect> toRemove);
List<AccessionGeo> removeGeo(List<AccessionGeo> toRemove);
List<AccessionExchange> removeExchange(List<AccessionExchange> toRemove);
List<AccessionBreeding> removeBreeding(List<AccessionBreeding> toRemove);
}
......@@ -91,9 +91,13 @@ public class BatchRESTServiceImpl implements BatchRESTService {
final List<Accession> toSave = new ArrayList<Accession>();
final List<AccessionCollect> toSaveColl = new ArrayList<AccessionCollect>();
final List<AccessionCollect> toRemoveColl = new ArrayList<AccessionCollect>();
final List<AccessionGeo> toSaveGeo = new ArrayList<AccessionGeo>();
final List<AccessionGeo> toRemoveGeo = new ArrayList<AccessionGeo>();
final List<AccessionBreeding> toSaveBreed = new ArrayList<AccessionBreeding>();
final List<AccessionBreeding> toRemoveBreed = new ArrayList<AccessionBreeding>();
final List<AccessionExchange> toSaveExch = new ArrayList<AccessionExchange>();
final List<AccessionExchange> toRemoveExch = new ArrayList<AccessionExchange>();
final List<AccessionRemark> toSaveRemarks = new ArrayList<AccessionRemark>();
final List<AccessionRemark> toRemoveRemarks = new ArrayList<AccessionRemark>();
......@@ -277,7 +281,11 @@ public class BatchRESTServiceImpl implements BatchRESTService {
accnColl.setCollMissId(StringUtils.defaultIfBlank(value.textValue(), null));
}
toSaveColl.add(accnColl);
if (!accnColl.isEmpty()) {
toSaveColl.add(accnColl);
} else if (accnColl.getId() != null) {
toRemoveColl.add(accnColl);
}
}
if (accnJson.has("geo")) {
......@@ -323,7 +331,11 @@ public class BatchRESTServiceImpl implements BatchRESTService {
if (value != null) {
accnGeo.setMethod(StringUtils.defaultIfBlank(value.textValue(), null));
}
toSaveGeo.add(accnGeo);
if (!accnGeo.isEmpty()) {
toSaveGeo.add(accnGeo);
} else if (accnGeo.getId() != null) {
toRemoveGeo.add(accnGeo);
}
}
if (accnJson.has("bredCode") || accnJson.has("ancest")) {
......@@ -340,7 +352,11 @@ public class BatchRESTServiceImpl implements BatchRESTService {
if (value != null) {
accnBred.setPedigree(StringUtils.defaultIfBlank(value.textValue(), null));
}
toSaveBreed.add(accnBred);
if (!accnBred.isEmpty()) {
toSaveBreed.add(accnBred);
} else if (accnBred.getId() != null) {
toRemoveBreed.add(accnBred);
}
}
if (accnJson.has("donorCode") || accnJson.has("donorNumb") || accnJson.has("donorName")) {
......@@ -371,10 +387,15 @@ public class BatchRESTServiceImpl implements BatchRESTService {
if (accnExch.getAccNumbDonor() != null) {
donorNumb.add(donorNumbStr + ":" + accnExch.getAccNumbDonor());
donorNumbs.put(accession, donorNumb);
toSaveExch.add(accnExch);
} else {
donorNumbs.put(accession, null);
}
if (!accnExch.isEmpty()) {
toSaveExch.add(accnExch);
} else if (accnExch.getId() != null) {
toRemoveExch.add(accnExch);
}
}
if (updated) {
......@@ -390,18 +411,30 @@ public class BatchRESTServiceImpl implements BatchRESTService {
if (toSaveColl.size() > 0) {
genesysService.saveCollecting(toSaveColl);
}
if (toRemoveColl.size() > 0) {
genesysService.removeCollecting(toRemoveColl);
}
if (toSaveGeo.size() > 0) {
genesysService.saveGeo(toSaveGeo);
}
if (toRemoveGeo.size() > 0) {
genesysService.removeGeo(toRemoveGeo);
}
if (toSaveBreed.size() > 0) {
genesysService.saveBreeding(toSaveBreed);
}
if (toRemoveBreed.size() > 0) {
genesysService.removeBreeding(toRemoveBreed);
}
if (toSaveExch.size() > 0) {
genesysService.saveExchange(toSaveExch);
}
if (toRemoveExch.size() > 0) {
genesysService.removeExchange(toRemoveExch);
}
if (toSaveRemarks.size() > 0) {
genesysService.saveRemarks(toSaveRemarks);
......@@ -456,6 +489,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
}
as.removeAll(toRemove);
accession.setStorage(listToString(as));
updated = true;
}
......@@ -655,23 +689,42 @@ public class BatchRESTServiceImpl implements BatchRESTService {
return currentValue;
}
public static <T> String listToString(List<T> list) {
if (list == null || list.isEmpty()) {
return null;
}
final StringBuffer mcpdArr = new StringBuffer(20);
for (final T st : list) {
if (st != null && !StringUtils.isBlank(st.toString())) {
if (mcpdArr.length() > 0) {
mcpdArr.append(";");
}
mcpdArr.append(st.toString());
}
}
return StringUtils.defaultIfBlank(mcpdArr.toString(), null);
}
private String arrayToString(ArrayNode arr) {
if (arr == null || arr.isNull()) {
return null;
}
final StringBuffer storageArr = new StringBuffer(20);
final StringBuffer mcpdArr = new StringBuffer(20);
for (final JsonNode st : arr) {
if (st != null && !st.isNull()) {
if (storageArr.length() > 0) {
storageArr.append(";");
if (mcpdArr.length() > 0) {
mcpdArr.append(";");
}
storageArr.append(st.asText());
mcpdArr.append(st.asText());
}
}
return StringUtils.defaultIfBlank(storageArr.toString(), null);
return StringUtils.defaultIfBlank(mcpdArr.toString(), null);
}
/**
......
......@@ -668,6 +668,13 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
return all;
}
@Override
@Transactional(readOnly = false)
public List<AccessionCollect> removeCollecting(List<AccessionCollect> all) {
accessionCollectRepository.delete(all);
return all;
}
@Override
@Transactional(readOnly = false)
public List<AccessionGeo> saveGeo(List<AccessionGeo> all) {
......@@ -675,6 +682,13 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
return all;
}
@Override
@Transactional(readOnly = false)
public List<AccessionGeo> removeGeo(List<AccessionGeo> all) {
accessionGeoRepository.delete(all);
return all;
}
@Override
@Transactional(readOnly = false)
public List<AccessionBreeding> saveBreeding(List<AccessionBreeding> all) {
......@@ -682,6 +696,13 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
return all;
}
@Override
@Transactional(readOnly = false)
public List<AccessionBreeding> removeBreeding(List<AccessionBreeding> all) {
accessionBreedingRepository.delete(all);
return all;
}
@Override
@Transactional(readOnly = false)
public List<AccessionExchange> saveExchange(List<AccessionExchange> all) {
......@@ -689,6 +710,13 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
return all;
}
@Override
@Transactional(readOnly = false)
public List<AccessionExchange> removeExchange(List<AccessionExchange> all) {
accessionExchangeRepository.delete(all);
return all;
}
@Override
@Transactional(readOnly = false)
public List<AccessionRemark> saveRemarks(List<AccessionRemark> toSaveRemarks) {
......
package org.genesys2.server.service.impl;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class ListToStringTest {
@Test
public void testLongListToString() {
List<Long> list = null;
assertTrue(null == BatchRESTServiceImpl.listToString(list));
list = new ArrayList<Long>();
assertTrue(null == BatchRESTServiceImpl.listToString(list));
list.add(1l);
assertTrue("1".equals(BatchRESTServiceImpl.listToString(list)));
list.add(2l);
list.add(3l);
assertTrue("1;2;3".equals(BatchRESTServiceImpl.listToString(list)));
list.add(3l);
assertTrue("1;2;3;3".equals(BatchRESTServiceImpl.listToString(list)));
}
@Test
public void testIntListToString() {
List<Integer> list = null;
assertTrue(null == BatchRESTServiceImpl.listToString(list));
list = new ArrayList<Integer>();
assertTrue(null == BatchRESTServiceImpl.listToString(list));
list.add(1);
assertTrue("1".equals(BatchRESTServiceImpl.listToString(list)));
list.add(2);
list.add(3);
assertTrue("1;2;3".equals(BatchRESTServiceImpl.listToString(list)));
list.add(3);
assertTrue("1;2;3;3".equals(BatchRESTServiceImpl.listToString(list)));
}
@Test
public void testStringListToString() {
List<String> list = null;
assertTrue(null == BatchRESTServiceImpl.listToString(list));
list = new ArrayList<String>();
assertTrue(null == BatchRESTServiceImpl.listToString(list));
list.add("1");
assertTrue("1".equals(BatchRESTServiceImpl.listToString(list)));
list.add("2");
list.add("3");
assertTrue("1;2;3".equals(BatchRESTServiceImpl.listToString(list)));
list.add("3");
assertTrue("1;2;3;3".equals(BatchRESTServiceImpl.listToString(list)));
}
}
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