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.ArrayList;
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.RequestParser;
25  import org.kuali.rice.edl.impl.UserAction;
26  import org.w3c.dom.Document;
27  import org.w3c.dom.Element;
28  import org.w3c.dom.NodeList;
29  
30  public class RefreshFromLookupComponent implements EDLModelComponent {
31  	
32  	public void updateDOM(Document dom, Element configElement, EDLContext edlContext) {
33  		String command = edlContext.getUserAction().getAction();
34  		if (UserAction.ACTION_REFRESH_FROM_LOOKUP.equals(command)) {
35  			RequestParser requestParser = edlContext.getRequestParser();
36  			
37  			Element currentVersion = VersioningPreprocessor.findCurrentVersion(dom);
38  			// First, create a list of all of the <field> tags that match the parameter names returned from the lookup
39  			List<Element> fieldsToDelete = new ArrayList<Element>();
40  			NodeList fieldNodes = currentVersion.getElementsByTagName("field");
41  			
42  			// get the list of input parameters returned from the lookup so we can clear empty ones as well
43  			List<String> requestParameterNames = requestParser.getParameterNames();
44  			
45  			for (int i = 0; i < fieldNodes.getLength(); i++) {
46  				Element fieldNode = (Element) fieldNodes.item(i);
47  				String fieldName = fieldNode.getAttribute("name");
48  				if (requestParameterNames.contains(fieldName)) {
49  					fieldsToDelete.add(fieldNode);
50  				}
51  			}
52  			
53  			// Second, delete those nodes; we will rely on normal population to recreate those nodes
54  			// if the nodes weren't deleted, EDL would continue to display the old value on the generated output
55  			for (Element fieldToDelete : fieldsToDelete) {
56  				currentVersion.removeChild(fieldToDelete);
57  			}
58  		}
59  	}
60  }