| package com.scraper.cpa.idp.service;
|
|
|
| import java.util.Arrays;
|
| import java.util.Date;
|
| import java.util.HashSet;
|
| import org.apache.kafka.clients.consumer.ConsumerRecord;
|
| import org.springframework.beans.BeanUtils;
|
|
|
| import com.highradius.agents.base.ClaimAgent;
|
| import com.highradius.arpa.util.ArpaJaxbUtil;
|
| import com.highradius.common.piiUtils.HRCPIIUtility;
|
| import com.highradius.common.util.SpringApplicationContext;
|
| import com.highradius.daggr.parser.CustomPDFParser;
|
| import com.highradius.datasource.CustomRoutingDataSource;
|
| import com.highradius.kafka.dto.IDPResultDto;
|
| import com.highradius.util.StringUtils;
|
| import com.highradius.util.cpa.StringConstants;
|
| import com.receivablesradius.claims.manager.CustomerClaimManager;
|
| import com.receivablesradius.claims.model.CustomerClaim;
|
| import com.receivablesradius.claims.model.CustomerClaimHistory;
|
| import com.receivablesradius.deductions.model.ErrorCode;
|
| import com.scraper.agents.base.Util;
|
| import com.fasterxml.jackson.databind.ObjectMapper;
|
| import com.fasterxml.jackson.databind.node.ObjectNode;
|
| import com.fasterxml.jackson.databind.JsonNode;
|
| import java.util.Iterator;
|
| import java.util.Map;
|
| import java.util.Set;
|
|
|
| public class IDPAggregationService {
|
|
|
| private static final Set<String> CUSTOMER_CLAIM_HEADER_PII_COLUMNS = new HashSet<String>(Arrays.asList("buyerName,contactPerson,brokerName,piiReferenceField1,piiReferenceField2".split(",")));
|
|
|
| public void aggregateData(ConsumerRecord<String, IDPResultDto> record) {
|
| try {
|
| CustomRoutingDataSource.setCurrentAccountDatasource(8009711);
|
| CustomerClaimManager customerClaimManager = (CustomerClaimManager) SpringApplicationContext.getBean("customerClaimManager");
|
| CustomerClaim oldClaim = customerClaimManager.getCustomerClaimByCustomerClaimId(52160);
|
| String jsonIdpData="";
|
| String mergedClaimJson=getMergedClaimJson(oldClaim.getNormalizedData(),jsonIdpData);
|
| Object claimObj=createIDPClaimObject(oldClaim,mergedClaimJson);
|
| persistObjectInstance(claimObj,oldClaim,customerClaimManager);
|
| }catch(Exception e) {
|
|
|
| }
|
| }
|
|
|
| private String getMergedClaimJson(String oldXmlData, String idpClaimJson) {
|
| String mergedClaimJson=StringConstants.EMPTY_STRING;
|
| ObjectMapper objectMapperFirst = new ObjectMapper();
|
| try {
|
| ArpaJaxbUtil arpaJaxbUtil = new ArpaJaxbUtil();
|
| oldXmlData=sanitizaXml(oldXmlData);
|
| String oldClaimJson=arpaJaxbUtil.parseClaimXmlToJson(oldXmlData);
|
| oldClaimJson=cleanNormFieldName(oldClaimJson);
|
| idpClaimJson=cleanNormFieldName(idpClaimJson);
|
| mergedClaimJson=mergeJson(objectMapperFirst.readTree(oldClaimJson),objectMapperFirst.readTree(idpClaimJson));
|
| }catch (Exception ex) {
|
|
|
| }
|
| return mergedClaimJson;
|
| }
|
|
|
| private Object createIDPClaimObject(CustomerClaim oldClaim, String mergedClaimJson) {
|
| Object objectInstance=null;
|
| ObjectMapper objectMapper = new ObjectMapper();
|
| try {
|
| CustomPDFParser agentValueSet=new CustomPDFParser();
|
| objectInstance = createObjectInstance(oldClaim);
|
| JsonNode rootNode = objectMapper.readTree(mergedClaimJson);
|
| JsonNode claimNode = rootNode.get("claim");
|
| Iterator<Map.Entry<String, JsonNode>> fields = claimNode.fields();
|
| while (fields.hasNext()) {
|
| Map.Entry<String, JsonNode> field = fields.next();
|
| String key = field.getKey();
|
| String value = field.getValue().asText();
|
| agentValueSet.setValuesForCustomAgent(objectInstance, key, value, "Header", null);
|
|
|
| }
|
| JsonNode lineItemsNode = claimNode.get("lineItems");
|
| if (lineItemsNode != null && lineItemsNode.isArray()) {
|
| for (JsonNode itemNode : lineItemsNode) {
|
| Iterator<Map.Entry<String, JsonNode>> itemFields = itemNode.fields();
|
| int slNum = 0;
|
| buildItemTags(true, objectInstance, ++slNum);
|
| while (itemFields.hasNext()) {
|
| Map.Entry<String, JsonNode> itemField = itemFields.next();
|
| String key = itemField.getKey();
|
| String value = itemField.getValue().asText();
|
| agentValueSet.setValuesForCustomAgent(objectInstance, key, value, "Item", null);
|
| }
|
| buildItemTags(false, objectInstance, slNum);
|
| }
|
| }
|
| }catch (Exception ex) {
|
|
|
| }
|
| return objectInstance;
|
| }
|
|
|
| private String sanitizaXml(String input) {
|
| if (input == null) return null;
|
| input = input.replace("&", "[AMP]");
|
| input = input.replace("&", "&");
|
| input = input.replace("[AMP]", "&");
|
| if(input.contains("Customer Name=")) {
|
| input=input.replace("Customer Name=","customerName=");
|
| }
|
| return input;
|
| }
|
|
|
| private String mergeJson(JsonNode claimJson, JsonNode idpJson) {
|
| ObjectMapper objectMapper = new ObjectMapper();
|
| ObjectNode parentJson = objectMapper.createObjectNode();
|
| ObjectNode claimNode = (ObjectNode) claimJson.get("claim");
|
| idpJson.fields().forEachRemaining(entry -> {
|
| String key = entry.getKey();
|
| JsonNode idpValue = entry.getValue();
|
| if (!idpValue.isNull() && !idpValue.asText().isEmpty()) {
|
| claimNode.put(key, idpValue.asText());
|
| }
|
| });
|
| JsonNode idpItems = idpJson.get("items");
|
| if (idpItems != null && idpItems.isArray() && idpItems.size() > 0) {
|
| claimNode.set("lineItems", idpItems);
|
| }
|
| parentJson.set("claim", claimNode);
|
| return parentJson.toString();
|
| }
|
|
|
| private void persistObjectInstance(Object obj,CustomerClaim oldClaim, CustomerClaimManager customerClaimManager) {
|
| CustomerClaim claim = (CustomerClaim) obj;
|
| StringBuffer normalizedDataBuffer = buildNormalizedData(claim);
|
| String normalizedData=normalizedDataBuffer.toString().replace("&","&").replace("customerName=", "Customer Name=");
|
| claim.setNormalizedData(normalizedData);
|
| claim.setCustomerClaimId(oldClaim.getCustomerClaimId());
|
| ErrorCode err=new ErrorCode();
|
| err.setErrorCodeId(Integer.parseInt(Util.getErrorCodeId(ClaimAgent.SUCCESS)));
|
| claim.setErrorCode(err);
|
| saveClaimHistory(oldClaim,customerClaimManager);
|
| String[] ignoreProperties = {"customerClaimId","createUser","createTime","createDate"};
|
| BeanUtils.copyProperties(claim, oldClaim, ignoreProperties);
|
| oldClaim.setUpdateDate(new Date());
|
| oldClaim.setUpdateTime(new Date());
|
| oldClaim.setRawClaimDoc(oldClaim.getClaimDoc());
|
| oldClaim.setCustomerName(claim.getCustomerName());
|
| customerClaimManager.saveOrUpdate(oldClaim);
|
| }
|
|
|
| private void saveClaimHistory(CustomerClaim oldClaim , CustomerClaimManager customerClaimManager) {
|
| try {
|
| CustomerClaimHistory claimHist = new CustomerClaimHistory();
|
| claimHist.setJobId(oldClaim.getJob().getJobId());
|
| claimHist.setCustomerClaimId(oldClaim.getCustomerClaimId());
|
| claimHist.setAccountId(oldClaim.getAccount());
|
| claimHist.setProviderDatatypeId(oldClaim.getProviderDatatypeId().getProviderDatatypeId());
|
| claimHist.setFkApdId(oldClaim.getFkAPDId().getAccountProviderDatatypeId());
|
| claimHist.setErrorCode(oldClaim.getErrorCode().getErrorCodeId());
|
| claimHist.setClaimNumber(oldClaim.getClaimNumber());
|
| claimHist.setNormalizedData(oldClaim.getNormalizedData());
|
| claimHist.setCreateUser(oldClaim.getCreateUser());
|
| claimHist.setCreateTime(new Date());
|
| customerClaimManager.saveOrUpdateCustClaimHistory(claimHist);
|
| } catch (Exception e){
|
|
|
| }
|
| }
|
|
|
| private StringBuffer buildNormalizedData(CustomerClaim claim) {
|
| StringBuffer normalizedData = new StringBuffer();
|
| normalizedData.append("<response>");
|
| normalizedData.append(claim.getHeaderNormalizedData()).append(">");
|
| normalizedData.append("<claimItems>");
|
| if(StringUtils.isNotBlank(claim.getItemNormalizedData())){
|
| normalizedData.append(claim.getItemNormalizedData());
|
| }
|
| normalizedData.append("</claimItems></claim></response>");
|
| return normalizedData;
|
| }
|
|
|
| public String buildHeaderNormalizedData(String fieldName,String fieldValue, String normalizedData, int accountId) {
|
| if(StringUtils.isBlank(normalizedData)){
|
| normalizedData = "<claim ";
|
| }
|
| //check for GDPR encryption
|
| String encryptedValue = encryptAttributesForClaim(fieldName, fieldValue, CUSTOMER_CLAIM_HEADER_PII_COLUMNS, accountId);
|
| normalizedData += cleanFieldName(fieldName)+ "=\"" + encryptedValue+ "\" ";
|
| return normalizedData;
|
| }
|
|
|
| protected String cleanFieldName(String input) {
|
| if(StringUtils.isNotBlank(input)){
|
| input = input.replace("vendorId" , "vendorID");
|
| input = input.replace("ecomfrt" , "ecomFrt");
|
| input = input.replace("misccharges" , "miscCharges");
|
| input = input.replace("payto" , "payTo");
|
| input = input.replace("promotionStartDate" , "promotionstartdate");
|
| input = input.replace("promotionEndDate" , "promotionenddate");
|
| input = input.replace("customerPromotionId" , "customerpromotionid");
|
| }
|
| return input;
|
| }
|
|
|
| protected String cleanNormFieldName(String input) {
|
| if(StringUtils.isNotBlank(input)){
|
| input = input.replace("vendorID" , "vendorId");
|
| input = input.replace("ecomFrt" , "ecomfrt");
|
| input = input.replace("miscCharges" , "misccharges");
|
| input = input.replace("payTo" , "payto");
|
| input = input.replace("promotionstartdate" , "promotionStartDate");
|
| input = input.replace("promotionenddate" , "promotionEndDate");
|
| input = input.replace("customerpromotionid" , "customerPromotionId");
|
| }
|
| return input;
|
| }
|
|
|
| private String encryptAttributesForClaim(String name, String value, Set<String> piiColumns, int accountId) {
|
| HRCPIIUtility encryptionUtil = HRCPIIUtility.getInstance(accountId);
|
| if(piiColumns.contains(name)) {
|
| try {
|
| return encryptionUtil.encrypt(value);
|
| } catch (Exception e) {
|
| }
|
| }
|
| return value;
|
| }
|
|
|
|
|
| private void buildItemTags(boolean isItemStart ,Object customerClaim,Integer slNum) {
|
| CustomerClaim cc = (CustomerClaim) customerClaim;
|
| if(isItemStart){
|
| if (StringUtils.isNotBlank(cc.getItemNormalizedData())) {
|
| cc.setItemNormalizedData(cc.getItemNormalizedData() + "<claimItem ");
|
| } else {
|
| cc.setItemNormalizedData("<claimItem ");
|
| }
|
| cc.setItemNormalizedData(cc.getItemNormalizedData() + "slNum =\"" + slNum + "\"");
|
| cc.setItemCount(String.valueOf(slNum));
|
| }else{
|
| cc.setItemNormalizedData(cc.getItemNormalizedData() + "></claimItem>");
|
| }
|
| }
|
|
|
| public Object createObjectInstance(CustomerClaim oldClaim) {
|
| CustomerClaim claim = new CustomerClaim();
|
| setInitialValues(claim,oldClaim);
|
| setAuditFields(claim);
|
| return claim;
|
| }
|
|
|
| private void setInitialValues(CustomerClaim claim,CustomerClaim oldClaim) {
|
| claim.setAccount(oldClaim.getAccount());
|
| claim.setProviderDatatypeId(oldClaim.getProviderDatatypeId());
|
| claim.setFkAPDId(oldClaim.getFkAPDId());
|
| claim.setFkAggregationType(oldClaim.getFkAggregationType());
|
| claim.setJob(oldClaim.getJob());
|
| claim.setErrorCode(oldClaim.getErrorCode());
|
| claim.setMapCustomerAccountRad(oldClaim.getMapCustomerAccountRad());
|
| claim.setClaimDoc(oldClaim.getClaimDoc());
|
| }
|
|
|
| private void setAuditFields(CustomerClaim claim) {
|
| claim.setCreateDate(new Date());
|
| claim.setCreateTime(new Date());
|
| claim.setCreateUser("IDP");
|
| claim.setUpdateDate(new Date());
|
| claim.setUpdateTime(new Date());
|
| claim.setUpdateUser("IDP");
|
| }
|
|
|
| }
|