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.Iterator;
020    import java.util.List;
021    
022    import org.kuali.rice.edl.impl.EDLContext;
023    import org.kuali.rice.edl.impl.EDLModelComponent;
024    import org.kuali.rice.edl.impl.EDLXmlUtils;
025    import org.kuali.rice.edl.impl.RequestParser;
026    import org.w3c.dom.Document;
027    import org.w3c.dom.Element;
028    
029    
030    
031    /**
032     * Matches request params to fields defined in edl configs.  Places matched value on the dom for rendering.  Places 
033     * currentDefinitionElement definition ( the element used to get the param from the request) element on the
034     * dom for rendering.
035     * 
036     * This is the base EDL Config Component for dealing with defined elements in an edl definition.  Most 
037     * custom edl element behavior can be achieved by subclassing this.
038     * 
039     * @author Kuali Rice Team (rice.collab@kuali.org)
040     *
041     */
042    public class SimpleWorkflowEDLConfigComponent implements EDLModelComponent {
043    
044            protected Element definitionElement;
045            private EDLContext edlContext;
046    
047            public void updateDOM(Document dom, Element currentDefinitionElement, EDLContext edlContext) {
048    
049                    RequestParser requestParser = edlContext.getRequestParser();
050                    this.edlContext = edlContext;
051                    
052                    
053                    this.definitionElement = currentDefinitionElement;
054                    Element configElementForDOM = getReplacementConfigElement(currentDefinitionElement);
055                    if (configElementForDOM == null) {
056                            configElementForDOM = currentDefinitionElement;
057                    }
058    
059                    Element edlContentElement = EDLXmlUtils.getEDLContent(dom, false);
060                    edlContentElement.appendChild(configElementForDOM);
061                    
062                    Element currentVersion = VersioningPreprocessor.findCurrentVersion(dom);
063                    
064                    List matchingParams = getMatchingParams(configElementForDOM, requestParser, edlContext);
065                    for (Iterator iter = matchingParams.iterator(); iter.hasNext();) {
066                            MatchingParam matchingParam = (MatchingParam) iter.next();
067                            EDLXmlUtils.createFieldDataElement(currentVersion, matchingParam);
068                    }
069            }
070            
071            public Element getReplacementConfigElement(Element element) {
072                    return null;
073            }
074    
075            public List getMatchingParams(Element originalConfigElement, RequestParser requestParser, EDLContext edlContext) {
076                    List params = new ArrayList();
077                    String paramName = originalConfigElement.getAttribute("name");
078                    String[] paramValues = requestParser.getParameterValues(paramName);
079                    if (paramValues == null) {
080                            return params;
081                    }
082                    for (int i = 0; i < paramValues.length; i++) {
083                            String value = paramValues[i];
084                            MatchingParam matchingParam = new MatchingParam();
085                            matchingParam.setParamName(paramName);
086                            matchingParam.setParamValue(value);
087                            String errorMessage = getErrorMessage(originalConfigElement, requestParser, matchingParam);
088                            if (errorMessage != null) {
089                                    matchingParam.setError(Boolean.TRUE);
090                                    matchingParam.setErrorMessage(errorMessage);
091                                    edlContext.setInError(true);
092                            }
093                            params.add(matchingParam);
094                    }
095                    return params;
096            }
097    
098            public String getErrorMessage(Element originalConfigElement, RequestParser requestParser, MatchingParam matchingParam) {
099                    return null;
100            }
101    
102            public Element getDefinitionElement() {
103                    return definitionElement;
104            }
105    
106            public void setDefinitionElement(Element definitionElement) {
107                    this.definitionElement = definitionElement;
108            }
109    
110            public EDLContext getEdlContext() {
111                    return edlContext;
112            }
113    
114            public void setEdlContext(EDLContext edlContext) {
115                    this.edlContext = edlContext;
116            }
117    }