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.List;
020    
021    import org.kuali.rice.kew.api.identity.Id;
022    import org.kuali.rice.kew.engine.RouteContext;
023    import org.kuali.rice.kew.routeheader.DocumentContent;
024    
025    
026    /**
027     * Generic base class that implements common functionality to simplify implementing
028     * a RoleAttribute.  This includes a standard qualified role name String format and simplified
029     * template methods, as well as a generic attribute content model.
030     * 
031     * <p>Control flow:</p>
032     * 
033     * <ol>
034     *   <li>{@link #getQualifiedRoleNames(String, DocumentContent)}
035     *     <ol>
036     *       <li>{@link #generateQualifiedRoleNames(String, DocumentContent)}
037     *         <ol>
038     *           <li>{@link #getRoleNameQualifiers(String, DocumentContent)}</li>
039     *         </ol>
040     *       </li>
041     *     </ol>
042     *   </li>
043     *   <li>{@link #resolveQualifiedRole(RouteContext, String, String)}
044     *     <ol>
045     *       <li>{@link #resolveQualifiedRole(RouteContext, QualifiedRoleName)}
046     *         <ol>
047     *           <li>{@link #resolveRecipients(RouteContext, QualifiedRoleName)}</li>
048     *           <li>{@link #getLabelForQualifiedRoleName(QualifiedRoleName)}</li>
049     *         </ol>
050     *       </li>
051     *     </ol>
052     *   </li>
053     * </ol> 
054     *     
055     * @author Kuali Rice Team (rice.collab@kuali.org)
056     */
057    public abstract class GenericRoleAttribute extends GenericWorkflowAttribute implements RoleAttribute {
058        public GenericRoleAttribute() {
059            super(null);
060        }
061    
062        public GenericRoleAttribute(String uniqueName) {
063            super(uniqueName);
064        }
065    
066        public boolean isMatch(DocumentContent docContent, List<RuleExtensionBo> ruleExtensions) {
067            // FIXME: ok, this MUST return true, as it IS evaluated (don't know why)
068            // maybe investigate only calling isMatch on rule attributes as it doesn't seem that
069            // matching is relevant for role attributes...is it?
070            // throw new UnsupportedOperationException("Role attributes do not implement isMatch");
071            return true;
072        }
073    
074        public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
075            List<QualifiedRoleName> qualifiedRoleNames = generateQualifiedRoleNames(roleName, documentContent);
076            if (qualifiedRoleNames == null) {
077                return null;
078            }
079            List<String> q = new ArrayList<String>(qualifiedRoleNames.size());
080            for (QualifiedRoleName qrn: qualifiedRoleNames) {
081                q.add(qrn.encode());
082            }
083            return q;
084        }
085    
086        /**
087         * Template method responsible for producing a list of QualifiedRoleName objects.  Default implementation
088         * calls {@link #getRoleNameQualifiers(String, DocumentContent)}
089         */
090        protected List<QualifiedRoleName> generateQualifiedRoleNames(String roleName, DocumentContent documentContent) {
091            List<String> qualifiers = getRoleNameQualifiers(roleName, documentContent);
092            if (qualifiers == null) {
093                qualifiers = new ArrayList<String>(0);
094            }
095            List<QualifiedRoleName> qualifiedRoleNames = new ArrayList<QualifiedRoleName>(qualifiers.size());
096            for (String qualifier: qualifiers) {
097                qualifiedRoleNames.add(new QualifiedRoleName(roleName, qualifier));
098            }
099            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    }