PartnerController.java

  1. /*
  2.  * Copyright 2018 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.Set;
  19. import java.util.UUID;

  20. import org.genesys.server.api.ApiBaseController;
  21. import org.genesys.server.api.FilteredPage;
  22. import org.genesys.server.api.Pagination;
  23. import org.genesys.server.model.Partner;
  24. import org.genesys.server.model.filters.PartnerFilter;
  25. import org.genesys.server.model.impl.FaoInstitute;
  26. import org.genesys.server.service.PartnerService;
  27. import org.genesys.server.service.ShortFilterService.FilterInfo;
  28. import org.genesys.server.service.filter.InstituteFilter;
  29. import org.genesys.server.exception.SearchException;
  30. import org.genesys.server.service.worker.ShortFilterProcessor;
  31. import org.springdoc.api.annotations.ParameterObject;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.data.domain.Page;
  34. import org.springframework.data.domain.Sort;
  35. import org.springframework.security.access.prepost.PreAuthorize;
  36. import org.springframework.web.bind.annotation.DeleteMapping;
  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.PutMapping;
  41. import org.springframework.web.bind.annotation.RequestBody;
  42. import org.springframework.web.bind.annotation.RequestMapping;
  43. import org.springframework.web.bind.annotation.RequestParam;
  44. import org.springframework.web.bind.annotation.RestController;

  45. import io.swagger.annotations.Api;

  46. /**
  47.  * Rest controller for Partner.
  48.  *
  49.  * @author Andrey Lugovskoy.
  50.  */
  51. @RestController("partnerApi1")
  52. @RequestMapping(org.genesys.server.api.v1.PartnerController.CONTROLLER_URL)
  53. @Api(tags = { "partner" })
  54. public class PartnerController extends ApiBaseController {

  55.     /** The Constant CONTROLLER_URL. */
  56.     public static final String CONTROLLER_URL = ApiBaseController.APIv1_BASE + "/partner";

  57.     @Autowired
  58.     private PartnerService partnerService;

  59.     /** The short filter service. */
  60.     @Autowired
  61.     protected ShortFilterProcessor shortFilterProcessor;

  62.     /**
  63.      * Gets the partner.
  64.      *
  65.      * @param uuid the uuid
  66.      * @return the partner
  67.      */
  68.     @GetMapping(value = "/{uuid}")
  69.     public Partner getPartner(@PathVariable("uuid") final UUID uuid) {
  70.         return partnerService.load(uuid);
  71.     }

  72.     /**
  73.      * Gets the partner.
  74.      *
  75.      * @param uuid the uuid
  76.      * @return the partner
  77.      */
  78.     @GetMapping(value = "/{uuid}/details")
  79.     public PartnerService.PartnerDetails getPartnerDetails(@PathVariable("uuid") final UUID uuid) {
  80.         return partnerService.loadPartnerDetails(uuid);
  81.     }

  82.     /**
  83.      * Delete partner.
  84.      *
  85.      * @param uuid the uuid
  86.      * @param version the version
  87.      * @return the partner
  88.      */
  89.     @DeleteMapping(value = "/{uuid},{version}")
  90.     public Partner deletePartner(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version) {
  91.         return partnerService.remove(partnerService.loadPartner(uuid, version));
  92.     }

  93.     /**
  94.      * Creates the partner.
  95.      *
  96.      * @param source the source
  97.      * @return the partner
  98.      */
  99.     @PreAuthorize("hasRole('ADMINISTRATOR')")
  100.     @PostMapping(value = "/create")
  101.     public Partner createPartner(@RequestBody final Partner source) {
  102.         return partnerService.create(source);
  103.     }

  104.     /**
  105.      * Update partner.
  106.      *
  107.      * @param source the source
  108.      * @return the partner
  109.      */
  110.     @PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#source.getId(), #source.class.getName(), 'write')")
  111.     @PutMapping(value = "/update")
  112.     public Partner updatePartner(@RequestBody final Partner source) {
  113.         return partnerService.update(source);
  114.     }

  115.     /**
  116.      * Adds the institutes to the Partner with provided uuid. Existing
  117.      * {@link FaoInstitute#owner} is force-set to this partner.
  118.      *
  119.      * @param uuid the uuid
  120.      * @param instituteCodes the institute codes
  121.      * @return the partner
  122.      */
  123.     @PreAuthorize("hasRole('ADMINISTRATOR')")
  124.     @PostMapping(value = "/addInstitutes/{UUID}")
  125.     public Partner addInstitutes(@PathVariable("UUID") UUID uuid, @RequestBody Set<String> instituteCodes) {
  126.         Partner partner = partnerService.get(uuid);
  127.         return partnerService.addInstitutes(partner, instituteCodes);
  128.     }

  129.     /**
  130.      * Removes the institutes from the Partner with provided uuid. Effectively this
  131.      * sets {@link FaoInstitute#owner} to null.
  132.      *
  133.      * @param uuid the uuid
  134.      * @param instituteCodes the institute codes
  135.      * @return the partner
  136.      */
  137.     @PreAuthorize("hasRole('ADMINISTRATOR')")
  138.     @PostMapping(value = "/removeInstitutes/{UUID}")
  139.     public Partner removeInstitutes(@PathVariable("UUID") UUID uuid, @RequestBody Set<String> instituteCodes) {
  140.         Partner partner = partnerService.get(uuid);
  141.         return partnerService.removeInstitutes(partner, instituteCodes);
  142.     }

  143.     /**
  144.      * List partners.
  145.      *
  146.      * @param page the page
  147.      * @param filterCode the filter code
  148.      * @param filter the partner filter
  149.      * @return the page
  150.      * @throws IOException
  151.      */
  152.     @PostMapping(value = "/list")
  153.     public FilteredPage<Partner, PartnerFilter> listPartners(@RequestParam(name = "f", required = false) String filterCode, @ParameterObject final Pagination page,
  154.             @RequestBody(required = false) PartnerFilter filter) throws IOException, SearchException {

  155.         FilterInfo<PartnerFilter> filterInfo = shortFilterProcessor.processFilter(filterCode, filter, PartnerFilter.class);
  156.         return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, partnerService.list(filterInfo.filter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE)));
  157.     }

  158.     /**
  159.      * Gets partners institutes.
  160.      *
  161.      * @param uuid the uuid
  162.      * @return the partner
  163.      */
  164.     @PostMapping(value = "/{UUID}/institutes")
  165.     public Page<FaoInstitute> listInstitutes(@PathVariable("UUID") final UUID uuid,
  166.             @RequestBody(required = false) InstituteFilter filter, @ParameterObject final Pagination page) {
  167.         return partnerService.loadPartnerInstitutes(uuid, filter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, Sort.Direction.DESC, "accessionCount"));
  168.     }

  169.     /**
  170.      * My partners.
  171.      *
  172.      * @param page the page
  173.      * @param partnerFilter the partner filter
  174.      * @return the page
  175.      */
  176.     @PostMapping(value = "/list-mine")
  177.     public Page<Partner> myPartners(@ParameterObject final Pagination page, @RequestBody final PartnerFilter partnerFilter) {
  178.         return partnerService.listPartnersForCurrentUser(partnerFilter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE));
  179.     }
  180. }