Commit 507811fc authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Matija Obreza

Added copyAccessionRefs, copyCreators and copyLocations methods;

- Updated tests;
- Incremented versionTag using VersionManager
- Don't copy stored field
parent 7ea4e698
......@@ -57,6 +57,7 @@ import org.genesys.catalog.persistence.dataset.DatasetRepository;
import org.genesys.catalog.persistence.dataset.DatasetVersionsRepository;
import org.genesys.catalog.service.DatasetService;
import org.genesys.catalog.service.DescriptorService;
import org.genesys.catalog.service.VersionManager;
import org.genesys.filerepository.FolderNotEmptyException;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
......@@ -167,6 +168,9 @@ public class DatasetServiceImpl implements DatasetService {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private VersionManager versionManager;
/**
* {@inheritDoc}
*/
......@@ -362,41 +366,30 @@ public class DatasetServiceImpl implements DatasetService {
@Transactional
@PreAuthorize("(hasRole('ADMINISTRATOR') || hasPermission(#source, 'write')) && #source.published")
public Dataset createNewVersion(@Valid Dataset source) {
source = loadDataset(source);
source = reloadFromDatabase(source);
final Dataset dataset = new Dataset();
copyValues(dataset, source);
dataset.setVersionTag(versionManager.nextMajor(source.getVersionTag()));
dataset.setState(PublishState.DRAFT);
dataset.setCurrent(null);
dataset.setUuid(null);
dataset.setVersions(source.getVersions());
// Copy descriptors
dataset.setDescriptors(new ArrayList<>());
copyDescriptors(dataset, source.getDescriptors());
final Dataset saved = lazyLoad(datasetRepository.save(dataset));
setAccessionRefs(saved, new HashSet<>(source.getAccessionRefs()));
final Dataset saved = datasetRepository.save(dataset);
// copy accessionRefs
copyAccessionRefs(saved, 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));
});
copyCreators(saved, source.getCreators());
// 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));
});
copyLocations(saved, source.getLocations());
saved.setCurrentVersion(source.getUuid());
......@@ -406,7 +399,75 @@ public class DatasetServiceImpl implements DatasetService {
// Make dataset folder
makeDatasetFolder(saved.getUuid());
return lazyLoad(saved);
return saved;
}
/**
* Copy and save dataset accessionRefs.
*
* @param target the target
* @param accessionRefs the dataset accessionRefs
*/
private void copyAccessionRefs(final Dataset target, final List<DatasetAccessionRef> accessionRefs) {
if (accessionRefs == null || accessionRefs.size() == 0) {
return;
}
Dataset loadedDataset = reloadFromDatabase(target);
List<DatasetAccessionRef> copiedAccessionRefs = Lists.newArrayList();
accessionRefs.forEach(dAccessionRef -> {
DatasetAccessionRef copy = new DatasetAccessionRef();
copyAccessionRef(copy, dAccessionRef);
copy.setDataset(loadedDataset);
copiedAccessionRefs.add(copy);
});
accessionRefRepository.save(copiedAccessionRefs);
loadedDataset.setAccessionCount((int) accessionRefRepository.countByDataset(loadedDataset));
LOG.info("Done saving {} accession refs, have {} in dataset", accessionRefs.size(), loadedDataset.getAccessionCount());
datasetRepository.save(loadedDataset);
}
/**
* Copy and save dataset locations.
*
* @param target the target
* @param locations the dataset locations
*/
private void copyLocations(final Dataset target, final List<DatasetLocation> locations) {
if (locations == null || locations.size() == 0) {
return;
}
List<DatasetLocation> copiedLocations = Lists.newArrayList();
locations.forEach(location -> {
DatasetLocation copy = new DatasetLocation();
copyLocation(copy, location);
copy.setDataset(target);
copiedLocations.add(copy);
});
target.setLocations(locationRepository.save(copiedLocations));
}
/**
* Copy and save dataset creators.
*
* @param target the target
* @param creators the dataset creators
*/
private void copyCreators(final Dataset target, final List<DatasetCreator> creators) {
if (creators == null || creators.size() == 0) {
return;
}
List<DatasetCreator> copiedCreators = Lists.newArrayList();
creators.forEach(creator -> {
DatasetCreator copy = new DatasetCreator();
copyCreator(copy, creator);
copy.setDataset(target);
copiedCreators.add(copy);
});
target.setCreators(datasetCreatorRepository.save(copiedCreators));
}
private void makeDatasetFolder(final UUID uuid) {
......@@ -975,7 +1036,7 @@ public class DatasetServiceImpl implements DatasetService {
if (!datasetCreator.getDataset().getUuid().equals(dataset.getUuid())) {
throw new InvalidApiUsageException("Creator does not belong to dataset");
}
copyCreatorValue(datasetCreator, input);
copyCreator(datasetCreator, input);
return datasetCreatorRepository.save(datasetCreator);
}
......@@ -991,12 +1052,12 @@ public class DatasetServiceImpl implements DatasetService {
}
/**
* Copy creator value.
* Copy creator values.
*
* @param target the target
* @param source the source
*/
protected void copyCreatorValue(final DatasetCreator target, final DatasetCreator source) {
private void copyCreator(final DatasetCreator target, final DatasetCreator source) {
target.setFullName(source.getFullName());
target.setEmail(source.getEmail());
target.setPhoneNumber(source.getPhoneNumber());
......@@ -1006,6 +1067,40 @@ public class DatasetServiceImpl implements DatasetService {
target.setRole(source.getRole());
}
/**
* Copy location values.
*
* @param target the target
* @param source the source
*/
private void copyLocation(final DatasetLocation target, final DatasetLocation source) {
target.setUserCountry(source.getUserCountry());
target.setMapCountry(source.getMapCountry());
target.setStateProvince(source.getStateProvince());
target.setVerbatimLocality(source.getVerbatimLocality());
target.setDecimalLatitude(source.getDecimalLatitude());
target.setDecimalLongitude(source.getDecimalLongitude());
target.setCountryCode(source.getCountryCode());
target.setStartDate(source.getStartDate());
target.setEndDate(source.getEndDate());
target.setDescription(source.getDescription());
}
/**
* Copy dataset accessionRef values.
*
* @param target the target
* @param source the source
*/
private void copyAccessionRef(final DatasetAccessionRef target, final DatasetAccessionRef source) {
target.setDoi(source.getDoi());
target.setInstCode(source.getInstCode());
target.setAcceNumb(source.getAcceNumb());
target.setGenus(source.getGenus());
target.setSpecies(source.getSpecies());
target.setAccession(source.getAccession());
}
/**
* Copy values.
*
......
......@@ -20,9 +20,7 @@ import static org.genesys2.server.model.impl.QSubsetCreator.subsetCreator;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
......@@ -728,23 +726,97 @@ public class SubsetServiceImpl implements SubsetService {
subset.setCurrent(null);
subset.setUuid(null);
subset.setVersions(source.getVersions());
Subset saved = lazyLoad(subsetRepository.save(subset));
setAccessionRefs(saved, new HashSet<>(source.getAccessionRefs()));
saved.setCreators(new ArrayList<>());
Subset saved = subsetRepository.save(subset);
// Copy accessionRefs
copyAccessionRefs(saved, source.getAccessionRefs());
// Copy creators
source.getCreators().forEach(creator -> {
entityManager.detach(creator);
creator.setSubset(subset);
creator.setId(null);
creator.setVersion(null);
creator.setUuid(null);
saved.getCreators().add(subsetCreatorRepository.save(creator));
});
copyCreators(saved, source.getCreators());
saved.setCurrentVersion(source.getUuid());
// Make Subset publicly not-readable
aclService.makePubliclyReadable(saved, false);
return lazyLoad(saved);
return saved;
}
/**
* Copy and save subset accessionRefs.
*
* @param target the target
* @param accessionRefs the subset accessionRefs
*/
private void copyAccessionRefs(final Subset target, final List<SubsetAccessionRef> accessionRefs) {
if (accessionRefs == null || accessionRefs.size() == 0) {
return;
}
final Subset loadedSubset = getSubset(target);
List<SubsetAccessionRef> copiedAccessionRefs = Lists.newArrayList();
accessionRefs.forEach(sAccessionRef -> {
SubsetAccessionRef copy = new SubsetAccessionRef();
copyAccessionRef(copy, sAccessionRef);
copy.setSubset(loadedSubset);
copiedAccessionRefs.add(copy);
});
accessionRefRepository.save(copiedAccessionRefs);
loadedSubset.setAccessionCount((int) accessionRefRepository.countBySubset(loadedSubset));
LOG.info("Done saving {} accession refs, have {} in subset", accessionRefs.size(), loadedSubset.getAccessionCount());
subsetRepository.save(loadedSubset);
}
/**
* Copy and save subset creators.
*
* @param target the target
* @param creators the subset creators
*/
private void copyCreators(final Subset target, final List<SubsetCreator> creators) {
if (creators == null || creators.size() == 0) {
return;
}
List<SubsetCreator> copiedCreators = Lists.newArrayList();
creators.forEach(creator -> {
SubsetCreator copy = new SubsetCreator();
copyCreator(copy, creator);
copy.setSubset(target);
copiedCreators.add(copy);
});
target.setCreators(subsetCreatorRepository.save(copiedCreators));
}
/**
* Copy creator values.
*
* @param target the target
* @param source the source
*/
private void copyCreator(final SubsetCreator target, final SubsetCreator source) {
target.setFullName(source.getFullName());
target.setEmail(source.getEmail());
target.setPhoneNumber(source.getPhoneNumber());
target.setFax(source.getFax());
target.setInstituteAddress(source.getInstituteAddress());
target.setInstitutionalAffiliation(source.getInstitutionalAffiliation());
target.setRole(source.getRole());
}
/**
* Copy subset accessionRef values.
*
* @param target the target
* @param source the source
*/
private void copyAccessionRef(final SubsetAccessionRef target, final SubsetAccessionRef source) {
target.setDoi(source.getDoi());
target.setInstCode(source.getInstCode());
target.setAcceNumb(source.getAcceNumb());
target.setGenus(source.getGenus());
target.setSpecies(source.getSpecies());
target.setAccession(source.getAccession());
}
}
......@@ -21,6 +21,7 @@ import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.docu
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import com.fasterxml.jackson.annotation.JsonInclude;
......@@ -30,6 +31,7 @@ import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.dataset.DatasetCreator;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.persistence.PartnerRepository;
import org.genesys.catalog.persistence.dataset.DatasetAccessionRefRepository;
import org.genesys.catalog.persistence.dataset.DatasetCreatorRepository;
import org.genesys.catalog.persistence.dataset.DatasetRepository;
import org.genesys.catalog.persistence.dataset.DatasetVersionsRepository;
......@@ -39,6 +41,7 @@ import org.genesys.catalog.service.ShortFilterService;
import org.genesys.filerepository.persistence.RepositoryFilePersistence;
import org.genesys.test.config.ApplicationConfig;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.genesys.AccessionRef;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.model.impl.SubsetCreator;
import org.genesys2.server.persistence.FaoInstituteRepository;
......@@ -97,6 +100,8 @@ public abstract class AbstractApiTest extends AbstractTest {
private FaoInstituteRepository instituteRepository;
@Autowired
protected RepositoryFilePersistence repositoryFilePersistence;
@Autowired
protected DatasetAccessionRefRepository accessionRefRepository;
protected MockMvc mockMvc;
@Rule
......@@ -119,6 +124,7 @@ public abstract class AbstractApiTest extends AbstractTest {
@Override
@Transactional
public void cleanup() throws Exception {
accessionRefRepository.deleteAll();
datasetVersionsRepository.deleteAll();
datasetCreatorRepository.deleteAll();
datasetRepository.deleteAll();
......@@ -137,6 +143,7 @@ public abstract class AbstractApiTest extends AbstractTest {
input.setTitle(title);
input.setDescription(description);
input.setOwner(owner);
input.setVersionTag("1.0");
// input.setAccessions(accessions);
return input;
}
......@@ -157,6 +164,14 @@ public abstract class AbstractApiTest extends AbstractTest {
return dataset;
}
protected Set<AccessionRef> generateAccessionRefs(final int size) {
final Set<AccessionRef> accessionRefs = new HashSet<>(size);
for (int i=0; i<size; i++) {
accessionRefs.add(new AccessionRef("InstCode", "TestAccNum" + i, "TestGen", null));
}
return accessionRefs;
}
protected DatasetCreator buildDatasetCreator(final String fullName, final String email, final String phoneNumber, final Dataset dataset) {
final DatasetCreator datasetCreator = new DatasetCreator();
datasetCreator.setDataset(dataset);
......
......@@ -90,6 +90,7 @@ public abstract class AbstractDatasetServiceTest extends CatalogServiceTest {
input.setDescription(datasetDescription);
input.setOwner(partner);
input.setState(state);
input.setVersionTag("1.0");
return input;
}
......
......@@ -149,10 +149,11 @@ public class DatasetControllerTest extends AbstractApiTest {
Dataset dataset = datasetService.createDataset(setUpDataset());
assertEquals(dataset.getState(), PublishState.DRAFT);
// add accessions
final Set<AccessionRef> accessionRefs = Sets.newHashSet(new AccessionRef("InstCode", "TestAccNum", "TestGen", null));
// add 5 accessions
final int accessionRefsCount = 5;
final Set<AccessionRef> accessionRefs = generateAccessionRefs(accessionRefsCount);
dataset = datasetService.addAccessionRefs(dataset, accessionRefs);
assertEquals(dataset.getAccessionCount(), 1);
assertEquals(accessionRefsCount, dataset.getAccessionCount());
// add dataset creator
final DatasetCreator datasetCreator = buildDatasetCreator("TestFullName", "TestEmailName", "0673579007", dataset);
......@@ -174,7 +175,7 @@ public class DatasetControllerTest extends AbstractApiTest {
.andExpect(jsonPath("$.owner.shortName", is(dataset.getOwner().getShortName())))
.andExpect(jsonPath("$.current", is(nullValue())))
.andExpect(jsonPath("$.currentVersion", is(dataset.getUuid().toString())))
.andExpect(jsonPath("$.accessionCount", is(1)))
.andExpect(jsonPath("$.accessionCount", is(accessionRefsCount)))
.andExpect(jsonPath("$.descriptorCount", is(1)))
.andExpect(jsonPath("$.state", is(PublishState.DRAFT.toString())))
.andReturn().getResponse().getContentAsString();
......@@ -190,6 +191,8 @@ public class DatasetControllerTest extends AbstractApiTest {
Page<DatasetCreator> res = datasetCreatorRepository.listByUUidOfDataset(new PageRequest(0, 5), result.getUuid());
// the new version of the dataset must contain the same creators
assertEquals(res.getTotalElements(), 1);
assertEquals(accessionRefsCount, accessionRefRepository.countByDataset(result));
}
/**
......
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