View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.roles.service;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.hr.job.Job;
21  import org.kuali.hr.time.roles.TkRole;
22  import org.kuali.hr.time.roles.TkRoleGroup;
23  import org.kuali.hr.time.roles.dao.TkRoleGroupDao;
24  import org.kuali.hr.time.service.base.TkServiceLocator;
25  import org.kuali.hr.time.util.TKContext;
26  import org.kuali.hr.time.util.TKUtils;
27  import org.kuali.rice.kim.api.identity.Person;
28  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
29  
30  import java.util.ArrayList;
31  import java.util.Iterator;
32  import java.util.List;
33  import java.util.Set;
34  
35  public class TkRoleGroupServiceImpl implements TkRoleGroupService {
36  
37      private static final Logger LOG = Logger.getLogger(TkRoleGroupServiceImpl.class);
38  
39      private TkRoleGroupDao tkRoleGroupDao;
40  
41      public void setTkRoleGroupDao(TkRoleGroupDao tkRoleGroupDao) {
42          this.tkRoleGroupDao = tkRoleGroupDao;
43      }
44  
45      @Override
46      public void saveOrUpdate(List<TkRoleGroup> roleGroups) {
47          this.tkRoleGroupDao.saveOrUpdateRoleGroups(roleGroups);
48      }
49  
50      @Override
51      public void saveOrUpdate(TkRoleGroup roleGroup) {
52          this.tkRoleGroupDao.saveOrUpdateRoleGroup(roleGroup);
53      }
54  
55      @Override
56      public TkRoleGroup getRoleGroup(String principalId) {
57          return tkRoleGroupDao.getRoleGroup(principalId);
58      }
59  
60      @Override
61      public void populateRoles(TkRoleGroup tkRoleGroup) {
62          if (tkRoleGroup != null) {
63              List<TkRole> tkRoles = TkServiceLocator.getTkRoleService().getRoles(tkRoleGroup.getPrincipalId(), TKUtils.getCurrentDate());
64              List<TkRole> tkInActiveRoles = TkServiceLocator.getTkRoleService().getInactiveRoles(tkRoleGroup.getPrincipalId(), TKUtils.getCurrentDate());
65              Iterator<TkRole> itr = tkRoles.iterator();
66              while (itr.hasNext()) {
67                  TkRole tkRole = (TkRole) itr.next();
68                  if (tkRoleGroup.getPositionRoles() != null && tkRoleGroup.getPositionRoles().contains(tkRole)) {
69                      itr.remove();
70                  }
71              }
72              itr = tkInActiveRoles.iterator();
73              while (itr.hasNext()) {
74                  TkRole tkRole = (TkRole) itr.next();
75                  if (tkRoleGroup.getPositionRoles() != null && tkRoleGroup.getPositionRoles().contains(tkRole)) {
76                      itr.remove();
77                  }
78              }
79              tkRoleGroup.setRoles(tkRoles);
80              tkRoleGroup.setInactiveRoles(tkInActiveRoles);
81          }
82      }
83  
84      @Override
85      public List<TkRoleGroup> getRoleGroups(String principalId, String principalName, String workArea, String dept, String roleName) {
86  
87          List<TkRoleGroup> tkRoleGroups = new ArrayList<TkRoleGroup>();
88          String principalIdToQuery = "";
89          /**
90           * There are three different wasys to search for the roles :
91           * 1) through principalId
92           * 2) through principalName
93           * 3) search for all the roles / role groups
94           */
95          if (StringUtils.isNotBlank(principalId)) {
96              Person person = KimApiServiceLocator.getPersonService().getPerson(principalId);
97              if (person != null && isAuthorizedToEditUserRole(person.getPrincipalId())) {
98                  principalIdToQuery = person.getPrincipalId();
99              } 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 }