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.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;
import org.genesys.blocks.model.JsonViews;
import org.genesys.catalog.api.FilteredPage;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.filters.DescriptorFilter;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.model.traits.DescriptorList;
import org.genesys.catalog.service.DescriptorService;
import org.genesys.catalog.service.ShortFilterService;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.exception.InvalidApiUsageException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
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 com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.Api;
import javax.servlet.http.HttpServletResponse;
/**
* The Class DescriptorController.
*
* @author Maxym Borodenko
* @deprecated Use {@link org.genesys2.server.api.v1.DescriptorController}
*/
@Deprecated
@RestController("catalogDescriptorApi0")
@RequestMapping(DescriptorController.CONTROLLER_URL)
@PreAuthorize("isAuthenticated()")
@Api(tags = { "descriptor" })
public class DescriptorController {
private Logger LOG = LoggerFactory.getLogger(DescriptorController.class);
/** The Constant API_BASE. */
public static final String CONTROLLER_URL = ApiBaseController.APIv0_BASE + "/descriptor";
@Autowired
private DescriptorService descriptorService;
/** The short filter service. */
@Autowired
protected ShortFilterService shortFilterService;
/**
* List categories.
*
* @return the category[]
*/
@GetMapping(value = "/categories")
public Descriptor.Category[] listCategories() {
return Descriptor.Category.values();
}
/**
* My descriptors.
*
* @param page the page
* @param pageSize the page size
* @param direction the direction
* @param sort the sort
* @param filter the descriptor filter
* @return the page
* @throws IOException
*/
@PostMapping(value = "/list-mine")
public FilteredPage<Descriptor> myDescriptors(@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) DescriptorFilter filter) throws IOException {
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, DescriptorFilter.class);
} else {
filterCode = shortFilterService.getCode(filter);
}
return new FilteredPage<>(filterCode, filter, descriptorService.listDescriptorsForCurrentUser(filter, new PageRequest(page, Integer.min(pageSize, 100), direction, sort)));
}
/**
* List descriptors.
*
* @param page the page
* @param pageSize the page size
* @param direction the direction
* @param sort the sort
* @param filter the descriptor filter
* @return the page
* @throws IOException
*/
@PostMapping(value = "/list")
public FilteredPage<Descriptor> listDescriptors(@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) DescriptorFilter filter) throws IOException {
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, DescriptorFilter.class);
} else {
filterCode = shortFilterService.getCode(filter);
}
return new FilteredPage<>(filterCode, filter, descriptorService.listDescriptors(filter, new PageRequest(page, Integer.min(pageSize, 100), direction, sort)));
}
/**
* Filter descriptors by filter code.
*
* @param page the page
* @param pageSize the page size
* @param direction the direction
* @param sort the sort
* @param filterCode the filter code
* @return the filtered page
* @throws IOException Signals that an I/O exception has occurred.
* @deprecated Use {@link #listDescriptors(int, int, org.springframework.data.domain.Sort.Direction, String[], DescriptorFilter)}
*/
@PostMapping(value = "/list/{filterCode}")
public FilteredPage<Descriptor> listDescriptorsByShort(@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("filterCode") final String filterCode) throws IOException {
final DescriptorFilter filter = shortFilterService.filterByCode(filterCode, DescriptorFilter.class);
return new FilteredPage<>(filterCode, filter, descriptorService.listDescriptors(filter, new PageRequest(page, Integer.min(pageSize, 100), direction, sort)));
}
/**
* Gets the descriptor.
*
* @param uuid the uuid
* @return the descriptor
*/
@RequestMapping(value = "/{UUID}", method = RequestMethod.GET)
public Descriptor getDescriptor(@PathVariable("UUID") final UUID uuid) {
return descriptorService.getDescriptor(uuid);
}
/**
* Get additional information about descriptor that is not provided in the
* {@link #getDescriptor(UUID)} call.
*
* @param uuid descriptor UUID
* @return the other descriptor info
*/
@RequestMapping(value = "/extra/{UUID}", method = RequestMethod.GET)
@SuppressWarnings("unused")
@JsonView(JsonViews.Minimal.class)
public Object getOtherDescriptorInfo(@PathVariable("UUID") final UUID uuid) {
final Descriptor descriptor = descriptorService.getDescriptor(uuid);
return new Object() {
public List<DescriptorList> descriptorLists = descriptorService.getDescriptorLists(descriptor);
public List<Dataset> datasets = descriptorService.getDatasets(descriptor);
};
}
/**
* Creates the descriptor.
*
* @param source the source
* @return the descriptor
*/
@RequestMapping(value = "/create", method = RequestMethod.POST)
@PreAuthorize("isAuthenticated()")
public Descriptor createDescriptor(@RequestBody final Descriptor source) {
return descriptorService.createDescriptor(source);
}
/**
* Search matching descriptor.
*
* @param source the source
* @return matching descriptor
*/
@RequestMapping(value = "/search-matching", method = RequestMethod.POST)
public Descriptor searchMatchingDescriptor(@RequestBody final Descriptor source) {
return descriptorService.searchMatchingDescriptor(source);
}
/**
* Update descriptor.
*
* @param source the source
* @return the descriptor
*/
@RequestMapping(value = "/update", method = RequestMethod.POST)
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#source, 'write')")
public Descriptor updateDescriptor(@RequestBody final Descriptor source) {
return descriptorService.updateDescriptor(source);
}
/**
* Update descriptor.
*
* @param source the source
* @return the list
*/
@RequestMapping(value = "/upsert", method = RequestMethod.POST)
@PreAuthorize("hasRole('ADMINISTRATOR')")
public List<Descriptor> updateDescriptors(@RequestBody final List<Descriptor> source) {
return descriptorService.upsertDescriptors(source);
}
/**
* Make a new version of a descriptor.
*
* @param major make a new major version
* @param source the descriptor to copy
* @return the descriptor
*/
@RequestMapping(value = "/copy", method = RequestMethod.POST)
public Descriptor copyDescriptor(@RequestParam(name = "major", defaultValue = "true", required = false) final boolean major, @RequestBody final Descriptor source) {
return descriptorService.nextVersion(source, major);
}
/**
* Delete descriptor.
*
* @param uuid the uuid
* @param version the version
* @return the descriptor
*/
@DeleteMapping(value = "/{UUID},{version}")
public Descriptor deleteDescriptor(@PathVariable("UUID") final UUID uuid, @PathVariable("version") final int version) {
try {
return descriptorService.removeDescriptor(descriptorService.getDescriptor(uuid, version));
} catch (DataIntegrityViolationException e) {
throw new InvalidApiUsageException("Cannot delete a referenced descriptor");
}
}
/**
* Loads descriptor by uuid and version and tries to publish it.
*
* @param uuid descriptor UUID
* @param version record version
* @return published Descriptor (admin-only)
*/
@RequestMapping(value = "/approve", method = RequestMethod.POST)
public Descriptor approveDescriptor(@RequestParam(value = "uuid", required = true) final UUID uuid, @RequestParam(value = "version", required = true) final int version) {
final Descriptor descriptor = descriptorService.getDescriptor(uuid, version);
return descriptorService.approveDescriptor(descriptor);
}
/**
* Loads descriptor by uuid and version and send to review.
*
* @param uuid descriptor UUID
* @param version record version
* @return descriptor in review state
*/
@RequestMapping(value = "/for-review", method = RequestMethod.POST)
public Descriptor reviewDescriptor(@RequestParam(value = "uuid", required = true) final UUID uuid, @RequestParam(value = "version", required = true) final int version) {
final Descriptor descriptor = descriptorService.getDescriptor(uuid, version);
return descriptorService.reviewDescriptor(descriptor);
}
/**
* Loads descriptor by uuid and version and unpublish it.
*
* @param uuid descriptor UUID
* @param version record version
* @return unpublished descriptor
*/
@RequestMapping(value = "/reject", method = RequestMethod.POST)
public Descriptor rejectDescriptor(@RequestParam(value = "uuid", required = true) final UUID uuid, @RequestParam(value = "version", required = true) final int version) {
final Descriptor descriptor = descriptorService.getDescriptor(uuid, version);
return descriptorService.rejectDescriptor(descriptor);
}
@PostMapping(value = "/export", produces = { "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" })
public void exportDescriptors(
@RequestParam(name = "f", required = false) String filterCode,
@RequestBody(required = false) DescriptorFilter filter, HttpServletResponse response) throws IOException {
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, DescriptorFilter.class);
} else {
filterCode = shortFilterService.getCode(filter);
}
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-descriptors-" + filterCode + ".xlsx\""));
response.flushBuffer();
// Write XLSX to the stream.
final OutputStream outputStream = response.getOutputStream();
try {
descriptorService.exportDescriptors(filter, outputStream);
response.flushBuffer();
} catch (EOFException e) {
LOG.warn("Download was aborted", e);
}
}
}
/*
* 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.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import org.genesys.blocks.model.JsonViews;
import org.genesys.catalog.api.FilteredPage;
import org.genesys.catalog.model.filters.DescriptorListFilter;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.model.traits.DescriptorList;
import org.genesys.catalog.mvc.DescriptorListExporter;
import org.genesys.catalog.service.DescriptorListService;
import org.genesys.catalog.service.DescriptorService;
import org.genesys.catalog.service.ShortFilterService;
import org.genesys.catalog.service.ShortFilterService.FilterInfo;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.api.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
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 com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.Api;
/**
* The Class DescriptorListController.
*
* @author Matija Obreza
* @deprecated Use {@link org.genesys2.server.api.v1.DescriptorListController}
*/
@Deprecated
@RestController("catalogDescriptorListApi0")
@RequestMapping(DescriptorListController.CONTROLLER_URL)
@PreAuthorize("isAuthenticated()")
@Api(tags = { "descriptor-list" })
public class DescriptorListController {
/** The Constant API_BASE. */
public static final String CONTROLLER_URL = ApiBaseController.APIv0_BASE + "/descriptorlist";
@Autowired
private DescriptorService descriptorService;
@Autowired
private DescriptorListService descriptorListService;