1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.ole.sys.document.validation.impl;
17  
18  import org.kuali.ole.sys.OLEConstants;
19  import org.kuali.ole.sys.OLEKeyConstants;
20  import org.kuali.ole.sys.context.SpringContext;
21  import org.kuali.ole.sys.document.AccountingDocument;
22  import org.kuali.ole.sys.document.validation.GenericValidation;
23  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
24  import org.kuali.ole.sys.document.validation.event.AttributedSaveDocumentEvent;
25  import org.kuali.rice.core.api.util.type.KualiDecimal;
26  import org.kuali.rice.core.web.format.CurrencyFormatter;
27  import org.kuali.rice.kew.api.exception.WorkflowException;
28  import org.kuali.rice.krad.service.DocumentService;
29  import org.kuali.rice.krad.util.GlobalVariables;
30  
31  
32  
33  
34  public class AccountingLineGroupTotalsUnchangedValidation extends GenericValidation {
35      private AccountingDocument accountingDocumentForValidation;
36      
37      
38  
39  
40  
41  
42  
43      public boolean validate(AttributedDocumentEvent event) {
44          AccountingDocument persistedDocument = null;
45          
46          if (event instanceof AttributedSaveDocumentEvent && !accountingDocumentForValidation.getDocumentHeader().getWorkflowDocument().isEnroute()) {
47              return true; 
48          }
49  
50          persistedDocument = retrievePersistedDocument(accountingDocumentForValidation);
51  
52          boolean isUnchanged = true;
53          if (persistedDocument == null) {
54              handleNonExistentDocumentWhenApproving(accountingDocumentForValidation);
55          }
56          else {
57              
58              KualiDecimal persistedSourceLineTotal = persistedDocument.getSourceTotal();
59              KualiDecimal persistedTargetLineTotal = persistedDocument.getTargetTotal();
60  
61              
62              KualiDecimal currentSourceLineTotal = accountingDocumentForValidation.getSourceTotal();
63              KualiDecimal currentTargetLineTotal = accountingDocumentForValidation.getTargetTotal();
64  
65              
66              
67              if (currentSourceLineTotal.compareTo(persistedSourceLineTotal) != 0) {
68                  isUnchanged = false;
69  
70                  
71                  buildTotalChangeErrorMessage(OLEConstants.SOURCE_ACCOUNTING_LINE_ERRORS, persistedSourceLineTotal, currentSourceLineTotal);
72              }
73  
74              if (currentTargetLineTotal.compareTo(persistedTargetLineTotal) != 0) {
75                  isUnchanged = false;
76  
77                  
78                  buildTotalChangeErrorMessage(OLEConstants.TARGET_ACCOUNTING_LINE_ERRORS, persistedTargetLineTotal, currentTargetLineTotal);
79              }
80          }
81  
82          return isUnchanged;
83      }
84  
85      
86  
87  
88  
89  
90  
91      protected AccountingDocument retrievePersistedDocument(AccountingDocument accountingDocument) {
92          AccountingDocument persistedDocument = null;
93  
94          try {
95              persistedDocument = (AccountingDocument) SpringContext.getBean(DocumentService.class).getByDocumentHeaderId(accountingDocument.getDocumentNumber());
96          }
97          catch (WorkflowException we) {
98              handleNonExistentDocumentWhenApproving(accountingDocument);
99          }
100 
101         return persistedDocument;
102     }
103     
104     
105 
106 
107 
108 
109 
110 
111     protected void buildTotalChangeErrorMessage(String propertyName, KualiDecimal persistedSourceLineTotal, KualiDecimal currentSourceLineTotal) {
112         String persistedTotal = (String) new CurrencyFormatter().format(persistedSourceLineTotal);
113         String currentTotal = (String) new CurrencyFormatter().format(currentSourceLineTotal);
114         GlobalVariables.getMessageMap().putError(propertyName, OLEKeyConstants.ERROR_DOCUMENT_SINGLE_ACCOUNTING_LINE_SECTION_TOTAL_CHANGED, new String[] { persistedTotal, currentTotal });
115     }
116 
117     
118 
119 
120 
121 
122     protected final void handleNonExistentDocumentWhenApproving(AccountingDocument accountingDocument) {
123         
124         if (!accountingDocument.getDocumentHeader().getWorkflowDocument().isInitiated()) {
125             throw new IllegalStateException("Document " + accountingDocument.getDocumentNumber() + " is not a valid document that currently exists in the system.");
126         }
127     }
128 
129     
130 
131 
132 
133     public AccountingDocument getAccountingDocumentForValidation() {
134         return accountingDocumentForValidation;
135     }
136 
137     
138 
139 
140 
141     public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
142         this.accountingDocumentForValidation = accountingDocumentForValidation;
143     }
144 }