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.List;
20  
21  import org.kuali.rice.kew.api.identity.Id;
22  import org.kuali.rice.kew.engine.RouteContext;
23  import org.kuali.rice.kew.routeheader.DocumentContent;
24  
25  
26  /**
27   * Generic base class that implements common functionality to simplify implementing
28   * a RoleAttribute.  This includes a standard qualified role name String format and simplified
29   * template methods, as well as a generic attribute content model.
30   * 
31   * <p>Control flow:</p>
32   * 
33   * <ol>
34   *   <li>{@link #getQualifiedRoleNames(String, DocumentContent)}
35   *     <ol>
36   *       <li>{@link #generateQualifiedRoleNames(String, DocumentContent)}
37   *         <ol>
38   *           <li>{@link #getRoleNameQualifiers(String, DocumentContent)}</li>
39   *         </ol>
40   *       </li>
41   *     </ol>
42   *   </li>
43   *   <li>{@link #resolveQualifiedRole(RouteContext, String, String)}
44   *     <ol>
45   *       <li>{@link #resolveQualifiedRole(RouteContext, QualifiedRoleName)}
46   *         <ol>
47   *           <li>{@link #resolveRecipients(RouteContext, QualifiedRoleName)}</li>
48   *           <li>{@link #getLabelForQualifiedRoleName(QualifiedRoleName)}</li>
49   *         </ol>
50   *       </li>
51   *     </ol>
52   *   </li>
53   * </ol> 
54   *     
55   * @author Kuali Rice Team (rice.collab@kuali.org)
56   */
57  public abstract class GenericRoleAttribute extends GenericWorkflowAttribute implements RoleAttribute {
58      public GenericRoleAttribute() {
59          super(null);
60      }
61  
62      public GenericRoleAttribute(String uniqueName) {
63          super(uniqueName);
64      }
65  
66      public boolean isMatch(DocumentContent docContent, List<RuleExtensionBo> ruleExtensions) {
67          // FIXME: ok, this MUST return true, as it IS evaluated (don't know why)
68          // maybe investigate only calling isMatch on rule attributes as it doesn't seem that
69          // matching is relevant for role attributes...is it?
70          // throw new UnsupportedOperationException("Role attributes do not implement isMatch");
71          return true;
72      }
73  
74      public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
75          List<QualifiedRoleName> qualifiedRoleNames = generateQualifiedRoleNames(roleName, documentContent);
76          if (qualifiedRoleNames == null) {
77              return null;
78          }
79          List<String> q = new ArrayList<String>(qualifiedRoleNames.size());
80          for (QualifiedRoleName qrn: qualifiedRoleNames) {
81              q.add(qrn.encode());
82          }
83          return q;
84      }
85  
86      /**
87       * Template method responsible for producing a list of QualifiedRoleName objects.  Default implementation
88       * calls {@link #getRoleNameQualifiers(String, DocumentContent)}
89       */
90      protected List<QualifiedRoleName> generateQualifiedRoleNames(String roleName, DocumentContent documentContent) {
91          List<String> qualifiers = getRoleNameQualifiers(roleName, documentContent);
92          if (qualifiers == null) {
93              qualifiers = new ArrayList<String>(0);
94          }
95          List<QualifiedRoleName> qualifiedRoleNames = new ArrayList<QualifiedRoleName>(qualifiers.size());
96          for (String qualifier: qualifiers) {
97              qualifiedRoleNames.add(new QualifiedRoleName(roleName, qualifier));
98          }
99          return qualifiedRoleNames;
100     }
101 
102     /**
103      * Template method responsible for producing qualifiers for a role name
104      */
105     protected List<String> getRoleNameQualifiers(String roleName, DocumentContent documentContent) {
106         return null;
107     }
108 
109     public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRoleName) {
110         QualifiedRoleName qrn = QualifiedRoleName.parse(qualifiedRoleName);
111         return resolveQualifiedRole(routeContext, qrn);
112     }
113 
114     /**
115      * Template method that delegates to {@link #resolveRecipients(RouteContext, QualifiedRoleName)} and
116      * {@link #getLabelForQualifiedRoleName(QualifiedRoleName)
117      */
118     protected ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, QualifiedRoleName qualifiedRoleName) {
119         List<Id> recipients = resolveRecipients(routeContext, qualifiedRoleName);
120         ResolvedQualifiedRole rqr = new ResolvedQualifiedRole(getLabelForQualifiedRoleName(qualifiedRoleName),
121                                                               recipients
122                                                               ); // default to no annotation...
123         return rqr;
124     }
125 
126     protected String getLabelForQualifiedRoleName(QualifiedRoleName qualifiedRoleName) {
127         // do what everybody else does and just use the base role name
128         return qualifiedRoleName.getBaseRoleName();
129     }
130 
131     /**
132      * Template method for subclasses to implement
133      */
134     protected List<Id> resolveRecipients(RouteContext routeContext, QualifiedRoleName qualifiedRoleName) {
135         return null;
136     }
137 }