CropController.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.gringlobal.api.v1.impl;

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

import org.apache.commons.lang3.ArrayUtils;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.gringlobal.api.v1.ApiBaseController;
import org.gringlobal.api.v1.CRUDController;
import org.gringlobal.api.v1.FilteredCRUDController;
import org.gringlobal.api.v1.FilteredPage;
import org.gringlobal.api.v1.Pagination;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.Crop;
import org.gringlobal.model.CropAttach;
import org.gringlobal.model.QCrop;
import org.gringlobal.model.TaxonomyCropMap;
import org.gringlobal.model.TaxonomySpecies;
import org.gringlobal.service.CropAttachmentService;
import org.gringlobal.service.CropService;
import org.gringlobal.service.filter.CropFilter;
import org.gringlobal.service.filter.TaxonomySpeciesFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
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.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.querydsl.core.types.OrderSpecifier;

import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestController("cropApi1")
@RequestMapping(CropController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "Crop")
public class CropController extends FilteredCRUDController<Crop, CropService, CropFilter> {

	/** The Constant API_URL. */
	public static final String API_URL = ApiBaseController.APIv1_BASE + "/crop";

	@Autowired
	private CropAttachmentService cropAttachmentService;

	@Override
	protected Class<CropFilter> filterType() {
		return CropFilter.class;
	}

	@Override
	protected OrderSpecifier<?>[] defaultSort() {
		return new OrderSpecifier[] { QCrop.crop.id.asc() };
	}

	@RestController("taxonomyCropMapApi1")
	@RequestMapping(CropController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "taxonomyCropMap")
	public static class TaxonomyCropMapController extends ApiBaseController {

		@Autowired
		private CropService cropService;

		/**
		 * Adding taxonomy species to the crop.
		 *
		 * @param cropId the crop ID
		 * @return added records
		 */
		@PostMapping(value = "/{id}/species", produces = { MediaType.APPLICATION_JSON_VALUE })
		@Operation(operationId = "addSpecies", description = "Add taxonomy species to the crop")
		public List<TaxonomyCropMap> addSpecies(@PathVariable("id") final Long cropId, @RequestBody final List<Long> taxonomySpeciesIds) {
			var species = taxonomySpeciesIds.stream().map(TaxonomySpecies::new).collect(Collectors.toList());
			return cropService.addSpecies(cropService.get(cropId), species, null);
		}

		/**
		 * Removing taxonomy species for the crop.
		 *
		 * @param cropId the crop ID
		 * @param taxonomySpeciesIds the list of taxonomy species ids
		 * @return removed records
		 */
		@DeleteMapping(value = "/{id}/species", produces = { MediaType.APPLICATION_JSON_VALUE })
		@Operation(operationId = "removeSpecies", description = "Remove taxonomy species for the crop")
		public List<TaxonomyCropMap> removeSpecies(@PathVariable("id") final Long cropId, @RequestBody final List<Long> taxonomySpeciesIds) {
			var species = taxonomySpeciesIds.stream().map(TaxonomySpecies::new).collect(Collectors.toList());
			return cropService.removeSpecies(cropService.get(cropId), species);
		}
	}

