Commit 37d971e1 authored by Viacheslav Pavlov's avatar Viacheslav Pavlov

Dataset model, service, controller updates

parent 0c66b196
......@@ -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 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.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;
......@@ -48,6 +58,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import org.springframework.web.multipart.MultipartFile;
/**
* The Class DatasetController.
......@@ -64,6 +75,10 @@ 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";
public static final String FILES_URL = "/{UUID}/files";
private static final Logger LOG = LoggerFactory.getLogger(DatasetController.class);
/** The dataset service. */
......@@ -300,4 +315,162 @@ 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));
}
}
/*
* 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));
}
}
......@@ -61,6 +61,9 @@ public class DatasetLocation extends UuidModel implements PublishValidationInter
@PublishValidation
private Double decimalLongitude;
@Column(length = 3)
private String countryCode;
/** The start date */
@Column(length = 8)
private String startDate;
......@@ -207,6 +210,24 @@ public class DatasetLocation extends UuidModel implements PublishValidationInter
this.decimalLongitude = decimalLongitude;
}
/**
* Gets location country code.
*
* @return country code
*/
public String getCountryCode() {
return countryCode;
}
/**
* Sets location country code.
*
* @param countryCode the new country code
*/
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
/**
* Gets the start date.
*
......
/*
* 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.DatasetCreator;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.UUID;
/**
* The DatasetCreatorService.
*
* @author Andrey Lugovskoy.
*/
public interface DatasetCreatorService {
/**
* Create DatasetCreator for Dataset.
*
* @param dataset the dataset
* @param input DatasetCreator
* @return created DatasetCreator
* @throws NotFoundElement throws if don't match the version
*/
DatasetCreator createDatasetCreator(Dataset dataset, DatasetCreator input) throws NotFoundElement;
/**
* Remove DatasetCreator of Dataset.
*
* @param dataset the dataset
* @param input DatasetCreator
* @return removed DatasetCreator
* @throws NotFoundElement throws if don't match the version
*/
DatasetCreator removeDatasetCreator(Dataset dataset, DatasetCreator input) throws NotFoundElement;
/**
* Removes the dataset creator.
*
* @param dataset the dataset
* @param datasetCreatorUuid the dataset creator uuid
* @return the dataset creator
* @throws NotFoundElement the not found element
*/
DatasetCreator removeDatasetCreator(Dataset dataset, UUID datasetCreatorUuid) throws NotFoundElement;
/**
* Load list DatasetCreators by Dataset UUID.
*
* @param datasetUuid Dataset UUID
* @param page Page
* @return Page with DatasetCreator list
*/
Page<DatasetCreator> listDatasetCreators(UUID datasetUuid, Pageable page);
/**
* Load dataset by input DatasetCreator.
*
* @param datasetCreator input DatasetCreator
* @return loaded DatasetCreator
* @throws NotFoundElement throws if don't match the version
*/
DatasetCreator loadDatasetCreator(DatasetCreator datasetCreator) throws NotFoundElement;
/**