APIBaseActionSupportFacade.java

  1. /*
  2.  * Copyright 2023 Global Crop Diversity Trust
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.gringlobal.api.v2.facade.impl;

  17. import org.gringlobal.api.model.AbstractActionDTO;
  18. import org.gringlobal.api.model.ActionRequestDTO;
  19. import org.gringlobal.api.v2.facade.ActionServiceFacade;
  20. import org.gringlobal.model.AbstractAction;
  21. import org.gringlobal.service.ActionService;
  22. import org.gringlobal.service.filter.ActionFilter;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.data.domain.Page;
  25. import org.springframework.data.domain.Pageable;
  26. import org.springframework.transaction.annotation.Transactional;

  27. import javax.validation.Valid;
  28. import javax.validation.constraints.NotNull;
  29. import java.util.List;
  30. import java.util.Map;
  31. import java.util.Set;

  32. public abstract class APIBaseActionSupportFacade<S extends ActionService<T, F, R, ASF>, DTO extends AbstractActionDTO, T extends AbstractAction<T>, F extends ActionFilter<F, T>, RD extends ActionRequestDTO, R extends ActionService.ActionRequest, ASF extends ActionService.ActionScheduleFilter<ASF, T>>
  33.     extends APIServiceFacadeImpl<S, DTO, T> implements ActionServiceFacade<DTO, T, F, RD, ASF> {

  34.     @Autowired
  35.     protected S service;

  36.     protected abstract @NotNull @Valid R convertRequest(RD source);

  37.     /** Use static import {@code map()} or {@code DTOMapper::map} in implemented methods */
  38.     protected abstract @NotNull @Valid RD convertRequest(R source);

  39.     @Override
  40.     public List<DTO> scheduleAction(RD actionData) {
  41.         return mapper.map(service.scheduleAction(convertRequest(actionData)), this::convert);
  42.     }

  43.     @Override
  44.     public List<DTO> startAction(RD actionData) {
  45.         return mapper.map(service.startAction(convertRequest(actionData)), this::convert);
  46.     }

  47.     @Override
  48.     public List<DTO> completeAction(RD actionData) {
  49.         return mapper.map(service.completeAction(convertRequest(actionData)), this::convert);
  50.     }

  51.     @Override
  52.     @Transactional
  53.     public List<DTO> reopenAction(RD actionData) {
  54.         return mapper.map(service.reopenAction(convertRequest(actionData)), this::convert);
  55.     }

  56.     @Override
  57.     public Page<DTO> listActions(F filter, Pageable page) {
  58.         return mapper.map(service.listActions(filter, page), this::convert);
  59.     }

  60.     @Override
  61.     public long countActions(F filter) {
  62.         return service.countActions(filter);
  63.     }

  64.     @Override
  65.     public Page<DTO> listCompletedActions(ASF filter, Pageable page) {
  66.         return mapper.map(service.listCompletedActions(filter, page), this::convert);
  67.     }

  68.     @Override
  69.     public Page<DTO> listCanceledActions(ASF filter, Pageable page) {
  70.         return mapper.map(service.listCanceledActions(filter, page), this::convert);
  71.     }

  72.     @Override
  73.     public Page<DTO> listInProgressActions(ASF filter, Pageable page) {
  74.         return mapper.map(service.listInProgressActions(filter, page), this::convert);
  75.     }

  76.     @Override
  77.     public Page<DTO> listScheduledActions(ASF filter, Pageable page) {
  78.         return mapper.map(service.listScheduledActions(filter, page), this::convert);
  79.     }

  80.     @Override
  81.     public Page<DTO> listAddedActions(ASF filter, Pageable page) {
  82.         return mapper.map(service.listAddedActions(filter, page), this::convert);
  83.     }

  84.     @Override
  85.     public Page<DTO> listCreatedActions(ASF filter, Pageable page) {
  86.         return mapper.map(service.listCreatedActions(filter, page), this::convert);
  87.     }

  88.     @Override
  89.     public Page<DTO> listOverdueActions(ASF filter, Pageable page) {
  90.         return mapper.map(service.listOverdueActions(filter, page), this::convert);
  91.     }

  92.     @Override
  93.     public ActionService.ActionScheduleOverview actionScheduleOverview(ASF filter) {
  94.         return service.actionScheduleOverview(filter);
  95.     }

  96.     @Override
  97.     public List<DTO> assignActions(@NotNull Map<Long, String> actionAssignee) {
  98.         return mapper.map(service.assignActions(actionAssignee), this::convert);
  99.     }

  100.     @Override
  101.     public List<DTO> startWorkflow(long workflowId, Set<Long> owningEntities) {
  102.         return mapper.map(service.startWorkflow(workflowId, owningEntities), this::convert);
  103.     }
  104. }