GermplasmController.java

/*
 * Copyright 2020 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.gringlobal.brapi.v1;

import org.gringlobal.brapi.BaseBrAPIController;
import org.gringlobal.brapi.BrAPIPage;
import org.gringlobal.brapi.BrAPIResponse;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.brapi.Germplasm;
import org.gringlobal.service.BrAPIService;
import org.gringlobal.service.filter.AccessionFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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;

/**
 * The BrAPI GermplasmController.
 */
@RestController("brapiGermplasm")
@RequestMapping(GermplasmController.API_URL)
public class GermplasmController extends BaseBrAPIController {

	/** The Constant API_URL. */
	public static final String API_URL = BaseBrAPIController.BRAPIv1_BASE + "/germplasm";

	@Autowired
	private BrAPIService brapiService;

	/**
	 * Search for germplasm.
	 *
	 * @param germplasmName the germplasm name
	 * @param germplasmPUI the germplasm PUI
	 * @param germplasmDbId the germplasm db id
	 * @param page the page
	 * @return list of germplasm
	 */
	@GetMapping(value = "", produces = { MediaType.APPLICATION_JSON_VALUE })
	public BrAPIResponse<Germplasm> germplasmSearch(@RequestParam(name = "germplasmName", required = false) String germplasmName,
			@RequestParam(name = "germplasmPUI", required = false) String germplasmPUI,
			@RequestParam(name = "germplasmDbId", required = false) Long germplasmDbId, final BrAPIPage page) throws SearchException {

		return new BrAPIResponse<>(brapiService.searchGermplasm(germplasmName, germplasmPUI, germplasmDbId, page.toPageRequest()));
	}

	/**
	 * Search for germplasm by accession filter.
	 *
	 * @param filter the accession filter
	 * @param page the page
	 * @return list of germplasm matching the filter
	 */
	@PostMapping(value = "", produces = { MediaType.APPLICATION_JSON_VALUE })
	public BrAPIResponse<Germplasm> germplasmSearch(@RequestBody AccessionFilter filter, final BrAPIPage page) throws SearchException {
		return new BrAPIResponse<>(brapiService.searchGermplasm(filter, page.toPageRequest()));
	}

	/**
	 * Get germplasm data by id
	 *
	 * @param germplasmId the germplasmId
	 * @return found germplasm
	 */
	@GetMapping(value = "/{germplasmId:.+}", produces = { MediaType.APPLICATION_JSON_VALUE })
	public BrAPIResponse<Germplasm> getGermplasmById(@PathVariable("germplasmId") long germplasmId) {
		return new BrAPIResponse<>(brapiService.getGermplasmById(germplasmId));
	}

}