Commit 5732b972 authored by Matija Obreza's avatar Matija Obreza

Persistent PDCI

parent b14bbe89
CREATE TABLE `pdci` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`version` bigint(20) NOT NULL,
`acceName` int(11) NOT NULL,
`acceUrl` int(11) NOT NULL,
`acqDate` int(11) NOT NULL,
`ancest` int(11) NOT NULL,
`bredCode` int(11) NOT NULL,
`collCode` int(11) NOT NULL,
`collDate` int(11) NOT NULL,
`collName` int(11) NOT NULL,
`collNumb` int(11) NOT NULL,
`collSite` int(11) NOT NULL,
`collSrc` int(11) NOT NULL,
`cropName` int(11) NOT NULL,
`donorCode` int(11) NOT NULL,
`donorName` int(11) NOT NULL,
`donorNumb` int(11) NOT NULL,
`duplInstName` int(11) NOT NULL,
`duplSite` int(11) NOT NULL,
`elevation` int(11) NOT NULL,
`genus` int(11) NOT NULL,
`latitude` int(11) NOT NULL,
`longitude` int(11) NOT NULL,
`mlsStat` int(11) NOT NULL,
`origCty` int(11) NOT NULL,
`otherNumb` int(11) NOT NULL,
`sampStat` int(11) NOT NULL,
`score` float DEFAULT NULL,
`scoreHist` float DEFAULT NULL,
`spAuthor` int(11) NOT NULL,
`species` int(11) NOT NULL,
`storage` int(11) NOT NULL,
`subTaxa` int(11) NOT NULL,
`subtAuthor` int(11) NOT NULL,
`accessionId` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UK_c3fnk9quh9lou6hd1v5hpanbw` (`accessionId`),
CONSTRAINT `FK_c3fnk9quh9lou6hd1v5hpanbw` FOREIGN KEY (`accessionId`) REFERENCES `acce` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
alter table accession add column acceurl varchar(300);
alter table accessionhistoric add column acceurl varchar(300);
alter table svalbard add column acceUrl varchar(300), add column unitId bigint(20);
......@@ -20,8 +20,10 @@ import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToOne;
import javax.persistence.PrePersist;
import javax.persistence.Table;
......@@ -40,6 +42,9 @@ public class AccessionId extends VersionedAuditedModel implements IdUUID {
@Column(columnDefinition = "binary(16)")
protected UUID uuid;
@OneToOne(cascade = {}, fetch = FetchType.LAZY, mappedBy = "accession", optional = true, orphanRemoval = true)
private PDCI pdci;
@PrePersist
private void prepersist() {
if (uuid == null) {
......@@ -55,4 +60,12 @@ public class AccessionId extends VersionedAuditedModel implements IdUUID {
public void setUuid(UUID uuid) {
this.uuid = uuid;
}
public PDCI getPdci() {
return pdci;
}
public void setPdci(PDCI pdci) {
this.pdci = pdci;
}
}
......@@ -16,20 +16,33 @@
package org.genesys2.server.model.genesys;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.Transient;
public class PDCI implements Serializable {
import org.genesys2.server.model.VersionedModel;
@Entity
@Table(name = "pdci")
public class PDCI extends VersionedModel {
public static final String[] independentItems = { "genus", "species", "spAuthor", "subTaxa", "subtAuthor", "cropName", "acqDate", "sampStat", "donorCode",
"donorNumb", "otherNumb", "duplSite", "storage", "donorName", "duplInstName", "acceUrl", "mlsStat" };
public static final String[] dependentItems = { "origCty", "collSite", "latitude", "longitude", "elevation", "collDate", "bredCode", "ancest", "collSrc",
"acceName", "collNumb", "collCode", "collName" };
@OneToOne(cascade = {}, fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "accessionId")
private AccessionId accession;
private Float score = null;
private Float scoreHist = null;
private int acqDate = 0;
private int sampStat = 0;
......@@ -66,6 +79,15 @@ public class PDCI implements Serializable {
@PrePersist
protected void prePersist() {
this.score = calculateScore();
this.scoreHist = (float) (Math.ceil(this.score * 2) / 2);
}
public AccessionId getAccession() {
return accession;
}
public void setAccession(AccessionId accession) {
this.accession = accession;
}
public int getAcqDate() {
......@@ -311,6 +333,17 @@ public class PDCI implements Serializable {
this.score = score;
}
public Float getScoreHist() {
if (scoreHist == null) {
this.scoreHist = (float) (Math.ceil(getScore() * 2) / 2);
}
return scoreHist;
}
public void setScoreHist(Float scoreHist) {
this.scoreHist = scoreHist;
}
private Float calculateScore() {
float total = getTotal();
return total / 100f;
......@@ -365,7 +398,7 @@ public class PDCI implements Serializable {
public String[] getIndependentItems() {
return independentItems;
}
public String[] getDependentItems() {
return dependentItems;
}
......@@ -380,4 +413,44 @@ public class PDCI implements Serializable {
}
return descriptors;
}
/**
* Reset all scores to 0.
*/
public void reset() {
score = null;
scoreHist = null;
acqDate = 0;
sampStat = 0;
donorCode = 0;
genus = 0;
species = 0;
spAuthor = 0;
subTaxa = 0;
subtAuthor = 0;
donorNumb = 0;
otherNumb = 0;
duplSite = 0;
storage = 0;
donorName = 0;
duplInstName = 0;
acceUrl = 0;
mlsStat = 0;
origCty = 0;
latitude = 0;
longitude = 0;
collSite = 0;
elevation = 0;
collDate = 0;
collSrc = 0;
collNumb = 0;
collCode = 0;
collName = 0;
ancest = 0;
acceName = 0;
bredCode = 0;
cropName = 0;
}
}
/**
* Copyright 2014 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.persistence.domain;
import org.genesys2.server.model.genesys.PDCI;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
public interface PDCIRepository extends JpaRepository<PDCI, Long> {
@Query("select pdci from PDCI pdci where pdci.accession.id=?1")
PDCI findByAccessionId(Long accessionId);
}
......@@ -39,6 +39,7 @@ import org.genesys2.server.model.genesys.AllAccnames;
import org.genesys2.server.model.genesys.ExperimentTrait;
import org.genesys2.server.model.genesys.Metadata;
import org.genesys2.server.model.genesys.Method;
import org.genesys2.server.model.genesys.PDCI;
import org.genesys2.server.model.genesys.SvalbardData;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.AccessionIdentifier3;
......@@ -227,4 +228,8 @@ public interface GenesysService {
AccessionHistoric getHistoricAccession(UUID uuid);
PDCI updatePDCI(Long accessionId);
PDCI loadPDCI(Long accessionId);
}
......@@ -60,6 +60,7 @@ import org.genesys2.server.model.genesys.ExperimentAccessionTrait;
import org.genesys2.server.model.genesys.ExperimentTrait;
import org.genesys2.server.model.genesys.Metadata;
import org.genesys2.server.model.genesys.Method;
import org.genesys2.server.model.genesys.PDCI;
import org.genesys2.server.model.genesys.SelfCopy;
import org.genesys2.server.model.genesys.SvalbardData;
import org.genesys2.server.model.genesys.Taxonomy2;
......@@ -86,6 +87,7 @@ import org.genesys2.server.persistence.domain.GenesysLowlevelRepository;
import org.genesys2.server.persistence.domain.MetadataMethodRepository;
import org.genesys2.server.persistence.domain.MetadataRepository;
import org.genesys2.server.persistence.domain.MethodRepository;
import org.genesys2.server.persistence.domain.PDCIRepository;
import org.genesys2.server.persistence.domain.SvalbardRepository;
import org.genesys2.server.persistence.domain.TraitValueRepository;
import org.genesys2.server.security.AuthUserDetails;
......@@ -179,6 +181,12 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
@Autowired
private CropService cropService;
@Autowired(required = false)
private PDCICalculator pdciCalculator;
@Autowired
private PDCIRepository repoPdci;
@Override
public long countAll() {
return accessionRepository.count();
......@@ -1541,4 +1549,24 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
}
return ah;
}
@Override
public PDCI loadPDCI(Long accessionId) {
return repoPdci.findByAccessionId(accessionId);
}
@Override
@Transactional
public PDCI updatePDCI(Long accessionId) {
if (pdciCalculator == null) {
return null;
}
PDCI pdci = repoPdci.findByAccessionId(accessionId);
if (pdci == null) {
pdci = new PDCI();
pdci.setAccession(accessionIdRepository.findOne(accessionId));
}
return repoPdci.save(pdciCalculator.updatePdci(pdci, accessionId));
}
}
......@@ -38,8 +38,8 @@ import org.springframework.stereotype.Component;
*
* <p>
* Theo van Hintum, Frank Menting and Elisabeth van Strien (2011). <b>Quality
* indicators for passport data in ex situ genebanks.</b> Plant Genetic Resources,
* 9, pp 478-485.
* indicators for passport data in ex situ genebanks.</b> Plant Genetic
* Resources, 9, pp 478-485.
*
* doi:10.1017/S1479262111000682
* </p>
......@@ -55,19 +55,23 @@ public class PDCICalculator implements InitializingBean {
public void afterPropertiesSet() throws Exception {
}
public PDCI getPdci(long accessionId) {
return calculatePDCI(accessionId);
public PDCI makePDCI(long accessionId) {
return calculatePDCI(new PDCI(), accessionId);
}
public PDCI updatePdci(PDCI pdci, long accessionId) {
return calculatePDCI(pdci, accessionId);
}
private PDCI calculatePDCI(PDCI pdci, long accessionId) {
pdci.reset();
private PDCI calculatePDCI(long accessionId) {
AccessionDetails accessionDetails = genesysService.getAccessionDetails(accessionId);
if (accessionDetails == null) {
LOG.warn("No such accession " + accessionId);
return null;
}
PDCI pdci = new PDCI();
independentDescriptors(pdci, accessionDetails);
switch (accessionDetails.getSampStat() == null ? -1 : accessionDetails.getSampStat() / 100) {
......@@ -139,9 +143,8 @@ public class PDCICalculator implements InitializingBean {
}
}
if (accessionDetails.getUuid() != null) {
// Missing ACCEURL in Genesys
pdci.setAcceUrl(40);
if (accessionDetails.getAcceUrl() != null) {
// pdci.setAcceUrl(40);
}
if (accessionDetails.getMlsStatus() != null) {
......@@ -183,9 +186,13 @@ public class PDCICalculator implements InitializingBean {
if (geo != null) {
if (geo.getLatitude() != null && geo.getLongitude() != null) {
pdci.setLatitude(60);
pdci.setLongitude(60);
hasLatLng = true;
if (geo.getLatitude() < -90 || geo.getLatitude() > 90 || geo.getLongitude() < -180 || geo.getLongitude() > 180) {
// Ignore coordinates
} else {
pdci.setLatitude(60);
pdci.setLongitude(60);
hasLatLng = true;
}
}
if (geo.getElevation() != null) {
......
......@@ -94,6 +94,12 @@ public class ElasticUpdater {
* @param ids
*/
public void update(Class<?> clazz, Long id) {
// If Accession, update PDCI
if (Accession.class.equals(clazz) && id !=null) {
genesysService.updatePDCI(id);
}
ElasticNode node = new ElasticNode(clazz, id);
elasticRemoveQueue.remove(node);
......
......@@ -22,6 +22,7 @@ import java.util.UUID;
import org.genesys2.server.model.dataset.DS;
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.FaoInstitute;
import org.genesys2.server.service.CropService;
......@@ -103,7 +104,21 @@ public class AccessionController extends BaseController {
// PDCI
if (pdciCalculator != null) {
try {
model.addAttribute("pdci", pdciCalculator.getPdci(accession.getId()));
PDCI pdci = pdciCalculator.makePDCI(accession.getId());
PDCI existingPdci = genesysService.loadPDCI(accession.getId());
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());
}
model.addAttribute("pdci", pdci);
} 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