Search in sources :

Example 6 with BeneficiaryMonthly

use of gov.cms.bfd.model.rif.BeneficiaryMonthly in project beneficiary-fhir-data by CMSgov.

the class PatientResourceProvider method queryBeneficiaryIdsByPartDContractCodeAndYearMonth.

/**
 * @param yearMonth the {@link BeneficiaryMonthly#getYearMonth()} value to match against
 * @param contractId the {@link BeneficiaryMonthly#getPartDContractNumberId()} value to match
 *     against
 * @param paging the {@link PatientLinkBuilder} being used for paging
 * @return the {@link List} of matching {@link Beneficiary#getBeneficiaryId()} values
 */
@Trace
private List<String> queryBeneficiaryIdsByPartDContractCodeAndYearMonth(LocalDate yearMonth, String contractId, PatientLinkBuilder paging) {
    // Create the query to run.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<String> beneIdCriteria = builder.createQuery(String.class);
    Root<BeneficiaryMonthly> beneMonthlyRoot = beneIdCriteria.from(BeneficiaryMonthly.class);
    beneIdCriteria.select(beneMonthlyRoot.get(BeneficiaryMonthly_.parentBeneficiary).get(Beneficiary_.beneficiaryId));
    List<Predicate> wherePredicates = new ArrayList<>();
    wherePredicates.add(builder.equal(beneMonthlyRoot.get(BeneficiaryMonthly_.yearMonth), yearMonth));
    wherePredicates.add(builder.equal(beneMonthlyRoot.get(BeneficiaryMonthly_.partDContractNumberId), contractId));
    if (paging.isPagingRequested() && !paging.isFirstPage()) {
        wherePredicates.add(builder.greaterThan(beneMonthlyRoot.get(BeneficiaryMonthly_.parentBeneficiary).get(Beneficiary_.beneficiaryId), paging.getCursor()));
    }
    beneIdCriteria.where(builder.and(wherePredicates.toArray(new Predicate[wherePredicates.size()])));
    beneIdCriteria.orderBy(builder.asc(beneMonthlyRoot.get(BeneficiaryMonthly_.parentBeneficiary)));
    // Run the query and return the results.
    List<String> matchingBeneIds = null;
    Long beneHistoryMatchesTimerQueryNanoSeconds = null;
    Timer.Context beneIdMatchesTimer = metricRegistry.timer(MetricRegistry.name(getClass().getSimpleName(), "query", "bene_ids_by_year_month_part_d_contract_id")).time();
    try {
        matchingBeneIds = entityManager.createQuery(beneIdCriteria).setMaxResults(paging.getQueryMaxSize()).getResultList();
        return matchingBeneIds;
    } finally {
        beneHistoryMatchesTimerQueryNanoSeconds = beneIdMatchesTimer.stop();
        TransformerUtils.recordQueryInMdc("bene_ids_by_year_month_part_d_contract_id", beneHistoryMatchesTimerQueryNanoSeconds, matchingBeneIds == null ? 0 : matchingBeneIds.size());
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Timer(com.codahale.metrics.Timer) ArrayList(java.util.ArrayList) BeneficiaryMonthly(gov.cms.bfd.model.rif.BeneficiaryMonthly) Predicate(javax.persistence.criteria.Predicate) Trace(com.newrelic.api.agent.Trace)

Example 7 with BeneficiaryMonthly

use of gov.cms.bfd.model.rif.BeneficiaryMonthly in project beneficiary-fhir-data by CMSgov.

the class RifLoader method updateBeneficiaryMonthly.

/**
 * Ensures that a {@link BeneficiaryMonthly} record is created or updated for the specified {@link
 * Beneficiary}, if that {@link Beneficiary} already exists and is just being updated.
 *
 * @param newBeneficiaryRecord the {@link Beneficiary} record being processed
 * @param oldBeneficiaryRecord the previous/current version of the {@link Beneficiary} (as it
 *     exists in the database before applying the specified {@link RifRecordEvent})
 */
private static void updateBeneficiaryMonthly(Beneficiary newBeneficiaryRecord, Optional<Beneficiary> oldBeneficiaryRecord) {
    if (newBeneficiaryRecord.getBeneEnrollmentReferenceYear().isPresent()) {
        int year = newBeneficiaryRecord.getBeneEnrollmentReferenceYear().get().intValue();
        List<BeneficiaryMonthly> currentYearBeneficiaryMonthly = new ArrayList<BeneficiaryMonthly>();
        BeneficiaryMonthly beneficiaryMonthly = getBeneficiaryMonthly(newBeneficiaryRecord, LocalDate.of(year, 1, 1), newBeneficiaryRecord.getEntitlementBuyInJanInd(), newBeneficiaryRecord.getFipsStateCntyJanCode(), newBeneficiaryRecord.getHmoIndicatorJanInd(), newBeneficiaryRecord.getMedicaidDualEligibilityJanCode(), newBeneficiaryRecord.getMedicareStatusJanCode(), newBeneficiaryRecord.getPartCContractNumberJanId(), newBeneficiaryRecord.getPartCPbpNumberJanId(), newBeneficiaryRecord.getPartCPlanTypeJanCode(), newBeneficiaryRecord.getPartDContractNumberJanId(), newBeneficiaryRecord.getPartDLowIncomeCostShareGroupJanCode(), newBeneficiaryRecord.getPartDPbpNumberJanId(), newBeneficiaryRecord.getPartDRetireeDrugSubsidyJanInd(), newBeneficiaryRecord.getPartDSegmentNumberJanId());
        if (beneficiaryMonthly != null) {
            currentYearBeneficiaryMonthly.add(beneficiaryMonthly);
        }
        beneficiaryMonthly = getBeneficiaryMonthly(newBeneficiaryRecord, LocalDate.of(year, 2, 1), newBeneficiaryRecord.getEntitlementBuyInFebInd(), newBeneficiaryRecord.getFipsStateCntyFebCode(), newBeneficiaryRecord.getHmoIndicatorFebInd(), newBeneficiaryRecord.getMedicaidDualEligibilityFebCode(), newBeneficiaryRecord.getMedicareStatusFebCode(), newBeneficiaryRecord.getPartCContractNumberFebId(), newBeneficiaryRecord.getPartCPbpNumberFebId(), newBeneficiaryRecord.getPartCPlanTypeFebCode(), newBeneficiaryRecord.getPartDContractNumberFebId(), newBeneficiaryRecord.getPartDLowIncomeCostShareGroupFebCode(), newBeneficiaryRecord.getPartDPbpNumberFebId(), newBeneficiaryRecord.getPartDRetireeDrugSubsidyFebInd(), newBeneficiaryRecord.getPartDSegmentNumberFebId());
        if (beneficiaryMonthly != null) {
            currentYearBeneficiaryMonthly.add(beneficiaryMonthly);
        }
        beneficiaryMonthly = getBeneficiaryMonthly(newBeneficiaryRecord, LocalDate.of(year, 3, 1), newBeneficiaryRecord.getEntitlementBuyInMarInd(), newBeneficiaryRecord.getFipsStateCntyMarCode(), newBeneficiaryRecord.getHmoIndicatorMarInd(), newBeneficiaryRecord.getMedicaidDualEligibilityMarCode(), newBeneficiaryRecord.getMedicareStatusMarCode(), newBeneficiaryRecord.getPartCContractNumberMarId(), newBeneficiaryRecord.getPartCPbpNumberMarId(), newBeneficiaryRecord.getPartCPlanTypeMarCode(), newBeneficiaryRecord.getPartDContractNumberMarId(), newBeneficiaryRecord.getPartDLowIncomeCostShareGroupMarCode(), newBeneficiaryRecord.getPartDPbpNumberMarId(), newBeneficiaryRecord.getPartDRetireeDrugSubsidyMarInd(), newBeneficiaryRecord.getPartDSegmentNumberMarId());
        if (beneficiaryMonthly != null) {
            currentYearBeneficiaryMonthly.add(beneficiaryMonthly);
        }
        beneficiaryMonthly = getBeneficiaryMonthly(newBeneficiaryRecord, LocalDate.of(year, 4, 1), newBeneficiaryRecord.getEntitlementBuyInAprInd(), newBeneficiaryRecord.getFipsStateCntyAprCode(), newBeneficiaryRecord.getHmoIndicatorAprInd(), newBeneficiaryRecord.getMedicaidDualEligibilityAprCode(), newBeneficiaryRecord.getMedicareStatusAprCode(), newBeneficiaryRecord.getPartCContractNumberAprId(), newBeneficiaryRecord.getPartCPbpNumberAprId(), newBeneficiaryRecord.getPartCPlanTypeAprCode(), newBeneficiaryRecord.getPartDContractNumberAprId(), newBeneficiaryRecord.getPartDLowIncomeCostShareGroupAprCode(), newBeneficiaryRecord.getPartDPbpNumberAprId(), newBeneficiaryRecord.getPartDRetireeDrugSubsidyAprInd(), newBeneficiaryRecord.getPartDSegmentNumberAprId());
        if (beneficiaryMonthly != null) {
            currentYearBeneficiaryMonthly.add(beneficiaryMonthly);
        }
        beneficiaryMonthly = getBeneficiaryMonthly(newBeneficiaryRecord, LocalDate.of(year, 5, 1), newBeneficiaryRecord.getEntitlementBuyInMayInd(), newBeneficiaryRecord.getFipsStateCntyMayCode(), newBeneficiaryRecord.getHmoIndicatorMayInd(), newBeneficiaryRecord.getMedicaidDualEligibilityMayCode(), newBeneficiaryRecord.getMedicareStatusMayCode(), newBeneficiaryRecord.getPartCContractNumberMayId(), newBeneficiaryRecord.getPartCPbpNumberMayId(), newBeneficiaryRecord.getPartCPlanTypeMayCode(), newBeneficiaryRecord.getPartDContractNumberMayId(), newBeneficiaryRecord.getPartDLowIncomeCostShareGroupMayCode(), newBeneficiaryRecord.getPartDPbpNumberMayId(), newBeneficiaryRecord.getPartDRetireeDrugSubsidyMayInd(), newBeneficiaryRecord.getPartDSegmentNumberMayId());
        if (beneficiaryMonthly != null) {
            currentYearBeneficiaryMonthly.add(beneficiaryMonthly);
        }
        beneficiaryMonthly = getBeneficiaryMonthly(newBeneficiaryRecord, LocalDate.of(year, 6, 1), newBeneficiaryRecord.getEntitlementBuyInJunInd(), newBeneficiaryRecord.getFipsStateCntyJunCode(), newBeneficiaryRecord.getHmoIndicatorJunInd(), newBeneficiaryRecord.getMedicaidDualEligibilityJunCode(), newBeneficiaryRecord.getMedicareStatusJunCode(), newBeneficiaryRecord.getPartCContractNumberJunId(), newBeneficiaryRecord.getPartCPbpNumberJunId(), newBeneficiaryRecord.getPartCPlanTypeJunCode(), newBeneficiaryRecord.getPartDContractNumberJunId(), newBeneficiaryRecord.getPartDLowIncomeCostShareGroupJunCode(), newBeneficiaryRecord.getPartDPbpNumberJunId(), newBeneficiaryRecord.getPartDRetireeDrugSubsidyJunInd(), newBeneficiaryRecord.getPartDSegmentNumberJunId());
        if (beneficiaryMonthly != null) {
            currentYearBeneficiaryMonthly.add(beneficiaryMonthly);
        }
        beneficiaryMonthly = getBeneficiaryMonthly(newBeneficiaryRecord, LocalDate.of(year, 7, 1), newBeneficiaryRecord.getEntitlementBuyInJulInd(), newBeneficiaryRecord.getFipsStateCntyJulCode(), newBeneficiaryRecord.getHmoIndicatorJulInd(), newBeneficiaryRecord.getMedicaidDualEligibilityJulCode(), newBeneficiaryRecord.getMedicareStatusJulCode(), newBeneficiaryRecord.getPartCContractNumberJulId(), newBeneficiaryRecord.getPartCPbpNumberJulId(), newBeneficiaryRecord.getPartCPlanTypeJulCode(), newBeneficiaryRecord.getPartDContractNumberJulId(), newBeneficiaryRecord.getPartDLowIncomeCostShareGroupJulCode(), newBeneficiaryRecord.getPartDPbpNumberJulId(), newBeneficiaryRecord.getPartDRetireeDrugSubsidyJulInd(), newBeneficiaryRecord.getPartDSegmentNumberJulId());
        if (beneficiaryMonthly != null) {
            currentYearBeneficiaryMonthly.add(beneficiaryMonthly);
        }
        beneficiaryMonthly = getBeneficiaryMonthly(newBeneficiaryRecord, LocalDate.of(year, 8, 1), newBeneficiaryRecord.getEntitlementBuyInAugInd(), newBeneficiaryRecord.getFipsStateCntyAugCode(), newBeneficiaryRecord.getHmoIndicatorAugInd(), newBeneficiaryRecord.getMedicaidDualEligibilityAugCode(), newBeneficiaryRecord.getMedicareStatusAugCode(), newBeneficiaryRecord.getPartCContractNumberAugId(), newBeneficiaryRecord.getPartCPbpNumberAugId(), newBeneficiaryRecord.getPartCPlanTypeAugCode(), newBeneficiaryRecord.getPartDContractNumberAugId(), newBeneficiaryRecord.getPartDLowIncomeCostShareGroupAugCode(), newBeneficiaryRecord.getPartDPbpNumberAugId(), newBeneficiaryRecord.getPartDRetireeDrugSubsidyAugInd(), newBeneficiaryRecord.getPartDSegmentNumberAugId());
        if (beneficiaryMonthly != null) {
            currentYearBeneficiaryMonthly.add(beneficiaryMonthly);
        }
        beneficiaryMonthly = getBeneficiaryMonthly(newBeneficiaryRecord, LocalDate.of(year, 9, 1), newBeneficiaryRecord.getEntitlementBuyInSeptInd(), newBeneficiaryRecord.getFipsStateCntySeptCode(), newBeneficiaryRecord.getHmoIndicatorSeptInd(), newBeneficiaryRecord.getMedicaidDualEligibilitySeptCode(), newBeneficiaryRecord.getMedicareStatusSeptCode(), newBeneficiaryRecord.getPartCContractNumberSeptId(), newBeneficiaryRecord.getPartCPbpNumberSeptId(), newBeneficiaryRecord.getPartCPlanTypeSeptCode(), newBeneficiaryRecord.getPartDContractNumberSeptId(), newBeneficiaryRecord.getPartDLowIncomeCostShareGroupSeptCode(), newBeneficiaryRecord.getPartDPbpNumberSeptId(), newBeneficiaryRecord.getPartDRetireeDrugSubsidySeptInd(), newBeneficiaryRecord.getPartDSegmentNumberSeptId());
        if (beneficiaryMonthly != null) {
            currentYearBeneficiaryMonthly.add(beneficiaryMonthly);
        }
        beneficiaryMonthly = getBeneficiaryMonthly(newBeneficiaryRecord, LocalDate.of(year, 10, 1), newBeneficiaryRecord.getEntitlementBuyInOctInd(), newBeneficiaryRecord.getFipsStateCntyOctCode(), newBeneficiaryRecord.getHmoIndicatorOctInd(), newBeneficiaryRecord.getMedicaidDualEligibilityOctCode(), newBeneficiaryRecord.getMedicareStatusOctCode(), newBeneficiaryRecord.getPartCContractNumberOctId(), newBeneficiaryRecord.getPartCPbpNumberOctId(), newBeneficiaryRecord.getPartCPlanTypeOctCode(), newBeneficiaryRecord.getPartDContractNumberOctId(), newBeneficiaryRecord.getPartDLowIncomeCostShareGroupOctCode(), newBeneficiaryRecord.getPartDPbpNumberOctId(), newBeneficiaryRecord.getPartDRetireeDrugSubsidyOctInd(), newBeneficiaryRecord.getPartDSegmentNumberOctId());
        if (beneficiaryMonthly != null) {
            currentYearBeneficiaryMonthly.add(beneficiaryMonthly);
        }
        beneficiaryMonthly = getBeneficiaryMonthly(newBeneficiaryRecord, LocalDate.of(year, 11, 1), newBeneficiaryRecord.getEntitlementBuyInNovInd(), newBeneficiaryRecord.getFipsStateCntyNovCode(), newBeneficiaryRecord.getHmoIndicatorNovInd(), newBeneficiaryRecord.getMedicaidDualEligibilityNovCode(), newBeneficiaryRecord.getMedicareStatusNovCode(), newBeneficiaryRecord.getPartCContractNumberNovId(), newBeneficiaryRecord.getPartCPbpNumberNovId(), newBeneficiaryRecord.getPartCPlanTypeNovCode(), newBeneficiaryRecord.getPartDContractNumberNovId(), newBeneficiaryRecord.getPartDLowIncomeCostShareGroupNovCode(), newBeneficiaryRecord.getPartDPbpNumberNovId(), newBeneficiaryRecord.getPartDRetireeDrugSubsidyNovInd(), newBeneficiaryRecord.getPartDSegmentNumberNovId());
        if (beneficiaryMonthly != null) {
            currentYearBeneficiaryMonthly.add(beneficiaryMonthly);
        }
        beneficiaryMonthly = getBeneficiaryMonthly(newBeneficiaryRecord, LocalDate.of(year, 12, 1), newBeneficiaryRecord.getEntitlementBuyInDecInd(), newBeneficiaryRecord.getFipsStateCntyDecCode(), newBeneficiaryRecord.getHmoIndicatorDecInd(), newBeneficiaryRecord.getMedicaidDualEligibilityDecCode(), newBeneficiaryRecord.getMedicareStatusDecCode(), newBeneficiaryRecord.getPartCContractNumberDecId(), newBeneficiaryRecord.getPartCPbpNumberDecId(), newBeneficiaryRecord.getPartCPlanTypeDecCode(), newBeneficiaryRecord.getPartDContractNumberDecId(), newBeneficiaryRecord.getPartDLowIncomeCostShareGroupDecCode(), newBeneficiaryRecord.getPartDPbpNumberDecId(), newBeneficiaryRecord.getPartDRetireeDrugSubsidyDecInd(), newBeneficiaryRecord.getPartDSegmentNumberDecId());
        if (beneficiaryMonthly != null) {
            currentYearBeneficiaryMonthly.add(beneficiaryMonthly);
        }
        if (currentYearBeneficiaryMonthly.size() > 0) {
            List<BeneficiaryMonthly> currentBeneficiaryMonthlyWithUpdates;
            if (oldBeneficiaryRecord.isPresent() && oldBeneficiaryRecord.get().getBeneficiaryMonthlys().size() > 0) {
                currentBeneficiaryMonthlyWithUpdates = oldBeneficiaryRecord.get().getBeneficiaryMonthlys();
                List<BeneficiaryMonthly> currentYearBeneficiaryMonthlyPrevious = oldBeneficiaryRecord.get().getBeneficiaryMonthlys().stream().filter(e -> year == e.getYearMonth().getYear()).collect(Collectors.toList());
                for (BeneficiaryMonthly previousEnrollment : currentYearBeneficiaryMonthlyPrevious) {
                    currentBeneficiaryMonthlyWithUpdates.remove(previousEnrollment);
                }
            } else {
                currentBeneficiaryMonthlyWithUpdates = new LinkedList<BeneficiaryMonthly>();
            }
            currentBeneficiaryMonthlyWithUpdates.addAll(currentYearBeneficiaryMonthly);
            newBeneficiaryRecord.setBeneficiaryMonthlys(currentBeneficiaryMonthlyWithUpdates);
        }
    }
}
Also used : Arrays(java.util.Arrays) Connection(java.sql.Connection) RifFileType(gov.cms.bfd.model.rif.RifFileType) CSVRecord(org.apache.commons.csv.CSVRecord) LoggerFactory(org.slf4j.LoggerFactory) LoadedBatchBuilder(gov.cms.bfd.model.rif.LoadedBatchBuilder) IdHasher(gov.cms.bfd.pipeline.sharedutils.IdHasher) BigDecimal(java.math.BigDecimal) CSVFormat(org.apache.commons.csv.CSVFormat) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CarrierClaimLine(gov.cms.bfd.model.rif.CarrierClaimLine) Map(java.util.Map) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) JoinType(javax.persistence.criteria.JoinType) BaseConnection(org.postgresql.core.BaseConnection) BeneficiaryHistory(gov.cms.bfd.model.rif.BeneficiaryHistory) PipelineApplicationState(gov.cms.bfd.pipeline.sharedutils.PipelineApplicationState) HikariProxyConnection(com.zaxxer.hikari.pool.HikariProxyConnection) Entity(javax.persistence.Entity) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) RifFileRecords(gov.cms.bfd.model.rif.RifFileRecords) LoadAction(gov.cms.bfd.pipeline.ccw.rif.load.RifRecordLoadResult.LoadAction) Instant(java.time.Instant) CarrierClaimCsvWriter(gov.cms.bfd.model.rif.CarrierClaimCsvWriter) Collectors(java.util.stream.Collectors) BeneficiaryMonthly(gov.cms.bfd.model.rif.BeneficiaryMonthly) RifFilesEvent(gov.cms.bfd.model.rif.RifFilesEvent) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) Beneficiary(gov.cms.bfd.model.rif.Beneficiary) LoadedBatch(gov.cms.bfd.model.rif.LoadedBatch) List(java.util.List) Stream(java.util.stream.Stream) SkipReasonCode(gov.cms.bfd.model.rif.SkippedRifRecord.SkipReasonCode) EntityManagerFactory(javax.persistence.EntityManagerFactory) CarrierClaim(gov.cms.bfd.model.rif.CarrierClaim) LocalDate(java.time.LocalDate) LoadedFile(gov.cms.bfd.model.rif.LoadedFile) Entry(java.util.Map.Entry) Timer(com.codahale.metrics.Timer) Optional(java.util.Optional) Gauge(com.codahale.metrics.Gauge) RifParsingUtils(gov.cms.bfd.model.rif.parse.RifParsingUtils) RifRecordEvent(gov.cms.bfd.model.rif.RifRecordEvent) CSVPrinter(org.apache.commons.csv.CSVPrinter) SkippedRifRecord(gov.cms.bfd.model.rif.SkippedRifRecord) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Session(org.hibernate.Session) BeneficiaryCsvWriter(gov.cms.bfd.model.rif.BeneficiaryCsvWriter) Beneficiary_(gov.cms.bfd.model.rif.Beneficiary_) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) Table(javax.persistence.Table) CopyManager(org.postgresql.copy.CopyManager) LinkedList(java.util.LinkedList) Root(javax.persistence.criteria.Root) Period(java.time.Period) RifFileEvent(gov.cms.bfd.model.rif.RifFileEvent) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) RecordAction(gov.cms.bfd.model.rif.RecordAction) BadCodeMonkeyException(gov.cms.bfd.sharedutils.exceptions.BadCodeMonkeyException) FileWriter(java.io.FileWriter) RifRecordBase(gov.cms.bfd.model.rif.RifRecordBase) IOException(java.io.IOException) EntityManager(javax.persistence.EntityManager) File(java.io.File) Work(org.hibernate.jdbc.Work) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) EntityTransaction(javax.persistence.EntityTransaction) FileReader(java.io.FileReader) Comparator(java.util.Comparator) ArrayList(java.util.ArrayList) BeneficiaryMonthly(gov.cms.bfd.model.rif.BeneficiaryMonthly)

