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.financial.rules;
017:
018: import static org.kuali.core.util.AssertionUtils.assertThat;
019:
020: import org.apache.commons.lang.StringUtils;
021: import org.kuali.core.bo.user.UniversalUser;
022: import org.kuali.core.util.GlobalVariables;
023: import org.kuali.core.util.ObjectUtils;
024: import org.kuali.kfs.KFSKeyConstants;
025: import org.kuali.kfs.KFSPropertyConstants;
026: import org.kuali.kfs.bo.AccountingLine;
027: import org.kuali.kfs.context.SpringContext;
028: import org.kuali.kfs.rules.AccountingDocumentRuleBase;
029: import org.kuali.module.financial.bo.ServiceBillingControl;
030: import org.kuali.module.financial.service.ServiceBillingControlService;
031:
032: /**
033: * This class contains static helper methods for ServiceBillingDocumentRule and ServiceBillingDocumentAuthorizer.
034: */
035: public class ServiceBillingDocumentRuleUtil {
036:
037: /**
038: * Checks the account and current user against the service billing control table.
039: *
040: * @param accountingLine The accounting line from the income section of the service billing document.
041: * @param action The type of error messages to generate, if not null.
042: * @return Whether the current user is authorized to use the given account in the service billing income section.
043: */
044: public static boolean serviceBillingIncomeAccountIsAccessible(
045: AccountingLine accountingLine,
046: AccountingDocumentRuleBase.AccountingLineAction action) {
047: return serviceBillingIncomeAccountIsAccessible(accountingLine,
048: action, GlobalVariables.getUserSession()
049: .getUniversalUser());
050: }
051:
052: /**
053: * Checks the account and user against the service billing control table.
054: *
055: * @param accountingLine The accounting line from the income section of the service billing document.
056: * @param action The type of error messages to generate, if not null.
057: * @param user The user for whom to check accessibility.
058: * @return Whether the given user is authorized to use the given account in the service billing income section.
059: */
060: public static boolean serviceBillingIncomeAccountIsAccessible(
061: AccountingLine accountingLine,
062: AccountingDocumentRuleBase.AccountingLineAction action,
063: UniversalUser user) {
064: assertThat(accountingLine.isSourceAccountingLine(),
065: accountingLine);
066: String chartOfAccountsCode = accountingLine
067: .getChartOfAccountsCode();
068: String accountNumber = accountingLine.getAccountNumber();
069: if (StringUtils.isEmpty(chartOfAccountsCode)
070: || StringUtils.isEmpty(accountNumber)) {
071: // Ignore empty key because hasAccessibleAccountingLines() may not validate beforehand.
072: return false;
073: }
074: ServiceBillingControl control = SpringContext.getBean(
075: ServiceBillingControlService.class).getByPrimaryId(
076: chartOfAccountsCode, accountNumber);
077: if (ObjectUtils.isNull(control)) {
078: if (action != null) {
079: GlobalVariables.getErrorMap().putError(
080: KFSPropertyConstants.ACCOUNT_NUMBER,
081: noServiceBillingControlErrorKey(action),
082: accountingLine.getAccountNumber());
083: }
084: return false;
085: }
086:
087: if (user.isMember(control.getWorkgroupName())) {
088: return true;
089: } else {
090: if (action != null) {
091: GlobalVariables.getErrorMap().putError(
092: KFSPropertyConstants.ACCOUNT_NUMBER,
093: notControlGroupMemberErrorKey(action),
094: accountingLine.getAccountNumber(),
095: user.getPersonUserIdentifier(),
096: control.getWorkgroupName());
097: }
098: return false;
099: }
100: }
101:
102: /**
103: * This method determines what error key to use when posting the associated error. The error key is determined
104: * based on the constants passed in, which are values defined in the AccountingDocumentRuleBase.AccountingLineAction.
105: *
106: * @param action The constant used to identify which error key to return.
107: * @return The error key for not having a service billing control.
108: *
109: * @see AccountingDocumentRuleBase.AccountingLineAction
110: */
111: private static String noServiceBillingControlErrorKey(
112: AccountingDocumentRuleBase.AccountingLineAction action) {
113: switch (action) {
114: case ADD:
115: return KFSKeyConstants.ERROR_ACCOUNTINGLINE_INACCESSIBLE_ADD_NO_SB_CTRL;
116: case UPDATE:
117: return KFSKeyConstants.ERROR_ACCOUNTINGLINE_INACCESSIBLE_UPDATE_NO_SB_CTRL;
118: case DELETE:
119: return KFSKeyConstants.ERROR_ACCOUNTINGLINE_INACCESSIBLE_DELETE_NO_SB_CTRL;
120: default:
121: throw new AssertionError(action);
122: }
123: }
124:
125: /**
126: * This method determines what error key to use when posting the associated error. The error key is determined
127: * based on the constants passed in, which are values defined in the AccountingDocumentRuleBase.AccountingLineAction.
128: *
129: * @param action The constant used to identify which error key to return.
130: * @return The error key for not being a member of the workgroup of the necessary service billing control.
131: *
132: * @see AccountingDocumentRuleBase.AccountingLineAction
133: */
134: private static String notControlGroupMemberErrorKey(
135: AccountingDocumentRuleBase.AccountingLineAction action) {
136: switch (action) {
137: case ADD:
138: return KFSKeyConstants.ERROR_ACCOUNTINGLINE_INACCESSIBLE_ADD_NOT_IN_SB_CTRL_GRP;
139: case UPDATE:
140: return KFSKeyConstants.ERROR_ACCOUNTINGLINE_INACCESSIBLE_UPDATE_NOT_IN_SB_CTRL_GRP;
141: case DELETE:
142: return KFSKeyConstants.ERROR_ACCOUNTINGLINE_INACCESSIBLE_DELETE_NOT_IN_SB_CTRL_GRP;
143: default:
144: throw new AssertionError(action);
145: }
146: }
147: }
|