001/**
002 * Copyright 2004-2014 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.kpme.tklm.time.workflow;
017
018import java.util.ArrayList;
019import java.util.HashSet;
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.commons.lang.StringUtils;
025import org.apache.commons.lang.math.NumberUtils;
026import org.apache.log4j.Logger;
027import org.joda.time.DateTime;
028import org.kuali.kpme.core.api.namespace.KPMENamespace;
029import org.kuali.kpme.core.role.KPMERole;
030import org.kuali.kpme.core.service.HrServiceLocator;
031import org.kuali.kpme.tklm.time.missedpunch.MissedPunchBo;
032import org.kuali.kpme.tklm.time.missedpunch.MissedPunchDocument;
033import org.kuali.rice.kew.api.exception.WorkflowException;
034import org.kuali.rice.kew.api.identity.Id;
035import org.kuali.rice.kew.api.identity.PrincipalId;
036import org.kuali.rice.kew.api.rule.RoleName;
037import org.kuali.rice.kew.engine.RouteContext;
038import org.kuali.rice.kew.routeheader.DocumentContent;
039import org.kuali.rice.kew.rule.GenericRoleAttribute;
040import org.kuali.rice.kew.rule.QualifiedRoleName;
041import org.kuali.rice.kim.api.role.RoleMember;
042import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
043
044@SuppressWarnings("unchecked")
045public class MissedPunchRoleAttribute extends GenericRoleAttribute {
046
047        private static final long serialVersionUID = -7188190168685823002L;
048        
049        private static final Logger LOG = Logger.getLogger(MissedPunchRoleAttribute.class);
050
051        @Override
052        public List<RoleName> getRoleNames() {
053                List<RoleName> roleNames = new ArrayList<RoleName>();
054                
055                roleNames.add(RoleName.Builder.create(getClass().getName(), KPMERole.APPROVER.getRoleName(), KPMERole.APPROVER.getRoleName()).build());
056                roleNames.add(RoleName.Builder.create(getClass().getName(), KPMERole.APPROVER_DELEGATE.getRoleName(), KPMERole.APPROVER_DELEGATE.getRoleName()).build());
057
058                return roleNames;
059        }
060        
061        @Override
062    protected List<String> getRoleNameQualifiers(String roleName, DocumentContent documentContent) {
063                Set<String> roleNameQualifiers = new HashSet<String>();
064                
065                try {
066                        String documentId = documentContent.getRouteContext().getDocument().getDocumentId();
067                        MissedPunchDocument missedPunchDocument = (MissedPunchDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(documentId);
068                        MissedPunchBo missedPunch = missedPunchDocument.getMissedPunch();
069                roleNameQualifiers.add(String.valueOf(missedPunch.getWorkArea()));
070                } catch (WorkflowException we) {
071                        we.printStackTrace();
072                }
073
074                return new ArrayList<String>(roleNameQualifiers);
075    }
076        
077        @Override
078    protected List<Id> resolveRecipients(RouteContext routeContext, QualifiedRoleName qualifiedRoleName) {
079                List<Id> recipients = new ArrayList<Id>();
080                
081                String roleName = qualifiedRoleName.getBaseRoleName();
082                String qualifier = qualifiedRoleName.getQualifier();
083                
084                if (StringUtils.isNotBlank(roleName) && NumberUtils.isNumber(qualifier)) {
085                        Long workArea = Long.valueOf(qualifier);
086        
087                        List<RoleMember> roleMembers = HrServiceLocator.getKPMERoleService().getRoleMembersInWorkArea(KPMENamespace.KPME_HR.getNamespaceCode(), roleName, workArea, new DateTime(), true);
088                        
089                for (RoleMember roleMember : roleMembers) {
090                        recipients.add(new PrincipalId(roleMember.getMemberId()));
091                    }
092                } else {
093                        LOG.error("Could not route to role " + roleName + " with work area " + qualifier);
094                }
095                
096                return recipients;
097    }
098
099        @Override
100        public Map<String, String> getProperties() {
101                return null;
102        }
103
104}