1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.clock.location.validation;
17
18 import java.util.List;
19
20 import org.apache.commons.lang.StringUtils;
21 import org.apache.log4j.Logger;
22 import org.kuali.hr.time.authorization.AuthorizationValidationUtils;
23 import org.kuali.hr.time.authorization.DepartmentalRule;
24 import org.kuali.hr.time.authorization.DepartmentalRuleAuthorizer;
25 import org.kuali.hr.time.clock.location.ClockLocationRule;
26 import org.kuali.hr.time.clock.location.ClockLocationRuleIpAddress;
27 import org.kuali.hr.time.service.base.TkServiceLocator;
28 import org.kuali.hr.time.util.TkConstants;
29 import org.kuali.hr.time.util.ValidationUtils;
30 import org.kuali.rice.kns.document.MaintenanceDocument;
31 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
32 import org.kuali.rice.krad.bo.PersistableBusinessObject;
33
34 public class ClockLocationRuleRule extends MaintenanceDocumentRuleBase {
35
36 private static Logger LOG = Logger.getLogger(ClockLocationRuleRule.class);
37
38 public static boolean validateIpAddress(String ip) {
39 LOG.debug("Validating IP address: " + ip);
40 if(ip == null) {
41 return false;
42 }
43 if(ip.isEmpty() || ip.length() > 15 || ip.endsWith(TkConstants.IP_SEPERATOR) || ip.startsWith(TkConstants.IP_SEPERATOR)) {
44 return false;
45 }
46 String[] lst = StringUtils.split(ip, TkConstants.IP_SEPERATOR);
47 if(lst.length > 4 || (lst.length <4 && ip.indexOf(TkConstants.WILDCARD_CHARACTER)< 0)) {
48 return false;
49 }
50 for(String str : lst) {
51 if(!str.matches(TkConstants.IP_WILDCARD_PATTERN)) {
52 return false;
53 }
54 }
55 return true;
56 }
57
58 boolean validateIpAddresses(List<ClockLocationRuleIpAddress> ipAddresses) {
59 for(ClockLocationRuleIpAddress ip : ipAddresses) {
60 if(!validateIpAddress(ip.getIpAddress())) {
61 return this.flagIPAddressError(ip.getIpAddress());
62 }
63 }
64 return true;
65 }
66
67 boolean flagIPAddressError(String ip) {
68 this.putFieldError("ipAddresses", "ipaddress.invalid.format", ip);
69 return false;
70 }
71
72 boolean validateWorkArea(ClockLocationRule clr) {
73 boolean valid = true;
74 if (clr.getWorkArea() != null
75 && !ValidationUtils.validateWorkArea(clr.getWorkArea(), clr
76 .getEffectiveDate())) {
77 this.putFieldError("workArea", "error.existence", "workArea '"
78 + clr.getWorkArea() + "'");
79 valid = false;
80 } else if (clr.getWorkArea() != null
81 && !clr.getWorkArea().equals(TkConstants.WILDCARD_LONG)) {
82 int count = TkServiceLocator.getWorkAreaService().getWorkAreaCount(clr.getDept(), clr.getWorkArea());
83 valid = (count > 0);
84 if (!valid) {
85 this.putFieldError("workArea", "dept.workarea.invalid.sync",
86 clr.getWorkArea() + "");
87 }
88 }
89 return valid;
90 }
91
92 protected boolean validateDepartment(ClockLocationRule clr) {
93 boolean ret = false;
94
95 if (!StringUtils.isEmpty(clr.getDept())) {
96 if (!ValidationUtils.validateDepartment(clr.getDept(), clr.getEffectiveDate())) {
97 this.putFieldError("dept", "error.existence", "department '" + clr.getDept() + "'");
98 } else if (!DepartmentalRuleAuthorizer.hasAccessToWrite(clr)) {
99 this.putFieldError("dept", "error.department.permissions", clr.getDept());
100 } else {
101 ret = true;
102 }
103 }
104
105 return ret;
106 }
107
108 protected boolean validateJobNumber(ClockLocationRule clr) {
109 boolean valid = true;
110 if (clr.getJobNumber() == null) {
111 valid = false;
112 } else if (!clr.getJobNumber().equals(TkConstants.WILDCARD_LONG)) {
113 int count = TkServiceLocator.getJobService().getJobCount(clr.getPrincipalId(), clr.getJobNumber(),null);
114 valid = (count > 0);
115 if (!valid) {
116 this.putFieldError("jobNumber", "principalid.job.invalid.sync",
117 clr.getJobNumber() + "");
118 }
119 }
120 return valid;
121 }
122
123 protected boolean validatePrincipalId(ClockLocationRule clr) {
124 boolean valid = false;
125 if (clr.getPrincipalId() == null) {
126 valid = false;
127 } else {
128 valid = true;
129 }
130 return valid;
131 }
132
133
134
135
136
137
138
139
140
141
142 boolean validateWildcards(DepartmentalRule clr) {
143 boolean valid = true;
144
145 if (!ValidationUtils.validateWorkAreaDeptWildcarding(clr)) {
146
147 this.putFieldError("dept", "error.wc.wadef", "department '" + clr.getDept() + "'");
148 valid = false;
149 }
150
151 if (StringUtils.equals(clr.getDept(), TkConstants.WILDCARD_CHARACTER) &&
152 !AuthorizationValidationUtils.canWildcardDepartment(clr)) {
153 this.putFieldError("dept", "error.wc.dept.perm", "department '" + clr.getDept() + "'");
154 valid = false;
155 }
156
157 if (clr!= null && clr.getWorkArea() != null && clr.getWorkArea().equals(TkConstants.WILDCARD_LONG) &&
158 !AuthorizationValidationUtils.canWildcardWorkArea(clr)) {
159 this.putFieldError("dept", "error.wc.wa.perm", "department '" + clr.getDept() + "'");
160 valid = false;
161 }
162
163 return valid;
164 }
165
166
167
168
169
170
171 @Override
172 protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
173 boolean valid = false;
174
175 PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
176 if (pbo instanceof ClockLocationRule) {
177 ClockLocationRule clr = (ClockLocationRule) pbo;
178 valid = this.validateDepartment(clr);
179 valid &= this.validateWorkArea(clr);
180 valid &= this.validateWildcards(clr);
181 valid &= this.validatePrincipalId(clr);
182 valid &= this.validateJobNumber(clr);
183 valid &= this.validateIpAddresses(clr.getIpAddresses());
184 }
185
186 return valid;
187 }
188 }