WorkflowActionStep.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.model.workflow;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.security.model.AclSid;
import org.gringlobal.api.exception.InvalidApiUsageException;
import org.gringlobal.custom.validation.javax.PeriodExpressionValidation;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.validation.constraints.Size;

import static org.gringlobal.model.community.CommunityCodeValues.CODE_VALUE_LENGTH;

@Entity
@Getter
@Setter
@NoArgsConstructor
public class WorkflowActionStep extends WorkflowStep {

	private static final long serialVersionUID = 4867031265286775747L;

	/** The action to be scheduled in this workflow step. */
	@Column(name = "action_name_code", updatable = false, length = CODE_VALUE_LENGTH)
	private String actionNameCode;

	/** The assignee of the action scheduled in this workflow step. */
	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "assignee_id")
	private AclSid actionAssignee;

	@Size(max = 20)
	@Column(length = 20)
	@PeriodExpressionValidation
	private String delay;

	public WorkflowActionStep(Long id) {
		super(id);
	}
	
	@PrePersist
	@Override
	protected void prePersist() {
		if (StringUtils.isBlank(actionNameCode)) {
			throw new InvalidApiUsageException("actionNameCode not specified");
		}
		if (actionAssignee == null || actionAssignee.getId() == null) {
			throw new InvalidApiUsageException("actionAssignee not specified");
		}
		super.prePersist();
	}

	@Override
	public boolean canEqual(Object other) {
		return other instanceof WorkflowActionStep;
	}
}