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.ken.kew;
017    
018    import java.util.ArrayList;
019    import java.util.Collections;
020    import java.util.List;
021    import java.util.Map;
022    
023    import javax.xml.xpath.XPathExpressionException;
024    
025    import org.apache.log4j.Logger;
026    import org.kuali.rice.kew.api.WorkflowRuntimeException;
027    import org.kuali.rice.kew.api.identity.Id;
028    import org.kuali.rice.kew.api.identity.PrincipalName;
029    import org.kuali.rice.kew.api.rule.RoleName;
030    import org.kuali.rice.kew.engine.RouteContext;
031    import org.kuali.rice.kew.routeheader.DocumentContent;
032    import org.kuali.rice.kew.rule.GenericRoleAttribute;
033    import org.kuali.rice.kew.rule.QualifiedRoleName;
034    import org.kuali.rice.kew.rule.RuleExtensionBo;
035    import org.kuali.rice.kew.workgroup.GroupNameId;
036    
037    
038    /**
039     * KEW RoleAttribute implementation that is responsible for encapsulating a list
040     * of users and groups which are reviewers for a Notification Channel.
041     * This implementation relies on the default XML form implemented by GenericRoleAttribute
042     * @author Kuali Rice Team (rice.collab@kuali.org)
043     */
044    public class ChannelReviewerRoleAttribute extends GenericRoleAttribute {
045        private static final Logger LOG = Logger.getLogger(ChannelReviewerRoleAttribute.class);
046        private static final List<RoleName> SUPPORTED_ROLES;
047        
048        static {
049            RoleName reviewerRole = new RoleName(ChannelReviewerRoleAttribute.class.getName(), "reviewers", "Reviewers");
050            List<RoleName> tmp = new ArrayList<RoleName>(1);
051            tmp.add(reviewerRole);
052            SUPPORTED_ROLES = Collections.unmodifiableList(tmp);
053        }
054    
055        /**
056         * Constructs a ChannelReviewerRoleAttribute.java.
057         */
058        public ChannelReviewerRoleAttribute() {
059            super("channelReviewers");
060            LOG.info("CHANNEL REVIEWER ROLE ATTRIBUTE CONSTRUCTOR");
061        }
062    
063        /**
064         * @see org.kuali.rice.kew.rule.GenericRoleAttribute#isMatch(org.kuali.rice.kew.routeheader.DocumentContent, java.util.List)
065         */
066        @Override
067        public boolean isMatch(DocumentContent docContent, List<RuleExtensionBo> ruleExtensions) {
068            LOG.info("CHANNEL REVIEWER ROLE ATTRIBUTE IS MATCH");
069            return super.isMatch(docContent, ruleExtensions);
070        }
071    
072        /**
073         * @see org.kuali.rice.kew.rule.GenericWorkflowAttribute#getProperties()
074         */
075        @Override
076        public Map<String, String> getProperties() {
077            LOG.info("CHANNEL REVIEWER ROLE ATTRIBUTE GETPROPERTIES");
078            // intentionally unimplemented...not intending on using this attribute client-side
079            return null;
080        }
081    
082        /**
083         * @see org.kuali.rice.kew.rule.RoleAttribute#getRoleNames()
084         */
085        public List<RoleName> getRoleNames() {
086            LOG.info("CHANNEL REVIEWER ROLE ATTRIBUTE CALLED ROLENAMES");
087            return SUPPORTED_ROLES;
088        }
089        
090        /**
091         * @see org.kuali.rice.kew.rule.GenericRoleAttribute#getQualifiedRoleNames(java.lang.String, org.kuali.rice.kew.routeheader.DocumentContent)
092         */
093        @Override
094        public List<String> getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
095            List<String> qrn = new ArrayList<String>(1);
096            qrn.add(roleName);
097            return qrn;
098        }
099    
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    }