View Javadoc
1   /*
2    * Copyright 2008-2009 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.fp.document.validation.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.fp.document.authorization.ServiceBillingDocumentAuthorizer;
20  import org.kuali.ole.sys.OLEKeyConstants;
21  import org.kuali.ole.sys.OLEPropertyConstants;
22  import org.kuali.ole.sys.businessobject.AccountingLine;
23  import org.kuali.ole.sys.businessobject.FinancialSystemDocumentHeader;
24  import org.kuali.ole.sys.document.AccountingDocument;
25  import org.kuali.ole.sys.document.Correctable;
26  import org.kuali.ole.sys.document.validation.GenericValidation;
27  import org.kuali.ole.sys.document.validation.event.AddAccountingLineEvent;
28  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
29  import org.kuali.ole.sys.document.validation.event.DeleteAccountingLineEvent;
30  import org.kuali.ole.sys.document.validation.event.UpdateAccountingLineEvent;
31  import org.kuali.rice.kew.api.WorkflowDocument;
32  import org.kuali.rice.kim.api.identity.Person;
33  import org.kuali.rice.kns.service.DataDictionaryService;
34  import org.kuali.rice.krad.rules.rule.event.KualiDocumentEvent;
35  import org.kuali.rice.krad.util.GlobalVariables;
36  
37  /**
38   * A validation that checks whether the given accounting line is accessible to the given user or not
39   */
40  public class ServiceBillingAccountingLineAccessibleValidation extends GenericValidation {
41      private DataDictionaryService dataDictionaryService;
42      private AccountingDocument accountingDocumentForValidation;
43      private AccountingLine accountingLineForValidation;
44      
45      /**
46       * Indicates what is being done to an accounting line. This allows the same method to be used for different actions.
47       */
48      public enum AccountingLineAction {
49          ADD(OLEKeyConstants.ERROR_ACCOUNTINGLINE_INACCESSIBLE_ADD), DELETE(OLEKeyConstants.ERROR_ACCOUNTINGLINE_INACCESSIBLE_DELETE), UPDATE(OLEKeyConstants.ERROR_ACCOUNTINGLINE_INACCESSIBLE_UPDATE);
50  
51          public final String accessibilityErrorKey;
52  
53          AccountingLineAction(String accessabilityErrorKey) {
54              this.accessibilityErrorKey = accessabilityErrorKey;
55          }
56      }
57  
58      /**
59       * Validates that the given accounting line is accessible for editing by the current user.
60       * <strong>This method expects a document as the first parameter and an accounting line as the second</strong>
61       * @see org.kuali.ole.sys.document.validation.Validation#validate(java.lang.Object[])
62       */
63      public boolean validate(AttributedDocumentEvent event) {        
64          final Person currentUser = GlobalVariables.getUserSession().getPerson();
65          
66          if (accountingDocumentForValidation instanceof Correctable) {
67              final String errorDocumentNumber = ((FinancialSystemDocumentHeader)accountingDocumentForValidation.getDocumentHeader()).getFinancialDocumentInErrorNumber();
68              if (StringUtils.isNotBlank(errorDocumentNumber))
69                  return true;
70          }
71          
72          final WorkflowDocument workflowDocument = accountingDocumentForValidation.getDocumentHeader().getWorkflowDocument();
73          if (accountingLineForValidation.isTargetAccountingLine() && (workflowDocument.isInitiated() || workflowDocument.isSaved())) {
74              return true; // all target lines are accessible PreRoute, no matter the account
75          }
76          
77          final boolean isAccessible = new ServiceBillingDocumentAuthorizer().canModifyAccountingLine(accountingDocumentForValidation, accountingLineForValidation,  currentUser);
78  
79          // report errors
80          if (!isAccessible) {
81              final String principalName = currentUser.getPrincipalName();
82              
83              final String[] chartErrorParams = new String[] { getDataDictionaryService().getAttributeLabel(accountingLineForValidation.getClass(), OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE), accountingLineForValidation.getChartOfAccountsCode(),  principalName};
84              GlobalVariables.getMessageMap().putError(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, convertEventToMessage(event), chartErrorParams);
85              
86              final String[] accountErrorParams = new String[] { getDataDictionaryService().getAttributeLabel(accountingLineForValidation.getClass(), OLEPropertyConstants.ACCOUNT_NUMBER), accountingLineForValidation.getAccountNumber(), principalName };
87              GlobalVariables.getMessageMap().putError(OLEPropertyConstants.ACCOUNT_NUMBER, convertEventToMessage(event), accountErrorParams);
88          }
89  
90          return isAccessible;
91      }
92      
93      
94      /**
95       * Determines what error message should be shown based on the event that required this validation
96       * @param event the event to use to determine the error message
97       * @return the key of the error message to display
98       */
99      protected String convertEventToMessage(KualiDocumentEvent event) {
100         if (event instanceof AddAccountingLineEvent) {
101             return AccountingLineAction.ADD.accessibilityErrorKey;
102         } else if (event instanceof UpdateAccountingLineEvent) {
103             return AccountingLineAction.UPDATE.accessibilityErrorKey;
104         } else if (event instanceof DeleteAccountingLineEvent) {
105             return AccountingLineAction.DELETE.accessibilityErrorKey;
106         } else {
107             return "";
108         }
109     }
110 
111     /**
112      * Gets the accountingDocumentForValidation attribute. 
113      * @return Returns the accountingDocumentForValidation.
114      */
115     public AccountingDocument getAccountingDocumentForValidation() {
116         return accountingDocumentForValidation;
117     }
118 
119     /**
120      * Sets the accountingDocumentForValidation attribute value.
121      * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
122      */
123     public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
124         this.accountingDocumentForValidation = accountingDocumentForValidation;
125     }
126 
127     /**
128      * Gets the accountingLineForValidation attribute. 
129      * @return Returns the accountingLineForValidation.
130      */
131     public AccountingLine getAccountingLineForValidation() {
132         return accountingLineForValidation;
133     }
134 
135     /**
136      * Sets the accountingLineForValidation attribute value.
137      * @param accountingLineForValidation The accountingLineForValidation to set.
138      */
139     public void setAccountingLineForValidation(AccountingLine accountingLineForValidation) {
140         this.accountingLineForValidation = accountingLineForValidation;
141     }
142 
143     /**
144      * Gets the dataDictionaryService attribute. 
145      * @return Returns the dataDictionaryService.
146      */
147     public DataDictionaryService getDataDictionaryService() {
148         return dataDictionaryService;
149     }
150 
151     /**
152      * Sets the dataDictionaryService attribute value.
153      * @param dataDictionaryService The dataDictionaryService to set.
154      */
155     public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
156         this.dataDictionaryService = dataDictionaryService;
157     }
158     
159 }
160