View Javadoc
1   /**
2    * Copyright 2005-2011 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.rule;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.kuali.rice.kew.api.identity.Id;
24  import org.kuali.rice.kew.api.identity.PrincipalName;
25  import org.kuali.rice.kew.api.rule.RoleName;
26  import org.kuali.rice.kew.engine.RouteContext;
27  import org.kuali.rice.kew.workgroup.GroupNameId;
28  
29  
30  
31  /**
32   * A RoleAttribute implementation that can be used in tests to easily provide canned/pre-configured
33   * recipients.  The role name should be a comma-delimited list of user authentication ids:
34   * <pre>
35   * <responsibilities>
36   *   <responsibility>
37   *     <role>org.kuali.rice.kew.rule.MockRole!user1,user2,user3</role>
38   *   </responsibility>
39   * </responsibilities>
40   * </pre>
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  public class MockRole extends UnqualifiedRoleAttribute {
44      private static final RoleName ROLE = new RoleName(MockRole.class.getName(), "List of authentication ids", "List of authentication ids");
45      private static final List<RoleName> ROLES;
46      static {
47          ArrayList<RoleName> roles = new ArrayList<RoleName>(1);
48          roles.add(ROLE);
49          ROLES = Collections.unmodifiableList(roles);
50      }
51  
52      public MockRole() {
53          super(ROLES);
54      }
55  
56      /**
57       * Overridden to accept any role name
58       * @see org.kuali.rice.kew.rule.UnqualifiedRoleAttribute#isValidRoleName(java.lang.String)
59       */
60      @Override
61      protected boolean isValidRoleName(String roleName) {
62          return true;
63      }
64  
65      @Override
66      protected ResolvedQualifiedRole resolveRole(RouteContext routeContext, String roleName) {
67          String[] ids = roleName.split("[,\\s+]");
68          ResolvedQualifiedRole rqr = new ResolvedQualifiedRole();
69          for (String id: ids) {
70              String type = "user";
71              String[] components = id.split(":", 2);
72              if (components.length > 1 && !StringUtils.isEmpty(components[0])) {
73                  type = components[0].trim();
74              }
75              Id recipientId;
76              if ("user".equals(type)) {
77                  recipientId = new PrincipalName(id);
78              } else if ("group".equals(type)) {
79                  recipientId = new GroupNameId(id);
80              } else {
81                  throw new RuntimeException("Unknown role recipient type: '" + type + "'. Must be 'user' or 'group'.");
82              }
83              rqr.getRecipients().add(recipientId);
84          }
85          rqr.setQualifiedRoleLabel("Recipients from parsing mock role: " + roleName);
86          rqr.setAnnotation("Recipients from parsing mock role: " + roleName);
87          return rqr;
88      }
89  }