PartnerController.java

/*
 * Copyright 2021 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.server.api.v2;

import io.swagger.v3.oas.annotations.tags.Tag;

import org.genesys.server.api.ApiBaseController;
import org.genesys.server.api.Pagination;
import org.genesys.server.api.v2.facade.PartnerApiService;
import org.genesys.server.api.v2.model.impl.FaoInstituteDTO;
import org.genesys.server.api.v2.model.impl.PartnerDTO;
import org.genesys.server.api.v2.model.impl.PartnerInfo;
import org.genesys.server.model.Partner;
import org.genesys.server.model.filters.PartnerFilter;
import org.genesys.server.model.impl.FaoInstitute;
import org.genesys.server.service.filter.InstituteFilter;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.data.domain.Page;
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.RestController;

import java.util.List;
import java.util.Set;
import java.util.UUID;

@RestController("partnerApi2")
@RequestMapping(PartnerController.API_URL)
@Tag(name = "Partner")
public class PartnerController extends FilteredCRUDController<PartnerDTO, Partner, PartnerApiService, PartnerFilter> {
	public static final String API_URL = ApiBaseController.APIv2_BASE + "/partner";

	/**
	 * Gets the partner.
	 *
	 * @param uuid the uuid
	 * @return the partner
	 */
	@GetMapping(value = "/{uuid}")
	public PartnerDTO getPartner(@PathVariable("uuid") final UUID uuid) {
		return serviceFacade.get(uuid);
	}

	/**
	 * Gets the partner.
	 *
	 * @param uuid the uuid
	 * @return the partner
	 */
	@GetMapping(value = "/{uuid}/details")
	public PartnerApiService.PartnerDetails getPartnerDetails(@PathVariable("uuid") final UUID uuid) {
		return serviceFacade.loadPartnerDetails(uuid);
	}

	/**
	 * Delete partner.
	 *
	 * @param uuid the uuid
	 * @param version the version
	 * @return the partner
	 */
	@DeleteMapping(value = "/{uuid},{version}")
	public PartnerDTO deletePartner(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version) {
		return serviceFacade.deletePartner(uuid, version);
	}

	/**
	 * Creates the partner.
	 *
	 * @param source the source
	 * @return the partner
	 */
	@PreAuthorize("hasRole('ADMINISTRATOR')")
	@Override
	public PartnerDTO create(@RequestBody final PartnerDTO source) {
		return super.create(source);
	}

	/**
	 * Update partner.
	 *
	 * @param source the source
	 * @return the partner
	 */
	@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#source.getId(), #source.class.getName(), 'write')")
	@Override
	public PartnerDTO update(@RequestBody final PartnerDTO source) {
		return super.update(source);
	}

	/**
	 * Adds the institutes to the Partner with provided uuid. Existing
	 * {@link FaoInstitute#owner} is force-set to this partner.
	 *
	 * @param uuid the uuid
	 * @param instituteCodes the institute codes
	 * @return the partner
	 */
	@PreAuthorize("hasRole('ADMINISTRATOR')")
	@PostMapping(value = "/addInstitutes/{UUID}")
	public PartnerDTO addInstitutes(@PathVariable("UUID") UUID uuid, @RequestBody Set<String> instituteCodes) {
		return serviceFacade.addInstitutes(uuid, instituteCodes);
	}

	/**
	 * Removes the institutes from the Partner with provided uuid. Effectively this
	 * sets {@link FaoInstitute#owner} to null.
	 *
	 * @param uuid the uuid
	 * @param instituteCodes the institute codes
	 * @return the partner
	 */
	@PreAuthorize("hasRole('ADMINISTRATOR')")
	@PostMapping(value = "/removeInstitutes/{UUID}")
	public PartnerDTO removeInstitutes(@PathVariable("UUID") UUID uuid, @RequestBody Set<String> instituteCodes) {
		return serviceFacade.removeInstitutes(uuid, instituteCodes);
	}

	/**
	 * Gets partners institutes.
	 *
	 * @param uuid the uuid
	 * @return the partner
	 */
	@PostMapping(value = "/{UUID}/institutes")
	public Page<FaoInstituteDTO> listInstitutes(@PathVariable("UUID") final UUID uuid,
		@RequestBody(required = false) InstituteFilter filter, @ParameterObject final Pagination page) {
		return serviceFacade.loadPartnerInstitutes(uuid, filter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, Sort.Direction.DESC, "accessionCount"));
	}

	/**
	 * My partners.
	 *
	 * @param page the page
	 * @param partnerFilter the partner filter
	 * @return the page
	 */
	@PostMapping(value = "/list-mine")
	public Page<PartnerDTO> myPartners(@ParameterObject final Pagination page, @RequestBody final PartnerFilter partnerFilter) {
		return serviceFacade.listPartnersForCurrentUser(partnerFilter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE));
	}

	/**
	 * List all partners.
	 *
	 * @return the list of PartnerInfo
	 */
	@GetMapping(value = "/list-all")
	public List<PartnerInfo> listAllPartners() {
		return serviceFacade.listAllPartners();
	}

	/**
	 * List my partners.
	 *
	 * @return the list of PartnerInfo
	 */
	@GetMapping(value = "/list-all-mine")
	public List<PartnerInfo> listMyPartners() {
		return serviceFacade.listMyPartners();
	}
}