Program: 112274
Project: 112274
Buggy source:
protected ResourceTable updateEntity(final IResource theResource,ResourceTable entity,boolean theUpdateHistory,Date theDeletedTimestampOrNull,Date theUpdateTime){
  return updateEntity(theResource,entity,theUpdateHistory,theDeletedTimestampOrNull,true,true,theUpdateTime);
}

Fixed version (ground truth):
@SuppressWarnings("String_Node_Str") protected ResourceTable updateEntity(final IBaseResource theResource,ResourceTable theEntity,boolean theUpdateHistory,Date theDeletedTimestampOrNull,boolean thePerformIndexing,boolean theUpdateVersion,Date theUpdateTime,RequestDetails theRequestDetails){
  if (theResource != null) {
    if (thePerformIndexing) {
      validateResourceForStorage((T)theResource,theEntity,theRequestDetails);
    }
    String resourceType=myContext.getResourceDefinition(theResource).getName();
    if (isNotBlank(theEntity.getResourceType()) && !theEntity.getResourceType().equals(resourceType)) {
      throw new UnprocessableEntityException("String_Node_Str" + theEntity.getIdDt().toUnqualifiedVersionless() + "String_Node_Str"+ theEntity.getResourceType()+ "String_Node_Str"+ resourceType+ "String_Node_Str");
    }
  }
  if (theEntity.getPublished() == null) {
    theEntity.setPublished(theUpdateTime);
  }
  if (theUpdateHistory) {
    final ResourceHistoryTable historyEntry=theEntity.toHistory();
    myEntityManager.persist(historyEntry);
  }
  if (theUpdateVersion) {
    theEntity.setVersion(theEntity.getVersion() + 1);
  }
  Collection<ResourceIndexedSearchParamString> paramsString=new ArrayList<ResourceIndexedSearchParamString>();
  if (theEntity.isParamsStringPopulated()) {
    paramsString.addAll(theEntity.getParamsString());
  }
  Collection<ResourceIndexedSearchParamToken> paramsToken=new ArrayList<ResourceIndexedSearchParamToken>();
  if (theEntity.isParamsTokenPopulated()) {
    paramsToken.addAll(theEntity.getParamsToken());
  }
  Collection<ResourceIndexedSearchParamNumber> paramsNumber=new ArrayList<ResourceIndexedSearchParamNumber>();
  if (theEntity.isParamsNumberPopulated()) {
    paramsNumber.addAll(theEntity.getParamsNumber());
  }
  Collection<ResourceIndexedSearchParamQuantity> paramsQuantity=new ArrayList<ResourceIndexedSearchParamQuantity>();
  if (theEntity.isParamsQuantityPopulated()) {
    paramsQuantity.addAll(theEntity.getParamsQuantity());
  }
  Collection<ResourceIndexedSearchParamDate> paramsDate=new ArrayList<ResourceIndexedSearchParamDate>();
  if (theEntity.isParamsDatePopulated()) {
    paramsDate.addAll(theEntity.getParamsDate());
  }
  Collection<ResourceIndexedSearchParamUri> paramsUri=new ArrayList<ResourceIndexedSearchParamUri>();
  if (theEntity.isParamsUriPopulated()) {
    paramsUri.addAll(theEntity.getParamsUri());
  }
  Collection<ResourceIndexedSearchParamCoords> paramsCoords=new ArrayList<ResourceIndexedSearchParamCoords>();
  if (theEntity.isParamsCoordsPopulated()) {
    paramsCoords.addAll(theEntity.getParamsCoords());
  }
  Collection<ResourceLink> existingResourceLinks=new ArrayList<ResourceLink>();
  if (theEntity.isHasLinks()) {
    existingResourceLinks.addAll(theEntity.getResourceLinks());
  }
  Set<ResourceIndexedSearchParamString> stringParams=null;
  Set<ResourceIndexedSearchParamToken> tokenParams=null;
  Set<ResourceIndexedSearchParamNumber> numberParams=null;
  Set<ResourceIndexedSearchParamQuantity> quantityParams=null;
  Set<ResourceIndexedSearchParamDate> dateParams=null;
  Set<ResourceIndexedSearchParamUri> uriParams=null;
  Set<ResourceIndexedSearchParamCoords> coordsParams=null;
  Set<ResourceLink> links=null;
  if (theDeletedTimestampOrNull != null) {
    stringParams=Collections.emptySet();
    tokenParams=Collections.emptySet();
    numberParams=Collections.emptySet();
    quantityParams=Collections.emptySet();
    dateParams=Collections.emptySet();
    uriParams=Collections.emptySet();
    coordsParams=Collections.emptySet();
    links=Collections.emptySet();
    theEntity.setDeleted(theDeletedTimestampOrNull);
    theEntity.setUpdated(theDeletedTimestampOrNull);
    theEntity.setNarrativeTextParsedIntoWords(null);
    theEntity.setContentTextParsedIntoWords(null);
  }
 else {
    theEntity.setDeleted(null);
    if (thePerformIndexing) {
      stringParams=extractSearchParamStrings(theEntity,theResource);
      numberParams=extractSearchParamNumber(theEntity,theResource);
      quantityParams=extractSearchParamQuantity(theEntity,theResource);
      dateParams=extractSearchParamDates(theEntity,theResource);
      uriParams=extractSearchParamUri(theEntity,theResource);
      coordsParams=extractSearchParamCoords(theEntity,theResource);
      ourLog.trace("String_Node_Str",stringParams);
      tokenParams=new HashSet<ResourceIndexedSearchParamToken>();
      for (      BaseResourceIndexedSearchParam next : extractSearchParamTokens(theEntity,theResource)) {
        if (next instanceof ResourceIndexedSearchParamToken) {
          tokenParams.add((ResourceIndexedSearchParamToken)next);
        }
 else {
          stringParams.add((ResourceIndexedSearchParamString)next);
        }
      }
      if (myConfig.isAllowInlineMatchUrlReferences()) {
        FhirTerser terser=getContext().newTerser();
        List<IBaseReference> allRefs=terser.getAllPopulatedChildElementsOfType(theResource,IBaseReference.class);
        for (        IBaseReference nextRef : allRefs) {
          IIdType nextId=nextRef.getReferenceElement();
          String nextIdText=nextId.getValue();
          int qmIndex=nextIdText.indexOf('?');
          if (qmIndex != -1) {
            for (int i=qmIndex - 1; i >= 0; i--) {
              if (nextIdText.charAt(i) == '/') {
                nextIdText=nextIdText.substring(i + 1);
                break;
              }
            }
            String resourceTypeString=nextIdText.substring(0,nextIdText.indexOf('?'));
            RuntimeResourceDefinition matchResourceDef=getContext().getResourceDefinition(resourceTypeString);
            if (matchResourceDef == null) {
              String msg=getContext().getLocalizer().getMessage(BaseHapiFhirDao.class,"String_Node_Str",nextId.getValue(),resourceTypeString);
              throw new InvalidRequestException(msg);
            }
            Class<? extends IBaseResource> matchResourceType=matchResourceDef.getImplementingClass();
            Set<Long> matches=processMatchUrl(nextIdText,matchResourceType);
            if (matches.isEmpty()) {
              String msg=getContext().getLocalizer().getMessage(BaseHapiFhirDao.class,"String_Node_Str",nextId.getValue());
              throw new InvalidRequestException(msg);
            }
            if (matches.size() > 1) {
              String msg=getContext().getLocalizer().getMessage(BaseHapiFhirDao.class,"String_Node_Str",nextId.getValue());
              throw new InvalidRequestException(msg);
            }
            Long next=matches.iterator().next();
            String newId=resourceTypeString + '/' + translatePidIdToForcedId(next);
            ourLog.info("String_Node_Str",nextId.getValue(),newId);
            nextRef.setReference(newId);
          }
        }
      }
      links=extractResourceLinks(theEntity,theResource);
      for (Iterator<ResourceLink> existingLinkIter=existingResourceLinks.iterator(); existingLinkIter.hasNext(); ) {
        ResourceLink nextExisting=existingLinkIter.next();
        if (links.remove(nextExisting)) {
          existingLinkIter.remove();
          links.add(nextExisting);
        }
      }
      populateResourceIntoEntity(theResource,theEntity);
      theEntity.setUpdated(theUpdateTime);
      if (theResource instanceof IResource) {
        theEntity.setLanguage(((IResource)theResource).getLanguage().getValue());
      }
 else {
        theEntity.setLanguage(((IAnyResource)theResource).getLanguageElement().getValue());
      }
      theEntity.setParamsString(stringParams);
      theEntity.setParamsStringPopulated(stringParams.isEmpty() == false);
      theEntity.setParamsToken(tokenParams);
      theEntity.setParamsTokenPopulated(tokenParams.isEmpty() == false);
      theEntity.setParamsNumber(numberParams);
      theEntity.setParamsNumberPopulated(numberParams.isEmpty() == false);
      theEntity.setParamsQuantity(quantityParams);
      theEntity.setParamsQuantityPopulated(quantityParams.isEmpty() == false);
      theEntity.setParamsDate(dateParams);
      theEntity.setParamsDatePopulated(dateParams.isEmpty() == false);
      theEntity.setParamsUri(uriParams);
      theEntity.setParamsUriPopulated(uriParams.isEmpty() == false);
      theEntity.setParamsCoords(coordsParams);
      theEntity.setParamsCoordsPopulated(coordsParams.isEmpty() == false);
      theEntity.setResourceLinks(links);
      theEntity.setHasLinks(links.isEmpty() == false);
      theEntity.setIndexStatus(INDEX_STATUS_INDEXED);
      theEntity.setNarrativeTextParsedIntoWords(parseNarrativeTextIntoWords(theResource));
      theEntity.setContentTextParsedIntoWords(parseContentTextIntoWords(theResource));
    }
 else {
      populateResourceIntoEntity(theResource,theEntity);
      theEntity.setUpdated(theUpdateTime);
      theEntity.setIndexStatus(null);
    }
  }
  if (theEntity.getId() == null) {
    myEntityManager.persist(theEntity);
    if (theEntity.getForcedId() != null) {
      myEntityManager.persist(theEntity.getForcedId());
    }
    postPersist(theEntity,(T)theResource);
  }
 else {
    theEntity=myEntityManager.merge(theEntity);
    postUpdate(theEntity,(T)theResource);
  }
  if (thePerformIndexing) {
    for (    ResourceIndexedSearchParamString next : paramsString) {
      myEntityManager.remove(next);
    }
    for (    ResourceIndexedSearchParamString next : stringParams) {
      myEntityManager.persist(next);
    }
    for (    ResourceIndexedSearchParamToken next : paramsToken) {
      myEntityManager.remove(next);
    }
    for (    ResourceIndexedSearchParamToken next : tokenParams) {
      myEntityManager.persist(next);
    }
    for (    ResourceIndexedSearchParamNumber next : paramsNumber) {
      myEntityManager.remove(next);
    }
    for (    ResourceIndexedSearchParamNumber next : numberParams) {
      myEntityManager.persist(next);
    }
    for (    ResourceIndexedSearchParamQuantity next : paramsQuantity) {
      myEntityManager.remove(next);
    }
    for (    ResourceIndexedSearchParamQuantity next : quantityParams) {
      myEntityManager.persist(next);
    }
    for (    ResourceIndexedSearchParamDate next : paramsDate) {
      myEntityManager.remove(next);
    }
    for (    ResourceIndexedSearchParamDate next : dateParams) {
      myEntityManager.persist(next);
    }
    for (    ResourceIndexedSearchParamUri next : paramsUri) {
      myEntityManager.remove(next);
    }
    for (    ResourceIndexedSearchParamUri next : uriParams) {
      myEntityManager.persist(next);
    }
    for (    ResourceIndexedSearchParamCoords next : paramsCoords) {
      myEntityManager.remove(next);
    }
    for (    ResourceIndexedSearchParamCoords next : coordsParams) {
      myEntityManager.persist(next);
    }
    for (    ResourceLink next : existingResourceLinks) {
      myEntityManager.remove(next);
    }
    for (    ResourceLink next : links) {
      myEntityManager.persist(next);
    }
  }
  theEntity=myEntityManager.merge(theEntity);
  myEntityManager.flush();
  if (theResource != null) {
    populateResourceId(theResource,theEntity);
  }
  return theEntity;
}