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