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.module.bc.document.web.struts;
017    
018    import java.util.Properties;
019    
020    import javax.servlet.http.HttpServletRequest;
021    import javax.servlet.http.HttpServletResponse;
022    import javax.servlet.http.HttpSession;
023    
024    import org.apache.commons.lang.StringUtils;
025    import org.apache.struts.action.ActionForm;
026    import org.apache.struts.action.ActionForward;
027    import org.apache.struts.action.ActionMapping;
028    import org.kuali.kfs.module.bc.BCConstants;
029    import org.kuali.kfs.module.bc.BCKeyConstants;
030    import org.kuali.kfs.module.bc.BCPropertyConstants;
031    import org.kuali.kfs.sys.KFSConstants;
032    import org.kuali.rice.kns.util.ErrorMap;
033    import org.kuali.rice.kns.util.GlobalVariables;
034    import org.kuali.rice.kns.util.MessageList;
035    import org.kuali.rice.kns.util.UrlFactory;
036    import org.kuali.rice.kns.web.struts.action.KualiAction;
037    
038    /**
039     * Handles close action to implement Budget return to caller (expansion screen) flow.
040     */
041    public class BudgetExpansionAction extends KualiAction {
042    
043        /**
044         * @see org.kuali.rice.kns.web.struts.action.KualiAction#execute(org.apache.struts.action.ActionMapping,
045         *      org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
046         */
047        @Override
048        public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
049    
050            BudgetExpansionForm budgetExpansionForm = (BudgetExpansionForm) form;
051    
052            // check for session timeout if not initial (re)load of BC selection
053            if (!(mapping.getType().endsWith("BudgetConstructionSelectionAction") && (budgetExpansionForm.getMethodToCall().equals(BCConstants.LOAD_EXPANSION_SCREEN_METHOD) || budgetExpansionForm.getMethodToCall().equals(BCConstants.LOAD_EXPANSION_SCREEN_METHOD_SESSION_TIMEOUT)))) {
054    
055                Boolean isBCHeartBeating = (Boolean) GlobalVariables.getUserSession().retrieveObject(BCConstants.BC_HEARTBEAT_SESSIONFLAG);
056                if (isBCHeartBeating == null) {
057                    budgetExpansionForm.setLostSession(Boolean.TRUE);
058                    
059                    if (form instanceof SalarySettingBaseForm){
060                        SalarySettingBaseForm salarySettingBaseForm = (SalarySettingBaseForm) form;
061                        if (!salarySettingBaseForm.isBudgetByAccountMode()){
062                            GlobalVariables.getMessageList().add(BCKeyConstants.MESSAGE_BUDGET_PREVIOUS_SESSION_TIMEOUT);
063                            return mapping.findForward(BCConstants.MAPPING_ORGANIZATION_SALARY_SETTING_RETURNING);
064                        }
065                    }
066                    if (budgetExpansionForm.isMainWindow()){
067                        Properties parameters = new Properties();
068                        parameters.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, BCConstants.LOAD_EXPANSION_SCREEN_METHOD_SESSION_TIMEOUT);
069    
070                        String lookupUrl = UrlFactory.parameterizeUrl("/" + BCConstants.BC_SELECTION_ACTION, parameters);
071    
072                        return new ActionForward(lookupUrl, true);
073                    }
074                    else {
075                        GlobalVariables.getMessageList().add(BCKeyConstants.MESSAGE_BUDGET_PREVIOUS_SESSION_TIMEOUT);
076                        return mapping.findForward(BCConstants.MAPPING_LOST_SESSION_RETURNING);
077                    }
078                }
079            }
080    
081            return super.execute(mapping, form, request, response);
082        }
083    
084        /**
085         * Handling for screen close. Default action is return to caller.
086         * 
087         * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm,
088         *      javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
089         */
090        public ActionForward close(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
091            return returnToCaller(mapping, form, request, response);
092        }
093    
094        /**
095         * @see org.kuali.rice.kns.web.struts.action.KualiAction#refresh(org.apache.struts.action.ActionMapping,
096         *      org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
097         */
098        @Override
099        public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
100            this.moveCallBackMessagesInPlace();
101            this.removeCallBackMessagesObjectFromSession();
102    
103            return super.refresh(mapping, form, request, response);
104        }
105    
106        /**
107         * Return to form's back location (usually previous screen). Returns back the form key that was passed in for the previous form
108         * and any previous anchor position. Default refresh method is executed.
109         * 
110         * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm,
111         *      javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
112         */
113        public ActionForward returnToCaller(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
114            BudgetExpansionForm budgetExpansionForm = (BudgetExpansionForm) form;
115    
116            // if this form is session scoped remove it 
117            this.cleanupAnySessionForm(mapping, request);
118    
119            Properties parameters = new Properties();
120            parameters.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, BCConstants.BC_SELECTION_REFRESH_METHOD);
121            parameters.put(KFSConstants.DOC_FORM_KEY, budgetExpansionForm.getReturnFormKey());
122    
123            if (StringUtils.isNotEmpty(budgetExpansionForm.getReturnAnchor())) {
124                parameters.put(KFSConstants.ANCHOR, budgetExpansionForm.getReturnAnchor());
125            }
126            parameters.put(KFSConstants.REFRESH_CALLER, this.getClass().getName());
127    
128            this.addCallBackMessagesAsObjectInSession(budgetExpansionForm);
129    
130            String backUrl = UrlFactory.parameterizeUrl(budgetExpansionForm.getBackLocation(), parameters);
131            return new ActionForward(backUrl, true);
132        }
133    
134        /**
135         * add the callback messages and error messages as objects in session variable
136         */
137        public void addCallBackMessagesAsObjectInSession(BudgetExpansionForm budgetExpansionForm) {
138            if (!budgetExpansionForm.getCallBackMessages().isEmpty()) {
139                GlobalVariables.getUserSession().addObject(BCPropertyConstants.CALL_BACK_MESSAGES, budgetExpansionForm.getCallBackMessages());
140            }
141    
142            if (budgetExpansionForm.getCallBackErrors().hasErrors()) {
143                GlobalVariables.getUserSession().addObject(BCPropertyConstants.CALL_BACK_ERRORS, budgetExpansionForm.getCallBackErrors());
144            }
145        }
146    
147        /**
148         * remove the objects that hold the callback messages and error messages from session variable
149         */
150        public void removeCallBackMessagesObjectFromSession() {
151            GlobalVariables.getUserSession().removeObject(BCPropertyConstants.CALL_BACK_MESSAGES);
152            GlobalVariables.getUserSession().removeObject(BCPropertyConstants.CALL_BACK_ERRORS);
153        }
154    
155        /**
156         * move the callback messages and error messages in place
157         */
158        public void moveCallBackMessagesInPlace() {
159            MessageList messagesList = (MessageList) GlobalVariables.getUserSession().retrieveObject(BCPropertyConstants.CALL_BACK_MESSAGES);
160            if (messagesList != null) {
161                GlobalVariables.getMessageList().addAll(messagesList);
162            }
163    
164            ErrorMap errorMap = (ErrorMap) GlobalVariables.getUserSession().retrieveObject(BCPropertyConstants.CALL_BACK_ERRORS);
165            if (errorMap != null) {
166                GlobalVariables.setErrorMap(errorMap);
167            }
168        }
169    
170        /**
171         * remove any session form attribute
172         * 
173         * @param mapping
174         * @param request
175         */
176        public void cleanupAnySessionForm(ActionMapping mapping, HttpServletRequest request) {
177            if (BCConstants.MAPPING_SCOPE_SESSION.equals(mapping.getScope())) {
178                HttpSession sess = request.getSession(Boolean.FALSE);
179                String formName = mapping.getAttribute();
180                sess.removeAttribute(formName);
181            }
182        }
183    }