001/*-
002 * #%L
003 * HAPI FHIR JPA Server - Batch2 Task Processor
004 * %%
005 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.batch2.model;
021
022import jakarta.annotation.Nonnull;
023import jakarta.annotation.Nullable;
024import org.apache.commons.lang3.builder.EqualsBuilder;
025import org.apache.commons.lang3.builder.HashCodeBuilder;
026
027/**
028 * The data required for the create transition.
029 * Payload for the work-chunk creation event including all the job coordinates, the chunk data, and a sequence within the step.
030 * @see hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/docs/server_jpa_batch/batch2_states.md
031 */
032public class WorkChunkCreateEvent {
033        public final String jobDefinitionId;
034        public final int jobDefinitionVersion;
035        public final String targetStepId;
036        public final String instanceId;
037        public final int sequence;
038        public final String serializedData;
039        public final boolean isGatedExecution;
040
041        /**
042         * Constructor
043         *
044         * @param theJobDefinitionId      The job definition ID
045         * @param theJobDefinitionVersion The job definition version
046         * @param theTargetStepId         The step ID that will be responsible for consuming this chunk
047         * @param theInstanceId           The instance ID associated with this chunk
048         * @param theSerializedData       The data. This will be in the form of a map where the values may be strings, lists, and other maps (i.e. JSON)
049         */
050        public WorkChunkCreateEvent(
051                        @Nonnull String theJobDefinitionId,
052                        int theJobDefinitionVersion,
053                        @Nonnull String theTargetStepId,
054                        @Nonnull String theInstanceId,
055                        int theSequence,
056                        @Nullable String theSerializedData,
057                        boolean theGatedExecution) {
058                jobDefinitionId = theJobDefinitionId;
059                jobDefinitionVersion = theJobDefinitionVersion;
060                targetStepId = theTargetStepId;
061                instanceId = theInstanceId;
062                sequence = theSequence;
063                serializedData = theSerializedData;
064                isGatedExecution = theGatedExecution;
065        }
066
067        /**
068         * Creates the WorkChunkCreateEvent for the first chunk of a job.
069         */
070        public static WorkChunkCreateEvent firstChunk(JobDefinition<?> theJobDefinition, String theInstanceId) {
071                String firstStepId = theJobDefinition.getFirstStepId();
072                String jobDefinitionId = theJobDefinition.getJobDefinitionId();
073                int jobDefinitionVersion = theJobDefinition.getJobDefinitionVersion();
074                // the first chunk of a job is always READY, no matter whether the job is gated
075                boolean isGatedExecution = false;
076                return new WorkChunkCreateEvent(
077                                jobDefinitionId, jobDefinitionVersion, firstStepId, theInstanceId, 0, null, isGatedExecution);
078        }
079
080        @Override
081        public boolean equals(Object theO) {
082                if (this == theO) return true;
083
084                if (theO == null || getClass() != theO.getClass()) return false;
085
086                WorkChunkCreateEvent that = (WorkChunkCreateEvent) theO;
087
088                return new EqualsBuilder()
089                                .append(jobDefinitionId, that.jobDefinitionId)
090                                .append(jobDefinitionVersion, that.jobDefinitionVersion)
091                                .append(targetStepId, that.targetStepId)
092                                .append(instanceId, that.instanceId)
093                                .append(sequence, that.sequence)
094                                .append(serializedData, that.serializedData)
095                                .append(isGatedExecution, that.isGatedExecution)
096                                .isEquals();
097        }
098
099        @Override
100        public int hashCode() {
101                return new HashCodeBuilder(17, 37)
102                                .append(jobDefinitionId)
103                                .append(jobDefinitionVersion)
104                                .append(targetStepId)
105                                .append(instanceId)
106                                .append(sequence)
107                                .append(serializedData)
108                                .append(isGatedExecution)
109                                .toHashCode();
110        }
111}