001    /**
002     * Copyright 2005-2013 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.rice.edl.impl.components;
017    
018    import java.util.List;
019    
020    import org.apache.commons.lang.StringUtils;
021    import org.kuali.rice.edl.impl.EDLContext;
022    import org.kuali.rice.edl.impl.EDLModelComponent;
023    import org.kuali.rice.edl.impl.EDLXmlUtils;
024    import org.kuali.rice.edl.impl.RequestParser;
025    import org.w3c.dom.Document;
026    import org.w3c.dom.Element;
027    
028    
029    /**
030     * Handles setting the current page in the EDL XML.
031     *
032     * @author Kuali Rice Team (rice.collab@kuali.org)
033     *
034     */
035    public class PageHandler implements EDLModelComponent {
036    
037            public static final String CURRENT_PAGE = "currentPage";
038            public static final String PREVIOUS_PAGE = "previousPage";
039            public static final String GOTO_PAGE = "edl.gotoPage";
040            
041            public void updateDOM(Document dom, Element configElement, EDLContext edlContext) {
042                String currentPage = edlContext.getRequestParser().getParameterValue("edl." + CURRENT_PAGE);
043                String previousPage = edlContext.getRequestParser().getParameterValue("edl." + PREVIOUS_PAGE);
044                String gotoPage = findGotoPage(edlContext.getRequestParser(), currentPage);
045                if (!StringUtils.isBlank(gotoPage)) {
046                    previousPage = currentPage;
047                    currentPage = gotoPage;
048                }
049                establishCurrentPage(dom, currentPage);
050                establishPreviousPage(dom, previousPage);
051            }
052            
053            /**
054             * Find parameter that looks like gotoPage:<pageName>
055             */
056            private String findGotoPage(RequestParser requestParser, String currentPage) {
057                List<String> parameterNames = requestParser.getParameterNames();
058                for (String parameterName : parameterNames) {
059                    if (parameterName.startsWith(GOTO_PAGE + ":")) {
060                        return parameterName.split(":")[1];
061                    }
062                }
063                return null;
064            }
065    
066            private void establishCurrentPage(Document dom, String currentPage) {
067                if (!StringUtils.isBlank(currentPage)) {
068                    Element currentPageElement = EDLXmlUtils.getOrCreateChildElement(dom.getDocumentElement(), CURRENT_PAGE, true);
069                    currentPageElement.appendChild(dom.createTextNode(currentPage));
070                }
071            }
072            
073    
074            private void establishPreviousPage(Document dom, String previousPage) {
075                if (!StringUtils.isBlank(previousPage)) {
076                    Element previousPageElement = EDLXmlUtils.getOrCreateChildElement(dom.getDocumentElement(), PREVIOUS_PAGE, true);
077                    previousPageElement.appendChild(dom.createTextNode(previousPage));
078                }
079            }
080                
081    
082    }