001 /** 002 * Copyright 2004-2012 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.clock.location.service; 017 018 import org.apache.commons.lang.StringUtils; 019 import org.kuali.hr.time.clock.location.ClockLocationRule; 020 import org.kuali.hr.time.clock.location.ClockLocationRuleIpAddress; 021 import org.kuali.hr.time.clock.location.dao.ClockLocationDao; 022 import org.kuali.hr.time.clocklog.ClockLog; 023 import org.kuali.rice.krad.util.GlobalVariables; 024 025 import java.sql.Date; 026 import java.util.List; 027 028 public class ClockLocationRuleServiceImpl implements ClockLocationRuleService { 029 private ClockLocationDao clockLocationDao; 030 031 public ClockLocationDao getClockLocationDao() { 032 return clockLocationDao; 033 } 034 035 public void setClockLocationDao(ClockLocationDao clockLocationDao) { 036 this.clockLocationDao = clockLocationDao; 037 } 038 039 public void processClockLocationRule(ClockLog clockLog, Date asOfDate){ 040 List<ClockLocationRule> lstClockLocationRules = getClockLocationRule(clockLog.getJob().getDept(), 041 clockLog.getWorkArea(), clockLog.getPrincipalId(), clockLog.getJobNumber(), asOfDate); 042 if(lstClockLocationRules.isEmpty()){ 043 return; 044 } 045 for(ClockLocationRule clockLocationRule : lstClockLocationRules){ 046 List<ClockLocationRuleIpAddress> ruleIpAddresses = clockLocationRule.getIpAddresses(); 047 String ipAddressClock = clockLog.getIpAddress(); 048 for(ClockLocationRuleIpAddress ruleIp : ruleIpAddresses) { 049 if(compareIpAddresses(ruleIp.getIpAddress(), ipAddressClock)){ 050 return; 051 } 052 } 053 } 054 GlobalVariables.getMessageMap().putWarning("property", "ipaddress.invalid.format", clockLog.getIpAddress()); 055 056 } 057 058 private boolean compareIpAddresses(String ipAddressRule, String ipAddress){ 059 String[] rulePieces = StringUtils.split(ipAddressRule, "."); 060 int ruleMax = rulePieces.length-1; 061 062 String[] ipAddPieces = StringUtils.split(ipAddress,"."); 063 boolean match = true; 064 for(int i=0; i<ipAddPieces.length; i++){ 065 if( ((i > ruleMax) && StringUtils.equals("%", rulePieces[ruleMax])) || 066 ((i <= ruleMax) && ( StringUtils.equals(ipAddPieces[i], rulePieces[i]) || StringUtils.equals("%", rulePieces[i]) )) 067 ) 068 { 069 // we don't need to do anything. 070 } else { 071 return false; 072 } 073 } 074 return match; 075 } 076 077 @Override 078 public List<ClockLocationRule> getClockLocationRule(String dept, Long workArea,String principalId, Long jobNumber, Date asOfDate) { 079 080 // 1 : dept, wa, principal, job 081 List<ClockLocationRule> clockLocationRule = clockLocationDao.getClockLocationRule(dept, workArea,principalId,jobNumber,asOfDate); 082 if(!clockLocationRule.isEmpty()){ 083 return clockLocationRule; 084 } 085 086 // 2 : dept, wa, principal, -1 087 clockLocationRule = clockLocationDao.getClockLocationRule(dept, workArea, principalId, -1L, asOfDate); 088 if(!clockLocationRule.isEmpty()){ 089 return clockLocationRule; 090 } 091 092 // 3 : dept, wa, % , job 093 clockLocationRule = clockLocationDao.getClockLocationRule(dept, workArea, "%", jobNumber, asOfDate); 094 if(!clockLocationRule.isEmpty()){ 095 return clockLocationRule; 096 } 097 098 // 4 : dept, -1, principal, job 099 clockLocationRule = clockLocationDao.getClockLocationRule(dept, -1L, principalId, jobNumber, asOfDate); 100 if(!clockLocationRule.isEmpty()){ 101 return clockLocationRule; 102 } 103 104 // 5 : dept, wa, % , -1 105 clockLocationRule = clockLocationDao.getClockLocationRule(dept, workArea, "%", -1L, asOfDate); 106 if(!clockLocationRule.isEmpty()){ 107 return clockLocationRule; 108 } 109 110 // 6 : dept, -1, principal, -1 111 clockLocationRule = clockLocationDao.getClockLocationRule(dept, -1L, principalId, -1L, asOfDate); 112 if(!clockLocationRule.isEmpty()){ 113 return clockLocationRule; 114 } 115 116 // 7 : dept, -1, % , job 117 clockLocationRule = clockLocationDao.getClockLocationRule(dept, -1L, "%", jobNumber, asOfDate); 118 if(!clockLocationRule.isEmpty()){ 119 return clockLocationRule; 120 } 121 122 // 8 : dept, -1, % , job 123 clockLocationRule = clockLocationDao.getClockLocationRule(dept, -1L, "%", -1L, asOfDate); 124 return clockLocationRule; 125 } 126 127 @Override 128 public List<ClockLocationRule> getNewerVersionClockLocationRule( 129 String dept, Long workArea, String principalId, Long jobNumber, 130 Date asOfDate) { 131 132 return clockLocationDao.getNewerVersionClockLocationRule(dept, workArea, principalId, jobNumber, asOfDate); 133 } 134 135 @Override 136 public ClockLocationRule getClockLocationRule(String tkClockLocationRuleId) { 137 return clockLocationDao.getClockLocationRule(tkClockLocationRuleId); 138 } 139 140 public void populateIPAddressesForCLR(ClockLocationRule clr){ 141 clockLocationDao.populateIPAddressesForCLR(clr); 142 } 143 144 }