Commit 86fc75b7 authored by Matija Obreza's avatar Matija Obreza

Merged LocationService to DatasetService, LocationConroller to DatasetController

- Added `DatasetLocation#description` lob
- Updated Excel generation
parent 0783f042
......@@ -22,13 +22,13 @@ import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import com.fasterxml.jackson.annotation.JsonView;
import org.genesys.blocks.model.JsonViews;
import org.genesys.catalog.api.FilteredPage;
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.DatasetCreator;
import org.genesys.catalog.model.dataset.DatasetLocation;
import org.genesys.catalog.model.filters.DatasetFilter;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.service.DatasetService;
......@@ -56,9 +56,11 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.Api;
import org.springframework.web.multipart.MultipartFile;
/**
* The Class DatasetController.
......@@ -75,11 +77,12 @@ public class DatasetController {
// Rest controller base URL
public static final String CONTROLLER_URL = ApiBaseController.APIv0_BASE + "/dataset";
public static final String CREATOR_URL = "/{UUID}/datasetcreator";
private static final Logger LOG = LoggerFactory.getLogger(DatasetController.class);
public static final String CREATOR_URL = "/{UUID}/datasetcreator";
public static final String FILES_URL = "/{UUID}/files";
protected static final String LOCATION_URL = "/{UUID}/location";
private static final Logger LOG = LoggerFactory.getLogger(DatasetController.class);
/** The dataset service. */
@Autowired
......@@ -473,4 +476,79 @@ public class DatasetController {
public List<RepositoryFile> getList(@PathVariable("UUID") final UUID datasetUuid) throws NotFoundElement {
return datasetService.listDatasetFiles(datasetService.loadDataset(datasetUuid));
}
/**
* List location.
*
* @param page the page
* @param pageSize the page size
* @param direction the direction
* @param sort the sort
* @param uuid the uuid
* @return the page
*/
@GetMapping(value = LOCATION_URL + "/list")
public Page<DatasetLocation> listLocation(@RequestParam(name = "p", required = false, defaultValue = "0") final int page,
@RequestParam(name = "l", required = false, defaultValue = "50") final int pageSize,
@RequestParam(name = "d", required = false, defaultValue = "ASC") final Sort.Direction direction,
@RequestParam(name = "s", required = false, defaultValue = "id") final String[] sort, @PathVariable("UUID") final UUID uuid) {
return datasetService.listLocation(uuid, new PageRequest(page, Integer.min(pageSize, 100), direction, sort));
}
/**
* Load by uuid.
*
* @param locationUuid the location uuid
* @return the dataset location
* @throws NotFoundElement the not found element
*/
@GetMapping(value = LOCATION_URL + "/{locationUuid}")
public DatasetLocation loadLocationByUuid(@PathVariable("locationUuid") final UUID locationUuid) throws NotFoundElement {
return datasetService.loadLocation(locationUuid);
}
/**
* Creates the location.
*
* @param datasetUuid the dataset uuid
* @param datasetLocation the dataset location
* @return the dataset location
* @throws NotFoundElement the not found element
*/
@PostMapping(value = LOCATION_URL + "/create")
public DatasetLocation createLocation(@PathVariable("UUID") final UUID datasetUuid, @RequestBody final DatasetLocation datasetLocation) throws NotFoundElement {
final Dataset dataset = datasetService.loadDataset(datasetUuid);
return datasetService.createLocation(dataset, datasetLocation);
}
/**
* Delete location.
*
* @param datasetUuid the dataset uuid
* @param datasetLocation the dataset location
* @return the dataset location
* @throws NotFoundElement the not found element
*/
@RequestMapping(value = LOCATION_URL + "/delete", method = { RequestMethod.POST, RequestMethod.DELETE })
public DatasetLocation deleteLocation(@PathVariable("UUID") final UUID datasetUuid, @RequestBody final DatasetLocation datasetLocation) throws NotFoundElement {
final Dataset dataset = datasetService.loadDataset(datasetUuid);
return datasetService.removeLocation(dataset, datasetLocation);
}
/**
* Update location.
*
* @param datasetUuid the dataset uuid
* @param datasetLocation the dataset location
* @return the dataset location
* @throws NotFoundElement the not found element
*/
@PostMapping(value = LOCATION_URL + "/update")
public DatasetLocation updateLocation(@PathVariable("UUID") final UUID datasetUuid, @RequestBody final DatasetLocation datasetLocation) throws NotFoundElement {
final Dataset dataset = datasetService.loadDataset(datasetUuid);
return datasetService.updateLocation(dataset, datasetLocation);
}
}
/*
* 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.api.v0;
import java.util.UUID;
import org.genesys.catalog.exceptions.NotFoundElement;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.dataset.DatasetLocation;
import org.genesys.catalog.service.DatasetService;
import org.genesys.catalog.service.LocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* The Class LocationController.
*
* @author Andrey Lugovskoy.
*/
@RestController
@RequestMapping(LocationController.CONTROLLER_URL)
public class LocationController {
/** The Constant API_BASE. */
protected static final String CONTROLLER_URL = DatasetController.CONTROLLER_URL + "/{UUID}/location";
/** The dataset service. */
@Autowired
protected DatasetService datasetService;
@Autowired
private LocationService locationService;
/**
* List location.
*
* @param page the page
* @param pageSize the page size
* @param direction the direction
* @param sort the sort
* @param uuid the uuid
* @return the page
*/
@GetMapping(value = "/list")
public Page<DatasetLocation> listLocation(@RequestParam(name = "p", required = false, defaultValue = "0") final int page,
@RequestParam(name = "l", required = false, defaultValue = "50") final int pageSize,
@RequestParam(name = "d", required = false, defaultValue = "ASC") final Sort.Direction direction,
@RequestParam(name = "s", required = false, defaultValue = "id") final String[] sort, @PathVariable("UUID") final UUID uuid) {
return locationService.listLocation(uuid, new PageRequest(page, Integer.min(pageSize, 100), direction, sort));
}
/**
* Load by uuid.
*
* @param locationUuid the location uuid
* @return the dataset location
* @throws NotFoundElement the not found element
*/
@GetMapping(value = "/{locationUuid}")
public DatasetLocation loadByUuid(@PathVariable("locationUuid") final UUID locationUuid) throws NotFoundElement {
return locationService.loadLocation(locationUuid);
}
/**
* Creates the location.
*
* @param datasetUuid the dataset uuid
* @param datasetLocation the dataset location
* @return the dataset location
* @throws NotFoundElement the not found element
*/
@PostMapping(value = "/create")
public DatasetLocation createLocation(@PathVariable("UUID") final UUID datasetUuid, @RequestBody final DatasetLocation datasetLocation) throws NotFoundElement {
final Dataset dataset = datasetService.loadDataset(datasetUuid);
return locationService.createLocation(dataset, datasetLocation);
}
/**
* Delete location.
*
* @param datasetUuid the dataset uuid
* @param datasetLocation the dataset location
* @return the dataset location
* @throws NotFoundElement the not found element
*/
@RequestMapping(value = "/delete", method = { RequestMethod.POST, RequestMethod.DELETE })
public DatasetLocation deleteLocation(@PathVariable("UUID") final UUID datasetUuid, @RequestBody final DatasetLocation datasetLocation) throws NotFoundElement {
final Dataset dataset = datasetService.loadDataset(datasetUuid);
return locationService.removeLocation(dataset, datasetLocation);
}
/**
* Update location.
*
* @param datasetUuid the dataset uuid
* @param datasetLocation the dataset location
* @return the dataset location
* @throws NotFoundElement the not found element
*/
@PostMapping(value = "/update")
public DatasetLocation updateLocation(@PathVariable("UUID") final UUID datasetUuid, @RequestBody final DatasetLocation datasetLocation) throws NotFoundElement {
final Dataset dataset = datasetService.loadDataset(datasetUuid);
return locationService.updateLocation(dataset, datasetLocation);
}
}
......@@ -19,6 +19,7 @@ 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.DatasetCreator;
import org.genesys.catalog.model.dataset.DatasetLocation;
import org.genesys.catalog.model.filters.DatasetFilter;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
......@@ -346,4 +347,71 @@ public interface DatasetService {
*/
List<DatasetCreator> autocompleteCreators(String text);
/**
* Method adding DatasetLocation to Dataset.
*
* @param dataset the dataset
* @param input DatasetLocation
* @return added DatasetLocation
* @throws NotFoundElement the not found element
*/
DatasetLocation createLocation(Dataset dataset, DatasetLocation input) throws NotFoundElement;
/**
* Method for remove DatasetLocation of Dataset.
*
* @param dataset the dataset
* @param input DatasetLocation
* @return removed DatasetLocation
* @throws NotFoundElement the not found element
*/
DatasetLocation removeLocation(Dataset dataset, DatasetLocation input) throws NotFoundElement;
/**
* Method for getting DatasetLocation list of Dataset.
*
* @param dataset the dataset
* @return loaded List
* @throws NotFoundElement the not found element
*/
List<DatasetLocation> listLocation(Dataset dataset) throws NotFoundElement;
/**
* Method for getting DatasetLocation list of Dataset.
*
* @param datasetUuid Dataset UUID
* @param page page
* @return page with loaded list DatasetLocation
*/
Page<DatasetLocation> listLocation(UUID datasetUuid, Pageable page);
/**
* Method for getting DatasetLocation by UUID.
*
* @param input DatasetLocation input
* @return loaded DatasetLocation
* @throws NotFoundElement the not found element
*/
DatasetLocation loadLocation(DatasetLocation input) throws NotFoundElement;
/**
* Method for getting DatasetLocation by UUID.
*
* @param locationUuid DatasetLocation UUID
* @return loaded DatasetLocation
* @throws NotFoundElement the not found element
*/
DatasetLocation loadLocation(UUID locationUuid) throws NotFoundElement;
/**
* Method for update location.
*
* @param dataset the dataset
* @param input data of location
* @return updated location
* @throws NotFoundElement the not found element
*/
DatasetLocation updateLocation(Dataset dataset, DatasetLocation input) throws NotFoundElement;
}
/*
* 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;
import org.genesys.catalog.exceptions.NotFoundElement;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.dataset.DatasetLocation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.UUID;
/**
* The Interface LocationService.
*
* @author Andrey Lugovskoy.
*/
public interface LocationService {
/**
* Method adding DatasetLocation to Dataset.
*
* @param dataset the dataset
* @param input DatasetLocation
* @return added DatasetLocation
* @throws NotFoundElement the not found element
*/
DatasetLocation createLocation(Dataset dataset, DatasetLocation input) throws NotFoundElement;
/**
* Method for remove DatasetLocation of Dataset.
*
* @param dataset the dataset
* @param input DatasetLocation
* @return removed DatasetLocation
* @throws NotFoundElement the not found element
*/
DatasetLocation removeLocation(Dataset dataset, DatasetLocation input) throws NotFoundElement;
/**
* Method for getting DatasetLocation list of Dataset.
*
* @param dataset the dataset
* @return loaded List
* @throws NotFoundElement the not found element
*/
List<DatasetLocation> listLocation(Dataset dataset) throws NotFoundElement;
/**
* Method for getting DatasetLocation list of Dataset.
*
* @param datasetUuid Dataset UUID
* @param page page
* @return page with loaded list DatasetLocation
*/
Page<DatasetLocation> listLocation(UUID datasetUuid, Pageable page);
/**
* Method for getting DatasetLocation by UUID.
*
* @param input DatasetLocation input
* @return loaded DatasetLocation
* @throws NotFoundElement the not found element
*/
DatasetLocation loadLocation(DatasetLocation input) throws NotFoundElement;
/**
* Method for getting DatasetLocation by UUID.
*
* @param locationUuid DatasetLocation UUID
* @return loaded DatasetLocation
* @throws NotFoundElement the not found element
*/
DatasetLocation loadLocation(UUID locationUuid) throws NotFoundElement;
/**
* Method for update location.
*
* @param dataset the dataset
* @param input data of location
* @return updated location
* @throws NotFoundElement the not found element
*/
DatasetLocation updateLocation(Dataset dataset, DatasetLocation input) throws NotFoundElement;
}
......@@ -39,6 +39,7 @@ import org.genesys.catalog.model.dataset.*;
import org.genesys.catalog.model.filters.DatasetFilter;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.persistence.dataset.DatasetCreatorRepository;
import org.genesys.catalog.persistence.dataset.DatasetLocationRepository;
import org.genesys.catalog.persistence.dataset.DatasetRepository;
import org.genesys.catalog.persistence.dataset.DatasetVersionsRepository;
import org.genesys.catalog.service.DatasetService;
......@@ -86,6 +87,10 @@ public class DatasetServiceImpl implements DatasetService {
@Autowired
private DatasetCreatorRepository datasetCreatorRepository;
/** The location repository. */
@Autowired
private DatasetLocationRepository locationRepository;
/** The dataset repository. */
@Autowired
private DatasetRepository datasetRepository;
......@@ -797,4 +802,145 @@ public class DatasetServiceImpl implements DatasetService {
datasetRepository.save(dataset);
});
}
/**
* {@inheritDoc}
*/
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'write')")
public DatasetLocation createLocation(Dataset dataset, final DatasetLocation input) throws NotFoundElement {
dataset = loadDataset(dataset);
LOG.info("Create DatasetLocation {} for dataset {}", input, dataset.getUuid());
input.setDataset(dataset);
DatasetLocation saved = locationRepository.save(input);
dataset.setStartDate(saved.getStartDate());
dataset.setEndDate(saved.getEndDate());
dataset.getLocations().add(saved);
updateDataset(dataset);
return saved;
}
/**
* {@inheritDoc}
*/
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'write')")
public DatasetLocation removeLocation(Dataset dataset, final DatasetLocation input) throws NotFoundElement {
LOG.info("Remove DatasetLocation {} of dataset {}", input, dataset.getUuid());
dataset = loadDataset(dataset);
final DatasetLocation datasetLocation = loadLocation(input);
if (!datasetLocation.getDataset().getUuid().equals(dataset.getUuid())) {
throw new InvalidApiUsageException("Location does not belong to dataset");
}
dataset.getLocations().remove(datasetLocation);
dataset.setStartDate("--------");
dataset.setEndDate("--------");
updateDataset(dataset);
return datasetLocation;
}
/**
* {@inheritDoc}
*/
@Override
public List<DatasetLocation> listLocation(Dataset dataset) throws NotFoundElement {
LOG.info("Load list DatasetLocation for dataset {}", dataset.getUuid());
dataset = loadDataset(dataset);
dataset.getLocations().size();
return dataset.getLocations();
}
/**
* {@inheritDoc}
*/
@Override
public Page<DatasetLocation> listLocation(final UUID datasetUuid, final Pageable page) {
LOG.info("Load list DatasetLocation for dataset {}", datasetUuid);
return locationRepository.listByUUidOfDataset(page, datasetUuid);
}
/**
* {@inheritDoc}
*/
@Override
public DatasetLocation loadLocation(final DatasetLocation input) throws NotFoundElement {
LOG.info("Load list DatasetLocation {}", input);
final DatasetLocation datasetLocation = locationRepository.findByUuid(input.getUuid());
if (datasetLocation == null) {
LOG.error("DatasetLocation by UUID {} not found", input);
throw new NotFoundElement("DatasetLocation " + input.getUuid().toString() + " no found");
}
if (!datasetLocation.getVersion().equals(input.getVersion())) {
LOG.error("Don't match the version");
throw new ConcurrencyFailureException("Object version changed to " + datasetLocation.getVersion() + ", you provided " + input.getVersion());
}
return datasetLocation;
}
/**
* {@inheritDoc}
*/
@Override
public DatasetLocation loadLocation(final UUID locationUuid) throws NotFoundElement {
LOG.info("Load list DatasetLocation by uuid {}", locationUuid);
final DatasetLocation datasetLocation = locationRepository.findByUuid(locationUuid);
if (datasetLocation == null) {
LOG.error("DatasetLocation by UUID {} not found", locationUuid);
throw new NotFoundElement("DatasetCreator by " + locationUuid.toString() + " no found");
}
return datasetLocation;
}
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'write')")
public DatasetLocation updateLocation(Dataset dataset, final DatasetLocation input) throws NotFoundElement {
dataset = loadDataset(dataset);
final DatasetLocation datasetLocation = loadLocation(input);
if (!datasetLocation.getDataset().getUuid().equals(dataset.getUuid())) {
throw new InvalidApiUsageException("Location does not belong to dataset");
}
copyValue(datasetLocation, input);
DatasetLocation saved = locationRepository.save(datasetLocation);
dataset.getLocations().remove(input);
dataset.getLocations().add(saved);
dataset.setStartDate(saved.getStartDate());
dataset.setEndDate(saved.getEndDate());
updateDataset(dataset);
return saved;
}
/**
* Copy value.
*
* @param target the target
* @param source the source
*/
protected void copyValue(final DatasetLocation target, final DatasetLocation source) {
target.setDescription(source.getDescription());
target.setUserCountry(source.getUserCountry());
target.setMapCountry(source.getMapCountry());
target.setCountryCode(source.getCountryCode());
target.setStateProvince(source.getStateProvince());
target.setVerbatimLocality(source.getVerbatimLocality());
target.setDecimalLatitude(source.getDecimalLatitude());
target.setDecimalLongitude(source.getDecimalLongitude());
target.setStartDate(source.getStartDate());
target.setEndDate(source.getEndDate());
}
}
/*
* 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;