1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.kuali.kfs.coa.document.validation.impl;
20
21 import java.sql.Timestamp;
22 import java.util.Calendar;
23
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.commons.lang.time.DateUtils;
26 import org.kuali.kfs.coa.businessobject.AccountDelegateModel;
27 import org.kuali.kfs.coa.businessobject.AccountDelegateModelDetail;
28 import org.kuali.kfs.coa.businessobject.Chart;
29 import org.kuali.kfs.coa.businessobject.Organization;
30 import org.kuali.kfs.sys.KFSConstants;
31 import org.kuali.kfs.sys.KFSKeyConstants;
32 import org.kuali.kfs.sys.context.SpringContext;
33 import org.kuali.kfs.sys.document.service.FinancialSystemDocumentTypeService;
34 import org.kuali.kfs.sys.document.validation.impl.KfsMaintenanceDocumentRuleBase;
35 import org.kuali.rice.core.api.util.type.KualiDecimal;
36 import org.kuali.rice.kns.document.MaintenanceDocument;
37 import org.kuali.rice.krad.bo.PersistableBusinessObject;
38 import org.kuali.rice.krad.util.GlobalVariables;
39 import org.kuali.rice.krad.util.ObjectUtils;
40
41
42
43
44 public class AccountDelegateModelRule extends KfsMaintenanceDocumentRuleBase {
45
46 protected AccountDelegateModel model;
47
48
49
50
51
52
53
54 @Override
55 public void setupConvenienceObjects() {
56 model = (AccountDelegateModel) super.getNewBo();
57 for (AccountDelegateModelDetail delegateModel : model.getAccountDelegateModelDetails()) {
58 delegateModel.refreshNonUpdateableReferences();
59 }
60 }
61
62
63
64
65
66
67
68
69
70 @Override
71 protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
72 setupConvenienceObjects();
73 return checkSimpleRules(document, this.model);
74 }
75
76
77
78
79
80
81
82
83
84 @Override
85 protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
86 setupConvenienceObjects();
87 return checkSimpleRules(document, this.model);
88 }
89
90
91
92
93
94
95
96
97
98 @Override
99 protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
100 setupConvenienceObjects();
101 checkSimpleRules(document, this.model);
102 return true;
103 }
104
105
106
107
108
109
110
111
112
113 @Override
114 public boolean processCustomAddCollectionLineBusinessRules(MaintenanceDocument document, String collectionName, PersistableBusinessObject line) {
115 setupConvenienceObjects();
116 final FinancialSystemDocumentTypeService documentService = SpringContext.getBean(FinancialSystemDocumentTypeService.class);
117 return checkSimpleRulesForOrganizationRoutingModel(document, this.model, (AccountDelegateModelDetail) line, documentService);
118 }
119
120
121
122
123
124
125
126 protected boolean checkSimpleRules(MaintenanceDocument document, AccountDelegateModel globalDelegateTemplate) {
127 boolean success = true;
128
129 success &= checkModelNameHasAtLeastOneModel(globalDelegateTemplate);
130
131 int line = 0;
132 final FinancialSystemDocumentTypeService documentService = SpringContext.getBean(FinancialSystemDocumentTypeService.class);
133 for (AccountDelegateModelDetail delegateModel : globalDelegateTemplate.getAccountDelegateModelDetails()) {
134 GlobalVariables.getMessageMap().addToErrorPath(MAINTAINABLE_ERROR_PATH + ".accountDelegateModelDetails[" + line + "].");
135 success &= checkSimpleRulesForOrganizationRoutingModel(document, globalDelegateTemplate, delegateModel, documentService);
136 GlobalVariables.getMessageMap().addToErrorPath(MAINTAINABLE_ERROR_PATH + ".accountDelegateModelDetails[" + line + "].");
137 line++;
138 }
139
140 return success;
141 }
142
143
144
145
146
147
148 protected boolean checkSimpleRulesForOrganizationRoutingModel(MaintenanceDocument document, AccountDelegateModel globalDelegateTemplate, AccountDelegateModelDetail delegateModel, FinancialSystemDocumentTypeService documentService) {
149 boolean success = true;
150
151 if (delegateModel.isActive()) {
152 success &= checkDelegateFromAmountPositive(delegateModel);
153 success &= checkDelegateToAmountGreaterThanFromAmount(delegateModel);
154 success &= checkDelegateUserRules(document, delegateModel);
155 success &= checkPrimaryRoutePerDocType(globalDelegateTemplate, delegateModel);
156 success &= checkDelegateDocumentTypeCode(delegateModel.getFinancialDocumentTypeCode(), documentService);
157 }
158
159 return success;
160 }
161
162
163
164
165
166
167
168
169 protected boolean checkModelNameHasAtLeastOneModel(AccountDelegateModel globalDelegateTemplate) {
170 boolean success = true;
171 if (globalDelegateTemplate.getAccountDelegateModelDetails().size() == 0) {
172 success = false;
173 GlobalVariables.getMessageMap().putError(KFSConstants.MAINTENANCE_NEW_MAINTAINABLE + "add.accountDelegateModelDetails.financialDocumentTypeCode", KFSKeyConstants.ERROR_DOCUMENT_DELEGATE_CHANGE_NO_DELEGATE, new String[0]);
174 }
175
176 success &= checkDelegateModel(globalDelegateTemplate);
177 return success;
178 }
179
180 private boolean checkDelegateModel(AccountDelegateModel globalDelegateTemplate) {
181 boolean success = true;
182 Chart accounts = globalDelegateTemplate.getChartOfAccounts();
183 if (ObjectUtils.isNull(accounts)) {
184 GlobalVariables.getMessageMap().putError(KFSConstants.MAINTENANCE_NEW_MAINTAINABLE + "chartOfAccountsCode", KFSKeyConstants.ERROR_DOCUMENT_ACCTDELEGATEMAINT_INVALID_CHART_CODE);
185 success = false;
186 }
187 Organization organization = globalDelegateTemplate.getOrganization();
188 if (ObjectUtils.isNull(organization)) {
189 GlobalVariables.getMessageMap().putError(KFSConstants.MAINTENANCE_NEW_MAINTAINABLE + "organizationCode", KFSKeyConstants.ERROR_DOCUMENT_ACCTDELEGATEMAINT_INVALID_ORGANIZATION_CODE);
190 success = false;
191 }
192
193 return success;
194 }
195
196
197
198
199
200
201
202
203 protected boolean checkModelNameHasAtLeastOneActiveModel(AccountDelegateModel globalDelegateTemplate) {
204 boolean success = true;
205 int activeModelCount = 0;
206
207 for (AccountDelegateModelDetail mdl : globalDelegateTemplate.getAccountDelegateModelDetails()) {
208 if (mdl.isActive()) {
209 activeModelCount++;
210 }
211 }
212
213 if (activeModelCount == 0) {
214 success = false;
215 if (globalDelegateTemplate.getAccountDelegateModelDetails().size() == 0) {
216 GlobalVariables.getMessageMap().putError(KFSConstants.MAINTENANCE_NEW_MAINTAINABLE + "add.accountDelegateModelDetails.active", KFSKeyConstants.ERROR_DOCUMENT_DELEGATE_CHANGE_NO_ACTIVE_DELEGATE, new String[0]);
217 }
218 else {
219 GlobalVariables.getMessageMap().putError(KFSConstants.MAINTENANCE_NEW_MAINTAINABLE + "accountDelegateModelDetails[0].active", KFSKeyConstants.ERROR_DOCUMENT_DELEGATE_CHANGE_NO_ACTIVE_DELEGATE, new String[0]);
220 }
221 }
222 return success;
223 }
224
225
226
227
228
229
230
231 protected boolean checkDelegateFromAmountPositive(AccountDelegateModelDetail delegateModel) {
232 boolean result = true;
233 if (!ObjectUtils.isNull(delegateModel.getApprovalFromThisAmount())) {
234 if (delegateModel.getApprovalFromThisAmount().isLessThan(KualiDecimal.ZERO)) {
235 GlobalVariables.getMessageMap().putError("approvalFromThisAmount", KFSKeyConstants.ERROR_DOCUMENT_ACCTDELEGATEMAINT_FROM_AMOUNT_NONNEGATIVE, new String[0]);
236 result = false;
237 }
238 }
239 return result;
240 }
241
242
243
244
245
246
247
248
249 protected boolean checkDelegateToAmountGreaterThanFromAmount(AccountDelegateModelDetail delegateModel) {
250 boolean result = true;
251 if (!ObjectUtils.isNull(delegateModel.getApprovalFromThisAmount())) {
252 if (!ObjectUtils.isNull(delegateModel.getApprovalToThisAmount())) {
253 if (delegateModel.getApprovalToThisAmount().isLessThan(delegateModel.getApprovalFromThisAmount())) {
254 GlobalVariables.getMessageMap().putError("approvalToThisAmount", KFSKeyConstants.ERROR_DOCUMENT_ACCTDELEGATEMAINT_TO_AMOUNT_MORE_THAN_FROM_OR_ZERO, new String[0]);
255 result = false;
256 }
257 }
258 }
259 if (!ObjectUtils.isNull(delegateModel.getApprovalToThisAmount()) && delegateModel.getApprovalToThisAmount().isLessThan(KualiDecimal.ZERO)) {
260 GlobalVariables.getMessageMap().putError("approvalToThisAmount", KFSKeyConstants.ERROR_DOCUMENT_ACCTDELEGATEMAINT_TO_AMOUNT_MORE_THAN_FROM_OR_ZERO, new String[0]);
261 result = false;
262 }
263 return result;
264 }
265
266
267
268
269
270
271
272 protected boolean checkDelegateUserRules(MaintenanceDocument document, AccountDelegateModelDetail delegateModel) {
273 boolean success = true;
274
275
276 try {
277 delegateModel.setAccountDelegate(SpringContext.getBean(org.kuali.rice.kim.api.identity.PersonService.class).getPerson(delegateModel.getAccountDelegateUniversalId()));
278 }
279 catch (Exception e) {
280 if (LOG.isDebugEnabled()) {
281 LOG.debug("User Not Found Exception: " + e);
282 }
283 }
284
285
286 if ((delegateModel.getAccountDelegate() == null) || (delegateModel.getAccountDelegate().getPrincipalId() == null)){
287 GlobalVariables.getMessageMap().putError("accountDelegate.principalName", KFSKeyConstants.ERROR_DOCUMENT_ACCTDELEGATEMAINT_USER_DOESNT_EXIST, new String[0]);
288 success = false;
289 }
290
291 if (success) {
292 if (!getDocumentHelperService().getDocumentAuthorizer(document).isAuthorized(document, KFSConstants.PermissionNames.SERVE_AS_FISCAL_OFFICER_DELEGATE.namespace, KFSConstants.PermissionNames.SERVE_AS_FISCAL_OFFICER_DELEGATE.name, delegateModel.getAccountDelegate().getPrincipalId())) {
293 super.putFieldError("accountDelegate.principalName", KFSKeyConstants.ERROR_USER_MISSING_PERMISSION, new String[] {delegateModel.getAccountDelegate().getName(), KFSConstants.PermissionNames.SERVE_AS_FISCAL_OFFICER_DELEGATE.namespace, KFSConstants.PermissionNames.SERVE_AS_FISCAL_OFFICER_DELEGATE.name});
294 success = false;
295 }
296 }
297
298 return success;
299 }
300
301
302
303
304
305
306
307
308
309
310
311
312 protected boolean checkPrimaryRoutePerDocType(AccountDelegateModel globalDelegateTemplate, AccountDelegateModelDetail delegateModel) {
313 boolean success = true;
314
315
316 if (delegateModel == null || globalDelegateTemplate == null || globalDelegateTemplate.getAccountDelegateModelDetails().isEmpty()) {
317 return success;
318 }
319 if (!delegateModel.getAccountDelegatePrimaryRoutingIndicator()) {
320 return success;
321 }
322 if (StringUtils.isBlank(delegateModel.getFinancialDocumentTypeCode())) {
323 return success;
324 }
325
326
327
328 String docType = delegateModel.getFinancialDocumentTypeCode();
329 for (AccountDelegateModelDetail currDelegateModel : globalDelegateTemplate.getAccountDelegateModelDetails()) {
330 if (currDelegateModel.isActive() && !delegateModel.equals(currDelegateModel) && currDelegateModel.getAccountDelegatePrimaryRoutingIndicator() && delegateModel.getFinancialDocumentTypeCode().equals(currDelegateModel.getFinancialDocumentTypeCode())) {
331 success = false;
332 GlobalVariables.getMessageMap().putError("accountDelegatePrimaryRoutingIndicator", KFSKeyConstants.ERROR_DOCUMENT_GLOBAL_DELEGATEMAINT_PRIMARY_ROUTE_ALREADY_EXISTS_FOR_DOCTYPE, new String[0]);
333 }
334 }
335
336 return success;
337 }
338
339
340
341
342
343
344
345 protected boolean checkDelegateDocumentTypeCode(String documentTypeCode, FinancialSystemDocumentTypeService documentService) {
346 if (!documentService.isFinancialSystemDocumentType(documentTypeCode)) {
347 putFieldError("financialDocumentTypeCode", KFSKeyConstants.ERROR_DOCUMENT_ACCTDELEGATEMAINT_INVALID_DOC_TYPE, new String[] { documentTypeCode, KFSConstants.ROOT_DOCUMENT_TYPE });
348 return false;
349 }
350 return true;
351 }
352
353 }
354