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