001/**
002 * Copyright 2005-2015 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.xml;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.log4j.Logger;
020import org.jdom.Element;
021import org.kuali.rice.core.api.util.xml.XmlException;
022import org.kuali.rice.kew.api.KewApiConstants;
023import org.kuali.rice.kew.api.rule.RoleName;
024import org.kuali.rice.kew.rule.RuleResponsibilityBo;
025import org.kuali.rice.kew.util.Utilities;
026import org.kuali.rice.kim.api.group.Group;
027import org.kuali.rice.kim.api.identity.principal.Principal;
028import org.kuali.rice.kim.api.services.KimApiServiceLocator;
029
030import static org.kuali.rice.core.api.impex.xml.XmlConstants.*;
031
032
033/**
034 * Parsing routines for XML structures shared across parsers.
035 *
036 * @author Kuali Rice Team (rice.collab@kuali.org)
037 */
038public class CommonXmlParser {
039    private static final Logger LOG = Logger.getLogger(CommonXmlParser.class);
040
041    /**
042     * Parses, but does not save, a RuleResponsibility from responsibility identifier elements nested in
043     * the specified element
044     * @param element parent element of responsibility identifier elements
045     * @return a parsed RuleResponsibility
046     * @throws XmlException
047     */
048    public static RuleResponsibilityBo parseResponsibilityNameAndType(Element element) throws XmlException {
049        RuleResponsibilityBo responsibility = new RuleResponsibilityBo();
050
051        String principalId = element.getChildText(PRINCIPAL_ID, element.getNamespace());
052        String principalName = element.getChildText(PRINCIPAL_NAME, element.getNamespace());
053        String groupId = element.getChildText(GROUP_ID, element.getNamespace());
054        Element groupNameElement = element.getChild(GROUP_NAME, element.getNamespace());
055        String role = element.getChildText(ROLE, element.getNamespace());
056        Element roleNameElement = element.getChild(ROLE_NAME, element.getNamespace());
057
058        String user = element.getChildText(USER, element.getNamespace());
059        String workgroup = element.getChildText(WORKGROUP, element.getNamespace());
060
061        if (!StringUtils.isEmpty(user)) {
062            principalName = user;
063            LOG.warn("Rule XML is using deprecated element 'user', please use 'principalName' instead.");
064        }
065
066        // in code below, we allow core config parameter replacement in responsibilities
067        if (!StringUtils.isBlank(principalId)) {
068            principalId = Utilities.substituteConfigParameters(principalId);
069            Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
070            if (principal == null) {
071                throw new XmlException("Could not locate principal with the given id: " + principalId);
072            }
073            responsibility.setRuleResponsibilityName(principalId);
074            responsibility.setRuleResponsibilityType(KewApiConstants.RULE_RESPONSIBILITY_WORKFLOW_ID);
075        } else if (!StringUtils.isBlank(principalName)) {
076            principalName = Utilities.substituteConfigParameters(principalName);
077            Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName);
078            if (principal == null) {
079                throw new XmlException("Could not locate principal with the given name: " + principalName);
080            }
081            responsibility.setRuleResponsibilityName(principal.getPrincipalId());
082            responsibility.setRuleResponsibilityType(KewApiConstants.RULE_RESPONSIBILITY_WORKFLOW_ID);
083        } else if (!StringUtils.isBlank(groupId)) {
084            groupId = Utilities.substituteConfigParameters(groupId);
085            Group group = KimApiServiceLocator.getGroupService().getGroup(groupId);
086            if (group == null) {
087                throw new XmlException("Could not locate group with the given id: " + groupId);
088            }
089            responsibility.setRuleResponsibilityName(groupId);
090            responsibility.setRuleResponsibilityType(KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID);
091        } else if (groupNameElement != null) {
092            String groupName = groupNameElement.getText();
093            String groupNamespace = groupNameElement.getAttributeValue(NAMESPACE);
094            if (StringUtils.isBlank(groupName)) {
095                throw new XmlException("Group name element has no value");
096            }
097            if (StringUtils.isBlank(groupNamespace)) {
098                throw new XmlException("namespace attribute must be specified");
099            }
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}