001    /*
002     * Copyright 2011 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.kfs.module.bc.batch;
017    
018    import java.util.Date;
019    import java.util.HashMap;
020    import java.util.List;
021    import java.util.Map;
022    
023    import org.kuali.kfs.module.bc.batch.service.GenesisService;
024    import org.kuali.kfs.sys.batch.AbstractStep;
025    import org.kuali.kfs.sys.batch.Job;
026    import org.kuali.rice.kns.bo.Parameter;
027    import org.kuali.rice.kns.service.BusinessObjectService;
028    import org.kuali.rice.kns.service.PersistenceStructureService;
029    
030    import org.kuali.kfs.module.bc.util.BudgetParameterFinder;
031    
032    public class GenesisBatchStep extends AbstractStep {
033    
034        private GenesisService genesisService;
035        private PersistenceStructureService psService;
036        private BusinessObjectService boService;
037        
038        // parameter constants and logging
039        private static final String RUN_INDICATOR_PARAMETER_NAMESPACE_CODE = "KFS-BC";
040        private static final String RUN_INDICATOR_PARAMETER_NAMESPACE_STEP = "GenesisBatchStep";
041        private static final String RUN_INDICATOR_PARAMETER_VALUE = "N";
042        private static final String RUN_INDICATOR_PARAMETER_ALLOWED = "A";
043        private static final String RUN_INDICATOR_PARAMETER_DESCRIPTION = "Tells the job framework whether to run this job or not; set to know because the GenesisBatchJob needs to only be run once after database initialization.";
044        private static final String RUN_INDICATOR_PARAMETER_TYPE = "CONFG";
045        private static final String RUN_INDICATOR_PARAMETER_APPLICATION_NAMESPACE_CODE = "KFS";
046        
047        
048    
049        public boolean execute(String jobName, Date jobRunDate) {
050            genesisService.genesisStep(BudgetParameterFinder.getBaseFiscalYear());
051            setInitiatedParameter();
052            return true;
053        }
054        
055        /**
056         * This method sets a parameter that tells the step that it has already run and it does not need to run again.
057         */
058        private void setInitiatedParameter() {
059            // first see if we can find an existing Parameter object with this key
060            Parameter runIndicatorParameter = (Parameter) boService.findByPrimaryKey(Parameter.class, this.buildSearchKeyMap());
061            if (runIndicatorParameter == null)
062            {
063               runIndicatorParameter = new Parameter();
064               runIndicatorParameter.setVersionNumber(new Long(1));
065               runIndicatorParameter.setParameterNamespaceCode(GenesisBatchStep.RUN_INDICATOR_PARAMETER_NAMESPACE_CODE);
066               runIndicatorParameter.setParameterDetailTypeCode(GenesisBatchStep.RUN_INDICATOR_PARAMETER_NAMESPACE_STEP);
067               runIndicatorParameter.setParameterName(Job.STEP_RUN_PARM_NM);
068               runIndicatorParameter.setParameterDescription(GenesisBatchStep.RUN_INDICATOR_PARAMETER_DESCRIPTION);
069               runIndicatorParameter.setParameterConstraintCode(GenesisBatchStep.RUN_INDICATOR_PARAMETER_ALLOWED);
070               runIndicatorParameter.setParameterTypeCode(GenesisBatchStep.RUN_INDICATOR_PARAMETER_TYPE);
071               runIndicatorParameter.setParameterApplicationNamespaceCode(RUN_INDICATOR_PARAMETER_APPLICATION_NAMESPACE_CODE);
072            }
073            runIndicatorParameter.setParameterValue(GenesisBatchStep.RUN_INDICATOR_PARAMETER_VALUE);
074            boService.save(runIndicatorParameter);
075        }
076        
077        private Map<String,Object> buildSearchKeyMap()
078        {
079           Map<String,Object> pkMapForParameter = new HashMap<String,Object>();
080    
081           // set up a list of all the  field names and values of the fields in the Parameter object.
082           // the OJB names are nowhere in Kuali properties, apparently.
083           // but, since we use set routines above, we know what the names must be.  if they change at some point, we will have to change the set routines anyway.
084           // we can change the code here also when we do that.
085           Map<String,Object> fieldNamesValuesForParameter = new HashMap<String,Object>();
086           fieldNamesValuesForParameter.put("parameterNamespaceCode",GenesisBatchStep.RUN_INDICATOR_PARAMETER_NAMESPACE_CODE);
087           fieldNamesValuesForParameter.put("parameterDetailTypeCode",GenesisBatchStep.RUN_INDICATOR_PARAMETER_NAMESPACE_STEP);
088           fieldNamesValuesForParameter.put("parameterName",Job.STEP_RUN_PARM_NM);
089           fieldNamesValuesForParameter.put("parameterConstraintCode",GenesisBatchStep.RUN_INDICATOR_PARAMETER_ALLOWED);
090           fieldNamesValuesForParameter.put("parameterTypeCode",GenesisBatchStep.RUN_INDICATOR_PARAMETER_TYPE);
091           fieldNamesValuesForParameter.put("parameterApplicationNamespaceCode",GenesisBatchStep.RUN_INDICATOR_PARAMETER_APPLICATION_NAMESPACE_CODE);
092           
093           // get the primary keys and assign them to values
094           List<String> parameterPKFields = psService.getPrimaryKeys(Parameter.class);
095           for (String pkFieldName: parameterPKFields)
096           {
097               pkMapForParameter.put(pkFieldName,fieldNamesValuesForParameter.get(pkFieldName));
098           }
099           return (pkMapForParameter);
100        }
101        
102        public void setGenesisService (GenesisService genesisService)
103        {
104            this.genesisService = genesisService;
105        }
106        
107        public void setPersistenceStructureService (PersistenceStructureService psService)
108        {
109            this.psService = psService;
110        }
111        
112        public void setBusinessObjectService (BusinessObjectService boService)
113        {
114            this.boService = boService;
115        }
116    }