SystemActionServiceImpl.java

/*
 * Copyright 2026 Global Crop Diversity Trust
 * Licensed under the Apache License, Version 2.0
 * See LICENSE file in project root folder or http://www.apache.org/licenses/LICENSE-2.0
 */

package org.gringlobal.service.impl;

import java.util.List;
import java.util.Set;

import org.gringlobal.model.SystemAction;
import org.gringlobal.model.workflow.WorkflowActionStep;
import org.gringlobal.persistence.SystemActionRepository;
import org.gringlobal.service.SystemActionService;
import org.gringlobal.service.filter.SystemActionFilter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.querydsl.core.types.EntityPath;
import com.querydsl.core.types.Predicate;

@Slf4j
@Component
public class SystemActionServiceImpl extends BaseActionSupport<SystemAction, SystemAction, SystemActionFilter, SystemActionRepository, SystemActionService.SystemActionRequest, SystemActionService.SystemActionScheduleFilter>
	implements SystemActionService {

	@Override
	protected EntityPath<SystemAction> getOwningEntityPath() {
		return null;
	}

	@Override
	protected void initializeActionDetails(List<SystemAction> actions) {
	}

	@Override
	protected void applyOwningEntityFilter(SystemActionScheduleFilter filter, String owningEntityAlias, List<Predicate> predicates) {
		throw new UnsupportedOperationException("Not supported.");
	}

	@Override
	protected SystemAction createAction(SystemAction owningEntity) {
		return new SystemAction();
	}

	@Override
	protected void updateAction(SystemAction action, SystemActionRequest request) {
	}

	@Override
	protected SystemAction prepareNextWorkflowStepAction(WorkflowActionStep nextStep, SystemAction completedAction) {
		throw new UnsupportedOperationException("Not supported.");
	}

	@Override
	@Transactional
	@PreAuthorize("hasRole('ADMINISTRATOR') or @ggceSec.actionAllowed('SystemAction', 'CREATE')")
	public SystemAction createFast(SystemAction source) {
		log.debug("Create SystemAction. Input data {}", source);
		SystemAction systemAction = new SystemAction();
		systemAction.apply(source);
		return repository.save(systemAction);
	}

	@Override
	@Transactional
	@PreAuthorize("hasRole('ADMINISTRATOR') or @ggceSec.actionAllowed('SystemAction', 'CREATE')")
	public SystemAction create(SystemAction source) {
		return _lazyLoad(createFast(source));
	}

	@Override
	@Transactional
	@PostAuthorize("hasRole('ADMINISTRATOR') or @ggceSec.actionAllowed('SystemAction', 'WRITE') or (@ggceSec.actionAllowed('SystemAction', 'CREATE') and returnObject.ownedBy.id == principal.id)")
	public SystemAction update(SystemAction updated) {
		return super.update(updated);
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('SystemAction', 'DELETE')")
	public SystemAction remove(SystemAction entity) {
		return super.remove(entity);
	}

	@Override
	@Transactional
	@PreAuthorize("hasRole('ADMINISTRATOR') or @ggceSec.actionAllowed('SystemAction', 'WRITE') or (@ggceSec.actionAllowed('SystemAction', 'CREATE') and #target.ownedBy.id == principal.id)")
	@PostAuthorize("hasRole('ADMINISTRATOR') or @ggceSec.actionAllowed('SystemAction', 'WRITE') or (@ggceSec.actionAllowed('SystemAction', 'CREATE') and returnObject.ownedBy.id == principal.id)")
	public SystemAction updateFast(SystemAction updated, SystemAction target) {
		return super.update(updated, target);
	}

	@Override
	@Transactional
	@PreAuthorize("hasRole('ADMINISTRATOR') or @ggceSec.actionAllowed('SystemAction', 'WRITE') or (@ggceSec.actionAllowed('SystemAction', 'CREATE') and #target.ownedBy.id == principal.id)")
	@PostAuthorize("hasRole('ADMINISTRATOR') or @ggceSec.actionAllowed('SystemAction', 'WRITE') or (@ggceSec.actionAllowed('SystemAction', 'CREATE') and returnObject.ownedBy.id == principal.id)")
	public SystemAction update(SystemAction updated, SystemAction target) {
		return _lazyLoad(updateFast(updated, target));
	}

	@Override
	protected Iterable<SystemAction> findOwningEntities(Set<Long> id) {
		return null;
	}

	@Override
	public List<SystemAction> startWorkflow(long workflowId, Set<Long> owningEntityIds) {
		throw new UnsupportedOperationException("Not supported.");
	}

	@Override
	@Transactional(rollbackFor = AccessDeniedException.class)
	@PreAuthorize("hasRole('ADMINISTRATOR') or @ggceSec.actionAllowed('SystemAction', 'CREATE')")
	public List<SystemAction> startAction(SystemActionRequest actionData) {
		return super.startAction(actionData);
	}

	@Override
	@Transactional(rollbackFor = AccessDeniedException.class)
	@PostAuthorize("hasRole('ADMINISTRATOR') or @ggceSec.actionAllowed('SystemAction', 'WRITE') or (@ggceSec.actionAllowed('SystemAction', 'CREATE') and returnObject[0].ownedBy.id == principal.id)")
	public List<SystemAction> completeAction(SystemActionRequest actionData) {
		return super.completeAction(actionData);
	}

}