View Javadoc

1   /*
2    * Copyright 2005-2008 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.edl.impl.components;
18  
19  import java.util.List;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.rice.edl.impl.EDLContext;
23  import org.kuali.rice.edl.impl.EDLModelComponent;
24  import org.kuali.rice.edl.impl.EDLXmlUtils;
25  import org.kuali.rice.edl.impl.RequestParser;
26  import org.w3c.dom.Document;
27  import org.w3c.dom.Element;
28  
29  
30  /**
31   * Handles setting the current page in the EDL XML.
32   *
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   *
35   */
36  public class PageHandler implements EDLModelComponent {
37  
38  	public static final String CURRENT_PAGE = "currentPage";
39  	public static final String PREVIOUS_PAGE = "previousPage";
40  	public static final String GOTO_PAGE = "edl.gotoPage";
41  	
42  	public void updateDOM(Document dom, Element configElement, EDLContext edlContext) {
43  	    String currentPage = edlContext.getRequestParser().getParameterValue("edl." + CURRENT_PAGE);
44  	    String previousPage = edlContext.getRequestParser().getParameterValue("edl." + PREVIOUS_PAGE);
45  	    String gotoPage = findGotoPage(edlContext.getRequestParser(), currentPage);
46  	    if (!StringUtils.isBlank(gotoPage)) {
47  		previousPage = currentPage;
48  		currentPage = gotoPage;
49  	    }
50  	    establishCurrentPage(dom, currentPage);
51  	    establishPreviousPage(dom, previousPage);
52  	}
53  	
54  	/**
55  	 * Find parameter that looks like gotoPage:<pageName>
56  	 */
57  	private String findGotoPage(RequestParser requestParser, String currentPage) {
58  	    List<String> parameterNames = requestParser.getParameterNames();
59  	    for (String parameterName : parameterNames) {
60  		if (parameterName.startsWith(GOTO_PAGE + ":")) {
61  		    return parameterName.split(":")[1];
62  		}
63  	    }
64  	    return null;
65  	}
66  
67  	private void establishCurrentPage(Document dom, String currentPage) {
68  	    if (!StringUtils.isBlank(currentPage)) {
69  		Element currentPageElement = EDLXmlUtils.getOrCreateChildElement(dom.getDocumentElement(), CURRENT_PAGE, true);
70  		currentPageElement.appendChild(dom.createTextNode(currentPage));
71  	    }
72  	}
73  	
74  
75  	private void establishPreviousPage(Document dom, String previousPage) {
76  	    if (!StringUtils.isBlank(previousPage)) {
77  		Element previousPageElement = EDLXmlUtils.getOrCreateChildElement(dom.getDocumentElement(), PREVIOUS_PAGE, true);
78  		previousPageElement.appendChild(dom.createTextNode(previousPage));
79  	    }
80  	}
81  	    
82  
83  }