View Javadoc
1   /**
2    * Copyright 2005-2015 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.rice.edl.impl.components;
17  
18  import java.util.List;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.kuali.rice.edl.impl.EDLContext;
22  import org.kuali.rice.edl.impl.EDLModelComponent;
23  import org.kuali.rice.edl.impl.EDLXmlUtils;
24  import org.kuali.rice.edl.impl.RequestParser;
25  import org.w3c.dom.Document;
26  import org.w3c.dom.Element;
27  
28  
29  /**
30   * Handles setting the current page in the EDL XML.
31   *
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   *
34   */
35  public class PageHandler implements EDLModelComponent {
36  
37  	public static final String CURRENT_PAGE = "currentPage";
38  	public static final String PREVIOUS_PAGE = "previousPage";
39  	public static final String GOTO_PAGE = "edl.gotoPage";
40  	
41  	public void updateDOM(Document dom, Element configElement, EDLContext edlContext) {
42  	    String currentPage = edlContext.getRequestParser().getParameterValue("edl." + CURRENT_PAGE);
43  	    String previousPage = edlContext.getRequestParser().getParameterValue("edl." + PREVIOUS_PAGE);
44  	    String gotoPage = findGotoPage(edlContext.getRequestParser(), currentPage);
45  	    if (!StringUtils.isBlank(gotoPage)) {
46  		previousPage = currentPage;
47  		currentPage = gotoPage;
48  	    }
49  	    establishCurrentPage(dom, currentPage);
50  	    establishPreviousPage(dom, previousPage);
51  	}
52  	
53  	/**
54  	 * Find parameter that looks like gotoPage:<pageName>
55  	 */
56  	private String findGotoPage(RequestParser requestParser, String currentPage) {
57  	    List<String> parameterNames = requestParser.getParameterNames();
58  	    for (String parameterName : parameterNames) {
59  		if (parameterName.startsWith(GOTO_PAGE + ":")) {
60  		    return parameterName.split(":")[1];
61  		}
62  	    }
63  	    return null;
64  	}
65  
66  	private void establishCurrentPage(Document dom, String currentPage) {
67  	    if (!StringUtils.isBlank(currentPage)) {
68  		Element currentPageElement = EDLXmlUtils.getOrCreateChildElement(dom.getDocumentElement(), CURRENT_PAGE, true);
69  		currentPageElement.appendChild(dom.createTextNode(currentPage));
70  	    }
71  	}
72  	
73  
74  	private void establishPreviousPage(Document dom, String previousPage) {
75  	    if (!StringUtils.isBlank(previousPage)) {
76  		Element previousPageElement = EDLXmlUtils.getOrCreateChildElement(dom.getDocumentElement(), PREVIOUS_PAGE, true);
77  		previousPageElement.appendChild(dom.createTextNode(previousPage));
78  	    }
79  	}
80  	    
81  
82  }