
001package ca.uhn.fhir.jpa.provider; 002 003/*- 004 * #%L 005 * HAPI FHIR JPA Server 006 * %% 007 * Copyright (C) 2014 - 2022 Smile CDR, Inc. 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import ca.uhn.fhir.i18n.Msg; 024import ca.uhn.fhir.context.FhirContext; 025import ca.uhn.fhir.jpa.api.dao.DaoRegistry; 026import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao; 027import ca.uhn.fhir.jpa.patch.FhirPatch; 028import ca.uhn.fhir.model.api.annotation.Description; 029import ca.uhn.fhir.rest.annotation.IdParam; 030import ca.uhn.fhir.rest.annotation.Operation; 031import ca.uhn.fhir.rest.annotation.OperationParam; 032import ca.uhn.fhir.rest.api.server.RequestDetails; 033import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; 034import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; 035import ca.uhn.fhir.rest.server.provider.ProviderConstants; 036import com.google.common.base.Objects; 037import org.hl7.fhir.instance.model.api.IBaseParameters; 038import org.hl7.fhir.instance.model.api.IBaseResource; 039import org.hl7.fhir.instance.model.api.IIdType; 040import org.hl7.fhir.instance.model.api.IPrimitiveType; 041import org.slf4j.Logger; 042import org.slf4j.LoggerFactory; 043import org.springframework.beans.factory.annotation.Autowired; 044 045import javax.annotation.Nonnull; 046 047public class DiffProvider { 048 private static final Logger ourLog = LoggerFactory.getLogger(DiffProvider.class); 049 @Autowired 050 private FhirContext myContext; 051 @Autowired 052 private DaoRegistry myDaoRegistry; 053 054 @Description( 055 value="This operation examines two resource versions (can be two versions of the same resource, or two different resources) and generates a FHIR Patch document showing the differences.", 056 shortDefinition = "Comparte two resources or two versions of a single resource") 057 @Operation(name = ProviderConstants.DIFF_OPERATION_NAME, global = true, idempotent = true) 058 public IBaseParameters diff( 059 @IdParam IIdType theResourceId, 060 061 @Description(value = "The resource ID and version to diff from", example = "Patient/example/version/1") 062 @OperationParam(name = ProviderConstants.DIFF_FROM_VERSION_PARAMETER, typeName = "string", min = 0, max = 1) 063 IPrimitiveType<?> theFromVersion, 064 065 @Description(value = "Should differences in the Resource.meta element be included in the diff", example = "false") 066 @OperationParam(name = ProviderConstants.DIFF_INCLUDE_META_PARAMETER, typeName = "boolean", min = 0, max = 1) 067 IPrimitiveType<Boolean> theIncludeMeta, 068 RequestDetails theRequestDetails) { 069 070 IFhirResourceDao<?> dao = myDaoRegistry.getResourceDao(theResourceId.getResourceType()); 071 IBaseResource targetResource = dao.read(theResourceId, theRequestDetails); 072 IBaseResource sourceResource = null; 073 074 Long versionId = targetResource.getIdElement().getVersionIdPartAsLong(); 075 076 if (theFromVersion == null || theFromVersion.getValueAsString() == null) { 077 078 // If no explicit from version is specified, find the next previous existing version 079 while (--versionId > 0L && sourceResource == null) { 080 IIdType nextVersionedId = theResourceId.withVersion(Long.toString(versionId)); 081 try { 082 sourceResource = dao.read(nextVersionedId, theRequestDetails); 083 } catch (ResourceNotFoundException e) { 084 ourLog.trace("Resource version {} can not be found, most likely it was expunged", nextVersionedId); 085 } 086 } 087 088 } else { 089 090 long fromVersion = Long.parseLong(theFromVersion.getValueAsString()); 091 sourceResource = dao.read(theResourceId.withVersion(Long.toString(fromVersion)), theRequestDetails); 092 093 } 094 095 FhirPatch fhirPatch = newPatch(theIncludeMeta); 096 return fhirPatch.diff(sourceResource, targetResource); 097 } 098 099 @Description("This operation examines two resource versions (can be two versions of the same resource, or two different resources) and generates a FHIR Patch document showing the differences.") 100 @Operation(name = ProviderConstants.DIFF_OPERATION_NAME, idempotent = true) 101 public IBaseParameters diff( 102 @Description(value = "The resource ID and version to diff from", example = "Patient/example/version/1") 103 @OperationParam(name = ProviderConstants.DIFF_FROM_PARAMETER, typeName = "id", min = 1, max = 1) 104 IIdType theFromVersion, 105 106 @Description(value = "The resource ID and version to diff to", example = "Patient/example/version/2") 107 @OperationParam(name = ProviderConstants.DIFF_TO_PARAMETER, typeName = "id", min = 1, max = 1) 108 IIdType theToVersion, 109 110 @Description(value = "Should differences in the Resource.meta element be included in the diff", example = "false") 111 @OperationParam(name = ProviderConstants.DIFF_INCLUDE_META_PARAMETER, typeName = "boolean", min = 0, max = 1) 112 IPrimitiveType<Boolean> theIncludeMeta, 113 RequestDetails theRequestDetails) { 114 115 if (!Objects.equal(theFromVersion.getResourceType(), theToVersion.getResourceType())) { 116 String msg = myContext.getLocalizer().getMessage(DiffProvider.class, "cantDiffDifferentTypes"); 117 throw new InvalidRequestException(Msg.code(1129) + msg); 118 } 119 120 IFhirResourceDao<?> dao = myDaoRegistry.getResourceDao(theFromVersion.getResourceType()); 121 IBaseResource sourceResource = dao.read(theFromVersion, theRequestDetails); 122 IBaseResource targetResource = dao.read(theToVersion, theRequestDetails); 123 124 FhirPatch fhirPatch = newPatch(theIncludeMeta); 125 return fhirPatch.diff(sourceResource, targetResource); 126 } 127 128 @Nonnull 129 public FhirPatch newPatch(IPrimitiveType<Boolean> theIncludeMeta) { 130 FhirPatch fhirPatch = new FhirPatch(myContext); 131 fhirPatch.setIncludePreviousValueInDiff(true); 132 133 if (theIncludeMeta != null && theIncludeMeta.getValue()) { 134 ourLog.trace("Including resource metadata in patch"); 135 } else { 136 fhirPatch.addIgnorePath("*.meta"); 137 } 138 139 return fhirPatch; 140 } 141 142}