001: /*
002: * Copyright 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.document.authorization;
017:
018: import java.util.HashMap;
019: import java.util.List;
020: import java.util.Map;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.kuali.core.bo.user.UniversalUser;
025: import org.kuali.core.document.Document;
026: import org.kuali.core.document.TransactionalDocument;
027: import org.kuali.core.document.authorization.DocumentActionFlags;
028: import org.kuali.core.document.authorization.TransactionalDocumentActionFlags;
029: import org.kuali.core.exceptions.DocumentTypeAuthorizationException;
030: import org.kuali.kfs.bo.AccountingLine;
031: import org.kuali.kfs.bo.SourceAccountingLine;
032: import org.kuali.kfs.context.SpringContext;
033: import org.kuali.kfs.document.authorization.AccountingDocumentAuthorizerBase;
034: import org.kuali.module.chart.bo.ChartUser;
035: import org.kuali.module.financial.bo.ServiceBillingControl;
036: import org.kuali.module.financial.document.ServiceBillingDocument;
037: import org.kuali.module.financial.rules.ServiceBillingDocumentRuleUtil;
038: import org.kuali.module.financial.service.ServiceBillingControlService;
039:
040: /**
041: * Authorization permissions specific to the Service Billing document.
042: */
043: public class ServiceBillingDocumentAuthorizer extends
044: AccountingDocumentAuthorizerBase {
045: private static Log LOG = LogFactory
046: .getLog(ServiceBillingDocumentAuthorizer.class);
047:
048: /**
049: * Overrides to always return false because there is never FO routing or FO approval for SB docs.
050: *
051: * @see FinancialDocumentAuthorizer#userOwnsAnyAccountingLine(KualiUser, List)
052: */
053: protected boolean userOwnsAnyAccountingLine(UniversalUser user,
054: List accountingLines) {
055: return false;
056: }
057:
058: /**
059: * Overrides parent to return an empty Map since FO routing doesn't apply to the SB doc.
060: *
061: * @see org.kuali.core.authorization.TransactionalDocumentAuthorizer#getEditableAccounts(org.kuali.core.document.TransactionalDocument,
062: * KualiUser)
063: */
064: public Map getEditableAccounts(TransactionalDocument document,
065: ChartUser user) {
066: return new HashMap();
067: }
068:
069: /**
070: * Overrides parent to return an empty Map since FO routing doesn't apply to the SB doc.
071: *
072: * @see org.kuali.kfs.document.authorization.AccountingDocumentAuthorizerBase#getEditableAccounts(java.util.List,
073: * org.kuali.module.chart.bo.ChartUser)
074: */
075: @Override
076: public Map getEditableAccounts(List<AccountingLine> lines,
077: ChartUser user) {
078: return new HashMap();
079: }
080:
081: /**
082: * SB docs use FP_SB_CTRL_T to limit income account users by workgroup. Thus a user who is not in any of the workgroups in that
083: * table will not be able to add any income accounting lines. This method uses those groups directly for initiation authority,
084: * instead of another group, so the administrator does not need to add users to multiple SB groups.
085: *
086: * @see org.kuali.core.authorization.DocumentAuthorizer#canInitiate(java.lang.String, org.kuali.core.bo.user.KualiUser)
087: */
088: public void canInitiate(String documentTypeName, UniversalUser user) {
089: boolean canInitiate = false;
090: ServiceBillingControl[] controls = SpringContext.getBean(
091: ServiceBillingControlService.class).getAll();
092: for (int i = 0; i < controls.length; i++) {
093: if (user.isMember(controls[i].getWorkgroupName())) {
094: canInitiate = true;
095: }
096: }
097: if (!canInitiate) {
098: // TODO: Give better message listing the required control workgroup names using DocumentInitiationAuthorizationException
099: throw new DocumentTypeAuthorizationException(user
100: .getPersonUserIdentifier(), "initiate",
101: documentTypeName);
102: }
103: }
104:
105: /**
106: * Overrides the error-correct flag for SB income account control. Unlike a copy, an SB error correction's income accounts
107: * cannot be changed, so if this user isn't authorized for all those income accounts then he won't be able to save or submit the
108: * error correction. We avoid this frustration by hiding that button in the first place.
109: *
110: * @see org.kuali.kfs.document.authorization.AccountingDocumentAuthorizerBase#getDocumentActionFlags(org.kuali.core.document.Document,
111: * org.kuali.core.bo.user.KualiUser)
112: */
113: @Override
114: public DocumentActionFlags getDocumentActionFlags(
115: Document document, UniversalUser user) {
116: TransactionalDocumentActionFlags flags = (TransactionalDocumentActionFlags) super
117: .getDocumentActionFlags(document, user);
118: boolean canUseAllIncomeSectionAccountsBool = false;
119: if (flags.getCanErrorCorrect() || flags.getCanCopy()) {
120: // canUseAllIncomeSectionAccounts may be an expensive operation, so we only invoke it exactly once only if it's
121: // absolutely necessary
122: // (i.e. can error correct or copy flags are true)
123: // if any of these flags are false, we rely on short circuiting to ignore the value of this variable when
124: // any of the flags are false
125: canUseAllIncomeSectionAccountsBool = canUseAllIncomeSectionAccounts(
126: (ServiceBillingDocument) document, user);
127: }
128: flags.setCanErrorCorrect(flags.getCanErrorCorrect()
129: && canUseAllIncomeSectionAccountsBool);
130: flags.setCanCopy(flags.getCanCopy()
131: && canUseAllIncomeSectionAccountsBool);
132: return flags;
133: }
134:
135: /**
136: * @param serviceBillingDocument
137: * @param user
138: * @return whether the given user is allowed to use all of the accounts in the given SB doc's income accounting lines section
139: */
140: private static boolean canUseAllIncomeSectionAccounts(
141: ServiceBillingDocument serviceBillingDocument,
142: UniversalUser user) {
143: for (SourceAccountingLine sourceAccountingLine : ((List<SourceAccountingLine>) serviceBillingDocument
144: .getSourceAccountingLines())) {
145: if (!ServiceBillingDocumentRuleUtil
146: .serviceBillingIncomeAccountIsAccessible(
147: sourceAccountingLine, null, user)) {
148: return false;
149: }
150: }
151: return true;
152: }
153: }
|