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