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.ken.kew;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  import java.util.Map;
22  
23  import javax.xml.xpath.XPathExpressionException;
24  
25  import org.apache.log4j.Logger;
26  import org.kuali.rice.kew.api.WorkflowRuntimeException;
27  import org.kuali.rice.kew.api.identity.Id;
28  import org.kuali.rice.kew.api.identity.PrincipalName;
29  import org.kuali.rice.kew.api.rule.RoleName;
30  import org.kuali.rice.kew.engine.RouteContext;
31  import org.kuali.rice.kew.routeheader.DocumentContent;
32  import org.kuali.rice.kew.rule.GenericRoleAttribute;
33  import org.kuali.rice.kew.rule.QualifiedRoleName;
34  import org.kuali.rice.kew.rule.RuleExtensionBo;
35  import org.kuali.rice.kew.workgroup.GroupNameId;
36  
37  
38  /**
39   * KEW RoleAttribute implementation that is responsible for encapsulating a list
40   * of users and groups which are reviewers for a Notification Channel.
41   * This implementation relies on the default XML form implemented by GenericRoleAttribute
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   */
44  public class ChannelReviewerRoleAttribute extends GenericRoleAttribute {
45      private static final Logger LOG = Logger.getLogger(ChannelReviewerRoleAttribute.class);
46      private static final List<RoleName> SUPPORTED_ROLES;
47      
48      static {
49          RoleName reviewerRole = new RoleName(ChannelReviewerRoleAttribute.class.getName(), "reviewers", "Reviewers");
50          List<RoleName> tmp = new ArrayList<RoleName>(1);
51          tmp.add(reviewerRole);
52          SUPPORTED_ROLES = Collections.unmodifiableList(tmp);
53      }
54  
55      /**
56       * Constructs a ChannelReviewerRoleAttribute.java.
57       */
58      public ChannelReviewerRoleAttribute() {
59          super("channelReviewers");
60          LOG.info("CHANNEL REVIEWER ROLE ATTRIBUTE CONSTRUCTOR");
61      }
62  
63      /**
64       * @see org.kuali.rice.kew.rule.GenericRoleAttribute#isMatch(org.kuali.rice.kew.routeheader.DocumentContent, java.util.List)
65       */
66      @Override
67      public boolean isMatch(DocumentContent docContent, List<RuleExtensionBo> ruleExtensions) {
68          LOG.info("CHANNEL REVIEWER ROLE ATTRIBUTE IS MATCH");
69          return super.isMatch(docContent, ruleExtensions);
70      }
71  
72      /**
73       * @see org.kuali.rice.kew.rule.GenericWorkflowAttribute#getProperties()
74       */
75      @Override
76      public Map<String, String> getProperties() {
77          LOG.info("CHANNEL REVIEWER ROLE ATTRIBUTE GETPROPERTIES");
78          // intentionally unimplemented...not intending on using this attribute client-side
79          return null;
80      }
81  
82      /**
83       * @see org.kuali.rice.kew.rule.RoleAttribute#getRoleNames()
84       */
85      public List<RoleName> getRoleNames() {
86          LOG.info("CHANNEL REVIEWER ROLE ATTRIBUTE CALLED ROLENAMES");
87          return SUPPORTED_ROLES;
88      }
89      
90      /**
91       * @see org.kuali.rice.kew.rule.GenericRoleAttribute#getQualifiedRoleNames(java.lang.String, org.kuali.rice.kew.routeheader.DocumentContent)
92       */
93      @Override
94      public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
95          List<String> qrn = new ArrayList<String>(1);
96          qrn.add(roleName);
97          return qrn;
98      }
99  
100     /**
101      * This looks at the reviewers list passed through from KEN and then resolves the individuals that need to actually approve 
102      * the message.
103      * @see org.kuali.rice.kew.rule.GenericRoleAttribute#resolveRecipients(org.kuali.rice.kew.engine.RouteContext, org.kuali.rice.kew.rule.QualifiedRoleName)
104      */
105     @Override
106     protected List<Id> resolveRecipients(RouteContext routeContext, QualifiedRoleName qualifiedRoleName) {
107         LOG.info("CHANNEL REVIEWER ROLE ATTRIBUTE CALLED");
108         List<Id> ids = new ArrayList<Id>();
109 
110         LOG.info("DOC CONTENT:" + routeContext.getDocumentContent().getDocContent());
111         LOG.info("ATTR CONTENT:" + routeContext.getDocumentContent().getAttributeContent());
112         DocumentContent dc = routeContext.getDocumentContent();
113         List<Map<String, String>> attrs;
114         try {
115             attrs = content.parseContent(dc.getAttributeContent());
116         } catch (XPathExpressionException xpee) {
117             throw new WorkflowRuntimeException("Error parsing ChannelReviewer role attribute content", xpee);
118         }
119         
120         if (attrs.size() > 0) {
121             Map<String, String> values = attrs.get(0);
122             if (values != null) {
123                 // iterate through all "fields" and accumulate a list of users and groups
124                 for (Map.Entry<String, String> entry: values.entrySet()) {
125                     String name = entry.getKey();
126                     String value = entry.getValue();
127                     LOG.info("Entry: " + name + "=" + value);
128                     Id id;
129                     if (name.startsWith("user")) {
130                         LOG.info("Adding user: " + value);
131                         id = new PrincipalName(value);
132                         ids.add(id);
133                     } else if (name.startsWith("group")) {
134                         LOG.info("Adding group: " + value);
135                         id = new GroupNameId(value);
136                         ids.add(id);
137                     } else {
138                         LOG.error("Invalid attribute value: " + name + "=" + value);
139                     }
140                 }
141             }
142         } else {
143             LOG.debug("No attribute content found for ChannelReviewerRoleAttribute");
144         }
145         
146         LOG.info("Returning ids: " + ids.size());
147         return ids;
148     }
149 }