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.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 clockLog.setUnapprovedIP(false);
051 return;
052 }
053 }
054 }
055 clockLog.setUnapprovedIP(true);
056 GlobalVariables.getMessageMap().putWarning("property", "ipaddress.invalid.format", clockLog.getIpAddress());
057
058 }
059
060 private boolean compareIpAddresses(String ipAddressRule, String ipAddress){
061 String[] rulePieces = StringUtils.split(ipAddressRule, ".");
062 int ruleMax = rulePieces.length-1;
063
064 String[] ipAddPieces = StringUtils.split(ipAddress,".");
065 boolean match = true;
066 for(int i=0; i<ipAddPieces.length; i++){
067 if( ((i > ruleMax) && StringUtils.equals("%", rulePieces[ruleMax])) ||
068 ((i <= ruleMax) && ( StringUtils.equals(ipAddPieces[i], rulePieces[i]) || StringUtils.equals("%", rulePieces[i]) ))
069 )
070 {
071 // we don't need to do anything.
072 } else {
073 return false;
074 }
075 }
076 return match;
077 }
078
079 @Override
080 public List<ClockLocationRule> getClockLocationRule(String dept, Long workArea,String principalId, Long jobNumber, Date asOfDate) {
081
082 // 1 : dept, wa, principal, job
083 List<ClockLocationRule> clockLocationRule = clockLocationDao.getClockLocationRule(dept, workArea,principalId,jobNumber,asOfDate);
084 if(!clockLocationRule.isEmpty()){
085 return clockLocationRule;
086 }
087
088 // 2 : dept, wa, principal, -1
089 clockLocationRule = clockLocationDao.getClockLocationRule(dept, workArea, principalId, -1L, asOfDate);
090 if(!clockLocationRule.isEmpty()){
091 return clockLocationRule;
092 }
093
094 // 3 : dept, wa, % , job
095 clockLocationRule = clockLocationDao.getClockLocationRule(dept, workArea, "%", jobNumber, asOfDate);
096 if(!clockLocationRule.isEmpty()){
097 return clockLocationRule;
098 }
099
100 // 4 : dept, -1, principal, job
101 clockLocationRule = clockLocationDao.getClockLocationRule(dept, -1L, principalId, jobNumber, asOfDate);
102 if(!clockLocationRule.isEmpty()){
103 return clockLocationRule;
104 }
105
106 // 5 : dept, wa, % , -1
107 clockLocationRule = clockLocationDao.getClockLocationRule(dept, workArea, "%", -1L, asOfDate);
108 if(!clockLocationRule.isEmpty()){
109 return clockLocationRule;
110 }
111
112 // 6 : dept, -1, principal, -1
113 clockLocationRule = clockLocationDao.getClockLocationRule(dept, -1L, principalId, -1L, asOfDate);
114 if(!clockLocationRule.isEmpty()){
115 return clockLocationRule;
116 }
117
118 // 7 : dept, -1, % , job
119 clockLocationRule = clockLocationDao.getClockLocationRule(dept, -1L, "%", jobNumber, asOfDate);
120 if(!clockLocationRule.isEmpty()){
121 return clockLocationRule;
122 }
123
124 // 8 : dept, -1, % , job
125 clockLocationRule = clockLocationDao.getClockLocationRule(dept, -1L, "%", -1L, asOfDate);
126 return clockLocationRule;
127 }
128
129 @Override
130 public List<ClockLocationRule> getNewerVersionClockLocationRule(
131 String dept, Long workArea, String principalId, Long jobNumber,
132 Date asOfDate) {
133
134 return clockLocationDao.getNewerVersionClockLocationRule(dept, workArea, principalId, jobNumber, asOfDate);
135 }
136
137 @Override
138 public ClockLocationRule getClockLocationRule(String tkClockLocationRuleId) {
139 return clockLocationDao.getClockLocationRule(tkClockLocationRuleId);
140 }
141
142 public void populateIPAddressesForCLR(ClockLocationRule clr){
143 clockLocationDao.populateIPAddressesForCLR(clr);
144 }
145
146 public List<ClockLocationRule> getClockLocationRules(Date fromEffdt, Date toEffdt, String principalId, String jobNumber,
147 String dept, String workArea, String active, String showHistory){
148 return clockLocationDao.getClockLocationRules(fromEffdt, toEffdt, principalId, jobNumber, dept, workArea, active, showHistory);
149
150 }
151 }