View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.opensource.org/licenses/ecl2.php
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.rule;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.kuali.rice.kew.engine.RouteContext;
23  import org.kuali.rice.kew.identity.Id;
24  import org.kuali.rice.kew.routeheader.DocumentContent;
25  
26  
27  /**
28   * Generic base class that implements common functionality to simplify implementing
29   * a RoleAttribute.  This includes a standard qualified role name String format and simplified
30   * template methods, as well as a generic attribute content model.
31   * 
32   * <p>Control flow:</p>
33   * 
34   * <ol>
35   *   <li>{@link #getQualifiedRoleNames(String, DocumentContent)}
36   *     <ol>
37   *       <li>{@link #generateQualifiedRoleNames(String, DocumentContent)}
38   *         <ol>
39   *           <li>{@link #getRoleNameQualifiers(String, DocumentContent)}</li>
40   *         </ol>
41   *       </li>
42   *     </ol>
43   *   </li>
44   *   <li>{@link #resolveQualifiedRole(RouteContext, String, String)}
45   *     <ol>
46   *       <li>{@link #resolveQualifiedRole(RouteContext, QualifiedRoleName)}
47   *         <ol>
48   *           <li>{@link #resolveRecipients(RouteContext, QualifiedRoleName)}</li>
49   *           <li>{@link #getLabelForQualifiedRoleName(QualifiedRoleName)}</li>
50   *         </ol>
51   *       </li>
52   *     </ol>
53   *   </li>
54   * </ol> 
55   *     
56   * @author Kuali Rice Team (rice.collab@kuali.org)
57   */
58  public abstract class GenericRoleAttribute extends GenericWorkflowAttribute implements RoleAttribute {
59      public GenericRoleAttribute() {
60          super(null);
61      }
62  
63      public GenericRoleAttribute(String uniqueName) {
64          super(uniqueName);
65      }
66  
67      public boolean isMatch(DocumentContent docContent, List<RuleExtension> ruleExtensions) {
68          // FIXME: ok, this MUST return true, as it IS evaluated (don't know why)
69          // maybe investigate only calling isMatch on rule attributes as it doesn't seem that
70          // matching is relevant for role attributes...is it?
71          // throw new UnsupportedOperationException("Role attributes do not implement isMatch");
72          return true;
73      }
74  
75      public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
76          List<QualifiedRoleName> qualifiedRoleNames = generateQualifiedRoleNames(roleName, documentContent);
77          if (qualifiedRoleNames == null) {
78              return null;
79          }
80          List<String> q = new ArrayList<String>(qualifiedRoleNames.size());
81          for (QualifiedRoleName qrn: qualifiedRoleNames) {
82              q.add(qrn.encode());
83          }
84          return q;
85      }
86  
87      /**
88       * Template method responsible for producing a list of QualifiedRoleName objects.  Default implementation
89       * calls {@link #getRoleNameQualifiers(String, DocumentContent)}
90       */
91      protected List<QualifiedRoleName> generateQualifiedRoleNames(String roleName, DocumentContent documentContent) {
92          List<String> qualifiers = getRoleNameQualifiers(roleName, documentContent);
93          if (qualifiers == null) {
94              qualifiers = new ArrayList<String>(0);
95          }
96          List<QualifiedRoleName> qualifiedRoleNames = new ArrayList<QualifiedRoleName>(qualifiers.size());
97          for (String qualifier: qualifiers) {
98              qualifiedRoleNames.add(new QualifiedRoleName(roleName, qualifier));
99          }
100         return qualifiedRoleNames;
101     }
102 
103     /**
104      * Template method responsible for producing qualifiers for a role name
105      */
106     protected List<String> getRoleNameQualifiers(String roleName, DocumentContent documentContent) {
107         return null;
108     }
109 
110     public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRoleName) {
111         QualifiedRoleName qrn = QualifiedRoleName.parse(qualifiedRoleName);
112         return resolveQualifiedRole(routeContext, qrn);
113     }
114 
115     /**
116      * Template method that delegates to {@link #resolveRecipients(RouteContext, QualifiedRoleName)} and
117      * {@link #getLabelForQualifiedRoleName(QualifiedRoleName)
118      */
119     protected ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, QualifiedRoleName qualifiedRoleName) {
120         List<Id> recipients = resolveRecipients(routeContext, qualifiedRoleName);
121         ResolvedQualifiedRole rqr = new ResolvedQualifiedRole(getLabelForQualifiedRoleName(qualifiedRoleName),
122                                                               recipients
123                                                               ); // default to no annotation...
124         return rqr;
125     }
126 
127     protected String getLabelForQualifiedRoleName(QualifiedRoleName qualifiedRoleName) {
128         // do what everybody else does and just use the base role name
129         return qualifiedRoleName.getBaseRoleName();
130     }
131 
132     /**
133      * Template method for subclasses to implement
134      */
135     protected List<Id> resolveRecipients(RouteContext routeContext, QualifiedRoleName qualifiedRoleName) {
136         return null;
137     }
138 }