private void expungeCurrentVersionOfResource(Long theResourceId, AtomicInteger theRemainingCount) { ResourceTable resource = myResourceTableDao.findById(theResourceId).orElseThrow(IllegalStateException::new); ResourceHistoryTable currentVersion = myResourceHistoryTableDao.findForIdAndVersion(resource.getId(), resource.getVersion()); if (currentVersion != null) { expungeHistoricalVersion(currentVersion.getId()); } ourLog.info("Expunging current version of resource {}", resource.getIdDt().getValue()); myResourceIndexedSearchParamUriDao.deleteAll(resource.getParamsUri()); myResourceIndexedSearchParamCoordsDao.deleteAll(resource.getParamsCoords()); myResourceIndexedSearchParamDateDao.deleteAll(resource.getParamsDate()); myResourceIndexedSearchParamNumberDao.deleteAll(resource.getParamsNumber()); myResourceIndexedSearchParamQuantityDao.deleteAll(resource.getParamsQuantity()); myResourceIndexedSearchParamStringDao.deleteAll(resource.getParamsString()); myResourceIndexedSearchParamTokenDao.deleteAll(resource.getParamsToken()); myResourceLinkDao.deleteAll(resource.getResourceLinks()); myResourceLinkDao.deleteAll(resource.getResourceLinksAsTarget()); myResourceTagDao.deleteAll(resource.getTags()); resource.getTags().clear(); if (resource.getForcedId() != null) { ForcedId forcedId = resource.getForcedId(); resource.setForcedId(null); myResourceTableDao.saveAndFlush(resource); myIdHelperService.delete(forcedId); } myResourceTableDao.delete(resource); theRemainingCount.decrementAndGet(); }
public void setParamsNumber(Collection<ResourceIndexedSearchParamNumber> theNumberParams) { if (!isParamsNumberPopulated() && theNumberParams.isEmpty()) { return; } getParamsNumber().clear(); getParamsNumber().addAll(theNumberParams); }
public void setResourceLinks(Collection<ResourceLink> theLinks) { if (!isHasLinks() && theLinks.isEmpty()) { return; } getResourceLinks().clear(); getResourceLinks().addAll(theLinks); }
public void setParamsQuantity(Collection<ResourceIndexedSearchParamQuantity> theQuantityParams) { if (!isParamsQuantityPopulated() && theQuantityParams.isEmpty()) { return; } getParamsQuantity().clear(); getParamsQuantity().addAll(theQuantityParams); }
public ResourceIndexedSearchParams(ResourceTable theEntity) { if (theEntity.isParamsStringPopulated()) { stringParams.addAll(theEntity.getParamsString()); } if (theEntity.isParamsTokenPopulated()) { tokenParams.addAll(theEntity.getParamsToken()); } if (theEntity.isParamsNumberPopulated()) { numberParams.addAll(theEntity.getParamsNumber()); } if (theEntity.isParamsQuantityPopulated()) { quantityParams.addAll(theEntity.getParamsQuantity()); } if (theEntity.isParamsDatePopulated()) { dateParams.addAll(theEntity.getParamsDate()); } if (theEntity.isParamsUriPopulated()) { uriParams.addAll(theEntity.getParamsUri()); } if (theEntity.isParamsCoordsPopulated()) { coordsParams.addAll(theEntity.getParamsCoords()); } if (theEntity.isHasLinks()) { links.addAll(theEntity.getResourceLinks()); } if (theEntity.isParamsCompositeStringUniquePresent()) { compositeStringUniques.addAll(theEntity.getParamsCompositeStringUnique()); } }
boolean theUpdateVersion, Date theUpdateTime, boolean theForceUpdate, boolean theCreateNewHistoryEntry) { Validate.notNull(theEntity); Validate.isTrue(theDeletedTimestampOrNull != null || theResource != null, "Must have either a resource[%s] or a deleted timestamp[%s] for resource PID[%s]", theDeletedTimestampOrNull != null, theResource != null, theEntity.getId()); if (isNotBlank(theEntity.getResourceType()) && !theEntity.getResourceType().equals(resourceType)) { throw new UnprocessableEntityException( "Existing resource ID[" + theEntity.getIdDt().toUnqualifiedVersionless() + "] is of type[" + theEntity.getResourceType() + "] - Cannot update with [" + resourceType + "]"); if (theEntity.getPublished() == null) { ourLog.debug("Entity has published time: {}", new InstantDt(theUpdateTime)); theEntity.setPublished(theUpdateTime); theEntity.setDeleted(theDeletedTimestampOrNull); theEntity.setUpdated(theDeletedTimestampOrNull); theEntity.setNarrativeTextParsedIntoWords(null); theEntity.setContentTextParsedIntoWords(null); theEntity.setHashSha256(null); theEntity.setIndexStatus(INDEX_STATUS_INDEXED); changed = populateResourceIntoEntity(theRequest, theResource, theEntity, true); theEntity.setDeleted(null); theEntity.setUpdated(theUpdateTime); if (theResource instanceof IResource) { theEntity.setLanguage(((IResource) theResource).getLanguage().getValue()); } else { theEntity.setLanguage(((IAnyResource) theResource).getLanguageElement().getValue());
reindexFailure = myTxTemplate.execute(t -> { ResourceTable resourceTable = myResourceTableDao.findById(myNextId).orElseThrow(IllegalStateException::new); myUpdated = resourceTable.getUpdatedDate(); ForcedId forcedId = resourceTable.getForcedId(); if (forcedId != null) { if (isBlank(forcedId.getResourceType())) { ourLog.info("Updating resource {} forcedId type to {}", forcedId.getForcedId(), resourceTable.getResourceType()); forcedId.setResourceType(resourceTable.getResourceType()); myForcedIdDao.save(forcedId); IFhirResourceDao<?> dao = myDaoRegistry.getResourceDao(resourceTable.getResourceType()); long expectedVersion = resourceTable.getVersion(); IBaseResource resource = dao.read(resourceTable.getIdDt().toVersionless(), null, true); if (resource == null) { throw new InternalErrorException("Could not find resource version " + resourceTable.getIdDt().toUnqualified().getValue() + " in database"); if (actualVersion < expectedVersion) { ourLog.warn("Resource {} version {} does not exist, renumbering version {}", resource.getIdElement().toUnqualifiedVersionless().getValue(), resource.getIdElement().getVersionIdPart(), expectedVersion); myResourceHistoryTableDao.updateVersion(resourceTable.getId(), actualVersion, expectedVersion); ourLog.error("Failed to index resource {}: {}", resourceTable.getIdDt(), e.toString(), e); t.setRollbackOnly(); return e;
public ResourceHistoryTable toHistory() { ResourceHistoryTable retVal = new ResourceHistoryTable(); retVal.setResourceId(myId); retVal.setResourceType(myResourceType); retVal.setVersion(myVersion); retVal.setPublished(getPublished()); retVal.setUpdated(getUpdated()); retVal.setFhirVersion(getFhirVersion()); retVal.setDeleted(getDeleted()); retVal.setForcedId(getForcedId()); retVal.getTags().clear(); retVal.setHasTags(isHasTags()); if (isHasTags()) { for (ResourceTag next : getTags()) { retVal.addTag(next); } } return retVal; }
@Override public String toString() { ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE); b.append("resPid", myResource.getIdDt().toUnqualifiedVersionless().getValue()); b.append("paramName", myParamName); b.append("present", myPresent); return b.build(); }
protected void validateOkToDelete(List<DeleteConflict> theDeleteConflicts, ResourceTable theEntity, boolean theForValidate) { TypedQuery<ResourceLink> query = myEntityManager.createQuery("SELECT l FROM ResourceLink l WHERE l.myTargetResourcePid = :target_pid", ResourceLink.class); query.setParameter("target_pid", theEntity.getId()); query.setMaxResults(1); List<ResourceLink> resultList = query.getResultList(); if (resultList.isEmpty()) { return; } if (myDaoConfig.isEnforceReferentialIntegrityOnDelete() == false && !theForValidate) { ourLog.debug("Deleting {} resource dependencies which can no longer be satisfied", resultList.size()); myResourceLinkDao.deleteAll(resultList); return; } ResourceLink link = resultList.get(0); IdDt targetId = theEntity.getIdDt(); IdDt sourceId = link.getSourceResource().getIdDt(); String sourcePath = link.getSourcePath(); theDeleteConflicts.add(new DeleteConflict(sourceId, sourcePath, targetId)); }
@Override public void reindex(T theResource, ResourceTable theEntity) { ourLog.debug("Indexing resource {} - PID {}", theEntity.getIdDt().getValue(), theEntity.getId()); if (theResource != null) { CURRENTLY_REINDEXING.put(theResource, Boolean.TRUE); } updateEntity(null, theResource, theEntity, theEntity.getDeleted(), true, false, theEntity.getUpdatedDate(), true, false); if (theResource != null) { CURRENTLY_REINDEXING.put(theResource, null); } }
/** * Returns the newly created forced ID. If the entity already had a forced ID, or if * none was created, returns null. */ protected ForcedId createForcedIdIfNeeded(ResourceTable theEntity, IIdType theId, boolean theCreateForPureNumericIds) { if (theId.isEmpty() == false && theId.hasIdPart() && theEntity.getForcedId() == null) { if (!theCreateForPureNumericIds && IdHelperService.isValidPid(theId)) { return null; } ForcedId fid = new ForcedId(); fid.setResourceType(theEntity.getResourceType()); fid.setForcedId(theId.getIdPart()); fid.setResource(theEntity); theEntity.setForcedId(fid); return fid; } return null; }
public void storeCompositeStringUniques(ResourceIndexedSearchParams theParams, ResourceTable theEntity, ResourceIndexedSearchParams existingParams) { // Store composite string uniques if (myDaoConfig.isUniqueIndexesEnabled()) { for (ResourceIndexedCompositeStringUnique next : myDaoSearchParamSynchronizer.subtract(existingParams.compositeStringUniques, theParams.compositeStringUniques)) { ourLog.debug("Removing unique index: {}", next); myEntityManager.remove(next); theEntity.getParamsCompositeStringUnique().remove(next); } for (ResourceIndexedCompositeStringUnique next : myDaoSearchParamSynchronizer.subtract(theParams.compositeStringUniques, existingParams.compositeStringUniques)) { if (myDaoConfig.isUniqueIndexesCheckedBeforeSave()) { ResourceIndexedCompositeStringUnique existing = myResourceIndexedCompositeStringUniqueDao.findByQueryString(next.getIndexString()); if (existing != null) { String msg = myContext.getLocalizer().getMessage(BaseHapiFhirDao.class, "uniqueIndexConflictFailure", theEntity.getResourceType(), next.getIndexString(), existing.getResource().getIdDt().toUnqualifiedVersionless().getValue()); throw new PreconditionFailedException(msg); } } ourLog.debug("Persisting unique index: {}", next); myEntityManager.persist(next); } } } }
Long pid = match.iterator().next(); entity = myEntityManager.find(ResourceTable.class, pid); resourceId = entity.getIdDt(); } else { return create(theResource, null, thePerformIndexing, new Date(), theRequestDetails); if (resourceId.hasVersionIdPart() && Long.parseLong(resourceId.getVersionIdPart()) != entity.getVersion()) { throw new ResourceVersionConflictException("Trying to update " + resourceId + " but this is not the current version"); "Invalid resource ID[" + entity.getIdDt().toUnqualifiedVersionless() + "] of type[" + entity.getResourceType() + "] - Does not match expected [" + getResourceName() + "]"); entity.setDeleted(null); theResource.setId(entity.getIdDt().getValue()); DaoMethodOutcome outcome = toMethodOutcome(entity, theResource).setCreated(false); outcome.setPreviousResource(oldResource);
ResourceTable entity = new ResourceTable(); entity.setResourceType(toResourceName(theResource)); theResource.setId(entity.getIdDt()); if (serverAssignedId) { switch (myDaoConfig.getResourceClientIdStrategy()) { if (!updatedEntity.isUnchangedInCurrentOperation()) { if (theRequest != null) { theRequest.getRequestOperationCallback().resourceCreated(theResource);
@Override protected ResourceTable updateEntity(RequestDetails theRequest, IBaseResource theResource, ResourceTable theEntity, Date theDeletedTimestampOrNull, boolean thePerformIndexing, boolean theUpdateVersion, Date theUpdateTime, boolean theForceUpdate, boolean theCreateNewHistoryEntry) { ResourceTable retVal = super.updateEntity(theRequest, theResource, theEntity, theDeletedTimestampOrNull, thePerformIndexing, theUpdateVersion, theUpdateTime, theForceUpdate, theCreateNewHistoryEntry); CodeSystem cs = (CodeSystem) theResource; if (cs != null && isNotBlank(cs.getUrl())) { String codeSystemUrl = cs.getUrl(); Long codeSystemResourcePid = retVal.getId(); if (retVal.getDeleted() != null) { // deleting } else if (cs.getContent() == CodeSystemContentMode.COMPLETE || cs.getContent() == null) { ourLog.info("CodeSystem {} has a status of {}, going to store concepts in terminology tables", retVal.getIdDt().getValue(), cs.getContentElement().getValueAsString()); TermCodeSystemVersion persCs = new TermCodeSystemVersion(); persCs.setResource(retVal); persCs.getConcepts().addAll(toPersistedConcepts(cs.getConcept(), persCs)); ourLog.info("Code system has {} concepts", persCs.getConcepts().size()); myTerminologySvc.storeNewCodeSystemVersion(codeSystemResourcePid, codeSystemUrl, cs.getName(), persCs); } } return retVal; }
protected void expungeHistoricalVersionsOfId(Long theResourceId, AtomicInteger theRemainingCount) { ResourceTable resource = myResourceTableDao.findById(theResourceId).orElseThrow(IllegalArgumentException::new); Pageable page = PageRequest.of(0, theRemainingCount.get()); Slice<Long> versionIds = myResourceHistoryTableDao.findForResourceId(page, resource.getId(), resource.getVersion()); ourLog.debug("Found {} versions of resource {} to expunge", versionIds.getNumberOfElements(), resource.getIdDt().getValue()); for (Long nextVersionId : versionIds) { expungeHistoricalVersion(nextVersionId); if (theRemainingCount.decrementAndGet() <= 0) { return; } } }
private void extractCompositeStringUniques(ResourceTable theEntity, ResourceIndexedSearchParams theParams) { final String resourceType = theEntity.getResourceType(); List<JpaRuntimeSearchParam> uniqueSearchParams = mySearchParamRegistry.getActiveUniqueSearchParams(resourceType); for (ResourceLink nextLink : linksForCompositePart) { if (linksForCompositePartWantPaths.contains(nextLink.getSourcePath())) { String value = nextLink.getTargetResource().getIdDt().toUnqualifiedVersionless().getValue(); if (isNotBlank(value)) { value = UrlUtil.escapeUrlParam(value);
if (theId.hasVersionIdPart() && Long.parseLong(theId.getVersionIdPart()) != entity.getVersion()) { throw new ResourceVersionConflictException("Trying to delete " + theId + " but this is not the current version"); if (entity.getDeleted() != null) { DaoMethodOutcome outcome = new DaoMethodOutcome(); outcome.setEntity(entity); id.setValue(entity.getIdDt().getValue()); outcome.setId(id); resourceToDelete.setId(entity.getIdDt());
public void setParamsCoords(Collection<ResourceIndexedSearchParamCoords> theParamsCoords) { if (!isParamsTokenPopulated() && theParamsCoords.isEmpty()) { return; } getParamsCoords().clear(); getParamsCoords().addAll(theParamsCoords); }