Untitled

 avatar
unknown
plain_text
9 days ago
20 kB
2
Indexable
package com.fca.miniro.service;

import com.fca.miniro.bean.*;
import com.fca.miniro.dao.MiniRoDao;
import com.fca.miniro.request.RequestBean;
import com.fca.miniro.request.Services;
import com.fca.miniro.utill.ApiConstant;
import com.fca.miniro.utill.SQLQueryConstant;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


@Service
public class MiniROService {
    private static final Logger logger = LogManager.getLogger(MiniROService.class);

    public Map<String, List<?>> mapMiniROData(List<RequestBean> requestBeanList) throws Exception {
        logger.info("Inside MiniRoService class miniRoDataMethod method request bean : " + requestBeanList.toString());

        Map<String, List<?>> response = new HashMap<>();
        List<String> receiptHandle = new ArrayList<>();
        List<NarrativeRo> narrativeRoList = new ArrayList<>();
        List<ConditionRO> conditionROGlobalInsertList = new ArrayList<>();
        List<ConditionRO> conditionROInsertList = new ArrayList<>();
        List<ConditionRO> conditionUpdateROList = new ArrayList<>();
        List<ConditionRO> conditionGlobalUpdateROList = new ArrayList<>();
        List<ClaimRo> claimRoInsertList = new ArrayList<>();
        List<ClaimRo> claimRoUpdateList = new ArrayList<>();
        List<VehicleRo> vehicleRoInsertList = new ArrayList<>();
        List<VehicleRo> vehicleRoUpdateList = new ArrayList<>();
        MiniRoDao miniRoDao = new MiniRoDao();
        List<FailedDataBean> filedList = new ArrayList<>();
        Map<String, RequestBean> requestBeanMap = new HashMap<>();
        for (RequestBean requestBean : requestBeanList) {
            requestBeanMap.put(requestBean.getVehicleInfo().getVIN(), requestBean);
        }
        requestBeanList = new ArrayList<>(requestBeanMap.values());
        for (RequestBean requestBean : requestBeanList) {
            ClaimRo claimRo = new ClaimRo();
            VehicleRo vehicleRo = new VehicleRo();
            narrativeRoList.clear();
            try {
               
                String vin = requestBean.getVehicleInfo().getVIN();
                String vin9 = vin.substring(0, 9);
                String vin8 = vin.substring(9);
                String vhclsan;
                String vhcPartiIn;
                Map<String, String> vinDetails = null;
                vinDetails = getvhclsan(miniRoDao, vin, vin9, vin8);
                if (vinDetails == null || vinDetails.isEmpty()) {
                    String vinNiv = miniRoDao.getVinNIV(vin9, vin8, SQLQueryConstant.GET_VIN_VINNIV);
                    if (vinNiv != null) {
                        vinDetails = getvhclsan(miniRoDao, vin, vin9, vin8);
                    }
                }

             if (vinDetails == null || vinDetails.isEmpty()) {
                    throw new Exception("VIN is not present in Data base");
                }
                vhclsan = checkNull(vinDetails.get(ApiConstant.VHCL_SAN), ApiConstant.VHCL_SAN);
                vhcPartiIn = checkNull(vinDetails.get(ApiConstant.PRTITN), ApiConstant.PRTITN);
                
                claimRo.setDealerCode(checkNull(checkLength(requestBean.getRoGeneralInfo().getDealerCode(), 5), "DEALER CODE"));
                claimRo.setRoNumber(checkNull(checkLength(requestBean.getRoGeneralInfo().getRONumber(), 50), "RO NUMBER"));

                claimRo.setVhcPrtiIn(vhcPartiIn);
                claimRo.setVhclSan(vhclsan);
                claimRo.setVin(checkNull((requestBean.getVehicleInfo().getVIN()), "VIN"));
                claimRo.setRoStatus(checkNull(checkLength(requestBean.getRoGeneralInfo().getROStatus(), 20), "RO STATE"));
                claimRo.setMileageUnits(checkNullToBlank(checkLength(requestBean.getRoGeneralInfo().getMileageUnits(), 2)));
                claimRo.setOdometerIn(checkNull(requestBean.getRoGeneralInfo().getOdometerIn()));
                claimRo.setDMSSource(checkNull(checkLength(requestBean.getRoGeneralInfo().getDmsSource(), 100), "DMS SOURCE"));
                claimRo.setRepeatRepairFlag(checkNull(checkLength(requestBean.getRoGeneralInfo().getRepeatRepairFlag(), 1), "REPEAT REPAIR"));
                claimRo.setRoTransitionType(checkNull(checkLength(requestBean.getROTransactionType(), 10), "RO TRANSITION TYPE"));
                claimRo.setRoOpenDate(checkNull(dateCheck(requestBean.getRoGeneralInfo().getRoOpenDate()), "RO OPEN DATE"));
                claimRo.setVehicleInwardDate(changeDateDefulte(dateCheck(requestBean.getRoGeneralInfo().getVehicleInwardDate())));
                claimRo.setCustomerId(checkNull(checkLength(requestBean.getCustomerInfo().getCustomerID(), 50), "CUSTOMER ID"));
                claimRo.setCustomerState(checkNull(checkLength(requestBean.getCustomerInfo().getCustomerStateProvince(), 20), "CUSTOMER STATE"));

                vehicleRo.setVin(checkNull(requestBean.getVehicleInfo().getVIN(), "VIN"));
                vehicleRo.setVhclSan(vhclsan);
                vehicleRo.setVhcPrtiIn(vhcPartiIn);
                vehicleRo.setMake(checkNull(requestBean.getVehicleInfo().getMake(), "MAKE"));
                vehicleRo.setTransmissionType(checkNullToBlank(checkLength(requestBean.getVehicleInfo().getTransmissiontype(), 1)));
                vehicleRo.setFuelType(checkNullToBlank(requestBean.getVehicleInfo().getFuelType()));
                vehicleRo.setModelYear(checkNull(requestBean.getVehicleInfo().getModelYear(), "MODEL YEAR"));

                if (requestBean.getROTransactionType().equalsIgnoreCase(ApiConstant.CLOSE)) {
                    claimRo.setPreviousRoNumber(checkNull(checkLength(requestBean.getRoGeneralInfo().getPreviousROnumber(), 50), "PREVIOUS RO NUMBER"));
                    claimRo.setOdometerOut(checkNull(requestBean.getRoGeneralInfo().getOdometerOut()));
                    claimRo.setRoCloseDate(checkNull(dateCheck(requestBean.getRoGeneralInfo().getROCloseDate()), "RO CLOSE DATE"));
                } else {
                    claimRo.setPreviousRoNumber(checkNullToBlank(checkLength(requestBean.getRoGeneralInfo().getPreviousROnumber(), 50)));
                    claimRo.setOdometerOut(requestBean.getRoGeneralInfo().getOdometerOut());
                    claimRo.setRoCloseDate(changeDateDefulte(dateCheck(requestBean.getRoGeneralInfo().getROCloseDate())));
                }
                List<String> existingService = new ArrayList<>();
                for (Services services : requestBean.getServiceInfo().getServices()) {
                    ConditionRO conditionRO = new ConditionRO();
                    List<NarrativeRo> narrativeRoListMul = new ArrayList<>();
                    if (existingService.contains(services.getServiceId())) {
                        throw new Exception("Duplicate Ro line Item number");
                    }
                    existingService.add(services.getServiceId());
                    try {
                        if (requestBean.getROTransactionType().equalsIgnoreCase(ApiConstant.CLOSE) && services.getComebackLineFlag().equalsIgnoreCase(ApiConstant.Y)) {
                            conditionRO.setComebackRoNumber(checkNull(checkLength(services.getComebackROnumber(), 50), "COMEBACK RO NUMBER"));
                        } else {
                            conditionRO.setComebackRoNumber(checkNullToBlank(checkLength(services.getComebackROnumber(), 50)));
                        }
                        if (requestBean.getROTransactionType().equalsIgnoreCase(ApiConstant.OPEN)) {
                            checkNull(services.getCauseNarrative(), "CAUSE NARRATIVE");
                            checkNull(services.getCorrectionNarrative(), "CORRECT NARRATIVE");
                        }
                        checkNull(services.getCustomerComplain(), "Customer Complain");
                        int corNarSize = services.getCorrectionNarrative().length();
                        int causNarSize = services.getCauseNarrative().length();
                        int cusComplain = services.getCustomerComplain().length();

                        corNarSize = (corNarSize / 1000 > 0 ? (corNarSize / 1000) + (corNarSize % 1000 > 0 ? 1 : 0) : 1);
                        causNarSize = (causNarSize / 1000 > 0 ? (causNarSize / 1000) + (causNarSize % 1000 > 0 ? 1 : 0) : 1);
                        cusComplain = (cusComplain / 1000 > 0 ? (cusComplain / 1000) + (cusComplain % 1000 > 0 ? 1 : 0) : 1);
                        int narSize = Math.max(Math.max(cusComplain, corNarSize), causNarSize);

                        for (int i = 1; i <= narSize; i++) {
                            NarrativeRo narrativeRo = new NarrativeRo();

                            narrativeRo = extractingSequence(vinDetails, requestBean, narrativeRo, services, corNarSize, causNarSize, cusComplain, i);
                            narrativeRoListMul.add(narrativeRo);
                        }
                        conditionRO.setVin(checkNull(requestBean.getVehicleInfo().getVIN(), "VIN"));
                        conditionRO.setVhclSan(vhclsan);
                        conditionRO.setVhcPrtiIn(vhcPartiIn);
                        conditionRO.setDealerCode(checkNull(checkLength(requestBean.getRoGeneralInfo().getDealerCode(), 5), "DEALER CODE"));
                        conditionRO.setRoNumber(checkNull(checkLength(requestBean.getRoGeneralInfo().getRONumber(), 50), "RO NUMBER"));
                        conditionRO.setComebackLineFlag(checkNull(checkLength(services.getComebackLineFlag(), 1), "COMEBACK LINE FLAG"));
                        conditionRO.setClaimNumber(checkNullToBlank(checkLength(services.getClaimNo(), 6)));
                        conditionRO.setRoLineItemNumber(checkNull(checkLength(services.getServiceId(), 2), "RO LINE ITEM NUMBER"));
                        conditionRO.setServicePaymentType(mandateName(checkNull(services.getServicePaymentType(), "ServicePaymentType"), "ServicePaymentType", ApiConstant.C, ApiConstant.W, ApiConstant.I));
                        conditionRO.setServiceIdStatus(checkNull(checkLength(services.getServiceIDStatus(), 20), "ServiceIdStatus"));
                        conditionRO.setDealerOpsCode(checkNull(checkLength(services.getDealerOpsCode(), 45), "DELAER OPS CODE"));
                        conditionRO.setDealerOpsCodeDesc(checkLength(checkNull(services.getDealerOpsCodeDescr(), "DealerOpsCodeDesc"), 199));
                        conditionRO.setServiceType(checkNull(services.getServiceType(), "ServiceType"));
                    } catch (Exception e) {
                        narrativeRoList.clear();
                        narrativeRoListMul.clear();
                        conditionROInsertList.clear();
                        conditionUpdateROList.clear();
                        FailedDataBean failedDataBean = new FailedDataBean();
                        failedDataBean.setDealer(requestBean.getRoGeneralInfo().getDealerCode());
                        failedDataBean.setVin(requestBean.getVehicleInfo().getVIN());
                        failedDataBean.setRoNumber(requestBean.getRoGeneralInfo().getRONumber());
                        failedDataBean.setRoTransactionType(requestBean.getROTransactionType());
                        failedDataBean.setReceiptHandle(requestBean.getMessageId());
                        failedDataBean.setErrorMessage(e.getMessage());
                        receiptHandle.add(requestBean.getMessageId());
                        filedList.add(failedDataBean);
                        throw new Exception(e);
                    }
                    narrativeRoList.addAll(narrativeRoListMul);
                    fileterInsertAndUpdate(conditionROInsertList, conditionUpdateROList, miniRoDao, conditionRO);
                }
                miniRoDao.insertNarrative(narrativeRoList);
                filterInsertAndUpdate(claimRoInsertList, vehicleRoInsertList, claimRoUpdateList, vehicleRoUpdateList, miniRoDao, claimRo, vehicleRo);
            } catch (Exception e) {
                FailedDataBean failedDataBean = new FailedDataBean();
                failedDataBean.setDealer(requestBean.getRoGeneralInfo().getDealerCode());
                failedDataBean.setVin(requestBean.getVehicleInfo().getVIN());
                failedDataBean.setRoNumber(requestBean.getRoGeneralInfo().getRONumber());
                failedDataBean.setRoTransactionType(requestBean.getROTransactionType());
                failedDataBean.setReceiptHandle(requestBean.getMessageId());
                failedDataBean.setErrorMessage(e.getMessage());
                receiptHandle.add(requestBean.getMessageId());
                filedList.add(failedDataBean);
                continue;

            }
            continue;
        }

        conditionROGlobalInsertList.addAll(conditionROInsertList);
        conditionGlobalUpdateROList.addAll(conditionUpdateROList);

        miniRoDao.insertRos(vehicleRoInsertList, claimRoInsertList, conditionROGlobalInsertList);
        miniRoDao.updateRos(vehicleRoUpdateList, claimRoUpdateList, conditionGlobalUpdateROList);
        response.put("1", filedList);
        response.put("2", receiptHandle);
        return response;

    }

