View Javadoc
1   /**
2    * Copyright 2005-2014 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.Iterator;
20  import java.util.List;
21  
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  /**
32   * Matches request params to fields defined in edl configs.  Places matched value on the dom for rendering.  Places 
33   * currentDefinitionElement definition ( the element used to get the param from the request) element on the
34   * dom for rendering.
35   * 
36   * This is the base EDL Config Component for dealing with defined elements in an edl definition.  Most 
37   * custom edl element behavior can be achieved by subclassing this.
38   * 
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   *
41   */
42  public class SimpleWorkflowEDLConfigComponent implements EDLModelComponent {
43  
44  	protected Element definitionElement;
45  	private EDLContext edlContext;
46  
47  	public void updateDOM(Document dom, Element currentDefinitionElement, EDLContext edlContext) {
48  
49  		RequestParser requestParser = edlContext.getRequestParser();
50  		this.edlContext = edlContext;
51  		
52  		
53  		this.definitionElement = currentDefinitionElement;
54  		Element configElementForDOM = getReplacementConfigElement(currentDefinitionElement);
55  		if (configElementForDOM == null) {
56  			configElementForDOM = currentDefinitionElement;
57  		}
58  
59  		Element edlContentElement = EDLXmlUtils.getEDLContent(dom, false);
60  		edlContentElement.appendChild(configElementForDOM);
61  		
62  		Element currentVersion = VersioningPreprocessor.findCurrentVersion(dom);
63  		
64  		List matchingParams = getMatchingParams(configElementForDOM, requestParser, edlContext);
65  		for (Iterator iter = matchingParams.iterator(); iter.hasNext();) {
66  			MatchingParam matchingParam = (MatchingParam) iter.next();
67  			EDLXmlUtils.createFieldDataElement(currentVersion, matchingParam);
68  		}
69  	}
70  	
71  	public Element getReplacementConfigElement(Element element) {
72  		return null;
73  	}
74  
75  	public List getMatchingParams(Element originalConfigElement, RequestParser requestParser, EDLContext edlContext) {
76  		List params = new ArrayList();
77  		String paramName = originalConfigElement.getAttribute("name");
78  		String[] paramValues = requestParser.getParameterValues(paramName);
79  		if (paramValues == null) {
80  			return params;
81  		}
82  		for (int i = 0; i < paramValues.length; i++) {
83  			String value = paramValues[i];
84  			MatchingParam matchingParam = new MatchingParam();
85  			matchingParam.setParamName(paramName);
86  			matchingParam.setParamValue(value);
87  			String errorMessage = getErrorMessage(originalConfigElement, requestParser, matchingParam);
88  			if (errorMessage != null) {
89  				matchingParam.setError(Boolean.TRUE);
90  				matchingParam.setErrorMessage(errorMessage);
91  				edlContext.setInError(true);
92  			}
93  			params.add(matchingParam);
94  		}
95  		return params;
96  	}
97  
98  	public String getErrorMessage(Element originalConfigElement, RequestParser requestParser, MatchingParam matchingParam) {
99  		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 }