package com.genpt.nsight.b2b.service.impl; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.genpt.nsight.NSightCache; import com.genpt.nsight.b2b.model.*; import com.genpt.nsight.b2b.service.InvoiceDetailAsPartPriceAvailabilityService; import com.genpt.nsight.config.NSightProperties; import com.genpt.nsight.v1.SplunkLogger; import com.genpt.nsight.v1.model.*; import com.genpt.nsight.v1.model.POSService.POSServiceResponse; import com.genpt.nsight.v1.model.POSService.POSServiceResponseData; import com.genpt.nsight.v4.AvailabilityServiceContextV4; import com.genpt.nsight.v4.model.Part; import com.genpt.nsight.v4.service.InventoryServiceV4; import com.genpt.nsight.v4.service.SiteRelationshipServiceV4; import com.google.common.base.Strings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.*; import java.util.concurrent.ExecutionException; @Service("InvoiceDetailAsPartPriceAvailabilityService") public class InvoiceDetailAsPartPriceAvailabilityServiceImpl implements InvoiceDetailAsPartPriceAvailabilityService { @Autowired NSightCache nSightCache; @Autowired NSightProperties nSightProperties; @Autowired private InventoryServiceV4 inventoryServiceV4; @Autowired SiteRelationshipServiceV4 siteRelationshipServiceV4; private static final Logger LOGGER = LoggerFactory.getLogger(InvoiceDetailAsPartPriceAvailabilityServiceImpl.class); @Autowired public void setInventoryServiceV4(InventoryServiceV4 inventoryServiceV4) { this.inventoryServiceV4 = inventoryServiceV4; } @Autowired public void setSiteRelationshipServiceV4(SiteRelationshipServiceV4 siteRelationshipServiceV4) { this.siteRelationshipServiceV4 = siteRelationshipServiceV4; } @Override public AvailabilityServiceContextV4 initializeContext(AvailabilityServiceContextV4 availabilityServiceContextV4, PriceAvailabilityRequest request){ Requestor requestor = new Requestor(); requestor.setRequestorApplication("PartPriceAvailability"); requestor.setRequestorType("B2BLegacy"); RequestorIdentifier requestorIdentifier = new RequestorIdentifier(); requestorIdentifier.setType("9 Digit Store Number"); requestorIdentifier.setValue(request.getStoreID()); requestor.setRequestorIdentifier(requestorIdentifier); Responder responder = new Responder(); responder.setResponderApplication("POS"); Security security = new Security(); UserCredentials userCredentials = new UserCredentials(); userCredentials.setPassword(request.getAccountPassword()); userCredentials.setUserName("POS"); security.setUserCredentials(userCredentials); responder.setSecurity(security); HeaderRequest headerRequest = new HeaderRequest(); headerRequest.setRequestor(requestor); headerRequest.setResponder(responder); //headerRequest.setRequestID(); headerRequest.setCorrelationId(availabilityServiceContextV4.getCorrelationID()); //need to get the country code of the calling store headerRequest.setCountryCode(nSightCache.getAlpha2CountryCode(request.getStoreID())); headerRequest.setLanguageCode("EN"); headerRequest.setTimestamp(String.valueOf(System.currentTimeMillis())); com.genpt.nsight.v4.model.ActionRequest actionRequest = new com.genpt.nsight.v4.model.ActionRequest(); com.genpt.nsight.v4.model.Action action = new com.genpt.nsight.v4.model.Action(); action.setNeedProductQuantity(true); action.setNeedPricing(true); action.setNeedSuperSedes(true); action.setNeedDeliveryEstimateCutOffTimes(false); String callingStoreSIteID = nSightCache.getAliasToSiteIdMap().get(requestor.getRequestorIdentifier().getType() + "|" +requestor.getRequestorIdentifier().getValue()); if (callingStoreSIteID!=null && nSightCache.getCombinedFriendSet().contains(callingStoreSIteID)) action.setNeedCombinedInventory(true); actionRequest.setAction(action); com.genpt.nsight.v4.model.DetailRequest detailRequest = new com.genpt.nsight.v4.model.DetailRequest(); com.genpt.nsight.v4.model.InventoryRelationshipsRequested inventoryRelationshipsRequested = new com.genpt.nsight.v4.model.InventoryRelationshipsRequested(); inventoryRelationshipsRequested.setLocalStore(true); inventoryRelationshipsRequested.setServingDC(false); inventoryRelationshipsRequested.setAllOtherDCs(false); inventoryRelationshipsRequested.setAllSuppliers(false); detailRequest.setInventoryRelationshipsRequested(inventoryRelationshipsRequested); Set parts = new HashSet<>(); for ( com.genpt.nsight.b2b.model.Part b2bPart: request.getPart()){ Part nsightPart = new Part(); nsightPart.setLineAbbrev(b2bPart.getLineAbbrev()); nsightPart.setPartNumber(b2bPart.getPartNumber()); parts.add(nsightPart); } detailRequest.setParts(parts); com.genpt.nsight.v4.model.ProductAvailabilityRequest productAvailabilityRequest = new com.genpt.nsight.v4.model.ProductAvailabilityRequest(); productAvailabilityRequest.setActionRequest(actionRequest); productAvailabilityRequest.setDetailRequest(detailRequest); productAvailabilityRequest.setHeaderRequest(headerRequest); availabilityServiceContextV4.setRequest(productAvailabilityRequest); return availabilityServiceContextV4; } @Override public PriceAvailabilityResponse getProductAvailability(AvailabilityServiceContextV4 availabilityServiceContextV4, PriceAvailabilityRequest priceAvailabilityRequest) { SplunkLogger splunkLogger = new SplunkLogger(availabilityServiceContextV4.getRequest().getHeaderRequest()); long startTime = System.currentTimeMillis(); //create the response PriceAvailabilityResponse orderedpriceAvailabilityResponse = new PriceAvailabilityResponse(); PriceAvailabilityResponse priceAvailabilityResponse = new PriceAvailabilityResponse(); priceAvailabilityResponse.setStatusCode("0"); String storeNumber = priceAvailabilityRequest.getStoreID(); String siteId = nSightCache.getAliasToSiteIdMap().get("9 Digit Store Number" + "|" + storeNumber); boolean dontCallStore = nSightCache.getDontCallStoreSet().contains(String.valueOf(storeNumber).substring(0, 3).concat(String.valueOf(storeNumber).substring(5, 9))); //Make List of Parts to be queried inventoryServiceV4.preparePartList(availabilityServiceContextV4); //Set Requesting Site availabilityServiceContextV4.setRequestingSite(nSightCache.getSiteIdToSiteMap().get(siteId)); siteRelationshipServiceV4.setRelatedSites(availabilityServiceContextV4); if (siteId == null) { priceAvailabilityResponse.setStatusCode("1"); priceAvailabilityResponse.setStatusMessage("Unknown StoreID"); } else if (dontCallStore) { priceAvailabilityResponse.setStatusCode("99"); priceAvailabilityResponse.setStatusMessage("Store did not answer"); } else { //Get the detail response first if (availabilityServiceContextV4.isCallNonTAMS()) { inventoryServiceV4.setNonNapaInventoryFutures(availabilityServiceContextV4, nSightCache.getSiteIdToSiteMap()); LOGGER.debug(splunkLogger.toString()+" Submitted NonNapa Inventory queries in {} ms", (System.currentTimeMillis() - startTime)); } if(availabilityServiceContextV4.getNonNapaInventoryFutures()!=null) { availabilityServiceContextV4.getNonNapaInventoryFutures().forEach(posServiceResponseListenableFuture -> { POSServiceResponse posServiceResponse = null; try { posServiceResponse = posServiceResponseListenableFuture.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } //valid future response if(posServiceResponse!= null &&posServiceResponse.getStatusCode()!=null && !posServiceResponse.getStatusCode().equalsIgnoreCase("0")) { priceAvailabilityResponse.setStatusCode(posServiceResponse.getStatusCode()); if(posServiceResponse.getStatusMessage()!=null) priceAvailabilityResponse.setStatusMessage(posServiceResponse.getStatusMessage()); } else if (posServiceResponse != null && posServiceResponse.getPosServiceResponseData() != null && posServiceResponse.getPosServiceResponseData().size()>0) { //iterate the parts NumberFormat df = DecimalFormat.getInstance(); df.setGroupingUsed(false); df.setMinimumFractionDigits(2); df.setMaximumFractionDigits(2); NumberFormat df1 = DecimalFormat.getInstance(); df1.setGroupingUsed(false); df1.setMinimumFractionDigits(0); df1.setMaximumFractionDigits(0); NumberFormat df3 = DecimalFormat.getInstance(); df3.setGroupingUsed(false); df3.setMinimumFractionDigits(3); df3.setMaximumFractionDigits(3); for (POSServiceResponseData posServiceResponseData : posServiceResponse.getPosServiceResponseData()) { String lineAbbrev = posServiceResponseData.getLineAbbrev(); PartPriceAvailability partPriceAvailability = new PartPriceAvailability(); if (lineAbbrev.trim().length()<3) partPriceAvailability.setLineAbbrev(Strings.padEnd(lineAbbrev.trim(),3,' ')); else partPriceAvailability.setLineAbbrev(lineAbbrev.trim()); if(posServiceResponseData.getPartNumber()!=null) partPriceAvailability.setPartNumber(posServiceResponseData.getPartNumber()); else partPriceAvailability.setPartNumber(""); partPriceAvailability.setTAMSErrorMsg(""); if(lineAbbrev.trim().equalsIgnoreCase("1")|| lineAbbrev.trim().equalsIgnoreCase("2")||lineAbbrev.trim().equalsIgnoreCase("3")) partPriceAvailability.setDCBalanceQty("0.00"); if(lineAbbrev.trim().equalsIgnoreCase("M")) partPriceAvailability.setQtyOnHand(""); else partPriceAvailability.setQtyOnHand(df.format(posServiceResponseData.getQuantityOnHand())); if(posServiceResponseData.getDescription()!=null) partPriceAvailability.setPartDescription(posServiceResponseData.getDescription()); if(lineAbbrev.trim().equalsIgnoreCase("1")|| lineAbbrev.trim().equalsIgnoreCase("2")||lineAbbrev.trim().equalsIgnoreCase("3")) partPriceAvailability.setDeliveryTime(" "); //check if it has pricing for (POSPrice posPrice : posServiceResponseData.getPosPrices()) { Price price = new Price(); if (posPrice.getPriceType().compareToIgnoreCase(" 1") == 0) { price.setPriceType(" 1"); price.setListPrice(df3.format(posPrice.getListPrice())); price.setYourCost(""); } if (posPrice.getPriceType().compareToIgnoreCase("T ") == 0) { price.setPriceType("T "); price.setListPrice(df3.format(posPrice.getListPrice())); price.setYourCost(""); } if (posPrice.getPriceType().compareToIgnoreCase("DTE") == 0) { price.setPriceType("DTE"); price.setListPrice(Strings.padStart(df1.format(posPrice.getListPrice()),6,'0')); price.setYourCost(Strings.padStart(df1.format(posPrice.getYourCost()),4,'0')); } if (posPrice.getPriceType().compareToIgnoreCase("EMP") == 0) { price.setPriceType("EMP"); price.setListPrice(df1.format(posPrice.getListPrice())); price.setYourCost(df1.format(posPrice.getYourCost())); } if (posPrice.getPriceType().compareToIgnoreCase("OTH") == 0) { price.setPriceType("OTH"); price.setListPrice(df.format(posPrice.getListPrice())); price.setYourCost("0"); } if (posPrice.getPriceType().compareToIgnoreCase("TXB") == 0) { price.setPriceType("TXB"); price.setListPrice(df.format(posPrice.getListPrice())); price.setYourCost(df.format(posPrice.getYourCost())); } if (posPrice.getPriceType().compareToIgnoreCase("TAX") == 0) { price.setPriceType("TAX"); price.setListPrice(df.format(posPrice.getListPrice())); price.setYourCost(df.format(posPrice.getYourCost())); } if(price!=null) partPriceAvailability.getPrice().add(price); } priceAvailabilityResponse.getPartPriceAvailability().add(partPriceAvailability);//for each part } } }); }else {//if success check priceAvailabilityResponse.setStatusCode("2"); priceAvailabilityResponse.setStatusMessage("Store did not answer"); } /////not a success } XmlMapper xmlMapper = new XmlMapper(); String requestAsString = null; try { requestAsString = xmlMapper.writeValueAsString(priceAvailabilityResponse); LOGGER.info(splunkLogger.toString() + " B2BPartPriceAvailabilityResponse : {} ", requestAsString); } catch (JsonProcessingException e) { e.printStackTrace(); } LOGGER.info(splunkLogger.toString()+ " Total time within B2BPartPriceAvailability = {} ms", System.currentTimeMillis() - startTime); return priceAvailabilityResponse; } }