TaxonomyController.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.genesys.server.api.v1;

import java.io.IOException;
import java.util.List;

import org.genesys.server.api.ApiBaseController;
import org.genesys.server.api.FilteredPage;
import org.genesys.server.api.Pagination;
import org.genesys.server.exception.NotFoundElement;
import org.genesys.server.model.genesys.Taxonomy2;
import org.genesys.server.model.grin.TaxonomySpecies;
import org.genesys.server.service.TaxonomyService;
import org.genesys.server.service.ShortFilterService.FilterInfo;
import org.genesys.server.service.filter.TaxonomyExtraFilter;
import org.genesys.server.service.filter.TaxonomyFilter;
import org.genesys.server.service.filter.TaxonomySpeciesFilter;
import org.genesys.server.exception.SearchException;
import org.genesys.server.service.worker.ShortFilterProcessor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
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;

import io.swagger.annotations.Api;

/**
 * @author Maxym Borodenko
 */
@RestController("taxonomyApi1")
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = TaxonomyController.CONTROLLER_URL)
@Api(tags = {"taxonomy"})
public class TaxonomyController extends ApiBaseController {

	/** The Constant CONTROLLER_URL. */
	public static final String CONTROLLER_URL = ApiBaseController.APIv1_BASE + "/taxonomy";

	/** The taxonomy service **/
	@Autowired
	private TaxonomyService taxonomyService;

	@Autowired
	protected ShortFilterProcessor shortFilterProcessor;

	/**
	 * Gets the Taxonomy2.
	 *
	 * @param id the id
	 * @return the taxonomy2
	 * @throws NotFoundElement the not found element
	 */
	@GetMapping(value = "/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
	public Taxonomy2 get(@PathVariable("id") final Long id) throws NotFoundElement {
		LOG.debug("Load taxonomy2 by id {}", id);
		Taxonomy2 taxonomy2 = taxonomyService.get(id);
		if (taxonomy2 == null) {
			throw new NotFoundElement("No such taxonomy2.");
		}
		return taxonomy2;
	}

	@PostMapping(value = "/", produces = { MediaType.APPLICATION_JSON_VALUE })
	public List<Taxonomy2> listTaxonomies(@RequestBody final TaxonomyFilter taxonomyFilter) {
		LOG.info("Listing taxonomies");
		return taxonomyService.list(taxonomyFilter);
	}

	/**
	 * List of Taxonomy2 entities with included count of accessions.
	 *
	 * @param page the page
	 * @param filter the taxonomy2 filter
	 * @return the page
	 */
	@PostMapping(value = "/list", produces = { MediaType.APPLICATION_JSON_VALUE })
	public FilteredPage<TaxonomyService.Taxonomy2Info, TaxonomyExtraFilter> listTaxonomies(@RequestParam(name = "f", required = false) String filterCode, @ParameterObject final Pagination page, @RequestBody final TaxonomyExtraFilter filter) throws IOException {
		
		FilterInfo<TaxonomyExtraFilter> filterInfo = shortFilterProcessor.processFilter(filterCode, filter, TaxonomyExtraFilter.class);
		
		return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, taxonomyService.list(filterInfo.filter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, Sort.Direction.ASC, "genus", "species", "id")));
	}

	/**
	 * List taxonomies species.
	 *
	 * @param page the page
	 * @param filter the filter
	 * @return the page
	 * @throws SearchException 
	 */
	@PostMapping(value = "/grin-species", produces = { MediaType.APPLICATION_JSON_VALUE })
	public FilteredPage<TaxonomySpecies, TaxonomySpeciesFilter> listSpecies(@ParameterObject final Pagination page, @RequestBody final TaxonomySpeciesFilter filter) throws SearchException {
		return new FilteredPage<>(filter, taxonomyService.listSpecies(filter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, Sort.Direction.ASC, "genusSpecies", "subtaxa")));
	}

	/**
	 * Sets GRIN species to taxonomy2.
	 *
	 * @param taxonomy2Id the ID of Taxonomy2
	 * @param customGrinSpeciesId the ID of GRIN TaxonomySpecies
	 * @return updated record of Taxonomy2
	 */
	@PostMapping(value = "/set-grin-species/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
	public Taxonomy2 setGrinSpecies(@PathVariable("id") final long taxonomy2Id, @RequestParam(required = false, value = "customGrinSpeciesId") final Long customGrinSpeciesId) {
		return taxonomyService.setGrinSpecies(taxonomy2Id, customGrinSpeciesId);
	}
}