Commit ac7daef6 authored by Matija Obreza's avatar Matija Obreza
Browse files

Removed Dataset

parent 81233e9f
/**
* 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.Date;
import java.util.List;
import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import org.genesys2.server.model.BusinessModel;
import org.hibernate.annotations.Type;
@Entity
@Table(name = "dataset")
public class Dataset extends BusinessModel {
private static final long serialVersionUID = 4562288873039057125L;
@ManyToOne(cascade = {}, optional = true)
private License license;
@Column(nullable = false, length = 500)
private String name;
@Lob
@Type(type = "org.hibernate.type.TextType")
private String description;
@Column(length = 500)
private String source;
@Lob
@Type(type = "org.hibernate.type.TextType")
private String headers;
@Lob
@Type(type = "org.hibernate.type.TextType")
private String mapping;
private Date uploadDate;
@OrderBy("orderIndex")
@OneToMany(cascade = {}, mappedBy = "dataset")
private List<DatasetDescriptor> datasetDescriptors;
@Column(unique = true, columnDefinition = "BINARY(16)")
private UUID uuid;
/**
* Generate UUID if missing
*/
@PrePersist
protected void prepersist() {
if (this.uuid == null) {
this.uuid = UUID.randomUUID();
}
}
public License getLicense() {
return license;
}
public void setLicense(final License license) {
this.license = license;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getUploadDate() {
return uploadDate;
}
public void setUploadDate(final Date uploadDate) {
this.uploadDate = uploadDate;
}
public String getSource() {
return source;
}
public void setSource(final String source) {
this.source = source;
}
/**
* Gets list of headers in source file
*
* @return
*/
public String getHeaders() {
return headers;
}
public void setHeaders(final String headers) {
this.headers = headers;
}
/**
* Data mapping info
*
* @return
*/
public String getMapping() {
return mapping;
}
public void setMapping(final String mapping) {
this.mapping = mapping;
}
public List<DatasetDescriptor> getDatasetDescriptors() {
return datasetDescriptors;
}
public void setDatasetDescriptors(List<DatasetDescriptor> datasetDescriptors) {
this.datasetDescriptors = datasetDescriptors;
}
@Override
public String toString() {
return MessageFormat.format("Dataset id={0,number,#} date={1,date,dd/MM/yyyy} name={2}", id, uploadDate, name);
}
public void setUuid(UUID uuid) {
this.uuid = uuid;
}
public UUID getUuid() {
return uuid;
}
}
/**
* 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 javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.genesys2.server.model.BusinessModel;
@Entity
@Table(name = "datasetdescriptor", uniqueConstraints = { @UniqueConstraint(columnNames = { "datasetId", "descriptorId" }) })
public class DatasetDescriptor extends BusinessModel {
private static final long serialVersionUID = 2413430585742976014L;
@ManyToOne(optional = false)
@JoinColumn(name = "datasetId")
private Dataset dataset;
@Column(nullable = false)
private int orderIndex;
@ManyToOne(optional = false)
@JoinColumn(name = "descriptorId")
private Descriptor descriptor;
public Dataset getDataset() {
return dataset;
}
public void setDataset(Dataset dataset) {
this.dataset = dataset;
}
public int getOrderIndex() {
return orderIndex;
}
public void setOrderIndex(int orderIndex) {
this.orderIndex = orderIndex;
}
public Descriptor getDescriptor() {
return descriptor;
}
public void setDescriptor(Descriptor descriptor) {
this.descriptor = descriptor;
}
}
/**
* 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 java.util.List;
import org.genesys2.server.model.impl.Dataset;
import org.genesys2.server.model.impl.DatasetDescriptor;
import org.genesys2.server.model.impl.Descriptor;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
public interface DatasetDescriptorRepository extends JpaRepository<DatasetDescriptor, Long> {
@Query("select dsd.descriptor from DatasetDescriptor dsd where dsd.dataset=?1 order by dsd.orderIndex")
List<Descriptor> findDescriptorsByDataset(Dataset dataset);
List<DatasetDescriptor> findByDataset(Dataset dataset);
DatasetDescriptor findByDatasetAndDescriptor(Dataset dataset, Descriptor descriptor);
}
/**
* 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 java.util.UUID;
import org.genesys2.server.model.impl.Dataset;
import org.springframework.data.jpa.repository.JpaRepository;
public interface DatasetRepository extends JpaRepository<Dataset, Long> {
Dataset findByUuid(UUID uuid);
}
/**
* 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.service;
import java.util.List;
import org.genesys2.server.model.impl.Dataset;
import org.genesys2.server.model.impl.Descriptor;
public interface DataService {
List<Dataset> list();
Dataset getDataset(long datasetId);
void save(Dataset dataset);
void writeEntries(Dataset dataset, Descriptor[] descriptors, List<String[]> data);
long countEntries(Dataset dataset);
long countData(Dataset dataset);
Iterable<Descriptor> getDescriptors(Dataset dataset);
}
......@@ -18,18 +18,12 @@ package org.genesys2.server.service;
import java.util.List;
import org.genesys2.server.model.impl.Dataset;
import org.genesys2.server.model.impl.DatasetDescriptor;
import org.genesys2.server.model.impl.Descriptor;
public interface DescriptorService {
List<Descriptor> list();
DatasetDescriptor[] getDatasetDescriptors(Dataset dataset, Descriptor[] descriptors);
DatasetDescriptor[] ensureDatasetDescriptors(Dataset dataset, Descriptor[] descriptors);
void saveDescriptor(Descriptor descriptor);
Descriptor getDescriptor(String variableName);
......
......@@ -20,15 +20,11 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.impl.Dataset;
import org.genesys2.server.model.impl.DatasetDescriptor;
import org.genesys2.server.model.impl.Descriptor;
import org.genesys2.server.persistence.domain.DatasetDescriptorRepository;
import org.genesys2.server.persistence.domain.DescriptorRepository;
import org.genesys2.server.service.DescriptorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
......@@ -39,68 +35,10 @@ public class DescriptorServiceImpl implements DescriptorService {
@Autowired
private DescriptorRepository descriptorRepository;
@Autowired
private DatasetDescriptorRepository datasetDescriptorRepository;
@Override
public List<Descriptor> list() {
return descriptorRepository.findAll();
}
/**
* Fetch {@link DatasetDescriptor}s.
*
* @param dataset
* @param descriptors
* @return
*/
@Override
@Transactional
public DatasetDescriptor[] getDatasetDescriptors(final Dataset dataset, final Descriptor[] descriptors) {
final DatasetDescriptor[] datasetDescriptors = new DatasetDescriptor[descriptors.length];
for (int i = 0; i < descriptors.length; i++) {
if (descriptors[i] == null) {
// Skip null descriptor
continue;
}
datasetDescriptors[i] = datasetDescriptorRepository.findByDatasetAndDescriptor(dataset, descriptors[i]);
}
return datasetDescriptors;
}
/**
* Fetch and ensure {@link DatasetDescriptor}s exist.
*
* @param dataset
* @param descriptors
* @return
*/
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false)
public DatasetDescriptor[] ensureDatasetDescriptors(final Dataset dataset, final Descriptor[] descriptors) {
final DatasetDescriptor[] datasetDescriptors = new DatasetDescriptor[descriptors.length];
for (int i = 0; i < descriptors.length; i++) {
if (descriptors[i] == null) {
// Skip null descriptor
continue;
}
datasetDescriptors[i] = datasetDescriptorRepository.findByDatasetAndDescriptor(dataset, descriptors[i]);
if (datasetDescriptors[i] == null) {
LOG.info("Adding new dataset descriptor for " + descriptors[i]);
final DatasetDescriptor dsd = new DatasetDescriptor();
dsd.setDataset(dataset);
dsd.setDescriptor(descriptors[i]);
dsd.setOrderIndex(i);
datasetDescriptors[i] = datasetDescriptorRepository.save(dsd);
}
}
LOG.info("Done with dataset descriptors");
return datasetDescriptors;
}
@Transactional
@Override
......
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