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 java.util.Iterator;
19 import java.util.Map;
20 import java.util.Set;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.kuali.ole.sys.OLEConstants;
24 import org.kuali.ole.sys.OLEKeyConstants;
25 import org.kuali.ole.sys.OLEPropertyConstants;
26 import org.kuali.ole.sys.businessobject.AccountingLine;
27 import org.kuali.ole.sys.businessobject.FinancialSystemDocumentHeader;
28 import org.kuali.ole.sys.document.AccountingDocument;
29 import org.kuali.ole.sys.document.Correctable;
30 import org.kuali.ole.sys.document.authorization.AccountingLineAuthorizer;
31 import org.kuali.ole.sys.document.authorization.AccountingLineAuthorizerBase;
32 import org.kuali.ole.sys.document.datadictionary.AccountingLineGroupDefinition;
33 import org.kuali.ole.sys.document.datadictionary.FinancialSystemTransactionalDocumentEntry;
34 import org.kuali.ole.sys.document.validation.GenericValidation;
35 import org.kuali.ole.sys.document.validation.event.AddAccountingLineEvent;
36 import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
37 import org.kuali.ole.sys.document.validation.event.DeleteAccountingLineEvent;
38 import org.kuali.ole.sys.document.validation.event.UpdateAccountingLineEvent;
39 import org.kuali.rice.kim.api.identity.Person;
40 import org.kuali.rice.krad.service.DataDictionaryService;
41 import org.kuali.rice.krad.rules.rule.event.KualiDocumentEvent;
42 import org.kuali.rice.krad.util.GlobalVariables;
43
44
45
46
47 public class AccountingLineAccessibleValidation extends GenericValidation {
48 protected DataDictionaryService dataDictionaryService;
49 protected AccountingDocument accountingDocumentForValidation;
50 protected AccountingLine accountingLineForValidation;
51
52
53
54
55 public enum AccountingLineAction {
56 ADD(OLEKeyConstants.ERROR_ACCOUNTINGLINE_INACCESSIBLE_ADD), DELETE(OLEKeyConstants.ERROR_ACCOUNTINGLINE_INACCESSIBLE_DELETE), UPDATE(OLEKeyConstants.ERROR_ACCOUNTINGLINE_INACCESSIBLE_UPDATE);
57
58 public final String accessibilityErrorKey;
59
60 AccountingLineAction(String accessabilityErrorKey) {
61 this.accessibilityErrorKey = accessabilityErrorKey;
62 }
63 }
64
65
66
67
68
69
70 public boolean validate(AttributedDocumentEvent event) {
71 final Person currentUser = GlobalVariables.getUserSession().getPerson();
72
73 if (accountingDocumentForValidation instanceof Correctable) {
74 final String errorDocumentNumber = ((FinancialSystemDocumentHeader)accountingDocumentForValidation.getDocumentHeader()).getFinancialDocumentInErrorNumber();
75 if (StringUtils.isNotBlank(errorDocumentNumber))
76 return true;
77 }
78
79 final AccountingLineAuthorizer accountingLineAuthorizer = lookupAccountingLineAuthorizer();
80 final boolean lineIsAccessible = accountingLineAuthorizer.hasEditPermissionOnAccountingLine(accountingDocumentForValidation, accountingLineForValidation, getAccountingLineCollectionProperty(), currentUser, true);
81 final boolean isAccessible = accountingLineAuthorizer.hasEditPermissionOnField(accountingDocumentForValidation, accountingLineForValidation, getAccountingLineCollectionProperty(), OLEPropertyConstants.ACCOUNT_NUMBER, lineIsAccessible, true, currentUser);
82
83
84 if (!isAccessible) {
85 final String principalName = currentUser.getPrincipalName();
86
87 final String[] chartErrorParams = new String[] { getDataDictionaryService().getAttributeLabel(accountingLineForValidation.getClass(), OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE), accountingLineForValidation.getChartOfAccountsCode(), principalName};
88 GlobalVariables.getMessageMap().putError(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, convertEventToMessage(event), chartErrorParams);
89
90 final String[] accountErrorParams = new String[] { getDataDictionaryService().getAttributeLabel(accountingLineForValidation.getClass(), OLEPropertyConstants.ACCOUNT_NUMBER), accountingLineForValidation.getAccountNumber(), principalName };
91 GlobalVariables.getMessageMap().putError(OLEPropertyConstants.ACCOUNT_NUMBER, convertEventToMessage(event), accountErrorParams);
92 }
93
94 return isAccessible;
95 }
96
97
98
99
100
101 protected String getGroupName() {
102 return (accountingLineForValidation.isSourceAccountingLine() ? OLEConstants.SOURCE_ACCOUNTING_LINES_GROUP_NAME : OLEConstants.TARGET_ACCOUNTING_LINES_GROUP_NAME);
103 }
104
105
106
107
108 protected AccountingLineAuthorizer lookupAccountingLineAuthorizer() {
109 final String groupName = getGroupName();
110 final Map<String, AccountingLineGroupDefinition> groups = ((FinancialSystemTransactionalDocumentEntry)dataDictionaryService.getDataDictionary().getDictionaryObjectEntry(accountingDocumentForValidation.getClass().getName())).getAccountingLineGroups();
111
112 if (groups.isEmpty()) return new AccountingLineAuthorizerBase();
113 if (groups.containsKey(groupName)) return groups.get(groupName).getAccountingLineAuthorizer();
114
115 final Set<String> groupNames = groups.keySet();
116 final Iterator<String> groupNameIterator = groupNames.iterator();
117 final String firstGroupName = groupNameIterator.next();
118 return groups.get(firstGroupName).getAccountingLineAuthorizer();
119 }
120
121
122
123
124
125 protected String getAccountingLineCollectionProperty() {
126 String propertyName = null;
127 if (GlobalVariables.getMessageMap().getErrorPath().size() > 0) {
128 propertyName = ((String)GlobalVariables.getMessageMap().getErrorPath().get(0)).replaceFirst(".*?document\\.", "");
129 } else {
130 propertyName = accountingLineForValidation.isSourceAccountingLine() ? OLEConstants.PermissionAttributeValue.SOURCE_ACCOUNTING_LINES.value : OLEConstants.PermissionAttributeValue.TARGET_ACCOUNTING_LINES.value;
131 }
132 if (propertyName.equals("newSourceLine")) return OLEConstants.PermissionAttributeValue.SOURCE_ACCOUNTING_LINES.value;
133 if (propertyName.equals("newTargetLine")) return OLEConstants.PermissionAttributeValue.TARGET_ACCOUNTING_LINES.value;
134 return propertyName;
135 }
136
137
138
139
140
141
142 protected String convertEventToMessage(KualiDocumentEvent event) {
143 if (event instanceof AddAccountingLineEvent) {
144 return AccountingLineAction.ADD.accessibilityErrorKey;
145 } else if (event instanceof UpdateAccountingLineEvent) {
146 return AccountingLineAction.UPDATE.accessibilityErrorKey;
147 } else if (event instanceof DeleteAccountingLineEvent) {
148 return AccountingLineAction.DELETE.accessibilityErrorKey;
149 } else {
150 return "";
151 }
152 }
153
154
155
156
157
158 public AccountingDocument getAccountingDocumentForValidation() {
159 return accountingDocumentForValidation;
160 }
161
162
163
164
165
166 public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
167 this.accountingDocumentForValidation = accountingDocumentForValidation;
168 }
169
170
171
172
173
174 public AccountingLine getAccountingLineForValidation() {
175 return accountingLineForValidation;
176 }
177
178
179
180
181
182 public void setAccountingLineForValidation(AccountingLine accountingLineForValidation) {
183 this.accountingLineForValidation = accountingLineForValidation;
184 }
185
186
187
188
189
190 public DataDictionaryService getDataDictionaryService() {
191 return dataDictionaryService;
192 }
193
194
195
196
197
198 public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
199 this.dataDictionaryService = dataDictionaryService;
200 }
201
202 }
203