Commit 3d4bc921 authored by Matija Obreza's avatar Matija Obreza
Browse files

Create a copy of Descriptor with new version

parent b0179559
......@@ -226,19 +226,19 @@
</exclusions>
</dependency>
<!--OpenCSV-->
<!--OpenCSV -->
<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>2.3</version>
</dependency>
<!-- Apache POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<!-- Apache POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<!-- Test dependencies -->
<dependency>
......@@ -302,6 +302,12 @@
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>semver4j</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.genesys-pgr</groupId>
<artifactId>application-blocks-core</artifactId>
......@@ -326,7 +332,7 @@
</exclusions>
</dependency>
<!--file repository-->
<!--file repository -->
<dependency>
<groupId>org.genesys-pgr</groupId>
<artifactId>file-repository-core</artifactId>
......
......@@ -25,6 +25,7 @@ import org.genesys.catalog.model.traits.DescriptorList;
import org.genesys.catalog.service.filters.DescriptorFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.method.P;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreAuthorize;
......@@ -157,4 +158,14 @@ public interface DescriptorService {
@PostFilter("filterObject==null || filterObject.published || hasPermission(filterObject, 'write')")
List<Dataset> getDatasets(Descriptor descriptor);
/**
* Create next version of the descriptor.
*
* @param descriptor the source descriptor
* @param major if a new major version should be created
* @return a new descriptor entity with updated versions
*/
@PreAuthorize("#descriptor.published || hasPermission(#descriptor, 'read')")
Descriptor nextVersion(@P("descriptor") Descriptor descriptor, boolean major);
}
/*
* Copyright 2018 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.genesys.catalog.service;
/**
* The Version Manager
*/
public interface VersionManager {
/**
* Next major version
*
* @param versionTag the version tag
* @return the string
*/
String nextMajor(String versionTag);
/**
* Next minor version
*
* @param versionTag the version tag
* @return the string
*/
String nextMinor(String versionTag);
/**
* Next.
*
* @param versionTag the version tag
* @param major true for next major version, false for next minor
* @return the string
*/
String next(String versionTag, boolean major);
}
......@@ -35,6 +35,7 @@ import org.genesys.catalog.model.vocab.VocabularyTerm;
import org.genesys.catalog.persistence.traits.DescriptorRepository;
import org.genesys.catalog.persistence.vocab.VocabularyTermRepository;
import org.genesys.catalog.service.DescriptorService;
import org.genesys.catalog.service.VersionManager;
import org.genesys.catalog.service.filters.DescriptorFilter;
import org.genesys.catalog.util.Utils;
import org.genesys.common.model.Partner;
......@@ -76,6 +77,9 @@ public class DescriptorServiceImpl implements DescriptorService {
@Autowired
private EntityManager entityManager;
@Autowired
private VersionManager versionManager;
/**
* {@inheritDoc}
*/
......@@ -350,4 +354,18 @@ public class DescriptorServiceImpl implements DescriptorService {
list.forEach(d -> entityManager.detach(d));
return list;
}
@Override
@Transactional
public Descriptor nextVersion(Descriptor descriptor, boolean major) {
Descriptor source = reloadDescriptor(descriptor);
LOG.info("Creating new version for descriptor uuid={} id={}", descriptor.getUuid(), descriptor.getId());
Descriptor copy = new Descriptor();
copy.apply(source);
copy.setUuid(null);
copy.setPublished(false);
copy.setVersionTag(versionManager.next(descriptor.getVersionTag(), major));
copy.setOwner(descriptor.getOwner());
return lazyLoad(descriptorRepository.save(copy));
}
}
/*
* Copyright 2018 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.genesys.catalog.service.impl;
import java.util.Calendar;
import org.genesys.catalog.service.VersionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.vdurmont.semver4j.Semver;
import com.vdurmont.semver4j.Semver.SemverType;
/**
* Manage version updates
*/
@Component
public class VersionManagerImpl implements VersionManager {
private static final Logger LOG = LoggerFactory.getLogger(VersionManagerImpl.class);
@Override
public String next(String versionTag, boolean major) {
return major ? nextMajor(versionTag) : nextMinor(versionTag);
}
@Override
public String nextMajor(String versionTag) {
Semver version = new Semver(versionTag, SemverType.LOOSE);
LOG.debug("Source version {} = {}", versionTag, version.getValue());
if (version.getMajor() > 1000) {
// likely a year
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
if (currentYear == version.getMajor()) {
if (version.getMinor() == null) {
return currentYear + ".1";
} else {
version = version.nextMinor();
}
} else {
return "" + currentYear;
}
} else {
version = version.nextMajor();
}
LOG.debug("Next version {} -> {}", versionTag, version.getValue());
return version.getValue();
}
@Override
public String nextMinor(String versionTag) {
Semver version = new Semver(versionTag, SemverType.LOOSE);
LOG.debug("Source version {} = {}", versionTag, version.getValue());
version = version.nextMinor();
LOG.debug("Next version {} -> {}", versionTag, version.getValue());
return version.getValue();
}
}
......@@ -475,4 +475,23 @@ public class DescriptorServiceTest extends CatalogServiceTest {
paged = descriptorService.listDescriptors(descriptorFilter, page);
assertThat(paged.getContent(), hasSize(0));
}
@Test
public void newDescriptorVersion() {
Descriptor input = setupDescriptor(null, DESCRIPTOR_TITLE_1, Category.PASSPORT, DataType.BOOLEAN, VERSION_1_0, false);
Descriptor result = descriptorService.createDescriptor(input);
assertThat(result.getVersionTag(), is(VERSION_1_0));
Descriptor newVersion = descriptorService.nextVersion(result, false);
assertThat(newVersion.getUuid(), not(result.getUuid()));
assertThat(newVersion.getVersionTag(), is("1.1"));
newVersion = descriptorService.nextVersion(result, true);
assertThat(newVersion.getUuid(), not(result.getUuid()));
assertThat(newVersion.getVersionTag(), is("2.0"));
newVersion = descriptorService.nextVersion(newVersion, false);
assertThat(newVersion.getVersionTag(), is("2.1"));
}
}
/*
* Copyright 2017 Global Crop Diversity Trust
* Copyright 2018 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.
......@@ -110,6 +110,18 @@ public class DescriptorController {
return descriptorService.upsertDescriptors(source);
}
/**
* Make a new version of a descriptor
*
* @param major make a new major version
* @param source the descriptor to copy
* @return the descriptor
*/
@RequestMapping(value = "/copy", method = RequestMethod.POST)
public Descriptor copyDescriptor(@RequestParam(name = "major", defaultValue = "true", required = false) boolean major, @RequestBody final Descriptor source) {
return descriptorService.nextVersion(source, major);
}
/**
* Delete descriptor.
*
......
Supports Markdown
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