1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.rule;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.kew.api.KewApiConstants;
20 import org.kuali.rice.kew.api.WorkflowRuntimeException;
21 import org.kuali.rice.kew.api.extension.ExtensionDefinition;
22 import org.kuali.rice.kew.api.identity.Id;
23 import org.kuali.rice.kew.engine.RouteContext;
24 import org.kuali.rice.kew.routeheader.DocumentContent;
25 import org.kuali.rice.kew.rule.xmlrouting.GenericXMLRuleAttribute;
26 import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
27 import org.w3c.dom.Element;
28 import org.w3c.dom.NodeList;
29 import org.xml.sax.InputSource;
30
31 import javax.xml.xpath.XPath;
32 import javax.xml.xpath.XPathConstants;
33 import javax.xml.xpath.XPathExpressionException;
34 import java.io.StringReader;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39
40
41
42
43
44
45
46
47 public abstract class AbstractIdRoleAttribute extends AbstractRoleAttribute
48 implements GenericXMLRuleAttribute {
49
50 private static final String XML_ELEMENT_LABEL = "xmlElementLabel";
51 private static final String ROLE_NAME_LABEL = "roleNameLabel";
52
53 private String idValue;
54 private Map paramMap = new HashMap();
55 private ExtensionDefinition extensionDefinition;
56
57 protected abstract String getAttributeElementName();
58
59 protected abstract Id resolveId(String id);
60
61 protected abstract String getIdName();
62
63
64
65
66
67
68
69
70 public List<String> getQualifiedRoleNames(String roleName,
71 DocumentContent documentContent) {
72 try {
73 readConfiguration();
74 String elementName = (String) getParamMap().get(XML_ELEMENT_LABEL);
75 List<String> qualifiedRoleNames = new ArrayList<String>();
76 XPath xPath = XPathHelper.newXPath();
77 NodeList idNodes = (NodeList) xPath.evaluate("//"
78 + getAttributeElementName() + "/" + elementName,
79 documentContent.getDocument(), XPathConstants.NODESET);
80 for (int index = 0; index < idNodes.getLength(); index++) {
81 Element idElement = (Element) idNodes.item(index);
82 String id = idElement.getTextContent();
83 qualifiedRoleNames.add(id);
84 }
85 return qualifiedRoleNames;
86 } catch (XPathExpressionException e) {
87 throw new WorkflowRuntimeException(
88 "Failed to evaulate XPath expression to find ids.", e);
89 }
90 }
91
92
93
94
95
96
97
98
99 public ResolvedQualifiedRole resolveQualifiedRole(
100 RouteContext routeContext, String roleName, String qualifiedRole) {
101 String roleNameLabel = (String) getParamMap().get(ROLE_NAME_LABEL);
102 if (roleNameLabel == null) {
103 readConfiguration();
104 roleNameLabel = (String) getParamMap().get(ROLE_NAME_LABEL);
105 }
106 ResolvedQualifiedRole resolvedRole = new ResolvedQualifiedRole();
107 resolvedRole.setQualifiedRoleLabel(roleNameLabel);
108 resolvedRole.getRecipients().add(resolveId(qualifiedRole));
109 return resolvedRole;
110 }
111
112
113
114
115
116
117 @Override
118 public String getDocContent() {
119 readConfiguration();
120 if (!StringUtils.isBlank(getIdValue())) {
121 String elementName = (String) getParamMap().get(XML_ELEMENT_LABEL);
122 return "<" + getAttributeElementName() + "><" + elementName + ">"
123 + getIdValue() + "</" + elementName + "></"
124 + getAttributeElementName() + ">";
125 }
126 return "";
127 }
128
129
130
131
132
133
134 protected void readConfiguration() {
135 String idInMap = (String) getParamMap().get(getIdName());
136 if (getIdValue() == null) {
137 setIdValue(idInMap);
138 }
139 if (getIdValue() != null) {
140 getParamMap().put(getIdName(), getIdValue());
141 }
142 if (extensionDefinition != null) {
143 String xmlConfigData = extensionDefinition.getConfiguration().get(KewApiConstants.ATTRIBUTE_XML_CONFIG_DATA);
144 if (!StringUtils.isBlank(xmlConfigData)) {
145 XPath xPath = XPathHelper.newXPath();
146 try {
147 String xmlElementLabel = xPath.evaluate("/configuration/"
148 + XML_ELEMENT_LABEL, new InputSource(
149 new StringReader(xmlConfigData)));
150 String roleNameLabel = xPath.evaluate("/configuration/"
151 + ROLE_NAME_LABEL, new InputSource(
152 new StringReader(xmlConfigData)));
153 if (!StringUtils.isBlank(xmlElementLabel)) {
154 getParamMap().put(XML_ELEMENT_LABEL, xmlElementLabel);
155 }
156 if (!StringUtils.isBlank(roleNameLabel)) {
157 getParamMap().put(ROLE_NAME_LABEL, roleNameLabel);
158 }
159
160 } catch (XPathExpressionException e) {
161 throw new WorkflowRuntimeException(
162 "Failed to locate Rule Attribute configuration.");
163 }
164 }
165 }
166
167 if (StringUtils.isBlank((String) getParamMap().get(XML_ELEMENT_LABEL))) {
168 getParamMap().put(XML_ELEMENT_LABEL, getIdName());
169 }
170 if (getParamMap().get(ROLE_NAME_LABEL) == null) {
171 getParamMap().put(ROLE_NAME_LABEL, "");
172 }
173 }
174
175 public String getIdValue() {
176 return this.idValue;
177 }
178
179 public void setIdValue(String idValue) {
180 this.idValue = idValue;
181 }
182
183 public Map getParamMap() {
184 return paramMap;
185 }
186
187 public void setParamMap(Map paramMap) {
188 this.paramMap = paramMap;
189 }
190
191 public void setExtensionDefinition(ExtensionDefinition extensionDefinition) {
192 this.extensionDefinition = extensionDefinition;
193 }
194
195 }