Commit 8990167d authored by Viacheslav Pavlov's avatar Viacheslav Pavlov

Dataset location and timing

parent a67ea924
......@@ -28,6 +28,7 @@ import org.genesys.catalog.model.Partner;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.service.PublishValidationInterface;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys2.util.MCPDUtil;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
......@@ -107,6 +108,14 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Pub
@JsonView({ JsonViews.Public.class })
private List<DatasetLocation> locations;
/** The min start date of all locations */
@Column(length = 8)
private String startDate;
/** The max end date of all locations */
@Column(length = 8)
private String endDate;
/** The accession count. */
// Number of accessions in the {@link #accessionIdentifiers} list
@Column(name = "accessions")
......@@ -182,6 +191,11 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Pub
if (descriptors != null) {
this.descriptorCount = descriptors.size();
}
if (locations != null) {
this.startDate = locations.stream().map(DatasetLocation::getStartDate).min(String::compareTo).orElse(null);
this.endDate = locations.stream().map(DatasetLocation::getEndDate).max(String::compareTo).orElse(null);
}
trimStringsToNull();
}
......@@ -284,6 +298,46 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Pub
this.locations = locations;
}
/**
* Gets the start date.
*
* @return the start date
*/
public String getStartDate() {
return startDate;
}
/**
* Sets the start date if startDate is valid McpdDate.
*
* @param startDate the new start date
*/
public void setStartDate(String startDate) {
if(MCPDUtil.isMcpdDate(startDate)) {
this.startDate = startDate;
}
}
/**
* Gets the end date.
*
* @return the end date
*/
public String getEndDate() {
return endDate;
}
/**
* Sets the end date if endDate is valid McpdDate.
*
* @param endDate the new start date
*/
public void setEndDate(String endDate) {
if(MCPDUtil.isMcpdDate(endDate)) {
this.endDate = endDate;
}
}
/**
* Gets the version tag.
*
......
......@@ -19,6 +19,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
import org.genesys.blocks.model.UuidModel;
import org.genesys.catalog.annotations.PublishValidation;
import org.genesys.catalog.service.PublishValidationInterface;
import org.genesys2.util.MCPDUtil;
import javax.persistence.*;
import java.util.Map;
......@@ -60,6 +61,14 @@ public class DatasetLocation extends UuidModel implements PublishValidationInter
@PublishValidation
private Double decimalLongitude;
/** The start date */
@Column(length = 8)
private String startDate;
/** The end date */
@Column(length = 8)
private String endDate;
/** The dataset. */
@ManyToOne(cascade = { CascadeType.REFRESH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.DETACH }, optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "datasetId")
......@@ -198,6 +207,46 @@ public class DatasetLocation extends UuidModel implements PublishValidationInter
this.decimalLongitude = decimalLongitude;
}
/**
* Gets the start date.
*
* @return the start date
*/
public String getStartDate() {
return startDate;
}
/**
* Sets the start date if startDate is valid McpdDate.
*
* @param startDate the new start date
*/
public void setStartDate(String startDate) {
if(MCPDUtil.isMcpdDate(startDate)) {
this.startDate = startDate;
}
}
/**
* Gets the end date.
*
* @return the end date
*/
public String getEndDate() {
return endDate;
}
/**
* Sets the end date if endDate is valid McpdDate.
*
* @param endDate the new start date
*/
public void setEndDate(String endDate) {
if(MCPDUtil.isMcpdDate(endDate)) {
this.endDate = endDate;
}
}
/*
* (non-Javadoc)
* @see org.genesys.catalog.service.PublishValidationInterface#validation()
......
......@@ -517,6 +517,8 @@ public class DatasetServiceImpl implements DatasetService {
target.setOwner(source.getOwner());
target.setVersionTag(source.getVersionTag());
target.setCreated(source.getCreated());
target.setStartDate(source.getStartDate());
target.setEndDate(source.getEndDate());
if (source.getCrops() != null) {
if (target.getCrops() == null) {
......@@ -525,6 +527,16 @@ public class DatasetServiceImpl implements DatasetService {
target.getCrops().clear();
target.getCrops().addAll(source.getCrops());
}
if (source.getLocations() != null) {
if (target.getLocations() == null) {
target.setLocations(new ArrayList<>());
}
if(target != source) {
target.getLocations().clear();
target.getLocations().addAll(source.getLocations());
}
}
// target.setDescriptors(source.getDescriptors());
}
......
......@@ -62,7 +62,14 @@ public class LocationServiceImpl implements LocationService {
dataset = datasetService.loadDataset(dataset);
LOG.info("Create DatasetLocation {} for dataset {}", input, dataset.getUuid());
input.setDataset(dataset);
return locationRepository.save(input);
DatasetLocation saved = locationRepository.save(input);
dataset.setStartDate(saved.getStartDate());
dataset.setEndDate(saved.getEndDate());
dataset.getLocations().add(saved);
datasetService.updateDataset(dataset);
return saved;
}
/**
......@@ -79,6 +86,9 @@ public class LocationServiceImpl implements LocationService {
throw new InvalidApiUsageException("Location does not belong to dataset");
}
dataset.getLocations().remove(datasetLocation);
dataset.setStartDate("--------");
dataset.setEndDate("--------");
datasetService.updateDataset(dataset);
return datasetLocation;
}
......@@ -148,7 +158,15 @@ public class LocationServiceImpl implements LocationService {
throw new InvalidApiUsageException("Location does not belong to dataset");
}
copyValue(datasetLocation, input);
return locationRepository.save(datasetLocation);
DatasetLocation saved = locationRepository.save(datasetLocation);
dataset.getLocations().remove(input);
dataset.getLocations().add(saved);
dataset.setStartDate(saved.getStartDate());
dataset.setEndDate(saved.getEndDate());
datasetService.updateDataset(dataset);
return saved;
}
/**
......@@ -164,5 +182,7 @@ public class LocationServiceImpl implements LocationService {
target.setVerbatimLocality(source.getVerbatimLocality());
target.setDecimalLatitude(source.getDecimalLatitude());
target.setDecimalLongitude(source.getDecimalLongitude());
target.setStartDate(source.getStartDate());
target.setEndDate(source.getEndDate());
}
}
......@@ -26,7 +26,7 @@ import org.apache.commons.lang3.StringUtils;
public class MCPDUtil {
static Pattern mcpdSplit = Pattern.compile("\\s*;\\s*");
public static final Pattern MCPDDATE_PATTERN = Pattern.compile("^[1-9]\\d{3}(\\d|-){4}$");
public static final Pattern MCPDDATE_PATTERN = Pattern.compile("^(?:(?:[1-9]\\d{3})|(?:----)|(?:0000))(?:--|(?:0[0-9])|(?:1[0-2]))(?:--|(?:0[0-9])|(?:[1-2][0-9])|(?:3[0-1]))$");
public static final Pattern WIEWSCODE_PATTERN = Pattern.compile("^\\p{Upper}{3}(\\d){3,4}$");
......
......@@ -3566,6 +3566,31 @@ databaseChangeLog:
sql: >-
CREATE INDEX UK_1my8xep8hi5fv42o3ivu0t41o ON short_filter(json(250))
- changeSet:
id: 1533223466241-1
author: vpavlov
comment: Added Dataset location timing
changes:
- addColumn:
tableName: dataset
columns:
- column:
name: startDate
type: varchar(8)
- column:
name: endDate
type: varchar(8)
- addColumn:
tableName: dataset_location
columns:
- column:
name: startDate
type: varchar(8)
- column:
name: endDate
type: varchar(8)
- changeSet:
id: 1533138107794-1
author: mborodenko
......
......@@ -28,6 +28,7 @@ import org.genesys.catalog.persistence.dataset.DatasetRepository;
import org.genesys.catalog.persistence.dataset.DatasetVersionsRepository;
import org.genesys.catalog.service.DatasetCreatorService;
import org.genesys.catalog.service.DatasetService;
import org.genesys.catalog.service.LocationService;
import org.genesys.filerepository.persistence.RepositoryFilePersistence;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionId;
......@@ -67,6 +68,8 @@ public abstract class AbstractDatasetServiceTest extends CatalogServiceTest {
private InstituteService instituteService;
@Autowired
private TaxonomyService taxonomyService;
@Autowired
protected LocationService locationService;
public AbstractDatasetServiceTest() {
super();
......
......@@ -20,6 +20,7 @@ import java.net.URL;
import java.util.Set;
import org.genesys.catalog.model.Partner;
import org.genesys.catalog.model.dataset.DatasetLocation;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.model.traits.Descriptor.Category;
import org.genesys.catalog.model.traits.DescriptorList;
......@@ -135,4 +136,17 @@ public abstract class CatalogServiceTest extends AbstractServiceTest {
protected Descriptor setupDescriptor(final Partner owner, final String title, final String versionTag, final Descriptor.DataType type) {
return descriptorService.createDescriptor(createDescriptor(owner, title, versionTag, type));
}
protected DatasetLocation createDatasetLocation(final String userCountry, final String mapCountry, final String stateProvince, final String verbatimLocality, final Double decimalLatitude, final Double decimalLongitude, final String startDate, final String endDate){
final DatasetLocation input = new DatasetLocation();
input.setUserCountry(userCountry);
input.setMapCountry(mapCountry);
input.setStateProvince(stateProvince);
input.setDecimalLatitude(decimalLatitude);
input.setDecimalLongitude(decimalLongitude);
input.setStartDate(startDate);
input.setEndDate(endDate);
return input;
}
}
......@@ -16,6 +16,7 @@
package org.genesys.test.catalog.services;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import java.io.File;
......@@ -34,6 +35,7 @@ import org.genesys.catalog.exceptions.InvalidApiUsageException;
import org.genesys.catalog.exceptions.NotFoundElement;
import org.genesys.catalog.model.dataset.AccessionIdentifier;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.dataset.DatasetLocation;
import org.genesys.catalog.model.filters.AccessionIdentifierFilter;
import org.genesys.catalog.model.filters.DatasetFilter;
import org.genesys.catalog.model.traits.Descriptor;
......@@ -601,4 +603,90 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
input = datasetService.updateDataset(input);
assertThat(input.getOwner(), not(partner2));
}
@Test
public void testAddLocationToDataset(){
DatasetLocation savedLocation = createDatasetLocation("testCountry", "testMapCountry", "testStateProvince", "testVerbatimLocality", 10.0, 20.0, "20000101", "20010101");
Dataset savedDataset = buildAndSaveDataset(DATASET_TITLE_1, DATASET_DESCRIPTION_1, partner, false);
savedLocation = locationService.createLocation(savedDataset, savedLocation);
Dataset loadedDataset = datasetService.loadDataset(savedDataset.getUuid());
assertThat(loadedDataset.getLocations(), hasSize(1));
assertEquals(savedLocation.getStartDate(), loadedDataset.getStartDate());
assertEquals(savedLocation.getEndDate(), loadedDataset.getEndDate());
}
@Test
public void testAddMultipleLocationsToDataset(){
DatasetLocation savedLocation1 = createDatasetLocation("testCountry1", "testMapCountry1", "testStateProvince1", "testVerbatimLocality1", 10.0, 20.0, "20010101", "20110101");
DatasetLocation savedLocation2 = createDatasetLocation("testCountry2", "testMapCountry2", "testStateProvince2", "testVerbatimLocality2", 11.0, 21.0, "20020202", "20120202");
DatasetLocation savedLocation3 = createDatasetLocation("testCountry3", "testMapCountry3", "testStateProvince3", "testVerbatimLocality3", 12.0, 22.0, "20030303", "20130303");
Dataset savedDataset = buildAndSaveDataset(DATASET_TITLE_1, DATASET_DESCRIPTION_1, partner, false);
savedLocation1 = locationService.createLocation(savedDataset, savedLocation1);
savedDataset = datasetService.loadDataset(savedDataset.getUuid());
savedLocation2 = locationService.createLocation(savedDataset, savedLocation2);
savedDataset = datasetService.loadDataset(savedDataset.getUuid());
savedLocation3 = locationService.createLocation(savedDataset, savedLocation3);
Dataset loadedDataset = datasetService.loadDataset(savedDataset.getUuid());
assertThat(loadedDataset.getLocations(), hasSize(3));
assertEquals(savedLocation1.getStartDate(), loadedDataset.getStartDate());
assertEquals(savedLocation3.getEndDate(), loadedDataset.getEndDate());
}
@Test
public void testUpdateLocation(){
DatasetLocation savedLocation = createDatasetLocation("testCountry", "testMapCountry", "testStateProvince", "testVerbatimLocality", 10.0, 20.0, "20000101", "20010101");
Dataset savedDataset = buildAndSaveDataset(DATASET_TITLE_1, DATASET_DESCRIPTION_1, partner, false);
savedLocation = locationService.createLocation(savedDataset, savedLocation);
Dataset loadedDataset = datasetService.loadDataset(savedDataset.getUuid());
assertThat(loadedDataset.getLocations(), hasSize(1));
assertEquals(savedLocation.getStartDate(), loadedDataset.getStartDate());
assertEquals(savedLocation.getEndDate(), loadedDataset.getEndDate());
savedLocation.setEndDate("20120011");
savedLocation.setStartDate("2002----");
savedLocation = locationService.updateLocation(loadedDataset, savedLocation);
loadedDataset = datasetService.loadDataset(savedDataset.getUuid());
assertThat(loadedDataset.getLocations(), hasSize(1));
assertEquals(savedLocation.getStartDate(), loadedDataset.getStartDate());
assertEquals(savedLocation.getEndDate(), loadedDataset.getEndDate());
}
@Test
public void testRemoveLocationFromDataset(){
DatasetLocation savedLocation1 = createDatasetLocation("testCountry1", "testMapCountry1", "testStateProvince1", "testVerbatimLocality1", 10.0, 20.0, "20010101", "20110101");
DatasetLocation savedLocation2 = createDatasetLocation("testCountry2", "testMapCountry2", "testStateProvince2", "testVerbatimLocality2", 11.0, 21.0, "20020202", "20120202");
Dataset savedDataset = buildAndSaveDataset(DATASET_TITLE_1, DATASET_DESCRIPTION_1, partner, false);
savedLocation1 = locationService.createLocation(savedDataset, savedLocation1);
savedDataset = datasetService.loadDataset(savedDataset.getUuid());
savedLocation2 = locationService.createLocation(savedDataset, savedLocation2);
Dataset loadedDataset = datasetService.loadDataset(savedDataset.getUuid());
assertThat(loadedDataset.getLocations(), hasSize(2));
assertEquals(savedLocation1.getStartDate(), loadedDataset.getStartDate());
assertEquals(savedLocation2.getEndDate(), loadedDataset.getEndDate());
savedLocation1 = locationService.removeLocation(loadedDataset, savedLocation1);
loadedDataset = datasetService.loadDataset(savedDataset.getUuid());
assertThat(loadedDataset.getLocations(), hasSize(1));
assertThat(loadedDataset.getLocations(), contains(savedLocation2));
assertEquals(savedLocation2.getStartDate(), loadedDataset.getStartDate());
assertEquals(savedLocation2.getEndDate(), loadedDataset.getEndDate());
}
}
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