001package org.kuali.ole.deliver.lookup;
002
003import org.apache.commons.lang.StringUtils;
004import org.kuali.ole.OLEConstants;
005import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
006import org.kuali.ole.deliver.service.OleDeliverRequestDocumentHelperServiceImpl;
007import org.kuali.ole.sys.context.SpringContext;
008import org.kuali.ole.util.DocstoreUtil;
009import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
010import org.kuali.rice.krad.lookup.LookupableImpl;
011import org.kuali.rice.krad.service.KRADServiceLocator;
012import org.kuali.rice.krad.uif.UifConstants;
013import org.kuali.rice.krad.uif.UifParameters;
014import org.kuali.rice.krad.uif.view.LookupView;
015import org.kuali.rice.krad.util.GlobalVariables;
016import org.kuali.rice.krad.util.KRADConstants;
017import org.kuali.rice.krad.util.KRADUtils;
018import org.kuali.rice.krad.util.UrlFactory;
019import org.kuali.rice.krad.web.form.LookupForm;
020
021import java.util.*;
022
023/**
024 * Created with IntelliJ IDEA.
025 * User: ?
026 * Date: 10/11/12
027 * Time: 4:01 PM
028 * To change this template use File | Settings | File Templates.
029 */
030
031public class OleDeliverRequestLookupableImpl extends LookupableImpl {
032    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleDeliverRequestLookupableImpl.class);
033    private OleDeliverRequestDocumentHelperServiceImpl oleDeliverRequestDocumentHelperService = new OleDeliverRequestDocumentHelperServiceImpl();
034    private String itemTitle;
035    //private String borrowerName;
036    private String firstName;
037    private String lastName;
038    private String itemId;
039    private String borrowerId;
040    private String borrowerBarcode;
041    private DocstoreUtil docstoreUtil;
042    public DocstoreUtil getDocstoreUtil() {
043
044        if (docstoreUtil == null) {
045            docstoreUtil = SpringContext.getBean(DocstoreUtil.class);
046
047        }
048        return docstoreUtil;
049    }
050
051
052    @Override
053    protected String getActionUrlHref(LookupForm lookupForm, Object dataObject, String methodToCall,
054                                      List<String> pkNames) {
055        LOG.debug("Inside getActionUrlHref()");
056        LookupView lookupView = (LookupView) lookupForm.getView();
057
058        Properties props = new Properties();
059        props.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, methodToCall);
060        Map<String, String> primaryKeyValues = KRADUtils.getPropertyKeyValuesFromDataObject(pkNames, dataObject);
061        for (String primaryKey : primaryKeyValues.keySet()) {
062            String primaryKeyValue = primaryKeyValues.get(primaryKey);
063
064            props.put(primaryKey, primaryKeyValue);
065        }
066
067        if (StringUtils.isNotBlank(lookupForm.getReturnLocation())) {
068            props.put(KRADConstants.RETURN_LOCATION_PARAMETER, lookupForm.getReturnLocation());
069        }
070
071        props.put(UifParameters.DATA_OBJECT_CLASS_NAME, OleDeliverRequestBo.class.getName());
072        props.put(UifParameters.VIEW_TYPE_NAME, UifConstants.ViewType.MAINTENANCE.name());
073
074        String maintenanceMapping = OLEConstants.OleDeliverRequest.DELIVER_REQUEST_MAINTENANCE_ACTION_LINK;
075
076        return UrlFactory.parameterizeUrl(maintenanceMapping, props);
077    }
078
079
080    @Override
081    public Collection<?> performSearch(LookupForm form, Map<String, String> searchCriteria, boolean bounded) {
082        LOG.debug("Inside performSearch()");
083        Map<String, String> lookupcriteria = form.getLookupCriteria();
084        HashMap<String, String> requestParameters = (HashMap<String, String>) form.getInitialRequestParameters();
085        if (requestParameters.containsKey(OLEConstants.OleDeliverRequest.FLAG)) {
086            if (requestParameters.get(OLEConstants.OleDeliverRequest.FLAG).equalsIgnoreCase("true")) {
087                String id = requestParameters.get(OLEConstants.OleDeliverRequest.ITEM_ID);
088                searchCriteria.put(OLEConstants.OleDeliverRequest.ITEM_ID, id);
089                lookupcriteria.put(OLEConstants.OleDeliverRequest.ITEM_ID, id);
090                form.setLookupCriteria(lookupcriteria);
091            }
092        }
093        itemTitle = searchCriteria.get(OLEConstants.TITLE) != null ? searchCriteria.get(OLEConstants.TITLE) : "";
094        //borrowerName = searchCriteria.get(OLEConstants.BORROWER_NAME);
095        firstName = searchCriteria.get(OLEConstants.OlePatron.PATRON_FIRST_NAME) != null ? searchCriteria.get(OLEConstants.OlePatron.PATRON_FIRST_NAME) : "";
096        lastName = searchCriteria.get(OLEConstants.OlePatron.PATRON_LAST_NAME) != null ? searchCriteria.get(OLEConstants.OlePatron.PATRON_LAST_NAME) : "";
097        itemId = searchCriteria.get(OLEConstants.OleDeliverRequest.ITEM_ID) != null ? searchCriteria.get(OLEConstants.OleDeliverRequest.ITEM_ID) : "";
098        borrowerId = searchCriteria.get(OLEConstants.OleDeliverRequest.BORROWER_ID) != null ? searchCriteria.get(OLEConstants.OleDeliverRequest.BORROWER_ID) : "";
099        borrowerBarcode = searchCriteria.get(OLEConstants.OleDeliverRequest.BORROWER_BARCODE) != null ? searchCriteria.get(OLEConstants.OleDeliverRequest.BORROWER_BARCODE) : "";
100       /* firstName = searchCriteria.get(OLEConstants.OlePatron.PATRON_FIRST_NAME);
101        lastName = searchCriteria.get(OLEConstants.OlePatron.PATRON_LAST_NAME);
102        itemId = searchCriteria.get(OLEConstants.OleDeliverRequest.ITEM_ID);
103        borrowerId = searchCriteria.get(OLEConstants.OleDeliverRequest.BORROWER_ID);
104        borrowerBarcode = searchCriteria.get(OLEConstants.OleDeliverRequest.BORROWER_BARCODE);*/
105        searchCriteria.remove(OLEConstants.TITLE);
106        searchCriteria.remove(OLEConstants.OlePatron.PATRON_FIRST_NAME);
107        searchCriteria.remove(OLEConstants.OlePatron.PATRON_LAST_NAME);
108        List<OleDeliverRequestBo> displayList = new ArrayList<OleDeliverRequestBo>();
109        // List<OlePatronDocument> olePatronDocumentList = new ArrayList<OlePatronDocument>();
110        List<EntityNameBo> entityNameBos = new ArrayList<EntityNameBo>();
111        //String modifiedBorrowerName = borrowerName.trim();
112        String modifiedBorrowerFirstName = firstName.trim();
113        String modifiedBorrowerLastName = lastName.trim();
114       /* String[] name = borrowerName.split(" ");
115        if (name.length > 0) {
116            modifiedBorrowerName = name[0];
117        }*/
118
119        if ((borrowerBarcode.equals("")) && (!modifiedBorrowerFirstName.equals("") || !modifiedBorrowerLastName.equals(""))) {
120            Map<String, String> patronMap = new HashMap<String, String>();
121            if (!modifiedBorrowerFirstName.equals("") && modifiedBorrowerFirstName != null) {
122                patronMap.put(OLEConstants.OlePatron.PATRON_FIRST_NAME, modifiedBorrowerFirstName);
123            }
124            if (!modifiedBorrowerLastName.equals("") && modifiedBorrowerLastName != null) {
125                patronMap.put(OLEConstants.OlePatron.PATRON_LAST_NAME, modifiedBorrowerLastName);
126            }
127            //entityNameBos = (List<EntityNameBo>) KRADServiceLocator.getBusinessObjectService().findMatching(EntityNameBo.class, patronMap);
128            entityNameBos = (List<EntityNameBo>) getLookupService().findCollectionBySearchHelper(EntityNameBo.class, patronMap, true);
129
130            if (entityNameBos.size() == 0) {
131                GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.NO_RECORD_FOUND);
132            }
133            if (entityNameBos.size() > 0) {
134                for (int i = 0; i < entityNameBos.size(); i++) {
135
136                    searchCriteria.put(OLEConstants.OleDeliverRequest.BORROWER_ID, entityNameBos.get(i).getEntityId());
137                    displayList.addAll((List<OleDeliverRequestBo>) super.performSearch(form, searchCriteria, bounded));
138                    searchCriteria.remove(OLEConstants.OleDeliverRequest.BORROWER_ID);
139                }
140            }
141        } else {
142            displayList = (List<OleDeliverRequestBo>) super.performSearch(form, searchCriteria, bounded);
143        }
144        searchCriteria.put(OLEConstants.TITLE, itemTitle);
145        //searchCriteria.put(OLEConstants.BORROWER_NAME, borrowerName);
146        searchCriteria.put(OLEConstants.OlePatron.PATRON_FIRST_NAME, firstName);
147        searchCriteria.put(OLEConstants.OlePatron.PATRON_LAST_NAME, lastName);
148
149        OleDeliverRequestBo oleDeliverRequestBo;
150        List<OleDeliverRequestBo> oleDeliverRequestBoList = new ArrayList<OleDeliverRequestBo>();
151        try {
152            for (int i = 0; i < displayList.size(); i++) {
153                displayList.get(i).setItemType(null);
154                oleDeliverRequestBo = displayList.get(i);
155
156                getDocstoreUtil().isItemAvailableInDocStore(oleDeliverRequestBo);
157                if (itemTitle != null && !itemTitle.isEmpty() && !oleDeliverRequestBo.getTitle().toUpperCase().equals(itemTitle.toUpperCase())) {
158                    // displayList.remove(i);
159                } else {
160                    Map<String, String> patronMap = new HashMap<String, String>();
161                    patronMap.put("entityId",oleDeliverRequestBo.getBorrowerId());
162                    entityNameBos = (List<EntityNameBo>) KRADServiceLocator.getBusinessObjectService().findMatching(EntityNameBo.class, patronMap);
163                    if (entityNameBos.size() > 0) {
164                        displayList.get(i).setFirstName(entityNameBos.get(0).getFirstName());
165                        displayList.get(i).setLastName(entityNameBos.get(0).getLastName());
166                    }
167                    oleDeliverRequestBoList.add(displayList.get(i));
168                }
169
170            }
171        } catch (Exception e) {
172            LOG.error("Not able to retrieve data:" + e.getMessage(), e);
173        }
174        if (oleDeliverRequestBoList.size() == 0) {
175            GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.NO_RECORD_FOUND);
176        }
177
178        return oleDeliverRequestBoList;
179    }
180
181}
182
183