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; ...@@ -3,6 +3,7 @@ package org.genesys2.server.model.dataset;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import javax.persistence.CascadeType;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
...@@ -23,12 +24,12 @@ public class DS extends BasicModel { ...@@ -23,12 +24,12 @@ public class DS extends BasicModel {
private UUID uuid; private UUID uuid;
@OrderBy("index") @OrderBy("index")
@OneToMany(mappedBy = "dataset") @OneToMany(mappedBy = "dataset", cascade = { CascadeType.ALL })
private List<DSQualifier> qualifiers; private List<DSQualifier> qualifiers;
@OrderBy("index") @OrderBy("index")
@OneToMany(mappedBy = "dataset") @OneToMany(mappedBy = "dataset", cascade = { CascadeType.ALL })
private List<DSDescriptor> descriptors; private List<DSColumn> columns;
/** /**
* Generate UUID if missing * Generate UUID if missing
...@@ -56,11 +57,11 @@ public class DS extends BasicModel { ...@@ -56,11 +57,11 @@ public class DS extends BasicModel {
this.qualifiers = qualifiers; this.qualifiers = qualifiers;
} }
public List<DSDescriptor> getDescriptors() { public List<DSColumn> getColumns() {
return descriptors; return columns;
} }
public void setDescriptors(List<DSDescriptor> descriptors) { public void setColumns(List<DSColumn> columns) {
this.descriptors = descriptors; 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; package org.genesys2.server.model.dataset;
import java.text.MessageFormat;
import java.util.UUID;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.JoinColumn; import javax.persistence.Lob;
import javax.persistence.ManyToOne; import javax.persistence.PrePersist;
import javax.persistence.Table; import javax.persistence.Table;
import org.genesys.blocks.model.BasicModel; 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 @Entity
@Table(name = "ds2descriptor") @Table(name = "ds2descriptor")
public class DSDescriptor extends BasicModel { 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();
}
}
@Column(name="idx") public UUID getUuid() {
private float index = 1.0f; return uuid;
}
public void setUuid(UUID uuid) {
this.uuid = uuid;
}
@ManyToOne(optional = false)
@JoinColumn(name="ds") public String getTitle() {
private DS dataset; return title;
}
@ManyToOne(optional = false) public void setTitle(String title) {
@JoinColumn(name="d") this.title = title;
private Descriptor descriptor; }
public float getIndex() { public String getCode() {
return index; return code;
} }
public void setIndex(float index) { public void setCode(String code) {
this.index = index; this.code = code;
} }
public DS getDataset() { public String getDescription() {
return dataset; return description;
} }
public void setDataset(DS dataset) { public void setDescription(final String description) {
this.dataset = dataset; this.description = description;
} }
public Descriptor getDescriptor() { public String getUom() {
return descriptor; return uom;
} }
public void setDescriptor(Descriptor descriptor) { public void setUom(String uom) {
this.descriptor = descriptor; this.uom = uom;
} }
@Override @Override
public String toString() { 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; ...@@ -7,10 +7,9 @@ import javax.persistence.ManyToOne;
import javax.persistence.Table; import javax.persistence.Table;
import org.genesys.blocks.model.BasicModel; 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 @Entity
@Table(name = "ds2qualifier") @Table(name = "ds2qualifier")
...@@ -30,7 +29,7 @@ public class DSQualifier extends BasicModel { ...@@ -30,7 +29,7 @@ public class DSQualifier extends BasicModel {
@ManyToOne(optional = false) @ManyToOne(optional = false)
@JoinColumn(name="d") @JoinColumn(name="d")
private Descriptor descriptor; private DSDescriptor descriptor;
public float getIndex() { public float getIndex() {
return index; return index;
...@@ -48,11 +47,11 @@ public class DSQualifier extends BasicModel { ...@@ -48,11 +47,11 @@ public class DSQualifier extends BasicModel {
this.dataset = dataset; this.dataset = dataset;
} }
public Descriptor getDescriptor() { public DSDescriptor getDescriptor() {
return descriptor; return descriptor;
} }
public void setDescriptor(Descriptor descriptor) { public void setDescriptor(DSDescriptor descriptor) {
this.descriptor = descriptor; this.descriptor = descriptor;
} }
......
package org.genesys2.server.model.dataset; package org.genesys2.server.model.dataset;
import java.nio.ByteBuffer;
import java.util.List; import java.util.List;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
...@@ -12,23 +13,36 @@ import javax.persistence.Id; ...@@ -12,23 +13,36 @@ import javax.persistence.Id;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.UniqueConstraint; import javax.persistence.UniqueConstraint;
import org.apache.commons.codec.digest.DigestUtils;
import org.genesys.blocks.model.EntityId; 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 @Entity
@Table(name = "ds2row", uniqueConstraints = { @UniqueConstraint(columnNames = { "md5", "sha1" }) }) @Table(name = "ds2row", uniqueConstraints = { @UniqueConstraint(columnNames = { "md5", "sha1" }) })
public class DSRow implements EntityId { public class DSRow implements EntityId {
@ManyToOne(fetch=FetchType.LAZY, optional = false) @ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name="ds") @JoinColumn(name = "ds")
private DS dataset; 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; private List<DSRowQualifier<?>> rowQualifiers;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "row") @OneToMany(fetch = FetchType.LAZY, mappedBy = "row", cascade = { CascadeType.ALL })
private List<DSValue<?>> values; private List<DSValue<?>> values;
@Id @Id
...@@ -41,6 +55,19 @@ public class DSRow implements EntityId { ...@@ -41,6 +55,19 @@ public class DSRow implements EntityId {
@Column(columnDefinition = "binary(20)", updatable = false) @Column(columnDefinition = "binary(20)", updatable = false)
private byte[] sha1; 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 @Override
public Long getId() { public Long getId() {
return this.id; return this.id;
...@@ -53,11 +80,11 @@ public class DSRow implements EntityId { ...@@ -53,11 +80,11 @@ public class DSRow implements EntityId {
public DS getDataset() { public DS getDataset() {
return dataset; return dataset;
} }
public void setDataset(DS dataset) { public void setDataset(DS dataset) {
this.dataset = dataset; this.dataset = dataset;
} }
public List<DSRowQualifier<?>> getRowQualifiers() { public List<DSRowQualifier<?>> getRowQualifiers() {
return rowQualifiers; return rowQualifiers;
} }
......
package org.genesys2.server.model.dataset; package org.genesys2.server.model.dataset;
import java.nio.ByteBuffer;
import javax.persistence.DiscriminatorColumn; import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType; import javax.persistence.DiscriminatorType;
import javax.persistence.Entity; import javax.persistence.Entity;
...@@ -14,6 +16,16 @@ import javax.persistence.JoinColumn; ...@@ -14,6 +16,16 @@ import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.Table; 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 @Entity
@Table(name = "ds2rowqualifier", indexes = { @Index(columnList = "dsq, vall") }) @Table(name = "ds2rowqualifier", indexes = { @Index(columnList = "dsq, vall") })
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
...@@ -70,4 +82,5 @@ public abstract class DSRowQualifier<T> { ...@@ -70,4 +82,5 @@ public abstract class DSRowQualifier<T> {
return null; return null;
} }
public abstract void putKey(ByteBuffer keyBuffer);
} }
package org.genesys2.server.model.dataset; package org.genesys2.server.model.dataset;
import java.nio.ByteBuffer;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.DiscriminatorValue; import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity; import javax.persistence.Entity;
...@@ -20,5 +22,9 @@ public class DSRowQualifierLong extends DSRowQualifier<Long> { ...@@ -20,5 +22,9 @@ public class DSRowQualifierLong extends DSRowQualifier<Long> {
public void setValue(Long value) { public void setValue(Long value) {
this.value = value; this.value = value;
} }
@Override
public void putKey(ByteBuffer keyBuffer) {
keyBuffer.putLong(this.value);
}
} }
...@@ -15,7 +15,7 @@ import javax.persistence.ManyToOne; ...@@ -15,7 +15,7 @@ import javax.persistence.ManyToOne;
import javax.persistence.Table; import javax.persistence.Table;
@Entity @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) @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.INTEGER, name = "typ") @DiscriminatorColumn(discriminatorType = DiscriminatorType.INTEGER, name = "typ")
public abstract class DSValue<T> { public abstract class DSValue<T> {
...@@ -28,8 +28,8 @@ public abstract class DSValue<T> { ...@@ -28,8 +28,8 @@ public abstract class DSValue<T> {
private DSRow row; private DSRow row;
@ManyToOne(fetch=FetchType.LAZY, optional = false) @ManyToOne(fetch=FetchType.LAZY, optional = false)
@JoinColumn(name = "dsd") @JoinColumn(name = "dsc")
private DSDescriptor datasetDescriptor; private DSColumn datasetColumn;
public abstract T getValue(); public abstract T getValue();
...@@ -51,12 +51,12 @@ public abstract class DSValue<T> { ...@@ -51,12 +51,12 @@ public abstract class DSValue<T> {
this.row = row; this.row = row;
} }
public DSDescriptor getDatasetDescriptor() { public DSColumn getDatasetColumn() {
return datasetDescriptor; return datasetColumn;
} }
public void setDatasetDescriptor(DSDescriptor datasetDescriptor) { public void setDatasetColumn(DSColumn datasetColumn) {
this.datasetDescriptor = datasetDescriptor; this.datasetColumn = datasetColumn;
} }
public static DSValue<?> make(Object v) { public static DSValue<?> make(Object v) {
......
...@@ -21,12 +21,15 @@ import javax.persistence.Entity; ...@@ -21,12 +21,15 @@ import javax.persistence.Entity;
import javax.persistence.FetchType; import javax.persistence.FetchType;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.OneToOne; import javax.persistence.OneToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Version; import javax.persistence.Version;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.genesys.blocks.auditlog.annotations.Audited; import org.genesys.blocks.auditlog.annotations.Audited;