Commit 7ea4e698 authored by Maxym Borodenko's avatar Maxym Borodenko

Versioned Datasets: update

Uncomment test
parent 84694442
/* /*
* Copyright 2018 Global Crop Diversity Trust * Copyright 2019 Global Crop Diversity Trust
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -17,6 +17,7 @@ package org.genesys.catalog.model.dataset; ...@@ -17,6 +17,7 @@ package org.genesys.catalog.model.dataset;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import javax.persistence.Cacheable; import javax.persistence.Cacheable;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
...@@ -35,12 +36,16 @@ import javax.persistence.ManyToMany; ...@@ -35,12 +36,16 @@ import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.OrderColumn; import javax.persistence.OrderColumn;
import javax.persistence.PostLoad;
import javax.persistence.PrePersist; import javax.persistence.PrePersist;
import javax.persistence.PreUpdate; import javax.persistence.PreUpdate;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
import com.fasterxml.jackson.annotation.JsonGetter;
import org.genesys.blocks.auditlog.annotations.Audited; import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.model.JsonViews; import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.Publishable; import org.genesys.blocks.model.Publishable;
...@@ -66,10 +71,10 @@ import com.fasterxml.jackson.annotation.JsonView; ...@@ -66,10 +71,10 @@ import com.fasterxml.jackson.annotation.JsonView;
* *
* @author Matija Obreza * @author Matija Obreza
* @author Andrey Lugovskoy * @author Andrey Lugovskoy
* @author Maxim Borodenko * @author Maxym Borodenko
*/ */
@Entity @Entity
@Table(name = "dataset") @Table(name = "dataset", uniqueConstraints = @UniqueConstraint(name = "UQ_current_dataset_version", columnNames={"versionsId", "current"}))
@Cacheable @Cacheable
@Audited @Audited
@Document(indexName = "dataset") @Document(indexName = "dataset")
...@@ -79,11 +84,13 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Acl ...@@ -79,11 +84,13 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Acl
private static final long serialVersionUID = -4601980446454791177L; private static final long serialVersionUID = -4601980446454791177L;
/** The versions. */ /** The versions. */
@ManyToOne(cascade = CascadeType.ALL, optional = false) @ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, optional = false)
@JoinColumn(name = "versionsId", updatable = false) @JoinColumn(name = "versionsId", updatable = false)
@JsonIgnore @JsonIgnore
private DatasetVersions versions; private DatasetVersions versions;
private Boolean current;
/** The owner. */ /** The owner. */
@ManyToOne(cascade = {}, optional = false) @ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "partnerId", updatable = false) @JoinColumn(name = "partnerId", updatable = false)
...@@ -195,6 +202,16 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Acl ...@@ -195,6 +202,16 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Acl
@Enumerated(EnumType.ORDINAL) @Enumerated(EnumType.ORDINAL)
private PublishState state = PublishState.DRAFT; private PublishState state = PublishState.DRAFT;
@Transient
private UUID currentVersion;
@PostLoad
protected void postLoad() {
if (this.versions != null && versions.getCurrentVersion() != null && !this.uuid.equals(versions.getCurrentVersion().getUuid())) {
this.currentVersion = versions.getCurrentVersion().getUuid();
}
}
/** /**
* Preupdate. * Preupdate.
*/ */
...@@ -215,6 +232,43 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Acl ...@@ -215,6 +232,43 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Acl
trimStringsToNull(); trimStringsToNull();
} }
/**
* Gets the current.
*
* @return the current
*/
public Boolean getCurrent() {
return current;
}
/**
* Sets current value
*
* @param current the new value of current
*/
public void setCurrent(final Boolean current) {
this.current = current;
}
/**
* Gets the UUID of current dataset
*
* @return the UUID of current dataset
*/
@JsonGetter
public UUID getCurrentVersion() {
return currentVersion;
}
/**
* Sets the UUID of current dataset
*
* @param currentVersion the UUID of current dataset
*/
public void setCurrentVersion(final UUID currentVersion) {
this.currentVersion = currentVersion;
}
/** /**
* Owner is the ACL parent object for the dataset * Owner is the ACL parent object for the dataset
*/ */
......
...@@ -463,4 +463,12 @@ public interface DatasetService { ...@@ -463,4 +463,12 @@ public interface DatasetService {
*/ */
void writeXlsxMCPD(Dataset dataset, OutputStream outputStream) throws IOException; void writeXlsxMCPD(Dataset dataset, OutputStream outputStream) throws IOException;
/**
* Method creating a new version of Dataset based on an existing published Dataset.
*
* @param source the source
* @return saved Dataset in db.
*/
Dataset createNewVersion(@Valid Dataset source);
} }
...@@ -26,9 +26,11 @@ import java.nio.file.Paths; ...@@ -26,9 +26,11 @@ import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -174,12 +176,15 @@ public class DatasetServiceImpl implements DatasetService { ...@@ -174,12 +176,15 @@ public class DatasetServiceImpl implements DatasetService {
public Dataset createDataset(@Valid final Dataset source) { public Dataset createDataset(@Valid final Dataset source) {
LOG.info("Create Dataset. Input data {}", source); LOG.info("Create Dataset. Input data {}", source);
final DatasetVersions datasetVersions = new DatasetVersions(); final DatasetVersions datasetVersions = new DatasetVersions();
datasetVersions.setCurrentVersion(null);
datasetVersionsRepository.save(datasetVersions); datasetVersionsRepository.save(datasetVersions);
Dataset dataset = new Dataset(); Dataset dataset = new Dataset();
dataset.setUuid(source.getUuid()); dataset.setUuid(source.getUuid());
dataset.setVersions(datasetVersions); dataset.setVersions(datasetVersions);
dataset.setCurrent(null);
copyValues(dataset, source); copyValues(dataset, source);
dataset.setState(PublishState.DRAFT);
dataset.setOwner(source.getOwner()); dataset.setOwner(source.getOwner());
dataset.setVersionTag(source.getVersionTag()); dataset.setVersionTag(source.getVersionTag());
dataset.setDescriptors(new ArrayList<>()); dataset.setDescriptors(new ArrayList<>());
...@@ -195,22 +200,8 @@ public class DatasetServiceImpl implements DatasetService { ...@@ -195,22 +200,8 @@ public class DatasetServiceImpl implements DatasetService {
// Make dataset publicly not-readable // Make dataset publicly not-readable
aclService.makePubliclyReadable(dataset, false); aclService.makePubliclyReadable(dataset, false);
try { // Make dataset folder
final Path datasetPath = Paths.get(datasetRepositoryPath, dataset.getUuid().toString()); makeDatasetFolder(dataset.getUuid());
final Partner partner = dataset.getOwner();
// Ensure folder ownership
asAdminInvoker.invoke(() -> {
// Ensure target folder exists for the Dataset
return repositoryService.ensureFolder(datasetPath, partner);
});
} catch (Exception e) {
LOG.warn("Could not update Folder properties: {}", e.getMessage());
}
datasetVersions.setCurrentVersion(dataset);
datasetVersionsRepository.save(datasetVersions);
return dataset; return dataset;
} }
...@@ -364,6 +355,82 @@ public class DatasetServiceImpl implements DatasetService { ...@@ -364,6 +355,82 @@ public class DatasetServiceImpl implements DatasetService {
downloadService.writeXlsxMCPD(queryAccessionId, outputStream, "", "/datasets/" + dataset.getUuid()); downloadService.writeXlsxMCPD(queryAccessionId, outputStream, "", "/datasets/" + dataset.getUuid());
} }
/**
* {@inheritDoc}
*/
@Override
@Transactional
@PreAuthorize("(hasRole('ADMINISTRATOR') || hasPermission(#source, 'write')) && #source.published")
public Dataset createNewVersion(@Valid Dataset source) {
source = loadDataset(source);
final Dataset dataset = new Dataset();
copyValues(dataset, source);
dataset.setState(PublishState.DRAFT);
dataset.setCurrent(null);
dataset.setUuid(null);
dataset.setVersions(source.getVersions());
dataset.setDescriptors(new ArrayList<>());
copyDescriptors(dataset, source.getDescriptors());
final Dataset saved = lazyLoad(datasetRepository.save(dataset));
setAccessionRefs(saved, new HashSet<>(source.getAccessionRefs()));
// Copy creators
saved.setCreators(new ArrayList<>());
source.getCreators().forEach(creator -> {
entityManager.detach(creator);
creator.setDataset(saved);
creator.setId(null);
creator.setVersion(null);
creator.setUuid(null);
saved.getCreators().add(datasetCreatorRepository.save(creator));
});
// Copy locations
saved.setLocations(new ArrayList<>());
source.getLocations().forEach(location -> {
entityManager.detach(location);
location.setDataset(saved);
location.setId(null);
location.setVersion(null);
location.setUuid(null);
saved.getLocations().add(locationRepository.save(location));
});
saved.setCurrentVersion(source.getUuid());
// Make dataset publicly not-readable
aclService.makePubliclyReadable(saved, false);
// Make dataset folder
makeDatasetFolder(saved.getUuid());
return lazyLoad(saved);
}
private void makeDatasetFolder(final UUID uuid) {
final Dataset dataset = datasetRepository.findByUuid(uuid);
if (dataset == null) {
LOG.warn("Folder wasn't created. No such dataset with UUID={}", uuid.toString());
return;
}
try {
final Path datasetPath = Paths.get(datasetRepositoryPath, dataset.getUuid().toString());
final Partner partner = dataset.getOwner();
// Ensure folder ownership
asAdminInvoker.invoke(() -> {
// Ensure target folder exists for the Dataset
return repositoryService.ensureFolder(datasetPath, partner);
});
} catch (Exception e) {
LOG.warn("Could not update Folder properties: {}", e.getMessage());
}
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
...@@ -394,9 +461,11 @@ public class DatasetServiceImpl implements DatasetService { ...@@ -394,9 +461,11 @@ public class DatasetServiceImpl implements DatasetService {
*/ */
@Override @Override
public Page<Dataset> listDatasets(final DatasetFilter filter, final Pageable page) { public Page<Dataset> listDatasets(final DatasetFilter filter, final Pageable page) {
Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "title"); final Pageable markdownSortPageRequest = JPAUtils.toMarkdownSort(page, "title");
Page<Dataset> res = datasetRepository.findAll(new BooleanBuilder().and(filter.buildPredicate()).and(QDataset.dataset.state.in(PublishState.PUBLISHED)), final BooleanBuilder published = new BooleanBuilder();
markdownSortPageRequest); published.and(QDataset.dataset.state.eq(PublishState.PUBLISHED).and(QDataset.dataset.current.isTrue()));
published.and(filter.buildPredicate());
final Page<Dataset> res = datasetRepository.findAll(published, markdownSortPageRequest);
return new PageImpl<>(res.getContent(), page, res.getTotalElements()); return new PageImpl<>(res.getContent(), page, res.getTotalElements());
} }
...@@ -576,6 +645,9 @@ public class DatasetServiceImpl implements DatasetService { ...@@ -576,6 +645,9 @@ public class DatasetServiceImpl implements DatasetService {
if (dataset.getCrops() != null) { if (dataset.getCrops() != null) {
dataset.getCrops().size(); dataset.getCrops().size();
} }
if (dataset.getVersions() != null && dataset.getVersions().getAllVersions() != null) {
dataset.getVersions().getAllVersions().size();
}
return dataset; return dataset;
} }
...@@ -705,6 +777,16 @@ public class DatasetServiceImpl implements DatasetService { ...@@ -705,6 +777,16 @@ public class DatasetServiceImpl implements DatasetService {
// Make dataset publicly readable // Make dataset publicly readable
aclService.makePubliclyReadable(loaded, true); aclService.makePubliclyReadable(loaded, true);
final DatasetVersions datasetVersions = loaded.getVersions();
final Dataset oldCurrentDataset = datasetVersions.getAllVersions().stream().filter(s -> Objects.equals(s.getCurrent(), Boolean.TRUE)).findFirst().orElse(null);
if (oldCurrentDataset != null) {
oldCurrentDataset.setCurrent(null);
datasetRepository.save(oldCurrentDataset);
}
loaded.setCurrent(Boolean.TRUE);
datasetVersions.setCurrentVersion(loaded);
datasetVersionsRepository.save(datasetVersions);
return lazyLoad(datasetRepository.save(loaded)); return lazyLoad(datasetRepository.save(loaded));
} }
...@@ -749,7 +831,7 @@ public class DatasetServiceImpl implements DatasetService { ...@@ -749,7 +831,7 @@ public class DatasetServiceImpl implements DatasetService {
@Transactional @Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'administration')") @PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'administration')")
public Dataset rejectDataset(final Dataset dataset) { public Dataset rejectDataset(final Dataset dataset) {
final Dataset loaded = reloadFromDatabase(dataset); Dataset loaded = reloadFromDatabase(dataset);
if (loaded == null) { if (loaded == null) {
throw new NotFoundElement("No dataset with specified uuid and version"); throw new NotFoundElement("No dataset with specified uuid and version");
} }
...@@ -761,6 +843,29 @@ public class DatasetServiceImpl implements DatasetService { ...@@ -761,6 +843,29 @@ public class DatasetServiceImpl implements DatasetService {
} }
} }
if (loaded.isPublished() && Objects.equals(loaded.getCurrent(), true)) {
final DatasetVersions datasetVersions = loaded.getVersions();
if (datasetVersions.getAllVersions().size() > 1) {
UUID youngestDatasetUUID = datasetVersions.getAllVersions().stream()
.filter(s -> s.getCurrent() == null)
.max(Comparator.comparing(Dataset::getCreatedDate)).get().getUuid();
loaded.setCurrent(null);
datasetRepository.save(loaded);
Dataset youngestDataset = datasetRepository.findByUuid(youngestDatasetUUID);
youngestDataset.setCurrent(true);
datasetRepository.save(youngestDataset);
datasetVersions.setCurrentVersion(youngestDataset);
} else {
loaded.setCurrent(null);
datasetVersions.setCurrentVersion(null);
}
datasetVersionsRepository.save(datasetVersions);
} else if (loaded.isPublished() && Objects.isNull(loaded.getCurrent())) {
throw new InvalidApiUsageException("Cannot be un-published. The dataset is not the latest version.");
}
loaded.setState(PublishState.DRAFT); loaded.setState(PublishState.DRAFT);
{ {
......
...@@ -568,4 +568,16 @@ public class DatasetController extends ApiBaseController { ...@@ -568,4 +568,16 @@ public class DatasetController extends ApiBaseController {
LOG.warn("Download was aborted: {}", e.getMessage()); LOG.warn("Download was aborted: {}", e.getMessage());
} }
} }
/**
* Create a new version of Dataset based on an existing published Dataset.
*
* @param uuid Dataset UUID
* @return the new version of Dataset
*/
@PostMapping(value = "/create-new-version")
public Dataset createNewVersion(@RequestParam(value = "uuid", required = true) final UUID uuid) {
final Dataset dataset = datasetService.loadDataset(uuid);
return datasetService.createNewVersion(dataset);
}
} }
...@@ -471,19 +471,39 @@ public class Subset extends UuidModel implements AclAwareModel, SelfCleaning { ...@@ -471,19 +471,39 @@ public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
this.source = source; this.source = source;
} }
/**
* Gets the owner.
*
* @return the owner
*/
public Partner getOwner() { public Partner getOwner() {
return owner; return owner;
} }
/**
* Sets the owner.
*
* @param owner the owner
*/
public void setOwner(Partner owner) { public void setOwner(Partner owner) {
this.owner = owner; this.owner = owner;
} }
/**
* Gets the UUID of current subset
*
* @return the UUID of current subset
*/
@JsonGetter @JsonGetter
public UUID getCurrentVersion() { public UUID getCurrentVersion() {
return currentVersion; return currentVersion;
} }
/**
* Sets the UUID of current subset
*
* @param currentVersion the UUID of current subset
*/
public void setCurrentVersion(UUID currentVersion) { public void setCurrentVersion(UUID currentVersion) {
this.currentVersion = currentVersion; this.currentVersion = currentVersion;
} }
......
...@@ -20,7 +20,9 @@ import static org.genesys2.server.model.impl.QSubsetCreator.subsetCreator; ...@@ -20,7 +20,9 @@ import static org.genesys2.server.model.impl.QSubsetCreator.subsetCreator;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
...@@ -727,7 +729,8 @@ public class SubsetServiceImpl implements SubsetService { ...@@ -727,7 +729,8 @@ public class SubsetServiceImpl implements SubsetService {
subset.setUuid(null); subset.setUuid(null);
subset.setVersions(source.getVersions()); subset.setVersions(source.getVersions());
Subset saved = lazyLoad(subsetRepository.save(subset)); Subset saved = lazyLoad(subsetRepository.save(subset));
setAccessionRefs(saved, new HashSet<>(source.getAccessionRefs()));
saved.setCreators(new ArrayList<>());
// Copy creators // Copy creators
source.getCreators().forEach(creator -> { source.getCreators().forEach(creator -> {
entityManager.detach(creator); entityManager.detach(creator);
...@@ -735,10 +738,8 @@ public class SubsetServiceImpl implements SubsetService { ...@@ -735,10 +738,8 @@ public class SubsetServiceImpl implements SubsetService {
creator.setId(null); creator.setId(null);
creator.setVersion(null); creator.setVersion(null);
creator.setUuid(null); creator.setUuid(null);
subsetCreatorRepository.save(creator); saved.getCreators().add(subsetCreatorRepository.save(creator));
}); });
setAccessionRefs(saved, new HashSet<>(source.getAccessionRefs()));
saved.setCurrentVersion(source.getUuid()); saved.setCurrentVersion(source.getUuid());
// Make Subset publicly not-readable // Make Subset publicly not-readable
......
...@@ -5783,3 +5783,28 @@ databaseChangeLog: ...@@ -5783,3 +5783,28 @@ databaseChangeLog:
- sql: - sql:
comment: Organization renamed to PGRFANetwork comment: Organization renamed to PGRFANetwork
sql: update classpk set classname='org.genesys2.server.model.impl.PGRFANetwork', shortName='pgrfanetwork' where classname='org.genesys2.server.model.impl.Organization'; sql: update classpk set classname='org.genesys2.server.model.impl.PGRFANetwork', shortName='pgrfanetwork' where classname='org.genesys2.server.model.impl.Organization';
- changeSet:
id: 1552920444624-1
author: mborodenko
comment: Extend dataset table
changes:
- addColumn:
tableName: dataset
columns:
- column:
name: current
type: BIT(1)
defaultValue: null
- sql:
comment: dataset#current should be true if dataset is published
sql: update dataset set current = true where state = 1
- changeSet:
id: 1552920444624-2
author: mborodenko
changes:
- sql:
comment: dataset_version#currentVersion_id must be null if subset is not published
sql: update dataset_version dv inner join dataset d on dv.currentVersion_id = d.id
set dv.currentVersion_id = null where d.state != 1
...@@ -15,23 +15,32 @@ ...@@ -15,23 +15,32 @@
*/ */
package org.genesys.test.base; package org.genesys.test.base;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.UUID;