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.roles.TkRole;
022    import org.kuali.hr.time.roles.TkRoleGroup;
023    import org.kuali.hr.time.roles.dao.TkRoleGroupDao;
024    import org.kuali.hr.time.service.base.TkServiceLocator;
025    import org.kuali.hr.time.util.TKContext;
026    import org.kuali.hr.time.util.TKUtils;
027    import org.kuali.rice.kim.api.identity.Person;
028    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
029    
030    import java.util.ArrayList;
031    import java.util.Iterator;
032    import java.util.List;
033    import java.util.Set;
034    
035    public class TkRoleGroupServiceImpl implements TkRoleGroupService {
036    
037        private static final Logger LOG = Logger.getLogger(TkRoleGroupServiceImpl.class);
038    
039        private TkRoleGroupDao tkRoleGroupDao;
040    
041        public void setTkRoleGroupDao(TkRoleGroupDao tkRoleGroupDao) {
042            this.tkRoleGroupDao = tkRoleGroupDao;
043        }
044    
045        @Override
046        public void saveOrUpdate(List<TkRoleGroup> roleGroups) {
047            this.tkRoleGroupDao.saveOrUpdateRoleGroups(roleGroups);
048        }
049    
050        @Override
051        public void saveOrUpdate(TkRoleGroup roleGroup) {
052            this.tkRoleGroupDao.saveOrUpdateRoleGroup(roleGroup);
053        }
054    
055        @Override
056        public TkRoleGroup getRoleGroup(String principalId) {
057            return tkRoleGroupDao.getRoleGroup(principalId);
058        }
059    
060        @Override
061        public void populateRoles(TkRoleGroup tkRoleGroup) {
062            if (tkRoleGroup != null) {
063                List<TkRole> tkRoles = TkServiceLocator.getTkRoleService().getRoles(tkRoleGroup.getPrincipalId(), TKUtils.getCurrentDate());
064                List<TkRole> tkInActiveRoles = TkServiceLocator.getTkRoleService().getInactiveRoles(tkRoleGroup.getPrincipalId(), TKUtils.getCurrentDate());
065                Iterator<TkRole> itr = tkRoles.iterator();
066                while (itr.hasNext()) {
067                    TkRole tkRole = (TkRole) itr.next();
068                    if (tkRoleGroup.getPositionRoles() != null && tkRoleGroup.getPositionRoles().contains(tkRole)) {
069                        itr.remove();
070                    }
071                }
072                itr = tkInActiveRoles.iterator();
073                while (itr.hasNext()) {
074                    TkRole tkRole = (TkRole) itr.next();
075                    if (tkRoleGroup.getPositionRoles() != null && tkRoleGroup.getPositionRoles().contains(tkRole)) {
076                        itr.remove();
077                    }
078                }
079                tkRoleGroup.setRoles(tkRoles);
080                tkRoleGroup.setInactiveRoles(tkInActiveRoles);
081            }
082        }
083    
084        @Override
085        public List<TkRoleGroup> getRoleGroups(String principalId, String principalName, String workArea, String dept, String roleName) {
086    
087            List<TkRoleGroup> tkRoleGroups = new ArrayList<TkRoleGroup>();
088            String principalIdToQuery = "";
089            /**
090             * There are three different wasys to search for the roles :
091             * 1) through principalId
092             * 2) through principalName
093             * 3) search for all the roles / role groups
094             */
095            if (StringUtils.isNotBlank(principalId)) {
096                Person person = KimApiServiceLocator.getPersonService().getPerson(principalId);
097                if (person != null && isAuthorizedToEditUserRole(person.getPrincipalId())) {
098                    principalIdToQuery = person.getPrincipalId();
099                } else {
100                    principalIdToQuery = principalId;
101                }
102            } else if (StringUtils.isNotBlank(principalName)) {
103                Person person = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(principalName);
104                if (person != null && isAuthorizedToEditUserRole(person.getPrincipalId())) {
105                    principalIdToQuery = person.getPrincipalId();
106                } else {
107                    principalIdToQuery = null;
108                }
109            } else {
110    
111            }
112    
113            Long workAreaToQuery = StringUtils.isEmpty(workArea) ? null : Long.parseLong(workArea);
114            if(principalIdToQuery != null) {
115                    List<TkRole> tkRoles  = TkServiceLocator.getTkRoleService().getRoles(principalIdToQuery, TKUtils.getCurrentDate(), roleName, workAreaToQuery, dept);
116            
117                    for (TkRole tkRole : tkRoles) {
118                            if (StringUtils.isEmpty(tkRole.getPositionNumber())) {
119                                    TkRoleGroup tkRoleGroup = new TkRoleGroup();
120                            if (isAuthorizedToEditUserRole(tkRole.getPrincipalId())) {
121                                    tkRoleGroup.setPerson(tkRole.getPerson());
122                                    tkRoleGroup.setPrincipalId(tkRole.getPrincipalId());
123                                    tkRoleGroups.add(tkRoleGroup);
124                            }
125                            if (StringUtils.isNotEmpty(principalIdToQuery)) {
126                                    break;
127                            }
128                            } else {
129                                    List<Job> listRolePositionActiveJobs = TkServiceLocator.getJobService().getActiveJobsForPosition(tkRole.getPositionNumber(), TKUtils.getCurrentDate());
130                                    for (Job rolePositionJob : listRolePositionActiveJobs) {
131                                            String rolePositionJobPrincipalId = rolePositionJob.getPrincipalId();
132                                            TkRoleGroup tkRoleGroup = new TkRoleGroup();
133                                            if (isAuthorizedToEditUserRole(rolePositionJobPrincipalId)) {
134                                                    if (((StringUtils.isNotEmpty(dept) && StringUtils.equals(tkRole.getDepartment(), dept)) || StringUtils.isEmpty(dept)) &&
135                                                    ((StringUtils.isNotEmpty(roleName) && StringUtils.equals(tkRole.getRoleName(), roleName)) || StringUtils.isEmpty(roleName)) &&
136                                                    ((StringUtils.isNotEmpty(workArea) && StringUtils.equals(tkRole.getWorkArea().toString(), workArea)) || StringUtils.isEmpty(workArea)) ) {
137                                                                    tkRoleGroup.setPerson(KimApiServiceLocator.getPersonService().getPerson(rolePositionJobPrincipalId));
138                                                                    tkRoleGroup.setPrincipalId(rolePositionJobPrincipalId);
139                                                                    tkRoleGroups.add(tkRoleGroup);
140                                                    }
141                                            }
142                                    }
143                            }
144                    }
145            }
146            return tkRoleGroups;
147        }
148    
149        private boolean isAuthorizedToEditUserRole(String principalId) {
150            boolean isAuthorized = false;
151            //System admin can do anything
152            if (TKContext.getUser().isSystemAdmin()) {
153                return true;
154            }
155    
156            List<Job> lstJobs = TkServiceLocator.getJobService().getJobs(principalId, TKUtils.getCurrentDate());
157            Set<String> locationAdminAreas = TKContext.getUser().getLocationAdminAreas();
158            //Confirm if any job matches this users location admin roles
159            for (String location : locationAdminAreas) {
160                for (Job job : lstJobs) {
161                    if (StringUtils.equals(location, job.getLocation())) {
162                        return true;
163                    }
164                }
165            }
166    
167            Set<String> departmentAdminAreas = TKContext.getUser().getDepartmentAdminAreas();
168            //Confirm if any job matches this users department admin roles
169            for (String dept : departmentAdminAreas) {
170                for (Job job : lstJobs) {
171                    if (StringUtils.equals(dept, job.getDept())) {
172                        return true;
173                    }
174                }
175            }
176            return isAuthorized;
177        }
178    
179    }