Example 8 with BeneficiaryMonthly

use of gov.cms.bfd.model.rif.BeneficiaryMonthly in project beneficiary-fhir-data by CMSgov.

the class R4PatientResourceProvider method queryBeneficiaryIdsByPartDContractCodeAndYearMonth.

/**
 * @param yearMonth the {@link BeneficiaryMonthly#getYearMonth()} value to match against
 * @param contractId the {@link BeneficiaryMonthly#getPartDContractNumberId()} value to match
 *     against
 * @param paging the {@link PatientLinkBuilder} being used for paging
 * @return the {@link List} of matching {@link Beneficiary#getBeneficiaryId()} values
 */
@Trace
private List<String> queryBeneficiaryIdsByPartDContractCodeAndYearMonth(LocalDate yearMonth, String contractId, PatientLinkBuilder paging) {
    // Create the query to run.
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<String> beneIdCriteria = builder.createQuery(String.class);
    Root<BeneficiaryMonthly> beneMonthlyRoot = beneIdCriteria.from(BeneficiaryMonthly.class);
    beneIdCriteria.select(beneMonthlyRoot.get(BeneficiaryMonthly_.parentBeneficiary).get(Beneficiary_.beneficiaryId));
    List<Predicate> wherePredicates = new ArrayList<>();
    wherePredicates.add(builder.equal(beneMonthlyRoot.get(BeneficiaryMonthly_.yearMonth), yearMonth));
    wherePredicates.add(builder.equal(beneMonthlyRoot.get(BeneficiaryMonthly_.partDContractNumberId), contractId));
    if (paging.isPagingRequested() && !paging.isFirstPage()) {
        wherePredicates.add(builder.greaterThan(beneMonthlyRoot.get(BeneficiaryMonthly_.parentBeneficiary).get(Beneficiary_.beneficiaryId), paging.getCursor()));
    }
    beneIdCriteria.where(builder.and(wherePredicates.toArray(new Predicate[wherePredicates.size()])));
    beneIdCriteria.orderBy(builder.asc(beneMonthlyRoot.get(BeneficiaryMonthly_.parentBeneficiary)));
    // Run the query and return the results.
    List<String> matchingBeneIds = null;
    Long beneHistoryMatchesTimerQueryNanoSeconds = null;
    Timer.Context beneIdMatchesTimer = metricRegistry.timer(MetricRegistry.name(getClass().getSimpleName(), "query", "bene_ids_by_year_month_part_d_contract_id")).time();
    try {
        matchingBeneIds = entityManager.createQuery(beneIdCriteria).setMaxResults(paging.getQueryMaxSize()).getResultList();
        return matchingBeneIds;
    } finally {
        beneHistoryMatchesTimerQueryNanoSeconds = beneIdMatchesTimer.stop();
        TransformerUtilsV2.recordQueryInMdc("bene_ids_by_year_month_part_d_contract_id", beneHistoryMatchesTimerQueryNanoSeconds, matchingBeneIds == null ? 0 : matchingBeneIds.size());
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Timer(com.codahale.metrics.Timer) ArrayList(java.util.ArrayList) BeneficiaryMonthly(gov.cms.bfd.model.rif.BeneficiaryMonthly) Predicate(javax.persistence.criteria.Predicate) Trace(com.newrelic.api.agent.Trace)

Aggregations

BeneficiaryMonthly (gov.cms.bfd.model.rif.BeneficiaryMonthly)8 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)6 Timer (com.codahale.metrics.Timer)5 Trace (com.newrelic.api.agent.Trace)4 Beneficiary (gov.cms.bfd.model.rif.Beneficiary)3 ArrayList (java.util.ArrayList)3 Predicate (javax.persistence.criteria.Predicate)2 Test (org.junit.jupiter.api.Test)2 IGenericClient (ca.uhn.fhir.rest.client.api.IGenericClient)1 TokenClientParam (ca.uhn.fhir.rest.gclient.TokenClientParam)1 Gauge (com.codahale.metrics.Gauge)1 MetricRegistry (com.codahale.metrics.MetricRegistry)1 HikariProxyConnection (com.zaxxer.hikari.pool.HikariProxyConnection)1 BeneficiaryCsvWriter (gov.cms.bfd.model.rif.BeneficiaryCsvWriter)1 BeneficiaryHistory (gov.cms.bfd.model.rif.BeneficiaryHistory)1 Beneficiary_ (gov.cms.bfd.model.rif.Beneficiary_)1 CarrierClaim (gov.cms.bfd.model.rif.CarrierClaim)1 CarrierClaimCsvWriter (gov.cms.bfd.model.rif.CarrierClaimCsvWriter)1 CarrierClaimLine (gov.cms.bfd.model.rif.CarrierClaimLine)1 LoadedBatch (gov.cms.bfd.model.rif.LoadedBatch)1