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     */
016    package org.kuali.kfs.sys.web.struts;
017    
018    import java.util.Properties;
019    
020    import javax.servlet.http.HttpServletRequest;
021    import javax.servlet.http.HttpServletResponse;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.apache.struts.action.ActionForm;
025    import org.apache.struts.action.ActionForward;
026    import org.apache.struts.action.ActionMapping;
027    import org.kuali.kfs.sys.KFSConstants;
028    import org.kuali.rice.kns.util.GlobalVariables;
029    import org.kuali.rice.kns.util.UrlFactory;
030    import org.kuali.rice.kns.web.struts.action.KualiAction;
031    import org.kuali.rice.kns.web.struts.form.KualiForm;
032    
033    /**
034     * This class handles Actions for the balance inquiry report menu
035     */
036    public class KualiBalanceInquiryReportMenuAction extends KualiAction {
037        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KualiBalanceInquiryReportMenuAction.class);
038    
039        /**
040         * Entry point to balance inquiry menu, forwards to jsp for rendering.
041         * 
042         * @param mapping
043         * @param form
044         * @param request
045         * @param response
046         * @return ActionForward
047         * @throws Exception
048         */
049        public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
050            return mapping.findForward(KFSConstants.MAPPING_BASIC);
051        }
052    
053        /**
054         * Returns back to calling document.
055         * 
056         * @param mapping
057         * @param form
058         * @param request
059         * @param response
060         * @return ActionForward
061         * @throws Exception
062         */
063        public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
064            KualiBalanceInquiryReportMenuForm balanceInquiryReportMenuForm = (KualiBalanceInquiryReportMenuForm) form;
065    
066            String backUrl = balanceInquiryReportMenuForm.getBackLocation() + "?methodToCall=refresh&docFormKey=" + balanceInquiryReportMenuForm.getDocFormKey();
067            return new ActionForward(backUrl, true);
068        }
069    
070        /**
071         * Needs to overrided to inject the real value into the docFormKey b/c otherwise the lookup's refresh back to this menu
072         * overwrites the original value that we actually need. It too leverages the docFormKey.
073         * 
074         * @param mapping
075         * @param form
076         * @param request
077         * @param response
078         * @return ActionForward
079         * @throws Exception
080         */
081        public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
082            KualiBalanceInquiryReportMenuForm balanceInquiryReportMenuForm = (KualiBalanceInquiryReportMenuForm) form;
083    
084            // need to inject the real value into the docFormKey b/c otherwise the lookup's refresh back to this menu overwrites
085            // the original value that we actually need.
086            balanceInquiryReportMenuForm.setDocFormKey(balanceInquiryReportMenuForm.getBalanceInquiryReportMenuCallerDocFormKey());
087    
088            return mapping.findForward(KFSConstants.MAPPING_BASIC);
089        }
090    
091        /**
092         * Takes care of storing the action form in the user session and forwarding to the balance inquiry lookup action.
093         * 
094         * @param mapping
095         * @param form
096         * @param request
097         * @param response
098         * @return ActionForward
099         * @throws Exception
100         */
101        public ActionForward performBalanceInquiryLookup(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
102            String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
103    
104            // parse out the important strings from our methodToCall parameter
105            String fullParameter = (String) request.getAttribute(KFSConstants.METHOD_TO_CALL_ATTRIBUTE);
106    
107            // parse out business object class name for lookup
108            String boClassName = StringUtils.substringBetween(fullParameter, KFSConstants.METHOD_TO_CALL_BOPARM_LEFT_DEL, KFSConstants.METHOD_TO_CALL_BOPARM_RIGHT_DEL);
109            if (StringUtils.isBlank(boClassName)) {
110                throw new RuntimeException("Illegal call to perform lookup, no business object class name specified.");
111            }
112    
113            // build the parameters for the lookup url
114            Properties parameters = new Properties();
115            String conversionFields = StringUtils.substringBetween(fullParameter, KFSConstants.METHOD_TO_CALL_PARM1_LEFT_DEL, KFSConstants.METHOD_TO_CALL_PARM1_RIGHT_DEL);
116            if (StringUtils.isNotBlank(conversionFields)) {
117                parameters.put(KFSConstants.CONVERSION_FIELDS_PARAMETER, conversionFields);
118            }
119    
120            // pass values from form that should be pre-populated on lookup search
121            String parameterFields = StringUtils.substringBetween(fullParameter, KFSConstants.METHOD_TO_CALL_PARM2_LEFT_DEL, KFSConstants.METHOD_TO_CALL_PARM2_RIGHT_DEL);
122            if (StringUtils.isNotBlank(parameterFields)) {
123                String[] lookupParams = parameterFields.split(KFSConstants.FIELD_CONVERSIONS_SEPERATOR);
124    
125                for (int i = 0; i < lookupParams.length; i++) {
126                    String[] keyValue = lookupParams[i].split(KFSConstants.FIELD_CONVERSION_PAIR_SEPERATOR);
127    
128                    // hard-coded passed value
129                    if (StringUtils.contains(keyValue[0], "'")) {
130                        parameters.put(keyValue[1], StringUtils.replace(keyValue[0], "'", ""));
131                    }
132                    // passed value should come from property
133                    else if (StringUtils.isNotBlank(request.getParameter(keyValue[0]))) {
134                        parameters.put(keyValue[1], request.getParameter(keyValue[0]));
135                    }
136                }
137            }
138    
139            // grab whether or not the "return value" link should be hidden or not
140            String hideReturnLink = StringUtils.substringBetween(fullParameter, KFSConstants.METHOD_TO_CALL_PARM3_LEFT_DEL, KFSConstants.METHOD_TO_CALL_PARM3_RIGHT_DEL);
141            if (StringUtils.isNotBlank(hideReturnLink)) {
142                parameters.put(KFSConstants.HIDE_LOOKUP_RETURN_LINK, hideReturnLink);
143            }
144    
145            // anchor, if it exists
146            if (form instanceof KualiForm && StringUtils.isNotEmpty(((KualiForm) form).getAnchor())) {
147                parameters.put(KFSConstants.LOOKUP_ANCHOR, ((KualiForm) form).getAnchor());
148            }
149    
150            // determine what the action path is
151            String actionPath = StringUtils.substringBetween(fullParameter, KFSConstants.METHOD_TO_CALL_PARM4_LEFT_DEL, KFSConstants.METHOD_TO_CALL_PARM4_RIGHT_DEL);
152            if (StringUtils.isBlank(actionPath)) {
153                throw new IllegalStateException("The \"actionPath\" attribute is an expected parameter for the <gl:balanceInquiryLookup> tag - it " + "should never be blank.");
154            }
155    
156            // now add required parameters
157            parameters.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, "start");
158            parameters.put(KFSConstants.DOC_FORM_KEY, GlobalVariables.getUserSession().addObject(form));
159            parameters.put(KFSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, boClassName);
160            parameters.put(KFSConstants.RETURN_LOCATION_PARAMETER, basePath + mapping.getPath() + ".do");
161    
162            String lookupUrl = UrlFactory.parameterizeUrl(basePath + "/" + actionPath, parameters);
163    
164            return new ActionForward(lookupUrl, true);
165        }
166    }