Commit ecd9b2f3 authored by Matija Obreza's avatar Matija Obreza
Browse files

Accession API v1

- Filters for Geneys models
parent 8038a9bf
/*
* 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.genesys2.server.api.v1;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.genesys.catalog.api.FilteredPage;
import org.genesys.catalog.service.ShortFilterService;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.service.AccessionService;
import org.genesys2.server.service.filter.AccessionFilter;
import org.springframework.beans.factory.annotation.Autowired;
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.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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerMapping;
import io.swagger.annotations.Api;
/**
* Accession API v1
*/
@RestController("accessionApi1")
@PreAuthorize("isAuthenticated()")
@RequestMapping(AccessionController.API_BASE)
@Api(tags = { "accession" })
public class AccessionController {
/** The Constant API_BASE. */
public static final String API_BASE = ApiBaseController.APIv1_BASE + "/acn";
@Autowired
private AccessionService accessionService;
/** The short filter service. */
@Autowired
protected ShortFilterService shortFilterService;
/**
* Gets the accession
*
* @param uuid the uuid
* @return the subset
*/
@GetMapping(value = "/{UUID}", produces = { MediaType.APPLICATION_JSON_VALUE })
public Accession getByUuid(@PathVariable("UUID") final UUID uuid) {
return accessionService.getByUuid(uuid);
}
/**
* Gets the accession
*
* @param uuid the uuid
* @return the subset
*/
@GetMapping(value = "/10.{dummy:[0-9]+}/**}", produces = { MediaType.APPLICATION_JSON_VALUE })
public Accession getByDoi(HttpServletRequest request) {
final String fullpath = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
final String doi = fullpath.substring(1);
return accessionService.getByDoi(doi);
}
/**
* List accessions by filterCode or filter
*
* @param page the page
* @param pageSize the page size
* @param direction the direction
* @param sort the sort
* @param filterCode short filter code
* @param filter the filter
* @return the page
* @throws IOException
*/
@PostMapping(value = "/list", produces = { MediaType.APPLICATION_JSON_VALUE })
public FilteredPage<Accession> list(@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) AccessionFilter filter) throws IOException {
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, AccessionFilter.class);
} else {
filterCode = shortFilterService.getCode(filter);
}
return new FilteredPage<>(filterCode, filter, accessionService.list(filter, new PageRequest(page, Integer.min(pageSize, 100), direction, sort)));
}
}
......@@ -28,6 +28,7 @@ import org.genesys.blocks.security.model.AclObjectIdentity;
import org.genesys.blocks.security.model.AclSid;
import org.genesys.blocks.security.serialization.SidPermissions;
import org.genesys.blocks.security.service.CustomAclService;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.model.impl.User;
import org.genesys2.server.service.UserService;
......@@ -55,10 +56,12 @@ import io.swagger.annotations.Api;
* @author Matija Obreza
*/
@RestController("permissionApi1")
@RequestMapping(value = { "/api/v1/permission" })
@RequestMapping(value = PermissionController.API_BASE )
@Api(tags = { "permission" })
public class PermissionController {
public static final String API_BASE = ApiBaseController.APIv1_BASE + "/permission";
private static final Logger LOG = LoggerFactory.getLogger(PermissionController.class);
/** The acl service. */
......
......@@ -22,6 +22,7 @@ import java.util.UUID;
import org.genesys.catalog.api.FilteredPage;
import org.genesys.catalog.service.ShortFilterService;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.service.SubsetService;
import org.genesys2.server.service.filter.SubsetFilter;
......@@ -53,7 +54,7 @@ import io.swagger.annotations.Api;
public class SubsetController {
/** The Constant API_BASE. */
public static final String API_BASE = "/api/v1/subset";
public static final String API_BASE = ApiBaseController.APIv1_BASE + "/subset";
@Autowired
private SubsetService subsetService;
......
......@@ -124,8 +124,6 @@ public class Taxonomy2 extends GlobalVersionedAuditedModel {
return sb.toString();
}
@Transient
public String getTaxonNameHtml() {
final StringBuilder sb = new StringBuilder();
sb.append("<i>").append(genus).append(" ").append(species).append("</i>");
......
......@@ -33,9 +33,10 @@ import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.repository.query.Param;
public interface AccessionRepository extends JpaRepository<Accession, Long>, AccessionRepositoryCustom {
public interface AccessionRepository extends JpaRepository<Accession, Long>, AccessionRepositoryCustom, QueryDslPredicateExecutor<Accession> {
@Query("select a.id from Accession a")
public List<Long> listAccessionsIds(Pageable pageable);
......
/*
* 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.genesys2.server.service;
import java.util.UUID;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.service.filter.AccessionFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
public interface AccessionService {
/**
* List accessions by filter
*
* @param filter the filter
* @param page the page
* @return the page
*/
Page<Accession> list(AccessionFilter filter, Pageable page);
/**
* Gets accession by uuid
*
* @param uuid the uuid
* @return the accession
*/
Accession getByUuid(UUID uuid);
/**
* Gets accession by doi.
*
* @param doi the doi
* @return the by doi
*/
Accession getByDoi(String doi);
}
/*
* 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.genesys2.server.service.filter;
import static org.genesys2.server.model.genesys.QAccession.accession;
import java.util.Set;
import org.genesys.blocks.model.filters.NumberFilter;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.model.filters.UuidModelFilter;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
// TODO: Auto-generated Javadoc
/**
* The Class SubsetFilter.
*
* @author Maxym Borodenko
*/
public class AccessionFilter extends UuidModelFilter {
/** The historic. */
/// Include historic
public Boolean historic;
/** The mls status. */
public Boolean mlsStatus;
/** The available. */
public Boolean available;
/** The crop. */
public String crop;
/** The crop name. */
public String cropName;
/** The holder. */
public InstituteFilter holder;
/** The acce numb. */
public StringFilter acceNumb;
/** The doi. */
public Set<String> doi;
/** The seq no. */
public NumberFilter<Double> seqNo;
/** The taxa. */
public TaxonomyFilter taxa;
/** The samp stat. */
public Set<Integer> sampStat;
/** The origin. */
public CountryFilter origin;
public AccessionGeoFilter geo;
/**
* Builds the query.
*
* @return the predicate
*/
public Predicate buildQuery() {
final BooleanBuilder and = new BooleanBuilder();
super.buildQuery(accession._super._super, and);
if (taxa != null) {
and.and(taxa.buildQuery(accession.taxonomy));
}
if (historic != null) {
and.and(accession.historic.eq(historic));
}
if (crop != null) {
and.and(accession.crop.shortName.eq(crop));
}
if (cropName != null) {
and.and(accession.cropName.eq(cropName));
}
if (holder != null) {
and.and(holder.buildQuery(accession.institute));
}
if (doi != null && !doi.isEmpty()) {
and.and(accession.doi.in(doi));
}
if (acceNumb != null) {
and.and(acceNumb.buildQuery(accession.accessionNumber));
}
if (seqNo != null) {
and.and(seqNo.buildQuery(accession.seqNo));
}
if (sampStat != null && !sampStat.isEmpty()) {
and.and(accession.sampStat.in(sampStat));
}
if (origin != null) {
and.and(origin.buildQuery(accession.countryOfOrigin));
}
if (geo != null) {
and.and(geo.buildQuery(accession.accessionId.geo));
}
if (mlsStatus != null) {
and.and(accession.mlsStatus.eq(mlsStatus));
}
if (available != null) {
and.and(accession.available.eq(available));
}
return and;
}
}
/*
* 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.genesys2.server.service.filter;
import static org.genesys2.server.model.genesys.QAccessionGeo.accessionGeo;
import org.genesys.blocks.model.filters.BasicModelFilter;
import org.genesys.blocks.model.filters.NumberFilter;
import org.genesys2.server.model.genesys.QAccessionGeo;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
/**
* The Class CountryFilter.
*/
public class AccessionGeoFilter extends BasicModelFilter {
/** The longitude. */
public NumberFilter<Double> longitude;
/** The latitude. */
public NumberFilter<Double> latitude;
/** The elevation. */
public NumberFilter<Double> elevation;
/**
* Builds the query.
*
* @return the predicate
*/
public Predicate buildQuery() {
return buildQuery(accessionGeo);
}
/**
* Builds the query.
*
* @param accessiongeo the country
* @return the predicate
*/
public Predicate buildQuery(QAccessionGeo accessiongeo) {
final BooleanBuilder and = new BooleanBuilder();
super.buildQuery(accessiongeo._super, and);
if (longitude != null) {
and.and(longitude.buildQuery(accessiongeo.longitude));
}
if (latitude != null) {
and.and(latitude.buildQuery(accessiongeo.latitude));
}
if (elevation != null) {
and.and(elevation.buildQuery(accessiongeo.elevation));
}
return and;
}
}
/*
* 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.genesys2.server.service.filter;
import static org.genesys2.server.model.impl.QCountry.country;
import java.util.Set;
import org.genesys.blocks.model.filters.BasicModelFilter;
import org.genesys2.server.model.impl.QCountry;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
// TODO: Auto-generated Javadoc
/**
* The Class CountryFilter.
*/
public class CountryFilter extends BasicModelFilter {
/** The iso 3. */
public Set<String> iso3;
/**
* Builds the query.
*
* @return the predicate
*/
public Predicate buildQuery() {
return buildQuery(country);
}
/**
* Builds the query.
*
* @param country the country
* @return the predicate
*/
public Predicate buildQuery(QCountry country) {
final BooleanBuilder and = new BooleanBuilder();
super.buildQuery(country._super, and);
if (iso3 != null && !iso3.isEmpty()) {
and.and(country.code3.in(iso3));
}
return and;
}
}
/*
* 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.genesys2.server.service.filter;
import static org.genesys2.server.model.impl.QFaoInstitute.faoInstitute;
import java.util.Set;
import org.genesys.blocks.model.filters.BasicModelFilter;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.QFaoInstitute;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
// TODO: Auto-generated Javadoc
/**
* Filter for {@link FaoInstitute}.
*/
public class InstituteFilter extends BasicModelFilter {
/** The code. */
public Set<String> code;
/** The country. */
public CountryFilter country;
/** The name. */
public StringFilter name;
/**
* Builds the query.
*
* @return the predicate
*/
public Predicate buildQuery() {
return buildQuery(faoInstitute);
}
/**
* Builds the query.
*
* @param institute the institute
* @return the predicate
*/
public Predicate buildQuery(QFaoInstitute institute) {
final BooleanBuilder and = new BooleanBuilder();
super.buildQuery(institute._super, and);
if (code != null && !code.isEmpty()) {
and.and(institute.code.in(code));
}
if (country != null) {
and.and(country.buildQuery(institute.country));
}
if (name != null) {
and.and(name.buildQuery(institute.fullName));
}
return and;
}
}
......@@ -25,6 +25,7 @@ import org.genesys.blocks.model.filters.UuidModelFilter;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
// TODO: Auto-generated Javadoc
/**
* The Class SubsetFilter.
*
......@@ -50,7 +51,7 @@ public class SubsetFilter extends UuidModelFilter {