001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.time.timecollection.rule.service;
017    
018    import java.sql.Timestamp;
019    import java.util.Map;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.kuali.hr.core.cache.CacheUtils;
023    import org.kuali.hr.time.collection.rule.TimeCollectionRule;
024    import org.kuali.hr.time.service.base.TkServiceLocator;
025    import org.kuali.hr.time.util.TKUtils;
026    import org.kuali.rice.kns.document.MaintenanceDocument;
027    import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
028    import org.kuali.rice.krad.service.KRADServiceLocator;
029    import org.kuali.rice.krad.util.GlobalVariables;
030    
031    public class TimeCollectionRuleMaintainableImpl extends KualiMaintainableImpl {
032        /**
033         *
034         */
035        private static final long serialVersionUID = 1L;
036    
037        @Override
038        public void processAfterNew(MaintenanceDocument document, Map<String, String[]> parameters) {
039            super.processAfterNew(document, parameters);
040        }
041    
042        @Override
043        public void processAfterPost(MaintenanceDocument document, Map<String, String[]> parameters) {
044            TimeCollectionRule timeCollectionRule = (TimeCollectionRule) document.getNewMaintainableObject().getBusinessObject();
045            timeCollectionRule.setUserPrincipalId(GlobalVariables.getUserSession().getPrincipalId());
046            super.processAfterPost(document, parameters);
047        }
048    
049        @Override
050        public void processAfterEdit(MaintenanceDocument document, Map<String, String[]> parameters) {
051            TimeCollectionRule timeCollectionRule = (TimeCollectionRule) document.getNewMaintainableObject().getBusinessObject();
052            timeCollectionRule.setUserPrincipalId(GlobalVariables.getUserSession().getPrincipalId());
053            super.processAfterEdit(document, parameters);
054        }
055    
056        @Override
057        public void saveBusinessObject() {
058            TimeCollectionRule timeCollectionRule = (TimeCollectionRule) this.getBusinessObject();
059    
060                    //Inactivate the old time collection rule as of the effective date of new time collection rule
061                    if(timeCollectionRule.getTkTimeCollectionRuleId()!=null && timeCollectionRule.isActive()){
062                            TimeCollectionRule oldTimeCollectRule = TkServiceLocator.getTimeCollectionRuleService().getTimeCollectionRule(timeCollectionRule.getTkTimeCollectionRuleId());
063                            if(timeCollectionRule.getEffectiveDate().equals(oldTimeCollectRule.getEffectiveDate())){
064                                    timeCollectionRule.setTimestamp(null);
065                            } else{
066                                    if(oldTimeCollectRule!=null){
067                                            oldTimeCollectRule.setActive(false);
068                                            //NOTE this is done to prevent the timestamp of the inactive one to be greater than the 
069                                            oldTimeCollectRule.setTimestamp(TKUtils.subtractOneSecondFromTimestamp(new Timestamp(System.currentTimeMillis())));
070                                            oldTimeCollectRule.setEffectiveDate(timeCollectionRule.getEffectiveDate());
071                                            KRADServiceLocator.getBusinessObjectService().save(oldTimeCollectRule);
072                                    }
073                                    timeCollectionRule.setTimestamp(new Timestamp(System.currentTimeMillis()));
074                                    timeCollectionRule.setTkTimeCollectionRuleId(null);
075                            }
076                    }
077                    
078                    KRADServiceLocator.getBusinessObjectService().save(timeCollectionRule);
079            CacheUtils.flushCache(TimeCollectionRule.CACHE_NAME);
080        }
081    
082    
083        @Override
084        public Map populateBusinessObject(Map<String, String> fieldValues, MaintenanceDocument maintenanceDocument, String methodToCall) {
085            if (fieldValues.containsKey("workArea") && StringUtils.equals(fieldValues.get("workArea"), "%")) {
086                fieldValues.put("workArea", "-1");
087            }
088            if (fieldValues.containsKey("jobNumber") && StringUtils.equals(fieldValues.get("jobNumber"), "%")) {
089                fieldValues.put("jobNumber", "-1");
090            }
091            return super.populateBusinessObject(fieldValues, maintenanceDocument, methodToCall);
092        }
093    }