CoreController.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.io.IOException;
import java.sql.SQLException;

import org.gringlobal.brapi.BaseBrAPIController;
import org.gringlobal.brapi.BrAPIPage;
import org.gringlobal.brapi.v2.BrAPIv2Facade;
import org.gringlobal.brapi.v2.query.BrAPIQuery;
import org.gringlobal.brapi.v2.query.ProgramSearchQuery;
import org.gringlobal.brapi.v2.query.ProgramSearchBody;
import org.gringlobal.brapi.v2.query.StudySearchQuery;
import org.gringlobal.brapi.v2.query.StudySearchBody;
import org.gringlobal.brapi.v2.query.TrialSearchQuery;
import org.gringlobal.brapi.v2.query.TrialSearchBody;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.tags.Tag;
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.core.program.Program;
import uk.ac.hutton.ics.brapi.resource.core.study.Study;
import uk.ac.hutton.ics.brapi.resource.core.trial.Trial;

@RestController("brApi21core")
@Tag(name = "BrAPI Core")
@RequestMapping(BaseBrAPIController.BRAPIv2_BASE)
public class CoreController extends BaseBrAPIController {

	public static final String ENDPOINT_STUDIES = "/studies";
	public static final String ENDPOINT_PROGRAMS = "/programs";
	public static final String ENDPOINT_TRIALS = "/trials";

	@Autowired
	private BrAPIv2Facade brAPIv2Facade;

	@GetMapping("/commoncropnames")
	public BaseResult<ArrayResult<String>> getCommonCropNames(final BrAPIPage page) throws SQLException, IOException {
		var p = brAPIv2Facade.getCommonCropNames(page.toPageRequest());
		return arrayResult(p);
	}


	@GetMapping("/studytypes")
	public BaseResult<ArrayResult<String>> getStudyTypes(final BrAPIPage page) throws Exception {
		var p = brAPIv2Facade.getStudyTypes(page.toPageRequest());
		return arrayResult(p);
	}

	@GetMapping(ENDPOINT_STUDIES + "/{studyDbId}")
	public BaseResult<Study> getStudy(@PathVariable long studyDbId) throws Exception {
		var study = brAPIv2Facade.getStudy(studyDbId);
		return new BaseResult<Study>().setResult(study);
	}

	@GetMapping(ENDPOINT_STUDIES)
	public BaseResult<ArrayResult<Study>> listStudies(@ParameterObject StudySearchQuery request, @ParameterObject BrAPIQuery page) throws Exception {
		var p = brAPIv2Facade.listStudies(request, page.toPageRequest(100));
		return arrayResult(p);
	}

	@PostMapping("/search" + ENDPOINT_STUDIES)
	public BaseResult<ArrayResult<Study>> searchStudies(@RequestBody(required = true) StudySearchBody request) throws Exception {
		var p = brAPIv2Facade.searchStudies(request, BrAPIQuery.toPageRequest(1000, request.getPage()));
		return arrayResult(p);
	}

	@GetMapping(ENDPOINT_TRIALS + "/{trialDbId}")
	public BaseResult<Trial> getTrial(@PathVariable long trialDbId) {
		var trial = brAPIv2Facade.getTrial(trialDbId);
		return new BaseResult<Trial>().setResult(trial);
	}

	@GetMapping(ENDPOINT_TRIALS)
	public BaseResult<ArrayResult<Trial>> listTrials(@ParameterObject TrialSearchQuery request, @ParameterObject BrAPIQuery page) throws Exception {
		var p = brAPIv2Facade.listTrials(request, BrAPIQuery.toPageRequest(1000, page));
		return arrayResult(p);
	}

	@PostMapping("/search" + ENDPOINT_TRIALS)
	public BaseResult<ArrayResult<Trial>> searchTrials(@RequestBody(required = true) TrialSearchBody request) throws Exception {
		var p = brAPIv2Facade.searchTrials(request, BrAPIQuery.toPageRequest(1000, request.getPage()));
		return arrayResult(p);
	}

	@GetMapping( ENDPOINT_PROGRAMS + "/{id}")
	public BaseResult<Program> getProgram(@PathVariable long id) {
		var program = brAPIv2Facade.getProgram(id);
		return new BaseResult<Program>().setResult(program);
	}

	@GetMapping(ENDPOINT_PROGRAMS)
	public BaseResult<ArrayResult<Program>> listPrograms(@ParameterObject ProgramSearchQuery request, @ParameterObject BrAPIPage page) throws Exception {
		var p = brAPIv2Facade.listPrograms(request, page.toPageRequest());
		return arrayResult(p);
	}

	@PostMapping("/search" + ENDPOINT_PROGRAMS)
	public BaseResult<ArrayResult<Program>> searchPrograms(@RequestBody(required = true) ProgramSearchBody request) throws Exception {
		var p = brAPIv2Facade.searchPrograms(request, BrAPIQuery.toPageRequest(1000, request.getPage()));
		return arrayResult(p);
	}
}