1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
83 List<ClockLocationRule> clockLocationRule = clockLocationDao.getClockLocationRule(dept, workArea,principalId,jobNumber,asOfDate);
84 if(!clockLocationRule.isEmpty()){
85 return clockLocationRule;
86 }
87
88
89 clockLocationRule = clockLocationDao.getClockLocationRule(dept, workArea, principalId, -1L, asOfDate);
90 if(!clockLocationRule.isEmpty()){
91 return clockLocationRule;
92 }
93
94
95 clockLocationRule = clockLocationDao.getClockLocationRule(dept, workArea, "%", jobNumber, asOfDate);
96 if(!clockLocationRule.isEmpty()){
97 return clockLocationRule;
98 }
99
100
101 clockLocationRule = clockLocationDao.getClockLocationRule(dept, -1L, principalId, jobNumber, asOfDate);
102 if(!clockLocationRule.isEmpty()){
103 return clockLocationRule;
104 }
105
106
107 clockLocationRule = clockLocationDao.getClockLocationRule(dept, workArea, "%", -1L, asOfDate);
108 if(!clockLocationRule.isEmpty()){
109 return clockLocationRule;
110 }
111
112
113 clockLocationRule = clockLocationDao.getClockLocationRule(dept, -1L, principalId, -1L, asOfDate);
114 if(!clockLocationRule.isEmpty()){
115 return clockLocationRule;
116 }
117
118
119 clockLocationRule = clockLocationDao.getClockLocationRule(dept, -1L, "%", jobNumber, asOfDate);
120 if(!clockLocationRule.isEmpty()){
121 return clockLocationRule;
122 }
123
124
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 }