001 /**
002 * Copyright 2004-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.hr.time.clocklog.validation;
017
018 import java.util.regex.Matcher;
019 import java.util.regex.Pattern;
020
021 import org.apache.log4j.Logger;
022 import org.kuali.hr.time.clock.location.validation.ClockLocationRuleRule;
023 import org.kuali.hr.time.clocklog.ClockLog;
024 import org.kuali.hr.time.service.base.TkServiceLocator;
025 import org.kuali.rice.kns.document.MaintenanceDocument;
026 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
027 import org.kuali.rice.krad.bo.PersistableBusinessObject;
028 import org.kuali.rice.krad.util.GlobalVariables;
029
030 public class ClockLogRule extends MaintenanceDocumentRuleBase {
031
032 private static final String WILDCARD_CHARACTER = "%";
033 private static final String REGEX_IP_ADDRESS_STRING = "(?:(?:"
034 + WILDCARD_CHARACTER
035 + "|25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:"
036 + WILDCARD_CHARACTER + "|25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";
037 private static final Pattern REGEX_IP_ADDRESS_PATTERN = Pattern
038 .compile(REGEX_IP_ADDRESS_STRING);
039
040 private static Logger LOG = Logger.getLogger(ClockLocationRuleRule.class);
041
042 protected boolean validateIpAddress(String ip) {
043 boolean valid = false;
044
045 LOG.debug("Validating IP address: " + ip);
046 Matcher matcher = REGEX_IP_ADDRESS_PATTERN.matcher(ip);
047 valid = matcher.matches();
048 if (!valid) {
049 this.putFieldError("ipAddress", "ipaddress.invalid.format", ip);
050 }
051
052 return valid;
053 }
054
055 //TODO fix this class
056 protected boolean validateWorkArea(ClockLog clockLog ) {
057 boolean valid = false;
058 LOG.debug("Validating workarea: " + clockLog.getWorkArea());
059 int count = TkServiceLocator.getWorkAreaService().getWorkAreaCount(null, clockLog.getWorkArea());
060 if (count >0 ) {
061 valid = true;
062 LOG.debug("found workarea.");
063 } else {
064 this.putFieldError("workArea", "error.existence", "Workarea '"
065 + clockLog.getWorkArea()+ "'");
066 }
067 return valid;
068 }
069
070 protected boolean validateTask(ClockLog clockLog ) {
071 boolean valid = false;
072 LOG.debug("Validating task: " + clockLog.getTask());
073 int count = TkServiceLocator.getTaskService().getTaskCount(clockLog.getTask());
074 if (count >0 ) {
075 valid = true;
076 LOG.debug("found task.");
077 } else {
078 this.putFieldError("task", "error.existence", "Task '"
079 + clockLog.getTask()+ "'");
080 }
081 return valid;
082 }
083
084
085 /**
086 * It looks like the method that calls this class doesn't actually care
087 * about the return type.
088 */
089 @Override
090 protected boolean processCustomRouteDocumentBusinessRules(
091 MaintenanceDocument document) {
092 boolean valid = false;
093
094 LOG.debug("entering custom validation for ClockLog");
095 PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
096 if (pbo instanceof ClockLog) {
097 ClockLog clockLog = (ClockLog) pbo;
098 clockLog.setUserPrincipalId(GlobalVariables.getUserSession().getPrincipalId());
099 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 }