1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.core.util;
17
18 import java.math.BigDecimal;
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.joda.time.LocalDate;
25 import org.kuali.kpme.core.accrualcategory.AccrualCategory;
26 import org.kuali.kpme.core.authorization.DepartmentalRule;
27 import org.kuali.kpme.core.calendar.Calendar;
28 import org.kuali.kpme.core.department.Department;
29 import org.kuali.kpme.core.earncode.EarnCode;
30 import org.kuali.kpme.core.earncode.group.EarnCodeGroup;
31 import org.kuali.kpme.core.earncode.group.EarnCodeGroupDefinition;
32 import org.kuali.kpme.core.earncode.security.EarnCodeSecurity;
33 import org.kuali.kpme.core.institution.Institution;
34 import org.kuali.kpme.core.kfs.coa.businessobject.Account;
35 import org.kuali.kpme.core.kfs.coa.businessobject.Chart;
36 import org.kuali.kpme.core.kfs.coa.businessobject.ObjectCode;
37 import org.kuali.kpme.core.kfs.coa.businessobject.Organization;
38 import org.kuali.kpme.core.kfs.coa.businessobject.SubAccount;
39 import org.kuali.kpme.core.kfs.coa.businessobject.SubObjectCode;
40 import org.kuali.kpme.core.leaveplan.LeavePlan;
41 import org.kuali.kpme.core.location.Location;
42 import org.kuali.kpme.core.paygrade.PayGrade;
43 import org.kuali.kpme.core.paytype.PayType;
44 import org.kuali.kpme.core.principal.PrincipalHRAttributes;
45 import org.kuali.kpme.core.salarygroup.SalaryGroup;
46 import org.kuali.kpme.core.service.HrServiceLocator;
47 import org.kuali.kpme.core.task.Task;
48 import org.kuali.kpme.core.workarea.WorkArea;
49 import org.kuali.rice.kim.api.identity.principal.Principal;
50 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
51 import org.kuali.rice.krad.service.KRADServiceLocator;
52 import org.kuali.rice.location.api.campus.Campus;
53 import org.kuali.rice.location.api.services.LocationApiServiceLocator;
54
55
56
57
58 public class ValidationUtils {
59
60
61
62
63
64
65
66
67
68 public static boolean validateWorkAreaDeptWildcarding(DepartmentalRule dr) {
69 boolean ret = true;
70
71 if (StringUtils.equals(dr.getDept(), HrConstants.WILDCARD_CHARACTER)) {
72 ret = dr.getWorkArea().equals(HrConstants.WILDCARD_LONG);
73 }
74
75 return ret;
76 }
77
78
79
80
81 public static boolean validateWorkArea(Long workArea) {
82 return validateWorkArea(workArea, null);
83 }
84
85
86
87
88 public static boolean validateDepartment(String department) {
89 return validateDepartment(department, null);
90 }
91
92
93
94
95 public static boolean validateAccrualCategory(String accrualCategory) {
96 return validateAccrualCategory(accrualCategory, null);
97 }
98
99
100 public static boolean validateSalGroup(String salGroup, LocalDate asOfDate) {
101 boolean valid = false;
102
103 if (StringUtils.equals(salGroup, HrConstants.WILDCARD_CHARACTER)) {
104 valid = true;
105 } else if (asOfDate != null) {
106 SalaryGroup sg = HrServiceLocator.getSalaryGroupService().getSalaryGroup(salGroup, asOfDate);
107 valid = (sg != null);
108 } else {
109 int count = HrServiceLocator.getSalaryGroupService().getSalGroupCount(salGroup);
110 valid = (count > 0);
111 }
112
113 return valid;
114 }
115
116 public static boolean validateEarnCode(String earnCode, LocalDate asOfDate) {
117 boolean valid = false;
118
119 if (asOfDate != null) {
120 EarnCode ec = HrServiceLocator.getEarnCodeService().getEarnCode(earnCode, asOfDate);
121 valid = (ec != null);
122 } else {
123 int count = HrServiceLocator.getEarnCodeService().getEarnCodeCount(earnCode);
124 valid = (count > 0);
125 }
126
127 return valid;
128 }
129
130 public static boolean validateLeavePlan(String leavePlan, LocalDate asOfDate) {
131 boolean valid = false;
132
133 if (asOfDate != null) {
134 LeavePlan lp = HrServiceLocator.getLeavePlanService().getLeavePlan(leavePlan, asOfDate);
135 valid = (lp != null);
136 } else {
137
138 valid = HrServiceLocator.getLeavePlanService().isValidLeavePlan(leavePlan);
139 }
140
141 return valid;
142 }
143
144 public static boolean validateEarnCodeOfAccrualCategory(String earnCode, String accrualCategory, LocalDate asOfDate) {
145 boolean valid = false;
146
147 if (asOfDate != null) {
148 EarnCode earnCodeObj = HrServiceLocator.getEarnCodeService().getEarnCode(earnCode, asOfDate);
149 if (earnCodeObj != null) {
150 if(StringUtils.equals(earnCodeObj.getAccrualCategory(),accrualCategory)) {
151 valid = true;
152 }
153 }
154 } else {
155 Map<String, String> fieldValues = new HashMap<String, String>();
156 fieldValues.put("earnCode", earnCode);
157 int matches = KRADServiceLocator.getBusinessObjectService().countMatching(EarnCode.class, fieldValues);
158
159 valid = matches > 0;
160 }
161
162 return valid;
163 }
164
165 public static boolean validateAccCategory(String accrualCategory, LocalDate asOfDate) {
166 boolean valid = false;
167
168 if (asOfDate != null) {
169 AccrualCategory ac = HrServiceLocator.getAccrualCategoryService().getAccrualCategory(accrualCategory, asOfDate);
170 valid = (ac != null);
171 } else {
172 Map<String, String> fieldValues = new HashMap<String, String>();
173 fieldValues.put("accrualCategory", accrualCategory);
174 int matches = KRADServiceLocator.getBusinessObjectService().countMatching(AccrualCategory.class, fieldValues);
175
176 valid = matches > 0;
177 }
178
179 return valid;
180 }
181
182 public static boolean validateAccCategory(String accrualCategory, String principalId, LocalDate asOfDate) {
183 boolean valid = false;
184
185 if (asOfDate != null) {
186 AccrualCategory ac = HrServiceLocator.getAccrualCategoryService().getAccrualCategory(accrualCategory, asOfDate);
187 if(ac != null && ac.getLeavePlan() != null) {
188
189 if(principalId != null) {
190 PrincipalHRAttributes principalHRAttributes = HrServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, asOfDate);
191 if(principalHRAttributes != null && principalHRAttributes.getLeavePlan() != null) {
192 valid = StringUtils.equals(ac.getLeavePlan().trim(), principalHRAttributes.getLeavePlan().trim());
193 }
194 } else {
195 valid = true;
196 }
197 }
198 } else {
199 Map<String, String> fieldValues = new HashMap<String, String>();
200 fieldValues.put("accrualCategory", accrualCategory);
201 int matches = KRADServiceLocator.getBusinessObjectService().countMatching(AccrualCategory.class, fieldValues);
202
203 valid = matches > 0;
204 }
205 return valid;
206 }
207
208 public static boolean validateLocation(String location, LocalDate asOfDate) {
209 boolean valid = false;
210 if(StringUtils.isNotEmpty(location)) {
211 if (asOfDate != null) {
212 if(ValidationUtils.isWildCard(location)) {
213 int count = HrServiceLocator.getLocationService().getLocationCount(location, asOfDate);
214 valid = (count > 0);
215 } else {
216 Location l = HrServiceLocator.getLocationService().getLocation(location, asOfDate);
217 valid = (l != null);
218 }
219 }
220 }
221
222 return valid;
223 }
224
225 public static boolean validatePayType(String payType, LocalDate asOfDate) {
226 boolean valid = false;
227
228 if (asOfDate != null) {
229 PayType pt = HrServiceLocator.getPayTypeService().getPayType(payType, asOfDate);
230 valid = (pt != null);
231 } else {
232 int count = HrServiceLocator.getPayTypeService().getPayTypeCount(payType);
233 valid = (count > 0);
234 }
235
236 return valid;
237 }
238
239
240 public static boolean validatePayGrade(String payGrade, String salGroup, LocalDate asOfDate) {
241 boolean valid = false;
242
243 if (asOfDate != null) {
244 PayGrade pg = HrServiceLocator.getPayGradeService().getPayGrade(payGrade, salGroup, asOfDate);
245 valid = (pg != null);
246 } else {
247 int count = HrServiceLocator.getPayGradeService().getPayGradeCount(payGrade);
248 valid = (count > 0);
249 }
250
251 return valid;
252 }
253
254
255
256
257
258
259
260
261 public static boolean validateEarnCode(String earnCode, boolean otEarnCode, LocalDate asOfDate) {
262 boolean valid = false;
263
264 if (asOfDate != null) {
265 EarnCode ec = HrServiceLocator.getEarnCodeService().getEarnCode(earnCode, asOfDate);
266 valid = (ec != null) && (otEarnCode ? ec.getOvtEarnCode().booleanValue() : true);
267 }
268
269 return valid;
270 }
271
272
273
274
275
276 public static boolean validateDepartment(String department, LocalDate asOfDate) {
277 boolean valid = false;
278
279 if (StringUtils.isEmpty(department)) {
280
281 } else if (asOfDate != null) {
282 Department d = HrServiceLocator.getDepartmentService().getDepartmentWithoutRoles(department, asOfDate);
283 valid = (d != null);
284 } else {
285 int count = HrServiceLocator.getDepartmentService().getDepartmentCount(department);
286 valid = (count > 0);
287 }
288
289 return valid;
290 }
291
292
293
294
295
296
297
298 public static boolean validateChart(String chart) {
299 boolean valid = false;
300
301 if (!StringUtils.isEmpty(chart)) {
302 Object o = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(Chart.class, chart);
303 if(o instanceof Chart) {
304 Chart chartObj = (Chart) o;
305 valid = chartObj.isActive();
306 }
307 }
308
309 return valid;
310 }
311
312
313
314
315
316 public static boolean validateWorkArea(Long workArea, LocalDate asOfDate) {
317 return ValidationUtils.validateWorkArea(workArea, null, asOfDate);
318 }
319
320 public static boolean validateWorkArea(Long workArea, String dept, LocalDate asOfDate) {
321 boolean valid = false;
322
323 if (workArea == null) {
324 valid = false;
325 } else if (workArea.equals(HrConstants.WILDCARD_LONG)) {
326 valid = true;
327 } else if (asOfDate != null) {
328 WorkArea wa = HrServiceLocator.getWorkAreaService().getWorkAreaWithoutRoles(workArea, asOfDate);
329 if (wa != null && dept != null) {
330 valid = StringUtils.equalsIgnoreCase(dept, wa.getDept());
331 } else {
332 valid = (wa != null);
333 }
334 } else {
335
336 }
337
338 return valid;
339 }
340
341
342
343
344 public static boolean validateAccrualCategory(String accrualCategory, LocalDate asOfDate) {
345 boolean valid = false;
346
347 if (StringUtils.equals(accrualCategory, HrConstants.WILDCARD_CHARACTER)) {
348 valid = true;
349 } else if (asOfDate != null) {
350 AccrualCategory ac = HrServiceLocator.getAccrualCategoryService().getAccrualCategory(accrualCategory, asOfDate);
351 valid = (ac != null);
352 }
353
354 return valid;
355 }
356
357
358
359
360
361 public static boolean validatePrincipalId(String principalId) {
362 boolean valid = false;
363 if (principalId != null) {
364 Principal p = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
365 valid = (p != null);
366 }
367 return valid;
368 }
369
370
371
372
373
374
375
376 public static boolean validateTask(Long task, LocalDate asOfDate) {
377 boolean valid = false;
378
379 if (task != null && asOfDate != null) {
380 Task t = HrServiceLocator.getTaskService().getTask(task, asOfDate);
381 valid = (t != null);
382 } else if (task != null) {
383 int count = HrServiceLocator.getTaskService().getTaskCount(task);
384 valid = (count > 0);
385 }
386
387 return valid;
388 }
389
390
391
392
393
394
395
396 public static boolean validateEarnGroup(String earnGroup, LocalDate asOfDate) {
397 boolean valid = false;
398
399 if (earnGroup != null && asOfDate != null) {
400 EarnCodeGroup eg = HrServiceLocator.getEarnCodeGroupService().getEarnCodeGroup(earnGroup, asOfDate);
401 valid = (eg != null);
402 } else if (earnGroup != null) {
403 int count = HrServiceLocator.getEarnCodeGroupService().getEarnCodeGroupCount(earnGroup);
404 valid = (count > 0);
405 }
406
407 return valid;
408 }
409
410
411
412
413
414
415 public static boolean earnGroupHasOvertimeEarnCodes(String earnGroup, LocalDate asOfDate) {
416 if (earnGroup != null && asOfDate != null) {
417 EarnCodeGroup eg = HrServiceLocator.getEarnCodeGroupService().getEarnCodeGroup(earnGroup, asOfDate);
418 if(eg != null) {
419 for(EarnCodeGroupDefinition egd : eg.getEarnCodeGroups()) {
420 if(egd.getEarnCode() != null) {
421 EarnCode ec = HrServiceLocator.getEarnCodeService().getEarnCode(egd.getEarnCode(), asOfDate);
422 if(ec != null && ec.getOvtEarnCode()) {
423 return true;
424 }
425 }
426 }
427 }
428 }
429
430 return false;
431 }
432
433
434
435
436
437 public static boolean validateCalendar(String calendarName) {
438 Calendar calendar = HrServiceLocator.getCalendarService().getCalendarByName(calendarName);
439 if(calendar!=null){
440 return true;
441 }else{
442 return false;
443 }
444 }
445
446 public static boolean duplicateDeptEarnCodeExists(EarnCodeSecurity deptEarnCode) {
447 boolean valid = false;
448 int count = HrServiceLocator.getEarnCodeSecurityService().getEarnCodeSecurityCount
449 (deptEarnCode.getDept(), deptEarnCode.getHrSalGroup(), deptEarnCode.getEarnCode(), deptEarnCode.isEmployee() ? "1" : "0",
450 deptEarnCode.isApprover() ? "1" : "0", deptEarnCode.isPayrollProcessor() ? "1" : "0", deptEarnCode.getLocation(), deptEarnCode.isActive() ? "Y" : "N", deptEarnCode.getEffectiveLocalDate(), null);
451 if(count == 1) {
452 valid = true;
453 count = HrServiceLocator.getEarnCodeSecurityService().getEarnCodeSecurityCount
454 (deptEarnCode.getDept(), deptEarnCode.getHrSalGroup(), deptEarnCode.getEarnCode(), deptEarnCode.isEmployee() ? "1" : "0",
455 deptEarnCode.isApprover() ? "1" : "0", deptEarnCode.isPayrollProcessor() ? "1" : "0", deptEarnCode.getLocation(), deptEarnCode.isActive() ? "Y" : "N", deptEarnCode.getEffectiveLocalDate(), deptEarnCode.getHrEarnCodeSecurityId());
456 if(count == 1) {
457 valid = false;
458 }
459 } else if(count > 1) {
460 valid = true;
461 }
462
463 return valid;
464 }
465
466
467
468
469
470
471 public static boolean validateOneYearFutureDate(LocalDate date){
472 LocalDate startDate = LocalDate.now().minusDays(1);
473 LocalDate endDate = LocalDate.now().plusYears(1);
474 return date.compareTo(startDate) * date.compareTo(endDate) <= 0;
475 }
476
477
478
479
480
481
482 public static boolean validateOneYearFutureEffectiveDate(LocalDate date){
483 LocalDate startDate = LocalDate.now().plusYears(1);
484 return date.compareTo(startDate) <= 0;
485 }
486
487
488
489
490
491
492 public static boolean validateFutureDate(LocalDate date){
493 LocalDate startDate = LocalDate.now();
494 return date.compareTo(startDate) > 0;
495 }
496
497
498
499
500 public static boolean validateCalendarByType(String calendarName, String calendarType) {
501 Map<String, String> fieldValues = new HashMap<String, String>();
502 fieldValues.put("calendarName", calendarName);
503 fieldValues.put("calendarTypes", calendarType);
504 int matches = KRADServiceLocator.getBusinessObjectService().countMatching(Calendar.class, fieldValues);
505
506 return matches > 0;
507 }
508
509 public static boolean validateRecordMethod(String recordMethod, String accrualCategory, LocalDate asOfDate) {
510 boolean valid = false;
511 if (asOfDate != null) {
512 AccrualCategory ac = HrServiceLocator.getAccrualCategoryService().getAccrualCategory(accrualCategory, asOfDate);
513 if (ac != null
514 && ac.getUnitOfTime() != null) {
515 if (HrConstants.RECORD_METHOD.HOUR.equals(ac.getUnitOfTime())
516 && (HrConstants.RECORD_METHOD.HOUR.equals(recordMethod))
517 || HrConstants.RECORD_METHOD.TIME.equals(recordMethod)) {
518 valid = true;
519 } else {
520 valid = StringUtils.equalsIgnoreCase(ac.getUnitOfTime(), recordMethod);
521 }
522
523 }
524 }
525 return valid;
526 }
527
528 public static boolean validateEarnCodeFraction(String earnCode, BigDecimal amount, LocalDate asOfDate) {
529 boolean valid = true;
530 EarnCode ec = HrServiceLocator.getEarnCodeService().getEarnCode(earnCode, asOfDate);
531 if(ec != null && ec.getFractionalTimeAllowed() != null) {
532 BigDecimal fracAllowed = new BigDecimal(ec.getFractionalTimeAllowed());
533 if(amount == null) {
534 amount=BigDecimal.ZERO;
535 }
536 if(amount.scale() > fracAllowed.scale()) {
537 valid = false;
538 }
539
540 }
541 return valid;
542 }
543
544 public static boolean validatePayGradeWithSalaryGroup(String salaryGroup, String payGrade, LocalDate asOfDate) {
545 if (asOfDate != null) {
546 PayGrade grade = HrServiceLocator.getPayGradeService().getPayGrade(payGrade, salaryGroup, asOfDate);
547 if(grade != null && StringUtils.isNotBlank(grade.getSalGroup()))
548 return StringUtils.equals(grade.getSalGroup(), salaryGroup);
549 }
550 return false;
551 }
552
553
554 public static boolean validateInstitution(String institutionCode, LocalDate asOfDate) {
555 boolean valid = false;
556 if(StringUtils.isNotEmpty(institutionCode)) {
557 if (asOfDate != null) {
558 if(ValidationUtils.isWildCard(institutionCode)) {
559 int count = HrServiceLocator.getInstitutionService().getInstitutionCount(institutionCode, asOfDate);
560 valid = (count > 0);
561 } else {
562 Institution inst = HrServiceLocator.getInstitutionService().getInstitution(institutionCode, asOfDate);
563 valid = (inst != null);
564 }
565 }
566 }
567 return valid;
568 }
569
570
571 public static boolean validateCampus(String campusCode) {
572 boolean valid = false;
573 if (ValidationUtils.isWildCard(campusCode)) {
574 valid = true;
575 } else {
576 Campus campusObj = LocationApiServiceLocator.getCampusService().getCampus(campusCode);
577 valid = (campusObj != null);
578 }
579 return valid;
580 }
581
582 public static boolean isWildCard(String aString) {
583 return (StringUtils.equals(aString, HrConstants.WILDCARD_CHARACTER) ||
584 StringUtils.equals(aString, "*"));
585 }
586
587 public static boolean wildCardMatch(String string1, String string2) {
588 if(ValidationUtils.isWildCard(string1) || ValidationUtils.isWildCard(string2))
589 return true;
590
591 return string1.equals(string2);
592 }
593
594
595
596
597
598
599
600 public static boolean validateAccount(String chartOfAccountsCode, String accountNumber) {
601 Map<String, String> fields = new HashMap<String, String>();
602 fields.put("accountNumber", accountNumber);
603 fields.put("chartOfAccountsCode", chartOfAccountsCode);
604 Account account = (Account) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(Account.class, fields);
605 if(account != null) {
606 return !account.isClosed();
607 }
608 return false;
609 }
610
611
612
613
614
615
616
617
618 public static boolean validateSubAccount(String subAccountNumber, String accountNumber, String chartOfAccountsCode) {
619 Map<String, String> fields = new HashMap<String, String>();
620 fields.put("subAccountNumber", subAccountNumber);
621 fields.put("accountNumber", accountNumber);
622 fields.put("chartOfAccountsCode", chartOfAccountsCode);
623 SubAccount subAccount = (SubAccount) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(SubAccount.class, fields);
624 if(subAccount != null) {
625 return subAccount.isActive();
626 }
627 return false;
628 }
629
630
631
632
633 public static boolean validateObjectCode(String financialObjectCode, String chartOfAccountsCode, Integer universityFiscalYear) {
634 Map<String, String> fields = new HashMap<String, String>();
635
636 fields.put("financialObjectCode", financialObjectCode);
637 fields.put("chartOfAccountsCode", chartOfAccountsCode);
638 if(universityFiscalYear != null) {
639 fields.put("universityFiscalYear", universityFiscalYear.toString());
640 }
641 fields.put("active", "true");
642 Collection<ObjectCode> objectCodes = KRADServiceLocator.getBusinessObjectService().findMatching(ObjectCode.class, fields);
643 if(objectCodes != null && objectCodes.size() >0) {
644 return true;
645 }
646 return false;
647 }
648
649
650
651
652
653
654
655
656
657
658
659 public static boolean validateSubObjectCode(String universityFiscalYear,
660 String chartOfAccountsCode,
661 String accountNumber,
662 String financialObjectCode,
663 String financialSubObjectCode) {
664 Map<String, String> fields = new HashMap<String, String>();
665 fields.put("financialSubObjectCode", financialSubObjectCode);
666 fields.put("chartOfAccountsCode", chartOfAccountsCode);
667 fields.put("accountNumber", accountNumber);
668 fields.put("financialObjectCode", financialObjectCode);
669 if(universityFiscalYear != null) {
670 fields.put("universityFiscalYear", universityFiscalYear.toString());
671 }
672 fields.put("active", "true");
673 Collection<SubObjectCode> subObjectCodes = KRADServiceLocator.getBusinessObjectService().findMatching(SubObjectCode.class, fields);
674 if(subObjectCodes != null && subObjectCodes.size() > 0) {
675 return true;
676 }
677 return false;
678 }
679
680
681
682
683
684
685
686 public static boolean validateOrganization(String organizationCode, String chartOfAccountsCode) {
687 Map<String, String> fields = new HashMap<String, String>();
688
689 fields.put("organizationCode", organizationCode);
690 fields.put("chartOfAccountsCode", chartOfAccountsCode);
691
692 Organization org = (Organization) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(Organization.class, fields);
693 if(org != null) {
694 return org.isActive();
695 }
696 return false;
697 }
698
699
700
701
702
703
704
705
706
707
708 public static boolean validateLocationWithSalaryGroup(String salaryGroup, String location, LocalDate asOfDate) {
709 if (asOfDate != null) {
710 SalaryGroup salGroup = HrServiceLocator.getSalaryGroupService().getSalaryGroup(salaryGroup, asOfDate);
711 if (salGroup != null && StringUtils.isNotBlank(salGroup.getLocation())) {
712 if (ValidationUtils.isWildCard(salGroup.getLocation())) {
713 return true;
714 } else {
715 return StringUtils.equals(salGroup.getLocation(), location);
716 }
717 }
718 }
719 return false;
720 }
721 }