001package org.kuali.ole.ingest.function;
002
003import org.kuali.ole.DataCarrierService;
004import org.kuali.ole.OLEConstants;
005import org.kuali.ole.service.impl.OverlayMatchingServiceImpl;
006import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
007import org.kuali.rice.krms.framework.engine.Function;
008
009import java.util.ArrayList;
010import java.util.Iterator;
011import java.util.List;
012import java.util.Map;
013
014/**
015 * Created with IntelliJ IDEA.
016 * User: ?
017 * Date: 2/6/13
018 * Time: 11:59 AM
019 * To change this template use File | Settings | File Templates.
020 */
021public class VendorLineItemReferenceFunction implements Function {
022
023
024    @Override
025    public Object invoke(Object... arguments) {
026        DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
027        String existingDocstoreField = (String)(arguments[0]);
028        String vendorLineItemReferenceNumber = (String)(arguments[1]);
029        if(vendorLineItemReferenceNumber != null){
030            try {
031                List list = new OverlayMatchingServiceImpl().getInstanceCollectionOnVendorLineItemIdentifierMatch(vendorLineItemReferenceNumber);
032                if(list.size() >= 1){
033                    for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
034                        Map map = (Map) iterator.next();
035                        if (map.containsKey(OLEConstants.BIB_UNIQUE_ID)) {
036                            map.remove(OLEConstants.BIB_UNIQUE_ID);
037                        }
038                        List<String> bibIdList = (List<String>) map.get(OLEConstants.BIB_IDENTIFIER);
039                        map.put(OLEConstants.BIB_UNIQUE_ID,bibIdList.get(0));
040                    }
041                    dataCarrierService.addData(OLEConstants.BIB_INFO_LIST_FROM_SOLR_RESPONSE, list);
042                    return true;
043                }
044            } catch (Exception e) {
045                e.printStackTrace();
046            }
047        }
048        return false;
049    }
050}