View Javadoc
1   package org.kuali.ole.sys.context;
2   
3   
4   import java.io.File;
5   import java.util.Date;
6   
7   import org.apache.commons.lang.StringUtils;
8   
9   /**
10   * BatchStepFileDescriptor contains the properties of a batch step file (name of the job, name of the step, and the file type extension). 
11   * The index of the step, the start time/date, and the completion time/date of the step can also be stored.
12   *
13   */
14  public class BatchStepFileDescriptor {
15  
16  	private static final String STEP_FILE_SUFFIX_RUN = "run";
17  	private static final String STEP_FILE_SUFFIX_RESULT_SUCCESS = "success";
18  	private static final String STEP_FILE_SUFFIX_RESULT_ERROR = "error";
19  	
20  	public static final String STEP_FILE_EXTENSION_SEPARATOR = ".";
21  	public static final String STEP_FILE_NAME_SEPARATOR = "~";
22      
23  	private String jobName;
24  	private String stepName;
25  	private String extension;
26  	
27  	private File stepFile;
28  	
29  	private Integer stepIndex;
30  	private Date startedDate;
31  	private Date completedDate;
32  	
33  	/** 
34  	 * Create a descriptor using the properties provided. getStepFile() will return null when this constructor is used.  
35  	 * This constructor is used to provide the details about a semaphore file to look for in the directory. 
36  	 * 
37  	 * @param jobName the name of the job in which the step is running
38  	 * @param stepName the name of the step to be executed
39  	 * @param extension the type of file to work with (RUN, SUCCESS, or ERROR)
40  	 */
41  	public BatchStepFileDescriptor(String jobName, String stepName, String extension) {
42  		this.jobName = jobName;
43  		this.stepName = stepName;
44  		this.extension = extension;
45  	}
46  	
47  	/** 
48  	 * @param stepFile the semaphore file in the directory. The jobName, stepName, and extension are retrieved from the semaphore file name.
49  	 */
50  	public BatchStepFileDescriptor(File stepFile) {
51  		this.stepFile = stepFile;
52  		
53  		this.jobName = getJobNameFromFile(stepFile);		
54  		this.stepName = getStepNameFromFile(stepFile);						
55  		this.extension = getExtensionFromFile(stepFile);
56  	}
57  	
58  	/**
59  	 * @return the name of the job in which the step is running
60  	 */
61  	public String getJobName() {
62  		return jobName;
63  	}
64  	
65  	/**
66  	 * @return the name of the step being executed
67  	 */
68  	public String getStepName() {
69  		return stepName;
70  	}
71  	
72  	/**
73  	 * @return the extension of the semaphore file 
74  	 */
75  	public String getExtension() {
76  		return extension;
77  	}
78  	
79  	/**
80  	 * @return the semaphore file in the directory
81  	 */
82  	public File getStepFile() {
83  		return stepFile;
84  	}			
85  	
86  	/**
87  	 * @return the name of the file with the extension (jobName~stepName.extension)
88  	 */
89  	public String getName() {
90  		return jobName + STEP_FILE_NAME_SEPARATOR + stepName + STEP_FILE_EXTENSION_SEPARATOR + extension;
91  	}
92  	
93  	/**
94  	 * @return the name of the file without an extension (jobName~stepName)
95  	 */
96  	public String getNameNoExtension() {
97  		return jobName + STEP_FILE_NAME_SEPARATOR + stepName;
98  	}
99      
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 }