PhenotypingController.java

/*
 * Copyright 2024 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 java.util.HashSet;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

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.ObservationSearchBody;
import org.gringlobal.brapi.v2.query.ObservationSearchQuery;
import org.gringlobal.brapi.v2.query.ObservationUnitSearchQuery;
import org.gringlobal.brapi.v2.query.ObservationVariableSearchQuery;
import org.gringlobal.brapi.v2.query.TraitSearchQuery;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.CropTraitObservation;
import org.gringlobal.model.CropTraitObservationData;
import org.gringlobal.service.filter.CropTraitFilter;
import org.gringlobal.service.filter.MethodFilter;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
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 uk.ac.hutton.ics.brapi.resource.base.ArrayResult;
import uk.ac.hutton.ics.brapi.resource.base.BaseResult;
import uk.ac.hutton.ics.brapi.resource.base.Status;
import uk.ac.hutton.ics.brapi.resource.germplasm.attribute.Trait;
import uk.ac.hutton.ics.brapi.resource.phenotyping.observation.Observation;
import uk.ac.hutton.ics.brapi.resource.phenotyping.observation.ObservationLevel;
import uk.ac.hutton.ics.brapi.resource.phenotyping.observation.ObservationUnit;
import uk.ac.hutton.ics.brapi.resource.phenotyping.observation.ObservationVariable;

@RestController("brApi21phenotyping")
@Tag(name = "BrAPI Phenotyping")
@RequestMapping(BaseBrAPIController.BRAPIv2_BASE)
@Slf4j
public class PhenotypingController extends BaseBrAPIController {

	public static final String ENDPOINT_OBSERVATION_LEVELS = "/observationlevels";
	public static final String ENDPOINT_OBSERVATION_UNITS = "/observationunits";
	public static final String ENDPOINT_TRAITS = "/traits";
	public static final String ENDPOINT_VARIABLES = "/variables";
	public static final String ENDPOINT_OBSERVATIONS = "/observations";

	@Autowired
	private BrAPIv2Facade brAPIv2Facade;

	/**
	 * We support a fixed set of observation levels mapped to GGCE schema:
	 * 
	 * {@link CropTraitObservation} represents things at "plant" level while {@link CropTraitObservationData} has "sample" level data.
	 * 
	 * @return
	 */
	@GetMapping(ENDPOINT_OBSERVATION_LEVELS)
	public BaseResult<ArrayResult<ObservationLevel>> getObservationLevels() {
		return arrayResult(new PageImpl<>(List.of(
			new ObservationLevel().setLevelCode("plot").setLevelName("Inventory").setLevelOrder(8)
			// new ObservationLevel().setLevelCode("plant").setLevelName("Plant").setLevelOrder(10)
		)));
	}

	/**
	 * An Observation Unit is anything that is being observed. Typically, this is a
	 * Plot or a Plant, but it could include things like Fields or Samples. The
	 * Observation Level defines the type of Observation Unit.
	 * @throws SearchException 
	 */
	@GetMapping(ENDPOINT_OBSERVATION_UNITS)
	public BaseResult<ArrayResult<ObservationUnit>> getObservationUnits(@ParameterObject final ObservationUnitSearchQuery query, @ParameterObject BrAPIPage page) throws SearchException {
		log.info("{}: page={} q={}", ENDPOINT_OBSERVATION_UNITS, page, query);
		var p = brAPIv2Facade.listObservationUnits(query, BrAPIPage.make(page));
		return arrayResult(p);
	}

	@GetMapping(ENDPOINT_TRAITS)
	public BaseResult<ArrayResult<Trait>> getTraits(@ParameterObject final TraitSearchQuery query, @ParameterObject final BrAPIPage page) throws Exception {
		log.info("Query page={} {}", page, query);
		var filter = new CropTraitFilter();
		if (query.getTraitDbId() != null) filter.id().add(query.getTraitDbId());
		if (query.getStudyDbId() != null) filter.method.id().add(query.getStudyDbId());

		var p = brAPIv2Facade.listTraits(filter, BrAPIPage.make(page));
		return arrayResult(p);
	}

	@GetMapping(ENDPOINT_TRAITS + "/{traitDbId}")
	public BaseResult<Trait> getTraitById(@PathVariable("traitDbId") long traitDbId) {
		log.info("Trait {}", traitDbId);
		return new BaseResult<>(brAPIv2Facade.getTrait(traitDbId), List.of());
	}

	/** Create new traits on this server */
	@PostMapping(ENDPOINT_TRAITS)
	public BaseResult<ArrayResult<Trait>> postTraits(@RequestBody Trait[] traits) throws Exception {
		// var p = brAPIv2Facade.createTraits(traits);
		// return arrayResult(p);
		throw new RuntimeException("Not implemented");
	}

	/** Update trait */
	@PutMapping(ENDPOINT_TRAITS + "/{traitDbId}")
	public BaseResult<Trait> putTraitById(@PathVariable("traitDbId") long traitDbId, @RequestBody Trait trait) throws Exception {
		// return new BaseResult<>(brAPIv2Facade.updateTrait(traitDbId, trait), STATUS_UPDATED);
		throw new RuntimeException("Not implemented");
	}


	@GetMapping(ENDPOINT_VARIABLES)
	public BaseResult<ArrayResult<ObservationVariable>> getObservationVariables(@ParameterObject final ObservationVariableSearchQuery query, @ParameterObject final BrAPIPage page) throws Exception {

		var filter = new CropTraitFilter();
		if (query.getObservationVariableDbId() != null) filter.id().add(query.getObservationVariableDbId());
		filter.method = new MethodFilter();
		filter.method.id = new HashSet<>();
		if (query.getStudyDbId() != null) filter.method.id().add(query.getStudyDbId());
		if (query.getTrialDbId() != null) filter.method.id().add(query.getTrialDbId());

		var p = brAPIv2Facade.listObservationVariables(filter, BrAPIPage.make(page));
		return arrayResult(p);
	}

	@GetMapping(ENDPOINT_VARIABLES + "/{observationVariableDbId}")
	public BaseResult<ObservationVariable> getObservationVariableById(@PathVariable("observationVariableDbId") long observationVariableDbId) {
		return new BaseResult<>(brAPIv2Facade.getObservationVariable(observationVariableDbId), List.of());
	}

	/** Create new traits on this server */
	@PostMapping(ENDPOINT_VARIABLES)
	public BaseResult<Collection<ObservationVariable>> postObservationVariable(@RequestBody ObservationVariable[] variables) throws Exception {
		// var p = brAPIv2Facade.createObservationVariables(variables);
		// return new BaseResult<>(p.getContent(), STATUS_CREATED);
		throw new RuntimeException("Not implemented");
	}

	/** Update trait */
	@PutMapping(ENDPOINT_VARIABLES + "/{observationVariableDbId}")
	public BaseResult<ObservationVariable> putObservationVariableById(@PathVariable("observationVariableDbId") long observationVariableDbId, @RequestBody ObservationVariable variables) throws Exception {
		// return new BaseResult<>(brAPIv2Facade.updateObservationVariable(observationVariableDbId, variables), STATUS_UPDATED );
		throw new RuntimeException("Not implemented");
	}

	@GetMapping(ENDPOINT_OBSERVATIONS + "/{observationDbId}")
	public BaseResult<Observation> getObservationById(@PathVariable long observationDbId) {
		return new BaseResult<Observation>().setResult(brAPIv2Facade.getObservation(observationDbId));
	}

	@GetMapping(ENDPOINT_OBSERVATIONS)
	public BaseResult<ArrayResult<Observation>> listObservation(ObservationSearchQuery request, BrAPIPage page) throws Exception {
		var p = brAPIv2Facade.listObservations(request, page.toPageRequest());
		return arrayResult(p);
	}

	@PostMapping("/search" + ENDPOINT_OBSERVATIONS)
	public BaseResult<ArrayResult<Observation>> searchObservation(@RequestBody(required = false) ObservationSearchBody request) throws Exception {
		var p = brAPIv2Facade.searchObservations(request, BrAPIQuery.toPageRequest(1000, request.getPage()));
		return arrayResult(p);
	}

	/** Add new Observation entities */
	@PostMapping(ENDPOINT_OBSERVATIONS)
	public BaseResult<ArrayResult<Observation>> createObservations(@RequestBody List<Observation> observations) throws Exception {
		var p = brAPIv2Facade.createObservations(observations);
		return arrayResult(new PageImpl<>(p));
	}

	/** Update multiple Observation entities simultaneously with a single call */
	@PutMapping(ENDPOINT_OBSERVATIONS + "/{observationDbId}")
	public BaseResult<Observation> updateObservation(@PathVariable long observationDbId, @RequestBody Observation observation) throws Exception {
		assert(observationDbId == Long.parseLong(observation.getObservationDbId()));
		var p = brAPIv2Facade.updateObservation(observationDbId, observation);
		return createResult(p, List.of(new Status().setMessage("OK")));
	}

	/** Update multiple Observation entities simultaneously with a single call */
	@PutMapping(ENDPOINT_OBSERVATIONS)
	public BaseResult<ArrayResult<Observation>> updateObservations(@RequestBody Map<Long, Observation> observationsById) throws Exception {
		var p = brAPIv2Facade.updateObservations(observationsById);
		return arrayResult(new PageImpl<>(p));
	}

}