<commit_before>package gov.va.vba.service.data;

import gov.va.vba.bcdss.models.ClaimRating;
import gov.va.vba.bcdss.models.Rating;
import gov.va.vba.bcdss.models.RatingDecisions;
import gov.va.vba.bcdss.models.VeteranClaim;
import gov.va.vba.bcdss.models.VeteranClaimRating;
import gov.va.vba.domain.Claim;
import gov.va.vba.domain.CustomBCDSSException;
import gov.va.vba.domain.ModelRatingPattern;
import gov.va.vba.domain.ServerRequestStatus;
import gov.va.vba.domain.util.ModelPatternIndex;
import gov.va.vba.persistence.common.ModelType;
import gov.va.vba.persistence.entity.DDMModelPattern;
import gov.va.vba.persistence.entity.DDMModelPatternIndex;
import gov.va.vba.persistence.entity.EditModelPatternResults;
import gov.va.vba.persistence.entity.ModelRatingResults;
import gov.va.vba.persistence.entity.ModelRatingResultsCntnt;
import gov.va.vba.persistence.entity.ModelRatingResultsCntntId;
import gov.va.vba.persistence.entity.ModelRatingResultsDiag;
import gov.va.vba.persistence.entity.ModelRatingResultsDiagId;
import gov.va.vba.persistence.entity.ModelRatingResultsStatus;
import gov.va.vba.persistence.entity.ModelRatingResultsStatusId;
import gov.va.vba.persistence.entity.RatingDecision;
import gov.va.vba.persistence.entity.Veteran;
import gov.va.vba.persistence.entity.BulkProcessRequest;
import gov.va.vba.persistence.models.data.ClaimDetails;
import gov.va.vba.persistence.models.data.ContentionDetails;
import gov.va.vba.persistence.models.data.DecisionDetails;
import gov.va.vba.persistence.models.data.DiagnosisCount;
import gov.va.vba.persistence.repository.ClaimRepository;
import gov.va.vba.persistence.repository.DDMContentionRepository;
import gov.va.vba.persistence.repository.EarDao;
import gov.va.vba.persistence.repository.ModelRatingResultsCntnRepository;
import gov.va.vba.persistence.repository.ModelRatingResultsDiagRepository;
import gov.va.vba.persistence.repository.ModelRatingResultsRepository;
import gov.va.vba.persistence.repository.ModelRatingResultsStatusRepository;
import gov.va.vba.persistence.repository.RatingDao;
import gov.va.vba.persistence.repository.RatingDecisionRepository;
import gov.va.vba.persistence.repository.VeteranRepository;
import gov.va.vba.service.AppUtill;
import gov.va.vba.service.EarService;
import gov.va.vba.service.KneeService;
import gov.va.vba.service.orika.ClaimMapper;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.maven.wagon.authentication.AuthenticationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ClaimDataService extends AbsDataService<gov.va.vba.persistence.entity.Claim, Claim> {

    private static final Logger LOG = LoggerFactory.getLogger(ClaimDataService.class);
    private static final List<Long> KNEE_CONTENTION_IDS = Arrays.asList(230L, 270L, 3690L, 3700L, 3710L, 8919L, 3720L, 3730L, 3780L, 3790L, 3800L);
    private static final List<Long> EAR_CONTENTION_IDS = Arrays.asList(2200L, 2210L, 3140L, 3150L, 4130L, 4210L, 4700L, 4920L, 5000L, 5010L, 5710L, 6850L);
    private static final List<String> KNEE_DIAGNOSIS_CODES = Arrays.asList("5055", "5161", "5162", "5163", "5164", "5165", "5256", "5257", "5258", "5259", "5260", "5261", "5262", "5263", "5264", "5313", "5314", "5315");
    private static final List<String> EAR_DIAGNOSIS_CODES = Arrays.asList("6100", "6200", "6210", "6202", "6204", "6205", "6207", "6209", "6201", "6211", "6260");

    @Autowired
    private ClaimRepository claimRepository;
    
    @Autowired
    private RatingDecisionRepository ratingDecisionRepository;

    @Autowired
    private ClaimMapper claimMapper;

    @Autowired
    private ModelRatingResultsRepository modelRatingResultsRepository;

    @Autowired
    private ModelRatingResultsCntnRepository modelRatingResultsCntnRepository;

    @Autowired
    private ModelRatingResultsDiagRepository modelRatingResultsDiagRepository;

    @Autowired
    private ModelRatingResultsStatusRepository modelRatingResultsStatusRepository;

    @Autowired
    private EarService earService;

    @Autowired
    private KneeService kneeService;

    @Autowired
    private RatingDao ratingDao;

    @Autowired
    private EarDao earDao;

    @Autowired
    private DDMDataService ddmDataService;

    @Autowired
    private DDMModelCntntService ddmModelCntntService;

    @Autowired
    private DDMModelDiagService ddmModelDiagService;

    public ClaimDataService() {
        this.setClasses(gov.va.vba.persistence.entity.Claim.class, Claim.class);
    }

    public List<Claim> findAll() {
        List<Claim> output = new ArrayList<>();
        List<gov.va.vba.persistence.entity.Claim> input = ((ClaimRepository) repository).findAll();
        mapper.mapAsCollection(input, output, outputClass);
        return output;
    }

    public List<Claim> findFirstNumberedRow() {
        List<ClaimDetails> claims = ratingDao.getClaims();
        LOG.info("SIZE of input :::: " + claims.size());
        return mapKneeClaimsToClaims(claims);
    }

    public List<VeteranClaimRating> findByVeteranId(List<VeteranClaim> veteranClaims) throws CustomBCDSSException{
        List<VeteranClaimRating> veteranClaimRatings = new ArrayList<>();
        String currentLogin = null;
        try{
        for (VeteranClaim vc : veteranClaims) {

            VeteranClaimRating veteranClaimRating = new VeteranClaimRating();
            veteranClaimRating.setVeteran(vc.getVeteran());
            int veteranId = vc.getVeteran().getVeteranId();
            if(null!=vc.getUserId()){
            	currentLogin = vc.getUserId();
            }
            List<gov.va.vba.bcdss.models.Claim> claims = vc.getClaim();
            List<ClaimRating> claimRatings = new ArrayList<>();
            for (gov.va.vba.bcdss.models.Claim c : claims) {
                int claimId = c.getClaimId();
                LOG.info("CONTENTION CODE IS ::: " + c.getContentionId());
                if(c.getContentionId() == 0) {
                    continue;
                }
                ContentionDetails contentionDetails = ratingDao.getContention(NumberUtils.toInt(c.getContentionClassificationId()));
                String modelType = contentionDetails.getModelType();
                ModelRatingResults results = null;
                List<ClaimDetails> claimDetails = new ArrayList<>();
                if (modelType.equalsIgnoreCase(ModelType.EAR.name())) {
                    claimDetails = earDao.getClaims(veteranId, claimId);
                    results = earService.processClaims(veteranId, claimDetails, currentLogin);
                } else if (modelType.equalsIgnoreCase(ModelType.KNEE.name())) {
                    claimDetails = ratingDao.getPreviousClaims(veteranId, claimId);
                    results = kneeService.processClaims(veteranId, claimDetails, currentLogin);
                }

                if (results != null) {
                    //Added code for Process Id Sequence generation
                    List<Long> maxprocessId = ratingDao.getProcessIDSeq();
                    LOG.debug("******************************");
                    long processIdSeq = maxprocessId.get(0);
                    LOG.info("MAX ProcessId -------- " + processIdSeq);

                    Set<Long> claimsList = new HashSet<>();
                    Map<Long, Integer> contentionCounts = new HashMap<>();
                    for (ClaimDetails details : claimDetails) {
                        claimsList.add(details.getClaimId());
                        if (contentionCounts.containsKey(details.getContentionClassificationId())) {
                            int value = contentionCounts.get(details.getContentionClassificationId());
                            contentionCounts.put(details.getContentionClassificationId(), value + 1);
                        } else {
                            contentionCounts.put(details.getContentionClassificationId(), 1);
                        }
                    }
                    LOG.info("Contention count :::::: " + contentionCounts);
                    

                    //setting ProcessId as Max Sequence generated+1
                    results.setProcessId(processIdSeq + 1);
                    results.setClaimCount((long) claimsList.size());
                    results.setContentionCount((long) contentionCounts.keySet().size());

                    ModelRatingResults savedResults = modelRatingResultsRepository.save(results);
                    BigDecimal processId = BigDecimal.valueOf(savedResults.getProcessId());

                    LOG.info("=================================================================");
                    LOG.info("RESULTS :::::::::::::::: " + savedResults);
                    LOG.info("=================================================================");
                    List<DiagnosisCount> diagnosisCount;
                    List<String> diagnosisCodes;
                    List<Long> contentions;
                    if(modelType.equalsIgnoreCase("EAR")) {
                        diagnosisCodes = EAR_DIAGNOSIS_CODES;
                        contentions = EAR_CONTENTION_IDS;
                        diagnosisCount = ratingDao.getEarDiagnosisCount((long) veteranId, savedResults.getClaimDate());
                    } else {
                        diagnosisCodes = KNEE_DIAGNOSIS_CODES;
                        contentions = KNEE_CONTENTION_IDS;
                        diagnosisCount = ratingDao.getDiagnosisCount((long) veteranId, savedResults.getClaimDate());
                    }
                    LOG.info("Diagnosis count :::::: " + diagnosisCount);
                    
                    saveModelResultsCtnts(contentionCounts, savedResults, contentions);
                    saveModelResultsDiag(savedResults, diagnosisCount, diagnosisCodes);
                    saveResultStatus(results, currentLogin);
                    //ddmModelCntntService.getPatternId(results.getModelType(), )

                    /*EarDecision ed = new EarDecision();
                    KneeDecision kd = new KneeDecision();*/
                    RatingDecisions ratingDecisions = new RatingDecisions();
                    ratingDecisions.setProcessId(processId.intValue());
                   /* ratingDecisions.setEarRatings(ed);
                    ratingDecisions.setKneeRatings(kd);*/
                    gov.va.vba.bcdss.models.Rating rating = new Rating();
                    rating.setCddAge(savedResults.getClaimantAge().intValue());
                    rating.setClaimAge(savedResults.getClaimantAge().intValue());
                    rating.setClaimantAge(savedResults.getClaimantAge().intValue());
                    rating.setClaimCount(savedResults.getClaimCount().intValue());
                    //rating.setContationCount(contentionsCount);
                    rating.setCurrentCdd(savedResults.getCurrentCDD().intValue());
                    rating.setRaterEvaluation(savedResults.getCurrentCDD().intValue());
                    rating.setPriorCdd(results.getPriorCDD().intValue());
                    rating.setProcessId(processId.intValue());
                    //rating.setQuantCdd(calculatedCdd.intValue());
                    rating.setRatingDecisions(ratingDecisions);
                    rating.setModelType(savedResults.getModelType());
                    
                    rating.setProcessDate(savedResults.getClaimDate());
                    c.setClaimDate(savedResults.getClaimDate());
                        /*c.setProfileDate(claim.getProfileDate());*/
                    //c.setContentionClassificationId(String.valueOf(kneeClaim.getContentionClsfcnId()));
                    //c.setContentionId(Integer.parseInt(kneeClaim.getContentionId()));
                    //c.setProductTypeCode(kneeClaim.getEndPrdctTypeCode());
                    // c.setContentionBeginDate(kneeClaim.getContentionBeginDate());
                    ClaimRating cr = new ClaimRating();
                    cr.setClaim(c);
                    cr.setRating(rating);
                    claimRatings.add(cr);
                    
                    //TODO: last param
                    List<DDMModelPattern> patterns = ddmDataService.getPatternId(results.getModelType(), results.getClaimantAge(), results.getClaimCount(), (long) contentionCounts.keySet().size(), 0L);
                    List<Long> diagPattren = new ArrayList<>();
                    List<Long> cntntPattrens = new ArrayList<>();
                    if (CollectionUtils.isNotEmpty(patterns)) {
                        List<Long> patternsList = patterns.stream().map(DDMModelPattern::getPatternId).collect(Collectors.toList());
                        if(MapUtils.isNotEmpty(contentionCounts)){
                        	 LOG.info("Contention count :::::: " + contentionCounts);
                        	cntntPattrens = ddmModelCntntService.getKneePatternId(contentionCounts, patternsList, modelType.toUpperCase());
                        }else{
                        	throw new CustomBCDSSException("No Valid Contention Codes exist for the selected data ");
                        }
                        if (CollectionUtils.isNotEmpty(cntntPattrens)) {
                            //List<Long> diagPatternsList = patterns.stream().map(DDMModelPattern::getPatternId).collect(Collectors.toList());
                        	if (CollectionUtils.isNotEmpty(diagnosisCount)) {
                        		LOG.info("Diagnosis count :::::: " + diagnosisCount);
                        		diagPattren = ddmModelDiagService.getKneePatternId(diagnosisCount, cntntPattrens, modelType.toUpperCase());
                        	}else{
                        		throw new CustomBCDSSException("No Diagnosis Codes exist for the selected data ");
                        	}
                            LOG.info("PATTREN SIZE :::::: " + diagPattren);
                            if (CollectionUtils.isNotEmpty(diagPattren)) {
                                Long pattrenId = diagPattren.get(0);
                                results.setPatternId(pattrenId);
                                LOG.info("PATTREN ID :: " + pattrenId);
                                results = modelRatingResultsRepository.save(results);
                            }else{
                            	throw new CustomBCDSSException("No Valid Diagnosis patterns found for the pattern ID ");
                            }
                        }else{
                        	throw new CustomBCDSSException("No Valid Contention patterns found for the pattern ID ");
                        }
                    }else{
                    	throw new CustomBCDSSException("No valid Pattern found for the data selected");
                    }
                   
                    if (results.getPatternId() != null) {
                        List<DDMModelPatternIndex> accuracy = ratingDao.getPatternAccuracy(results.getPatternId());
                        if (CollectionUtils.isNotEmpty(accuracy)) {
                            DDMModelPatternIndex ddmModelPatternIndex = accuracy.get(0);
                            LOG.info("PATTREN INDEX :: PATTERN {} AND ACCURACY {}", ddmModelPatternIndex.getPatternId(), ddmModelPatternIndex.getAccuracy());
                            if (ddmModelPatternIndex.getAccuracy() != null) {
                                rating.setAccuracy(ddmModelPatternIndex.getAccuracy().intValue());
                            }
                            if (ddmModelPatternIndex.getPatternIndexNumber() != null) {
                                rating.setRateOfUse(ddmModelPatternIndex.getPatternIndexNumber().intValue());
                            }
                            if (ddmModelPatternIndex.getCDD() != null) {
                                rating.setQuantCdd(ddmModelPatternIndex.getCDD().intValue());
                            }
                        }
                        if(results.getPatternId() != null) {
                            rating.setPatternId(results.getPatternId().intValue());
                        }

                    }else{
                    	throw new CustomBCDSSException("No valid Pattern found for the data selected");
                    }

                }else{
                	throw new CustomBCDSSException("No Model Rating Results found for the data selected");
                }
            }
            veteranClaimRating.getClaimRating().addAll(claimRatings);
            veteranClaimRatings.add(veteranClaimRating);<commit_msg>Making process_claims to continue without populating the error message or exception when pattern_id is null<commit_after>package gov.va.vba.service.data;

import gov.va.vba.bcdss.models.ClaimRating;
import gov.va.vba.bcdss.models.Rating;
import gov.va.vba.bcdss.models.RatingDecisions;
import gov.va.vba.bcdss.models.VeteranClaim;
import gov.va.vba.bcdss.models.VeteranClaimRating;
import gov.va.vba.domain.Claim;
import gov.va.vba.domain.CustomBCDSSException;
import gov.va.vba.domain.ModelRatingPattern;
import gov.va.vba.domain.ServerRequestStatus;
import gov.va.vba.domain.util.ModelPatternIndex;
import gov.va.vba.persistence.common.ModelType;
import gov.va.vba.persistence.entity.DDMModelPattern;
import gov.va.vba.persistence.entity.DDMModelPatternIndex;
import gov.va.vba.persistence.entity.EditModelPatternResults;
import gov.va.vba.persistence.entity.ModelRatingResults;
import gov.va.vba.persistence.entity.ModelRatingResultsCntnt;
import gov.va.vba.persistence.entity.ModelRatingResultsCntntId;
import gov.va.vba.persistence.entity.ModelRatingResultsDiag;
import gov.va.vba.persistence.entity.ModelRatingResultsDiagId;
import gov.va.vba.persistence.entity.ModelRatingResultsStatus;
import gov.va.vba.persistence.entity.ModelRatingResultsStatusId;
import gov.va.vba.persistence.entity.RatingDecision;
import gov.va.vba.persistence.entity.Veteran;
import gov.va.vba.persistence.entity.BulkProcessRequest;
import gov.va.vba.persistence.models.data.ClaimDetails;
import gov.va.vba.persistence.models.data.ContentionDetails;
import gov.va.vba.persistence.models.data.DecisionDetails;
import gov.va.vba.persistence.models.data.DiagnosisCount;
import gov.va.vba.persistence.repository.ClaimRepository;
import gov.va.vba.persistence.repository.DDMContentionRepository;
import gov.va.vba.persistence.repository.EarDao;
import gov.va.vba.persistence.repository.ModelRatingResultsCntnRepository;
import gov.va.vba.persistence.repository.ModelRatingResultsDiagRepository;
import gov.va.vba.persistence.repository.ModelRatingResultsRepository;
import gov.va.vba.persistence.repository.ModelRatingResultsStatusRepository;
import gov.va.vba.persistence.repository.RatingDao;
import gov.va.vba.persistence.repository.RatingDecisionRepository;
import gov.va.vba.persistence.repository.VeteranRepository;
import gov.va.vba.service.AppUtill;
import gov.va.vba.service.EarService;
import gov.va.vba.service.KneeService;
import gov.va.vba.service.orika.ClaimMapper;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.maven.wagon.authentication.AuthenticationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ClaimDataService extends AbsDataService<gov.va.vba.persistence.entity.Claim, Claim> {

    private static final Logger LOG = LoggerFactory.getLogger(ClaimDataService.class);
    private static final List<Long> KNEE_CONTENTION_IDS = Arrays.asList(230L, 270L, 3690L, 3700L, 3710L, 8919L, 3720L, 3730L, 3780L, 3790L, 3800L);
    private static final List<Long> EAR_CONTENTION_IDS = Arrays.asList(2200L, 2210L, 3140L, 3150L, 4130L, 4210L, 4700L, 4920L, 5000L, 5010L, 5710L, 6850L);
    private static final List<String> KNEE_DIAGNOSIS_CODES = Arrays.asList("5055", "5161", "5162", "5163", "5164", "5165", "5256", "5257", "5258", "5259", "5260", "5261", "5262", "5263", "5264", "5313", "5314", "5315");
    private static final List<String> EAR_DIAGNOSIS_CODES = Arrays.asList("6100", "6200", "6210", "6202", "6204", "6205", "6207", "6209", "6201", "6211", "6260");

    @Autowired
    private ClaimRepository claimRepository;
    
    @Autowired
    private RatingDecisionRepository ratingDecisionRepository;

    @Autowired
    private ClaimMapper claimMapper;

    @Autowired
    private ModelRatingResultsRepository modelRatingResultsRepository;

    @Autowired
    private ModelRatingResultsCntnRepository modelRatingResultsCntnRepository;

    @Autowired
    private ModelRatingResultsDiagRepository modelRatingResultsDiagRepository;

    @Autowired
    private ModelRatingResultsStatusRepository modelRatingResultsStatusRepository;

    @Autowired
    private EarService earService;

    @Autowired
    private KneeService kneeService;

    @Autowired
    private RatingDao ratingDao;

    @Autowired
    private EarDao earDao;

    @Autowired
    private DDMDataService ddmDataService;

    @Autowired
    private DDMModelCntntService ddmModelCntntService;

    @Autowired
    private DDMModelDiagService ddmModelDiagService;

    public ClaimDataService() {
        this.setClasses(gov.va.vba.persistence.entity.Claim.class, Claim.class);
    }

    public List<Claim> findAll() {
        List<Claim> output = new ArrayList<>();
        List<gov.va.vba.persistence.entity.Claim> input = ((ClaimRepository) repository).findAll();
        mapper.mapAsCollection(input, output, outputClass);
        return output;
    }

    public List<Claim> findFirstNumberedRow() {
        List<ClaimDetails> claims = ratingDao.getClaims();
        LOG.info("SIZE of input :::: " + claims.size());
        return mapKneeClaimsToClaims(claims);
    }

    public List<VeteranClaimRating> findByVeteranId(List<VeteranClaim> veteranClaims) throws CustomBCDSSException{
        List<VeteranClaimRating> veteranClaimRatings = new ArrayList<>();
        String currentLogin = null;
        try{
        for (VeteranClaim vc : veteranClaims) {

            VeteranClaimRating veteranClaimRating = new VeteranClaimRating();
            veteranClaimRating.setVeteran(vc.getVeteran());
            int veteranId = vc.getVeteran().getVeteranId();
            if(null!=vc.getUserId()){
            	currentLogin = vc.getUserId();
            }
            List<gov.va.vba.bcdss.models.Claim> claims = vc.getClaim();
            List<ClaimRating> claimRatings = new ArrayList<>();
            for (gov.va.vba.bcdss.models.Claim c : claims) {
                int claimId = c.getClaimId();
                LOG.info("CONTENTION CODE IS ::: " + c.getContentionId());
                if(c.getContentionId() == 0) {
                    continue;
                }
                ContentionDetails contentionDetails = ratingDao.getContention(NumberUtils.toInt(c.getContentionClassificationId()));
                String modelType = contentionDetails.getModelType();
                ModelRatingResults results = null;
                List<ClaimDetails> claimDetails = new ArrayList<>();
                if (modelType.equalsIgnoreCase(ModelType.EAR.name())) {
                    claimDetails = earDao.getClaims(veteranId, claimId);
                    results = earService.processClaims(veteranId, claimDetails, currentLogin);
                } else if (modelType.equalsIgnoreCase(ModelType.KNEE.name())) {
                    claimDetails = ratingDao.getPreviousClaims(veteranId, claimId);
                    results = kneeService.processClaims(veteranId, claimDetails, currentLogin);
                }

                if (results != null) {
                    //Added code for Process Id Sequence generation
                    List<Long> maxprocessId = ratingDao.getProcessIDSeq();
                    LOG.debug("******************************");
                    long processIdSeq = maxprocessId.get(0);
                    LOG.info("MAX ProcessId -------- " + processIdSeq);

                    Set<Long> claimsList = new HashSet<>();
                    Map<Long, Integer> contentionCounts = new HashMap<>();
                    for (ClaimDetails details : claimDetails) {
                        claimsList.add(details.getClaimId());
                        if (contentionCounts.containsKey(details.getContentionClassificationId())) {
                            int value = contentionCounts.get(details.getContentionClassificationId());
                            contentionCounts.put(details.getContentionClassificationId(), value + 1);
                        } else {
                            contentionCounts.put(details.getContentionClassificationId(), 1);
                        }
                    }
                    LOG.info("Contention count :::::: " + contentionCounts);
                    

                    //setting ProcessId as Max Sequence generated+1
                    results.setProcessId(processIdSeq + 1);
                    results.setClaimCount((long) claimsList.size());
                    results.setContentionCount((long) contentionCounts.keySet().size());

                    ModelRatingResults savedResults = modelRatingResultsRepository.save(results);
                    BigDecimal processId = BigDecimal.valueOf(savedResults.getProcessId());

                    LOG.info("=================================================================");
                    LOG.info("RESULTS :::::::::::::::: " + savedResults);
                    LOG.info("=================================================================");
                    List<DiagnosisCount> diagnosisCount;
                    List<String> diagnosisCodes;
                    List<Long> contentions;
                    if(modelType.equalsIgnoreCase("EAR")) {
                        diagnosisCodes = EAR_DIAGNOSIS_CODES;
                        contentions = EAR_CONTENTION_IDS;
                        diagnosisCount = ratingDao.getEarDiagnosisCount((long) veteranId, savedResults.getClaimDate());
                    } else {
                        diagnosisCodes = KNEE_DIAGNOSIS_CODES;
                        contentions = KNEE_CONTENTION_IDS;
                        diagnosisCount = ratingDao.getDiagnosisCount((long) veteranId, savedResults.getClaimDate());
                    }
                    LOG.info("Diagnosis count :::::: " + diagnosisCount);
                    
                    saveModelResultsCtnts(contentionCounts, savedResults, contentions);
                    saveModelResultsDiag(savedResults, diagnosisCount, diagnosisCodes);
                    saveResultStatus(results, currentLogin);
                    //ddmModelCntntService.getPatternId(results.getModelType(), )

                    /*EarDecision ed = new EarDecision();
                    KneeDecision kd = new KneeDecision();*/
                    RatingDecisions ratingDecisions = new RatingDecisions();
                    ratingDecisions.setProcessId(processId.intValue());
                   /* ratingDecisions.setEarRatings(ed);
                    ratingDecisions.setKneeRatings(kd);*/
                    gov.va.vba.bcdss.models.Rating rating = new Rating();
                    rating.setCddAge(savedResults.getClaimantAge().intValue());
                    rating.setClaimAge(savedResults.getClaimantAge().intValue());
                    rating.setClaimantAge(savedResults.getClaimantAge().intValue());
                    rating.setClaimCount(savedResults.getClaimCount().intValue());
                    //rating.setContationCount(contentionsCount);
                    rating.setCurrentCdd(savedResults.getCurrentCDD().intValue());
                    rating.setRaterEvaluation(savedResults.getCurrentCDD().intValue());
                    rating.setPriorCdd(results.getPriorCDD().intValue());
                    rating.setProcessId(processId.intValue());
                    //rating.setQuantCdd(calculatedCdd.intValue());
                    rating.setRatingDecisions(ratingDecisions);
                    rating.setModelType(savedResults.getModelType());
                    
                    rating.setProcessDate(savedResults.getClaimDate());
                    c.setClaimDate(savedResults.getClaimDate());
                        /*c.setProfileDate(claim.getProfileDate());*/
                    //c.setContentionClassificationId(String.valueOf(kneeClaim.getContentionClsfcnId()));
                    //c.setContentionId(Integer.parseInt(kneeClaim.getContentionId()));
                    //c.setProductTypeCode(kneeClaim.getEndPrdctTypeCode());
                    // c.setContentionBeginDate(kneeClaim.getContentionBeginDate());
                    ClaimRating cr = new ClaimRating();
                    cr.setClaim(c);
                    cr.setRating(rating);
                    claimRatings.add(cr);
                    
                    //TODO: last param
                    List<DDMModelPattern> patterns = ddmDataService.getPatternId(results.getModelType(), results.getClaimantAge(), results.getClaimCount(), (long) contentionCounts.keySet().size(), 0L);
                    List<Long> diagPattren = new ArrayList<>();
                    List<Long> cntntPattrens = new ArrayList<>();
                    if (CollectionUtils.isNotEmpty(patterns)) {
                        List<Long> patternsList = patterns.stream().map(DDMModelPattern::getPatternId).collect(Collectors.toList());
                        if(MapUtils.isNotEmpty(contentionCounts)){
                        	 LOG.info("Contention count :::::: " + contentionCounts);
                        	cntntPattrens = ddmModelCntntService.getKneePatternId(contentionCounts, patternsList, modelType.toUpperCase());
                        }else{
                        	throw new CustomBCDSSException("No Valid Contention Codes exist for the selected data ");
                        }
                        if (CollectionUtils.isNotEmpty(cntntPattrens)) {
                            //List<Long> diagPatternsList = patterns.stream().map(DDMModelPattern::getPatternId).collect(Collectors.toList());
                        	if (CollectionUtils.isNotEmpty(diagnosisCount)) {
                        		LOG.info("Diagnosis count :::::: " + diagnosisCount);
                        		diagPattren = ddmModelDiagService.getKneePatternId(diagnosisCount, cntntPattrens, modelType.toUpperCase());
                        	}else{
                        		throw new CustomBCDSSException("No Diagnosis Codes exist for the selected data ");
                        	}
                            LOG.info("PATTREN SIZE :::::: " + diagPattren);
                            if (CollectionUtils.isNotEmpty(diagPattren)) {
                                Long pattrenId = diagPattren.get(0);
                                results.setPatternId(pattrenId);
                                LOG.info("PATTREN ID :: " + pattrenId);
                                results = modelRatingResultsRepository.save(results);
                            }else{
                            	throw new CustomBCDSSException("No Valid Diagnosis patterns found for the pattern ID ");
                            }
                        }else{
                        	throw new CustomBCDSSException("No Valid Contention patterns found for the pattern ID ");
                        }
                    }else{
                    	throw new CustomBCDSSException("No valid Pattern found for the data selected");
                    }
                   
                    if (results.getPatternId() != null) {
                        List<DDMModelPatternIndex> accuracy = ratingDao.getPatternAccuracy(results.getPatternId());
                        if (CollectionUtils.isNotEmpty(accuracy)) {
                            DDMModelPatternIndex ddmModelPatternIndex = accuracy.get(0);
                            LOG.info("PATTREN INDEX :: PATTERN {} AND ACCURACY {}", ddmModelPatternIndex.getPatternId(), ddmModelPatternIndex.getAccuracy());
                            if (ddmModelPatternIndex.getAccuracy() != null) {
                                rating.setAccuracy(ddmModelPatternIndex.getAccuracy().intValue());
                            }
                            if (ddmModelPatternIndex.getPatternIndexNumber() != null) {
                                rating.setRateOfUse(ddmModelPatternIndex.getPatternIndexNumber().intValue());
                            }
                            if (ddmModelPatternIndex.getCDD() != null) {
                                rating.setQuantCdd(ddmModelPatternIndex.getCDD().intValue());
                            }
                        }
                            rating.setPatternId(results.getPatternId().intValue());

                    }

                }else{
                	throw new CustomBCDSSException("No Model Rating Results found for the data selected");
                }
            }
            veteranClaimRating.getClaimRating().addAll(claimRatings);
            veteranClaimRatings.add(veteranClaimRating);
{'fraction_non_alphanumeric': 0.10778338339505993, 'fraction_numerical': 0.012656382141086582, 'mean_word_length': 2.305890292104502, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4825815', 'n_tokens_mistral': 9680, 'n_tokens_neox': 9011, 'n_words': 1428}