001package org.kuali.ole.select.lookup;
002
003
004import org.hsqldb.lib.StringUtil;
005import org.kuali.ole.DocumentUniqueIDPrefix;
006import org.kuali.ole.OleLookupableImpl;
007import org.kuali.ole.select.businessobject.OLEPurchaseOrderItemSearch;
008import org.kuali.ole.select.businessobject.OlePurchaseOrderItem;
009import org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl;
010import org.kuali.rice.krad.bo.BusinessObject;
011import org.kuali.rice.krad.lookup.LookupableImpl;
012import org.kuali.rice.krad.web.form.LookupForm;
013
014import java.util.*;
015
016public class OLEPurchaseOrderItemLookupableHelperServiceImpl extends OleLookupableImpl {
017
018    protected List<?> getSearchResults(LookupForm form, Map<String, String> searchCriteria, boolean unbounded) {
019        Map<String, String> criteria = new HashMap<String, String>();
020        List<OLEPurchaseOrderItemSearch> searchResults = new ArrayList<OLEPurchaseOrderItemSearch>();
021        List<OlePurchaseOrderItem> purchaseOrderItems = new ArrayList<OlePurchaseOrderItem>();
022        if(searchCriteria.get("vendorDetailAssignedIdentifier")!=null && !searchCriteria.get("vendorDetailAssignedIdentifier").isEmpty()){
023            criteria.put("purchaseOrder.vendorDetailAssignedIdentifier",searchCriteria.get("vendorDetailAssignedIdentifier"));
024        }
025        if(searchCriteria.get("vendorHeaderGeneratedIdentifier")!=null && !searchCriteria.get("vendorHeaderGeneratedIdentifier").isEmpty()){
026            criteria.put("purchaseOrder.vendorHeaderGeneratedIdentifier",searchCriteria.get("vendorHeaderGeneratedIdentifier"));
027        }
028        if (StringUtil.isEmpty(searchCriteria.get("purapDocumentIdentifier"))) {
029            purchaseOrderItems = (List<OlePurchaseOrderItem>)getBusinessObjectService().findMatching(OlePurchaseOrderItem.class, criteria);
030        }
031        else if(isNumber(searchCriteria.get("purapDocumentIdentifier"))){
032            if(searchCriteria.get("purapDocumentIdentifier")!=null && !searchCriteria.get("purapDocumentIdentifier").isEmpty()){
033                criteria.put("purchaseOrder.purapDocumentIdentifier",searchCriteria.get("purapDocumentIdentifier"));
034            }
035            purchaseOrderItems = (List<OlePurchaseOrderItem>)getBusinessObjectService().findMatching(OlePurchaseOrderItem.class,criteria);
036        }
037        OLEPurchaseOrderItemSearch poItemSearch;
038        for (OlePurchaseOrderItem poItem : purchaseOrderItems) {
039            if (poItem.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
040                if(poItem.getItemTitleId()!=null){
041                    poItem.setDocFormat(DocumentUniqueIDPrefix.getBibFormatType(poItem.getItemTitleId()));
042                }
043                poItemSearch = new OLEPurchaseOrderItemSearch();
044                poItemSearch.setPurchaseOrderItemID(poItem.getItemIdentifier());
045                poItemSearch.setPurapDocumentIdentifier(poItem.getPurapDocument() != null &&
046                        poItem.getPurapDocument().getPurapDocumentIdentifier() != null ? poItem.getPurapDocument().getPurapDocumentIdentifier().toString() : "");
047                poItemSearch.setTempItemIdentifier(poItem.getItemIdentifier());
048                poItemSearch.setItemDescription(poItem.getItemDescription());
049                poItemSearch.setLocalId(poItem.getItemTitleId());
050                poItemSearch.setAccountsPayableLinkIdentifier(poItem.getPurapDocument() != null &&
051                        poItem.getPurapDocument().getAccountsPayablePurchasingDocumentLinkIdentifier() != null ?
052                        poItem.getPurapDocument().getAccountsPayablePurchasingDocumentLinkIdentifier() : Integer.valueOf("0"));
053                poItemSearch.setPoItemPrice(poItem.getExtendedPrice() != null ? poItem.getExtendedPrice().toString() : "0.00");
054                poItemSearch.setItemLineNumber(poItem.getItemLineNumber());
055                poItemSearch.setDocFormat(poItem.getDocFormat());
056                searchResults.add(poItemSearch);
057            }
058        }
059        return searchResults;
060    }
061    public static boolean isNumber(String actionInterval) {
062        String actStr = actionInterval;
063        for (int i = 0; i < actStr.length(); i++) {
064            if (!Character.isDigit(actStr.charAt(i)))
065                return false;
066        }
067        return true;
068    }
069
070    @Override
071    public Map<String, String> performClear(LookupForm form, Map<String, String> searchCriteria) {
072        super.performClear(form,searchCriteria);
073        // Map<String, InputField> criteriaFieldMap = new HashMap<String, InputField>();
074        Map<String, String> clearedSearchCriteria = new HashMap<String, String>();
075        for (Map.Entry<String, String> searchKeyValue : searchCriteria.entrySet()) {
076            String searchPropertyName = searchKeyValue.getKey();
077            if(searchPropertyName.equals("vendorDetailAssignedIdentifier") ||
078                    searchPropertyName.equals("vendorHeaderGeneratedIdentifier") ||
079                searchPropertyName.equals("vendorName")) {
080                clearedSearchCriteria.put(searchPropertyName,searchKeyValue.getValue());
081            }
082        }
083        return clearedSearchCriteria;
084    }
085}