001package org.kuali.ole.sys.context;
002
003
004import java.io.File;
005import java.util.Date;
006
007import org.apache.commons.lang.StringUtils;
008
009/**
010 * BatchStepFileDescriptor contains the properties of a batch step file (name of the job, name of the step, and the file type extension). 
011 * The index of the step, the start time/date, and the completion time/date of the step can also be stored.
012 *
013 */
014public class BatchStepFileDescriptor {
015
016        private static final String STEP_FILE_SUFFIX_RUN = "run";
017        private static final String STEP_FILE_SUFFIX_RESULT_SUCCESS = "success";
018        private static final String STEP_FILE_SUFFIX_RESULT_ERROR = "error";
019        
020        public static final String STEP_FILE_EXTENSION_SEPARATOR = ".";
021        public static final String STEP_FILE_NAME_SEPARATOR = "~";
022    
023        private String jobName;
024        private String stepName;
025        private String extension;
026        
027        private File stepFile;
028        
029        private Integer stepIndex;
030        private Date startedDate;
031        private Date completedDate;
032        
033        /** 
034         * Create a descriptor using the properties provided. getStepFile() will return null when this constructor is used.  
035         * This constructor is used to provide the details about a semaphore file to look for in the directory. 
036         * 
037         * @param jobName the name of the job in which the step is running
038         * @param stepName the name of the step to be executed
039         * @param extension the type of file to work with (RUN, SUCCESS, or ERROR)
040         */
041        public BatchStepFileDescriptor(String jobName, String stepName, String extension) {
042                this.jobName = jobName;
043                this.stepName = stepName;
044                this.extension = extension;
045        }
046        
047        /** 
048         * @param stepFile the semaphore file in the directory. The jobName, stepName, and extension are retrieved from the semaphore file name.
049         */
050        public BatchStepFileDescriptor(File stepFile) {
051                this.stepFile = stepFile;
052                
053                this.jobName = getJobNameFromFile(stepFile);            
054                this.stepName = getStepNameFromFile(stepFile);                                          
055                this.extension = getExtensionFromFile(stepFile);
056        }
057        
058        /**
059         * @return the name of the job in which the step is running
060         */
061        public String getJobName() {
062                return jobName;
063        }
064        
065        /**
066         * @return the name of the step being executed
067         */
068        public String getStepName() {
069                return stepName;
070        }
071        
072        /**
073         * @return the extension of the semaphore file 
074         */
075        public String getExtension() {
076                return extension;
077        }
078        
079        /**
080         * @return the semaphore file in the directory
081         */
082        public File getStepFile() {
083                return stepFile;
084        }                       
085        
086        /**
087         * @return the name of the file with the extension (jobName~stepName.extension)
088         */
089        public String getName() {
090                return jobName + STEP_FILE_NAME_SEPARATOR + stepName + STEP_FILE_EXTENSION_SEPARATOR + extension;
091        }
092        
093        /**
094         * @return the name of the file without an extension (jobName~stepName)
095         */
096        public String getNameNoExtension() {
097                return jobName + STEP_FILE_NAME_SEPARATOR + stepName;
098        }
099    
100        /**
101         * Return a representation of the step file descriptor. STEP and the stepIndex are only printed if the stepIndex is not null.
102         * 
103         * @return [jobName] STEP[stepIndex]-[stepName]  or [jobName] [stepName]
104         */
105    public String toString() {
106        return getJobName() +" "+ (stepIndex != null ? "STEP"+ stepIndex +"-":"") + getStepName();
107    }
108    
109    /**
110     * @return true if the semaphore file is an ERROR file, false otherwise
111     */
112    public boolean isStepFileAnErrorResultFile() {
113        return getName().endsWith(BatchStepFileDescriptor.getFileExtensionError());
114    }
115    
116    /**
117     * @return the index of the step in its job
118     */
119    public Integer getStepIndex() {
120        return stepIndex;
121    }
122    
123    /**
124     * @param stepIndex the index of the step in its job
125     */
126    public void setStepIndex(Integer stepIndex) {
127        this.stepIndex = stepIndex;
128    }
129    
130    /**
131     * @return the time the step started
132     */
133    public Date getStartedDate() {
134        return startedDate;
135    }
136    
137    /**
138     * @param startedDate the time the step started
139     */
140    public void setStartedDate(Date startedDate) {
141        this.startedDate = startedDate;
142    }
143    
144    /**
145     * @return the time the step completed
146     */
147    public Date getCompletedDate() {
148        return completedDate;
149    }
150    
151    /**
152     * @param completedDate the time the step completed
153     */
154    public void setCompletedDate(Date completedDate) {
155        this.completedDate = completedDate;
156    }
157    
158    /**
159     * Retrieves the name of the job from the File
160     * 
161     * @param runFile the semaphore file
162     * @return the job name
163     */
164    private String getJobNameFromFile(File runFile) {
165        String runFileName = runFile.getName();
166                int indexOfExtension = runFileName.lastIndexOf(STEP_FILE_EXTENSION_SEPARATOR);
167                String fileNameNoExtension = StringUtils.substring(runFileName, 0, indexOfExtension);
168                int indexOfStep = fileNameNoExtension.lastIndexOf(STEP_FILE_NAME_SEPARATOR);
169                String jobName = StringUtils.substring(fileNameNoExtension, 0, indexOfStep);
170                return jobName;
171    }
172    
173    /**
174     * Retrieves the name of the step from the File
175     * 
176     * @param runFile the semaphore file
177     * @return the step name
178     */
179    private String getStepNameFromFile(File runFile) {
180        String runFileName = runFile.getName();
181                int indexOfExtension = runFileName.lastIndexOf(STEP_FILE_EXTENSION_SEPARATOR);
182                String fileNameNoExtension = StringUtils.substring(runFileName, 0, indexOfExtension);
183                int indexOfStep = fileNameNoExtension.lastIndexOf(STEP_FILE_NAME_SEPARATOR);
184                String stepName = StringUtils.substring(fileNameNoExtension, indexOfStep+1);
185                return stepName;
186    }            
187    
188    /**
189     * Retrieves the extension from the File
190     * 
191     * @param runFile the semaphore file
192     * @return the extension
193     */
194    private String getExtensionFromFile(File runFile) {
195        String runFileName = runFile.getName();
196                int indexOfExtension = runFileName.lastIndexOf(STEP_FILE_EXTENSION_SEPARATOR);
197                String extension = StringUtils.substring(runFileName, indexOfExtension+1);
198                return extension;
199    }            
200        
201    /**
202     * @return the extension for the RUN file
203     */
204    public static String getFileExtensionRun() {
205        return BatchStepFileDescriptor.STEP_FILE_SUFFIX_RUN;
206    }
207    
208    /**
209     * @return the extension for the SUCCESS file
210     */
211    public static String getFileExtensionSuccess() {
212        return BatchStepFileDescriptor.STEP_FILE_SUFFIX_RESULT_SUCCESS;
213    }
214    
215    /**
216     * @return the extension for the ERROR file
217     */
218    public static String getFileExtensionError() {
219        return BatchStepFileDescriptor.STEP_FILE_SUFFIX_RESULT_ERROR;
220    }    
221}