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.api; 021 022import ca.uhn.fhir.batch2.model.JobInstance; 023import ca.uhn.fhir.batch2.model.WorkChunk; 024import ca.uhn.fhir.model.api.IModelJson; 025import jakarta.annotation.Nonnull; 026import jakarta.annotation.Nullable; 027import org.apache.commons.lang3.Validate; 028 029import static ca.uhn.fhir.batch2.util.Batch2Utils.REDUCTION_STEP_CHUNK_ID_PLACEHOLDER; 030 031public class StepExecutionDetails<PT extends IModelJson, IT extends IModelJson> { 032 033 private final PT myParameters; 034 private final IT myData; 035 private final IJobInstance myInstance; 036 private final WorkChunk myChunk; 037 038 /** 039 * Create and returns a step execution details for a reduction job 040 */ 041 public static <P1 extends IModelJson, I1 extends IModelJson> 042 StepExecutionDetails<P1, I1> createReductionStepDetails( 043 P1 theParameters, I1 theIntermediateParams, JobInstance theInstance) { 044 WorkChunk reductionChunk = new WorkChunk().setId(REDUCTION_STEP_CHUNK_ID_PLACEHOLDER); 045 046 return new StepExecutionDetails<>(theParameters, theIntermediateParams, theInstance, reductionChunk); 047 } 048 049 /** 050 * Deprecated in 7.3 051 */ 052 @Deprecated 053 public StepExecutionDetails( 054 @Nonnull PT theParameters, @Nullable IT theData, @Nonnull JobInstance theInstance, String theChunkId) { 055 this( 056 theParameters, 057 theData, 058 theInstance, 059 new WorkChunk() 060 .setId(theChunkId) 061 .setInstanceId(theInstance.getInstanceId()) 062 .setData(theData)); 063 } 064 065 public StepExecutionDetails( 066 @Nonnull PT theParameters, 067 @Nullable IT theData, 068 @Nonnull JobInstance theInstance, 069 @Nonnull WorkChunk theChunk) { 070 Validate.notNull(theParameters); 071 myParameters = theParameters; 072 myData = theData; 073 // Make a copy so the step worker can't change the one passed in 074 myInstance = new JobInstance(theInstance); 075 myChunk = theChunk; 076 } 077 078 /** 079 * Returns the data associated with this step execution. 080 * This method should never be called during the first step of a job, 081 * or in a reduction step, and will never return <code>null</code> during 082 * any other steps. 083 * 084 * @throws NullPointerException If this method is called during the first step of a job 085 */ 086 @Nonnull 087 public IT getData() { 088 Validate.notNull(myData); 089 return myData; 090 } 091 092 /** 093 * Returns the parameters associated with this job instance. Note that parameters 094 * are set when the job instance is created and can not be modified after that. 095 */ 096 @Nonnull 097 public PT getParameters() { 098 return myParameters; 099 } 100 101 /** 102 * Returns the job instance ID being executed 103 */ 104 @Nonnull 105 public IJobInstance getInstance() { 106 return myInstance; 107 } 108 109 /** 110 * Returns the work chunk ID being executed 111 */ 112 @Nonnull 113 public String getChunkId() { 114 return myChunk.getId(); 115 } 116 117 @Nonnull 118 public WorkChunk getWorkChunk() { 119 return myChunk; 120 } 121 122 /** 123 * Returns true if there's a workchunk to store data to. 124 * If false, failures and data storage go straight to the jobinstance instead 125 * @return - true if there's a workchunk in the db to store to. 126 * false if the output goes to the jobinstance instead 127 */ 128 public boolean hasAssociatedWorkChunk() { 129 return myChunk != null && !myChunk.isReductionWorkChunk(); 130 } 131}