001package org.kuali.ole.select.lookup;
002
003import org.apache.commons.lang.StringUtils;
004import org.kuali.ole.OLEConstants;
005import org.kuali.ole.select.bo.OleLicenseRequestBo;
006import org.kuali.ole.service.OleLicenseRequestService;
007import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
008import org.kuali.rice.krad.lookup.LookupableImpl;
009import org.kuali.rice.krad.uif.UifConstants;
010import org.kuali.rice.krad.uif.UifParameters;
011import org.kuali.rice.krad.uif.view.LookupView;
012import org.kuali.rice.krad.util.GlobalVariables;
013import org.kuali.rice.krad.util.KRADConstants;
014import org.kuali.rice.krad.util.KRADUtils;
015import org.kuali.rice.krad.util.UrlFactory;
016import org.kuali.rice.krad.web.form.LookupForm;
017
018import java.text.SimpleDateFormat;
019import java.util.*;
020
021/**
022 * OleLicenseRequestLookupableImpl is the view helper service class for Ole License Request Document
023 */
024public class OleLicenseRequestLookupableImpl extends LookupableImpl {
025
026    private OleLicenseRequestService oleLicenseRequestService;
027
028    private String returnLocation = "";
029
030    /**
031     * Gets the returnLocation attribute.
032     *
033     * @return Returns the returnLocation
034     */
035    public String getReturnLocation() {
036        return returnLocation;
037    }
038
039    /**
040     * Sets the returnLocation attribute value.
041     *
042     * @param returnLocation The returnLocation to set.
043     */
044    public void setReturnLocation(String returnLocation) {
045        this.returnLocation = returnLocation;
046    }
047
048    /**
049     * This method returns action URL as oleLicenseRequest.
050     *
051     * @param lookupForm
052     * @param dataObject
053     * @param methodToCall
054     * @param pkNames
055     * @return String
056     */
057    @Override
058    protected String getActionUrlHref(LookupForm lookupForm, Object dataObject, String methodToCall,
059                                      List<String> pkNames) {
060        LookupView lookupView = (LookupView) lookupForm.getView();
061
062        Properties props = new Properties();
063        props.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, methodToCall);
064
065        Map<String, String> primaryKeyValues = KRADUtils.getPropertyKeyValuesFromDataObject(pkNames, dataObject);
066        for (String primaryKey : primaryKeyValues.keySet()) {
067            String primaryKeyValue = primaryKeyValues.get(primaryKey);
068
069            props.put(primaryKey, primaryKeyValue);
070        }
071
072        if (StringUtils.isNotBlank(lookupForm.getReturnLocation())) {
073            props.put(KRADConstants.RETURN_LOCATION_PARAMETER, lookupForm.getReturnLocation());
074            returnLocation = lookupForm.getReturnLocation();
075        }
076
077        props.put(UifParameters.DATA_OBJECT_CLASS_NAME, lookupForm.getDataObjectClassName());
078        props.put(UifParameters.VIEW_TYPE_NAME, UifConstants.ViewType.MAINTENANCE.name());
079
080        String maintenanceMapping = "oleLicenseRequest";
081
082        return UrlFactory.parameterizeUrl(maintenanceMapping, props);
083    }
084
085    /**
086     * This method performs the search for license request based on the search criterias
087     *
088     * @param form
089     * @param criteria
090     * @param bounded
091     * @return
092     */
093    @Override
094    public Collection<?> performSearch(LookupForm form, Map<String, String> criteria, boolean bounded) {
095        returnLocation = form.getReturnLocation();
096        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
097        String createdDateFrom = criteria.get(OLEConstants.OleLicenseRequest.CREATED_FROM_DATE);
098        String createdDateTo = criteria.get(OLEConstants.OleLicenseRequest.CREATED_TO_DATE);
099        String lastModifiedDateFrom = criteria.get(OLEConstants.OleLicenseRequest.LAST_MOD_FROM_DATE);
100        String lastModifiedDateTo = criteria.get(OLEConstants.OleLicenseRequest.LAST_MOD_TO_DATE);
101        /*String bibliographicTitle = criteria.get(OLEConstants.OleLicenseRequest.BIB_TITLE);
102        String licenseRequestStatusCode = criteria.get(OLEConstants.OleLicenseRequest.STATUS_CODE);
103        String assignee = criteria.get(OLEConstants.OleLicenseRequest.ASSIGNEE);
104        String locationId = criteria.get(OLEConstants.OleLicenseRequest.LOCATION_ID);
105        String licenseRequestTypeId = criteria.get(OLEConstants.OleLicenseRequest.LICENSE_REQUEST_TYPE_ID) ;*/
106
107        if (!createdDateFrom.equalsIgnoreCase("") && !createdDateTo.equalsIgnoreCase("")) {
108            if (createdDateFrom.compareTo(createdDateTo) > 0) {
109                GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OleLicenseRequest.ERROR_DATE_FROM_EXCEEDS_DATE_TO);
110                return Collections.emptyList();
111            }
112        }
113        if (!lastModifiedDateFrom.equalsIgnoreCase("") && !lastModifiedDateTo.equalsIgnoreCase("")) {
114            if (lastModifiedDateFrom.compareTo(lastModifiedDateTo) > 0) {
115                GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OleLicenseRequest.ERROR_DATE_FROM_EXCEEDS_DATE_TO);
116                return Collections.emptyList();
117            }
118        }/*else if (bibliographicTitle.isEmpty() && licenseRequestStatusCode.isEmpty() && assignee.isEmpty() &&
119                locationId.isEmpty() && licenseRequestTypeId.isEmpty() && createdDateFrom.equalsIgnoreCase("") && createdDateTo.equalsIgnoreCase("")) {
120                createdDateFrom = dateFormat.format(new Date());
121                criteria.put(OLEConstants.OleLicenseRequest.CREATED_FROM_DATE,createdDateFrom);
122            }*/
123        Collection<?> displayList = null;
124        try {
125            displayList = getOleLicenseRequestservice().findLicenseRequestByCriteria(criteria);
126        } catch (Exception e) {
127            e.printStackTrace();
128            throw new RuntimeException(e);
129        }
130        if (displayList.size() == 0) {
131            GlobalVariables.getMessageMap().putInfo(OLEConstants.OleLicenseRequest.ERROR_NOT_FOUND, OLEConstants.OleLicenseRequest.ERROR_NOT_FOUND);
132        }
133        return displayList;
134
135    }
136
137
138    /**
139     * This method returns the url for viewing the Requisition Document
140     *
141     * @param object
142     * @return url
143     */
144    public String viewParticularDocument(Object object) {
145        OleLicenseRequestBo oleLicenseRequestBo = (OleLicenseRequestBo) object;
146        String url = "";
147        if (returnLocation != null & !returnLocation.isEmpty())
148            url = returnLocation.substring(0, returnLocation.indexOf("portal.do"));
149        return url + "kew/DocHandler.do?command=displayDocSearchView&docId=" + oleLicenseRequestBo.getDocumentNumber();
150    }
151
152    /**
153     * This method returns the OleLicenseRequestService object
154     *
155     * @return
156     */
157    public OleLicenseRequestService getOleLicenseRequestservice() {
158        if (oleLicenseRequestService == null) {
159            oleLicenseRequestService = GlobalResourceLoader.getService("oleLicenseRequestService");
160        }
161        return oleLicenseRequestService;
162    }
163
164}