ActionController.java
/*
* Copyright 2023 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.v2;
import com.querydsl.core.types.OrderSpecifier;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.commons.lang3.ArrayUtils;
import org.gringlobal.api.model.AbstractActionDTO;
import org.gringlobal.api.model.ActionRequestDTO;
import org.gringlobal.api.v1.ApiBaseController;
import org.gringlobal.api.v1.FilteredPage;
import org.gringlobal.api.v1.MultiOp;
import org.gringlobal.api.v1.Pagination;
import org.gringlobal.api.v2.facade.ActionServiceFacade;
import org.gringlobal.model.AbstractAction;
import org.gringlobal.service.ActionService;
import org.gringlobal.service.ShortFilterService;
import org.gringlobal.service.filter.ActionFilter;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
@Validated
public abstract class ActionController<DTO extends AbstractActionDTO, T extends AbstractAction<T>, F extends ActionFilter<F, T>, R extends ActionRequestDTO, ASF extends ActionService.ActionScheduleFilter<ASF, T>, SF extends ActionServiceFacade<DTO, T, F, R, ASF>>
extends ApiBaseController {
protected static final int MAX_PAGE_SIZE = 100;
protected static final int FILTER_GENERIC_INDEX = 2;
protected static final int SCHEDULE_FILTER_GENERIC_INDEX = 4;
@Autowired
protected SF actionServiceFacade;
@Autowired
private ShortFilterService shortFilterService;
@SuppressWarnings("unchecked")
private Class<F> filterType = (Class<F>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[FILTER_GENERIC_INDEX];
@SuppressWarnings("unchecked")
private Class<ASF> actionScheduleFilterType = (Class<ASF>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[SCHEDULE_FILTER_GENERIC_INDEX];
public ActionController() {
super();
}
/**
* Default order specifier for this entity.
*
* @return the order specifier
*/
protected OrderSpecifier<?>[] defaultSort() {
return null;
}
/**
* Filter type
*
* @return the class
*/
protected final Class<F> filterType() {
return this.filterType;
}
@PostMapping(value = "/action/schedule", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "startActions", description = "Start entity actions", summary = "Start actions")
public List<DTO> scheduleActions(@RequestBody @Valid final R request) {
return actionServiceFacade.scheduleAction(request);
}
@PostMapping(value = "/action/start", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "startActions", description = "Start entity actions", summary = "Start actions", deprecated = true)
public List<DTO> startActions(@RequestBody @Valid final R request) {
return actionServiceFacade.startAction(request);
}
@PostMapping(value = "/action/complete", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "completeActions", description = "Complete entity actions", summary = "Complete actions", deprecated = true)
public List<DTO> completeActions(@RequestBody @Valid final R request) {
return actionServiceFacade.completeAction(request);
}
@PostMapping(value = "/action/reopen", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "reopenActions", description = "Reopen entity actions", summary = "Reopen actions", deprecated = true)
public List<DTO> reopenActions(@RequestBody @Valid final R request) {
return actionServiceFacade.reopenAction(request);
}
@PostMapping(value = "/action/list", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "listActions", description = "List actions matching the filter", summary = "List actions by filter")
public FilteredPage<DTO, F> listActions(@ParameterObject final Pagination page, @RequestBody(required = false) final F filter) throws IOException {
var normalizedFilter = shortFilterService.normalizeFilter(filter, filterType());
Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, defaultSort()) : page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE);
return new FilteredPage<>(normalizedFilter, actionServiceFacade.listActions(filter, pageable));
}
@PostMapping(value = "/action/list/completed", produces = { MediaType.APPLICATION_JSON_VALUE })
public FilteredPage<DTO, ASF> listCompletedActions(@ParameterObject final Pagination page, @RequestBody final ASF filter) throws IOException {
var normalizedFilter = (ASF) shortFilterService.normalizeFilter(filter, actionScheduleFilterType);
Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, defaultSort()) : page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE);
return new FilteredPage<>(normalizedFilter, actionServiceFacade.listCompletedActions(filter, pageable));
}
@PostMapping(value = "/action/list/canceled", produces = { MediaType.APPLICATION_JSON_VALUE })
public FilteredPage<DTO, ASF> listCanceledActions(@ParameterObject final Pagination page, @RequestBody final ASF filter) throws IOException {
var normalizedFilter = (ASF) shortFilterService.normalizeFilter(filter, actionScheduleFilterType);
Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, defaultSort()) : page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE);
return new FilteredPage<>(normalizedFilter, actionServiceFacade.listCanceledActions(filter, pageable));
}
@PostMapping(value = "/action/list/inProgress", produces = { MediaType.APPLICATION_JSON_VALUE })
public FilteredPage<DTO, ASF> listInProgressActions(@ParameterObject final Pagination page, @RequestBody final ASF filter) throws IOException {
var normalizedFilter = (ASF) shortFilterService.normalizeFilter(filter, actionScheduleFilterType);
Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, defaultSort()) : page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE);
return new FilteredPage<>(normalizedFilter, actionServiceFacade.listInProgressActions(filter, pageable));
}
@PostMapping(value = "/action/list/scheduled", produces = { MediaType.APPLICATION_JSON_VALUE })
public FilteredPage<DTO, ASF> listScheduledActions(@ParameterObject final Pagination page, @RequestBody final ASF filter) throws IOException {
var normalizedFilter = (ASF) shortFilterService.normalizeFilter(filter, actionScheduleFilterType);
Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, defaultSort()) : page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE);
return new FilteredPage<>(normalizedFilter, actionServiceFacade.listScheduledActions(filter, pageable));
}
@PostMapping(value = "/action/list/added", produces = { MediaType.APPLICATION_JSON_VALUE })
public FilteredPage<DTO, ASF> listAddedActions(@ParameterObject final Pagination page, @RequestBody final ASF filter) throws IOException {
var normalizedFilter = (ASF) shortFilterService.normalizeFilter(filter, actionScheduleFilterType);
Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, defaultSort()) : page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE);
return new FilteredPage<>(normalizedFilter, actionServiceFacade.listAddedActions(filter, pageable));
}
@PostMapping(value = "/action/list/created", produces = { MediaType.APPLICATION_JSON_VALUE })
public FilteredPage<DTO, ASF> listCreatedActions(@ParameterObject final Pagination page, @RequestBody final ASF filter) throws IOException {
var normalizedFilter = (ASF) shortFilterService.normalizeFilter(filter, actionScheduleFilterType);
Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, defaultSort()) : page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE);
return new FilteredPage<>(normalizedFilter, actionServiceFacade.listCreatedActions(filter, pageable));
}
@PostMapping(value = "/action/list/overdue", produces = { MediaType.APPLICATION_JSON_VALUE })
public FilteredPage<DTO, ASF> listOverdueActions(@ParameterObject final Pagination page, @RequestBody final ASF filter) throws IOException {
var normalizedFilter = (ASF) shortFilterService.normalizeFilter(filter, actionScheduleFilterType);
Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, defaultSort()) : page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE);
return new FilteredPage<>(normalizedFilter, actionServiceFacade.listOverdueActions(filter, pageable));
}
@PostMapping("/schedule")
public ActionService.ActionScheduleOverview actionSchedule(@RequestBody final ASF filter) {
return actionServiceFacade.actionScheduleOverview(filter);
}
/**
* Get entity by id
*
* @param id the id
* @return the loaded record
*/
@GetMapping(value = "/action/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "get", description = "Get record by ID", summary = "Get")
public DTO get(@PathVariable("id") final long id) {
return actionServiceFacade.get(id);
}
/**
* Remove the entity.
*
* @param id the id
* @return the removed record
*/
@DeleteMapping(value = "/action/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "remove", description = "Delete existing record by ID", summary = "Delete")
public DTO remove(@PathVariable("id") final long id) {
return actionServiceFacade.remove(actionServiceFacade.get(id));
}
/**
* Register a new entity.
*
* @param entity the site
* @return the recorded record
*/
@PostMapping(value = "/action", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "create", description = "Create a record", summary = "Add")
public DTO create(@RequestBody @Valid @NotNull final DTO entity) {
return actionServiceFacade.get(actionServiceFacade.create(entity).getId());
}
/**
* Register several new actions.
*
* @param inserts list of entities to add
* @return the recorded records
*/
@PostMapping(value = "/action/many", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "createMany", description = "Create multiple new actions", summary = "Add many")
public MultiOp<DTO> createMany(@RequestBody @Valid @NotNull final List<DTO> inserts) {
return actionServiceFacade.create(inserts).map(actionServiceFacade::get);
}
/**
* Update several actions.
*
* @param updates list of entities to updates
* @return the recorded records
*/
@PutMapping(value = "/action/many", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "updateMany", description = "Update multiple actions at once", summary = "Update many")
public MultiOp<DTO> updateMany(@RequestBody @Valid @NotNull final List<DTO> updates) {
return actionServiceFacade.update(updates).map(actionServiceFacade::get);
}
/**
* Update the entity.
*
* @param entity entity with updates
* @return the updated record
*/
@PutMapping(value = "/action", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "update", description = "Update an existing record", summary = "Update")
public DTO update(@RequestBody @Valid @NotNull final DTO entity) {
return actionServiceFacade.get(actionServiceFacade.update(entity).getId());
}
/**
* Assign actions to sids.
*
* @param actionAssignee map of action ids and sid ids
* @return the assigned actions
*/
@PostMapping(value = "/action/assign", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "assignActions", description = "Assign actions to sids", summary = "Assign actions")
public List<DTO> assignActions(@RequestBody @Valid @NotNull final LinkedHashMap<Long, String> actionAssignee) {
return actionServiceFacade.assignActions(actionAssignee);
}
/**
* Start workflow on selected entities.
*
* @param actionAssignee map of action ids and sid ids
* @return the assigned actions
*/
@PostMapping(value = "/workflow/{workflowId}/start", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "startWorkflow", description = "Start workflow on the objects specified by their IDs", summary = "Start workflow")
public List<DTO> startWorkflow(
@PathVariable long workflowId,
@RequestBody @Valid @NotNull final LinkedHashSet<Long> owningEntities
) {
return actionServiceFacade.startWorkflow(workflowId, owningEntities);
}
}