SystemActionApiServiceImpl.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.api.v2.facade.impl;

import java.time.Instant;

import org.gringlobal.api.model.SystemActionDTO;
import org.gringlobal.api.model.SystemActionRequestDTO;
import org.gringlobal.api.v2.facade.SystemActionApiService;
import org.gringlobal.component.GGCETransactionHelper;
import org.gringlobal.model.SysUser;
import org.gringlobal.model.SystemAction;
import org.gringlobal.model.community.CommunityCodeValues;
import org.gringlobal.service.SystemActionService;
import org.gringlobal.service.filter.SystemActionFilter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SystemActionApiServiceImpl extends APIBaseActionSupportFacade<SystemActionService, SystemActionDTO, SystemAction, SystemActionFilter, SystemActionRequestDTO, SystemActionService.SystemActionRequest, SystemActionService.SystemActionScheduleFilter>
	implements SystemActionApiService {

	@Autowired
	private GGCETransactionHelper transactionHelper;

	@Override
	protected SystemAction convert(SystemActionDTO source) {
		return mapper.map(source);
	}

	@Override
	protected SystemActionDTO convert(SystemAction source) {
		return mapper.map(source);
	}

	@Override
	protected SystemActionService.SystemActionRequest convertRequest(SystemActionRequestDTO source) {
		return mapper.map(source);
	}

	@Override
	protected SystemActionRequestDTO convertRequest(SystemActionService.SystemActionRequest source) {
		return mapper.map(source);
	}

	@Override
	public SystemAction createSystemAction(Authentication auth, String actionCode) {
		if (auth == null) auth = SecurityContextHolder.getContext().getAuthentication();
		try {
			return transactionHelper.asUser(auth, () -> {
				var action = new SystemAction();
				action.setActionNameCode(actionCode);
				action.setStartedDate(Instant.now());
				action.setStartedDateCode(CommunityCodeValues.DATE_FORMAT_DATETIME.value);
				return service.create(action);
			});
		} catch (Throwable e) {
			log.error("Error while creating system action: {}", e.getMessage());
			return null;
		}
	}

	@Override
	public SystemAction completeSystemAction(Authentication auth, SystemAction action, boolean isDone, String note) {
		if (auth == null) auth = SecurityContextHolder.getContext().getAuthentication();
		try {
			return transactionHelper.asUser(auth, () -> {
				var actionToComplete = service.get(action.getId());
				actionToComplete.setNote(note);
				actionToComplete.setCompletedDate(Instant.now());
				actionToComplete.setCompletedDateCode(CommunityCodeValues.DATE_FORMAT_DATETIME.value);
				actionToComplete.setIsDone(isDone ? "Y" : "N");
				return service.update(actionToComplete);
			});
		} catch (Throwable e) {
			log.error("Error while completing system action: {} ownedBy: {} auth: {}", e.getMessage(), action.getOwnedBy().getId(), ((SysUser)auth.getPrincipal()).getId());
			return action;
		}
	}
}