001package org.kuali.ole.ingest;
002
003import org.kuali.ole.OLEConstants;
004import org.kuali.ole.ingest.pojo.ProfileAttributeBo;
005import org.kuali.ole.pojo.OleTxRecord;
006import org.kuali.ole.pojo.edi.*;
007import org.kuali.ole.select.bo.OleVendorAccountInfo;
008import org.kuali.ole.service.OverlayRetrivalService;
009import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
010
011import java.util.*;
012
013/**
014 * OleTxRecordBuilder is used to build the transaction information into a pojo which are extracted from the ingested marc and edi file.
015 */
016public class OleTxRecordBuilder {
017    private static OleTxRecordBuilder oleTxRecordBuilder;
018    private OverlayRetrivalService overlayRetrivalService;
019
020    /**
021     * default constructor of OleTxRecordBuilder.
022     */
023    private OleTxRecordBuilder() {
024
025    }
026
027    /**
028     *  Gets the instance of OleTxRecordBuilder.
029     *  If OleTxRecordBuilder is null it returns new instance else it returns existing instance.
030     * @return
031     */
032    public static OleTxRecordBuilder getInstance() {
033        if (null == oleTxRecordBuilder) {
034            oleTxRecordBuilder = new OleTxRecordBuilder();
035        }
036        return oleTxRecordBuilder;
037    }
038
039    /**
040     * This method returns OleTxRecord.
041     * This method build the OleTxRecord based on lineItemOrder,list of profileAttribute,ediOrder
042     * @param lineItemOrder
043     * @param profileAttributeBos
044     * @param ediOrder
045     * @return  oleTxRecord
046     */
047    public OleTxRecord build(LineItemOrder lineItemOrder, EDIOrder ediOrder) throws Exception {
048        OleTxRecord oleTxRecord = new OleTxRecord();
049        oleTxRecord.setListPrice(getListPrice(lineItemOrder));
050        oleTxRecord.setQuantity(getQuantity(lineItemOrder));
051        oleTxRecord.setVendorItemIdentifier(getVendorItemIdentifier(lineItemOrder));
052        oleTxRecord.setVendorNumber(getVendorNumber(ediOrder));
053        /*oleTxRecord.setChartCode(getChartCode(profileAttributeBos));
054        oleTxRecord.setOrgCode(getAttributeValue(profileAttributeBos, OLEConstants.ORG_CODE));
055        oleTxRecord.setReceivingRequired(Boolean.parseBoolean(getAttributeValue(profileAttributeBos, OLEConstants.RECV_REQUIRED)));
056        oleTxRecord.setContractManager(getAttributeValue(profileAttributeBos, OLEConstants.CONTRACT_MANAGER));*/
057        //oleTxRecord.setAssignToUser(getAttributeValue(profileAttributeBos, OLEConstants.ASSIGN_TO_USER));
058        //oleTxRecord.setUseTaxIndicator(Boolean.parseBoolean(getAttributeValue(profileAttributeBos, OLEConstants.USE_TAXIND)));
059        //oleTxRecord.setOrderType(getAttributeValue(profileAttributeBos, OLEConstants.ORDER_TYPE));
060        /*oleTxRecord.setFundingSource(getAttributeValue(profileAttributeBos, OLEConstants.FUNDING_SOURCE));
061        oleTxRecord.setPayReqPositiveApprovalReq(Boolean.parseBoolean(getAttributeValue(profileAttributeBos, OLEConstants.PAYREQ_POSITIVE_APPROVAL)));
062        oleTxRecord.setPurchaseOrderConfirmationIndicator(Boolean.parseBoolean(getAttributeValue(profileAttributeBos, OLEConstants.PURCHASE_CONFIRMATION_INDICATOR)));
063        oleTxRecord.setRequisitionSource(getAttributeValue(profileAttributeBos, OLEConstants.REQUISITION_SOURCE));
064        oleTxRecord.setDeliveryCampusCode(getAttributeValue(profileAttributeBos, OLEConstants.DELIVERY_CAMPUS));
065        oleTxRecord.setBuildingCode(getAttributeValue(profileAttributeBos, OLEConstants.BUILDING));*/
066        //oleTxRecord.setVendorChoice(getAttributeValue(profileAttributeBos, OLEConstants.VENDOR_CHOICE));
067        /*oleTxRecord.setItemType(getAttributeValue(profileAttributeBos, OLEConstants.ITEM_TYPE));
068        oleTxRecord.setRouteToRequestor(Boolean.parseBoolean(getAttributeValue(profileAttributeBos, OLEConstants.ROUTE_RQUESTER)));
069        oleTxRecord.setRouteToRequestor(Boolean.parseBoolean(getAttributeValue(profileAttributeBos, OLEConstants.PUBLIC_VIEW)));
070        oleTxRecord.setMethodOfPOTransmission(getAttributeValue(profileAttributeBos, OLEConstants.PO_TRAMISSION_METHOD));
071        oleTxRecord.setInternalPurchasingLimit(getAttributeValue(profileAttributeBos, OLEConstants.INTERNAL_PURCHASING_LIMIT));
072        oleTxRecord.setCostSource(getAttributeValue(profileAttributeBos, OLEConstants.COST_SOURCE));
073        oleTxRecord.setPercent(getAttributeValue(profileAttributeBos, OLEConstants.PERCENT));
074        oleTxRecord.setDefaultLocation(getAttributeValue(profileAttributeBos, OLEConstants.DEFAULT_LOCATION));*/
075        return oleTxRecord;
076    }
077
078    public OleTxRecord build(List<ProfileAttributeBo> profileAttributeBos) throws Exception {
079        OleTxRecord oleTxRecord = new OleTxRecord();
080/*        oleTxRecord.setListPrice(getListPrice(lineItemOrder));
081        oleTxRecord.setQuantity(getQuantity(lineItemOrder));
082        oleTxRecord.setVendorItemIdentifier(getVendorItemIdentifier(lineItemOrder));
083        oleTxRecord.setVendorNumber(getVendorNumber(ediOrder));       */
084        /*oleTxRecord.setChartCode(getChartCode(profileAttributeBos));
085        oleTxRecord.setOrgCode(getAttributeValue(profileAttributeBos, OLEConstants.ORG_CODE));
086        oleTxRecord.setReceivingRequired(Boolean.parseBoolean(getAttributeValue(profileAttributeBos, OLEConstants.RECV_REQUIRED)));
087        oleTxRecord.setContractManager(getAttributeValue(profileAttributeBos, OLEConstants.CONTRACT_MANAGER));*/
088        //oleTxRecord.setAssignToUser(getAttributeValue(profileAttributeBos, OLEConstants.ASSIGN_TO_USER));
089        //oleTxRecord.setUseTaxIndicator(Boolean.parseBoolean(getAttributeValue(profileAttributeBos, OLEConstants.USE_TAXIND)));
090        //oleTxRecord.setOrderType(getAttributeValue(profileAttributeBos, OLEConstants.ORDER_TYPE));
091        /*oleTxRecord.setFundingSource(getAttributeValue(profileAttributeBos, OLEConstants.FUNDING_SOURCE));
092        oleTxRecord.setPayReqPositiveApprovalReq(Boolean.parseBoolean(getAttributeValue(profileAttributeBos, OLEConstants.PAYREQ_POSITIVE_APPROVAL)));
093        oleTxRecord.setPurchaseOrderConfirmationIndicator(Boolean.parseBoolean(getAttributeValue(profileAttributeBos, OLEConstants.PURCHASE_CONFIRMATION_INDICATOR)));
094        oleTxRecord.setRequisitionSource(getAttributeValue(profileAttributeBos, OLEConstants.REQUISITION_SOURCE));
095        oleTxRecord.setDeliveryCampusCode(getAttributeValue(profileAttributeBos, OLEConstants.DELIVERY_CAMPUS));
096        oleTxRecord.setBuildingCode(getAttributeValue(profileAttributeBos, OLEConstants.BUILDING));*/
097        //oleTxRecord.setVendorChoice(getAttributeValue(profileAttributeBos, OLEConstants.VENDOR_CHOICE));
098        /*oleTxRecord.setItemType(getAttributeValue(profileAttributeBos, OLEConstants.ITEM_TYPE));
099        oleTxRecord.setRouteToRequestor(Boolean.parseBoolean(getAttributeValue(profileAttributeBos, OLEConstants.ROUTE_RQUESTER)));
100        oleTxRecord.setRouteToRequestor(Boolean.parseBoolean(getAttributeValue(profileAttributeBos, OLEConstants.PUBLIC_VIEW)));
101        oleTxRecord.setMethodOfPOTransmission(getAttributeValue(profileAttributeBos, OLEConstants.PO_TRAMISSION_METHOD));
102        oleTxRecord.setInternalPurchasingLimit(getAttributeValue(profileAttributeBos, OLEConstants.INTERNAL_PURCHASING_LIMIT));
103        oleTxRecord.setCostSource(getAttributeValue(profileAttributeBos, OLEConstants.COST_SOURCE));
104        oleTxRecord.setPercent(getAttributeValue(profileAttributeBos, OLEConstants.PERCENT));
105        oleTxRecord.setDefaultLocation(getAttributeValue(profileAttributeBos, OLEConstants.DEFAULT_LOCATION));*/
106        return oleTxRecord;
107    }
108
109
110    /**
111     *  This method returns chartCode from the List of profileAttributeBos.
112     * @param profileAttributeBos
113     * @return   AttributeValue
114     */
115    private String getChartCode(List<ProfileAttributeBo> profileAttributeBos) {
116        return getAttributeValue(profileAttributeBos, OLEConstants.OLE_CHART_CODE);
117    }
118
119    /**
120     *  This method gets the vendorNumber from ediOrder.
121     * @param ediOrder
122     * @return
123     */
124    private String getVendorNumber(EDIOrder ediOrder) {
125        return ediOrder.getMessage().getSupplierPartyQualifier().getSupplierInformation().getSupplierCodeIdentification();
126    }
127
128    /**
129     * This method returns fundCode as AccountInfo, based on List of  supplierReferenceInformation got from lineItemOrder.
130     * @param lineItemOrder
131     * @return AccountInfo
132     */
133    public Map<String, String> getAccountInfo(LineItemOrder lineItemOrder) throws Exception {
134        List<SupplierReferenceInformation> supplierReferenceInformationList = lineItemOrder.getSupplierReferenceInformation();
135        if (!supplierReferenceInformationList.isEmpty()) {
136            SupplierReferenceInformation supplierReferenceInformation = supplierReferenceInformationList.get(0);
137            List<SupplierLineItemReference> supplierLineItemReferenceList = supplierReferenceInformation.getSupplierLineItemReference();
138            if (!supplierLineItemReferenceList.isEmpty()) {
139                SupplierLineItemReference supplierLineItemReference = supplierLineItemReferenceList.get(0);
140                if (supplierLineItemReference.getSuppliersOrderLine().equals("BFN")) {
141                    return translateRefNumberToOLEAccountInfo(supplierLineItemReference.getVendorReferenceNumber());
142                }
143            }
144        }
145        return null;
146    }
147
148
149
150    /**
151     * This method returns fundCode as AccountInfo, based on List of  supplierReferenceInformation got from lineItemOrder.
152     * @param vendorReferenceNumber
153     * @return AccountInfo
154     */
155    public Map<String, String> getAccountInfo(String vendorReferenceNumber) throws Exception {
156            return translateRefNumberToOLEAccountInfo(vendorReferenceNumber);
157    }
158
159    /**
160     *  This method maps the vendorReferenceNumber and returns the  fundCode.
161     * @param vendorReferenceNumber
162     * @return  fundCodes
163     */
164    private Map<String, String> translateRefNumberToOLEAccountInfo(String vendorReferenceNumber)throws Exception{
165        //HashMap<String, String> fundCodes = new OLEAccountInfoExtractor().buildAccountInfoMap(vendorReferenceNumber);
166        HashMap<String, String> fundCodes = getVendorAccountObjectDetails(vendorReferenceNumber);
167        return fundCodes;
168    }
169
170    private HashMap<String,String> getVendorAccountObjectDetails(String vendorReferenceNumber)throws Exception{
171        HashMap<String,String>  criteriaMap=new HashMap<String,String>();
172        criteriaMap.put("vendorRefNumber",vendorReferenceNumber);
173        OleVendorAccountInfo oleVendorAccountInfo = getOverlayRetrivalService().getAccountObjectForVendorRefNo(criteriaMap);
174        if(oleVendorAccountInfo==null)
175            return null;
176        HashMap<String,String> accountObjectMap=new HashMap<String, String>();
177        accountObjectMap.put(oleVendorAccountInfo.getAccountNumber(), oleVendorAccountInfo.getObjectCode());
178        return accountObjectMap;
179
180    }
181
182    /**
183     *  This method returns vendorItemReference number from the List of buyerReferenceInformation got from lineItemOrder.
184     * @param lineItemOrder
185     * @return vendorItemReference
186     */
187    public String getVendorItemIdentifier(LineItemOrder lineItemOrder) {
188        List<BuyerReferenceInformation> buyerReferenceInformationList = lineItemOrder.getBuyerReferenceInformation();
189        if (buyerReferenceInformationList.size() > 0) {
190            BuyerReferenceInformation buyerReferenceInformation = buyerReferenceInformationList.get(0);
191            List<BuyerLineItemReference> buyerLineItemReferenceList = buyerReferenceInformation.getBuyerLineItemReference();
192            if (buyerLineItemReferenceList.size() > 0) {
193                BuyerLineItemReference buyerLineItemReferenceRef = buyerLineItemReferenceList.get(0);
194                String buyersOrderLine = buyerLineItemReferenceRef.getBuyersOrderLine();
195                String vendorItemReference = buyerLineItemReferenceRef.getOrderLineNumber();
196                if (buyersOrderLine.equals("SLI")) {
197                    return vendorItemReference;
198                }
199            }
200        }
201        return null;
202    }
203
204    /**
205     * This method returns the Quantity from the list of QuantityInformation got from lineItemOrder.
206     * If there are no QuantityInformation then it return null.
207     * @param lineItemOrder
208     * @return Quantity
209     */
210    private String getQuantity(LineItemOrder lineItemOrder) {
211        List<QuantityInformation> quantityInformation = lineItemOrder.getQuantityInformation();
212        if (quantityInformation.size() > 0) {
213            List<Qunatity> qunatity = quantityInformation.get(0).getQunatity();
214            if (qunatity.size() > 0) {
215                return qunatity.get(0).getQuantity();
216            }
217        }
218        return null;
219    }
220
221    /**
222     *  This method returns ListPrice from the List of PriceInformation got from lineItemOrder.
223     *  If there are no PriceInformation then it return null.
224     * @param lineItemOrder
225     * @return  Price
226     */
227    private String getListPrice(LineItemOrder lineItemOrder) {
228        List<PriceInformation> priceInformation = lineItemOrder.getPriceInformation();
229        if (priceInformation.size() > 0) {
230            List<ItemPrice> itemPrice = priceInformation.get(0).getItemPrice();
231            if (itemPrice.size() > 0) {
232                return itemPrice.get(0).getPrice();
233            }
234        }
235        return null;
236    }
237
238    /**
239     * This method returns AttributeValue from List of profileAttribute with matching attributeName.
240     * If there are no profile attributes then it return null.
241     * @param profileAttributes
242     * @param attributeName
243     * @return  attributeValue
244     */
245    private String getAttributeValue(List<ProfileAttributeBo> profileAttributes, String attributeName) {
246        for (Iterator<ProfileAttributeBo> iterator = profileAttributes.iterator(); iterator.hasNext(); ) {
247            ProfileAttributeBo attribute = iterator.next();
248            if (attribute.getAttributeName().equals(attributeName)) {
249                return attribute.getAttributeValue();
250            }
251        }
252        return null;
253    }
254
255    public OverlayRetrivalService getOverlayRetrivalService() {
256        if(overlayRetrivalService == null){
257            overlayRetrivalService = GlobalResourceLoader.getService(OLEConstants.OVERLAY_RETRIVAL_SERVICE);
258        }
259        return overlayRetrivalService;
260    }
261
262    public void setOverlayRetrivalService(OverlayRetrivalService overlayRetrivalService) {
263        this.overlayRetrivalService = overlayRetrivalService;
264    }
265
266}