View Javadoc
1   /*
2    * Copyright 2008 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.ole.sys.service.impl;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.kuali.ole.coa.businessobject.Chart;
26  import org.kuali.ole.coa.businessobject.Organization;
27  import org.kuali.ole.coa.identity.FinancialSystemUserRoleTypeServiceImpl;
28  import org.kuali.ole.coa.service.ChartService;
29  import org.kuali.ole.coa.service.OrganizationService;
30  import org.kuali.ole.sys.OLEConstants;
31  import org.kuali.ole.sys.businessobject.ChartOrgHolder;
32  import org.kuali.ole.sys.identity.OleKimAttributes;
33  import org.kuali.ole.sys.service.FinancialSystemUserService;
34  import org.kuali.rice.kim.api.KimConstants;
35  import org.kuali.rice.kim.api.identity.Person;
36  import org.kuali.rice.kim.api.identity.PersonService;
37  import org.kuali.rice.kim.api.role.RoleService;
38  
39  public class FinancialSystemUserServiceImpl implements FinancialSystemUserService {
40  
41      protected ChartService chartService;
42      protected OrganizationService organizationService;
43      protected RoleService roleService;
44      protected PersonService personService;
45      protected String userRoleId;
46      private List<String> userRoleIdList = new ArrayList<String>(1);
47  
48      protected String getUserRoleId() {
49          if (userRoleId == null) {
50              userRoleId = roleService.getRoleIdByNamespaceCodeAndName(OLEConstants.CoreModuleNamespaces.OLE, OLEConstants.SysKimApiConstants.KFS_USER_ROLE_NAME);
51          }
52          return userRoleId;
53      }
54  
55      protected List<String> getUserRoleIdAsList() {
56          if (userRoleIdList.isEmpty()) {
57              userRoleIdList.add(getUserRoleId());
58          }
59          return userRoleIdList;
60      }
61  
62      /**
63       * @see org.kuali.ole.sys.service.FinancialSystemUserService#isActiveFinancialSystemUser(org.kuali.rice.kim.api.identity.Person)
64       */
65      @Override
66      public boolean isActiveFinancialSystemUser(Person p) {
67         if ( p == null ) {
68             return false;
69         }
70         return isActiveFinancialSystemUser(p.getPrincipalId());
71      }
72  
73      /**
74       * @see org.kuali.ole.sys.service.FinancialSystemUserService#isActiveFinancialSystemUser(java.lang.String)
75       */
76      @Override
77      public boolean isActiveFinancialSystemUser(String principalId) {
78          if ( StringUtils.isBlank(principalId)) {
79              return false;
80          }
81          return roleService.principalHasRole(principalId, getUserRoleIdAsList(), new HashMap<String, String>());
82      }
83  
84      /**
85       * @see org.kuali.ole.sys.service.FinancialSystemUserService#getPrimaryOrganization(org.kuali.rice.kim.api.identity.Person,
86       *      java.lang.String)
87       */
88      @Override
89      public ChartOrgHolder getPrimaryOrganization(Person person, String namespaceCode) {
90          if ( person == null ) {
91              return null;
92          }
93          ChartOrgHolder chartOrgHolder = getOrganizationForFinancialSystemUser(person.getPrincipalId(), namespaceCode);
94          if (chartOrgHolder == null) {
95              chartOrgHolder = getOrganizationForNonFinancialSystemUser(person);
96          }
97          return (chartOrgHolder == null) ? new ChartOrgHolderImpl() : chartOrgHolder;
98      }
99  
100     /**
101      * @see org.kuali.ole.sys.service.FinancialSystemUserService#getOrganizationByNamespaceCode(org.kuali.rice.kim.api.identity.Person,
102      *      java.lang.String)
103      */
104     @Override
105     public ChartOrgHolder getPrimaryOrganization(String principalId, String namespaceCode) {
106         if ( StringUtils.isBlank(principalId) ) {
107             return new ChartOrgHolderImpl();
108         }
109         ChartOrgHolder chartOrgHolder = getOrganizationForFinancialSystemUser(principalId, namespaceCode);
110         if (chartOrgHolder == null) {
111             chartOrgHolder = getOrganizationForNonFinancialSystemUser(personService.getPerson(principalId));
112         }
113         return (chartOrgHolder == null) ? new ChartOrgHolderImpl() : chartOrgHolder;
114     }
115 
116     protected ChartOrgHolder getOrganizationForFinancialSystemUser(String principalId, String namespaceCode) {
117         if ( StringUtils.isBlank( principalId ) ) {
118             return null;
119         }
120         Map<String,String> qualification = new HashMap<String,String>(2);
121         qualification.put(FinancialSystemUserRoleTypeServiceImpl.PERFORM_QUALIFIER_MATCH, "true");
122         qualification.put(KimConstants.AttributeConstants.NAMESPACE_CODE, namespaceCode);
123         List<Map<String,String>> roleQualifiers = roleService.getRoleQualifersForPrincipalByNamespaceAndRolename(principalId, OLEConstants.CoreModuleNamespaces.OLE, OLEConstants.SysKimApiConstants.KFS_USER_ROLE_NAME, qualification);
124         if ((roleQualifiers != null) && !roleQualifiers.isEmpty()) {
125             return new ChartOrgHolderImpl(roleQualifiers.get(0).get(OleKimAttributes.CHART_OF_ACCOUNTS_CODE), roleQualifiers.get(0).get(OleKimAttributes.ORGANIZATION_CODE));
126         }
127         return null;
128     }
129 
130     @Deprecated
131     protected ChartOrgHolder getOrganizationForNonFinancialSystemUser(Person person) {
132         // HACK ALERT!!!!! - This is to support the original universal user table setup where the home department
133         // was encoded as CAMPUS-ORG (Where campus happened to match the chart) in the original FS_UNIVERSAL_USR_T table.
134         // This **REALLY** needs a new implementation
135         if (person.getPrimaryDepartmentCode().contains("-")) {
136             return new ChartOrgHolderImpl(StringUtils.substringBefore(person.getPrimaryDepartmentCode(), "-"), StringUtils.substringAfter(person.getPrimaryDepartmentCode(), "-"));
137         }
138         return null;
139     }
140 
141     @Override
142     public Collection<String> getPrincipalIdsForFinancialSystemOrganizationUsers( String namespaceCode, ChartOrgHolder chartOrg) {
143         Map<String,String> qualification = new HashMap<String,String>(2);
144         qualification.put(FinancialSystemUserRoleTypeServiceImpl.PERFORM_QUALIFIER_MATCH, "true");
145         qualification.put(KimConstants.AttributeConstants.NAMESPACE_CODE, namespaceCode);
146         qualification.put(OleKimAttributes.CHART_OF_ACCOUNTS_CODE, chartOrg.getChartOfAccountsCode());
147         qualification.put(OleKimAttributes.ORGANIZATION_CODE, chartOrg.getOrganizationCode());
148         return roleService.getRoleMemberPrincipalIds(OLEConstants.CoreModuleNamespaces.OLE, OLEConstants.SysKimApiConstants.KFS_USER_ROLE_NAME, qualification);
149     }
150 
151     @Override
152     public Collection<String> getPrincipalIdsForFinancialSystemOrganizationUsers(String namespaceCode, List<ChartOrgHolder> chartOrgs) {
153         List<String> principalIds = new ArrayList<String>();
154         for ( ChartOrgHolder chartOrg : chartOrgs ) {
155             principalIds.addAll( getPrincipalIdsForFinancialSystemOrganizationUsers(namespaceCode, chartOrg));
156         }
157         return principalIds;
158     }
159 
160     public class ChartOrgHolderImpl implements ChartOrgHolder {
161         protected String chartOfAccountsCode;
162         protected String organizationCode;
163 
164 
165         public ChartOrgHolderImpl() {}
166 
167         public ChartOrgHolderImpl(String chartOfAccountsCode, String organizationCode) {
168             this.chartOfAccountsCode = chartOfAccountsCode;
169             this.organizationCode = organizationCode;
170         }
171 
172         @Override
173         public Chart getChartOfAccounts() {
174             return chartService.getByPrimaryId(chartOfAccountsCode);
175         }
176 
177         @Override
178         public Organization getOrganization() {
179             return organizationService.getByPrimaryIdWithCaching(chartOfAccountsCode, organizationCode);
180         }
181 
182         @Override
183         public String getChartOfAccountsCode() {
184             return chartOfAccountsCode;
185         }
186 
187         public void setChartOfAccountsCode(String chartOfAccountsCode) {
188             this.chartOfAccountsCode = chartOfAccountsCode;
189         }
190 
191         @Override
192         public String getOrganizationCode() {
193             return organizationCode;
194         }
195 
196         public void setOrganizationCode(String organizationCode) {
197             this.organizationCode = organizationCode;
198         }
199         @Override
200         public boolean equals(Object obj) {
201             if ( !(obj instanceof ChartOrgHolder) ) {
202                 return false;
203             }
204             return chartOfAccountsCode.equals(((ChartOrgHolder)obj).getChartOfAccountsCode())
205                     && organizationCode.equals(((ChartOrgHolder)obj).getOrganizationCode());
206         }
207 
208         @Override
209         public int hashCode() {
210             return chartOfAccountsCode.hashCode() + organizationCode.hashCode();
211         }
212     }
213 
214     public void setChartService(ChartService chartService) {
215         this.chartService = chartService;
216     }
217 
218     public void setOrganizationService(OrganizationService organizationService) {
219         this.organizationService = organizationService;
220     }
221 
222     public void setRoleService(RoleService roleService) {
223         this.roleService = roleService;
224     }
225 
226     public void setPersonService(PersonService personService) {
227         this.personService = personService;
228     }
229 }