View Javadoc

1   /**
2    * Copyright 2004-2012 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.timecollection.rule.service;
17  
18  import java.sql.Timestamp;
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.collection.rule.TimeCollectionRule;
24  import org.kuali.hr.time.service.base.TkServiceLocator;
25  import org.kuali.hr.time.util.TKUtils;
26  import org.kuali.rice.kns.document.MaintenanceDocument;
27  import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
28  import org.kuali.rice.krad.service.KRADServiceLocator;
29  import org.kuali.rice.krad.util.GlobalVariables;
30  
31  public class TimeCollectionRuleMaintainableImpl extends KualiMaintainableImpl {
32      /**
33       *
34       */
35      private static final long serialVersionUID = 1L;
36  
37      @Override
38      public void processAfterNew(MaintenanceDocument document, Map<String, String[]> parameters) {
39          super.processAfterNew(document, parameters);
40      }
41  
42      @Override
43      public void processAfterPost(MaintenanceDocument document, Map<String, String[]> parameters) {
44          TimeCollectionRule timeCollectionRule = (TimeCollectionRule) document.getNewMaintainableObject().getBusinessObject();
45          timeCollectionRule.setUserPrincipalId(GlobalVariables.getUserSession().getPrincipalId());
46          super.processAfterPost(document, parameters);
47      }
48  
49      @Override
50      public void processAfterEdit(MaintenanceDocument document, Map<String, String[]> parameters) {
51          TimeCollectionRule timeCollectionRule = (TimeCollectionRule) document.getNewMaintainableObject().getBusinessObject();
52          timeCollectionRule.setUserPrincipalId(GlobalVariables.getUserSession().getPrincipalId());
53          super.processAfterEdit(document, parameters);
54      }
55  
56      @Override
57      public void saveBusinessObject() {
58          TimeCollectionRule timeCollectionRule = (TimeCollectionRule) this.getBusinessObject();
59  
60  		//Inactivate the old time collection rule as of the effective date of new time collection rule
61  		if(timeCollectionRule.getTkTimeCollectionRuleId()!=null && timeCollectionRule.isActive()){
62  			TimeCollectionRule oldTimeCollectRule = TkServiceLocator.getTimeCollectionRuleService().getTimeCollectionRule(timeCollectionRule.getTkTimeCollectionRuleId());
63  			if(timeCollectionRule.getEffectiveDate().equals(oldTimeCollectRule.getEffectiveDate())){
64  				timeCollectionRule.setTimestamp(null);
65  			} else{
66  				if(oldTimeCollectRule!=null){
67  					oldTimeCollectRule.setActive(false);
68  					//NOTE this is done to prevent the timestamp of the inactive one to be greater than the 
69  					oldTimeCollectRule.setTimestamp(TKUtils.subtractOneSecondFromTimestamp(new Timestamp(System.currentTimeMillis())));
70  					oldTimeCollectRule.setEffectiveDate(timeCollectionRule.getEffectiveDate());
71  					KRADServiceLocator.getBusinessObjectService().save(oldTimeCollectRule);
72  				}
73  				timeCollectionRule.setTimestamp(new Timestamp(System.currentTimeMillis()));
74  				timeCollectionRule.setTkTimeCollectionRuleId(null);
75  			}
76  		}
77  		
78  		KRADServiceLocator.getBusinessObjectService().save(timeCollectionRule);
79          CacheUtils.flushCache(TimeCollectionRule.CACHE_NAME);
80      }
81  
82  
83      @Override
84      public Map populateBusinessObject(Map<String, String> fieldValues, MaintenanceDocument maintenanceDocument, String methodToCall) {
85          if (fieldValues.containsKey("workArea") && StringUtils.equals(fieldValues.get("workArea"), "%")) {
86              fieldValues.put("workArea", "-1");
87          }
88          if (fieldValues.containsKey("jobNumber") && StringUtils.equals(fieldValues.get("jobNumber"), "%")) {
89              fieldValues.put("jobNumber", "-1");
90          }
91          return super.populateBusinessObject(fieldValues, maintenanceDocument, methodToCall);
92      }
93  }