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.tklm.time.clocklog.web;
17  
18  import java.util.*;
19  
20  import org.kuali.kpme.core.api.department.Department;
21  import org.kuali.kpme.core.api.namespace.KPMENamespace;
22  import org.kuali.kpme.core.api.permission.KPMEPermissionTemplate;
23  import org.kuali.kpme.core.lookup.KPMELookupableImpl;
24  import org.kuali.kpme.core.role.KPMERoleMemberAttribute;
25  import org.kuali.kpme.core.service.HrServiceLocator;
26  import org.kuali.kpme.core.workarea.WorkAreaBo;
27  import org.kuali.kpme.tklm.time.clocklog.ClockLogBo;
28  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
29  import org.kuali.rice.kim.api.KimConstants;
30  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
31  import org.kuali.rice.krad.lookup.LookupUtils;
32  import org.kuali.rice.krad.uif.view.LookupView;
33  import org.kuali.rice.krad.util.GlobalVariables;
34  import org.kuali.rice.krad.web.form.LookupForm;
35  
36  public class ClockLogLookupableImpl extends KPMELookupableImpl {
37  
38  	@Override
39  	public void initSuppressAction(LookupForm lookupForm) {
40  		((LookupView) lookupForm.getView()).setSuppressActions(false);
41  	}
42  
43  	@Override
44  	public List<?> getSearchResults(LookupForm form, Map<String, String> searchCriteria, boolean unbounded) {
45          String userPrincipalId = GlobalVariables.getUserSession().getPrincipalId();
46  
47          Integer searchResultsLimit = null;
48  
49          Collection<?> rawSearchResults;
50  
51          // removed blank search values and decrypt any encrypted search values
52          Map<String, String> nonBlankSearchCriteria = processSearchCriteria(form, searchCriteria);
53  
54          if (nonBlankSearchCriteria == null) {
55              return new ArrayList<Object>();
56          }
57  
58          if (!unbounded) {
59              searchResultsLimit = LookupUtils.getSearchResultsLimit(getDataObjectClass(), form);
60          }
61  
62          rawSearchResults = getLookupService().findCollectionBySearchHelper(getDataObjectClass(),
63                  nonBlankSearchCriteria, unbounded, searchResultsLimit);
64  
65          if (rawSearchResults == null) {
66              rawSearchResults = new ArrayList<Object>();
67          } else {
68              sortSearchResults(form, (List<?>) rawSearchResults);
69          }
70  
71          List<ClockLogBo> filteredResults = filterLookupResults((List<ClockLogBo>)rawSearchResults, userPrincipalId);
72  
73          generateLookupResultsMessages(form, nonBlankSearchCriteria, filteredResults, unbounded);
74  
75  		for (ClockLogBo searchResult : filteredResults) {
76  			if(searchResult != null) {
77                  searchResult.setClockedByMissedPunch(TkServiceLocator.getClockLogService().isClockLogCreatedByMissedPunch(searchResult.getTkClockLogId()));
78  			}
79  		}
80  
81  		return filteredResults;
82  	}
83  
84      protected List<ClockLogBo> filterLookupResults(List<ClockLogBo> rawResults, String userPrincipalId) {
85          List<ClockLogBo> results = new ArrayList<ClockLogBo>();
86          for (ClockLogBo clockLogObj : rawResults) {
87  
88  
89              String department = clockLogObj.getDept();
90              String groupKeyCode = clockLogObj.getGroupKeyCode();
91              Department departmentObj = HrServiceLocator.getDepartmentService().getDepartment(department, groupKeyCode, clockLogObj.getClockDateTime().toLocalDate());
92              String location = departmentObj != null ? departmentObj.getGroupKey().getLocationId() : null;
93  
94              Map<String, String> roleQualification = new HashMap<String, String>();
95  
96              roleQualification.put(KimConstants.AttributeConstants.PRINCIPAL_ID, userPrincipalId);
97              roleQualification.put(KPMERoleMemberAttribute.DEPARTMENT.getRoleMemberAttributeName(), department);
98              roleQualification.put(KPMERoleMemberAttribute.GROUP_KEY_CODE.getRoleMemberAttributeName(), groupKeyCode);
99              roleQualification.put(KPMERoleMemberAttribute.LOCATION.getRoleMemberAttributeName(), location);
100 
101             if (!KimApiServiceLocator.getPermissionService().isPermissionDefinedByTemplate(KPMENamespace.KPME_WKFLW.getNamespaceCode(),
102                     KPMEPermissionTemplate.VIEW_KPME_RECORD.getPermissionTemplateName(), new HashMap<String, String>())
103                     || KimApiServiceLocator.getPermissionService().isAuthorizedByTemplate(userPrincipalId, KPMENamespace.KPME_WKFLW.getNamespaceCode(),
104                     KPMEPermissionTemplate.VIEW_KPME_RECORD.getPermissionTemplateName(), new HashMap<String, String>(), roleQualification)) {
105                 results.add(clockLogObj);
106             }
107         }
108 
109         return results;
110     }
111 
112 }