    private NarrativeRo extractingSequence(Map<String, String> vinDetails, RequestBean requestBean, NarrativeRo narrativeRo,
                                           Services services, int corNarSize, int causNarSize, int cusComplain, int i) throws Exception {
        narrativeRo.setVin(requestBean.getVehicleInfo().getVIN());
        narrativeRo.setVhclSan(checkNull(vinDetails.get(ApiConstant.VHCL_SAN), "VCHL SAN"));
        narrativeRo.setVhcPrtiIn(checkNull(vinDetails.get(ApiConstant.PRTITN), "PRTIIN"));
        narrativeRo.setDealerCode(checkNull(checkLength(requestBean.getRoGeneralInfo().getDealerCode(), 5), "DealerCode"));
        narrativeRo.setRoNumber(checkNull(checkLength(requestBean.getRoGeneralInfo().getRONumber(), 50), "RO NUMBER"));
        narrativeRo.setRoLineItemNumber(checkNull(checkLength(services.getServiceId(), 2), "RoLineItemNumber"));
        narrativeRo.setSequenceNumber(i);
        String customerComplain = checkNullToDefault(services.getCustomerComplain(), requestBean.getRoGeneralInfo().getRONumber(), services.getServiceId() );
        String correctNarrative = checkNullToBlank(services.getCorrectionNarrative());
        String causeNarrative = checkNullToBlank(services.getCauseNarrative());
        //This for customerComplain
        if (i == cusComplain) {
            narrativeRo.setCustomerComplain(customerComplain.substring(((i - 1) * 1000), (customerComplain.length())));
        } else if (i > cusComplain) {
            narrativeRo.setCustomerComplain(ApiConstant.BLANK);
        } else {
            narrativeRo.setCustomerComplain(customerComplain.substring(((i - 1) * 1000), (i * 1000)));
        }
        //This for correction narrative
        if (i == corNarSize) {
            narrativeRo.setCorrectionNarrative(correctNarrative.substring(((i - 1) * 1000), (correctNarrative.length())));
        } else if (i > corNarSize) {
            narrativeRo.setCorrectionNarrative(ApiConstant.BLANK);
        } else {
            narrativeRo.setCorrectionNarrative(correctNarrative.substring(((i - 1) * 1000), (i * 1000)));
        }
        //this is for causenarrative
        if (i == causNarSize) {
            narrativeRo.setCauseNarrative(causeNarrative.substring(((i - 1) * 1000), (causeNarrative.length())));
        } else if (i > causNarSize) {
            narrativeRo.setCauseNarrative(ApiConstant.BLANK);
        } else {
            narrativeRo.setCauseNarrative((causeNarrative.substring(((i - 1) * 1000), (i * 1000))));
        }
        return narrativeRo;
    }

