View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.tem.businessobject.lookup;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.commons.lang.StringUtils;
27  import org.kuali.kfs.module.tem.TemConstants;
28  import org.kuali.kfs.module.tem.businessobject.TemProfile;
29  import org.kuali.kfs.module.tem.businessobject.TravelerProfileForLookup;
30  import org.kuali.kfs.module.tem.identity.TemKimAttributes;
31  import org.kuali.kfs.sys.KFSConstants;
32  import org.kuali.kfs.sys.context.SpringContext;
33  import org.kuali.kfs.sys.identity.KfsKimAttributes;
34  import org.kuali.rice.kew.api.WorkflowDocument;
35  import org.kuali.rice.kim.api.KimConstants;
36  import org.kuali.rice.kim.api.permission.PermissionService;
37  import org.kuali.rice.kns.util.FieldUtils;
38  import org.kuali.rice.kns.util.KNSGlobalVariables;
39  import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
40  import org.kuali.rice.kns.web.struts.form.KualiForm;
41  import org.kuali.rice.kns.web.struts.form.LookupForm;
42  import org.kuali.rice.krad.bo.BusinessObject;
43  import org.kuali.rice.krad.service.SessionDocumentService;
44  import org.kuali.rice.krad.util.GlobalVariables;
45  
46  /**
47   * Overridden to filter results to only those available to the currently logged in user to use.  This will be used within TA, TR, ENT, and RELO
48   * document traveler lookups - NOT the non-document TemProfile lookup
49   */
50  public class TravelerProfileDocLookupableHelperServiceImpl extends TemProfileLookupableHelperServiceImpl {
51      protected PermissionService permissionService;
52  
53      /**
54       * Filters searched for profiles so they include only those the user should be able to access to use as a traveler on a travel, relocation, or entertainment document
55       * @see org.kuali.kfs.module.tem.businessobject.lookup.TemProfileLookupableHelperServiceImpl#getSearchResults(java.util.Map)
56       */
57      @Override
58      public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
59          final Map<String, String> viewRecordPermissionDetails = getPermissionDetailsForViewRecordsCheck();
60          final String currentUserPrincipalId = GlobalVariables.getUserSession().getPrincipalId();
61          final String documentTypeName = updateAuthorizationDocumentType(getCurrentDocumentTypeName());
62  
63          final List<TemProfile> allProfiles = (List<TemProfile>)super.getSearchResults(fieldValues);
64          List<TemProfile> qualifyingProfiles = new ArrayList<TemProfile>();
65          for (TemProfile profile : allProfiles) {
66              final Map<String, String> roleQualifier = getRoleQualifierForViewRecordsCheck(profile, documentTypeName);
67              if (getPermissionService().isAuthorizedByTemplate(currentUserPrincipalId, KFSConstants.PermissionTemplate.VIEW_RECORD.namespace, KFSConstants.PermissionTemplate.VIEW_RECORD.name, viewRecordPermissionDetails, roleQualifier)) {
68                  qualifyingProfiles.add(profile);
69              }
70          }
71          return qualifyingProfiles;
72      }
73  
74      /**
75       * @return the PermissionDetails map to do permission checks on TravelerProfileForLookup records
76       */
77      protected Map<String, String> getPermissionDetailsForViewRecordsCheck() {
78          Map<String, String> permissionDetails = new HashMap<String, String>();
79          permissionDetails.put(KimConstants.AttributeConstants.COMPONENT_NAME, TravelerProfileForLookup.class.getSimpleName());
80          return permissionDetails;
81      }
82  
83      /**
84       * Pulls the principal id, home chart, and home organization codes from the profile to build a role qualifier
85       * @param profile the profile to pull qualifications from
86       * @return a role qualifier with attribute values from the profile
87       */
88      protected Map<String, String> getRoleQualifierForViewRecordsCheck(TemProfile profile, String documentTypeName) {
89          Map<String, String> roleQualifier = new HashMap<String, String>();
90          roleQualifier.put(KfsKimAttributes.CHART_OF_ACCOUNTS_CODE, profile.getHomeDeptChartOfAccountsCode());
91          roleQualifier.put(KfsKimAttributes.ORGANIZATION_CODE, profile.getHomeDeptOrgCode());
92          roleQualifier.put(KfsKimAttributes.PROFILE_PRINCIPAL_ID, profile.getPrincipalId());
93          roleQualifier.put(TemKimAttributes.CUSTOMER_PROFILE_ID, profile.getCustomerNumber());
94          roleQualifier.put(TemKimAttributes.PROFILE_ID, profile.getProfileId().toString());
95  
96          if (!StringUtils.isBlank(documentTypeName)) {
97              roleQualifier.put(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME, documentTypeName);
98          }
99  
100         return roleQualifier;
101     }
102 
103     /**
104      * Looks in the form from KNSGlobalVariables to try to figure out what the document type of the current document is
105      * @return
106      */
107     protected String getCurrentDocumentTypeName() {
108         final KualiForm form = KNSGlobalVariables.getKualiForm();
109         if (form != null) {
110             if (form instanceof KualiDocumentFormBase) {
111                 return ((KualiDocumentFormBase)KNSGlobalVariables.getKualiForm()).getDocTypeName();
112             } else if (form instanceof LookupForm) {
113                 final String docNum = ((LookupForm)KNSGlobalVariables.getKualiForm()).getDocNum();
114                 if(!StringUtils.isBlank(docNum)) {
115                     WorkflowDocument workflowDocument = SpringContext.getBean(SessionDocumentService.class).getDocumentFromSession(GlobalVariables.getUserSession(), docNum);
116                     return workflowDocument.getDocumentTypeName();
117                 }
118             }
119         }
120         return null;
121     }
122 
123     /**
124      * If the passed in document type represents one of the travel authorization child documents, upgrade to travel authorization document; otherwise return the given document type unmodified
125      * @param documentType the document type name to filter
126      * @return TA if TAC or TAA is passed in as the document type, otherwise the passed in document type
127      */
128     protected String updateAuthorizationDocumentType(String documentType) {
129        if (!StringUtils.isBlank(documentType) && (TemConstants.TravelDocTypes.TRAVEL_AUTHORIZATION_CLOSE_DOCUMENT.equals(documentType) || TemConstants.TravelDocTypes.TRAVEL_AUTHORIZATION_AMEND_DOCUMENT.equals(documentType))) {
130            return TemConstants.TravelDocTypes.TRAVEL_AUTHORIZATION_DOCUMENT;
131        }
132        return documentType;
133     }
134 
135     /**
136      * Overridden to always return TemProfile
137      * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getBusinessObjectClass()
138      */
139     @Override
140     public Class getBusinessObjectClass() {
141         return TemProfile.class;
142     }
143 
144     /**
145      * This lookup only occurs within documents; it should never have a supplemental menu bar
146      * @see org.kuali.kfs.module.tem.businessobject.lookup.TemProfileLookupableHelperServiceImpl#getSupplementalMenuBar()
147      */
148     @Override
149     public String getSupplementalMenuBar() {
150         return KFSConstants.EMPTY_STRING;
151     }
152 
153     /**
154      * Overrides set rows so that the _rows_ always look at TravelerProfileForLookup as the business object class - we want that lookup,
155      * but we want to search for TemProfiles....
156      * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#setRows()
157      */
158     @Override
159     protected void setRows() {
160         List<String> lookupFieldAttributeList = null;
161         final Class<? extends BusinessObject> businessObjectClazz = TravelerProfileForLookup.class;
162         if (getBusinessObjectMetaDataService().isLookupable(businessObjectClazz)) {
163             lookupFieldAttributeList = getBusinessObjectMetaDataService().getLookupableFieldNames(
164                     businessObjectClazz);
165         }
166         if (lookupFieldAttributeList == null) {
167             throw new RuntimeException("Lookup not defined for business object " + getBusinessObjectClass());
168         }
169 
170         // construct field object for each search attribute
171         List fields = new ArrayList();
172         try {
173             fields = FieldUtils.createAndPopulateFieldsForLookup(lookupFieldAttributeList, getReadOnlyFieldsList(),
174                     businessObjectClazz);
175         } catch (InstantiationException e) {
176             throw new RuntimeException("Unable to create instance of business object class" + e.getMessage());
177         } catch (IllegalAccessException e) {
178             throw new RuntimeException("Unable to create instance of business object class" + e.getMessage());
179         }
180 
181         int numCols = getBusinessObjectDictionaryService().getLookupNumberOfColumns(businessObjectClazz);
182 
183         this.rows = FieldUtils.wrapFields(fields, numCols);
184     }
185 
186     /**
187      * @return the injected implementation KIM's PermissionService
188      */
189     public PermissionService getPermissionService() {
190         return permissionService;
191     }
192 
193     /**
194      * Injects an implementation of KIM's PermissionService
195      * @param permissionService an implementation of KIM's PermissionService
196      */
197     public void setPermissionService(PermissionService permissionService) {
198         this.permissionService = permissionService;
199     }
200 
201 }