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 java.util.List;
19  import java.util.Map;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.hr.core.cache.CacheUtils;
23  import org.kuali.hr.time.HrBusinessObject;
24  import org.kuali.hr.time.clock.location.ClockLocationRule;
25  import org.kuali.hr.time.clock.location.ClockLocationRuleIpAddress;
26  import org.kuali.hr.time.clock.location.validation.ClockLocationRuleRule;
27  import org.kuali.hr.time.service.base.TkServiceLocator;
28  import org.kuali.hr.time.util.HrBusinessObjectMaintainableImpl;
29  import org.kuali.rice.kns.document.MaintenanceDocument;
30  import org.kuali.rice.krad.service.KRADServiceLocator;
31  import org.kuali.rice.krad.util.GlobalVariables;
32  import org.kuali.rice.krad.util.KRADConstants;
33  
34  
35  
36  
37  
38  
39  public class ClockLocationMaintainableImpl extends HrBusinessObjectMaintainableImpl {
40  
41  	
42  
43  
44  	private static final long serialVersionUID = 1L;
45  
46  	
47  
48  
49  	@Override
50  	public Map populateBusinessObject(Map<String, String> fieldValues,
51  			MaintenanceDocument maintenanceDocument, String methodToCall) {
52  		if (fieldValues.containsKey("workArea")
53  				&& StringUtils.equals(fieldValues.get("workArea"), "%")) {
54  			fieldValues.put("workArea", "-1");
55  		}
56  		if (fieldValues.containsKey("jobNumber")
57  				&& StringUtils.equals(fieldValues.get("jobNumber"), "%")) {
58  			fieldValues.put("jobNumber", "-1");
59  		}
60  		return super.populateBusinessObject(fieldValues, maintenanceDocument,
61  				methodToCall);
62  	}
63  
64  	@Override
65  	public void processAfterEdit(MaintenanceDocument document,
66  			Map<String, String[]> parameters) {
67  		ClockLocationRule clockLocationRule = (ClockLocationRule) this
68  				.getBusinessObject();
69  		List<ClockLocationRule> clockLocationRuleList = TkServiceLocator
70  				.getClockLocationRuleService()
71  				.getNewerVersionClockLocationRule(clockLocationRule.getDept(),
72  						clockLocationRule.getWorkArea(),
73  						clockLocationRule.getPrincipalId(),
74  						clockLocationRule.getJobNumber(),
75  						clockLocationRule.getEffectiveDate());
76  		if (clockLocationRuleList.size() > 0) {
77  			GlobalVariables.getMessageMap().putWarningForSectionId(
78  					"Clock Location Rule Maintenance",
79  					"clocklocationrule.newer.exists", null);
80  		}
81  		super.processAfterEdit(document, parameters);
82  	}
83  
84  	@Override
85  	public HrBusinessObject getObjectById(String id) {
86  		return TkServiceLocator.getClockLocationRuleService().getClockLocationRule(id);
87  	}
88  	
89  	@Override
90      public void addNewLineToCollection( String collectionName ) {
91  		
92          if (collectionName.equals("ipAddresses")) {
93          	ClockLocationRuleIpAddress anIP = (ClockLocationRuleIpAddress)newCollectionLines.get(collectionName );
94      		if(!ClockLocationRuleRule.validateIpAddress(anIP.getIpAddress())) {
95      			GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.MAINTENANCE_NEW_MAINTAINABLE +"ipAddresses", 
96          				"ipaddress.invalid.format",anIP.getIpAddress());
97          		return;
98      		}
99          }
100 		super.addNewLineToCollection(collectionName);
101     }
102 		
103 	@Override
104 	public void saveBusinessObject() {
105 		ClockLocationRule clr = (ClockLocationRule) this.getBusinessObject();
106 		List<ClockLocationRuleIpAddress> ips = clr.getIpAddresses();
107 		super.saveBusinessObject();
108 		if(!ips.isEmpty()) {
109 			for(ClockLocationRuleIpAddress ipAddress : ips) {
110 				ipAddress.setTkClockLocationRuleId(clr.getTkClockLocationRuleId());
111 				KRADServiceLocator.getBusinessObjectService().save(ipAddress);
112 			}
113             CacheUtils.flushCache(ClockLocationRule.CACHE_NAME);
114 		}
115 		
116 	}
117 }