View Javadoc

1   /**
2    * Copyright 2004-2013 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  					clockLog.setUnapprovedIP(false);
51  					return;
52  				}
53  			}
54  		}
55  		clockLog.setUnapprovedIP(true);
56  		GlobalVariables.getMessageMap().putWarning("property", "ipaddress.invalid.format", clockLog.getIpAddress());
57  
58  	}
59  
60  	private boolean compareIpAddresses(String ipAddressRule, String ipAddress){
61  		String[] rulePieces = StringUtils.split(ipAddressRule, ".");
62          int ruleMax = rulePieces.length-1;
63  
64  		String[] ipAddPieces = StringUtils.split(ipAddress,".");
65  		boolean match = true;
66  		for(int i=0; i<ipAddPieces.length; i++){
67  			if( ((i > ruleMax) && StringUtils.equals("%", rulePieces[ruleMax])) ||
68                    ((i <= ruleMax) && ( StringUtils.equals(ipAddPieces[i], rulePieces[i]) || StringUtils.equals("%", rulePieces[i]) ))
69                  )
70              {
71  				// we don't need to do anything.
72  			} else {
73  			    return false;
74  			}
75  		}
76  		return match;
77  	}
78  
79  	@Override
80  	public List<ClockLocationRule> getClockLocationRule(String dept, Long workArea,String principalId, Long jobNumber, Date asOfDate) {
81  
82          // 1 : dept, wa, principal, job
83  		List<ClockLocationRule> clockLocationRule = clockLocationDao.getClockLocationRule(dept, workArea,principalId,jobNumber,asOfDate);
84  		if(!clockLocationRule.isEmpty()){
85  			return clockLocationRule;
86  		}
87  
88          // 2 : dept, wa, principal, -1
89  		clockLocationRule = clockLocationDao.getClockLocationRule(dept, workArea, principalId, -1L, asOfDate);
90  		if(!clockLocationRule.isEmpty()){
91  			return clockLocationRule;
92  		}
93  
94          // 3 : dept, wa, %        , job
95          clockLocationRule = clockLocationDao.getClockLocationRule(dept, workArea, "%", jobNumber, asOfDate);
96          if(!clockLocationRule.isEmpty()){
97              return clockLocationRule;
98          }
99  
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 }