diff --git a/src/main/java/org/genesys/catalog/model/dataset/Dataset.java b/src/main/java/org/genesys/catalog/model/dataset/Dataset.java index c403601c0fb9b3a9739ba93c6d5bb27fd233dc04..5fc24d3d19eda6ba353d50ef8d84b85542e36450 100644 --- a/src/main/java/org/genesys/catalog/model/dataset/Dataset.java +++ b/src/main/java/org/genesys/catalog/model/dataset/Dataset.java @@ -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 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. * diff --git a/src/main/java/org/genesys/catalog/model/dataset/DatasetLocation.java b/src/main/java/org/genesys/catalog/model/dataset/DatasetLocation.java index 4bea62ca8caa5a1c551cf10adb65ade3e280bf74..9eafa6fb0733091283fc7d885aed530170443e2c 100644 --- a/src/main/java/org/genesys/catalog/model/dataset/DatasetLocation.java +++ b/src/main/java/org/genesys/catalog/model/dataset/DatasetLocation.java @@ -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() diff --git a/src/main/java/org/genesys/catalog/service/impl/DatasetServiceImpl.java b/src/main/java/org/genesys/catalog/service/impl/DatasetServiceImpl.java index dcc37ad078e3c1170052d35c57c72675f3c5b818..896368b2211643110431e719731265c956c7c94f 100644 --- a/src/main/java/org/genesys/catalog/service/impl/DatasetServiceImpl.java +++ b/src/main/java/org/genesys/catalog/service/impl/DatasetServiceImpl.java @@ -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()); } diff --git a/src/main/java/org/genesys/catalog/service/impl/LocationServiceImpl.java b/src/main/java/org/genesys/catalog/service/impl/LocationServiceImpl.java index 7e91cce82aa6b85b67d899610f8c4215ae2dfe1f..733f8541f46903cfaa8bf6b6765a8baf9fbfcfec 100644 --- a/src/main/java/org/genesys/catalog/service/impl/LocationServiceImpl.java +++ b/src/main/java/org/genesys/catalog/service/impl/LocationServiceImpl.java @@ -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()); } } diff --git a/src/main/java/org/genesys2/util/MCPDUtil.java b/src/main/java/org/genesys2/util/MCPDUtil.java index 1d667ed502f70a7ec6a6c6d1fa4da8c394118486..155cfda3b97874558bb39c788621bb7635a9b055 100644 --- a/src/main/java/org/genesys2/util/MCPDUtil.java +++ b/src/main/java/org/genesys2/util/MCPDUtil.java @@ -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}$"); diff --git a/src/main/resources/liquibase/liquibase-changeLog.yml b/src/main/resources/liquibase/liquibase-changeLog.yml index 1ea98fc3406c87f5763676f81a623d545ef0936a..9f80a6c6c7b95e3ffe374656e0274a4b59ee96d8 100644 --- a/src/main/resources/liquibase/liquibase-changeLog.yml +++ b/src/main/resources/liquibase/liquibase-changeLog.yml @@ -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 diff --git a/src/test/java/org/genesys/test/catalog/services/AbstractDatasetServiceTest.java b/src/test/java/org/genesys/test/catalog/services/AbstractDatasetServiceTest.java index 50ef1f40a4233491617f86eaa5c481d8fe8ac5e2..d80a72a7bddce0987dc42c6cc52828ce7dac9329 100644 --- a/src/test/java/org/genesys/test/catalog/services/AbstractDatasetServiceTest.java +++ b/src/test/java/org/genesys/test/catalog/services/AbstractDatasetServiceTest.java @@ -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(); diff --git a/src/test/java/org/genesys/test/catalog/services/CatalogServiceTest.java b/src/test/java/org/genesys/test/catalog/services/CatalogServiceTest.java index 81b8b3ad3c7a40482965e0bdc6ae8e8be9f914fa..267205e6a7a49a5105aa889bf2437d9d8015059d 100644 --- a/src/test/java/org/genesys/test/catalog/services/CatalogServiceTest.java +++ b/src/test/java/org/genesys/test/catalog/services/CatalogServiceTest.java @@ -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; + } } diff --git a/src/test/java/org/genesys/test/catalog/services/DatasetServiceTest.java b/src/test/java/org/genesys/test/catalog/services/DatasetServiceTest.java index e7206d4631d8b8ec288644216ffd122ec46bbf28..498511667603dd51694083a54389e86b0fff229e 100644 --- a/src/test/java/org/genesys/test/catalog/services/DatasetServiceTest.java +++ b/src/test/java/org/genesys/test/catalog/services/DatasetServiceTest.java @@ -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()); + } + }