View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.bc.document.web.struts;
20  
21  import java.util.Properties;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  import javax.servlet.http.HttpSession;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.apache.struts.action.ActionForm;
29  import org.apache.struts.action.ActionForward;
30  import org.apache.struts.action.ActionMapping;
31  import org.kuali.kfs.module.bc.BCConstants;
32  import org.kuali.kfs.module.bc.BCPropertyConstants;
33  import org.kuali.kfs.sys.KFSConstants;
34  import org.kuali.rice.kns.util.KNSGlobalVariables;
35  import org.kuali.rice.kns.util.MessageList;
36  import org.kuali.rice.kns.web.struts.action.KualiAction;
37  import org.kuali.rice.krad.util.GlobalVariables;
38  import org.kuali.rice.krad.util.MessageMap;
39  import org.kuali.rice.krad.util.UrlFactory;
40  
41  /**
42   * Handles close action to implement Budget return to caller (expansion screen) flow.
43   */
44  public class BudgetExpansionAction extends KualiAction {
45  
46      /**
47       * @see org.kuali.rice.kns.web.struts.action.KualiAction#execute(org.apache.struts.action.ActionMapping,
48       *      org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
49       */
50      @Override
51      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
52  
53          BudgetExpansionForm budgetExpansionForm = (BudgetExpansionForm) form;
54  
55          return super.execute(mapping, form, request, response);
56      }
57  
58      /**
59       * Handling for screen close. Default action is return to caller.
60       *
61       * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm,
62       *      javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
63       */
64      public ActionForward close(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
65          return returnToCaller(mapping, form, request, response);
66      }
67  
68      /**
69       * @see org.kuali.rice.kns.web.struts.action.KualiAction#refresh(org.apache.struts.action.ActionMapping,
70       *      org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
71       */
72      @Override
73      public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
74          this.moveCallBackMessagesInPlace();
75          this.removeCallBackMessagesObjectFromSession();
76  
77          return super.refresh(mapping, form, request, response);
78      }
79  
80      /**
81       * Return to form's back location (usually previous screen). Returns back the form key that was passed in for the previous form
82       * and any previous anchor position. Default refresh method is executed.
83       *
84       * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm,
85       *      javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
86       */
87      public ActionForward returnToCaller(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
88          BudgetExpansionForm budgetExpansionForm = (BudgetExpansionForm) form;
89  
90          // if this form is session scoped remove it
91          this.cleanupAnySessionForm(mapping, request);
92  
93          Properties parameters = new Properties();
94          parameters.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, BCConstants.BC_SELECTION_REFRESH_METHOD);
95          parameters.put(KFSConstants.DOC_FORM_KEY, budgetExpansionForm.getReturnFormKey());
96  
97          if (StringUtils.isNotEmpty(budgetExpansionForm.getReturnAnchor())) {
98              parameters.put(KFSConstants.ANCHOR, budgetExpansionForm.getReturnAnchor());
99          }
100         parameters.put(KFSConstants.REFRESH_CALLER, this.getClass().getName());
101 
102         this.addCallBackMessagesAsObjectInSession(budgetExpansionForm);
103 
104         String backUrl = UrlFactory.parameterizeUrl(budgetExpansionForm.getBackLocation(), parameters);
105         return new ActionForward(backUrl, true);
106     }
107 
108     /**
109      * add the callback messages and error messages as objects in session variable
110      */
111     public void addCallBackMessagesAsObjectInSession(BudgetExpansionForm budgetExpansionForm) {
112         if (!budgetExpansionForm.getCallBackMessages().isEmpty()) {
113             GlobalVariables.getUserSession().addObject(BCPropertyConstants.CALL_BACK_MESSAGES, budgetExpansionForm.getCallBackMessages());
114         }
115 
116         if (budgetExpansionForm.getCallBackErrors().hasErrors()) {
117             GlobalVariables.getUserSession().addObject(BCPropertyConstants.CALL_BACK_ERRORS, budgetExpansionForm.getCallBackErrors());
118         }
119     }
120 
121     /**
122      * remove the objects that hold the callback messages and error messages from session variable
123      */
124     public void removeCallBackMessagesObjectFromSession() {
125         GlobalVariables.getUserSession().removeObject(BCPropertyConstants.CALL_BACK_MESSAGES);
126         GlobalVariables.getUserSession().removeObject(BCPropertyConstants.CALL_BACK_ERRORS);
127     }
128 
129     /**
130      * move the callback messages and error messages in place
131      */
132     public void moveCallBackMessagesInPlace() {
133         MessageList messagesList = (MessageList) GlobalVariables.getUserSession().retrieveObject(BCPropertyConstants.CALL_BACK_MESSAGES);
134         if (messagesList != null) {
135             KNSGlobalVariables.getMessageList().addAll(messagesList);
136         }
137 
138         MessageMap messageMap = (MessageMap) GlobalVariables.getUserSession().retrieveObject(BCPropertyConstants.CALL_BACK_ERRORS);
139         if (messageMap != null) {
140             GlobalVariables.setMessageMap(messageMap);
141         }
142     }
143 
144     /**
145      * remove any session form attribute
146      *
147      * @param mapping
148      * @param request
149      */
150     public void cleanupAnySessionForm(ActionMapping mapping, HttpServletRequest request) {
151         if (BCConstants.MAPPING_SCOPE_SESSION.equals(mapping.getScope())) {
152             HttpSession sess = request.getSession(Boolean.FALSE);
153             String formName = mapping.getAttribute();
154             sess.removeAttribute(formName);
155         }
156     }
157 }