001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.module.chart.bo;
017:
018: import java.util.HashMap;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Map;
022:
023: import org.apache.commons.lang.StringUtils;
024: import org.kuali.core.bo.user.KualiModuleUserBase;
025: import org.kuali.core.bo.user.UniversalUser;
026: import org.kuali.core.service.KualiModuleService;
027: import org.kuali.kfs.KFSPropertyConstants;
028: import org.kuali.kfs.bo.AccountResponsibility;
029: import org.kuali.kfs.context.SpringContext;
030: import org.kuali.module.chart.service.AccountService;
031: import org.kuali.module.chart.service.ChartService;
032: import org.kuali.module.chart.service.ChartUserService;
033: import org.kuali.module.chart.service.OrganizationService;
034:
035: public class ChartUser extends KualiModuleUserBase {
036:
037: public static final String MODULE_ID = "chart";
038:
039: private transient Chart chartOfAccounts;
040: private transient Org organization;
041: private transient Chart userChartOfAccounts;
042: private transient Org userOrganization;
043:
044: private static ChartUserService chartUserService;
045: private static ChartService chartService;
046: private static AccountService accountService;
047: private static OrganizationService organizationService;
048:
049: public ChartUser() {
050: this (null);
051: }
052:
053: public ChartUser(UniversalUser user) {
054: super (MODULE_ID, user);
055: if (chartUserService == null) {
056: chartService = SpringContext.getBean(ChartService.class);
057: accountService = SpringContext
058: .getBean(AccountService.class);
059: organizationService = SpringContext
060: .getBean(OrganizationService.class);
061: chartUserService = (ChartUserService) SpringContext
062: .getBean(KualiModuleService.class).getModule(
063: MODULE_ID).getModuleUserService();
064: }
065: }
066:
067: private Map accountResponsibilities = null;
068: // account map keyed by chartCode + "::" + accountNumber
069: private Map chartResponsibilities = null;
070:
071: public Map getAccountResponsibilities() {
072: if (accountResponsibilities == null
073: && getUniversalUser() != null
074: && getUniversalUser().getPersonUniversalIdentifier() != null) {
075: setAccountResponsibilities(accountService
076: .getAccountsThatUserIsResponsibleFor(getUniversalUser()));
077: }
078: return accountResponsibilities;
079: }
080:
081: public void setAccountResponsibilities(Map accountResponsibilities) {
082: this .accountResponsibilities = accountResponsibilities;
083: }
084:
085: public Chart getChartOfAccounts() {
086: if (chartOfAccounts == null
087: || !chartOfAccounts.getChartOfAccountsCode().equals(
088: getChartOfAccountsCode())) {
089: refresh();
090: }
091: return chartOfAccounts;
092: }
093:
094: public void setChartOfAccounts(Chart chartOfAccounts) {
095: this .chartOfAccounts = chartOfAccounts;
096: }
097:
098: public String getChartOfAccountsCode() {
099: if (getUserProperty(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE)
100: .equals("")) {
101: return chartUserService
102: .getDefaultChartCode(getUniversalUser());
103: }
104: return getUserProperty(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
105: }
106:
107: public Map getChartResponsibilities() {
108: if (chartResponsibilities == null && getUniversalUser() != null) {
109: setChartResponsibilities(chartService
110: .getChartsThatUserIsResponsibleFor(getUniversalUser()));
111: }
112: return chartResponsibilities;
113: }
114:
115: public void setChartResponsibilities(Map chartResponsibilities) {
116: this .chartResponsibilities = chartResponsibilities;
117: }
118:
119: public Org getOrganization() {
120: if (organization == null
121: || !organization.getChartOfAccountsCode().equals(
122: getChartOfAccountsCode())
123: || !organization.getOrganizationCode().equals(
124: getOrganizationCode())) {
125: refresh();
126: }
127: return organization;
128: }
129:
130: public void setOrganization(Org organization) {
131: this .organization = organization;
132: }
133:
134: public String getOrganizationCode() {
135: if (getUserProperty(KFSPropertyConstants.ORGANIZATION_CODE)
136: .equals("")) {
137: return chartUserService
138: .getDefaultOrganizationCode(getUniversalUser());
139: }
140: return getUserProperty(KFSPropertyConstants.ORGANIZATION_CODE);
141: }
142:
143: public String getUserChartOfAccountsCode() {
144: return getUserProperty(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
145: }
146:
147: public void setUserChartOfAccountsCode(String chartOfAccountsCode) {
148: setUserProperty(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE,
149: chartOfAccountsCode);
150: }
151:
152: public String getUserOrganizationCode() {
153: return getUserProperty(KFSPropertyConstants.ORGANIZATION_CODE);
154: }
155:
156: public void setUserOrganizationCode(String organizationCode) {
157: setUserProperty(KFSPropertyConstants.ORGANIZATION_CODE,
158: organizationCode);
159: }
160:
161: public Chart getUserChartOfAccounts() {
162: if (userChartOfAccounts == null
163: || !userChartOfAccounts.getChartOfAccountsCode()
164: .equals(getUserChartOfAccountsCode())) {
165: refresh();
166: }
167: return userChartOfAccounts;
168: }
169:
170: public void setUserChartOfAccounts(Chart chartOfAccounts) {
171: this .userChartOfAccounts = chartOfAccounts;
172: }
173:
174: public Org getUserOrganization() {
175: if (userOrganization == null
176: || !userOrganization.getChartOfAccountsCode().equals(
177: getUserChartOfAccountsCode())
178: || !userOrganization.getOrganizationCode().equals(
179: getUserOrganizationCode())) {
180: refresh();
181: }
182: return userOrganization;
183: }
184:
185: public void setUserOrganization(Org organization) {
186: this .userOrganization = organization;
187: }
188:
189: /**
190: * check if the user is an administrator
191: *
192: * @return
193: */
194: public boolean isAdministratorUser() {
195: return chartUserService.isAdministratorUser(this );
196: }
197:
198: /**
199: * boolean to indicate if the user is responsible for a given account
200: *
201: * @param account
202: * @return true if the user is responsible for the account passed in
203: */
204: public boolean isResponsibleForAccount(String chartCode,
205: String accountNumber) {
206: boolean isResponsible = false;
207:
208: if (!StringUtils.isEmpty(chartCode)
209: && !StringUtils.isEmpty(accountNumber)) {
210: String accountKey = buildAccountKey(chartCode,
211: accountNumber);
212:
213: isResponsible = getAccountResponsibilities().containsKey(
214: accountKey);
215: }
216:
217: return isResponsible;
218: }
219:
220: /**
221: * boolean to indicate if the user is responsible for a given chart
222: *
223: * @param chartCode
224: * @return true if the user is responsible for the chart passed in
225: */
226: public boolean isManagerForChart(String chartCode) {
227: boolean isResponsible = false;
228:
229: if (!StringUtils.isEmpty(chartCode)) {
230:
231: isResponsible = getChartResponsibilities().containsKey(
232: chartCode);
233: }
234:
235: return isResponsible;
236: }
237:
238: /**
239: * Convenience setter for converting AccountResponsibility list into AccountResponsibility map
240: *
241: * @param accountList The accountResponsibilities to set.
242: */
243: public void setChartResponsibilities(List accountList) {
244: // build the map
245: Map chartMap = new HashMap();
246: for (Iterator i = accountList.iterator(); i.hasNext();) {
247: Chart chart = (Chart) i.next();
248: String chartCode = chart.getChartOfAccountsCode();
249:
250: chartMap.put(chartCode, chart);
251: }
252:
253: setChartResponsibilities(chartMap);
254: }
255:
256: /**
257: * Convenience setter for converting AccountResponsibility list into AccountResponsibility map
258: *
259: * @param accountList The accountResponsibilities to set.
260: */
261: public void setAccountResponsibilities(List accountList) {
262: // build the map
263: Map accountMap = new HashMap();
264: for (Iterator i = accountList.iterator(); i.hasNext();) {
265: AccountResponsibility accountResponsibility = (AccountResponsibility) i
266: .next();
267: Account account = accountResponsibility.getAccount();
268:
269: accountMap.put(buildAccountKey(account
270: .getChartOfAccountsCode(), account
271: .getAccountNumber()), accountResponsibility);
272: }
273:
274: setAccountResponsibilities(accountMap);
275: }
276:
277: /**
278: * @param chartCode
279: * @param accountNumber
280: * @return key used to store the given Account in the Account Map
281: */
282: private String buildAccountKey(String chartCode,
283: String accountNumber) {
284: if (StringUtils.isEmpty(chartCode)) {
285: throw new IllegalArgumentException(
286: "invalid (blank) chartCode");
287: }
288: if (StringUtils.isEmpty(accountNumber)) {
289: throw new IllegalArgumentException(
290: "invalid (blank) accountNumber");
291: }
292:
293: String accountKey = chartCode + "::" + accountNumber;
294: return accountKey;
295: }
296:
297: /**
298: * Get the linked objects manually since this object is not directly persistable. Mimics the refresh method on OJB persistable
299: * objects. TODO: replace me when BO MetaData can handle this
300: */
301: public void refresh() {
302: if (StringUtils.isNotEmpty(getChartOfAccountsCode())) {
303: if (chartOfAccounts == null
304: || !chartOfAccounts.getChartOfAccountsCode()
305: .equals(getChartOfAccountsCode())) {
306: chartOfAccounts = chartService
307: .getByPrimaryId(getChartOfAccountsCode()
308: .toUpperCase());
309: }
310: } else {
311: chartOfAccounts = null;
312: }
313: if (StringUtils.isNotEmpty(getOrganizationCode())) {
314: if (organization == null
315: || !organization.getChartOfAccountsCode().equals(
316: getChartOfAccountsCode())
317: || !organization.getOrganizationCode().equals(
318: getOrganizationCode())) {
319: organization = organizationService.getByPrimaryId(
320: getChartOfAccountsCode().toUpperCase(),
321: getOrganizationCode().toUpperCase());
322: }
323: } else {
324: organization = null;
325: }
326: if (StringUtils.isNotEmpty(getUserChartOfAccountsCode())) {
327: if (userChartOfAccounts == null
328: || !userChartOfAccounts.getChartOfAccountsCode()
329: .equals(getUserChartOfAccountsCode())) {
330: userChartOfAccounts = chartService
331: .getByPrimaryId(getUserChartOfAccountsCode()
332: .toUpperCase());
333: }
334: } else {
335: userChartOfAccounts = null;
336: }
337: if (StringUtils.isNotEmpty(getUserOrganizationCode())) {
338: if (userOrganization == null
339: || !userOrganization.getChartOfAccountsCode()
340: .equals(getUserChartOfAccountsCode())
341: || !userOrganization.getOrganizationCode().equals(
342: getUserOrganizationCode())) {
343: userOrganization = organizationService.getByPrimaryId(
344: getUserChartOfAccountsCode().toUpperCase(),
345: getUserOrganizationCode().toUpperCase());
346: }
347: } else {
348: userOrganization = null;
349: }
350: }
351:
352: }
|