Commit 459a51bf authored by Matija Obreza's avatar Matija Obreza

Use MCPD#cropName instead of taxonomy to determine crop of accession

parent 4f9ad5f5
CREATE TABLE `cropname` (
`cropId` bigint(20) NOT NULL,
`otherName` varchar(255) NOT NULL,
UNIQUE KEY `UK_o6svkv9o98t67jnnxv7i0d4a7` (`otherName`),
KEY `FK_tb67bwitjd2dlmmfbxqsfcjmq` (`cropId`),
CONSTRAINT `FK_tb67bwitjd2dlmmfbxqsfcjmq` FOREIGN KEY (`cropId`) REFERENCES `crop` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
alter table accession add `cropName` varchar(50) DEFAULT NULL,
add `cropId` bigint(20) DEFAULT NULL,
add CONSTRAINT `FK_826wi40aq5ucsmeki61dnr9pt` FOREIGN KEY (`cropId`) REFERENCES `crop` (`id`);
alter table accessionhistoric add `cropName` varchar(50) DEFAULT NULL,
add `cropId` bigint(20) DEFAULT NULL,
add CONSTRAINT `FK_tb12h2n6q2ck6wvpp2l73up2a` FOREIGN KEY (`cropId`) REFERENCES `crop` (`id`);
update accession a inner join croptaxonomy ct on ct.taxonomyId=a.taxonomyId2 inner join crop c on c.id=ct.cropId set a.cropName=c.shortName, a.cropId=c.id;
update accessionhistoric a inner join croptaxonomy ct on ct.taxonomyId=a.taxonomyId2 inner join crop c on c.id=ct.cropId set a.cropName=c.shortName, a.cropId=c.id;
......@@ -153,6 +153,18 @@ public class AccessionDetails {
}
}
/**
* Single crop
*
* @param shortName
*/
public void crops(String cropName) {
if (StringUtils.isBlank(cropName))
return;
this.crops = new ArrayList<String>();
this.crops.add(cropName);
}
public void crops(List<Crop> crops) {
if (crops == null || crops.isEmpty())
return;
......@@ -473,4 +485,5 @@ public class AccessionDetails {
public void setLists(Set<String> lists) {
this.lists = lists;
}
}
......@@ -26,6 +26,7 @@ import java.util.UUID;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
......@@ -39,6 +40,7 @@ import javax.persistence.Version;
import org.genesys2.server.model.IdUUID;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.util.MCPDUtil;
import org.genesys2.util.NumberUtils;
......@@ -92,6 +94,13 @@ public abstract class AccessionData implements IdUUID, Serializable {
@Column(name = "seqNo", nullable = false)
private float seqNo = 0;
@Column(name = "cropName", nullable = true, length = 50)
private String cropName;
@ManyToOne(cascade = {}, optional = true, fetch = FetchType.LAZY)
@JoinColumn(name = "cropId")
private Crop crop;
// @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
@ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "taxonomyId2")
......@@ -153,7 +162,7 @@ public abstract class AccessionData implements IdUUID, Serializable {
this.instituteCode = getInstitute().getCode();
this.storage = MCPDUtil.toMcpdArray(getStoRage());
// Update the ACCENUMB number
this.seqNo = NumberUtils.numericValue(this.accessionName);
}
......@@ -388,4 +397,20 @@ public abstract class AccessionData implements IdUUID, Serializable {
public void setSeqNo(float acceNumbNumb) {
this.seqNo = acceNumbNumb;
}
public String getCropName() {
return cropName;
}
public void setCropName(String cropName) {
this.cropName = cropName;
}
public Crop getCrop() {
return crop;
}
public void setCrop(Crop crop) {
this.crop = crop;
}
}
......@@ -24,12 +24,18 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
......@@ -56,6 +62,12 @@ public class Crop extends GlobalVersionedAuditedModel implements AclAwareModel {
@Column(nullable = false, length = 50, unique = true)
private String shortName;
@Column(name = "otherName", nullable = false)
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "cropname", joinColumns = @JoinColumn(name = "cropId", referencedColumnName = "id"), uniqueConstraints = { @UniqueConstraint(columnNames = "otherName") })
@OrderBy("otherName")
private List<String> otherNames;
@Column(nullable = false, length = 200)
private String name;
......@@ -219,4 +231,11 @@ public class Crop extends GlobalVersionedAuditedModel implements AclAwareModel {
return buildVernacularMap("description");
}
public List<String> getOtherNames() {
return otherNames;
}
public void setOtherNames(List<String> otherNames) {
this.otherNames = otherNames;
}
}
......@@ -186,8 +186,13 @@ public interface Api1Constants {
*/
public static final String ACCEURL = "acceUrl";
/**
* Gene bank provided MCPD#CROPNAME
*/
public static final String CROPNAME = "cropName";
}
public static interface Institute {
/**
* Corresponds to INSTCODE
......@@ -197,9 +202,9 @@ public interface Api1Constants {
public static final String INSTCODE_COUNTRY = "country.iso3";
}
public static interface Taxonomy {
public static final String SCIENTIFIC_NAME = "sciName";
/**
......
......@@ -19,6 +19,7 @@ package org.genesys2.server.persistence.domain;
import java.util.List;
import org.genesys2.server.model.impl.Crop;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CropRepository extends JpaRepository<Crop, Long> {
......@@ -26,4 +27,7 @@ public interface CropRepository extends JpaRepository<Crop, Long> {
Crop findByShortName(String shortName);
List<Crop> findByShortName(List<String> cropNames);
@Query("select c from Crop c where ?1 member of c.otherNames")
Crop findByOtherNames(String cropName);
}
......@@ -45,6 +45,7 @@ import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.json.Api1Constants;
import org.genesys2.server.persistence.domain.AccessionCustomRepository;
import org.genesys2.server.service.BatchRESTService;
import org.genesys2.server.service.CropService;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
......@@ -78,6 +79,9 @@ public class BatchRESTServiceImpl implements BatchRESTService {
@Autowired
TaxonomyService taxonomyService;
@Autowired
CropService cropService;
@Autowired
OrganizationService organizationService;
......@@ -195,10 +199,10 @@ public class BatchRESTServiceImpl implements BatchRESTService {
throw new RESTApiException("Accession does not belong to instCode=" + institute.getCode() + " acn=" + dataJson);
}
Accession accession = loaded.stream()
.filter(a -> useUniqueAcceNumbs ? (a.getAccessionName().equalsIgnoreCase(dataJson.acceNumb))
: (a.getAccessionName().equalsIgnoreCase(dataJson.acceNumb) && a.getTaxonomy().getGenus().equalsIgnoreCase(dataJson.genus)))
.findFirst().orElse(null);
Accession accession = loaded
.stream()
.filter(a -> useUniqueAcceNumbs ? (a.getAccessionName().equalsIgnoreCase(dataJson.acceNumb)) : (a.getAccessionName().equalsIgnoreCase(
dataJson.acceNumb) && a.getTaxonomy().getGenus().equalsIgnoreCase(dataJson.genus))).findFirst().orElse(null);
boolean updated = false;
......@@ -237,6 +241,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
updated |= updateTaxonomy(accession, accnJson);
}
updated |= updateCrop(accession, accnJson.get(Api1Constants.Accession.CROPNAME));
updated |= updateAcceNumb(accession, accnJson.get(Api1Constants.Accession.ACCENUMB_NEW));
updated |= updateOrgCty(accession, accnJson.get(Api1Constants.Accession.ORIGCTY));
updated |= updateUuid(accession, accnJson.get(Api1Constants.Accession.UUID));
......@@ -520,24 +525,24 @@ public class BatchRESTServiceImpl implements BatchRESTService {
List<Accession> savedData = genesysService.saveAccessions(institute, toSave);
// Iterate savedData to extract UUIDs
upsertResponses.stream().forEach(response -> {
Accession accession = savedData.stream()
.filter(a -> useUniqueAcceNumbs ? (a.getAccessionName()
.equalsIgnoreCase(response.getAcceNumb()))
: (a.getAccessionName().equalsIgnoreCase(response.getAcceNumb())
&& a.getTaxonomy().getGenus().equalsIgnoreCase(response.getGenus())))
.findFirst().orElse(null);
UpsertResult result = response.getResult();
if (accession != null) {
if (result.getUuid() == null) {
result.setAction(UpsertResult.Type.INSERT);
} else {
result.setAction(UpsertResult.Type.UPDATE);
}
result.setUUID(accession.getUuid());
}
});
upsertResponses.stream().forEach(
response -> {
Accession accession = savedData
.stream()
.filter(a -> useUniqueAcceNumbs ? (a.getAccessionName().equalsIgnoreCase(response.getAcceNumb())) : (a.getAccessionName()
.equalsIgnoreCase(response.getAcceNumb()) && a.getTaxonomy().getGenus().equalsIgnoreCase(response.getGenus())))
.findFirst().orElse(null);
UpsertResult result = response.getResult();
if (accession != null) {
if (result.getUuid() == null) {
result.setAction(UpsertResult.Type.INSERT);
} else {
result.setAction(UpsertResult.Type.UPDATE);
}
result.setUUID(accession.getUuid());
}
});
}
if (toSaveColl.size() > 0) {
......@@ -584,6 +589,30 @@ public class BatchRESTServiceImpl implements BatchRESTService {
return upsertResponses;
}
private boolean updateCrop(Accession accession, JsonNode value) throws RESTApiDataTypeException {
if (value != null) {
if (!value.isTextual()) {
throw new RESTApiDataTypeException("cropName must be a String");
}
if (value.isNull()) {
if (!StringUtils.equals(null, accession.getCropName())) {
accession.setCropName(null);
accession.setCrop(null);
return true;
}
}
final String cropName = value.textValue().trim();
if (!StringUtils.equals(cropName, accession.getCropName())) {
accession.setCropName(cropName);
accession.setCrop(cropService.getCrop(cropName));
return true;
}
}
return false;
}
private boolean updateStorage(AccessionData accession, ObjectNode accnJson) throws RESTApiDataTypeException {
boolean updated = false;
......@@ -1043,7 +1072,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#institute, 'DELETE') or hasPermission(#institute, 'MANAGE')")
public List<AccessionOpResponse> deleteAccessions(FaoInstitute institute, List<AccessionHeaderJson> batch) throws RESTApiException {
public List<AccessionOpResponse> deleteAccessions(FaoInstitute institute, List<AccessionHeaderJson> batch) throws RESTApiException {
LOG.info("Batch deleting " + batch.size() + " entries for " + institute);
final List<AccessionOpResponse> upsertResponses = new ArrayList<AccessionOpResponse>();
......@@ -1077,12 +1106,12 @@ public class BatchRESTServiceImpl implements BatchRESTService {
if (accession != null) {
toDelete.add(accession);
upsertResult=new UpsertResult(UpsertResult.Type.DELETE);
upsertResult = new UpsertResult(UpsertResult.Type.DELETE);
upsertResult.setUUID(accession.getUuid());
} else {
upsertResult=new UpsertResult(UpsertResult.Type.NOOP);
upsertResult = new UpsertResult(UpsertResult.Type.NOOP);
}
upsertResponse.setResult(upsertResult);
}
......
......@@ -72,7 +72,12 @@ public class CropServiceImpl implements CropService {
@Override
public Crop getCrop(String shortName) {
return cropRepository.findByShortName(shortName);
Crop crop = cropRepository.findByShortName(shortName);
if (crop == null) {
// Find crop by alias
crop = cropRepository.findByOtherNames(shortName);
}
return crop;
}
@PreAuthorize("hasRole('ADMINISTRATOR')")
......
......@@ -110,17 +110,16 @@ public class DirectMysqlQuery {
innerJoin("accelist", "al", "al.id=ali.listid");
}
if (filters.hasFilter(FilterConstants.CROPS) || filters.hasFilter(FilterConstants.TAXONOMY_GENUS) || filters.hasFilter(FilterConstants.TAXONOMY_SPECIES)
if (filters.hasFilter(FilterConstants.TAXONOMY_GENUS) || filters.hasFilter(FilterConstants.TAXONOMY_SPECIES)
|| filters.hasFilter(FilterConstants.TAXONOMY_SUBTAXA) || filters.hasFilter(FilterConstants.TAXONOMY_SCINAME)) {
innerJoin("taxonomy2", "t", "t.id=a.taxonomyId2");
if (filters.hasFilter(FilterConstants.CROPS)) {
innerJoin("croptaxonomy", "ct", "ct.taxonomyId=t.id");
innerJoin("crop", null, "crop.id=ct.cropId");
}
}
if (filters.hasFilter(FilterConstants.CROPS)) {
innerJoin("crop", null, "crop.id=a.cropId");
}
// if (filters.hasFilter(FilterConstants.ORGCTY_ISO3)) {
// innerJoin("country", "cty", "cty.id=a.orgCtyId");
// }
......
......@@ -363,6 +363,8 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
accession.getStoRage().size();
if (accession.getCountryOfOrigin() != null)
accession.getCountryOfOrigin().getId();
if (accession.getCrop() != null)
accession.getCrop().getId();
}
return accession;
}
......@@ -394,7 +396,11 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
ad.remarks(all.remarks);
// ad.traits(listMethods(accession),
// getAccessionTraitValues(accession));
ad.crops(cropService.getCrops(all.accession.getTaxonomy()));
if (all.accession.getCrop() != null) {
ad.crops(all.accession.getCrop().getShortName());
} else {
ad.crops(cropService.getCrops(all.accession.getTaxonomy()));
}
return ad;
}
......@@ -1105,8 +1111,7 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
zos.putNextEntry(metaEntry);
final BufferedWriter osw = new BufferedWriter(new OutputStreamWriter(zos));
osw.write("<?xml version='1.0' encoding='utf-8'?>\n");
osw.write(
"<archive xmlns=\"http://rs.tdwg.org/dwc/text/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://rs.tdwg.org/dwc/text/ http://rs.tdwg.org/dwc/text/tdwg_dwc_text.xsd\">\n");
osw.write("<archive xmlns=\"http://rs.tdwg.org/dwc/text/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://rs.tdwg.org/dwc/text/ http://rs.tdwg.org/dwc/text/tdwg_dwc_text.xsd\">\n");
osw.write("<core encoding=\"UTF-8\" fieldsTerminatedBy=\",\" linesTerminatedBy=\"\\n\" fieldsEnclosedBy=\"&quot;\" ignoreHeaderLines=\"0\">\n");
osw.write("\t<files><location>core.csv</location></files>\n");
osw.write("\t<id index=\"0\" />\n");
......@@ -1373,8 +1378,7 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
zos.putNextEntry(metaEntry);
final BufferedWriter osw = new BufferedWriter(new OutputStreamWriter(zos));
osw.write("<?xml version='1.0' encoding='utf-8'?>\n");
osw.write(
"<archive xmlns=\"http://rs.tdwg.org/dwc/text/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://rs.tdwg.org/dwc/text/ http://rs.tdwg.org/dwc/text/tdwg_dwc_text.xsd\">\n");
osw.write("<archive xmlns=\"http://rs.tdwg.org/dwc/text/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://rs.tdwg.org/dwc/text/ http://rs.tdwg.org/dwc/text/tdwg_dwc_text.xsd\">\n");
osw.write("<core encoding=\"UTF-8\" fieldsTerminatedBy=\",\" linesTerminatedBy=\"\\n\" fieldsEnclosedBy=\"&quot;\" ignoreHeaderLines=\"0\">\n");
osw.write("\t<files><location>core.csv</location></files>\n");
osw.write("\t<id index=\"0\" />\n");
......@@ -1386,8 +1390,7 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
for (int i = 0; i < metadataMethods.size(); i++) {
final Method method = metadataMethods.get(i);
osw.write(
"<extension encoding=\"UTF-8\" fieldsTerminatedBy=\",\" linesTerminatedBy=\"\\n\" fieldsEnclosedBy=\"&quot;\" ignoreHeaderLines=\"0\">\n");
osw.write("<extension encoding=\"UTF-8\" fieldsTerminatedBy=\",\" linesTerminatedBy=\"\\n\" fieldsEnclosedBy=\"&quot;\" ignoreHeaderLines=\"0\">\n");
osw.write("\t<files><location>");
osw.write(method.getFieldName().toLowerCase());
osw.write(".csv</location></files>\n");
......
......@@ -16,6 +16,7 @@
package org.genesys2.server.servlet.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
......@@ -24,6 +25,7 @@ import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionGeo;
import org.genesys2.server.model.genesys.PDCI;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.CropService;
import org.genesys2.server.service.DSService;
......@@ -95,7 +97,14 @@ public class AccessionController extends BaseController {
model.addAttribute("methods", traitService.listMethods(accession.getAccessionId()));
model.addAttribute("methodValues", genesysService.getAccessionTraitValues(accession.getAccessionId()));
model.addAttribute("crops", cropService.getCrops(accession.getTaxonomy()));
if (accession.getCrop() != null) {
List<Crop> crops = new ArrayList<Crop>();
crops.add(accession.getCrop());
model.addAttribute("crops", crops);
} else {
// TODO Remove
// model.addAttribute("crops", cropService.getCrops(accession.getTaxonomy()));
}
// Worldclim data
if (accessionGeo != null && accessionGeo.getTileIndex() != null) {
......@@ -116,7 +125,7 @@ public class AccessionController extends BaseController {
if (existingPdci == null) {
existingPdci = genesysService.updatePDCI(accession.getId());
}
if (existingPdci != null && pdci.getTotal() != existingPdci.getTotal()) {
_logger.info("Forcing recalculation of PDCI");
genesysService.updatePDCI(accession.getId());
......@@ -124,7 +133,7 @@ public class AccessionController extends BaseController {
model.addAttribute("pdci", pdci);
model.addAttribute("institutePDCI", statisticsService.statisticsPDCI(accession.getInstitute()));
} catch (Throwable e) {
_logger.warn(e.getMessage(), e);
}
......
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