001    /**
002     * Copyright 2004-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     */
016    package org.kuali.hr.time.roles.service;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.apache.log4j.Logger;
020    import org.kuali.hr.job.Job;
021    import org.kuali.hr.time.assignment.Assignment;
022    import org.kuali.hr.time.roles.TkRole;
023    import org.kuali.hr.time.roles.dao.TkRoleDao;
024    import org.kuali.hr.time.service.base.TkServiceLocator;
025    import org.kuali.hr.time.util.TKUser;
026    import org.kuali.hr.time.util.TkConstants;
027    
028    import java.sql.Date;
029    import java.util.ArrayList;
030    import java.util.HashSet;
031    import java.util.List;
032    import java.util.Set;
033    
034    public class TkRoleServiceImpl implements TkRoleService {
035    
036        private static final Logger LOG = Logger.getLogger(TkRoleServiceImpl.class);
037    
038            private TkRoleDao tkRoleDao;
039    
040            @Override
041            public List<TkRole> getDepartmentRoles(String department) {
042                    List<TkRole> departmentRoles = new ArrayList<TkRole>();
043                    departmentRoles.addAll(tkRoleDao.findRoles(null, null, TkConstants.ROLE_TK_DEPT_ADMIN, null, department, null));
044                    departmentRoles.addAll(tkRoleDao.findRoles(null, null, TkConstants.ROLE_TK_DEPT_VO, null, department, null));
045                    departmentRoles.addAll(tkRoleDao.findRoles(null, null, TkConstants.ROLE_LV_DEPT_ADMIN, null, department, null));
046                    departmentRoles.addAll(tkRoleDao.findRoles(null, null, TkConstants.ROLE_LV_DEPT_VO, null, department, null));
047                    return departmentRoles;
048            }
049    
050            @Override
051            public List<TkRole> getDepartmentRoles(String department, String roleName, Date asOfDate) {
052                    return tkRoleDao.findRoles(null, asOfDate, roleName, null, department, null);
053            }
054            
055            @Override
056            public List<TkRole> getDepartmentInactiveRoles(String department) {
057                    List<TkRole> departmentRoles = new ArrayList<TkRole>();
058                    departmentRoles.addAll(tkRoleDao.findInActiveRoles(null, null, TkConstants.ROLE_TK_DEPT_ADMIN, null, department, null));
059                    departmentRoles.addAll(tkRoleDao.findInActiveRoles(null, null, TkConstants.ROLE_TK_DEPT_VO, null, department, null));
060                    departmentRoles.addAll(tkRoleDao.findInActiveRoles(null, null, TkConstants.ROLE_LV_DEPT_ADMIN, null, department, null));
061                    departmentRoles.addAll(tkRoleDao.findInActiveRoles(null, null, TkConstants.ROLE_LV_DEPT_VO, null, department, null));
062                    return departmentRoles;
063            }
064            
065            @Override
066            public List<TkRole> getDepartmentInactiveRoles(String department, String roleName, Date asOfDate) {
067                    return tkRoleDao.findInActiveRoles(null, asOfDate, roleName, null, department, null);
068            }
069    
070            @Override
071            public List<TkRole> getWorkAreaRoles(Long workArea) {
072                    return tkRoleDao.findRoles(null, null, null, workArea, null, null);
073            }
074    
075            @Override
076            public List<TkRole> getWorkAreaRoles(Long workArea, String roleName, Date asOfDate) {
077                    return tkRoleDao.findRoles(null, asOfDate, roleName, workArea, null, null);
078            }
079            
080            @Override
081            public List<TkRole> getInActiveWorkAreaRoles(Long workArea) {
082                    return tkRoleDao.findInActiveRoles(null, null, null, workArea, null, null);
083            }
084    
085            @Override
086            public List<TkRole> getInActiveWorkAreaRoles(Long workArea, String roleName, Date asOfDate) {
087                    return tkRoleDao.findInActiveRoles(null, asOfDate, roleName, workArea, null, null);
088            }
089    
090            public void setTkRoleDao(TkRoleDao tkRoleDao) {
091                    this.tkRoleDao = tkRoleDao;
092            }
093    
094            @Override
095            public void saveOrUpdate(List<TkRole> roles) {
096                    this.tkRoleDao.saveOrUpdateRoles(roles);
097            }
098    
099            @Override
100            public void saveOrUpdate(TkRole role) {
101                    this.tkRoleDao.saveOrUpdateRole(role);
102            }
103    
104            /**
105             * Returns all active roles for the given principal as of the indi
106             */
107            public List<TkRole> getRoles(String principalId, Date asOfDate) {
108                    return tkRoleDao.findRoles(principalId, asOfDate, null, null, null, null);
109            }
110    
111            /**
112             * Returns all active roles for the given principal as of the indi
113             */
114            public List<TkRole> getInactiveRoles(String principalId, Date asOfDate) {
115                    return tkRoleDao.findInActiveRoles(principalId, asOfDate, null, null, null, null);
116            }
117            
118            /**
119             * Return a List of TkRoles that match the principal ID and roleName.
120             *
121             * ex:
122             *
123             * admin,TK_APPROVER will return all TK_APPROVER roles for the user admin.
124             */
125            public List<TkRole> getRoles(String principalId, String roleName, Date asOfDate) {
126                    return this.tkRoleDao.findRoles(principalId, asOfDate, roleName, null, null, null);
127            }
128    
129        /**
130         * Return a List of TkRoles that matches criteria.
131         * @param principalId
132         * @param asOfDate
133         * @param roleName
134         * @param workArea
135         * @param department
136         * @return
137         */
138        @Override
139        public List<TkRole> getRoles(String principalId, Date asOfDate, String roleName, Long workArea, String department) {
140            return this.tkRoleDao.findRoles(principalId, asOfDate, roleName, workArea, department, null);
141        }
142    
143        @Override
144        public List<String> getResponsibleParties(Assignment a, String roleName, Date asOfDate) {
145            List<String> users = new ArrayList<String>();
146    
147            List<TkRole> roles = this.getWorkAreaRoles(a.getWorkArea(), roleName, asOfDate);
148            for (TkRole role: roles) {
149                    if(StringUtils.isNotBlank(role.getPrincipalId())){
150                            users.add(role.getPrincipalId());
151                    } else if(StringUtils.isNotBlank(role.getPositionNumber())){
152                            List<Job> lstJobs = TkServiceLocator.getJobService().getActiveJobsForPosition(role.getPositionNumber(), asOfDate);
153                            for(Job job : lstJobs){
154                                    users.add(job.getPrincipalId());
155                            }
156                    
157                    }
158            }
159    
160            return users;
161        }
162    
163        @Override
164        public Set<Long> getWorkAreasForApprover(String principalId, Date asOfDate) {
165            Set<Long> workAreas = new HashSet<Long>();
166    
167            List<TkRole> roles = this.getRoles(principalId, TkConstants.ROLE_TK_APPROVER, asOfDate);
168            for (TkRole role : roles) {
169                Long wa = role.getWorkArea();
170                if (wa != null)
171                    workAreas.add(wa);
172                else
173                    LOG.warn(TkConstants.ROLE_TK_APPROVER + " found without WorkArea number, ignoring roleId: " + role.getHrRolesId());
174            }
175    
176            return workAreas;
177        }
178    
179    
180    
181        @Override
182        public Set<String> getActivePrincipalsForWorkAreas(Set<Long> workAreas, Date asOfDate) {
183            Set<String> principals = new HashSet<String>();
184    
185            for (Long workArea : workAreas) {
186                List<Assignment> assignments = TkServiceLocator.getAssignmentService().getActiveAssignmentsForWorkArea(workArea, asOfDate);
187                for (Assignment assignment : assignments) {
188                    principals.add(assignment.getPrincipalId());
189                }
190            }
191    
192            return principals;
193        }
194    
195            @Override
196            public TkRole getRole(String tkRoleId) {
197                    return tkRoleDao.getRole(tkRoleId);
198            }
199            
200            @Override
201            public TkRole getRolesByPosition(String positionNumber) {
202                    return tkRoleDao.getRolesByPosition(positionNumber);
203            }
204            
205            @Override
206            public TkRole getInactiveRolesByPosition(String positionNumber) {
207                    return tkRoleDao.getInactiveRolesByPosition(positionNumber);
208            }
209    
210            @Override
211            public List<TkRole> getPositionRolesForWorkArea(Long workArea, Date asOfDate) {
212                    return tkRoleDao.getPositionRolesForWorkArea(workArea, asOfDate);
213            }
214    }