ActionService.java

/*
 * Copyright 2020 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.service;

import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.experimental.SuperBuilder;

import org.genesys.blocks.model.filters.SuperModelFilter;
import org.genesys.blocks.security.model.AclSid;
import org.gringlobal.model.AbstractAction;
import org.gringlobal.model.Cooperator;
import org.gringlobal.model.workflow.WorkflowActionStep;
import org.gringlobal.service.ActionService.ActionRequest;
import org.gringlobal.service.ActionService.ActionScheduleFilter;
import org.gringlobal.service.filter.AclSidFilter;
import org.gringlobal.service.filter.ActionFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;

import com.querydsl.core.types.Predicate;

/**
 * @author Maxym Borodenko
 */
@Transactional(readOnly = true)
public interface ActionService<T extends AbstractAction<T>, F extends ActionFilter<F, T>, R extends ActionRequest, ASF extends ActionScheduleFilter<ASF, T>>
	extends CRUDService2<T> {

	@Transactional
	List<T> scheduleAction(@Valid R request);

	@Transactional
	List<T> startAction(@Valid R request);

	@Transactional
	List<T> completeAction(@Valid R request);

	@Transactional
	List<T> cancelAction(@Valid R request);

	@Transactional
	List<T> reopenAction(@Valid R request);

	@Transactional(readOnly = true)
	Page<T> listActions(F filter, Pageable page);

	long countActions(F filter);

	@NoArgsConstructor
	@SuperBuilder
	abstract class ActionRequest {
		public Set<Long> id;
		public String actionNameCode;
		public String note;
		public Cooperator cooperator;
		public AclSid assignee;
		public Instant notBeforeDate;
		public String notBeforeDateCode;
	}

	@Setter
	@Getter
	@Accessors(fluent = true)
	abstract class ActionScheduleFilter<F extends ActionScheduleFilter<F, T>, T extends AbstractAction<T>> extends SuperModelFilter<F, T> {
		private static final long serialVersionUID = 6905980861555793439L;
		@NotNull
		public Instant fromInclusive; // x >= DATE1
		@NotNull
		public Instant toExclusive; // x < DATE2
		public Set<String> actionCode;
		/** The action assignee. */
		public AclSidFilter assignee;


		@Override
		public List<Predicate> collectPredicates() {
			return null;
		}
	}

	class ActionScheduleOverview {
		/**
		 * Actions scheduled for the selected period
		 */
		public Map<String, Number> scheduled;
		/**
		 * Action without due date added during the selected period
		 */
		public Map<String, Number> added;
		/**
		 * Actions registered (created) in the selected period
		 */
		public Map<String, Number> created;
		/**
		 * Actions completed in the period
		 */
		public Map<String, Number> completed;
		/**
		 * Actions canceled in the period
		 */
		public Map<String, Number> canceled;
		/**
		 * Actions in progress during the selected period
		 */
		public Map<String, Number> inProgress;
		/**
		 * Actions due in the selected period, but not yet started
		 */
		public Map<String, Number> overdue;
	}

	@Transactional(readOnly = true)
	Page<T> listCompletedActions(@Valid ASF filter, Pageable page);

	@Transactional(readOnly = true)
	Page<T> listCanceledActions(ASF filter, Pageable page);

	@Transactional(readOnly = true)
	Page<T> listInProgressActions(@Valid ASF filter, Pageable page);

	@Transactional(readOnly = true)
	List<T> listInProgressActions(@Valid R request);

	@Transactional(readOnly = true)
	Page<T> listScheduledActions(@Valid ASF filter, Pageable page);

	@Transactional(readOnly = true)
	Page<T> listAddedActions(@Valid ASF filter, Pageable page);

	@Transactional(readOnly = true)
	Page<T> listCreatedActions(@Valid ASF filter, Pageable page);

	@Transactional(readOnly = true)
	Page<T> listOverdueActions(@Valid ASF filter, Pageable page);

	@Transactional(readOnly = true)
	ActionScheduleOverview actionScheduleOverview(@Valid ASF filter);

	@Transactional
	List<T> assignActions(@NotNull Map<Long, String> actionAssignee);

	@Transactional
	List<T> startWorkflow(long workflowId, Set<Long> owningEntityIds);

	@Transactional
	T createNextWorkflowStepAction(WorkflowActionStep nextStep, T completedAction);

	Class<T> getActionType();

}