001/**
002 * Copyright 2005-2014 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.rice.edl.impl.components;
017
018import java.util.List;
019
020import org.apache.commons.lang.StringUtils;
021import org.kuali.rice.edl.impl.EDLContext;
022import org.kuali.rice.edl.impl.EDLModelComponent;
023import org.kuali.rice.edl.impl.RequestParser;
024import org.kuali.rice.edl.impl.UserAction;
025import org.kuali.rice.krad.util.KRADConstants;
026import org.w3c.dom.Document;
027import org.w3c.dom.Element;
028
029
030/**
031 * Handles establishing what action the user submitted.  It's important to normalize this because
032 * the action could be submitted in the "userAction" request parameter, in the "command" request
033 * parameter or not passed at all.
034 * 
035 * <p>This is primarily important in identifying whether the submission is the first-time "load"
036 * of a document or an action being executed against an already loaded document.
037 *
038 * @author Kuali Rice Team (rice.collab@kuali.org)
039 */
040public class EstablishUserAction implements EDLModelComponent {
041        
042    public static final String USER_ACTION_PARAM = "userAction";
043    public static final String COMMAND_PARAM = "command";
044    
045    public void updateDOM(Document dom, Element configElement, EDLContext edlContext) {
046        RequestParser requestParser = edlContext.getRequestParser();
047
048        List<String> params = requestParser.getParameterNames();
049        
050        /**
051         * IE does not process image buttons in the same way as other browsers. If you have 
052         *   <input name="userAction" value="performAction.unit_1" type=image src="images/searchicon.gif"/>
053         *   The actual request parameters that are sent are: userAction.x=9; userAction.y=22. The numbers
054         *   correspond to the location of the button on the screen. NO OTHER DATA IS SENT.  So in order 
055         *   for this to work for IE we need to change the name to include the value of userAction.
056         *   So we're now sending
057         *   <input name="userAction.performLookup.unit_1" value="This no longer matters" type=image src="images/searchicon.gif"/>
058         *   So we now need to parse out the userAction, the action, and the value. Which is what happens below.
059         *   The end result is a new parameter "userAction=performLookup.unit_1". 
060         */
061        for(String param: params){
062                if(param.startsWith("userAction.performLookup") && param.endsWith(".y")){                       
063                        requestParser.setParameterValue(USER_ACTION_PARAM, param.substring("userAction.".length(), param.length()-2));
064                }
065        }
066        String userAction = requestParser.getParameterValue(USER_ACTION_PARAM);
067
068        if (StringUtils.isEmpty(userAction)) {
069            String command = requestParser.getParameterValue(COMMAND_PARAM);
070            if (!StringUtils.isEmpty(command)) {
071                // from Workflow Quick Links, the "command" parameter will be passed with a value of "initiate"
072                if (UserAction.ACTION_CREATE.equals(command)) {
073                    userAction = UserAction.ACTION_CREATE;
074                }
075                // from Document Search/Action List a command parameter is passed but we want to load the document
076                userAction = UserAction.ACTION_LOAD;
077            } else {
078                String methodToCall = requestParser.getParameterValue(KRADConstants.DISPATCH_REQUEST_PARAMETER);
079                        if (StringUtils.equals(methodToCall, KRADConstants.RETURN_METHOD_TO_CALL)) {
080                                userAction = UserAction.ACTION_REFRESH_FROM_LOOKUP;
081                        }
082                        else {
083                                userAction = UserAction.ACTION_UNDEFINED;
084                    }
085            }
086        }
087        edlContext.setUserAction(new UserAction(userAction));
088    }
089        
090}