View Javadoc
1   /**
2    * Copyright 2005-2015 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.kew.xml;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.jdom.Element;
21  import org.kuali.rice.core.api.util.xml.XmlException;
22  import org.kuali.rice.kew.api.KewApiConstants;
23  import org.kuali.rice.kew.api.rule.RoleName;
24  import org.kuali.rice.kew.rule.RuleResponsibilityBo;
25  import org.kuali.rice.kew.util.Utilities;
26  import org.kuali.rice.kim.api.group.Group;
27  import org.kuali.rice.kim.api.identity.principal.Principal;
28  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
29  
30  import static org.kuali.rice.core.api.impex.xml.XmlConstants.*;
31  
32  
33  /**
34   * Parsing routines for XML structures shared across parsers.
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  public class CommonXmlParser {
39      private static final Logger LOG = Logger.getLogger(CommonXmlParser.class);
40  
41      /**
42       * Parses, but does not save, a RuleResponsibility from responsibility identifier elements nested in
43       * the specified element
44       * @param element parent element of responsibility identifier elements
45       * @return a parsed RuleResponsibility
46       * @throws XmlException
47       */
48      public static RuleResponsibilityBo parseResponsibilityNameAndType(Element element) throws XmlException {
49          RuleResponsibilityBo responsibility = new RuleResponsibilityBo();
50  
51          String principalId = element.getChildText(PRINCIPAL_ID, element.getNamespace());
52          String principalName = element.getChildText(PRINCIPAL_NAME, element.getNamespace());
53          String groupId = element.getChildText(GROUP_ID, element.getNamespace());
54          Element groupNameElement = element.getChild(GROUP_NAME, element.getNamespace());
55          String role = element.getChildText(ROLE, element.getNamespace());
56          Element roleNameElement = element.getChild(ROLE_NAME, element.getNamespace());
57  
58          String user = element.getChildText(USER, element.getNamespace());
59          String workgroup = element.getChildText(WORKGROUP, element.getNamespace());
60  
61          if (!StringUtils.isEmpty(user)) {
62              principalName = user;
63              LOG.warn("Rule XML is using deprecated element 'user', please use 'principalName' instead.");
64          }
65  
66          // in code below, we allow core config parameter replacement in responsibilities
67          if (!StringUtils.isBlank(principalId)) {
68              principalId = Utilities.substituteConfigParameters(principalId);
69              Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
70              if (principal == null) {
71                  throw new XmlException("Could not locate principal with the given id: " + principalId);
72              }
73              responsibility.setRuleResponsibilityName(principalId);
74              responsibility.setRuleResponsibilityType(KewApiConstants.RULE_RESPONSIBILITY_WORKFLOW_ID);
75          } else if (!StringUtils.isBlank(principalName)) {
76              principalName = Utilities.substituteConfigParameters(principalName);
77              Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName);
78              if (principal == null) {
79                  throw new XmlException("Could not locate principal with the given name: " + principalName);
80              }
81              responsibility.setRuleResponsibilityName(principal.getPrincipalId());
82              responsibility.setRuleResponsibilityType(KewApiConstants.RULE_RESPONSIBILITY_WORKFLOW_ID);
83          } else if (!StringUtils.isBlank(groupId)) {
84              groupId = Utilities.substituteConfigParameters(groupId);
85              Group group = KimApiServiceLocator.getGroupService().getGroup(groupId);
86              if (group == null) {
87                  throw new XmlException("Could not locate group with the given id: " + groupId);
88              }
89              responsibility.setRuleResponsibilityName(groupId);
90              responsibility.setRuleResponsibilityType(KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID);
91          } else if (groupNameElement != null) {
92              String groupName = groupNameElement.getText();
93              String groupNamespace = groupNameElement.getAttributeValue(NAMESPACE);
94              if (StringUtils.isBlank(groupName)) {
95                  throw new XmlException("Group name element has no value");
96              }
97              if (StringUtils.isBlank(groupNamespace)) {
98                  throw new XmlException("namespace attribute must be specified");
99              }
100             groupName = Utilities.substituteConfigParameters(groupName);
101             groupNamespace = Utilities.substituteConfigParameters(groupNamespace);
102             Group group = KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName(groupNamespace,
103                     groupName);
104             if (group == null) {
105                 throw new XmlException("Could not locate group with the given namespace: " + groupNamespace + " and name: " + groupName);
106             }
107             responsibility.setRuleResponsibilityName(group.getId());
108             responsibility.setRuleResponsibilityType(KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID);
109         } else if (!StringUtils.isBlank(role)) {
110             role = Utilities.substituteConfigParameters(role);
111             responsibility.setRuleResponsibilityName(role);
112             responsibility.setRuleResponsibilityType(KewApiConstants.RULE_RESPONSIBILITY_ROLE_ID);
113         } else if (roleNameElement != null) {
114             String roleName = roleNameElement.getText();
115             String attributeClassName = roleNameElement.getAttributeValue(ATTRIBUTE_CLASS_NAME);
116             if (StringUtils.isBlank(roleName)) {
117                 throw new XmlException("Role name element has no value");
118             }
119             if (StringUtils.isBlank(attributeClassName)) {
120                 throw new XmlException("attributeClassName attribute must be specified");
121             }
122             roleName = Utilities.substituteConfigParameters(roleName);
123             attributeClassName = Utilities.substituteConfigParameters(attributeClassName);
124             responsibility.setRuleResponsibilityName(RoleName.constructRoleValue(attributeClassName, roleName));
125             responsibility.setRuleResponsibilityType(KewApiConstants.RULE_RESPONSIBILITY_ROLE_ID);
126         } else if (!StringUtils.isBlank(workgroup)) {
127             LOG.warn("Rule XML is using deprecated element 'workgroup', please use 'groupName' instead.");
128             workgroup = Utilities.substituteConfigParameters(workgroup);
129             String workgroupNamespace = Utilities.parseGroupNamespaceCode(workgroup);
130             String workgroupName = Utilities.parseGroupName(workgroup);
131 
132             Group workgroupObject = KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName(
133                     workgroupNamespace, workgroupName);
134             if (workgroupObject == null) {
135                 throw new XmlException("Could not locate workgroup: " + workgroup);
136             }
137             responsibility.setRuleResponsibilityName(workgroupObject.getId());
138             responsibility.setRuleResponsibilityType(KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID);
139         } else {
140             return null;
141         }
142 
143         return responsibility;
144     }
145 }