    private Map<String, String> getvhclsan(MiniRoDao miniRoDao, String vin, String vin9, String vin8) throws Exception {
        Map<String, String> vinDetails;
        vinDetails = miniRoDao.getVehclSan(vin, SQLQueryConstant.GET_VHCL_SAN_SVEHW);
        if (vinDetails == null || vinDetails.isEmpty()) {
            vinDetails = miniRoDao.getVehclSan(vin, SQLQueryConstant.GET_VHCL_SAN_SNONVEH);
        }
        if (vinDetails == null || vinDetails.isEmpty()) {
            vinDetails = miniRoDao.getVehclSanFromHIS(vin9, vin8, SQLQueryConstant.GET_VHCL_SAN_SHIST);
        }
        return vinDetails;
    }

    private String checkLength(String value, int size) {
        if (value == null || value.equalsIgnoreCase("null")) {
            return value;
        } else {
            if (value.length() > size) {
                return value.substring(0, size);
            }
            return value;
        }
    }


    private void fileterInsertAndUpdate(List<ConditionRO> conditionROInsertList, List<ConditionRO> conditionUpdateROList, MiniRoDao miniRoDao, ConditionRO conditionRO) throws Exception {

        if (ApiConstant.ZERO < miniRoDao.getConditionCount(conditionRO)) {
            conditionUpdateROList.add(conditionRO);
        } else {
            conditionROInsertList.add(conditionRO);
        }

    }

