WorkflowApiServiceImpl.java

/*
 * Copyright 2024 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.WorkflowDTO;
import org.gringlobal.api.model.WorkflowDetailsDTO;
import org.gringlobal.api.model.WorkflowStepDTO;
import org.gringlobal.api.v2.facade.WorkflowApiService;
import org.gringlobal.model.workflow.Workflow;
import org.gringlobal.model.workflow.WorkflowStep;
import org.gringlobal.model.workflow.WorkflowTransition;
import org.gringlobal.persistence.WorkflowStepRepository;
import org.gringlobal.service.WorkflowService;
import org.gringlobal.service.WorkflowService.WorkflowStepService;
import org.gringlobal.service.filter.WorkflowFilter;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.stream.Collectors;

@Service
public class WorkflowApiServiceImpl extends APIFilteredServiceFacadeImpl<WorkflowService, WorkflowDTO, Workflow, WorkflowFilter> implements WorkflowApiService {

	@Autowired
	private WorkflowStepRepository workflowStepRepository;

	@Override
	protected Workflow convert(WorkflowDTO source) {
		var wf = mapper.map(source);
		if (source.getTransitions() != null) {
			wf.setTransitions(mapper.map(source.getTransitions(), (t) -> new WorkflowTransition(t.getId(), wf, workflowStepRepository.getReferenceById(t.getOriginStepId()), workflowStepRepository.getReferenceById(t.getTargetStepId()), t.getCondition())));
		}
		return wf;
	}

	@Override
	protected WorkflowDTO convert(Workflow source) {
		return mapper.map(source);
	}

	@Service
	public static class WorkflowStepApiServiceImpl extends APIServiceFacadeImpl<WorkflowStepService, WorkflowStepDTO, WorkflowStep> implements WorkflowStepApiService {

		@Override
		protected WorkflowStep convert(WorkflowStepDTO source) {
			return mapper.map(source);
		}

		@Override
		protected WorkflowStepDTO convert(WorkflowStep source) {
			return mapper.map(source);
		}
	}

	@Override
	@Transactional(readOnly = true)
	public WorkflowDetailsDTO getDetails(long workflowId) {
		var workflow = service.get(workflowId);
		WorkflowDetailsDTO details = new WorkflowDetailsDTO();
		details.setWorkflow(mapper.map(workflow));
		details.setSteps(mapper.map(workflow.getSteps().stream().map(step -> (WorkflowStep)Hibernate.unproxy(step)).collect(Collectors.toList()), mapper::map));
		return details;
	}

	@Override
	@Transactional
	public Workflow updateTransitions(WorkflowDTO workflow) {
		return service.updateTransitions(service.get(workflow.getId(), workflow.getModifiedDate()), convert(workflow).getTransitions());
	}
}