Commit 2a5449fb authored by Matija Obreza's avatar Matija Obreza

Dataset 2

parent 8cb4ddc4
/**
* 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.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.SequenceGenerator;
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(name = "datasetval")
@Table(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.TABLE_PER_CLASS)
public abstract class DatasetRow<T> implements EntityId {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "datasetrow_seq")
@SequenceGenerator(name = "datasetrow_seq", sequenceName = "datasetrow_seq", allocationSize = 10)
@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.Entity;
/**
* A Descriptor represents a dimension of a sparse vector.
*
* @author mobreza
*/
@Entity(name = "datasetdbl")
public class TraitDblValue extends DatasetRow<Double> {
@Column(nullable = false, 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.Entity;
import javax.persistence.Index;
import javax.persistence.Table;
/**
* A Descriptor represents a dimension of a sparse vector.
*
* @author mobreza
*/
@Entity(name = "datasetint")
@Table(indexes={ @Index(name="IX_datasetint_val", unique=false, columnList="descriptorId, vali") })
public class TraitIntValue extends DatasetRow<Integer> {
@Column(nullable = false, 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.Entity;
/**
* A Descriptor represents a dimension of a sparse vector.
*
* @author mobreza
*/
@Entity(name = "datasetstr")
public class TraitStrValue extends DatasetRow<String> {
@Column(nullable = false, 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 org.genesys2.server.model.impl.Dataset;
import org.genesys2.server.model.impl.DatasetRow;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
public interface DatasetRowRepository extends JpaRepository<DatasetRow<?>, Long> {
Page<DatasetRow<?>> findByDataset(Dataset ds, Pageable page);
}
/**
* 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;
public interface Dataset2Service {
}
/**
* 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 org.genesys2.server.service.Dataset2Service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class Dataset2ServiceImpl implements Dataset2Service {
}
/**
* 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 static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import java.util.ArrayList;
import java.util.List;
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.TraitIntValue;
import org.genesys2.server.model.impl.TraitStrValue;
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.genesys2.server.test.JpaDataConfig;
import org.genesys2.server.test.PropertyPlacholderInitializer;
import org.genesys2.spring.config.HazelcastConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.spring.cache.HazelcastCacheManager;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DatasetTest.Config.class, initializers = PropertyPlacholderInitializer.class)
@ActiveProfiles("dev")
public class DatasetTest {
@Import({ HazelcastConfig.class, JpaDataConfig.class })
public static class Config {
@Bean
public HazelcastCacheManager cacheManager(HazelcastInstance hazelcastInstance) {
HazelcastCacheManager cm = new HazelcastCacheManager(hazelcastInstance);
return cm;
}
@Bean
public Dataset2Service dataset2Service() {
return new Dataset2ServiceImpl();
}
}
@Autowired
private Dataset2Service datasetService;
@Autowired
private DatasetRepository datasetRepository;
@Autowired
private DescriptorRepository descriptorRepository;
@Autowired
private DatasetRowRepository datasetRowRepository;
@Test
public void test1() {
List<Dataset> x = datasetRepository.findAll();
System.err.println(x);
}
@Test
@Transactional
public void test2() {
Dataset ds=new Dataset();
ds.setName("DS1");
datasetRepository.save(ds);
assertThat("Dataset ID was not assigned", ds.getId(), notNullValue());
Dataset loaded = datasetRepository.getOne(ds.getId());
assertThat("Loaded ds not same as saved ds", loaded.getId(), is(ds.getId()));
datasetRepository.delete(loaded);
loaded = datasetRepository.findOne(ds.getId());
assertThat("Expected null", loaded, nullValue());
}
@Test
@Transactional
public void testInts() {
Dataset ds=new Dataset();
ds.setName("DS1");
datasetRepository.save(ds);
assertThat("Dataset ID was not assigned", ds.getId(), notNullValue());
Descriptor intDescriptor=new Descriptor();
intDescriptor.setCode("INT");
intDescriptor.setTitle("Integer descriptor");
descriptorRepository.save(intDescriptor);
List<TraitIntValue> vals=new ArrayList<TraitIntValue>();
for (int i=0; i<100; i++) {
TraitIntValue val = new TraitIntValue();
val.setDataset(ds);
val.setDescriptor(intDescriptor);
val.setVal(i);
vals.add(val);
}
datasetRowRepository.save(vals);
Page<DatasetRow<?>> res = datasetRowRepository.findByDataset(ds, new PageRequest(0, 101));
assertThat("Expected non-null result", res, notNullValue());
assertThat("Expected 100 result entries", res.getContent(), hasSize(100));
datasetRowRepository.delete(res);
descriptorRepository.delete(intDescriptor);
datasetRepository.delete(ds);
ds = datasetRepository.findOne(ds.getId());
assertThat("Expected null", ds, nullValue());
}
@Test
@Transactional
public void testStrs() {
Dataset ds=new Dataset();
ds.setName("DS1");
datasetRepository.save(ds);
assertThat("Dataset ID was not assigned", ds.getId(), notNullValue());
Descriptor strDescriptor=new Descriptor();
strDescriptor.setCode("STR");
strDescriptor.setTitle("String descriptor");
descriptorRepository.save(strDescriptor);
List<TraitStrValue> vals=new ArrayList<TraitStrValue>();
for (int i=0; i<100; i++) {
TraitStrValue val = new TraitStrValue();
val.setDataset(ds);
val.setDescriptor(strDescriptor);
val.setVal("Str " + i);
vals.add(val);
}
datasetRowRepository.save(vals);
Page<DatasetRow<?>> res = datasetRowRepository.findByDataset(ds, new PageRequest(0, 101));
assertThat("Expected non-null result", res, notNullValue());
assertThat("Expected 100 result entries", res.getContent(), hasSize(100));
datasetRowRepository.delete(res);
descriptorRepository.delete(strDescriptor);
datasetRepository.delete(ds);
ds = datasetRepository.findOne(ds.getId());
assertThat("Expected null", ds, nullValue());
}
@Test
@Transactional
public void testStrsAndInts() {
Dataset ds=new Dataset();
ds.setName("DS1");
datasetRepository.save(ds);
assertThat("Dataset ID was not assigned", ds.getId(), notNullValue());
Descriptor strDescriptor=new Descriptor();
strDescriptor.setCode("STR");
strDescriptor.setTitle("String descriptor");
descriptorRepository.save(strDescriptor);
List<TraitStrValue> vals=new ArrayList<TraitStrValue>();
for (int i=0; i<100; i++) {
TraitStrValue val = new TraitStrValue();
val.setDataset(ds);
val.setDescriptor(strDescriptor);
val.setVal("Str " + i);
vals.add(val);
}
datasetRowRepository.save(vals);
Descriptor intDescriptor=new Descriptor();
intDescriptor.setCode("INT");
intDescriptor.setTitle("Integer descriptor");
descriptorRepository.save(intDescriptor);
List<TraitIntValue> iVals=new ArrayList<TraitIntValue>();
for (int i=0; i<100; i++) {
TraitIntValue val = new TraitIntValue();
val.setDataset(ds);
val.setDescriptor(intDescriptor);
val.setVal(i);
iVals.add(val);
}
datasetRowRepository.save(iVals);
Page<DatasetRow<?>> res = datasetRowRepository.findByDataset(ds, new PageRequest(0, 201));
assertThat("Expected non-null result", res, notNullValue());
assertThat("Expected 200 result entries", res.getContent(), hasSize(200));
for (DatasetRow<?> row : res.getContent()) {
System.out.println(row.getId() + " " + row.getVal());
}
datasetRowRepository.delete(res);
descriptorRepository.delete(intDescriptor);
descriptorRepository.delete(strDescriptor);
datasetRepository.delete(ds);
ds = datasetRepository.findOne(ds.getId());
assertThat("Expected null", ds, nullValue());
}
}
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