001    /**
002     * Copyright 2005-2011 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     */
016    package org.kuali.rice.kew.rule;
017    
018    import java.util.ArrayList;
019    import java.util.Collections;
020    import java.util.List;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.kuali.rice.kew.api.identity.Id;
024    import org.kuali.rice.kew.api.identity.PrincipalName;
025    import org.kuali.rice.kew.api.rule.RoleName;
026    import org.kuali.rice.kew.engine.RouteContext;
027    import org.kuali.rice.kew.workgroup.GroupNameId;
028    
029    
030    
031    /**
032     * A RoleAttribute implementation that can be used in tests to easily provide canned/pre-configured
033     * recipients.  The role name should be a comma-delimited list of user authentication ids:
034     * <pre>
035     * <responsibilities>
036     *   <responsibility>
037     *     <role>org.kuali.rice.kew.rule.MockRole!user1,user2,user3</role>
038     *   </responsibility>
039     * </responsibilities>
040     * </pre>
041     * @author Kuali Rice Team (rice.collab@kuali.org)
042     */
043    public class MockRole extends UnqualifiedRoleAttribute {
044        private static final RoleName ROLE = new RoleName(MockRole.class.getName(), "List of authentication ids", "List of authentication ids");
045        private static final List<RoleName> ROLES;
046        static {
047            ArrayList<RoleName> roles = new ArrayList<RoleName>(1);
048            roles.add(ROLE);
049            ROLES = Collections.unmodifiableList(roles);
050        }
051    
052        public MockRole() {
053            super(ROLES);
054        }
055    
056        /**
057         * Overridden to accept any role name
058         * @see org.kuali.rice.kew.rule.UnqualifiedRoleAttribute#isValidRoleName(java.lang.String)
059         */
060        @Override
061        protected boolean isValidRoleName(String roleName) {
062            return true;
063        }
064    
065        @Override
066        protected ResolvedQualifiedRole resolveRole(RouteContext routeContext, String roleName) {
067            String[] ids = roleName.split("[,\\s+]");
068            ResolvedQualifiedRole rqr = new ResolvedQualifiedRole();
069            for (String id: ids) {
070                String type = "user";
071                String[] components = id.split(":", 2);
072                if (components.length > 1 && !StringUtils.isEmpty(components[0])) {
073                    type = components[0].trim();
074                }
075                Id recipientId;
076                if ("user".equals(type)) {
077                    recipientId = new PrincipalName(id);
078                } else if ("group".equals(type)) {
079                    recipientId = new GroupNameId(id);
080                } else {
081                    throw new RuntimeException("Unknown role recipient type: '" + type + "'. Must be 'user' or 'group'.");
082                }
083                rqr.getRecipients().add(recipientId);
084            }
085            rqr.setQualifiedRoleLabel("Recipients from parsing mock role: " + roleName);
086            rqr.setAnnotation("Recipients from parsing mock role: " + roleName);
087            return rqr;
088        }
089    }