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.kpme.tklm.time.rules.clocklocation.web;
17  
18  import java.util.List;
19  import java.util.Map;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.kpme.core.bo.HrBusinessObject;
23  import org.kuali.kpme.core.bo.HrBusinessObjectMaintainableImpl;
24  import org.kuali.kpme.core.cache.CacheUtils;
25  import org.kuali.kpme.tklm.time.rules.clocklocation.ClockLocationRule;
26  import org.kuali.kpme.tklm.time.rules.clocklocation.ClockLocationRuleIpAddress;
27  import org.kuali.kpme.tklm.time.rules.clocklocation.validation.ClockLocationRuleRule;
28  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
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   * Used to modify behavior of Clock Location Maintenance page
37   * 
38   */
39  public class ClockLocationMaintainableImpl extends HrBusinessObjectMaintainableImpl {
40  
41  	/**
42       * 
43       */
44  	private static final long serialVersionUID = 1L;
45  
46  	/**
47  	 * Used to swap out wildcard values for numeric types
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.getEffectiveLocalDate());
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  		// need to validate each new ipAddress
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 }