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.ArrayList;
019    import java.util.List;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.kuali.rice.edl.impl.EDLContext;
023    import org.kuali.rice.edl.impl.EDLModelComponent;
024    import org.kuali.rice.edl.impl.RequestParser;
025    import org.kuali.rice.edl.impl.UserAction;
026    import org.w3c.dom.Document;
027    import org.w3c.dom.Element;
028    import org.w3c.dom.NodeList;
029    
030    public class RefreshFromLookupComponent implements EDLModelComponent {
031            
032            public void updateDOM(Document dom, Element configElement, EDLContext edlContext) {
033                    String command = edlContext.getUserAction().getAction();
034                    if (UserAction.ACTION_REFRESH_FROM_LOOKUP.equals(command)) {
035                            RequestParser requestParser = edlContext.getRequestParser();
036                            
037                            Element currentVersion = VersioningPreprocessor.findCurrentVersion(dom);
038                            // First, create a list of all of the <field> tags that match the parameter names returned from the lookup
039                            List<Element> fieldsToDelete = new ArrayList<Element>();
040                            NodeList fieldNodes = currentVersion.getElementsByTagName("field");
041                            
042                            // get the list of input parameters returned from the lookup so we can clear empty ones as well
043                            List<String> requestParameterNames = requestParser.getParameterNames();
044                            
045                            for (int i = 0; i < fieldNodes.getLength(); i++) {
046                                    Element fieldNode = (Element) fieldNodes.item(i);
047                                    String fieldName = fieldNode.getAttribute("name");
048                                    if (requestParameterNames.contains(fieldName)) {
049                                            fieldsToDelete.add(fieldNode);
050                                    }
051                            }
052                            
053                            // Second, delete those nodes; we will rely on normal population to recreate those nodes
054                            // if the nodes weren't deleted, EDL would continue to display the old value on the generated output
055                            for (Element fieldToDelete : fieldsToDelete) {
056                                    currentVersion.removeChild(fieldToDelete);
057                            }
058                    }
059            }
060    }