Commit 4a52ffab authored by Matija Obreza's avatar Matija Obreza

Dataset: Updates for new version

parent 507811fc
......@@ -104,6 +104,7 @@ import com.querydsl.core.types.dsl.PathBuilder;
import com.querydsl.core.types.dsl.PathBuilderFactory;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.JPQLQuery;
import com.vdurmont.semver4j.SemverException;
/**
* The Class DatasetServiceImpl.
......@@ -242,7 +243,7 @@ public class DatasetServiceImpl implements DatasetService {
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'write')")
public Dataset setAccessionRefs(Dataset dataset, @Valid final Set<AccessionRef> accessionRefs) {
LOG.info("Update AccessionIdentifiers. Input dataset: {}, input accessionRefs {}", dataset, accessionRefs);
dataset = reloadFromDatabase(dataset);
dataset = getDataset(dataset);
if (dataset.isPublished()) {
throw new InvalidApiUsageException("Cannot modify a published Dataset.");
}
......@@ -260,7 +261,7 @@ public class DatasetServiceImpl implements DatasetService {
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'write')")
public Dataset addDescriptors(Dataset dataset, final Descriptor... descriptors) {
LOG.info("Add descriptor to Dataset {}. Input descriptors {}", dataset, descriptors);
dataset = reloadFromDatabase(dataset);
dataset = getDataset(dataset);
if (dataset.isPublished()) {
throw new InvalidApiUsageException("Cannot modify a published Dataset.");
}
......@@ -291,7 +292,7 @@ public class DatasetServiceImpl implements DatasetService {
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'write')")
public Dataset removeDescriptors(Dataset dataset, final Descriptor... descriptors) {
LOG.info("Remove descriptors {} of dataset {}.", descriptors, dataset);
dataset = reloadFromDatabase(dataset);
dataset = getDataset(dataset);
if (dataset.isPublished()) {
throw new InvalidApiUsageException("Cannot modify a published Dataset.");
}
......@@ -313,7 +314,7 @@ public class DatasetServiceImpl implements DatasetService {
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'write')")
public Dataset updateDescriptors(Dataset dataset, final List<Descriptor> descriptors) {
LOG.info("Update descriptors for Dataset {}. Input descriptors {}", dataset, descriptors);
dataset = reloadFromDatabase(dataset);
dataset = getDataset(dataset);
if (dataset.isPublished()) {
throw new InvalidApiUsageException("Cannot modify a published Dataset.");
}
......@@ -366,11 +367,15 @@ public class DatasetServiceImpl implements DatasetService {
@Transactional
@PreAuthorize("(hasRole('ADMINISTRATOR') || hasPermission(#source, 'write')) && #source.published")
public Dataset createNewVersion(@Valid Dataset source) {
source = reloadFromDatabase(source);
source = getDataset(source);
final Dataset dataset = new Dataset();
copyValues(dataset, source);
dataset.setVersionTag(versionManager.nextMajor(source.getVersionTag()));
try {
dataset.setVersionTag(versionManager.nextMajor(source.getVersionTag()));
} catch (SemverException e) {
dataset.setVersionTag(source.getVersionTag() + ".1");
}
dataset.setState(PublishState.DRAFT);
dataset.setCurrent(null);
dataset.setUuid(null);
......@@ -380,10 +385,10 @@ public class DatasetServiceImpl implements DatasetService {
dataset.setDescriptors(new ArrayList<>());
copyDescriptors(dataset, source.getDescriptors());
final Dataset saved = datasetRepository.save(dataset);
Dataset saved = datasetRepository.save(dataset);
// copy accessionRefs
copyAccessionRefs(saved, source.getAccessionRefs());
saved = copyAccessionRefs(saved, source.getAccessionRefs());
// Copy creators
copyCreators(saved, source.getCreators());
......@@ -407,13 +412,14 @@ public class DatasetServiceImpl implements DatasetService {
*
* @param target the target
* @param accessionRefs the dataset accessionRefs
* @return
*/
private void copyAccessionRefs(final Dataset target, final List<DatasetAccessionRef> accessionRefs) {
private Dataset copyAccessionRefs(final Dataset target, final List<DatasetAccessionRef> accessionRefs) {
if (accessionRefs == null || accessionRefs.size() == 0) {
return;
return target;
}
Dataset loadedDataset = reloadFromDatabase(target);
Dataset loadedDataset = getDataset(target);
List<DatasetAccessionRef> copiedAccessionRefs = Lists.newArrayList();
accessionRefs.forEach(dAccessionRef -> {
DatasetAccessionRef copy = new DatasetAccessionRef();
......@@ -425,7 +431,7 @@ public class DatasetServiceImpl implements DatasetService {
loadedDataset.setAccessionCount((int) accessionRefRepository.countByDataset(loadedDataset));
LOG.info("Done saving {} accession refs, have {} in dataset", accessionRefs.size(), loadedDataset.getAccessionCount());
datasetRepository.save(loadedDataset);
return datasetRepository.save(loadedDataset);
}
/**
......@@ -498,11 +504,11 @@ public class DatasetServiceImpl implements DatasetService {
@Override
@PostAuthorize("hasRole('ADMINISTRATOR') || returnObject==null || returnObject.isPublished() || hasPermission(returnObject, 'read')")
public Dataset loadDataset(final Dataset input) {
final Dataset dataset = reloadFromDatabase(input);
final Dataset dataset = getDataset(input);
return lazyLoad(dataset);
}
private Dataset reloadFromDatabase(final Dataset input) {
private Dataset getDataset(final Dataset input) {
LOG.debug("Load Dataset. Input data {}", input);
final Dataset dataset = datasetRepository.findOne(input.getId());
......@@ -589,7 +595,7 @@ public class DatasetServiceImpl implements DatasetService {
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') || #dataset.published || hasPermission(#dataset, 'read')")
public Page<? extends AccessionRef> listAccessions(Dataset dataset, final Pageable page) {
dataset = reloadFromDatabase(dataset);
dataset = getDataset(dataset);
return accessionRefRepository.findAll(dataset, page);
}
......@@ -719,7 +725,7 @@ public class DatasetServiceImpl implements DatasetService {
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'delete')")
public Dataset removeDataset(final Dataset dataset) {
final Dataset loadedDataset = reloadFromDatabase(dataset);
final Dataset loadedDataset = getDataset(dataset);
for (RepositoryFile repositoryFile : loadedDataset.getRepositoryFiles()) {
try {
......@@ -747,7 +753,7 @@ public class DatasetServiceImpl implements DatasetService {
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'write')")
public Dataset addAccessionRefs(final Dataset dataset, @Valid final Set<AccessionRef> accessionRefs) throws NotFoundElement {
final Dataset loadedDataset = reloadFromDatabase(dataset);
final Dataset loadedDataset = getDataset(dataset);
if (loadedDataset == null) {
throw new NotFoundElement("Dataset doesn't exist");
}
......@@ -779,7 +785,7 @@ public class DatasetServiceImpl implements DatasetService {
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR')")
public Dataset approveDataset(final Dataset dataset) {
Dataset loaded = reloadFromDatabase(dataset);
Dataset loaded = getDataset(dataset);
if (loaded == null) {
throw new NotFoundElement("No dataset with specified uuid and version");
}
......@@ -858,7 +864,7 @@ public class DatasetServiceImpl implements DatasetService {
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'write')")
public Dataset reviewDataset(final Dataset dataset) {
final Dataset loaded = reloadFromDatabase(dataset);
final Dataset loaded = getDataset(dataset);
if (loaded == null) {
throw new NotFoundElement("No dataset with specified uuid and version");
}
......@@ -892,7 +898,7 @@ public class DatasetServiceImpl implements DatasetService {
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'administration')")
public Dataset rejectDataset(final Dataset dataset) {
Dataset loaded = reloadFromDatabase(dataset);
Dataset loaded = getDataset(dataset);
if (loaded == null) {
throw new NotFoundElement("No dataset with specified uuid and version");
}
......@@ -957,7 +963,7 @@ public class DatasetServiceImpl implements DatasetService {
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'write')")
public DatasetCreator createDatasetCreator(Dataset dataset, @Valid final DatasetCreator input) throws NotFoundElement {
dataset = reloadFromDatabase(dataset);
dataset = getDataset(dataset);
input.setDataset(dataset);
return datasetCreatorRepository.save(input);
}
......@@ -1021,7 +1027,7 @@ public class DatasetServiceImpl implements DatasetService {
*/
@Override
public List<DatasetCreator> loadDatasetCreators(Dataset dataset) throws NotFoundElement {
dataset = reloadFromDatabase(dataset);
dataset = getDataset(dataset);
return dataset.getCreators();
}
......@@ -1254,7 +1260,7 @@ public class DatasetServiceImpl implements DatasetService {
@Override
public List<DatasetLocation> listLocation(Dataset dataset) throws NotFoundElement {
LOG.info("Load list DatasetLocation for dataset {}", dataset.getUuid());
dataset = reloadFromDatabase(dataset);
dataset = getDataset(dataset);
dataset.getLocations().size();
return dataset.getLocations();
}
......@@ -1308,7 +1314,7 @@ public class DatasetServiceImpl implements DatasetService {
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'write')")
public DatasetLocation updateLocation(Dataset dataset, @Valid final DatasetLocation input) throws NotFoundElement {
dataset = reloadFromDatabase(dataset);
dataset = getDataset(dataset);
final DatasetLocation datasetLocation = loadLocation(input);
if (!datasetLocation.getDataset().getUuid().equals(dataset.getUuid())) {
throw new InvalidApiUsageException("Location does not belong to dataset");
......
......@@ -33,7 +33,6 @@ import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.SelfCleaning;
import org.genesys2.server.model.impl.AccessionIdentifier3;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
/**
......@@ -86,10 +85,6 @@ public class AccessionRef implements SelfCleaning, AccessionIdentifier3, Seriali
@JoinColumn(name = "accessionId", nullable = true)
@JsonView({ JsonViews.Public.class })
protected Accession accession;
@Transient
@JsonIgnore
private boolean stored;
public AccessionRef() {
}
......@@ -321,15 +316,6 @@ public class AccessionRef implements SelfCleaning, AccessionIdentifier3, Seriali
return true;
}
@Transient
public boolean isStored() {
return stored;
}
public void setStored(boolean stored) {
this.stored = stored;
}
@Override
public String toString() {
return "AccessionRef " + instCode + " " + acceNumb + " " + genus;
......
......@@ -747,10 +747,11 @@ public class SubsetServiceImpl implements SubsetService {
*
* @param target the target
* @param accessionRefs the subset accessionRefs
* @return
*/
private void copyAccessionRefs(final Subset target, final List<SubsetAccessionRef> accessionRefs) {
private Subset copyAccessionRefs(final Subset target, final List<SubsetAccessionRef> accessionRefs) {
if (accessionRefs == null || accessionRefs.size() == 0) {
return;
return target;
}
final Subset loadedSubset = getSubset(target);
......@@ -765,7 +766,7 @@ public class SubsetServiceImpl implements SubsetService {
loadedSubset.setAccessionCount((int) accessionRefRepository.countBySubset(loadedSubset));
LOG.info("Done saving {} accession refs, have {} in subset", accessionRefs.size(), loadedSubset.getAccessionCount());
subsetRepository.save(loadedSubset);
return subsetRepository.save(loadedSubset);
}
/**
......
......@@ -156,7 +156,7 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
}
@Test
public void testLookupAccessionsForAccessionIdentifiersInDataset() {
public void testLookupAccessionsForAccessionIdentifiersInDataset() throws InterruptedException {
final Accession accession = upsertAccession(TEST_INSTCODE, "TestAccNum", "TestGen");
Dataset input = buildDataset(DATASET_TITLE_1, DATASET_DESCRIPTION_1, partner, PublishState.DRAFT);
......@@ -168,6 +168,8 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
assertThat(input, not(nullValue()));
assertThat(input.getAccessionCount(), is(1));
datasetService.rematchDatasetAccessions(input);
Thread.sleep(2000);
final AccessionRef datasetAccessionRef = datasetService.listAccessions(input, new PageRequest(0, 10)).getContent().get(0);
assertThat(datasetAccessionRef, notNullValue());
......@@ -176,7 +178,7 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
}
@Test
public void testCreateAndUpdateAccessionsInDataset() {
public void testCreateAndUpdateAccessionsInDataset() throws InterruptedException {
final Accession accession1 = upsertAccession(TEST_INSTCODE, TEST_ACCNUM, TEST_GEN);
final Accession accession2 = upsertAccession(TEST_INSTCODE2, TEST_ACCNUM2, TEST_GEN2);
final Accession accession3 = upsertAccession(TEST_INSTCODE3, TEST_ACCNUM3, TEST_GEN3);
......@@ -192,6 +194,8 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
assertThat(input.getAccessionCount(), is(3));
assertThat(input.getAccessionCount(), is(datasetAccessionRefs.size()));
datasetService.rematchDatasetAccessions(input);
Thread.sleep(5500);
datasetService.listAccessions(input, new PageRequest(0, 10)).forEach(aid -> {
assertThat(aid.getAccession(), notNullValue());
......@@ -845,6 +849,8 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
datasetService.rematchDatasetAccessions(dataset1);
datasetService.rematchDatasetAccessions(dataset2);
Thread.sleep(5500);
List<Dataset> datasets1 = datasetService.listByAccession(accession1);
assertThat(datasets1, not(nullValue()));
assertThat(datasetRepository.count(), is(2l));
......@@ -854,7 +860,7 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
final Accession accession2 = upsertAccession(TEST_INSTCODE, "A8", "Musa");
datasetService.rematchDatasetAccessions(dataset1);
Thread.sleep(5000);
Thread.sleep(5500);
datasets1 = datasetService.listByAccession(accession2);
assertThat(datasets1.size(), is(1));
......
......@@ -16,10 +16,7 @@
package org.genesys.test.server.api.v1;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
......@@ -46,13 +43,14 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.ResultActions;
import com.google.common.collect.Sets;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
/**
* @author Andrey Lugovskoy.
* @author Andrey Lugovskoy
* @author Maxym Borodenko
* @author Matija Obreza
*/
public class DatasetControllerTest extends AbstractApiTest {
......@@ -353,7 +351,7 @@ public class DatasetControllerTest extends AbstractApiTest {
return mockMvc.perform(post(DatasetController.CONTROLLER_URL.concat("/create-new-version"))
.param("uuid", dataset.getUuid().toString())
.contentType(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultHandlers.print())
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
/*@formatter:on*/
......
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