@Override
  public RecordLinksListWeb loadRecordLinksPaged(RecordSearchCriteriaWeb searchCriteria)
      throws Exception {
    log.debug("Get Record Links");

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

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

      int offset = searchCriteria.getFirstResult();
      int pageSize = searchCriteria.getMaxResults();
      String state = searchCriteria.getSearchMode().substring(0, 1);

      // Get total count
      Long totalCount = searchCriteria.getTotalCount();
      if (totalCount == 0) {
        totalCount =
            entityInstanceService.getRecordLinkCount(entityDef, RecordLinkState.fromString(state));
      }

      // get List of Record
      List<RecordLink> recordLinks =
          entityInstanceService.loadRecordLinks(
              entityDef, RecordLinkState.fromString(state), offset, pageSize);

      List<RecordLinkWeb> dtos = new java.util.ArrayList<RecordLinkWeb>(recordLinks.size());
      for (RecordLink record : recordLinks) {

        // loadRecordLinks without left and right Records
        RecordLinkWeb dto = ModelTransformer.mapToRecordLink(record, RecordLinkWeb.class, false);
        dtos.add(dto);
      }

      RecordLinksListWeb recordList = new RecordLinksListWeb();
      recordList.setTotalCount(totalCount);
      recordList.setRecordLinks(dtos);

      return recordList;

    } catch (Exception e) {
      log.error("Failed while trying to get audit events: " + e, e);
    }
    return null;
  }
  @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;
  }
  public RecordListWeb findEntitiesByIdentifier(RecordSearchCriteriaWeb searchCriteria)
      throws Exception {
    log.debug("Received entity records by identifier.");

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

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

      IdentifierWeb identifierWeb = searchCriteria.getIdentifier();
      int offset = searchCriteria.getFirstResult();
      int pageSize = searchCriteria.getMaxResults();

      org.openhie.openempi.model.Identifier identifier =
          ModelTransformer.map(identifierWeb, org.openhie.openempi.model.Identifier.class);

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

      // get List of Record
      List<Record> records =
          entityInstanceService.findRecordsByIdentifier(entityDef, identifier, 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 (Throwable t) {
      log.error("Failed to execute: " + t.getMessage(), t);
      throw new RuntimeException(t);
    }
  }