View Javadoc
1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.kpme.tklm.time.clocklog.validation;
17  
18  import java.util.regex.Matcher;
19  import java.util.regex.Pattern;
20  
21  import org.apache.log4j.Logger;
22  import org.kuali.kpme.core.service.HrServiceLocator;
23  import org.kuali.kpme.tklm.time.clocklog.ClockLogBo;
24  import org.kuali.kpme.tklm.time.rules.clocklocation.validation.ClockLocationRuleRule;
25  import org.kuali.rice.krad.bo.PersistableBusinessObject;
26  import org.kuali.rice.krad.util.GlobalVariables;
27  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
28  import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
29  
30  public class ClockLogRule  extends MaintenanceDocumentRuleBase {
31  
32  	private static final String WILDCARD_CHARACTER = "%";
33  	private static final String REGEX_IP_ADDRESS_STRING = "(?:(?:"
34  			+ WILDCARD_CHARACTER
35  			+ "|25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:"
36  			+ WILDCARD_CHARACTER + "|25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";
37  	private static final Pattern REGEX_IP_ADDRESS_PATTERN = Pattern
38  			.compile(REGEX_IP_ADDRESS_STRING);
39  
40  	private static Logger LOG = Logger.getLogger(ClockLocationRuleRule.class);
41  
42  	protected boolean validateIpAddress(String ip) {
43  		boolean valid = false;
44  
45  		LOG.debug("Validating IP address: " + ip);
46  		Matcher matcher = REGEX_IP_ADDRESS_PATTERN.matcher(ip);
47  		valid = matcher.matches();
48  		if (!valid) {
49  			this.putFieldError("ipAddress", "ipaddress.invalid.format", ip);
50  		}
51  
52  		return valid;
53  	}
54  
55  	//TODO fix this class
56  	protected boolean validateWorkArea(ClockLogBo clockLog ) {
57  		boolean valid = false;
58  		LOG.debug("Validating workarea: " + clockLog.getWorkArea());
59  		int count = HrServiceLocator.getWorkAreaService().getWorkAreaCount(null, clockLog.getWorkArea());
60  		if (count >0 ) {
61  			valid = true;
62  			LOG.debug("found workarea.");
63  		} else {
64  			this.putFieldError("workArea", "error.existence", "Workarea '"
65  					+ clockLog.getWorkArea()+ "'");
66  		}
67  		return valid;
68  	}
69  
70  	protected boolean validateTask(ClockLogBo clockLog ) {
71  		boolean valid = false;
72  		LOG.debug("Validating task: " + clockLog.getTask());
73  		int count = HrServiceLocator.getTaskService().getTaskCount(clockLog.getTask());
74  		if (count >0 ) {
75  			valid = true;
76  			LOG.debug("found task.");
77  		} else {
78  			this.putFieldError("task", "error.existence", "Task '"
79  					+ clockLog.getTask()+ "'");
80  		}
81  		return valid;
82  	}
83  
84  
85  	/**
86  	 * It looks like the method that calls this class doesn't actually care
87  	 * about the return type.
88  	 */
89  	@Override
90  	protected boolean processCustomRouteDocumentBusinessRules(
91  			MaintenanceDocument document) {
92  		boolean valid = false;
93  
94  		LOG.debug("entering custom validation for ClockLog");
95  		PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewDataObject();
96  		if (pbo instanceof ClockLogBo) {
97  			ClockLogBo clockLog = (ClockLogBo) pbo;
98  			clockLog.setUserPrincipalId(GlobalVariables.getUserSession().getPrincipalId());
99  			if (clockLog != null) {
100 				valid = true;
101 				valid &= this.validateIpAddress(clockLog.getIpAddress());
102 				valid &= this.validateWorkArea(clockLog);
103 				valid &= this.validateTask(clockLog);
104 			}
105 		}
106 
107 		return valid;
108 	}
109 
110 }