public List<RecordWeb> getMatchingEntities(EntityWeb entityModel, RecordWeb entity)
      throws Exception {
    log.debug(
        "Received request to retrieve a list of entity instance records that match the entity specified as a parameter.");

    authenticateCaller();
    try {
      RecordQueryService entityInstanceService = Context.getRecordQueryService();
      EntityDefinitionManagerService entityDefService = Context.getEntityDefinitionManagerService();
      Entity entityDef = entityDefService.loadEntity(entityModel.getEntityVersionId());

      // pass the entity definition model for formatting the Date search string
      org.openhie.openempi.model.Record recordForMatch =
          ModelTransformer.mapToRecordForSearch(entityDef, entity, Record.class);

      List<Record> records =
          entityInstanceService.findRecordsByAttributes(entityDef, recordForMatch);

      List<RecordWeb> dtos = new java.util.ArrayList<RecordWeb>(records.size());
      for (Record record : records) {
        RecordWeb dto = ModelTransformer.mapToRecord(record, RecordWeb.class);
        dtos.add(dto);
      }

      return dtos;
    } catch (Throwable t) {
      log.error("Failed to execute: " + t.getMessage(), t);
      throw new RuntimeException(t);
    }
  }
  @Override
  public RecordListWeb getEntityRecordsBySearch(RecordSearchCriteriaWeb searchCriteria)
      throws Exception {
    log.debug("Get Entity Records By Search");

    authenticateCaller();
    try {
      RecordQueryService entityInstanceService = Context.getRecordQueryService();
      EntityDefinitionManagerService entityDefService = Context.getEntityDefinitionManagerService();

      EntityWeb entityModel = searchCriteria.getEntityModel();
      Entity entityDef = entityDefService.loadEntity(entityModel.getEntityVersionId());

      RecordWeb entity = searchCriteria.getRecord();
      int offset = searchCriteria.getFirstResult();
      int pageSize = searchCriteria.getMaxResults();

      org.openhie.openempi.model.Record recordForMatch =
          ModelTransformer.mapToRecordForSearch(entityDef, entity, Record.class);

      // Get total count
      Long totalCount = searchCriteria.getTotalCount();
      if (totalCount == 0) {
        totalCount = entityInstanceService.getRecordCount(entityDef, recordForMatch);
      }

      // get List of Record
      List<Record> records =
          entityInstanceService.findRecordsByAttributes(
              entityDef, recordForMatch, offset, pageSize);

      List<RecordWeb> dtos = new java.util.ArrayList<RecordWeb>(records.size());
      for (Record record : records) {
        RecordWeb dto = ModelTransformer.mapToRecord(record, RecordWeb.class);
        dtos.add(dto);
      }

      RecordListWeb recordList = new RecordListWeb();
      recordList.setTotalCount(totalCount);
      recordList.setRecords(dtos);

      return recordList;

    } catch (Exception e) {
      log.error("Failed while trying to get audit events: " + e, e);
    }
    return null;
  }