001/**
002 * Copyright 2005-2013 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 */
016package org.kuali.rice.kew.engine;
017
018import org.jdom.Document;
019import org.jdom.Element;
020import org.kuali.rice.core.api.util.xml.XmlHelper;
021import org.kuali.rice.kew.api.identity.Id;
022import org.kuali.rice.kew.api.rule.RoleName;
023import org.kuali.rice.kew.routeheader.DocumentContent;
024import org.kuali.rice.kew.rule.AbstractRoleAttribute;
025import org.kuali.rice.kew.rule.ResolvedQualifiedRole;
026import org.kuali.rice.kew.workgroup.GroupNameId;
027
028import java.util.ArrayList;
029import java.util.Arrays;
030import java.util.Collection;
031import java.util.Iterator;
032import java.util.List;
033
034
035public class ChartOrgDispatchAttribute extends AbstractRoleAttribute {
036
037    private static final long serialVersionUID = -9114899910238607996L;
038        public static final String DISPATCH_ROLE = "DISPATCH";
039    private String workgroupName;
040    
041    public ChartOrgDispatchAttribute() {}
042    
043    public ChartOrgDispatchAttribute(String workgroupName) {
044        this.workgroupName = workgroupName;
045    }
046
047    public List getRoleNames() {
048        return Arrays.asList(new RoleName[] { new RoleName(getClass().getName(), DISPATCH_ROLE, DISPATCH_ROLE) });
049    }
050
051    public List getQualifiedRoleNames(String roleName, DocumentContent documentContent) {
052        return parseWorkgroups(documentContent);
053    }
054
055    public ResolvedQualifiedRole resolveQualifiedRole(RouteContext routeContext, String roleName, String qualifiedRole) {
056        List<Id> recipients = new ArrayList<Id>();
057        recipients.add(new GroupNameId(qualifiedRole));
058        return new ResolvedQualifiedRole(roleName, recipients);
059    }
060
061    public String getDocContent() {
062        return "<"+DISPATCH_ROLE+">"+workgroupName+"</"+DISPATCH_ROLE+">";
063    }
064    
065    private List<String> parseWorkgroups(DocumentContent documentContent) {
066        Document document = XmlHelper.buildJDocument(documentContent.getDocument());
067        Collection<Element> dispatchElements = XmlHelper.findElements(document.getRootElement(), DISPATCH_ROLE);
068        List<String> workgroupNames = new ArrayList<String>();
069        for (Iterator<Element> iterator = dispatchElements.iterator(); iterator.hasNext();) {
070            Element element = (Element) iterator.next();
071            workgroupNames.add(element.getText());
072        }
073        return workgroupNames;
074    }
075    
076    
077    
078    
079
080}