001 /*
002 * Copyright 2011 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.kfs.sys.service.impl;
017
018 import java.util.ArrayList;
019 import java.util.Collection;
020 import java.util.HashMap;
021 import java.util.List;
022 import java.util.Map;
023
024 import org.apache.commons.lang.StringUtils;
025 import org.kuali.kfs.coa.businessobject.Chart;
026 import org.kuali.kfs.coa.businessobject.Organization;
027 import org.kuali.kfs.coa.identity.FinancialSystemUserRoleTypeServiceImpl;
028 import org.kuali.kfs.sys.KFSConstants;
029 import org.kuali.kfs.sys.KFSPropertyConstants;
030 import org.kuali.kfs.sys.businessobject.ChartOrgHolder;
031 import org.kuali.kfs.sys.context.SpringContext;
032 import org.kuali.kfs.sys.identity.KfsKimAttributes;
033 import org.kuali.kfs.sys.service.FinancialSystemUserService;
034 import org.kuali.rice.kim.bo.Person;
035 import org.kuali.rice.kim.bo.types.dto.AttributeSet;
036 import org.kuali.rice.kim.service.IdentityManagementService;
037 import org.kuali.rice.kim.service.PersonService;
038 import org.kuali.rice.kim.service.RoleManagementService;
039 import org.kuali.rice.kns.service.BusinessObjectService;
040
041 public class FinancialSystemUserServiceImpl implements FinancialSystemUserService {
042
043 private BusinessObjectService businessObjectService;
044 private IdentityManagementService identityManagementService;
045 private RoleManagementService roleManagementService;
046 private PersonService personService;
047 private String userRoleId;
048 private List<String> userRoleIdList = new ArrayList<String>(1);
049
050 protected BusinessObjectService getBusinessObjectService() {
051 if (businessObjectService == null) {
052 businessObjectService = SpringContext.getBean(BusinessObjectService.class);
053 }
054 return businessObjectService;
055 }
056
057 protected IdentityManagementService getIdentityManagementService() {
058 if (identityManagementService == null) {
059 identityManagementService = SpringContext.getBean(IdentityManagementService.class);
060 }
061 return identityManagementService;
062 }
063
064 protected RoleManagementService getRoleManagementService() {
065 if (roleManagementService == null) {
066 roleManagementService = SpringContext.getBean(RoleManagementService.class);
067 }
068 return roleManagementService;
069 }
070
071 protected PersonService getPersonService() {
072 if (personService == null) {
073 personService = SpringContext.getBean(PersonService.class);
074 }
075 return personService;
076 }
077
078 protected String getUserRoleId() {
079 if (userRoleId == null) {
080 userRoleId = getRoleManagementService().getRoleIdByName(KFSConstants.ParameterNamespaces.KFS, FinancialSystemUserRoleTypeServiceImpl.FINANCIAL_SYSTEM_USER_ROLE_NAME);
081 }
082 return userRoleId;
083 }
084
085 protected List<String> getUserRoleIdAsList() {
086 if (userRoleIdList.isEmpty()) {
087 userRoleIdList.add(getUserRoleId());
088 }
089 return userRoleIdList;
090 }
091
092 /**
093 * @see org.kuali.kfs.sys.service.FinancialSystemUserService#isActiveFinancialSystemUser(org.kuali.rice.kim.bo.Person)
094 */
095 public boolean isActiveFinancialSystemUser(Person p) {
096 return getRoleManagementService().principalHasRole(p.getPrincipalId(), getUserRoleIdAsList(), null);
097 }
098
099 /**
100 * @see org.kuali.kfs.sys.service.FinancialSystemUserService#isActiveFinancialSystemUser(java.lang.String)
101 */
102 public boolean isActiveFinancialSystemUser(String principalId) {
103 return getRoleManagementService().principalHasRole(principalId, getUserRoleIdAsList(), null);
104 }
105
106 /**
107 * @see org.kuali.kfs.sys.service.FinancialSystemUserService#getPrimaryOrganization(org.kuali.rice.kim.bo.Person,
108 * java.lang.String)
109 */
110 public ChartOrgHolder getPrimaryOrganization(Person person, String namespaceCode) {
111 if (person == null) {
112 return null;
113 }
114 ChartOrgHolder chartOrgHolder = getOrganizationForFinancialSystemUser(person.getPrincipalId(), namespaceCode);
115 if (chartOrgHolder == null) {
116 chartOrgHolder = getOrganizationForNonFinancialSystemUser(person);
117 }
118 return (chartOrgHolder == null) ? new ChartOrgHolderImpl() : chartOrgHolder;
119 }
120
121 /**
122 * @see org.kuali.kfs.sys.service.FinancialSystemUserService#getOrganizationByNamespaceCode(org.kuali.rice.kim.bo.Person,
123 * java.lang.String)
124 */
125 public ChartOrgHolder getPrimaryOrganization(String principalId, String namespaceCode) {
126 ChartOrgHolder chartOrgHolder = getOrganizationForFinancialSystemUser(principalId, namespaceCode);
127 if (chartOrgHolder == null) {
128 chartOrgHolder = getOrganizationForNonFinancialSystemUser(getPersonService().getPerson(principalId));
129 }
130 return (chartOrgHolder == null) ? new ChartOrgHolderImpl() : chartOrgHolder;
131 }
132
133 protected ChartOrgHolder getOrganizationForFinancialSystemUser(String principalId, String namespaceCode) {
134 if (principalId == null) {
135 return null;
136 }
137 AttributeSet qualification = new AttributeSet(2);
138 qualification.put(FinancialSystemUserRoleTypeServiceImpl.PERFORM_QUALIFIER_MATCH, "true");
139 qualification.put(KfsKimAttributes.NAMESPACE_CODE, namespaceCode);
140 List<AttributeSet> roleQualifiers = getRoleManagementService().getRoleQualifiersForPrincipal(principalId, KFSConstants.ParameterNamespaces.KFS, FinancialSystemUserRoleTypeServiceImpl.FINANCIAL_SYSTEM_USER_ROLE_NAME, qualification);
141 if ((roleQualifiers != null) && !roleQualifiers.isEmpty()) {
142 return new ChartOrgHolderImpl(roleQualifiers.get(0).get(KfsKimAttributes.CHART_OF_ACCOUNTS_CODE), roleQualifiers.get(0).get(KfsKimAttributes.ORGANIZATION_CODE));
143 }
144 return null;
145 }
146
147 protected ChartOrgHolder getOrganizationForNonFinancialSystemUser(Person person) {
148 if (person.getPrimaryDepartmentCode().contains("-")) {
149 return new ChartOrgHolderImpl(StringUtils.substringBefore(person.getPrimaryDepartmentCode(), "-"), StringUtils.substringAfter(person.getPrimaryDepartmentCode(), "-"));
150 }
151 return null;
152 }
153
154 public Collection<String> getPrincipalIdsForFinancialSystemOrganizationUsers( String namespaceCode, ChartOrgHolder chartOrg) {
155 AttributeSet qualification = new AttributeSet(4);
156 qualification.put(FinancialSystemUserRoleTypeServiceImpl.PERFORM_QUALIFIER_MATCH, "true");
157 qualification.put(KfsKimAttributes.NAMESPACE_CODE, namespaceCode);
158 qualification.put(KfsKimAttributes.CHART_OF_ACCOUNTS_CODE, chartOrg.getChartOfAccountsCode());
159 qualification.put(KfsKimAttributes.ORGANIZATION_CODE, chartOrg.getOrganizationCode());
160 return getRoleManagementService().getRoleMemberPrincipalIds(KFSConstants.ParameterNamespaces.KFS, FinancialSystemUserRoleTypeServiceImpl.FINANCIAL_SYSTEM_USER_ROLE_NAME, qualification);
161 }
162
163 public Collection<String> getPrincipalIdsForFinancialSystemOrganizationUsers(String namespaceCode, List<ChartOrgHolder> chartOrgs) {
164 List<String> principalIds = new ArrayList<String>();
165 for ( ChartOrgHolder chartOrg : chartOrgs ) {
166 principalIds.addAll( getPrincipalIdsForFinancialSystemOrganizationUsers(namespaceCode, chartOrg));
167 }
168 return principalIds;
169 }
170
171 public class ChartOrgHolderImpl implements ChartOrgHolder {
172 private String chartOfAccountsCode;
173 private String organizationCode;
174
175
176 public ChartOrgHolderImpl() {
177 }
178
179 public ChartOrgHolderImpl(String chartOfAccountsCode, String organizationCode) {
180 this.chartOfAccountsCode = chartOfAccountsCode;
181 this.organizationCode = organizationCode;
182 }
183
184 public Chart getChartOfAccounts() {
185 Map<String, String> pk = new HashMap<String, String>(2);
186 pk.put(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
187 return (Chart) getBusinessObjectService().findByPrimaryKey(Chart.class, pk);
188 }
189
190 public Organization getOrganization() {
191 Map<String, String> pk = new HashMap<String, String>(2);
192 pk.put(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
193 pk.put(KFSPropertyConstants.ORGANIZATION_CODE, organizationCode);
194 return (Organization) getBusinessObjectService().findByPrimaryKey(Organization.class, pk);
195 }
196
197 public String getChartOfAccountsCode() {
198 return chartOfAccountsCode;
199 }
200
201 public void setChartOfAccountsCode(String chartOfAccountsCode) {
202 this.chartOfAccountsCode = chartOfAccountsCode;
203 }
204
205 public String getOrganizationCode() {
206 return organizationCode;
207 }
208
209 public void setOrganizationCode(String organizationCode) {
210 this.organizationCode = organizationCode;
211 }
212 @Override
213 public boolean equals(Object obj) {
214 if ( !(obj instanceof ChartOrgHolder) ) {
215 return false;
216 }
217 return chartOfAccountsCode.equals(((ChartOrgHolder)obj).getChartOfAccountsCode())
218 && organizationCode.equals(((ChartOrgHolder)obj).getOrganizationCode());
219 }
220
221 @Override
222 public int hashCode() {
223 return chartOfAccountsCode.hashCode() + organizationCode.hashCode();
224 }
225 }
226 }