View Javadoc

1   /**
2    * Copyright 2004-2014 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.kpme.core.location.service;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.HashSet;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import org.apache.commons.collections.CollectionUtils;
26  import org.joda.time.LocalDate;
27  import org.kuali.kpme.core.KPMENamespace;
28  import org.kuali.kpme.core.location.Location;
29  import org.kuali.kpme.core.location.dao.LocationDao;
30  import org.kuali.kpme.core.permission.KPMEPermissionTemplate;
31  import org.kuali.kpme.core.role.KPMERole;
32  import org.kuali.kpme.core.role.KPMERoleMemberAttribute;
33  import org.kuali.kpme.core.role.location.LocationPrincipalRoleMemberBo;
34  import org.kuali.kpme.core.service.HrServiceLocator;
35  import org.kuali.rice.kim.api.KimConstants;
36  import org.kuali.rice.kim.api.role.RoleMember;
37  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
38  import org.kuali.rice.kim.impl.role.RoleMemberBo;
39  
40  public class LocationServiceImpl implements LocationService {
41  
42  	private LocationDao locationDao;
43  	
44  	public LocationDao getLocationDao() {
45  		return locationDao;
46  	}
47  	
48  	public void setLocationDao(LocationDao locationDao) {
49  		this.locationDao = locationDao;
50  	}
51  	
52  	@Override
53  	public Location getLocation(String hrLocationId) {
54  		Location locationObj = locationDao.getLocation(hrLocationId);
55  		
56  		if (locationObj != null) {
57  			populateLocationRoleMembers(locationObj, locationObj.getEffectiveLocalDate());
58  		}
59  		
60  		return locationObj;
61  	}
62  	
63  	@Override
64  	public int getLocationCount(String location,  LocalDate asOfDate) {
65  		return locationDao.getLocationCount(location, asOfDate);
66  	}
67  	
68  	public Location getLocation(String location, LocalDate asOfDate) {
69  		Location locationObj = locationDao.getLocation(location, asOfDate);
70  		
71  		if (locationObj != null) {
72  			populateLocationRoleMembers(locationObj, asOfDate);
73  		}
74  		
75  		return locationObj;
76  	}
77  
78      @Override
79      public List<Location> getLocations(String location) {
80          return locationDao.getLocations(location);
81      }
82  
83      private void populateLocationRoleMembers(Location location, LocalDate asOfDate) {
84      	if (location != null && asOfDate != null    			
85      			&& CollectionUtils.isEmpty(location.getRoleMembers()) && CollectionUtils.isEmpty(location.getInactiveRoleMembers())) {
86      		Set<RoleMember> roleMembers = new HashSet<RoleMember>();
87  	    	roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInLocation(KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_LOCATION_VIEW_ONLY.getRoleName(), location.getLocation(), asOfDate.toDateTimeAtStartOfDay(), false));
88  	    	roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInLocation(KPMENamespace.KPME_TK.getNamespaceCode(), KPMERole.TIME_LOCATION_ADMINISTRATOR.getRoleName(), location.getLocation(), asOfDate.toDateTimeAtStartOfDay(), false));
89  	    	roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInLocation(KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_LOCATION_VIEW_ONLY.getRoleName(), location.getLocation(), asOfDate.toDateTimeAtStartOfDay(), false));
90  	    	roleMembers.addAll(HrServiceLocator.getKPMERoleService().getRoleMembersInLocation(KPMENamespace.KPME_LM.getNamespaceCode(), KPMERole.LEAVE_LOCATION_ADMINISTRATOR.getRoleName(), location.getLocation(), asOfDate.toDateTimeAtStartOfDay(), false));
91  	
92  	    	for (RoleMember roleMember : roleMembers) {
93  	    		RoleMemberBo roleMemberBo = RoleMemberBo.from(roleMember);
94  	    		
95  	    		if (roleMemberBo.isActive()) {
96  	    			location.addRoleMember(LocationPrincipalRoleMemberBo.from(roleMemberBo, roleMember.getAttributes()));
97  	    		} else {
98  	    			location.addInactiveRoleMember(LocationPrincipalRoleMemberBo.from(roleMemberBo, roleMember.getAttributes()));
99  	    		}
100 	    	}
101     	}
102     }
103 
104     @Override
105     public List<Location> searchLocations(String userPrincipalId, String location, String locationDescr, String active, String showHistory) {
106     	List<Location> results = new ArrayList<Location>();
107     	
108     	List<Location> locationObjs = locationDao.searchLocations(location, locationDescr, active, showHistory);
109     
110     	for (Location locationObj : locationObjs) {
111         	Map<String, String> roleQualification = new HashMap<String, String>();
112         	roleQualification.put(KimConstants.AttributeConstants.PRINCIPAL_ID, userPrincipalId);
113         	roleQualification.put(KPMERoleMemberAttribute.LOCATION.getRoleMemberAttributeName(), locationObj.getLocation());
114         	
115         	if (!KimApiServiceLocator.getPermissionService().isPermissionDefinedByTemplate(KPMENamespace.KPME_WKFLW.getNamespaceCode(),
116     				KPMEPermissionTemplate.VIEW_KPME_RECORD.getPermissionTemplateName(), new HashMap<String, String>())
117     		  || KimApiServiceLocator.getPermissionService().isAuthorizedByTemplate(userPrincipalId, KPMENamespace.KPME_WKFLW.getNamespaceCode(),
118     				  KPMEPermissionTemplate.VIEW_KPME_RECORD.getPermissionTemplateName(), new HashMap<String, String>(), roleQualification)) {
119         		results.add(locationObj);
120         	}
121     	}
122     	
123     	return results;
124     }
125 
126     @Override
127     public List<Location> getNewerVersionLocation(String location, LocalDate asOfDate) {
128         return locationDao.getNewerVersionLocation(location, asOfDate);
129     }
130 }