View Javadoc
1   /*
2    * Copyright 2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.sys.web.struts;
17  
18  import java.util.Properties;
19  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.apache.struts.action.ActionForm;
25  import org.apache.struts.action.ActionForward;
26  import org.apache.struts.action.ActionMapping;
27  import org.kuali.ole.sys.OLEConstants;
28  import org.kuali.rice.kns.web.struts.action.KualiAction;
29  import org.kuali.rice.kns.web.struts.form.KualiForm;
30  import org.kuali.rice.krad.util.GlobalVariables;
31  import org.kuali.rice.krad.util.UrlFactory;
32  
33  /**
34   * This class handles Actions for the balance inquiry report menu
35   */
36  public class KualiBalanceInquiryReportMenuAction extends KualiAction {
37      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KualiBalanceInquiryReportMenuAction.class);
38  
39      /**
40       * Entry point to balance inquiry menu, forwards to jsp for rendering.
41       * 
42       * @param mapping
43       * @param form
44       * @param request
45       * @param response
46       * @return ActionForward
47       * @throws Exception
48       */
49      public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
50          return mapping.findForward(OLEConstants.MAPPING_BASIC);
51      }
52  
53      /**
54       * Returns back to calling document.
55       * 
56       * @param mapping
57       * @param form
58       * @param request
59       * @param response
60       * @return ActionForward
61       * @throws Exception
62       */
63      public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
64          KualiBalanceInquiryReportMenuForm balanceInquiryReportMenuForm = (KualiBalanceInquiryReportMenuForm) form;
65  
66          String backUrl = balanceInquiryReportMenuForm.getBackLocation() + "?methodToCall=refresh&docFormKey=" + balanceInquiryReportMenuForm.getDocFormKey();
67          return new ActionForward(backUrl, true);
68      }
69  
70      /**
71       * Needs to overrided to inject the real value into the docFormKey b/c otherwise the lookup's refresh back to this menu
72       * overwrites the original value that we actually need. It too leverages the docFormKey.
73       * 
74       * @param mapping
75       * @param form
76       * @param request
77       * @param response
78       * @return ActionForward
79       * @throws Exception
80       */
81      public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
82          KualiBalanceInquiryReportMenuForm balanceInquiryReportMenuForm = (KualiBalanceInquiryReportMenuForm) form;
83  
84          // need to inject the real value into the docFormKey b/c otherwise the lookup's refresh back to this menu overwrites
85          // the original value that we actually need.
86          balanceInquiryReportMenuForm.setDocFormKey(balanceInquiryReportMenuForm.getBalanceInquiryReportMenuCallerDocFormKey());
87  
88          return mapping.findForward(OLEConstants.MAPPING_BASIC);
89      }
90  
91      /**
92       * Takes care of storing the action form in the user session and forwarding to the balance inquiry lookup action.
93       * 
94       * @param mapping
95       * @param form
96       * @param request
97       * @param response
98       * @return ActionForward
99       * @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(OLEConstants.METHOD_TO_CALL_ATTRIBUTE);
106 
107         // parse out business object class name for lookup
108         String boClassName = StringUtils.substringBetween(fullParameter, OLEConstants.METHOD_TO_CALL_BOPARM_LEFT_DEL, OLEConstants.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, OLEConstants.METHOD_TO_CALL_PARM1_LEFT_DEL, OLEConstants.METHOD_TO_CALL_PARM1_RIGHT_DEL);
116         if (StringUtils.isNotBlank(conversionFields)) {
117             parameters.put(OLEConstants.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, OLEConstants.METHOD_TO_CALL_PARM2_LEFT_DEL, OLEConstants.METHOD_TO_CALL_PARM2_RIGHT_DEL);
122         if (StringUtils.isNotBlank(parameterFields)) {
123             String[] lookupParams = parameterFields.split(OLEConstants.FIELD_CONVERSIONS_SEPERATOR);
124 
125             for (int i = 0; i < lookupParams.length; i++) {
126                 String[] keyValue = lookupParams[i].split(OLEConstants.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, OLEConstants.METHOD_TO_CALL_PARM3_LEFT_DEL, OLEConstants.METHOD_TO_CALL_PARM3_RIGHT_DEL);
141         if (StringUtils.isNotBlank(hideReturnLink)) {
142             parameters.put(OLEConstants.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(OLEConstants.LOOKUP_ANCHOR, ((KualiForm) form).getAnchor());
148         }
149 
150         // determine what the action path is
151         String actionPath = StringUtils.substringBetween(fullParameter, OLEConstants.METHOD_TO_CALL_PARM4_LEFT_DEL, OLEConstants.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(OLEConstants.DISPATCH_REQUEST_PARAMETER, "start");
158         parameters.put(OLEConstants.DOC_FORM_KEY, GlobalVariables.getUserSession().addObjectWithGeneratedKey(form));
159         parameters.put(OLEConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, boClassName);
160         parameters.put(OLEConstants.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 }