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