PartnerController.java

/*
 * 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.server.api.v1;

import java.io.IOException;
import java.util.Set;
import java.util.UUID;

import org.genesys.server.api.ApiBaseController;
import org.genesys.server.api.FilteredPage;
import org.genesys.server.api.Pagination;
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.PartnerService;
import org.genesys.server.service.ShortFilterService.FilterInfo;
import org.genesys.server.service.filter.InstituteFilter;
import org.genesys.server.exception.SearchException;
import org.genesys.server.service.worker.ShortFilterProcessor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
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.PutMapping;
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 io.swagger.annotations.Api;

/**
 * Rest controller for Partner.
 *
 * @author Andrey Lugovskoy.
 */
@RestController("partnerApi1")
@RequestMapping(org.genesys.server.api.v1.PartnerController.CONTROLLER_URL)
@Api(tags = { "partner" })
public class PartnerController extends ApiBaseController {

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

	@Autowired
	private PartnerService partnerService;

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

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

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

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

	/**
	 * Creates the partner.
	 *
	 * @param source the source
	 * @return the partner
	 */
	@PreAuthorize("hasRole('ADMINISTRATOR')")
	@PostMapping(value = "/create")
	public Partner createPartner(@RequestBody final Partner source) {
		return partnerService.create(source);
	}

	/**
	 * Update partner.
	 *
	 * @param source the source
	 * @return the partner
	 */
	@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#source.getId(), #source.class.getName(), 'write')")
	@PutMapping(value = "/update")
	public Partner updatePartner(@RequestBody final Partner source) {
		return partnerService.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 Partner addInstitutes(@PathVariable("UUID") UUID uuid, @RequestBody Set<String> instituteCodes) {
		Partner partner = partnerService.get(uuid);
		return partnerService.addInstitutes(partner, 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 Partner removeInstitutes(@PathVariable("UUID") UUID uuid, @RequestBody Set<String> instituteCodes) {
		Partner partner = partnerService.get(uuid);
		return partnerService.removeInstitutes(partner, instituteCodes);
	}

	/**
	 * List partners.
	 *
	 * @param page the page
	 * @param filterCode the filter code
	 * @param filter the partner filter
	 * @return the page
	 * @throws IOException
	 */
	@PostMapping(value = "/list")
	public FilteredPage<Partner, PartnerFilter> listPartners(@RequestParam(name = "f", required = false) String filterCode, @ParameterObject final Pagination page,
			@RequestBody(required = false) PartnerFilter filter) throws IOException, SearchException {

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

	/**
	 * Gets partners institutes.
	 *
	 * @param uuid the uuid
	 * @return the partner
	 */
	@PostMapping(value = "/{UUID}/institutes")
	public Page<FaoInstitute> listInstitutes(@PathVariable("UUID") final UUID uuid,
			@RequestBody(required = false) InstituteFilter filter, @ParameterObject final Pagination page) {
		return partnerService.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<Partner> myPartners(@ParameterObject final Pagination page, @RequestBody final PartnerFilter partnerFilter) {
		return partnerService.listPartnersForCurrentUser(partnerFilter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE));
	}
}