View Javadoc

1   /**
2    * Copyright 2004-2012 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.hr.time.clock.location.service;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.hr.time.clock.location.ClockLocationRule;
20  import org.kuali.hr.time.clock.location.ClockLocationRuleIpAddress;
21  import org.kuali.hr.time.clock.location.dao.ClockLocationDao;
22  import org.kuali.hr.time.clocklog.ClockLog;
23  import org.kuali.rice.krad.util.GlobalVariables;
24  
25  import java.sql.Date;
26  import java.util.List;
27  
28  public class ClockLocationRuleServiceImpl implements ClockLocationRuleService {
29  	private ClockLocationDao clockLocationDao;
30  
31  	public ClockLocationDao getClockLocationDao() {
32  		return clockLocationDao;
33  	}
34  
35  	public void setClockLocationDao(ClockLocationDao clockLocationDao) {
36  		this.clockLocationDao = clockLocationDao;
37  	}
38  
39  	public void processClockLocationRule(ClockLog clockLog, Date asOfDate){
40  		List<ClockLocationRule> lstClockLocationRules = getClockLocationRule(clockLog.getJob().getDept(),
41  										clockLog.getWorkArea(), clockLog.getPrincipalId(), clockLog.getJobNumber(), asOfDate);
42  		if(lstClockLocationRules.isEmpty()){
43  			return;
44  		}
45  		for(ClockLocationRule clockLocationRule : lstClockLocationRules){
46  			List<ClockLocationRuleIpAddress> ruleIpAddresses = clockLocationRule.getIpAddresses();
47  			String ipAddressClock = clockLog.getIpAddress();
48  			for(ClockLocationRuleIpAddress ruleIp : ruleIpAddresses) {
49  				if(compareIpAddresses(ruleIp.getIpAddress(), ipAddressClock)){
50  					return;
51  				}
52  			}
53  		}
54  		GlobalVariables.getMessageMap().putWarning("property", "ipaddress.invalid.format", clockLog.getIpAddress());
55  
56  	}
57  
58  	private boolean compareIpAddresses(String ipAddressRule, String ipAddress){
59  		String[] rulePieces = StringUtils.split(ipAddressRule, ".");
60          int ruleMax = rulePieces.length-1;
61  
62  		String[] ipAddPieces = StringUtils.split(ipAddress,".");
63  		boolean match = true;
64  		for(int i=0; i<ipAddPieces.length; i++){
65  			if( ((i > ruleMax) && StringUtils.equals("%", rulePieces[ruleMax])) ||
66                    ((i <= ruleMax) && ( StringUtils.equals(ipAddPieces[i], rulePieces[i]) || StringUtils.equals("%", rulePieces[i]) ))
67                  )
68              {
69  				// we don't need to do anything.
70  			} else {
71  			    return false;
72  			}
73  		}
74  		return match;
75  	}
76  
77  	@Override
78  	public List<ClockLocationRule> getClockLocationRule(String dept, Long workArea,String principalId, Long jobNumber, Date asOfDate) {
79  
80          // 1 : dept, wa, principal, job
81  		List<ClockLocationRule> clockLocationRule = clockLocationDao.getClockLocationRule(dept, workArea,principalId,jobNumber,asOfDate);
82  		if(!clockLocationRule.isEmpty()){
83  			return clockLocationRule;
84  		}
85  
86          // 2 : dept, wa, principal, -1
87  		clockLocationRule = clockLocationDao.getClockLocationRule(dept, workArea, principalId, -1L, asOfDate);
88  		if(!clockLocationRule.isEmpty()){
89  			return clockLocationRule;
90  		}
91  
92          // 3 : dept, wa, %        , job
93          clockLocationRule = clockLocationDao.getClockLocationRule(dept, workArea, "%", jobNumber, asOfDate);
94          if(!clockLocationRule.isEmpty()){
95              return clockLocationRule;
96          }
97  
98          // 4 : dept, -1, principal, job
99          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 }