InventoryGroupController.java

/*
 * Copyright 2019 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.stream.Collectors;

import org.apache.commons.lang3.ArrayUtils;
import org.gringlobal.api.v1.FilteredCRUDController;
import org.gringlobal.api.v1.Pagination;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.Accession;
import org.gringlobal.model.AccessionInvGroup;
import org.gringlobal.model.AccessionInvGroupMap;
import org.gringlobal.model.Inventory;
import org.gringlobal.model.QAccessionInvGroup;
import org.gringlobal.model.QAccessionInvGroupMap;
import org.gringlobal.service.AccessionInvGroupService;
import org.gringlobal.service.filter.AccessionInvGroupFilter;
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.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 com.querydsl.core.types.OrderSpecifier;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestController("inventoryGroupApi1")
@RequestMapping(InventoryGroupController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "Inventory")
public class InventoryGroupController extends FilteredCRUDController<AccessionInvGroup, AccessionInvGroupService, AccessionInvGroupFilter> {

	/** The Constant API_URL. */
	public static final String API_URL = InventoryController.API_URL + "/group";

	@Override
	protected OrderSpecifier<?>[] defaultSort() {
		return new OrderSpecifier[] { QAccessionInvGroup.accessionInvGroup.groupName.asc() };
	}

	protected OrderSpecifier<?>[] defaultMemberSort() {
		return new OrderSpecifier[] { QAccessionInvGroupMap.accessionInvGroupMap.inventory().inventoryNumberPart1.asc(), QAccessionInvGroupMap.accessionInvGroupMap.inventory().inventoryNumberPart2.asc(), QAccessionInvGroupMap.accessionInvGroupMap.inventory().inventoryNumberPart3.asc() };
	}

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

	@PostMapping(value = "/{id}/members", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "listMembers", description = "Fetch members of the group", summary = "List or filter order items")
	public Page<AccessionInvGroupMap> filterItems(@PathVariable("id") final Long groupId, @Parameter(hidden = true) Pagination page) throws SearchException {
		final AccessionInvGroup accessionInvGroup = crudService.get(groupId);
		Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, defaultMemberSort()) : page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE);
		return crudService.listMembers(accessionInvGroup, pageable);
	}

	@PostMapping(value = "/{id}/add-inventory", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "addInventories", description = "Add inventories to the group")
	public AccessionInvGroup addInventories(@PathVariable("id") final Long groupId, @RequestBody final List<Long> inventoryIds) {
		List<Inventory> inventories = inventoryIds.stream().map(Inventory::new).collect(Collectors.toList());

		return crudService.addMembers(crudService.get(groupId), inventories, null);
	}

	@PostMapping(value = "/{id}/add-accession", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "addAccessions", description = "Add accession's system inventory to the group")
	public AccessionInvGroup addAccessions(@PathVariable("id") final Long groupId, @RequestBody final List<Long> accessionIds) throws SearchException {
		List<Accession> accessions = accessionIds.stream().map(Accession::new).collect(Collectors.toList());

		return crudService.addMembers(crudService.get(groupId), accessions);
	}

	@PostMapping(value = "/{id}/remove", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "removeMembers", description = "Remove memberships of the group")
	public AccessionInvGroup removeMembers(@PathVariable("id") final Long groupId, @RequestBody final List<AccessionInvGroupMap> accessionInvGroupMap) {
		return crudService.removeMembers(crudService.get(groupId), accessionInvGroupMap);
	}

}