View Javadoc

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