Commit 83b4786b authored by Matija Obreza's avatar Matija Obreza

Merge branch '255-rename-model' into 'master'

Resolve "Rename model"

Closes #255

See merge request genesys-pgr/genesys-server!150
parents b0af34c4 d5cc8be7
......@@ -3,6 +3,7 @@ package org.genesys2.server.model.dataset;
import java.util.List;
import java.util.UUID;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
......@@ -23,12 +24,12 @@ public class DS extends BasicModel {
private UUID uuid;
@OrderBy("index")
@OneToMany(mappedBy = "dataset")
@OneToMany(mappedBy = "dataset", cascade = { CascadeType.ALL })
private List<DSQualifier> qualifiers;
@OrderBy("index")
@OneToMany(mappedBy = "dataset")
private List<DSDescriptor> descriptors;
@OneToMany(mappedBy = "dataset", cascade = { CascadeType.ALL })
private List<DSColumn> columns;
/**
* Generate UUID if missing
......@@ -56,11 +57,11 @@ public class DS extends BasicModel {
this.qualifiers = qualifiers;
}
public List<DSDescriptor> getDescriptors() {
return descriptors;
public List<DSColumn> getColumns() {
return columns;
}
public void setDescriptors(List<DSDescriptor> descriptors) {
this.descriptors = descriptors;
public void setColumns(List<DSColumn> columns) {
this.columns = columns;
}
}
package org.genesys2.server.model.dataset;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.genesys.blocks.model.BasicModel;
/**
* Dataset column.
*/
@Entity
@Table(name = "ds2column")
public class DSColumn extends BasicModel {
/**
*
*/
private static final long serialVersionUID = -7693331325536594367L;
@Column(name="idx")
private float index = 1.0f;
@ManyToOne(optional = false)
@JoinColumn(name="ds")
private DS dataset;
@ManyToOne(optional = false)
@JoinColumn(name="d")
private DSDescriptor descriptor;
public float getIndex() {
return index;
}
public void setIndex(float index) {
this.index = index;
}
public DS getDataset() {
return dataset;
}
public void setDataset(DS dataset) {
this.dataset = dataset;
}
public DSDescriptor getDescriptor() {
return descriptor;
}
public void setDescriptor(DSDescriptor descriptor) {
this.descriptor = descriptor;
}
@Override
public String toString() {
return "DSD " + this.getId();
}
}
/**
* 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.model.dataset;
import java.text.MessageFormat;
import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Lob;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import org.genesys.blocks.model.BasicModel;
import org.genesys2.server.model.impl.Descriptor;
import org.hibernate.annotations.Type;
/**
* Dataset descriptor
* A DSDescriptor represents a dimension of a sparse vector.
*
* @author mobreza
*/
@Entity
@Table(name = "ds2descriptor")
public class DSDescriptor extends BasicModel {
private static final long serialVersionUID = 3832200593904442940L;
@Column(unique = true, nullable=false, updatable = false)
@Type(type = "uuid-binary")
private UUID uuid;
@Lob
@Type(type = "org.hibernate.type.TextType")
private String title;
@Column(nullable = false, length = 200)
private String code;
@Lob
@Type(type = "org.hibernate.type.TextType")
private String description;
@Column(length = 20)
private String uom;
/**
*
* Generate UUID if missing
*/
private static final long serialVersionUID = -7693331325536594367L;
@PrePersist
protected void prepersist() {
if (this.uuid == null) {
this.uuid = UUID.randomUUID();
}
}
public UUID getUuid() {
return uuid;
}
@Column(name="idx")
private float index = 1.0f;
public void setUuid(UUID uuid) {
this.uuid = uuid;
}
@ManyToOne(optional = false)
@JoinColumn(name="ds")
private DS dataset;
@ManyToOne(optional = false)
@JoinColumn(name="d")
private Descriptor descriptor;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public float getIndex() {
return index;
public String getCode() {
return code;
}
public void setIndex(float index) {
this.index = index;
public void setCode(String code) {
this.code = code;
}
public DS getDataset() {
return dataset;
public String getDescription() {
return description;
}
public void setDataset(DS dataset) {
this.dataset = dataset;
public void setDescription(final String description) {
this.description = description;
}
public Descriptor getDescriptor() {
return descriptor;
public String getUom() {
return uom;
}
public void setDescriptor(Descriptor descriptor) {
this.descriptor = descriptor;
public void setUom(String uom) {
this.uom = uom;
}
@Override
public String toString() {
return "DSD " + this.getId();
return MessageFormat.format("DSDescriptor id={0,number,#} name={1}", getId(), code);
}
}
......@@ -7,10 +7,9 @@ import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.genesys.blocks.model.BasicModel;
import org.genesys2.server.model.impl.Descriptor;
/**
* Dataset qualifier specification
* The dataset qualifier is a key lookup component for dataset rows.
*/
@Entity
@Table(name = "ds2qualifier")
......@@ -30,7 +29,7 @@ public class DSQualifier extends BasicModel {
@ManyToOne(optional = false)
@JoinColumn(name="d")
private Descriptor descriptor;
private DSDescriptor descriptor;
public float getIndex() {
return index;
......@@ -48,11 +47,11 @@ public class DSQualifier extends BasicModel {
this.dataset = dataset;
}
public Descriptor getDescriptor() {
public DSDescriptor getDescriptor() {
return descriptor;
}
public void setDescriptor(Descriptor descriptor) {
public void setDescriptor(DSDescriptor descriptor) {
this.descriptor = descriptor;
}
......
package org.genesys2.server.model.dataset;
import java.nio.ByteBuffer;
import java.util.List;
import javax.persistence.CascadeType;
......@@ -12,23 +13,36 @@ import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.apache.commons.codec.digest.DigestUtils;
import org.genesys.blocks.model.EntityId;
/**
* Represents a row in a DS2 dataset. The row has a unique {@link #id} and
* groups together different {@link DSValue} values (column values).
*
* The row is qualified by a list of row qualifiers ({@link DSRowQualifier})
* that represent the "keys" of the row. Their SHA-1 and MD5 sums are calculated
* and stored in {@link #sha1} and {@link #md5} respectively.
*
* @author Matija Obreza
*/
@Entity
@Table(name = "ds2row", uniqueConstraints = { @UniqueConstraint(columnNames = { "md5", "sha1" }) })
public class DSRow implements EntityId {
@ManyToOne(fetch=FetchType.LAZY, optional = false)
@JoinColumn(name="ds")
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "ds")
private DS dataset;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "row", cascade = { CascadeType.REMOVE })
@OneToMany(fetch = FetchType.LAZY, mappedBy = "row", cascade = { CascadeType.ALL })
private List<DSRowQualifier<?>> rowQualifiers;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "row")
@OneToMany(fetch = FetchType.LAZY, mappedBy = "row", cascade = { CascadeType.ALL })
private List<DSValue<?>> values;
@Id
......@@ -41,6 +55,19 @@ public class DSRow implements EntityId {
@Column(columnDefinition = "binary(20)", updatable = false)
private byte[] sha1;
@PrePersist
@PreUpdate
private void prePersist() {
ByteBuffer keyBuffer = ByteBuffer.allocate(500);
for (DSRowQualifier<?> dsq : this.getRowQualifiers()) {
dsq.putKey(keyBuffer);
}
byte[] array = keyBuffer.array();
this.md5 = DigestUtils.md5(array);
this.sha1 = DigestUtils.sha1(array);
}
@Override
public Long getId() {
return this.id;
......
package org.genesys2.server.model.dataset;
import java.nio.ByteBuffer;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
......@@ -14,6 +16,16 @@ import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
* Abstract class {@link DSRowQualifier} represents the "columns" of a
* {@link DSRow} that serve as "keys" or qualifying columns to a row. A row may
* have more than one qualifier and are not necessarily unique, but provide a
* fast mechanism to look up values in a row ({@link DSValue}).
*
* @author Matija Obreza
*
* @param <T> type of qualifier value
*/
@Entity
@Table(name = "ds2rowqualifier", indexes = { @Index(columnList = "dsq, vall") })
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
......@@ -70,4 +82,5 @@ public abstract class DSRowQualifier<T> {
return null;
}
public abstract void putKey(ByteBuffer keyBuffer);
}
package org.genesys2.server.model.dataset;
import java.nio.ByteBuffer;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
......@@ -21,4 +23,8 @@ public class DSRowQualifierLong extends DSRowQualifier<Long> {
this.value = value;
}
@Override
public void putKey(ByteBuffer keyBuffer) {
keyBuffer.putLong(this.value);
}
}
......@@ -15,7 +15,7 @@ import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "ds2value", indexes = { @Index(columnList = "r,dsd"), @Index(columnList = "dsd,vall"), @Index(columnList = "dsd,vald") })
@Table(name = "ds2value", indexes = { @Index(columnList = "r,dsc"), @Index(columnList = "dsc,vall"), @Index(columnList = "dsc,vald") })
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.INTEGER, name = "typ")
public abstract class DSValue<T> {
......@@ -28,8 +28,8 @@ public abstract class DSValue<T> {
private DSRow row;
@ManyToOne(fetch=FetchType.LAZY, optional = false)
@JoinColumn(name = "dsd")
private DSDescriptor datasetDescriptor;
@JoinColumn(name = "dsc")
private DSColumn datasetColumn;
public abstract T getValue();
......@@ -51,12 +51,12 @@ public abstract class DSValue<T> {
this.row = row;
}
public DSDescriptor getDatasetDescriptor() {
return datasetDescriptor;
public DSColumn getDatasetColumn() {
return datasetColumn;
}
public void setDatasetDescriptor(DSDescriptor datasetDescriptor) {
this.datasetDescriptor = datasetDescriptor;
public void setDatasetColumn(DSColumn datasetColumn) {
this.datasetColumn = datasetColumn;
}
public static DSValue<?> make(Object v) {
......
......@@ -21,12 +21,15 @@ import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Version;
import org.apache.commons.lang.StringUtils;
import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.model.BasicModel;
import org.genesys.worldclim.WorldClimUtil;
import org.genesys2.server.model.impl.GeoReferencedEntity;
@Entity
......@@ -57,6 +60,15 @@ public class AccessionGeo extends BasicModel implements GeoReferencedEntity, Acc
private Long tileIndex;
/**
* Recalculate {@link #tileIndex} on insert and update
*/
@PrePersist
@PreUpdate
private void prePersist() {
tileIndex = WorldClimUtil.getTileIndex(5, this.longitude, this.latitude);
}
public long getVersion() {
return version;
}
......
/**
* 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.model.impl;
import java.text.MessageFormat;
import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import org.genesys.blocks.model.BasicModel;
import org.hibernate.annotations.Type;
/**
* A Descriptor represents a dimension of a sparse vector.
*
* @author mobreza
*/
@Entity
@Table(name = "descriptor")
public class Descriptor extends BasicModel {
private static final long serialVersionUID = 3832200593904442940L;
@Column(unique = true, nullable=false, updatable = false)
@Type(type = "uuid-binary")
private UUID uuid;
@Lob
@Type(type = "org.hibernate.type.TextType")
private String title;
@Column(nullable = false, length = 200)
private String code;
@Lob
@Type(type = "org.hibernate.type.TextType")
private String description;
@Column(length = 20)
private String uom;
/**
* Generate UUID if missing
*/
@PrePersist
protected void prepersist() {
if (this.uuid == null) {
this.uuid = UUID.randomUUID();
}
}
public UUID getUuid() {
return uuid;
}
public void setUuid(UUID uuid) {
this.uuid = uuid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(final String description) {
this.description = description;
}
public String getUom() {
return uom;
}
public void setUom(String uom) {
this.uom = uom;
}
@Override
public String toString() {
return MessageFormat.format("Descriptor id={0,number,#} name={1}", getId(), code);
}
}
......@@ -3,7 +3,7 @@ package org.genesys2.server.model.json;
import java.util.HashMap;
import java.util.Map;
import org.genesys2.server.model.impl.Descriptor;
import org.genesys2.server.model.dataset.DSDescriptor;
import com.fasterxml.jackson.annotation.JsonIgnore;
......@@ -18,7 +18,7 @@ public class WorldclimJson {
private Double[] tempMax = new Double[12];
@JsonIgnore