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.HrBusinessObject;
024    import org.kuali.hr.time.collection.rule.TimeCollectionRule;
025    import org.kuali.hr.time.service.base.TkServiceLocator;
026    import org.kuali.hr.time.util.HrBusinessObjectMaintainableImpl;
027    import org.kuali.hr.time.util.TKUtils;
028    import org.kuali.rice.kns.document.MaintenanceDocument;
029    import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
030    import org.kuali.rice.krad.service.KRADServiceLocator;
031    import org.kuali.rice.krad.util.GlobalVariables;
032    
033    public class TimeCollectionRuleMaintainableImpl extends HrBusinessObjectMaintainableImpl {
034        /**
035         *
036         */
037        private static final long serialVersionUID = 1L;
038    
039        @Override
040        public HrBusinessObject getObjectById(String id) {
041            return TkServiceLocator.getTimeCollectionRuleService().getTimeCollectionRule(id);
042        }
043    
044        @Override
045        public void processAfterNew(MaintenanceDocument document, Map<String, String[]> parameters) {
046            super.processAfterNew(document, parameters);
047        }
048    
049        @Override
050        public void processAfterPost(MaintenanceDocument document, Map<String, String[]> parameters) {
051            TimeCollectionRule timeCollectionRule = (TimeCollectionRule) document.getNewMaintainableObject().getBusinessObject();
052            timeCollectionRule.setUserPrincipalId(GlobalVariables.getUserSession().getPrincipalId());
053            super.processAfterPost(document, parameters);
054        }
055    
056        @Override
057        public void processAfterEdit(MaintenanceDocument document, Map<String, String[]> parameters) {
058            TimeCollectionRule timeCollectionRule = (TimeCollectionRule) document.getNewMaintainableObject().getBusinessObject();
059            timeCollectionRule.setUserPrincipalId(GlobalVariables.getUserSession().getPrincipalId());
060            super.processAfterEdit(document, parameters);
061        }
062    
063        /*@Override
064        public void saveBusinessObject() {
065            TimeCollectionRule timeCollectionRule = (TimeCollectionRule) this.getBusinessObject();
066    
067                    //Inactivate the old time collection rule as of the effective date of new time collection rule
068                    if(timeCollectionRule.getTkTimeCollectionRuleId()!=null && timeCollectionRule.isActive()){
069                            TimeCollectionRule oldTimeCollectRule = TkServiceLocator.getTimeCollectionRuleService().getTimeCollectionRule(timeCollectionRule.getTkTimeCollectionRuleId());
070                            if(timeCollectionRule.getEffectiveDate().equals(oldTimeCollectRule.getEffectiveDate())){
071                                    timeCollectionRule.setTimestamp(null);
072                            } else{
073                                    if(oldTimeCollectRule!=null){
074                                            oldTimeCollectRule.setActive(false);
075                                            //NOTE this is done to prevent the timestamp of the inactive one to be greater than the 
076                                            oldTimeCollectRule.setTimestamp(TKUtils.subtractOneSecondFromTimestamp(new Timestamp(System.currentTimeMillis())));
077                                            oldTimeCollectRule.setEffectiveDate(timeCollectionRule.getEffectiveDate());
078                                            KRADServiceLocator.getBusinessObjectService().save(oldTimeCollectRule);
079                                    }
080                                    timeCollectionRule.setTimestamp(new Timestamp(System.currentTimeMillis()));
081                                    timeCollectionRule.setTkTimeCollectionRuleId(null);
082                            }
083                    }
084                    
085                    KRADServiceLocator.getBusinessObjectService().save(timeCollectionRule);
086            CacheUtils.flushCache(TimeCollectionRule.CACHE_NAME);
087        }*/
088    
089    
090        @Override
091        public Map populateBusinessObject(Map<String, String> fieldValues, MaintenanceDocument maintenanceDocument, String methodToCall) {
092            if (fieldValues.containsKey("workArea") && StringUtils.equals(fieldValues.get("workArea"), "%")) {
093                fieldValues.put("workArea", "-1");
094            }
095            if (fieldValues.containsKey("jobNumber") && StringUtils.equals(fieldValues.get("jobNumber"), "%")) {
096                fieldValues.put("jobNumber", "-1");
097            }
098            return super.populateBusinessObject(fieldValues, maintenanceDocument, methodToCall);
099        }
100    }