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.TKUser;
27  import org.kuali.hr.time.util.TKUtils;
28  import org.kuali.rice.kim.api.identity.principal.Principal;
29  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
30  
31  import java.util.ArrayList;
32  import java.util.Iterator;
33  import java.util.List;
34  import java.util.Set;
35  
36  public class TkRoleGroupServiceImpl implements TkRoleGroupService {
37  
38      private static final Logger LOG = Logger.getLogger(TkRoleGroupServiceImpl.class);
39  
40      private TkRoleGroupDao tkRoleGroupDao;
41  
42      public void setTkRoleGroupDao(TkRoleGroupDao tkRoleGroupDao) {
43          this.tkRoleGroupDao = tkRoleGroupDao;
44      }
45  
46      @Override
47      public void saveOrUpdate(List<TkRoleGroup> roleGroups) {
48          this.tkRoleGroupDao.saveOrUpdateRoleGroups(roleGroups);
49      }
50  
51      @Override
52      public void saveOrUpdate(TkRoleGroup roleGroup) {
53          this.tkRoleGroupDao.saveOrUpdateRoleGroup(roleGroup);
54      }
55  
56      @Override
57      public TkRoleGroup getRoleGroup(String principalId) {
58          return tkRoleGroupDao.getRoleGroup(principalId);
59      }
60  
61      @Override
62      public void populateRoles(TkRoleGroup tkRoleGroup) {
63          if (tkRoleGroup != null) {
64              List<TkRole> tkRoles = TkServiceLocator.getTkRoleService().getRoles(tkRoleGroup.getPrincipalId(), TKUtils.getCurrentDate());
65              List<TkRole> tkInActiveRoles = TkServiceLocator.getTkRoleService().getInactiveRoles(tkRoleGroup.getPrincipalId(), TKUtils.getCurrentDate());
66              Iterator<TkRole> itr = tkRoles.iterator();
67              while (itr.hasNext()) {
68                  TkRole tkRole = (TkRole) itr.next();
69                  if (tkRoleGroup.getPositionRoles() != null && tkRoleGroup.getPositionRoles().contains(tkRole)) {
70                      itr.remove();
71                  }
72              }
73              itr = tkInActiveRoles.iterator();
74              while (itr.hasNext()) {
75                  TkRole tkRole = (TkRole) itr.next();
76                  if (tkRoleGroup.getPositionRoles() != null && tkRoleGroup.getPositionRoles().contains(tkRole)) {
77                      itr.remove();
78                  }
79              }
80              tkRoleGroup.setRoles(tkRoles);
81              tkRoleGroup.setInactiveRoles(tkInActiveRoles);
82          }
83      }
84  
85      @Override
86      public List<TkRoleGroup> getRoleGroups(String principalId, String principalName, String workArea, String dept, String roleName) {
87  
88          List<TkRoleGroup> tkRoleGroups = new ArrayList<TkRoleGroup>();
89          String principalIdToQuery = "";
90          /**
91           * There are three different wasys to search for the roles :
92           * 1) through principalId
93           * 2) through principalName
94           * 3) search for all the roles / role groups
95           */
96          if (StringUtils.isNotBlank(principalId)) {
97              Principal person = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
98              if (person != null && isAuthorizedToEditUserRole(person.getPrincipalId())) {
99                  principalIdToQuery = person.getPrincipalId();
100             } else {
101             	principalIdToQuery = principalId;
102             }
103         } else if (StringUtils.isNotBlank(principalName)) {
104             Principal person = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName);
105             if (person != null && isAuthorizedToEditUserRole(person.getPrincipalId())) {
106                 principalIdToQuery = person.getPrincipalId();
107             } else {
108             	principalIdToQuery = null;
109             }
110         } else {
111 
112         }
113 
114         Long workAreaToQuery = StringUtils.isEmpty(workArea) ? null : Long.parseLong(workArea);
115         if(principalIdToQuery != null) {
116 	        List<TkRole> tkRoles  = TkServiceLocator.getTkRoleService().getRoles(principalIdToQuery, TKUtils.getCurrentDate(), roleName, workAreaToQuery, dept);
117 	
118 	        for (TkRole tkRole : tkRoles) {
119 	        	if (StringUtils.isEmpty(tkRole.getPositionNumber())) {
120 	        		TkRoleGroup tkRoleGroup = new TkRoleGroup();
121 	            	if (isAuthorizedToEditUserRole(tkRole.getPrincipalId())) {
122 	            		tkRoleGroup.setPerson(tkRole.getPerson());
123 	                	tkRoleGroup.setPrincipalId(tkRole.getPrincipalId());
124 	                	tkRoleGroups.add(tkRoleGroup);
125 	            	}
126 	            	if (StringUtils.isNotEmpty(principalIdToQuery)) {
127 	            		break;
128 	            	}
129 	        	} else {
130 	        		List<Job> listRolePositionActiveJobs = TkServiceLocator.getJobService().getActiveJobsForPosition(tkRole.getPositionNumber(), TKUtils.getCurrentDate());
131 	        		for (Job rolePositionJob : listRolePositionActiveJobs) {
132 	        			String rolePositionJobPrincipalId = rolePositionJob.getPrincipalId();
133 	        			TkRoleGroup tkRoleGroup = new TkRoleGroup();
134 	        			if (isAuthorizedToEditUserRole(rolePositionJobPrincipalId)) {
135 	        				if (((StringUtils.isNotEmpty(dept) && StringUtils.equals(tkRole.getDepartment(), dept)) || StringUtils.isEmpty(dept)) &&
136 	            				((StringUtils.isNotEmpty(roleName) && StringUtils.equals(tkRole.getRoleName(), roleName)) || StringUtils.isEmpty(roleName)) &&
137 	            				((StringUtils.isNotEmpty(workArea) && StringUtils.equals(tkRole.getWorkArea().toString(), workArea)) || StringUtils.isEmpty(workArea)) ) {
138 	        						tkRoleGroup.setPerson(KimApiServiceLocator.getPersonService().getPerson(rolePositionJobPrincipalId));
139 	        						tkRoleGroup.setPrincipalId(rolePositionJobPrincipalId);
140 	        						tkRoleGroups.add(tkRoleGroup);
141 	        				}
142 	        			}
143 	        		}
144 	        	}
145 	        }
146         }
147         return tkRoleGroups;
148     }
149 
150     private boolean isAuthorizedToEditUserRole(String principalId) {
151         boolean isAuthorized = false;
152         //System admin can do anything
153         if (TKUser.isSystemAdmin()) {
154             return true;
155         }
156 
157         List<Job> lstJobs = TkServiceLocator.getJobService().getJobs(principalId, TKUtils.getCurrentDate());
158         Set<String> locationAdminAreas = TKUser.getLocationAdminAreas();
159         //Confirm if any job matches this users location admin roles
160         for (String location : locationAdminAreas) {
161             for (Job job : lstJobs) {
162                 if (StringUtils.equals(location, job.getLocation())) {
163                     return true;
164                 }
165             }
166         }
167 
168         Set<String> departmentAdminAreas = TKUser.getDepartmentAdminAreas();
169         //Confirm if any job matches this users department admin roles
170         for (String dept : departmentAdminAreas) {
171             for (Job job : lstJobs) {
172                 if (StringUtils.equals(dept, job.getDept())) {
173                     return true;
174                 }
175             }
176         }
177         return isAuthorized;
178     }
179 
180 }