Commit 9db3f803 authored by Matija Obreza's avatar Matija Obreza

Remove Dataset2, cleanup

parent 6df3434c
......@@ -76,46 +76,4 @@ public abstract class AuditedModel extends BusinessModel {
this.lastModifiedDate = lastModifiedDate;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((createdBy == null) ? 0 : createdBy.hashCode());
result = prime * result + ((createdDate == null) ? 0 : createdDate.hashCode());
result = prime * result + ((lastModifiedBy == null) ? 0 : lastModifiedBy.hashCode());
result = prime * result + ((lastModifiedDate == null) ? 0 : lastModifiedDate.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
AuditedModel other = (AuditedModel) obj;
if (createdBy == null) {
if (other.createdBy != null)
return false;
} else if (!createdBy.equals(other.createdBy))
return false;
if (createdDate == null) {
if (other.createdDate != null)
return false;
} else if (!createdDate.equals(other.createdDate))
return false;
if (lastModifiedBy == null) {
if (other.lastModifiedBy != null)
return false;
} else if (!lastModifiedBy.equals(other.lastModifiedBy))
return false;
if (lastModifiedDate == null) {
if (other.lastModifiedDate != null)
return false;
} else if (!lastModifiedDate.equals(other.lastModifiedDate))
return false;
return true;
}
}
......@@ -44,28 +44,4 @@ public abstract class BusinessModel implements EntityId, HibernateModel {
this.id = id;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BusinessModel other = (BusinessModel) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
......@@ -33,27 +33,4 @@ public abstract class VersionedAuditedModel extends AuditedModel {
public void setVersion(long version) {
this.version = version;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + (int) (version ^ (version >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
VersionedAuditedModel other = (VersionedAuditedModel) obj;
if (version != other.version)
return false;
return true;
}
}
/**
* Copyright 2015 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.model.impl;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.genesys2.server.model.EntityId;
import org.genesys2.server.model.genesys.Accession;
/**
* A Descriptor represents a dimension of a sparse vector.
*
* @author mobreza
*/
@Entity
@Table(name = "datasetval", indexes = { @Index(name = "IX_datasetrow_dataset", unique = false, columnList = "datasetId, descriptorId, accessionId"),
@Index(name = "IX_datasetrow_accn", unique = false, columnList = "accessionId, descriptorId, datasetId") })
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.INTEGER, name = "typ")
public abstract class DatasetRow<T> implements EntityId {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "id", unique = true, nullable = false, length = 20)
protected Long id;
@ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "datasetId")
private Dataset dataset;
@ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "descriptorId")
private Descriptor descriptor;
@ManyToOne(cascade = {}, optional = true)
@JoinColumn(name = "accessionId")
private Accession accession;
@Override
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public Dataset getDataset() {
return dataset;
}
public void setDataset(Dataset dataset) {
this.dataset = dataset;
}
public Descriptor getDescriptor() {
return descriptor;
}
public void setDescriptor(Descriptor descriptor) {
this.descriptor = descriptor;
}
public Accession getAccession() {
return accession;
}
public void setAccession(Accession accession) {
this.accession = accession;
}
public abstract T getVal();
public abstract void setVal(T val);
}
/**
* Copyright 2015 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.model.impl;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
* A Descriptor represents a dimension of a sparse vector.
*
* @author mobreza
*/
@Entity
//@Table(name = "datasetdbl")
@DiscriminatorValue("2")
public class TraitDblValue extends DatasetRow<Double> {
@Column(name="vald")
private Double val;
@Override
public Double getVal() {
return val;
}
@Override
public void setVal(Double val) {
this.val = val;
}
}
/**
* Copyright 2015 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.model.impl;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
/**
* A Descriptor represents a dimension of a sparse vector.
*
* @author mobreza
*/
@Entity
//@Table(name = "datasetint", indexes={ @Index(name="IX_datasetint_val", unique=false, columnList="descriptorId, vali") })
@DiscriminatorValue("1")
public class TraitIntValue extends DatasetRow<Integer> {
@Column(name="vali")
private Integer val;
@Override
public Integer getVal() {
return val;
}
@Override
public void setVal(Integer val) {
this.val=val;
}
}
/**
* Copyright 2015 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.model.impl;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
* A Descriptor represents a dimension of a sparse vector.
*
* @author mobreza
*/
@Entity
//@Table(name = "datasetstr")
@DiscriminatorValue("3")
public class TraitStrValue extends DatasetRow<String> {
@Column(name="vals")
private String val;
@Override
public String getVal() {
return val;
}
@Override
public void setVal(String val) {
this.val = val;
}
}
/**
* Copyright 2015 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 java.util.List;
import java.util.Set;
import org.genesys2.server.model.impl.Dataset;
import org.genesys2.server.model.impl.DatasetRow;
import org.genesys2.server.model.impl.Descriptor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
public interface DatasetRowRepository extends JpaRepository<DatasetRow<?>, Long> {
Page<DatasetRow<?>> findByDataset(Dataset ds, Pageable page);
@Query("select dr from DatasetRow dr where dr.dataset=?1 and dr.descriptor=?2 and dr.accession.id in ( ?3 )")
List<DatasetRow<?>> list(Dataset dataset, Descriptor descriptor, Set<Long> ids);
@Query("select dr from DatasetRow dr where dr.dataset=?1 and dr.accession.id=?2")
List<DatasetRow<?>> list(Dataset dataset, long accessionId);
}
/**
* Copyright 2015 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.service;
import java.nio.MappedByteBuffer;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.genesys2.server.model.impl.Dataset;
import org.genesys2.server.model.impl.DatasetRow;
import org.genesys2.server.model.impl.Descriptor;
import org.genesys2.server.model.json.WorldclimJson;
public interface Dataset2Service {
Dataset getDataSet(UUID uuid);
Dataset saveDataset(Dataset dataset);
Descriptor getDescriptor(Dataset dataset, String variableName);
Descriptor saveDescriptor(Descriptor descriptor);
List<DatasetRow<?>> getForAccessionIds(Dataset dataset, Descriptor descriptor, Set<Long> ids);
void saveData(List<DatasetRow<?>> toSave);
void worldclimUpdate(Dataset dataset, Descriptor descriptor, Set<Long> ids, MappedByteBuffer buffer, short nullValue, double factor);
List<DatasetRow<?>> getForAccessionId(Dataset dataset, long accessionId);
WorldclimJson jsonForAccessionId(Dataset worldClimDataset, long accessionId);
}
/**
* Copyright 2015 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.service.impl;
import java.nio.MappedByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys.worldclim.WorldClimUtil;
import org.genesys2.server.model.genesys.AccessionGeo;
import org.genesys2.server.model.impl.Dataset;
import org.genesys2.server.model.impl.DatasetRow;
import org.genesys2.server.model.impl.Descriptor;
import org.genesys2.server.model.impl.TraitDblValue;
import org.genesys2.server.model.impl.TraitIntValue;
import org.genesys2.server.model.json.WorldclimJson;
import org.genesys2.server.persistence.domain.AccessionGeoRepository;
import org.genesys2.server.persistence.domain.AccessionRepository;
import org.genesys2.server.persistence.domain.DatasetRepository;
import org.genesys2.server.persistence.domain.DatasetRowRepository;
import org.genesys2.server.persistence.domain.DescriptorRepository;
import org.genesys2.server.service.Dataset2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(readOnly = true)
public class Dataset2ServiceImpl implements Dataset2Service {
private final Log LOG = LogFactory.getLog(getClass());
@Autowired
private DatasetRepository datasetRepository;
@Autowired
private DescriptorRepository descriptorRepository;
@Autowired
private DatasetRowRepository datasetRowRepository;
@Autowired
private AccessionRepository accessionRepository;
@Autowired
private AccessionGeoRepository accessionGeoRepository;
@Override
@Transactional
public Dataset getDataSet(UUID uuid) {
return datasetRepository.findByUuid(uuid);
}
@Override
@Transactional
public Dataset saveDataset(Dataset dataset) {
return datasetRepository.save(dataset);
}
@Override
public Descriptor getDescriptor(Dataset dataset, String variableName) {
return descriptorRepository.findByCode(variableName);
}
@Override
@Transactional
public Descriptor saveDescriptor(Descriptor descriptor) {
return descriptorRepository.save(descriptor);
}
@Override
public List<DatasetRow<?>> getForAccessionIds(Dataset dataset, Descriptor descriptor, Set<Long> ids) {
return datasetRowRepository.list(dataset, descriptor, ids);
}
@Override
@Transactional
public void saveData(List<DatasetRow<?>> toSave) {
datasetRowRepository.save(toSave);
}
/**
* Updates worldclim data for accessions
*
* @param ids
* @param buffer
* @param nullValue
*/
@Override
@Transactional
public void worldclimUpdate(Dataset dataset, Descriptor descriptor, Set<Long> ids, MappedByteBuffer buffer, short nullValue, double factor) {
if (LOG.isDebugEnabled()) {
LOG.debug("Updating " + ids + " accessionIds with " + descriptor.getCode());
LOG.debug("IDs: " + ArrayUtils.toString(ids.toArray(), "!"));
}
List<AccessionGeo> acGeo = accessionGeoRepository.findForAccessions(ids);
List<DatasetRow<?>> existing = getForAccessionIds(dataset, descriptor, ids);
LOG.info("Found " + existing.size() + " existing datasetRows");
for (DatasetRow<?> dr : existing) {
ids.remove(dr.getAccession().getId());
}
List<DatasetRow<?>> all = new ArrayList<DatasetRow<?>>(existing);
List<DatasetRow<?>> toSave = new ArrayList<DatasetRow<?>>();
// New ones
if (LOG.isDebugEnabled()) {
LOG.debug("Generating " + ids.size() + " new datasetRows");
}
for (Long id : ids) {
DatasetRow<?> tv = null;
// TODO FIXME Figure this out!
if (factor < 1.0) {
tv = new TraitDblValue();
} else if (factor == 1.0) {
tv = new TraitIntValue();
}
tv.setAccession(accessionRepository.findOne(id));
tv.setDataset(dataset);
tv.setDescriptor(descriptor);
all.add(tv);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Updating " + all.size() + " accessions with worldclim.org data");
}
for (final DatasetRow<?> dr : all) {
AccessionGeo geo = CollectionUtils.find(acGeo, new Predicate<AccessionGeo>() {
@Override
public boolean evaluate(AccessionGeo ag) {
return ag.getAccession().getId().equals(dr.getAccession().getId());
}
});
if (geo != null) {
Double lat = geo.getLatitude();
Double lon = geo.getLongitude();
if (lat != null && lon != null) {
Integer tileIndex = WorldClimUtil.getTileIndex(5, lon, lat);
if (tileIndex==null || tileIndex * 2 > buffer.capacity()-2) {
LOG.info("OUT OF FILE tile=" + tileIndex + " lat=" + lat + " lon=" + lon + " aid=" + dr.getAccession().getId());
} else {
try {
short val = buffer.getShort(tileIndex * 2);
if (val != nullValue) {
if (LOG.isDebugEnabled()) {
LOG.debug("tile=" + tileIndex + " val=" + val + " lat=" + lat + " lon=" + lon + " aid=" + dr.getAccession().getId());
}
if (factor < 1.0) {