| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.kew.xml; |
| 18 | |
|
| 19 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.ACTION_REQUESTED; |
| 20 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.APPROVE_POLICY; |
| 21 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.ATTRIBUTE_CLASS_NAME; |
| 22 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.DELEGATIONS; |
| 23 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.DELEGATION_TYPE; |
| 24 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.DESCRIPTION; |
| 25 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.DOCUMENT_TYPE; |
| 26 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.FORCE_ACTION; |
| 27 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.FROM_DATE; |
| 28 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.GROUP_ID; |
| 29 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.GROUP_NAME; |
| 30 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.NAME; |
| 31 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.NAMESPACE; |
| 32 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.PARENT_RESPONSIBILITY; |
| 33 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.PARENT_RULE_NAME; |
| 34 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.PRINCIPAL_ID; |
| 35 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.PRINCIPAL_NAME; |
| 36 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.PRIORITY; |
| 37 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.RESPONSIBILITIES; |
| 38 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.RESPONSIBILITY; |
| 39 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.RESPONSIBILITY_ID; |
| 40 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.ROLE; |
| 41 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.ROLE_NAME; |
| 42 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.RULE; |
| 43 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.RULES; |
| 44 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.RULE_DELEGATION; |
| 45 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.RULE_DELEGATIONS; |
| 46 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.RULE_EXPRESSION; |
| 47 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.RULE_EXTENSIONS; |
| 48 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.RULE_NAMESPACE; |
| 49 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.RULE_TEMPLATE; |
| 50 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.TO_DATE; |
| 51 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.USER; |
| 52 | |
import static org.kuali.rice.core.api.impex.xml.XmlConstants.WORKGROUP; |
| 53 | |
|
| 54 | |
import java.io.IOException; |
| 55 | |
import java.io.InputStream; |
| 56 | |
import java.sql.Timestamp; |
| 57 | |
import java.text.ParseException; |
| 58 | |
import java.util.ArrayList; |
| 59 | |
import java.util.Iterator; |
| 60 | |
import java.util.List; |
| 61 | |
|
| 62 | |
import javax.xml.parsers.ParserConfigurationException; |
| 63 | |
|
| 64 | |
import org.apache.commons.lang.StringUtils; |
| 65 | |
import org.jdom.Document; |
| 66 | |
import org.jdom.Element; |
| 67 | |
import org.jdom.JDOMException; |
| 68 | |
import org.kuali.rice.core.util.RiceConstants; |
| 69 | |
import org.kuali.rice.core.util.XmlHelper; |
| 70 | |
import org.kuali.rice.core.xml.XmlException; |
| 71 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
| 72 | |
import org.kuali.rice.kew.rule.Role; |
| 73 | |
import org.kuali.rice.kew.rule.RuleBaseValues; |
| 74 | |
import org.kuali.rice.kew.rule.RuleDelegation; |
| 75 | |
import org.kuali.rice.kew.rule.RuleExpressionDef; |
| 76 | |
import org.kuali.rice.kew.rule.RuleResponsibility; |
| 77 | |
import org.kuali.rice.kew.rule.bo.RuleTemplate; |
| 78 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
| 79 | |
import org.kuali.rice.kew.util.KEWConstants; |
| 80 | |
import org.kuali.rice.kew.util.Utilities; |
| 81 | |
import org.kuali.rice.kim.bo.Group; |
| 82 | |
import org.kuali.rice.kim.bo.entity.KimPrincipal; |
| 83 | |
import org.kuali.rice.kim.service.KIMServiceLocator; |
| 84 | |
import org.xml.sax.SAXException; |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | 0 | public class RuleXmlParser { |
| 94 | |
|
| 95 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RuleXmlParser.class); |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
private static final int DEFAULT_RULE_PRIORITY = 1; |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
private static final boolean DEFAULT_FORCE_ACTION = false; |
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
private static final String DEFAULT_APPROVE_POLICY = KEWConstants.APPROVE_POLICY_FIRST_APPROVE; |
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
private static final String DEFAULT_ACTION_REQUESTED = KEWConstants.ACTION_REQUEST_APPROVE_REQ; |
| 113 | |
|
| 114 | |
public List<RuleDelegation> parseRuleDelegations(InputStream input) throws IOException, XmlException { |
| 115 | |
try { |
| 116 | 0 | Document doc = XmlHelper.trimSAXXml(input); |
| 117 | 0 | Element root = doc.getRootElement(); |
| 118 | 0 | return parseRuleDelegations(root); |
| 119 | 0 | } catch (JDOMException e) { |
| 120 | 0 | throw new XmlException("Parse error.", e); |
| 121 | 0 | } catch (SAXException e){ |
| 122 | 0 | throw new XmlException("Parse error.",e); |
| 123 | 0 | } catch(ParserConfigurationException e){ |
| 124 | 0 | throw new XmlException("Parse error.",e); |
| 125 | |
} |
| 126 | |
} |
| 127 | |
|
| 128 | |
public List<RuleBaseValues> parseRules(InputStream input) throws IOException, XmlException { |
| 129 | |
try { |
| 130 | 0 | Document doc = XmlHelper.trimSAXXml(input); |
| 131 | 0 | Element root = doc.getRootElement(); |
| 132 | 0 | return parseRules(root); |
| 133 | 0 | } catch (JDOMException e) { |
| 134 | 0 | throw new XmlException("Parse error.", e); |
| 135 | 0 | } catch (SAXException e){ |
| 136 | 0 | throw new XmlException("Parse error.",e); |
| 137 | 0 | } catch(ParserConfigurationException e){ |
| 138 | 0 | throw new XmlException("Parse error.",e); |
| 139 | |
} |
| 140 | |
} |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
public List<RuleBaseValues> parseRules(Element element) throws XmlException { |
| 148 | 0 | List<RuleBaseValues> rulesToSave = new ArrayList<RuleBaseValues>(); |
| 149 | 0 | for (Element rulesElement: (List<Element>) element.getChildren(RULES, RULE_NAMESPACE)) { |
| 150 | 0 | for (Element ruleElement: (List<Element>) rulesElement.getChildren(RULE, RULE_NAMESPACE)) { |
| 151 | 0 | RuleBaseValues rule = parseRule(ruleElement); |
| 152 | 0 | rulesToSave.add(rule); |
| 153 | 0 | } |
| 154 | |
} |
| 155 | 0 | checkForDuplicateRules(rulesToSave); |
| 156 | 0 | return KEWServiceLocator.getRuleService().saveRules(rulesToSave, false); |
| 157 | |
} |
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
public List<RuleDelegation> parseRuleDelegations(Element element) throws XmlException { |
| 165 | 0 | List<RuleDelegation> ruleDelegationsToSave = new ArrayList<RuleDelegation>(); |
| 166 | 0 | for (Element ruleDelegationsElement: (List<Element>) element.getChildren(RULE_DELEGATIONS, RULE_NAMESPACE)) { |
| 167 | 0 | for (Element ruleDelegationElement: (List<Element>) ruleDelegationsElement.getChildren(RULE_DELEGATION, RULE_NAMESPACE)) { |
| 168 | 0 | RuleDelegation ruleDelegation = parseRuleDelegation(ruleDelegationElement); |
| 169 | 0 | ruleDelegationsToSave.add(ruleDelegation); |
| 170 | 0 | } |
| 171 | |
} |
| 172 | |
|
| 173 | 0 | return KEWServiceLocator.getRuleService().saveRuleDelegations(ruleDelegationsToSave, false); |
| 174 | |
} |
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
private void checkForDuplicateRules(List<RuleBaseValues> rules) throws XmlException { |
| 180 | 0 | for (RuleBaseValues rule : rules) { |
| 181 | 0 | if (StringUtils.isBlank(rule.getName())) { |
| 182 | 0 | LOG.debug("Checking for rule duplication on an anonymous rule."); |
| 183 | 0 | checkRuleForDuplicate(rule); |
| 184 | |
} |
| 185 | |
} |
| 186 | 0 | } |
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
private void checkForDuplicateRuleDelegations(List<RuleDelegation> ruleDelegations) throws XmlException { |
| 192 | 0 | for (RuleDelegation ruleDelegation : ruleDelegations) { |
| 193 | 0 | if (StringUtils.isBlank(ruleDelegation.getDelegationRuleBaseValues().getName())) { |
| 194 | 0 | LOG.debug("Checking for rule duplication on an anonymous rule delegation."); |
| 195 | 0 | checkRuleDelegationForDuplicate(ruleDelegation); |
| 196 | |
} |
| 197 | |
} |
| 198 | 0 | } |
| 199 | |
|
| 200 | |
private RuleDelegation parseRuleDelegation(Element element) throws XmlException { |
| 201 | 0 | RuleDelegation ruleDelegation = new RuleDelegation(); |
| 202 | 0 | Element parentResponsibilityElement = element.getChild(PARENT_RESPONSIBILITY, element.getNamespace()); |
| 203 | 0 | if (parentResponsibilityElement == null) { |
| 204 | 0 | throw new XmlException("parent responsibility was not defined"); |
| 205 | |
} |
| 206 | 0 | Long parentResponsibilityId = parseParentResponsibilityId(parentResponsibilityElement); |
| 207 | 0 | String delegationType = element.getChildText(DELEGATION_TYPE, element.getNamespace()); |
| 208 | 0 | if (delegationType == null || !(delegationType.equals(KEWConstants.DELEGATION_PRIMARY) || delegationType.equals(KEWConstants.DELEGATION_SECONDARY))) { |
| 209 | 0 | throw new XmlException("Invalid delegation type specified for delegate rule '" + delegationType + "'"); |
| 210 | |
} |
| 211 | |
|
| 212 | 0 | ruleDelegation.setResponsibilityId(parentResponsibilityId); |
| 213 | 0 | ruleDelegation.setDelegationType(delegationType); |
| 214 | |
|
| 215 | 0 | Element ruleElement = element.getChild(RULE, element.getNamespace()); |
| 216 | 0 | RuleBaseValues rule = parseRule(ruleElement); |
| 217 | 0 | rule.setDelegateRule(true); |
| 218 | 0 | ruleDelegation.setDelegationRuleBaseValues(rule); |
| 219 | |
|
| 220 | 0 | return ruleDelegation; |
| 221 | |
} |
| 222 | |
|
| 223 | |
private Long parseParentResponsibilityId(Element element) throws XmlException { |
| 224 | 0 | String responsibilityId = element.getChildText(RESPONSIBILITY_ID, element.getNamespace()); |
| 225 | 0 | if (!StringUtils.isBlank(responsibilityId)) { |
| 226 | 0 | return Long.valueOf(responsibilityId); |
| 227 | |
} |
| 228 | 0 | String parentRuleName = element.getChildText(PARENT_RULE_NAME, element.getNamespace()); |
| 229 | 0 | if (StringUtils.isBlank(parentRuleName)) { |
| 230 | 0 | throw new XmlException("One of responsibilityId or parentRuleName needs to be defined"); |
| 231 | |
} |
| 232 | 0 | RuleBaseValues parentRule = KEWServiceLocator.getRuleService().getRuleByName(parentRuleName); |
| 233 | 0 | if (parentRule == null) { |
| 234 | 0 | throw new XmlException("Could find the parent rule with name '" + parentRuleName + "'"); |
| 235 | |
} |
| 236 | 0 | RuleResponsibility ruleResponsibilityNameAndType = parseResponsibilityNameAndType(element); |
| 237 | 0 | if (ruleResponsibilityNameAndType == null) { |
| 238 | 0 | throw new XmlException("Could not locate a valid responsibility declaration for the parent responsibility."); |
| 239 | |
} |
| 240 | 0 | Long parentResponsibilityId = KEWServiceLocator.getRuleService().findResponsibilityIdForRule(parentRuleName, |
| 241 | |
ruleResponsibilityNameAndType.getRuleResponsibilityName(), |
| 242 | |
ruleResponsibilityNameAndType.getRuleResponsibilityType()); |
| 243 | 0 | if (parentResponsibilityId == null) { |
| 244 | 0 | throw new XmlException("Failed to locate parent responsibility for rule with name '" + parentRuleName + "' and responsibility " + ruleResponsibilityNameAndType); |
| 245 | |
} |
| 246 | 0 | return parentResponsibilityId; |
| 247 | |
} |
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
private RuleBaseValues parseRule(Element element) throws XmlException { |
| 258 | 0 | String name = element.getChildText(NAME, element.getNamespace()); |
| 259 | 0 | RuleBaseValues rule = createRule(name); |
| 260 | |
|
| 261 | 0 | setDefaultRuleValues(rule); |
| 262 | 0 | rule.setName(name); |
| 263 | |
|
| 264 | 0 | String toDatestr = element.getChildText( TO_DATE, element.getNamespace()); |
| 265 | 0 | String fromDatestr = element.getChildText( FROM_DATE, element.getNamespace()); |
| 266 | 0 | rule.setToDate(formatDate("toDate", toDatestr)); |
| 267 | 0 | rule.setFromDate(formatDate("fromDate", fromDatestr)); |
| 268 | |
|
| 269 | 0 | String description = element.getChildText(DESCRIPTION, element.getNamespace()); |
| 270 | 0 | if (StringUtils.isBlank(description)) { |
| 271 | 0 | throw new XmlException("Rule must have a description."); |
| 272 | |
} |
| 273 | |
|
| 274 | 0 | String documentTypeName = element.getChildText(DOCUMENT_TYPE, element.getNamespace()); |
| 275 | 0 | if (StringUtils.isBlank(documentTypeName)) { |
| 276 | 0 | throw new XmlException("Rule must have a document type."); |
| 277 | |
} |
| 278 | 0 | DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findByName(documentTypeName); |
| 279 | 0 | if (documentType == null) { |
| 280 | 0 | throw new XmlException("Could not locate document type '" + documentTypeName + "'"); |
| 281 | |
} |
| 282 | |
|
| 283 | 0 | RuleTemplate ruleTemplate = null; |
| 284 | 0 | String ruleTemplateName = element.getChildText(RULE_TEMPLATE, element.getNamespace()); |
| 285 | 0 | Element ruleExtensionsElement = element.getChild(RULE_EXTENSIONS, element.getNamespace()); |
| 286 | 0 | if (!StringUtils.isBlank(ruleTemplateName)) { |
| 287 | 0 | ruleTemplate = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateName(ruleTemplateName); |
| 288 | 0 | if (ruleTemplate == null) { |
| 289 | 0 | throw new XmlException("Could not locate rule template '" + ruleTemplateName + "'"); |
| 290 | |
} |
| 291 | |
} else { |
| 292 | 0 | if (ruleExtensionsElement != null) { |
| 293 | 0 | throw new XmlException("Templateless rules may not have rule extensions"); |
| 294 | |
} |
| 295 | |
} |
| 296 | |
|
| 297 | 0 | RuleExpressionDef ruleExpressionDef = null; |
| 298 | 0 | Element exprElement = element.getChild(RULE_EXPRESSION, element.getNamespace()); |
| 299 | 0 | if (exprElement != null) { |
| 300 | 0 | String exprType = exprElement.getAttributeValue("type"); |
| 301 | 0 | if (StringUtils.isEmpty(exprType)) { |
| 302 | 0 | throw new XmlException("Expression type must be specified"); |
| 303 | |
} |
| 304 | 0 | String expression = exprElement.getTextTrim(); |
| 305 | 0 | ruleExpressionDef = new RuleExpressionDef(); |
| 306 | 0 | ruleExpressionDef.setType(exprType); |
| 307 | 0 | ruleExpressionDef.setExpression(expression); |
| 308 | |
} |
| 309 | |
|
| 310 | 0 | String forceActionValue = element.getChildText(FORCE_ACTION, element.getNamespace()); |
| 311 | 0 | Boolean forceAction = Boolean.valueOf(DEFAULT_FORCE_ACTION); |
| 312 | 0 | if (!StringUtils.isBlank(forceActionValue)) { |
| 313 | 0 | forceAction = Boolean.valueOf(forceActionValue); |
| 314 | |
} |
| 315 | |
|
| 316 | 0 | rule.setDocTypeName(documentType.getName()); |
| 317 | 0 | if (ruleTemplate != null) { |
| 318 | 0 | rule.setRuleTemplateId(ruleTemplate.getRuleTemplateId()); |
| 319 | 0 | rule.setRuleTemplate(ruleTemplate); |
| 320 | |
} |
| 321 | 0 | if (ruleExpressionDef != null) { |
| 322 | 0 | rule.setRuleExpressionDef(ruleExpressionDef); |
| 323 | |
} |
| 324 | 0 | rule.setDescription(description); |
| 325 | 0 | rule.setForceAction(forceAction); |
| 326 | |
|
| 327 | 0 | Element responsibilitiesElement = element.getChild(RESPONSIBILITIES, element.getNamespace()); |
| 328 | 0 | rule.setResponsibilities(parseResponsibilities(responsibilitiesElement, rule)); |
| 329 | 0 | rule.setRuleExtensions(parseRuleExtensions(ruleExtensionsElement, rule)); |
| 330 | |
|
| 331 | 0 | return rule; |
| 332 | |
} |
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
private RuleBaseValues createRule(String ruleName) { |
| 341 | 0 | RuleBaseValues rule = new RuleBaseValues(); |
| 342 | 0 | RuleBaseValues existingRule = (ruleName != null) ? KEWServiceLocator.getRuleService().getRuleByName(ruleName) : null; |
| 343 | 0 | if (existingRule != null) { |
| 344 | |
|
| 345 | 0 | rule.setRuleBaseValuesId(existingRule.getRuleBaseValuesId()); |
| 346 | 0 | rule.setPreviousVersionId(existingRule.getPreviousVersionId()); |
| 347 | 0 | rule.setPreviousVersion(existingRule.getPreviousVersion()); |
| 348 | 0 | rule.setResponsibilities(existingRule.getResponsibilities()); |
| 349 | |
} |
| 350 | 0 | return rule; |
| 351 | |
} |
| 352 | |
|
| 353 | |
|
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
private void checkRuleForDuplicate(RuleBaseValues rule) throws XmlException { |
| 360 | 0 | Long ruleId = KEWServiceLocator.getRuleService().getDuplicateRuleId(rule); |
| 361 | 0 | if (ruleId != null) { |
| 362 | 0 | throw new XmlException("Rule '" + rule.getDescription() + "' on doc '" + rule.getDocTypeName() + "' is a duplicate of rule with rule Id " + ruleId); |
| 363 | |
} |
| 364 | 0 | } |
| 365 | |
|
| 366 | |
private void checkRuleDelegationForDuplicate(RuleDelegation ruleDelegation) throws XmlException { |
| 367 | 0 | checkRuleForDuplicate(ruleDelegation.getDelegationRuleBaseValues()); |
| 368 | 0 | } |
| 369 | |
|
| 370 | |
private void setDefaultRuleValues(RuleBaseValues rule) throws XmlException { |
| 371 | 0 | rule.setForceAction(Boolean.FALSE); |
| 372 | 0 | rule.setActivationDate(new Timestamp(System.currentTimeMillis())); |
| 373 | 0 | rule.setActiveInd(Boolean.TRUE); |
| 374 | 0 | rule.setCurrentInd(Boolean.TRUE); |
| 375 | 0 | rule.setTemplateRuleInd(Boolean.FALSE); |
| 376 | 0 | rule.setVersionNbr(new Integer(0)); |
| 377 | 0 | rule.setDelegateRule(false); |
| 378 | 0 | } |
| 379 | |
|
| 380 | |
private List<RuleResponsibility> parseResponsibilities(Element element, RuleBaseValues rule) throws XmlException { |
| 381 | 0 | if (element == null) { |
| 382 | 0 | return new ArrayList<RuleResponsibility>(0); |
| 383 | |
} |
| 384 | 0 | List<RuleResponsibility> existingResponsibilities = rule.getResponsibilities(); |
| 385 | 0 | List<RuleResponsibility> responsibilities = new ArrayList<RuleResponsibility>(); |
| 386 | 0 | List responsibilityElements = element.getChildren(RESPONSIBILITY, element.getNamespace()); |
| 387 | 0 | for (Iterator iterator = responsibilityElements.iterator(); iterator.hasNext();) { |
| 388 | 0 | Element responsibilityElement = (Element) iterator.next(); |
| 389 | 0 | RuleResponsibility responsibility = parseResponsibility(responsibilityElement, rule); |
| 390 | 0 | reconcileWithExistingResponsibility(responsibility, existingResponsibilities); |
| 391 | 0 | responsibilities.add(responsibility); |
| 392 | 0 | } |
| 393 | 0 | if (responsibilities.size() == 0) { |
| 394 | 0 | throw new XmlException("Rule responsibility list must have at least one responsibility."); |
| 395 | |
} |
| 396 | 0 | return responsibilities; |
| 397 | |
} |
| 398 | |
|
| 399 | |
public RuleResponsibility parseResponsibility(Element element, RuleBaseValues rule) throws XmlException { |
| 400 | 0 | RuleResponsibility responsibility = new RuleResponsibility(); |
| 401 | 0 | responsibility.setRuleBaseValues(rule); |
| 402 | 0 | String actionRequested = null; |
| 403 | 0 | String priority = null; |
| 404 | 0 | actionRequested = element.getChildText(ACTION_REQUESTED, element.getNamespace()); |
| 405 | 0 | if (StringUtils.isBlank(actionRequested)) { |
| 406 | 0 | actionRequested = DEFAULT_ACTION_REQUESTED; |
| 407 | |
} |
| 408 | 0 | priority = element.getChildText(PRIORITY, element.getNamespace()); |
| 409 | 0 | if (StringUtils.isBlank(priority)) { |
| 410 | 0 | priority = String.valueOf(DEFAULT_RULE_PRIORITY); |
| 411 | |
} |
| 412 | 0 | String approvePolicy = element.getChildText(APPROVE_POLICY, element.getNamespace()); |
| 413 | 0 | Element delegations = element.getChild(DELEGATIONS, element.getNamespace()); |
| 414 | 0 | if (actionRequested == null) { |
| 415 | 0 | throw new XmlException("actionRequested is required on responsibility"); |
| 416 | |
} |
| 417 | 0 | if (!actionRequested.equals(KEWConstants.ACTION_REQUEST_COMPLETE_REQ) && !actionRequested.equals(KEWConstants.ACTION_REQUEST_APPROVE_REQ) && !actionRequested.equals(KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ) && !actionRequested.equals(KEWConstants.ACTION_REQUEST_FYI_REQ)) { |
| 418 | 0 | throw new XmlException("Invalid action requested code '" + actionRequested + "'"); |
| 419 | |
} |
| 420 | 0 | if (StringUtils.isBlank(approvePolicy)) { |
| 421 | 0 | approvePolicy = DEFAULT_APPROVE_POLICY; |
| 422 | |
} |
| 423 | 0 | if (!approvePolicy.equals(KEWConstants.APPROVE_POLICY_ALL_APPROVE) && !approvePolicy.equals(KEWConstants.APPROVE_POLICY_FIRST_APPROVE)) { |
| 424 | 0 | throw new XmlException("Invalid approve policy '" + approvePolicy + "'"); |
| 425 | |
} |
| 426 | 0 | Integer priorityNumber = Integer.valueOf(priority); |
| 427 | 0 | responsibility.setActionRequestedCd(actionRequested); |
| 428 | 0 | responsibility.setPriority(priorityNumber); |
| 429 | 0 | responsibility.setApprovePolicy(approvePolicy); |
| 430 | |
|
| 431 | 0 | RuleResponsibility responsibilityNameAndType = parseResponsibilityNameAndType(element); |
| 432 | 0 | if (responsibilityNameAndType == null) { |
| 433 | 0 | throw new XmlException("Could not locate a valid responsibility declaration on a responsibility on rule with description '" + rule.getDescription() + "'"); |
| 434 | |
} |
| 435 | 0 | if (responsibilityNameAndType.getRuleResponsibilityType().equals(KEWConstants.RULE_RESPONSIBILITY_GROUP_ID) |
| 436 | |
&& responsibility.getApprovePolicy().equals(KEWConstants.APPROVE_POLICY_ALL_APPROVE)) { |
| 437 | 0 | throw new XmlException("Invalid approve policy '" + approvePolicy + "'. This policy is not supported with Groups."); |
| 438 | |
} |
| 439 | 0 | responsibility.setRuleResponsibilityName(responsibilityNameAndType.getRuleResponsibilityName()); |
| 440 | 0 | responsibility.setRuleResponsibilityType(responsibilityNameAndType.getRuleResponsibilityType()); |
| 441 | |
|
| 442 | 0 | return responsibility; |
| 443 | |
} |
| 444 | |
|
| 445 | |
public RuleResponsibility parseResponsibilityNameAndType(Element element) throws XmlException { |
| 446 | 0 | RuleResponsibility responsibility = new RuleResponsibility(); |
| 447 | |
|
| 448 | 0 | String principalId = element.getChildText(PRINCIPAL_ID, element.getNamespace()); |
| 449 | 0 | String principalName = element.getChildText(PRINCIPAL_NAME, element.getNamespace()); |
| 450 | 0 | String groupId = element.getChildText(GROUP_ID, element.getNamespace()); |
| 451 | 0 | Element groupNameElement = element.getChild(GROUP_NAME, element.getNamespace()); |
| 452 | 0 | String role = element.getChildText(ROLE, element.getNamespace()); |
| 453 | 0 | Element roleNameElement = element.getChild(ROLE_NAME, element.getNamespace()); |
| 454 | |
|
| 455 | 0 | String user = element.getChildText(USER, element.getNamespace()); |
| 456 | 0 | String workgroup = element.getChildText(WORKGROUP, element.getNamespace()); |
| 457 | |
|
| 458 | 0 | if (!StringUtils.isEmpty(user)) { |
| 459 | 0 | principalName = user; |
| 460 | 0 | LOG.warn("Rule XML is using deprecated element 'user', please use 'principalName' instead."); |
| 461 | |
} |
| 462 | |
|
| 463 | |
|
| 464 | 0 | if (!StringUtils.isBlank(principalId)) { |
| 465 | 0 | principalId = Utilities.substituteConfigParameters(principalId); |
| 466 | 0 | KimPrincipal principal = KIMServiceLocator.getIdentityManagementService().getPrincipal(principalId); |
| 467 | 0 | if (principal == null) { |
| 468 | 0 | throw new XmlException("Could not locate principal with the given id: " + principalId); |
| 469 | |
} |
| 470 | 0 | responsibility.setRuleResponsibilityName(principalId); |
| 471 | 0 | responsibility.setRuleResponsibilityType(KEWConstants.RULE_RESPONSIBILITY_WORKFLOW_ID); |
| 472 | 0 | } else if (!StringUtils.isBlank(principalName)) { |
| 473 | 0 | principalName = Utilities.substituteConfigParameters(principalName); |
| 474 | 0 | KimPrincipal principal = KIMServiceLocator.getIdentityManagementService().getPrincipalByPrincipalName(principalName); |
| 475 | 0 | if (principal == null) { |
| 476 | 0 | throw new XmlException("Could not locate principal with the given name: " + principalName); |
| 477 | |
} |
| 478 | 0 | responsibility.setRuleResponsibilityName(principal.getPrincipalId()); |
| 479 | 0 | responsibility.setRuleResponsibilityType(KEWConstants.RULE_RESPONSIBILITY_WORKFLOW_ID); |
| 480 | 0 | } else if (!StringUtils.isBlank(groupId)) { |
| 481 | 0 | groupId = Utilities.substituteConfigParameters(groupId); |
| 482 | 0 | Group group = KIMServiceLocator.getIdentityManagementService().getGroup(groupId); |
| 483 | 0 | if (group == null) { |
| 484 | 0 | throw new XmlException("Could not locate group with the given id: " + groupId); |
| 485 | |
} |
| 486 | 0 | responsibility.setRuleResponsibilityName(groupId); |
| 487 | 0 | responsibility.setRuleResponsibilityType(KEWConstants.RULE_RESPONSIBILITY_GROUP_ID); |
| 488 | 0 | } else if (groupNameElement != null) { |
| 489 | 0 | String groupName = groupNameElement.getText(); |
| 490 | 0 | String groupNamespace = groupNameElement.getAttributeValue(NAMESPACE); |
| 491 | 0 | if (StringUtils.isBlank(groupName)) { |
| 492 | 0 | throw new XmlException("Group name element has no value"); |
| 493 | |
} |
| 494 | 0 | if (StringUtils.isBlank(groupNamespace)) { |
| 495 | 0 | throw new XmlException("namespace attribute must be specified"); |
| 496 | |
} |
| 497 | 0 | groupName = Utilities.substituteConfigParameters(groupName); |
| 498 | 0 | groupNamespace = Utilities.substituteConfigParameters(groupNamespace); |
| 499 | 0 | Group group = KIMServiceLocator.getIdentityManagementService().getGroupByName(groupNamespace, groupName); |
| 500 | 0 | if (group == null) { |
| 501 | 0 | throw new XmlException("Could not locate group with the given namespace: " + groupNamespace + " and name: " + groupName); |
| 502 | |
} |
| 503 | 0 | responsibility.setRuleResponsibilityName(group.getGroupId()); |
| 504 | 0 | responsibility.setRuleResponsibilityType(KEWConstants.RULE_RESPONSIBILITY_GROUP_ID); |
| 505 | 0 | } else if (!StringUtils.isBlank(role)) { |
| 506 | 0 | role = Utilities.substituteConfigParameters(role); |
| 507 | 0 | responsibility.setRuleResponsibilityName(role); |
| 508 | 0 | responsibility.setRuleResponsibilityType(KEWConstants.RULE_RESPONSIBILITY_ROLE_ID); |
| 509 | 0 | } else if (roleNameElement != null) { |
| 510 | 0 | String roleName = roleNameElement.getText(); |
| 511 | 0 | String attributeClassName = roleNameElement.getAttributeValue(ATTRIBUTE_CLASS_NAME); |
| 512 | 0 | if (StringUtils.isBlank(roleName)) { |
| 513 | 0 | throw new XmlException("Role name element has no value"); |
| 514 | |
} |
| 515 | 0 | if (StringUtils.isBlank(attributeClassName)) { |
| 516 | 0 | throw new XmlException("attributeClassName attribute must be specified"); |
| 517 | |
} |
| 518 | 0 | roleName = Utilities.substituteConfigParameters(roleName); |
| 519 | 0 | attributeClassName = Utilities.substituteConfigParameters(attributeClassName); |
| 520 | 0 | responsibility.setRuleResponsibilityName(Role.constructRoleValue(attributeClassName, roleName)); |
| 521 | 0 | responsibility.setRuleResponsibilityType(KEWConstants.RULE_RESPONSIBILITY_ROLE_ID); |
| 522 | 0 | } else if (!StringUtils.isBlank(workgroup)) { |
| 523 | 0 | LOG.warn("Rule XML is using deprecated element 'workgroup', please use 'groupName' instead."); |
| 524 | 0 | workgroup = Utilities.substituteConfigParameters(workgroup); |
| 525 | 0 | String workgroupNamespace = Utilities.parseGroupNamespaceCode(workgroup); |
| 526 | 0 | String workgroupName = Utilities.parseGroupName(workgroup); |
| 527 | |
|
| 528 | 0 | Group workgroupObject = KIMServiceLocator.getIdentityManagementService().getGroupByName(workgroupNamespace, workgroupName); |
| 529 | 0 | if (workgroupObject == null) { |
| 530 | 0 | throw new XmlException("Could not locate workgroup: " + workgroup); |
| 531 | |
} |
| 532 | 0 | responsibility.setRuleResponsibilityName(workgroupObject.getGroupId()); |
| 533 | 0 | responsibility.setRuleResponsibilityType(KEWConstants.RULE_RESPONSIBILITY_GROUP_ID); |
| 534 | 0 | } else { |
| 535 | 0 | return null; |
| 536 | |
} |
| 537 | |
|
| 538 | 0 | return responsibility; |
| 539 | |
} |
| 540 | |
|
| 541 | |
|
| 542 | |
|
| 543 | |
|
| 544 | |
|
| 545 | |
|
| 546 | |
|
| 547 | |
private void reconcileWithExistingResponsibility(RuleResponsibility responsibility, List<RuleResponsibility> existingResponsibilities) { |
| 548 | 0 | if (existingResponsibilities == null || existingResponsibilities.isEmpty()) { |
| 549 | 0 | return; |
| 550 | |
} |
| 551 | 0 | RuleResponsibility exactMatch = null; |
| 552 | 0 | for (RuleResponsibility existingResponsibility : existingResponsibilities) { |
| 553 | 0 | if (isExactResponsibilityMatch(responsibility, existingResponsibility)) { |
| 554 | 0 | exactMatch = existingResponsibility; |
| 555 | 0 | break; |
| 556 | |
} |
| 557 | |
} |
| 558 | 0 | if (exactMatch != null) { |
| 559 | 0 | responsibility.setResponsibilityId(exactMatch.getResponsibilityId()); |
| 560 | |
} |
| 561 | 0 | } |
| 562 | |
|
| 563 | |
|
| 564 | |
|
| 565 | |
|
| 566 | |
private boolean isExactResponsibilityMatch(RuleResponsibility newResponsibility, RuleResponsibility existingResponsibility) { |
| 567 | 0 | if (existingResponsibility.getResponsibilityId().equals(newResponsibility.getResponsibilityId())) { |
| 568 | 0 | return true; |
| 569 | |
} |
| 570 | 0 | if (existingResponsibility.getRuleResponsibilityName().equals(newResponsibility.getRuleResponsibilityName()) && |
| 571 | |
existingResponsibility.getRuleResponsibilityType().equals(newResponsibility.getRuleResponsibilityType()) && |
| 572 | |
existingResponsibility.getApprovePolicy().equals(newResponsibility.getApprovePolicy()) && |
| 573 | |
existingResponsibility.getActionRequestedCd().equals(newResponsibility.getActionRequestedCd()) && |
| 574 | |
existingResponsibility.getPriority().equals(newResponsibility.getPriority())) { |
| 575 | 0 | return true; |
| 576 | |
} |
| 577 | 0 | return false; |
| 578 | |
} |
| 579 | |
|
| 580 | |
private List parseRuleExtensions(Element element, RuleBaseValues rule) throws XmlException { |
| 581 | 0 | if (element == null) { |
| 582 | 0 | return new ArrayList(); |
| 583 | |
} |
| 584 | 0 | RuleExtensionXmlParser parser = new RuleExtensionXmlParser(); |
| 585 | 0 | return parser.parseRuleExtensions(element, rule); |
| 586 | |
} |
| 587 | |
|
| 588 | |
public Timestamp formatDate(String dateLabel, String dateString) throws XmlException { |
| 589 | 0 | if (StringUtils.isBlank(dateString)) { |
| 590 | 0 | return null; |
| 591 | |
} |
| 592 | |
try { |
| 593 | 0 | return new Timestamp(RiceConstants.getDefaultDateFormat().parse(dateString).getTime()); |
| 594 | 0 | } catch (ParseException e) { |
| 595 | 0 | throw new XmlException(dateLabel + " is not in the proper format. Should have been: " + RiceConstants.DEFAULT_DATE_FORMAT_PATTERN); |
| 596 | |
} |
| 597 | |
} |
| 598 | |
|
| 599 | |
} |