APIBaseActionSupportFacade.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.facade.impl;

import org.gringlobal.api.model.AbstractActionDTO;
import org.gringlobal.api.model.ActionRequestDTO;
import org.gringlobal.api.v2.facade.ActionServiceFacade;
import org.gringlobal.model.AbstractAction;
import org.gringlobal.service.ActionService;
import org.gringlobal.service.filter.ActionFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
import java.util.Set;

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>>
	extends APIServiceFacadeImpl<S, DTO, T> implements ActionServiceFacade<DTO, T, F, RD, ASF> {

	@Autowired
	protected S service;

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

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

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

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

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

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

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

	@Override
	public long countActions(F filter) {
		return service.countActions(filter);
	}

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

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

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

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

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

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

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

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

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

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