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 java.util.ArrayList;
019    import java.util.HashMap;
020    import java.util.List;
021    import java.util.Map;
022    import java.util.Properties;
023    
024    import org.apache.commons.lang.StringUtils;
025    import org.kuali.hr.time.HrEffectiveDateActiveLookupableHelper;
026    import org.kuali.hr.time.roles.TkRoleGroup;
027    import org.kuali.hr.time.service.base.TkServiceLocator;
028    import org.kuali.hr.time.util.TKContext;
029    import org.kuali.hr.time.util.TKUtils;
030    import org.kuali.rice.kns.lookup.HtmlData;
031    import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
032    import org.kuali.rice.krad.bo.BusinessObject;
033    import org.kuali.rice.krad.util.KRADConstants;
034    import org.kuali.rice.krad.util.UrlFactory;
035    
036    public class TkRoleLookupableHelper extends HrEffectiveDateActiveLookupableHelper {
037    
038        @Override
039        public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
040            List<HtmlData> customActionUrls = new ArrayList<HtmlData>();
041                    
042                    List<HtmlData> defaultCustomActionUrls = super.getCustomActionUrls(businessObject, pkNames);
043            
044                    TkRoleGroup tkRoleGroup = (TkRoleGroup) businessObject;
045            String principalId = tkRoleGroup.getPrincipalId();
046            String location = null;
047            if (StringUtils.isNotBlank(tkRoleGroup.getDepartment())) {
048                location = TkServiceLocator.getDepartmentService().getDepartment(tkRoleGroup.getDepartment(), TKUtils.getCurrentDate()).getLocation();
049            }
050            String department = tkRoleGroup.getDepartment();
051            
052            boolean systemAdmin = TKContext.getUser().isSystemAdmin();
053                    boolean locationAdmin = TKContext.getUser().getLocationAdminAreas().contains(location);
054                    boolean departmentAdmin = TKContext.getUser().getDepartmentAdminAreas().contains(department);
055                    
056                    for (HtmlData defaultCustomActionUrl : defaultCustomActionUrls){
057                            if (StringUtils.equals(defaultCustomActionUrl.getMethodToCall(), "edit")) {
058                                    if (systemAdmin || locationAdmin || departmentAdmin) {
059                                            customActionUrls.add(defaultCustomActionUrl);
060                                    }
061                            } else {
062                                    customActionUrls.add(defaultCustomActionUrl);
063                            }
064                    }
065                    
066                    Properties params = new Properties();
067                    params.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, getBusinessObjectClass().getName());
068                    params.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.MAINTENANCE_NEW_METHOD_TO_CALL);
069                    params.put("principalId", principalId);
070                    AnchorHtmlData viewUrl = new AnchorHtmlData(UrlFactory.parameterizeUrl(KRADConstants.INQUIRY_ACTION, params), "view");
071                    viewUrl.setDisplayText("view");
072                    viewUrl.setTarget(AnchorHtmlData.TARGET_BLANK);
073                    customActionUrls.add(viewUrl);
074                    
075                    return customActionUrls;
076        }
077    
078        @Override
079        public List<? extends BusinessObject> getSearchResults(
080                Map<String, String> fieldValues) {
081    
082            String principalId = fieldValues.get("principalId");
083            String principalName = fieldValues.get("principalName");
084            String workArea = fieldValues.get("workArea");
085            String department = fieldValues.get("department");
086            String roleName = fieldValues.get("roleName");
087    
088            List<TkRoleGroup> roleGroupList = TkServiceLocator.getTkRoleGroupService().getRoleGroups(principalId, principalName, workArea, department, roleName);
089    
090            //BELOW is done to allow grouping of roles by principal id
091            //TODO find a better way todo this
092            for(TkRoleGroup roleGroup : roleGroupList){
093                    TkRoleGroup tkRoleGroup = TkServiceLocator.getTkRoleGroupService().getRoleGroup(roleGroup.getPrincipalId());
094                    if(tkRoleGroup == null){
095                            tkRoleGroup = new TkRoleGroup();
096                                    tkRoleGroup.setPrincipalId(roleGroup.getPrincipalId());
097                                    TkServiceLocator.getTkRoleGroupService().saveOrUpdate(roleGroup);
098                    }
099            }
100            
101            /* We have the full set of roles, but since we are displaying only Principal IDs in the result list, we should return a distinct list */
102            Map<String,TkRoleGroup> mapDistinctPrincipalIdsToRoleGroups = new HashMap<String, TkRoleGroup>();
103            for(TkRoleGroup roleGroup : roleGroupList){
104                mapDistinctPrincipalIdsToRoleGroups.put(roleGroup.getPrincipalId(), roleGroup);
105            }
106            List<TkRoleGroup> distinctRoleGroupPrincipalIds = new ArrayList<TkRoleGroup>();
107            for(String distinctPrincipalId : mapDistinctPrincipalIdsToRoleGroups.keySet()){
108                    distinctRoleGroupPrincipalIds.add(mapDistinctPrincipalIdsToRoleGroups.get(distinctPrincipalId));
109            }
110            
111    //              if(principalId!=""){
112    //                      Person person = KIMServiceLocator.getPersonService().getPerson(principalId);
113    //                      if (isAuthorizedToEditUserRole(person)
114    //                                      && (StringUtils.isEmpty(principalName) || StringUtils.equals(
115    //                                                      person.getPrincipalName(), principalName))) {
116    //                              TkRoleGroup tkRoleGroup = getRoleGroupFromPerson(person);
117    //                              if(tkRoleGroup != null ){
118    //                                      roleGroupList.add(getRoleGroupFromPerson(person));
119    //                              }
120    //                      }
121    //              } else if(principalName!=""){
122    //                      Person person = KIMServiceLocator.getPersonService().getPersonByPrincipalName(principalName);
123    //                      if(isAuthorizedToEditUserRole(person)){
124    //                              TkRoleGroup tkRoleGroup = getRoleGroupFromPerson(person);
125    //                              if(tkRoleGroup != null ){
126    //                                      roleGroupList.add(getRoleGroupFromPerson(person));
127    //                              }
128    //                      }
129    //              }else{
130    //                      List<Person> personList = KIMServiceLocator.getPersonService().findPeople(null);
131    //                      for(Person person : personList){
132    //                              if(isAuthorizedToEditUserRole(person)){
133    //                                      roleGroupList.add(getRoleGroupFromPerson(person));
134    //                              }
135    //                      }
136    //              }
137    //              Iterator<BusinessObject> itr = roleGroupList.iterator();
138    //              while(itr.hasNext()){
139    //                      TkRoleGroup roleGroup = (TkRoleGroup)itr.next();
140    //                      TkRoleGroup tkRoleGroup = TkServiceLocator.getTkRoleGroupService().getRoleGroup(roleGroup.getPrincipalId());
141    //                      if(tkRoleGroup == null){
142    //                              tkRoleGroup = new TkRoleGroup();
143    //                              tkRoleGroup.setPrincipalId(roleGroup.getPrincipalId());
144    //                              TkServiceLocator.getTkRoleGroupService().saveOrUpdate(roleGroup);
145    //                      }
146    //                      String workArea = fieldValues.get("workArea");
147    //                      String department = fieldValues.get("department");
148    //                      String roleName = fieldValues.get("roleName");
149    //                      boolean isAllowed = false;
150    //                      if (tkRoleGroup.getRoles()!= null && tkRoleGroup.getRoles().size() > 0) {
151    //                              for(TkRole tkRole : tkRoleGroup.getRoles()){
152    //                                      if ((StringUtils.isEmpty(workArea) || (tkRole.getWorkArea() != null && StringUtils
153    //                                                      .equals(workArea, tkRole.getWorkArea().toString())))
154    //                                                      && (StringUtils.isEmpty(department) || StringUtils
155    //                                                                      .equals(department, tkRole.getDepartment()))
156    //                                                      && (StringUtils.isEmpty(roleName) || StringUtils
157    //                                                                      .equals(roleName, tkRole.getRoleName()))) {
158    //                                              isAllowed = true;
159    //                                              break;
160    //                                      }
161    //                              }
162    //                      } else if (StringUtils.isEmpty(workArea)
163    //                                      && StringUtils.isEmpty(department)
164    //                                      && StringUtils.isEmpty(roleName)) {
165    //                              isAllowed = true;
166    //                      }
167    //                      if(!isAllowed){
168    //                              itr.remove();
169    //                      }
170    //              }
171            return distinctRoleGroupPrincipalIds;
172        }
173    }