GermplasmController.java

/*
 * Copyright 2023 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.v2.impl;

import java.util.Collection;
import org.gringlobal.brapi.BaseBrAPIController;
import org.gringlobal.brapi.BrAPIPage;
import org.gringlobal.brapi.v2.BrAPIv2Facade;
import org.gringlobal.brapi.v2.BrAPIv2MapperX;
import org.gringlobal.brapi.v2.query.BrAPIQuery;
import org.gringlobal.brapi.v2.query.GermplasmQuery;
import org.gringlobal.brapi.v2.query.GermplasmSearchBody;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import uk.ac.hutton.ics.brapi.resource.base.ArrayResult;
import uk.ac.hutton.ics.brapi.resource.base.BaseResult;
import uk.ac.hutton.ics.brapi.resource.germplasm.germplasm.Germplasm;
import uk.ac.hutton.ics.brapi.resource.germplasm.germplasm.Mcpd;

@RestController("brApi21germplasm")
@Tag(name = "BrAPI Germplasm")
@RequestMapping(BaseBrAPIController.BRAPIv2_BASE)
@Slf4j
public class GermplasmController extends BaseBrAPIController {

	public static final String ENDPOINT_GERMPLASM = "/germplasm";

	@Autowired
	private BrAPIv2MapperX brAPIv2Mapper;

	@Autowired
	private BrAPIv2Facade brAPIv2Facade;

	@GetMapping(ENDPOINT_GERMPLASM)
	public BaseResult<ArrayResult<Germplasm>> getGermplasm(@ParameterObject GermplasmQuery query, @ParameterObject final BrAPIPage page) throws SearchException {
		var p = brAPIv2Facade.listAccessions(brAPIv2Mapper.filterAccession(query), BrAPIPage.make(page));
		return arrayResult(p);
	}

	@GetMapping(ENDPOINT_GERMPLASM + "/{germplasmDbId}")
	public BaseResult<Germplasm> getGermplasmById(@PathVariable("germplasmDbId")  long germplasmDbId) {
		return new BaseResult<>(brAPIv2Facade.getAccession(germplasmDbId), 0, 0, 0);
	}

	@GetMapping(ENDPOINT_GERMPLASM + "/{germplasmDbId}/mcpd")
	public BaseResult<Mcpd> getGermplasmMcpd(@PathVariable("germplasmDbId") long germplasmDbId) {
		return new BaseResult<>(brAPIv2Facade.getAccessionMcpd(germplasmDbId), 0, 0, 0);
	}

	/** Create new Germplasm entities on this server */
	@PostMapping(ENDPOINT_GERMPLASM)
	public BaseResult<Collection<Germplasm>> postGermplasm(@RequestBody Germplasm[] accessions) throws Exception {
		// var p = brAPIv2Facade.createAccessions(accessions);
		// return createResult(p, STATUS_CREATED);
		throw new RuntimeException("Not implemented");
	}

	/** Update germplasm */
	@PutMapping(ENDPOINT_GERMPLASM + "/{germplasmDbId}")
	public BaseResult<Germplasm> putGermplasmById(@PathVariable("germplasmDbId") long germplasmDbId, @RequestBody Germplasm accession) throws Exception {
		// return new BaseResult<>(brAPIv2Facade.updateAccession(germplasmDbId, accession), STATUS_UPDATED);
		throw new RuntimeException("Not implemented");
	}

	@PostMapping("/search" + ENDPOINT_GERMPLASM)
	public BaseResult<ArrayResult<Germplasm>> search(@RequestBody GermplasmSearchBody search) throws Exception {
		log.info("Searching {}", search);
		var p = brAPIv2Facade.listAccessions(brAPIv2Mapper.filterAccession(search), BrAPIQuery.toPageRequest(1000, search.getPage()));
		return arrayResult(p);
	}

}