    private void filterInsertAndUpdate(List<ClaimRo> claimRoInsertList, List<VehicleRo> vehicleRoInsertList, List<ClaimRo> claimRoUpdateList, List<VehicleRo> vehicleRoUpdateList, MiniRoDao miniRoDao, ClaimRo claimRo, VehicleRo vehicleRo) throws Exception {
        if (ApiConstant.ZERO < miniRoDao.getClaimCount(claimRo)) {
            claimRoUpdateList.add(claimRo);
        } else {
            claimRoInsertList.add(claimRo);
        }
        if (ApiConstant.ZERO < miniRoDao.getVehicleCount(vehicleRo)) {
            vehicleRoUpdateList.add(vehicleRo);
        } else {
            vehicleRoInsertList.add(vehicleRo);
        }
    }

    private String checkNull(String value, String name) throws Exception {
        if (value != null && !value.isEmpty()) {
            return value;
        } else {
            throw new Exception("Invalid values " + name);
        }
    }

    private String mandateName(String value, String name, String... args) throws Exception {
        if (value.equalsIgnoreCase(args[0]) || value.equalsIgnoreCase(args[1]) || value.equalsIgnoreCase(args[2])) {
            return value;
        } else {
            throw new Exception("Invalid Value" + name);
        }
    }

    private int checkNull(int value) throws Exception {
        return (int) Optional.ofNullable(value).orElseThrow(() -> new Exception(value + "is null"));
    }

