Commit 99ec18fb authored by Matija Obreza's avatar Matija Obreza

Merge branch 'vocabularies-update' into 'master'

Vocabularies update

See merge request genesys-pgr/genesys-server!281
parents 4d84d753 789d89c4
/*
* 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.genesys2.server.api.ApiBaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import java.net.UnknownHostException;
/**
* The Class ApiInfoController.
*
* @author Andrey Lugovskoy.
*/
@RestController
@RequestMapping(ApiInfoController.CONTROLLER_URL)
@PreAuthorize("isAuthenticated()")
@Api(tags = { "api-info" })
public class ApiInfoController implements InitializingBean {
private static final Logger LOG = LoggerFactory.getLogger(ApiInfoController.class);
/** The Constant API_BASE. */
public static final String CONTROLLER_URL = ApiBaseController.APIv0_BASE + "/info";
@Value("${build.version}")
private String version;
@Value("${build.artifactId}")
private String artifactId;
@Value("${build.revision}")
private String revision;
private ApiInfo apiInfo;
@Override
public void afterPropertiesSet() throws Exception {
String hN;
try {
hN = java.net.InetAddress.getLocalHost().getHostName();
} catch (final UnknownHostException e) {
LOG.error("Error getting Host Name");
hN = "";
}
apiInfo = new ApiInfo(artifactId, version, revision, hN);
LOG.info("API Info: art=" + artifactId + " ver=" + version + " rev=" + revision);
}
/**
* Api info.
*
* @return the api info
*/
@RequestMapping(value = "/version", method = RequestMethod.GET)
public ApiInfo apiInfo() {
return apiInfo;
}
/**
* The Class ApiInfo.
*/
public static class ApiInfo {
private final String artifactId;
private final String version;
private final String revision;
private final String hostName;
/**
* Instantiates a new api info.
*
* @param artifactId the artifact id
* @param version the version
* @param revision the revision
* @param hostName the host name
*/
public ApiInfo(final String artifactId, final String version, final String revision, final String hostName) {
this.artifactId = artifactId;
this.version = version;
this.revision = revision;
this.hostName = hostName;
}
/**
* Gets the host name.
*
* @return the host name
*/
public String getHostName() {
return hostName;
}
/**
* Gets the artifact id.
*
* @return the artifactId
*/
public String getArtifactId() {
return artifactId;
}
/**
* Gets the version.
*
* @return the version
*/
public String getVersion() {
return version;
}
/**
* Gets the revision.
*
* @return the revision
*/
public String getRevision() {
return revision;
}
}
}
/*
* 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.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.genesys2.server.model.genesys.AccessionRef;
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.genesys2.server.exception.NotFoundElement;
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;
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;
import org.springframework.web.multipart.MultipartFile;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.Api;
/**
* The Class DatasetController.
*
* @author Andrey Lugovskoy
* @author Matija Obreza
* @deprecated Use {@link org.genesys2.server.api.v1.DatasetController}
*/
@Deprecated
@RestController("catalogDatasetApi0")
@RequestMapping(DatasetController.CONTROLLER_URL)
@PreAuthorize("isAuthenticated()")
@Api(tags = { "dataset" })
public class DatasetController {
// Rest controller base URL
public static final String CONTROLLER_URL = ApiBaseController.APIv0_BASE + "/dataset";
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;
/** The short filter service. */
@Autowired
protected ShortFilterService shortFilterService;
/** The descriptor service. */
@Autowired
protected DescriptorService descriptorService;
/**
* My datasets.
*
* @param page the page
* @param pageSize the page size
* @param direction the direction
* @param sort the sort
* @param filterCode short filter code -- overrides filter in body
* @param filter the filter
* @return the page
* @throws IOException
*/
@PostMapping(value = "/list-mine")
public FilteredPage<Dataset> myDatasets(@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 = "DESC") final Sort.Direction direction,
@RequestParam(name = "s", required = false, defaultValue = "lastModifiedDate") final String[] sort,
@RequestParam(name = "f", required = false) String filterCode,
@RequestBody(required = false) DatasetFilter filter) throws IOException {
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, DatasetFilter.class);
} else {
filterCode = shortFilterService.getCode(filter);
}
return new FilteredPage<>(filterCode, filter, datasetService.listDatasetsForCurrentUser(filter, new PageRequest(page, Integer.min(pageSize, 100), direction, sort)));
}
/**
* List datasets.
*
* @param page the page
* @param pageSize the page size
* @param direction the direction
* @param sort the sort
* @param filter the dataset filter
* @return the page
*/
@PostMapping(value = "/list")
public FilteredPage<Dataset> datasetList(@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,
@RequestParam(name = "f", required = false) String filterCode,
@RequestBody(required = false) DatasetFilter filter) throws IOException {
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, DatasetFilter.class);
} else {
filterCode = shortFilterService.getCode(filter);
}
return new FilteredPage<>(filterCode, filter, datasetService.listDatasets(filter, new PageRequest(page, Integer.min(pageSize, 100), direction, sort)));
}
/**
* Gets the dataset.
*
* @param uuid the uuid
* @return the dataset
* @throws NotFoundElement the not found element
*/
@GetMapping(value = "/{UUID}")
public Dataset getDataset(@PathVariable("UUID") final UUID uuid) throws NotFoundElement {
LOG.debug("Load Dataset by uuid {}", uuid);
return datasetService.loadDataset(uuid);
}
/**
* Creates the dataset.
*
* @param dataset the dataset
* @return the dataset
*/
@PostMapping(value = "/create")
@PreAuthorize("isAuthenticated()")
public Dataset createDataset(@RequestBody final Dataset dataset) {
LOG.info("Create Dataset {}", dataset);
return datasetService.createDataset(dataset);
}
/**
* Update dataset.
*
* @param dataset the dataset
* @return the dataset
*/
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'write')")
@PostMapping(value = "/update")
public Dataset updateDataset(@RequestBody final Dataset dataset) {
LOG.debug("Update Dataset {}", dataset);
return datasetService.updateDataset(dataset);
}
/**
* Upsert accessions.
*
* @param uuid the uuid
* @param version the version
* @param accessionRefs the accession identifiers
* @return the dataset
* @throws NotFoundElement the not found element
*/
@PostMapping(value = "/upsertaccessions/{UUID},{version}")
@JsonView(JsonViews.Minimal.class)
public Dataset upsertAccessions(@PathVariable("UUID") final UUID uuid, @PathVariable("version") final int version,
@RequestBody final Set<AccessionRef> accessionRefs) throws NotFoundElement {
final Dataset dataset = datasetService.loadDataset(uuid, version);
LOG.debug("Received {} refs for Dataset {}", accessionRefs.size(), uuid);
return datasetService.upsertAccessions(dataset, accessionRefs);
}
/**
* Loads dataset by uuid and version and tries to publish it.
*
* @param uuid dataset UUID
* @param version record version
* @return published Dataset (admin-only)
*/
@RequestMapping(value = "/approve", method = RequestMethod.POST)
public Dataset approveDataset(@RequestParam(value = "uuid", required = true) final UUID uuid, @RequestParam(value = "version", required = true) final int version) {
final Dataset dataset = datasetService.loadDataset(uuid, version);
return datasetService.approveDataset(dataset);
}
/**
* Loads dataset by uuid and version and send to review.
*
* @param uuid dataset UUID
* @param version record version
* @return dataset in review state
*/
@RequestMapping(value = "/for-review", method = RequestMethod.POST)
public Dataset reviewDataset(@RequestParam(value = "uuid", required = true) final UUID uuid, @RequestParam(value = "version", required = true) final int version) {
final Dataset dataset = datasetService.loadDataset(uuid, version);
return datasetService.reviewDataset(dataset);
}
/**
* Loads dataset by uuid and version and unpublish it.
*
* @param uuid dataset UUID
* @param version record version
* @return unpublished dataset
*/
@RequestMapping(value = "/reject", method = RequestMethod.POST)
public Dataset rejectDataset(@RequestParam(value = "uuid", required = true) final UUID uuid, @RequestParam(value = "version", required = true) final int version) {
final Dataset dataset = datasetService.loadDataset(uuid, version);
return datasetService.rejectDataset(dataset);
}
/**
* Adds the descriptors.
*
* @param uuid the uuid
* @param version the version
* @param descriptorUuids the descriptor uuids
* @return the dataset
*/
@PostMapping(value = "/add-descriptors/{UUID},{version}")
public Dataset addDescriptors(@PathVariable("UUID") final UUID uuid, @PathVariable("version") final int version, @RequestBody final Set<UUID> descriptorUuids) {
final Dataset dataset = datasetService.loadDataset(uuid, version);
final Set<Descriptor> descriptors = descriptorUuids.stream().map(descriptorUuid -> descriptorService.getDescriptor(descriptorUuid)).collect(Collectors.toSet());
return datasetService.addDescriptors(dataset, descriptors.toArray(new Descriptor[] {}));
}
@PostMapping(value = "/set-descriptors/{UUID},{version}")
public Dataset updateDescriptors(@PathVariable("UUID") final UUID uuid, @PathVariable("version") final int version, @RequestBody final List<UUID> descriptorUuids) {
final Dataset dataset = datasetService.loadDataset(uuid, version);
final List<Descriptor> descriptors = descriptorUuids.stream().map(descriptorUuid -> descriptorService.getDescriptor(descriptorUuid)).collect(Collectors.toList());
return datasetService.updateDescriptors(dataset, descriptors);
}
/**
* Removes the descriptors.
*
* @param uuid the uuid
* @param version the version
* @param descriptorUuids the descriptor uuids
* @return the dataset
*/
@PostMapping(value = "/remove-descriptors/{UUID},{version}")
public Dataset removeDescriptors(@PathVariable("UUID") final UUID uuid, @PathVariable("version") final int version, @RequestBody final Set<UUID> descriptorUuids) {
final Dataset dataset = datasetService.loadDataset(uuid, version);
final Set<Descriptor> descriptors = descriptorUuids.stream().map(descriptorUuid -> descriptorService.getDescriptor(descriptorUuid)).collect(Collectors.toSet());
return datasetService.removeDescriptors(dataset, descriptors.toArray(new Descriptor[] {}));
}
/**
* Delete dataset.
*
* @param uuid the uuid
* @param version the version
* @return the dataset
* @throws Exception the exception
*/
@DeleteMapping("/{UUID},{version}")
public Dataset deleteDataset(@PathVariable("UUID") final UUID uuid, @PathVariable("version") final int version) throws Exception {
final Dataset dataset = datasetService.removeDataset(datasetService.loadDataset(uuid, version));
dataset.setId(null);
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