001/*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.ole.select.document.web.struts;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.struts.action.ActionForm;
020import org.apache.struts.action.ActionForward;
021import org.apache.struts.action.ActionMapping;
022import org.kuali.ole.select.businessobject.OleAcquisitionSearchResult;
023import org.kuali.ole.select.document.OleAcquisitionsSearchDocument;
024import org.kuali.ole.sys.OLEConstants;
025import org.kuali.ole.sys.OLEKeyConstants;
026import org.kuali.rice.core.api.util.RiceConstants;
027import org.kuali.rice.kns.web.struts.action.KualiTransactionalDocumentActionBase;
028import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
029import org.kuali.rice.krad.util.GlobalVariables;
030import org.kuali.rice.krad.util.KRADConstants;
031
032import javax.servlet.http.HttpServletRequest;
033import javax.servlet.http.HttpServletResponse;
034import java.util.ArrayList;
035import java.util.List;
036
037/**
038 * This class is the Action Class for Acquisitions Search
039 */
040public class OleAcquisitionsSearchAction extends KualiTransactionalDocumentActionBase {
041
042    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleAcquisitionsSearchAction.class);
043
044    /**
045     * This method returns the list of requisition items that satisfy the search criteria
046     * input from Acquisitions Search page.
047     *
048     * @param mapping
049     * @param form
050     * @param request
051     * @param response
052     * @return
053     * @throws Exception
054     */
055    public ActionForward search(ActionMapping mapping, ActionForm form,
056                                HttpServletRequest request, HttpServletResponse response) throws Exception {
057        LOG.debug("Inside searchRequisitions of OleAcquisitionsSearchAction");
058        Long begin = System.currentTimeMillis();
059        OleAcquisitionsSearchForm acqForm = (OleAcquisitionsSearchForm) form;
060        OleAcquisitionsSearchDocument acqDoc = (OleAcquisitionsSearchDocument) acqForm.getDocument();
061
062        // Modified for Jira Ole-2563 by Aditya Starts
063
064        boolean isToDateLesserThanFromDate;
065        isToDateLesserThanFromDate = acqDoc.isToDateLesserThanFromDate();
066        if (!isToDateLesserThanFromDate) {
067            List<OleAcquisitionSearchResult> displayList = acqDoc.searchResults();
068            if (displayList.size() > 0) {
069                acqDoc.setAcqSearchResults(displayList);
070            } else {
071                acqDoc.setAcqSearchResults(new ArrayList<OleAcquisitionSearchResult>());
072                GlobalVariables.getMessageMap().clearErrorMessages();
073                GlobalVariables.getMessageMap().putInfo(OLEConstants.OrderQueue.REQUISITIONS, OLEKeyConstants.ERROR_NO_RESULTS_FOUND);
074            }
075        } else {
076            GlobalVariables.getMessageMap().putError(OLEConstants.OrderQueue.REQUISITIONS, OLEKeyConstants.ERROR_DATE_TO_NOT_LESSER_THAN_DATE_FROM, new String[]{});
077        }
078        Long end = System.currentTimeMillis();
079        Long timeTaken = end-begin;
080        LOG.info("The Time Taken for Fetching Acquisition Search : "+timeTaken);
081
082        // Modified for Jira Ole-2563 by Aditya Ends
083
084        //request.setAttribute(OLEConstants.OrderQueue.CONSTANTS, new JSTLConstants(KewApiConstants.class));
085        return mapping.findForward(RiceConstants.MAPPING_BASIC);
086    }
087
088    /**
089     * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#cancel(org.apache.struts.action.ActionMapping,
090     *      org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
091     */
092    @Override
093    public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
094        KualiDocumentFormBase kualiDocumentFormBase = (KualiDocumentFormBase) form;
095        return returnToSender(request, mapping, kualiDocumentFormBase);
096    }
097
098    /**
099     * This method clears the search criteria's that is on the Acquisitions Search Page
100     *
101     * @param mapping
102     * @param form
103     * @param request
104     * @param response
105     * @return
106     * @throws Exception
107     */
108    public ActionForward clear(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
109        OleAcquisitionsSearchForm searchForm = (OleAcquisitionsSearchForm) form;
110        OleAcquisitionsSearchDocument oleAcqSearchDoc = (OleAcquisitionsSearchDocument) searchForm.getDocument();
111        oleAcqSearchDoc.setDocumentType(null);
112        oleAcqSearchDoc.setDocNumber(null);
113        oleAcqSearchDoc.setPurapDocumentIdentifier(null);
114        oleAcqSearchDoc.setVendorName(null);
115        oleAcqSearchDoc.setDateFrom(null);
116        oleAcqSearchDoc.setDateTo(null);
117        oleAcqSearchDoc.setInitiator(null);
118        oleAcqSearchDoc.setRequestorName(null);
119        oleAcqSearchDoc.setAccountNumber(null);
120        oleAcqSearchDoc.setOrganizationCode(null);
121        oleAcqSearchDoc.setChartOfAccountsCode(null);
122        oleAcqSearchDoc.setTitle(null);
123        oleAcqSearchDoc.setPublisher(null);
124        oleAcqSearchDoc.setIsbn(null);
125        oleAcqSearchDoc.setAuthor(null);
126        oleAcqSearchDoc.setLocalIdentifier(null);
127        oleAcqSearchDoc.setAcqSearchResults(new ArrayList<OleAcquisitionSearchResult>());
128        return mapping.findForward(RiceConstants.MAPPING_BASIC);
129    }
130
131    /**
132     * Takes care of storing the action form in the User session and forwarding to the prlookup action.
133     *
134     * @param mapping
135     * @param form
136     * @param request
137     * @param response
138     * @return
139     * @throws Exception
140     */
141    public ActionForward performPRLookup(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
142
143        LOG.debug("Inside performPRLookup of OleAcquisitionsSearchAction");
144        ActionForward forward = super.performLookup(mapping, form, request, response);
145        String path = forward.getPath();
146        if (path.contains("kr/" + KRADConstants.LOOKUP_ACTION)) {
147            path = path.replace("kr/" + KRADConstants.LOOKUP_ACTION, OLEConstants.PR_LOOKUP_ACTION);
148        } else if (path.contains(KRADConstants.LOOKUP_ACTION)) {
149            path = path.replace(KRADConstants.LOOKUP_ACTION, OLEConstants.PR_LOOKUP_ACTION);
150        }
151        forward.setPath(path);
152
153        LOG.debug("Leaving performPRLookup of OleAcquisitionsSearchAction");
154        return forward;
155
156    }
157
158    /**
159     * This method... find the List Of All POs search for batch upload
160     * @param mapping
161     * @param form
162     * @param request
163     * @param response
164     * @return
165     * @throws Exception
166     */
167    public ActionForward docHandler(ActionMapping mapping, ActionForm form,
168                                    HttpServletRequest request, HttpServletResponse response) throws Exception {
169        LOG.debug("Inside searchRequisitions of OleAcquisitionsSearchAction");
170        ActionForward af = super.docHandler(mapping, form, request, response);
171        String loadSumId = ((OleAcquisitionsSearchForm) form).getLoadSumId();
172        if (StringUtils.isNotEmpty(loadSumId)) {
173            OleAcquisitionsSearchDocument acqDoc = (OleAcquisitionsSearchDocument) ((OleAcquisitionsSearchForm) form).getDocument();
174            List<OleAcquisitionSearchResult> displayList = acqDoc.listOfPOsSearchResults(loadSumId);
175            if (displayList.size() > 0) {
176                acqDoc.setAcqSearchResults(displayList);
177            } else {
178                acqDoc.setAcqSearchResults(new ArrayList<OleAcquisitionSearchResult>());
179                GlobalVariables.getMessageMap().putInfo(OLEConstants.OrderQueue.REQUISITIONS, OLEKeyConstants.ERROR_NO_RESULTS_FOUND);
180            }
181            ((OleAcquisitionsSearchForm) form).setLoadSumId(null);
182        }
183        return af;
184
185    }
186
187
188    @Override
189    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
190        ActionForward actionForward = null;
191        if(((OleAcquisitionsSearchForm) form).getMethodToCall() != null && ((OleAcquisitionsSearchForm) form).getMethodToCall().equalsIgnoreCase("docHandler")){
192            ((OleAcquisitionsSearchForm) form).setDocId(null);
193        }
194        return super.execute(mapping, form, request, response);
195    }
196
197}