GermplasmController.java

/*
 * Copyright 2019 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.brapi;

import java.util.UUID;

import org.genesys.brapi.model.BrAPIPage;
import org.genesys.brapi.model.BrAPIResponse;
import org.genesys.brapi.model.Germplasm;
import org.genesys.brapi.service.BrAPIService;
import org.genesys.server.service.filter.AccessionFilter;
import org.genesys.server.exception.NoSuchAccessionException;
import org.genesys.server.exception.SearchException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController("brapiGermplasm")
@RequestMapping(value = { "/brapi/v1" })
public class GermplasmController extends BaseBrAPIController {

	@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 the br API response
	 */
	@RequestMapping(value = "/germplasm", method = { RequestMethod.GET}, produces = { MediaType.APPLICATION_JSON_VALUE })
	public @ResponseBody BrAPIResponse<Germplasm> germplasmSearch(@RequestParam(name = "germplasmName", required = false) String germplasmName,
			@RequestParam(name = "germplasmPUI", required = false) String germplasmPUI, @RequestParam(name = "germplasmDbId", required = false) UUID germplasmDbId, final BrAPIPage page) throws SearchException {

		return new BrAPIResponse<>(brapiService.searchGermplasm(germplasmName, germplasmPUI, germplasmDbId, page.toPageRequest()));
	}
	
	/**
	 * @param filter
	 * @param page
	 * @return
	 */
	@PostMapping(value = "/germplasm", 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
	 * 
	 * @return
	 * 
	 * @return
	 * @throws NoSuchAccessionException
	 */
	@RequestMapping(value = "/germplasm/{germplasmId:.+}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
	public @ResponseBody BrAPIResponse<Germplasm> getGermplasmById(@PathVariable("germplasmId") UUID germplasmId) throws NoSuchAccessionException {
		return new BrAPIResponse<>(brapiService.getGermplasmById(germplasmId));
	}
}