Commit e2a2f553 authored by Maxym Borodenko's avatar Maxym Borodenko
Browse files

Merge branch '357-dataset-model-service-controller-updates' into 'master'

Resolve "Dataset model, service, controller updates"

Closes #357

See merge request genesys-pgr/genesys-server!253
parents 0c66b196 c6200429
......@@ -16,26 +16,36 @@
package org.genesys.catalog.api.v0;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
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;
import org.genesys.catalog.service.DescriptorService;
import org.genesys.catalog.service.ShortFilterService;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys2.server.api.ApiBaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
......@@ -46,6 +56,9 @@ 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;
......@@ -66,6 +79,11 @@ public class DatasetController {
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";
/** The dataset service. */
@Autowired
protected DatasetService datasetService;
......@@ -300,4 +318,237 @@ public class DatasetController {
return dataset;
}
/**
* List dataset creators.
*
* @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 = CREATOR_URL + "/list")
public Page<DatasetCreator> listDatasetCreators(@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.listDatasetCreators(uuid, new PageRequest(page, Integer.min(pageSize, 100), direction, sort));
}
/**
* Load by uuid.
*
* @param uuid the uuid
* @return the dataset creator
* @throws NotFoundElement the not found element
*/
@GetMapping(value = CREATOR_URL + "/{creatorUuid}")
public DatasetCreator loadByUuid(@PathVariable("creatorUuid") final UUID uuid) throws NotFoundElement {
return datasetService.loadDatasetCreator(uuid);
}
/**
* Creates the dataset creator.
*
* @param uuid the uuid
* @param datasetCreator the dataset creator
* @return the dataset creator
* @throws NotFoundElement the not found element
*/
@PostMapping(value = CREATOR_URL + "/create")
public DatasetCreator createDatasetCreator(@PathVariable("UUID") final UUID uuid, @RequestBody final DatasetCreator datasetCreator) throws NotFoundElement {
final Dataset dataset = datasetService.loadDataset(uuid);
return datasetService.createDatasetCreator(dataset, datasetCreator);
}
/**
* Delete dataset creator.
*
* @param datasetUuid the dataset uuid
* @param datasetCreator the dataset creator
* @return the dataset creator
* @throws NotFoundElement the not found element
*/
// uses request body
@RequestMapping(value = CREATOR_URL + "/delete", method = { RequestMethod.POST, RequestMethod.DELETE })
public DatasetCreator deleteDatasetCreator(@PathVariable("UUID") final UUID datasetUuid, @RequestBody final DatasetCreator datasetCreator) throws NotFoundElement {
final Dataset dataset = datasetService.loadDataset(datasetUuid);
return datasetService.removeDatasetCreator(dataset, datasetCreator);
}
/**
* Update dataset creator.
*
* @param datasetUuid the dataset uuid
* @param datasetCreator the dataset creator
* @return the dataset creator
* @throws NotFoundElement the not found element
*/
@PostMapping(value = CREATOR_URL + "/update")
public DatasetCreator updateDatasetCreator(@PathVariable("UUID") final UUID datasetUuid, @RequestBody final DatasetCreator datasetCreator) throws NotFoundElement {
final Dataset dataset = datasetService.loadDataset(datasetUuid);
return datasetService.updateDatasetCreator(dataset, datasetCreator);
}
/**
* Autocomplete.
*
* @param text the text
* @return the list
*/
@JsonView(JsonViews.Public.class)
@GetMapping(value = CREATOR_URL + "/autocomplete", produces = MediaType.APPLICATION_JSON_VALUE)
public List<DatasetCreator> autocomplete(@RequestParam("c") final String text) {
if (text.length() < 3) {
return Collections.emptyList();
}
return datasetService.autocompleteCreators(text);
}
/**
* Adds the file to dataset.
*
* @param inputFile the input file
* @param datasetUuid the dataset uuid
* @return the dataset
* @throws NotFoundElement the not found element
* @throws InvalidRepositoryFileDataException the invalid repository file data
* exception
* @throws InvalidRepositoryPathException the invalid repository path exception
* @throws IOException Signals that an I/O exception has occurred.
*/
@PostMapping(value = FILES_URL +"/add")
public Dataset addFileToDataset(@RequestParam("file") final MultipartFile inputFile, @PathVariable("UUID") final UUID datasetUuid) throws NotFoundElement,
InvalidRepositoryFileDataException, InvalidRepositoryPathException, IOException {
LOG.info("Upload file to dataset by uuid {}", datasetUuid);
return datasetService.addDatasetFile(datasetService.loadDataset(datasetUuid), inputFile);
}
/**
* Update dataset file.
*
* @param datasetUuid the dataset uuid
* @param metadata the metadata
* @return the dataset
* @throws NotFoundElement the not found element
* @throws InvalidRepositoryFileDataException the invalid repository file data
* exception
* @throws InvalidRepositoryPathException the invalid repository path exception
* @throws IOException Signals that an I/O exception has occurred.
* @throws NoSuchRepositoryFileException the no such repository file exception
*/
@PostMapping(value = FILES_URL + "/update")
public Dataset updateDatasetFile(@PathVariable("UUID") final UUID datasetUuid, @RequestBody final RepositoryFile metadata) throws NotFoundElement,
InvalidRepositoryFileDataException, InvalidRepositoryPathException, IOException, NoSuchRepositoryFileException {
return datasetService.updateDatasetFile(datasetService.loadDataset(datasetUuid), metadata);
}
/**
* Removes the file of dataset.
*
* @param datasetUuid the dataset uuid
* @param fileUuid the file uuid
* @return the dataset
* @throws NotFoundElement the not found element
* @throws InvalidRepositoryFileDataException the invalid repository file data
* exception
* @throws InvalidRepositoryPathException the invalid repository path exception
* @throws IOException Signals that an I/O exception has occurred.
* @throws NoSuchRepositoryFileException the no such repository file exception
*/
@DeleteMapping(value = FILES_URL + "/delete/{fileUuid}")
public Dataset removeFileOfDataset(@PathVariable("UUID") final UUID datasetUuid, @PathVariable("fileUuid") final UUID fileUuid) throws NotFoundElement,
InvalidRepositoryFileDataException, InvalidRepositoryPathException, IOException, NoSuchRepositoryFileException {
return datasetService.removeDatasetFile(datasetService.loadDataset(datasetUuid), fileUuid);
}
/**
* Gets the list.
*
* @param datasetUuid the dataset uuid
* @return the list
* @throws NotFoundElement the not found element
*/
@GetMapping(value = FILES_URL + "/list")
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 com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.Api;
import org.genesys.blocks.model.JsonViews;
import org.genesys.catalog.exceptions.NotFoundElement;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.dataset.DatasetCreator;
import org.genesys.catalog.service.DatasetCreatorService;
import org.genesys.catalog.service.DatasetService;
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.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
/**
* The Class DatasetCreatorController.
*
* @author Andrey Lugovskoy.
*/
@RestController("catalogDatasetCreatorApi0")
@PreAuthorize("isAuthenticated()")
@RequestMapping(DatasetCreatorController.CONTROLLER_URL)
@Api(tags = { "dataset" })
public class DatasetCreatorController {
/** The Constant API_BASE. */
public static final String CONTROLLER_URL = DatasetController.CONTROLLER_URL + "/{UUID}/datasetcreator";
/** The dataset service. */
@Autowired
protected DatasetService datasetService;
/** The dataset creator service. */
@Autowired
protected DatasetCreatorService datasetCreatorService;
/**
* List dataset creators.
*
* @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<DatasetCreator> listDatasetCreators(@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 datasetCreatorService.listDatasetCreators(uuid, new PageRequest(page, Integer.min(pageSize, 100), direction, sort));
}
/**
* Load by uuid.
*
* @param uuid the uuid
* @return the dataset creator
* @throws NotFoundElement the not found element
*/
@GetMapping(value = "/{creatorUuid}")
public DatasetCreator loadByUuid(@PathVariable("creatorUuid") final UUID uuid) throws NotFoundElement {
return datasetCreatorService.loadDatasetCreator(uuid);
}
/**
* Creates the dataset creator.
*
* @param uuid the uuid
* @param datasetCreator the dataset creator
* @return the dataset creator
* @throws NotFoundElement the not found element
*/
@PostMapping(value = "/create")
public DatasetCreator createDatasetCreator(@PathVariable("UUID") final UUID uuid, @RequestBody final DatasetCreator datasetCreator) throws NotFoundElement {
final Dataset dataset = datasetService.loadDataset(uuid);
return datasetCreatorService.createDatasetCreator(dataset, datasetCreator);
}
/**
* Delete dataset creator.
*
* @param datasetUuid the dataset uuid
* @param datasetCreator the dataset creator
* @return the dataset creator
* @throws NotFoundElement the not found element
*/
// uses request body
@RequestMapping(value = "/delete", method = { RequestMethod.POST, RequestMethod.DELETE })
public DatasetCreator deleteDatasetCreator(@PathVariable("UUID") final UUID datasetUuid, @RequestBody final DatasetCreator datasetCreator) throws NotFoundElement {
final Dataset dataset = datasetService.loadDataset(datasetUuid);
return datasetCreatorService.removeDatasetCreator(dataset, datasetCreator);
}
/**
* Update dataset creator.
*
* @param datasetUuid the dataset uuid
* @param datasetCreator the dataset creator
* @return the dataset creator
* @throws NotFoundElement the not found element
*/
@PostMapping(value = "/update")
public DatasetCreator updateDatasetCreator(@PathVariable("UUID") final UUID datasetUuid, @RequestBody final DatasetCreator datasetCreator) throws NotFoundElement {
final Dataset dataset = datasetService.loadDataset(datasetUuid);
return datasetCreatorService.updateDatasetCreator(dataset, datasetCreator);
}
/**
* Autocomplete.
*
* @param text the text
* @return the list
*/
@JsonView(JsonViews.Public.class)
@GetMapping(value = "/autocomplete", produces = MediaType.APPLICATION_JSON_VALUE)
public List<DatasetCreator> autocomplete(@RequestParam("c") final String text) {
if (text.length() < 3) {
return Collections.emptyList();
}
return datasetCreatorService.autocompleteCreators(text);
}
}
/*
* 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 org.genesys.catalog.exceptions.NotFoundElement;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.service.DatasetService;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.genesys.filerepository.model.RepositoryFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import io.swagger.annotations.Api;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
/**
* Manage dataset files.
*
* @author Andrey Lugovskoy.
*/
@RestController("catalogDatasetFilesApi0")
@RequestMapping(DatasetFilesController.CONTROLLER_URL)
@PreAuthorize("isAuthenticated()")
@Api(tags = { "dataset" })
public class DatasetFilesController {
/** The Constant API_BASE. */
protected static final String CONTROLLER_URL = DatasetController.CONTROLLER_URL + "/{UUID}/files";
private static final Logger LOG = LoggerFactory.getLogger(DatasetFilesController.class);
/** The dataset service. */
@Autowired
protected DatasetService datasetService;
/**
* Adds the file to dataset.
*
* @param inputFile the input file
* @param datasetUuid the dataset uuid
* @return the dataset
* @throws NotFoundElement the not found element
* @throws InvalidRepositoryFileDataException the invalid repository file data
* exception
* @throws InvalidRepositoryPathException the invalid repository path exception
* @throws IOException Signals that an I/O exception has occurred.
*/
@PostMapping(value = "/add")
public Dataset addFileToDataset(@RequestParam("file") final MultipartFile inputFile, @PathVariable("UUID") final UUID datasetUuid) throws NotFoundElement,
InvalidRepositoryFileDataException, InvalidRepositoryPathException, IOException {
LOG.info("Upload file to dataset by uuid {}", datasetUuid);
return datasetService.addDatasetFile(datasetService.loadDataset(datasetUuid), inputFile);
}
/**
* Update dataset file.
*
* @param datasetUuid the dataset uuid
* @param metadata the metadata
* @return the dataset
* @throws NotFoundElement the not found element
* @throws InvalidRepositoryFileDataException the invalid repository file data
* exception
* @throws InvalidRepositoryPathException the invalid repository path exception
* @throws IOException Signals that an I/O exception has occurred.
* @throws NoSuchRepositoryFileException the no such repository file exception
*/
@PostMapping(value = "/update")
public Dataset updateDatasetFile(@PathVariable("UUID") final UUID datasetUuid, @RequestBody final RepositoryFile metadata) throws NotFoundElement,
InvalidRepositoryFileDataException, InvalidRepositoryPathException, IOException, NoSuchRepositoryFileException {
return datasetService.updateDatasetFile(datasetService.loadDataset(datasetUuid), metadata);
}
/**
* Removes the file of dataset.
*
* @param datasetUuid the dataset uuid
* @param fileUuid the file uuid
* @return the dataset
* @throws NotFoundElement the not found element
* @throws InvalidRepositoryFileDataException the invalid repository file data
* exception
* @throws InvalidRepositoryPathException the invalid repository path exception
* @throws IOException Signals that an I/O exception has occurred.
* @throws NoSuchRepositoryFileException the no such repository file exception
*/
@DeleteMapping(value = "/delete/{fileUuid}")
public Dataset removeFileOfDataset(@PathVariable("UUID") final UUID datasetUuid, @PathVariable("fileUuid") final UUID fileUuid) throws NotFoundElement,
InvalidRepositoryFileDataException, InvalidRepositoryPathException, IOException, NoSuchRepositoryFileException {
return datasetService.removeDatasetFile(datasetService.loadDataset(datasetUuid), fileUuid);
}
/**
* Gets the list.
*
* @param datasetUuid the dataset uuid
* @return the list
* @throws NotFoundElement the not found element
*/
@GetMapping(value = "/list")
public List<RepositoryFile> getList(@PathVariable("UUID") final UUID datasetUuid) throws NotFoundElement {
return datasetService.listDatasetFiles(datasetService.loadDataset(datasetUuid));
}
}
/*
* 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.