	/**
	 * Retrieve crop details by id
	 *
	 * @param id the id
	 * @return the crop details
	 */
	@GetMapping(value = "/details/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "cropDetails", description = "Retrieve crop details by ID", summary = "Details")
	public CropService.CropDetails details(@PathVariable("id") final long id) {
		return crudService.getCropDetails(crudService.get(id));
	}

	@Override
	@PreAuthorize("hasAuthority('GROUP_ADMINS')")
	@Operation(operationId = "createCrop", description = "Create a Crop", summary = "Add")
	public Crop create(@RequestBody final Crop entity) {
		return super.create(entity);
	}

	@Override
	@PreAuthorize("hasAuthority('GROUP_ADMINS')")
	@Operation(operationId = "updateCrop", description = "Update a Crop", summary = "Update")
	public Crop update(@RequestBody final Crop entity) {
		return super.update(entity);
	}

	@Override
	@PreAuthorize("hasAuthority('GROUP_ADMINS')")
	@Operation(operationId = "deleteCrop", description = "Delete a Crop by ID", summary = "Delete")
	public Crop remove(@PathVariable("id") final long id) {
		return super.remove(id);
	}

	@PreAuthorize("hasAuthority('GROUP_ADMINS')")
	@PostMapping(value = "/attach/{cropId}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "uploadFile", description = "Attach crop file", summary = "Attach file")
	public CropAttach uploadFile(@PathVariable(name = "cropId") final Long cropId, @RequestPart(name = "file") final MultipartFile file,
			@RequestPart(name = "metadata") final CropAttachmentService.CropAttachmentRequest metadata) throws InvalidRepositoryPathException, InvalidRepositoryFileDataException, IOException {

		return cropAttachmentService.uploadFile(crudService.get(cropId), file, metadata);
	}

	@PreAuthorize("hasAuthority('GROUP_ADMINS')")
	@DeleteMapping(value = "/attach/{cropId}/{attachmentId}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "removeFile", description = "Remove attached file", summary = "Remove file")
	public CropAttach removeFile(@PathVariable(name = "cropId") final Long cropId, @PathVariable(name = "attachmentId") final Long attachmentId) {
		return cropAttachmentService.removeFile(crudService.get(cropId), attachmentId);
	}

	@Override
	@Operation(operationId = "listCrops", description = "List Crops", summary = "List")
	public FilteredPage<Crop, CropFilter> list(@ParameterObject final Pagination page, @RequestBody(required = false) final CropFilter filter) throws SearchException, IOException {
		return super.list(page, filter);
	}

	@Override
	@Operation(operationId = "filterCrops", description = "Filter Crops", summary = "Filter")
	public FilteredPage<Crop, CropFilter> filter(@RequestParam(name = "f", required = false) final String filterCode, @ParameterObject final Pagination page,
			@RequestBody(required = false) final CropFilter filter) throws IOException, SearchException {

		return super.filter(filterCode, page, filter);
	}

	@PostMapping(value = "/species/{cropId}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "listCropSpecies", description = "List species of selected crop", summary = "List species")
	public FilteredPage<TaxonomySpecies, TaxonomySpeciesFilter> listSpecies(@PathVariable("cropId") final long id, @ParameterObject final Pagination page, @RequestBody(required = false) final TaxonomySpeciesFilter filter) throws IOException {

		TaxonomySpeciesFilter normalizedFilter = shortFilterService.normalizeFilter(filter, TaxonomySpeciesFilter.class);
		Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, defaultSort()) : page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE);
		FilteredPage<TaxonomySpecies, TaxonomySpeciesFilter> results = new FilteredPage<>(normalizedFilter, crudService.listSpecies(crudService.get(id), filter, pageable));
		return results;
	}

	/**
	 * Auto-complete crops.
	 *
	 * @param term the search term
	 */
	@GetMapping(value = "/autocomplete", produces = MediaType.APPLICATION_JSON_VALUE)
	public List<Crop> autocompleteCrops(@RequestParam("term") final String term) {
		return crudService.autocompleteCrops(term);
	}


	@RestController("cropAttachApi1")
	@RequestMapping(CropAttachController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "Crop")
	public static class CropAttachController extends CRUDController<CropAttach, CropAttachmentService> {
		/** The Constant API_URL. */
		public static final String API_URL = CropController.API_URL + "/attach/meta";

		@Override
		@Operation(operationId = "createCropAttach", description = "Create CropAttach", summary = "Create")
		public CropAttach create(@RequestBody CropAttach entity) {
			// Throws UnsupportedOperationException
			return super.create(entity);
		}

		@Override
		@Operation(operationId = "updateCropAttach", description = "Update an existing record", summary = "Update")
		public CropAttach update(@RequestBody CropAttach entity) {
			return super.update(entity);
		}

		@Override
		@Operation(operationId = "getCropAttach", description = "Get record by ID", summary = "Get")
		public CropAttach get(@PathVariable long id) {
			return super.get(id);
		}

		@Override
		@Operation(operationId = "deleteCropAttach", description = "Delete existing record by ID", summary = "Delete")
		public CropAttach remove(@PathVariable long id) {
			return super.remove(id);
		}
	}

}