WorkflowStepInfo.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.model;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.genesys.blocks.util.JsonClassNameWriter;
import org.gringlobal.custom.json.CustomTypeIdResolver;

import java.util.HashMap;
import java.util.Map;

@Data
@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "_class")
@JsonTypeIdResolver(WorkflowStepInfo.WorkflowStepInfoTypeIdResolver.class)
/* @formatter:off */
@JsonAppend(props = {
	@JsonAppend.Prop(name = "_class", value = JsonClassNameWriter.class, type = String.class),
})
/* @formatter:on */
@Schema(name = "WorkflowStepInfo")
public abstract class WorkflowStepInfo {

	private Long id;
	private long workflowId;

	@Data
	@EqualsAndHashCode(callSuper = true)
	public static class WorkflowStartStepInfo extends WorkflowStepInfo {
	}

	@Data
	@EqualsAndHashCode(callSuper = true)
	public static class WorkflowEndStepInfo extends WorkflowStepInfo {
	}

	@Data
	@EqualsAndHashCode(callSuper = true)
	public static class WorkflowActionStepInfo extends WorkflowStepInfo {
		private String actionNameCode;
		private AclSidInfo actionAssignee;
		private String delay;
	}
	
	public static class WorkflowStepInfoTypeIdResolver extends CustomTypeIdResolver {
		@Override
		protected Map<String, Class<?>> initClassesMap() {
			Map<String, Class<?>> classes = new HashMap<>();
			classes.put(WorkflowStartStepInfo.class.getSimpleName(), WorkflowStartStepInfo.class);
			classes.put(WorkflowActionStepInfo.class.getSimpleName(), WorkflowActionStepInfo.class);
			classes.put(WorkflowEndStepInfo.class.getSimpleName(), WorkflowEndStepInfo.class);
			return classes;
		}
	}
}