diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirDao.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirDao.java index 387babd3eda..a9e44d26aec 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirDao.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirDao.java @@ -618,49 +618,7 @@ public abstract class BaseHapiFhirDao extends BaseStora skipUpdatingTags |= myConfig.getTagStorageMode() == DaoConfig.TagStorageModeEnum.INLINE; if (!skipUpdatingTags) { - Set allDefs = new HashSet<>(); - Set allTagsOld = getAllTagDefinitions(theEntity); - - if (theResource instanceof IResource) { - extractTagsHapi(theTransactionDetails, (IResource) theResource, theEntity, allDefs); - } else { - extractTagsRi(theTransactionDetails, (IAnyResource) theResource, theEntity, allDefs); - } - - RuntimeResourceDefinition def = myContext.getResourceDefinition(theResource); - if (def.isStandardType() == false) { - String profile = def.getResourceProfile(""); - if (isNotBlank(profile)) { - TagDefinition profileDef = getTagOrNull(theTransactionDetails, TagTypeEnum.PROFILE, NS_JPA_PROFILE, profile, null); - - ResourceTag tag = theEntity.addTag(profileDef); - allDefs.add(tag); - theEntity.setHasTags(true); - } - } - - Set allTagsNew = getAllTagDefinitions(theEntity); - Set allDefsPresent = new HashSet<>(); - allTagsNew.forEach(tag -> { - - // Don't keep duplicate tags - if (!allDefsPresent.add(tag.getTag())) { - theEntity.getTags().remove(tag); - } - - // Drop any tags that have been removed - if (!allDefs.contains(tag)) { - if (shouldDroppedTagBeRemovedOnUpdate(theRequest, tag)) { - theEntity.getTags().remove(tag); - } - } - - }); - - if (!allTagsOld.equals(allTagsNew)) { - changed = true; - } - theEntity.setHasTags(!allTagsNew.isEmpty()); + changed |= updateTags(theTransactionDetails, theRequest, theResource, theEntity); } } else { @@ -669,7 +627,7 @@ public abstract class BaseHapiFhirDao extends BaseStora encoding = ResourceEncodingEnum.DEL; } - if (thePerformIndexing && changed == false) { + if (thePerformIndexing && !changed) { if (theEntity.getId() == null) { changed = true; } else if (myConfig.isMassIngestionMode()) { @@ -701,6 +659,51 @@ public abstract class BaseHapiFhirDao extends BaseStora return retVal; } + private boolean updateTags(TransactionDetails theTransactionDetails, RequestDetails theRequest, IBaseResource theResource, ResourceTable theEntity) { + Set allDefs = new HashSet<>(); + Set allTagsOld = getAllTagDefinitions(theEntity); + + if (theResource instanceof IResource) { + extractTagsHapi(theTransactionDetails, (IResource) theResource, theEntity, allDefs); + } else { + extractTagsRi(theTransactionDetails, (IAnyResource) theResource, theEntity, allDefs); + } + + RuntimeResourceDefinition def = myContext.getResourceDefinition(theResource); + if (def.isStandardType() == false) { + String profile = def.getResourceProfile(""); + if (isNotBlank(profile)) { + TagDefinition profileDef = getTagOrNull(theTransactionDetails, TagTypeEnum.PROFILE, NS_JPA_PROFILE, profile, null); + + ResourceTag tag = theEntity.addTag(profileDef); + allDefs.add(tag); + theEntity.setHasTags(true); + } + } + + Set allTagsNew = getAllTagDefinitions(theEntity); + Set allDefsPresent = new HashSet<>(); + allTagsNew.forEach(tag -> { + + // Don't keep duplicate tags + if (!allDefsPresent.add(tag.getTag())) { + theEntity.getTags().remove(tag); + } + + // Drop any tags that have been removed + if (!allDefs.contains(tag)) { + if (shouldDroppedTagBeRemovedOnUpdate(theRequest, tag)) { + theEntity.getTags().remove(tag); + } + } + + }); + + boolean retval = !allTagsOld.equals(allTagsNew); + theEntity.setHasTags(!allTagsNew.isEmpty()); + return retval; + } + @SuppressWarnings("unchecked") private R populateResourceMetadataHapi(Class theResourceType, IBaseResourceEntity theEntity, @Nullable Collection theTagList, boolean theForHistoryOperation, IResource res, Long theVersion) { R retVal = (R) res; @@ -1288,52 +1291,8 @@ public abstract class BaseHapiFhirDao extends BaseStora postUpdate(entity, (T) theResource); } - /* - * Create history entry - */ if (theCreateNewHistoryEntry) { - boolean versionedTags = getConfig().getTagStorageMode() == DaoConfig.TagStorageModeEnum.VERSIONED; - final ResourceHistoryTable historyEntry = entity.toHistory(versionedTags); - historyEntry.setEncoding(changed.getEncoding()); - historyEntry.setResource(changed.getResource()); - - ourLog.debug("Saving history entry {}", historyEntry.getIdDt()); - myResourceHistoryTableDao.save(historyEntry); - - // Save resource source - String source = null; - String requestId = theRequest != null ? theRequest.getRequestId() : null; - if (theResource != null) { - if (myContext.getVersion().getVersion().isEqualOrNewerThan(FhirVersionEnum.R4)) { - IBaseMetaType meta = theResource.getMeta(); - source = MetaUtil.getSource(myContext, meta); - } - if (myContext.getVersion().getVersion().equals(FhirVersionEnum.DSTU3)) { - source = ((IBaseHasExtensions) theResource.getMeta()) - .getExtension() - .stream() - .filter(t -> HapiExtensions.EXT_META_SOURCE.equals(t.getUrl())) - .filter(t -> t.getValue() instanceof IPrimitiveType) - .map(t -> ((IPrimitiveType) t.getValue()).getValueAsString()) - .findFirst() - .orElse(null); - } - } - boolean haveSource = isNotBlank(source) && myConfig.getStoreMetaSourceInformation().isStoreSourceUri(); - boolean haveRequestId = isNotBlank(requestId) && myConfig.getStoreMetaSourceInformation().isStoreRequestId(); - if (haveSource || haveRequestId) { - ResourceHistoryProvenanceEntity provenance = new ResourceHistoryProvenanceEntity(); - provenance.setResourceHistoryTable(historyEntry); - provenance.setResourceTable(entity); - provenance.setPartitionId(entity.getPartitionId()); - if (haveRequestId) { - provenance.setRequestId(left(requestId, Constants.REQUEST_ID_LENGTH)); - } - if (haveSource) { - provenance.setSourceUri(source); - } - myEntityManager.persist(provenance); - } + createHistoryEntry(theRequest, theResource, entity, changed); } /* @@ -1415,6 +1374,51 @@ public abstract class BaseHapiFhirDao extends BaseStora return entity; } + private void createHistoryEntry(RequestDetails theRequest, IBaseResource theResource, ResourceTable entity, EncodedResource changed) { + boolean versionedTags = getConfig().getTagStorageMode() == DaoConfig.TagStorageModeEnum.VERSIONED; + final ResourceHistoryTable historyEntry = entity.toHistory(versionedTags); + historyEntry.setEncoding(changed.getEncoding()); + historyEntry.setResource(changed.getResource()); + + ourLog.debug("Saving history entry {}", historyEntry.getIdDt()); + myResourceHistoryTableDao.save(historyEntry); + + // Save resource source + String source = null; + String requestId = theRequest != null ? theRequest.getRequestId() : null; + if (theResource != null) { + if (myContext.getVersion().getVersion().isEqualOrNewerThan(FhirVersionEnum.R4)) { + IBaseMetaType meta = theResource.getMeta(); + source = MetaUtil.getSource(myContext, meta); + } + if (myContext.getVersion().getVersion().equals(FhirVersionEnum.DSTU3)) { + source = ((IBaseHasExtensions) theResource.getMeta()) + .getExtension() + .stream() + .filter(t -> HapiExtensions.EXT_META_SOURCE.equals(t.getUrl())) + .filter(t -> t.getValue() instanceof IPrimitiveType) + .map(t -> ((IPrimitiveType) t.getValue()).getValueAsString()) + .findFirst() + .orElse(null); + } + } + boolean haveSource = isNotBlank(source) && myConfig.getStoreMetaSourceInformation().isStoreSourceUri(); + boolean haveRequestId = isNotBlank(requestId) && myConfig.getStoreMetaSourceInformation().isStoreRequestId(); + if (haveSource || haveRequestId) { + ResourceHistoryProvenanceEntity provenance = new ResourceHistoryProvenanceEntity(); + provenance.setResourceHistoryTable(historyEntry); + provenance.setResourceTable(entity); + provenance.setPartitionId(entity.getPartitionId()); + if (haveRequestId) { + provenance.setRequestId(left(requestId, Constants.REQUEST_ID_LENGTH)); + } + if (haveSource) { + provenance.setSourceUri(source); + } + myEntityManager.persist(provenance); + } + } + private void validateIncomingResourceTypeMatchesExisting(IBaseResource theResource, ResourceTable entity) { String resourceType = myContext.getResourceType(theResource); if (!resourceType.equals(entity.getResourceType())) { diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirResourceDao.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirResourceDao.java index 2d2911b9f92..39ef8a38ca4 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirResourceDao.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirResourceDao.java @@ -51,7 +51,6 @@ import ca.uhn.fhir.jpa.partition.SystemRequestDetails; import ca.uhn.fhir.jpa.patch.FhirPatch; import ca.uhn.fhir.jpa.patch.JsonPatchUtils; import ca.uhn.fhir.jpa.patch.XmlPatchUtils; -import ca.uhn.fhir.jpa.search.DatabaseBackedPagingProvider; import ca.uhn.fhir.jpa.search.PersistedJpaBundleProvider; import ca.uhn.fhir.jpa.search.cache.SearchCacheStatusEnum; import ca.uhn.fhir.jpa.search.reindex.IResourceReindexingSvc; @@ -1407,7 +1406,7 @@ public abstract class BaseHapiFhirResourceDao extends B } } - translateSearchParams(theParams); + translateListSearchParams(theParams); notifySearchInterceptors(theParams, theRequest); @@ -1432,7 +1431,7 @@ public abstract class BaseHapiFhirResourceDao extends B return retVal; } - private void translateSearchParams(SearchParameterMap theParams) { + private void translateListSearchParams(SearchParameterMap theParams) { Iterator keyIterator = theParams.keySet().iterator(); // Translate _list=42 to _has=List:item:_id=42