    private String dateCheck(String date) throws Exception {
        String validDate = date;
        if (date == null || date.equalsIgnoreCase("null")) {
            return date;
        } else {
            if (date.length() != 19) {
                validDate = date + ":00";
            }
            SimpleDateFormat dateFormat
                    = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            dateFormat.setLenient(false);
            try {
                dateFormat.parse(validDate);
                return validDate;
            } catch (ParseException e) {
                throw new Exception("Invalid date format");
            }
        }
    }

    private String checkNullToBlank(String value) {
        if (value == null || value.equalsIgnoreCase("null")) {
            return ApiConstant.BLANK;
        } else {
            return value;
        }
    }

    private String checkNullToDefault(String value , String roNumber, String roLineItem) {
        if (value.equalsIgnoreCase("null") || value.trim().isEmpty()) {
            return ApiConstant.DEFAULT_CUSTOMER_COMPLAIN_1+ roNumber+ ApiConstant.DEFAULT_CUSTOMER_COMPLAIN_2+ roLineItem+ ApiConstant.DEFAULT_CUSTOMER_COMPLAIN_3 ;
        } else {
            return value;
        }
    }

    private String changeDateDefulte(String value) {
        if (value == null || value.equalsIgnoreCase("null")) {
            return "0001-01-01 00:00:00";
        } else {
            return value;
        }
    }


}
Editor is loading...
Leave a Comment