| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.edl.impl.components; |
| 18 | |
|
| 19 | |
import javax.xml.xpath.XPath; |
| 20 | |
import javax.xml.xpath.XPathConstants; |
| 21 | |
import javax.xml.xpath.XPathExpressionException; |
| 22 | |
|
| 23 | |
import org.apache.commons.lang.StringUtils; |
| 24 | |
import org.kuali.rice.edl.impl.EDLContext; |
| 25 | |
import org.kuali.rice.edl.impl.EDLModelComponent; |
| 26 | |
import org.kuali.rice.kew.rule.xmlrouting.XPathHelper; |
| 27 | |
import org.w3c.dom.Document; |
| 28 | |
import org.w3c.dom.Element; |
| 29 | |
import org.w3c.dom.Node; |
| 30 | |
import org.w3c.dom.NodeList; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | 0 | public class SelectControlEDLComponent implements EDLModelComponent { |
| 44 | |
|
| 45 | |
public void updateDOM(Document dom, Element currentDefinitionElement, EDLContext edlContext) { |
| 46 | 0 | Element currentVersion = VersioningPreprocessor.findCurrentVersion(dom); |
| 47 | 0 | XPath xPath = XPathHelper.newXPath(dom); |
| 48 | |
try { |
| 49 | 0 | NodeList selectFieldDefs = (NodeList)xPath.evaluate("//fieldDef[display/type = 'select' and display/valuesGroup] | //fieldDef[display/type = 'select_refresh' and display/valuesGroup]", dom, XPathConstants.NODESET); |
| 50 | 0 | for (int fIndex = 0; fIndex < selectFieldDefs.getLength(); fIndex++) { |
| 51 | 0 | Element fieldDef = (Element)selectFieldDefs.item(fIndex); |
| 52 | 0 | NodeList valuesGroups = (NodeList)xPath.evaluate("./display/valuesGroup", fieldDef, XPathConstants.NODESET); |
| 53 | 0 | for (int index = 0; index < valuesGroups.getLength(); index++) { |
| 54 | 0 | Element valuesGroupElem = (Element)valuesGroups.item(index); |
| 55 | 0 | NodeList dependsOnFields = (NodeList)xPath.evaluate("./dependsOn/field", valuesGroupElem, XPathConstants.NODESET); |
| 56 | 0 | String fieldEvalExpression = ""; |
| 57 | 0 | for (int dIndex = 0; dIndex < dependsOnFields.getLength(); dIndex++) { |
| 58 | 0 | if (!StringUtils.isBlank(fieldEvalExpression)) { |
| 59 | 0 | fieldEvalExpression += " and "; |
| 60 | |
} |
| 61 | 0 | Element fieldElem = (Element)dependsOnFields.item(dIndex); |
| 62 | 0 | String name = fieldElem.getAttribute("name"); |
| 63 | 0 | String value = fieldElem.getTextContent(); |
| 64 | 0 | fieldEvalExpression += "./field[@name='" + name + "']/value = '" + value + "'"; |
| 65 | |
} |
| 66 | 0 | if ((Boolean)xPath.evaluate(fieldEvalExpression, currentVersion, XPathConstants.BOOLEAN)) { |
| 67 | 0 | includeValuesGroup(valuesGroupElem); |
| 68 | |
} else { |
| 69 | |
|
| 70 | 0 | valuesGroupElem.getParentNode().removeChild(valuesGroupElem); |
| 71 | |
} |
| 72 | |
} |
| 73 | |
} |
| 74 | 0 | } catch (XPathExpressionException e) { |
| 75 | 0 | throw new RuntimeException("Failed to evaluate xpath expression.", e); |
| 76 | 0 | } |
| 77 | 0 | } |
| 78 | |
|
| 79 | |
protected void includeValuesGroup(Element valuesGroupElem) { |
| 80 | 0 | Element valuesGroupParent = (Element)valuesGroupElem.getParentNode(); |
| 81 | 0 | NodeList valuesGroupChildren = valuesGroupElem.getChildNodes(); |
| 82 | |
|
| 83 | 0 | for (int index = 0; index < valuesGroupChildren.getLength(); index++) { |
| 84 | 0 | Node item = valuesGroupChildren.item(index); |
| 85 | 0 | if (Node.ELEMENT_NODE == item.getNodeType() && item.getNodeName().equals("values")) { |
| 86 | 0 | valuesGroupParent.insertBefore(item, valuesGroupElem); |
| 87 | |
} |
| 88 | |
} |
| 89 | 0 | valuesGroupParent.removeChild(valuesGroupElem); |
| 90 | 0 | } |
| 91 | |
|
| 92 | |
} |