InventoryViabilityRuleController.java
/*
* Copyright 2021 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.util.List;
import java.util.Set;
import java.util.stream.Collectors;
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;
import org.apache.commons.lang3.ArrayUtils;
import org.gringlobal.api.v1.ApiBaseController;
import org.gringlobal.api.v1.FilteredCRUDController;
import org.gringlobal.api.v1.Pagination;
import org.gringlobal.model.InventoryViabilityRule;
import org.gringlobal.model.InventoryViabilityRuleMap;
import org.gringlobal.model.QInventoryViabilityRuleMap;
import org.gringlobal.model.TaxonomySpecies;
import org.gringlobal.service.InventoryViabilityRuleService;
import org.gringlobal.service.filter.InventoryViabilityRuleFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
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.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;
@RestController("inventoryViabilityRuleApi1")
@RequestMapping(InventoryViabilityRuleController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = InventoryViabilityRuleController.API_TAG)
public class InventoryViabilityRuleController extends FilteredCRUDController<InventoryViabilityRule, InventoryViabilityRuleService, InventoryViabilityRuleFilter> {
/**
* The Constant API_URL.
*/
public static final String API_URL = InventoryViabilityController.API_URL + "/rule";
public static final String API_TAG = "InventoryViabilityRule";
@RestController("viabilityRuleSpeciesApi1")
@RequestMapping(InventoryViabilityRuleController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "inventoryViabilityRuleMap")
public static class ViabilityRuleSpeciesController extends ApiBaseController {
@Autowired
private InventoryViabilityRuleService iViabilityRuleService;
private OrderSpecifier<?>[] defaultSpeciesSort() {
return new OrderSpecifier[] {
QInventoryViabilityRuleMap.inventoryViabilityRuleMap.taxonomySpecies().taxonomyGenus().genusName.asc(),
QInventoryViabilityRuleMap.inventoryViabilityRuleMap.taxonomySpecies().speciesName.asc()
};
}
@PostMapping(value = "/{id}/species", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "addSpecies", description = "Add taxonomy species to the viability rule")
public List<InventoryViabilityRuleMap> addSpecies(@PathVariable("id") final Long ruleId, @RequestBody final Set<Long> taxonomySpeciesIds) {
List<TaxonomySpecies> species = taxonomySpeciesIds.stream().map(TaxonomySpecies::new).collect(Collectors.toList());
return iViabilityRuleService.addSpecies(iViabilityRuleService.get(ruleId), species, null);
}
@PostMapping(value = "/{id}/species/list", produces = { MediaType.APPLICATION_JSON_VALUE })
public Page<InventoryViabilityRuleMap> listSpecies(@PathVariable("id") final Long ruleId, @ParameterObject final Pagination page) {
Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, defaultSpeciesSort()) : page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE);
return iViabilityRuleService.listSpecies(iViabilityRuleService.get(ruleId), pageable);
}
@PostMapping(value = "/species", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "addInventoryViabilityRuleMap", description = "Create a record", summary = "Add")
public InventoryViabilityRuleMap addInventoryViabilityRuleMap(@RequestBody final InventoryViabilityRuleMap source) {
return iViabilityRuleService.addInventoryViabilityRuleMap(source);
}
@DeleteMapping(value = "/species/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "removeInventoryViabilityRuleMap", description = "Delete existing record by ID", summary = "Delete")
public InventoryViabilityRuleMap removeInventoryViabilityRuleMap(@PathVariable("id") final long id) {
return iViabilityRuleService.removeInventoryViabilityRuleMap(id);
}
}
}