TaxonomyController.java

  1. /*
  2.  * Copyright 2020 Global Crop Diversity Trust
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */

  16. package org.genesys.server.api.v1;

  17. import java.io.IOException;
  18. import java.util.List;

  19. import org.genesys.server.api.ApiBaseController;
  20. import org.genesys.server.api.FilteredPage;
  21. import org.genesys.server.api.Pagination;
  22. import org.genesys.server.exception.NotFoundElement;
  23. import org.genesys.server.model.genesys.Taxonomy2;
  24. import org.genesys.server.model.grin.TaxonomySpecies;
  25. import org.genesys.server.service.TaxonomyService;
  26. import org.genesys.server.service.ShortFilterService.FilterInfo;
  27. import org.genesys.server.service.filter.TaxonomyExtraFilter;
  28. import org.genesys.server.service.filter.TaxonomyFilter;
  29. import org.genesys.server.service.filter.TaxonomySpeciesFilter;
  30. import org.genesys.server.exception.SearchException;
  31. import org.genesys.server.service.worker.ShortFilterProcessor;
  32. import org.springdoc.api.annotations.ParameterObject;
  33. import org.springframework.beans.factory.annotation.Autowired;
  34. import org.springframework.data.domain.Sort;
  35. import org.springframework.http.MediaType;
  36. import org.springframework.security.access.prepost.PreAuthorize;
  37. import org.springframework.web.bind.annotation.GetMapping;
  38. import org.springframework.web.bind.annotation.PathVariable;
  39. import org.springframework.web.bind.annotation.PostMapping;
  40. import org.springframework.web.bind.annotation.RequestBody;
  41. import org.springframework.web.bind.annotation.RequestMapping;
  42. import org.springframework.web.bind.annotation.RequestParam;
  43. import org.springframework.web.bind.annotation.RestController;

  44. import io.swagger.annotations.Api;

  45. /**
  46.  * @author Maxym Borodenko
  47.  */
  48. @RestController("taxonomyApi1")
  49. @PreAuthorize("isAuthenticated()")
  50. @RequestMapping(value = TaxonomyController.CONTROLLER_URL)
  51. @Api(tags = {"taxonomy"})
  52. public class TaxonomyController extends ApiBaseController {

  53.     /** The Constant CONTROLLER_URL. */
  54.     public static final String CONTROLLER_URL = ApiBaseController.APIv1_BASE + "/taxonomy";

  55.     /** The taxonomy service **/
  56.     @Autowired
  57.     private TaxonomyService taxonomyService;

  58.     @Autowired
  59.     protected ShortFilterProcessor shortFilterProcessor;

  60.     /**
  61.      * Gets the Taxonomy2.
  62.      *
  63.      * @param id the id
  64.      * @return the taxonomy2
  65.      * @throws NotFoundElement the not found element
  66.      */
  67.     @GetMapping(value = "/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
  68.     public Taxonomy2 get(@PathVariable("id") final Long id) throws NotFoundElement {
  69.         LOG.debug("Load taxonomy2 by id {}", id);
  70.         Taxonomy2 taxonomy2 = taxonomyService.get(id);
  71.         if (taxonomy2 == null) {
  72.             throw new NotFoundElement("No such taxonomy2.");
  73.         }
  74.         return taxonomy2;
  75.     }

  76.     @PostMapping(value = "/", produces = { MediaType.APPLICATION_JSON_VALUE })
  77.     public List<Taxonomy2> listTaxonomies(@RequestBody final TaxonomyFilter taxonomyFilter) {
  78.         LOG.info("Listing taxonomies");
  79.         return taxonomyService.list(taxonomyFilter);
  80.     }

  81.     /**
  82.      * List of Taxonomy2 entities with included count of accessions.
  83.      *
  84.      * @param page the page
  85.      * @param filter the taxonomy2 filter
  86.      * @return the page
  87.      */
  88.     @PostMapping(value = "/list", produces = { MediaType.APPLICATION_JSON_VALUE })
  89.     public FilteredPage<TaxonomyService.Taxonomy2Info, TaxonomyExtraFilter> listTaxonomies(@RequestParam(name = "f", required = false) String filterCode, @ParameterObject final Pagination page, @RequestBody final TaxonomyExtraFilter filter) throws IOException {
  90.        
  91.         FilterInfo<TaxonomyExtraFilter> filterInfo = shortFilterProcessor.processFilter(filterCode, filter, TaxonomyExtraFilter.class);
  92.        
  93.         return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, taxonomyService.list(filterInfo.filter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, Sort.Direction.ASC, "genus", "species", "id")));
  94.     }

  95.     /**
  96.      * List taxonomies species.
  97.      *
  98.      * @param page the page
  99.      * @param filter the filter
  100.      * @return the page
  101.      * @throws SearchException
  102.      */
  103.     @PostMapping(value = "/grin-species", produces = { MediaType.APPLICATION_JSON_VALUE })
  104.     public FilteredPage<TaxonomySpecies, TaxonomySpeciesFilter> listSpecies(@ParameterObject final Pagination page, @RequestBody final TaxonomySpeciesFilter filter) throws SearchException {
  105.         return new FilteredPage<>(filter, taxonomyService.listSpecies(filter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, Sort.Direction.ASC, "genusSpecies", "subtaxa")));
  106.     }

  107.     /**
  108.      * Sets GRIN species to taxonomy2.
  109.      *
  110.      * @param taxonomy2Id the ID of Taxonomy2
  111.      * @param customGrinSpeciesId the ID of GRIN TaxonomySpecies
  112.      * @return updated record of Taxonomy2
  113.      */
  114.     @PostMapping(value = "/set-grin-species/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
  115.     public Taxonomy2 setGrinSpecies(@PathVariable("id") final long taxonomy2Id, @RequestParam(required = false, value = "customGrinSpeciesId") final Long customGrinSpeciesId) {
  116.         return taxonomyService.setGrinSpecies(taxonomy2Id, customGrinSpeciesId);
  117.     }
  118. }