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  
11  
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  
35  
36  
37  
38  
39  
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  
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  
60  
61  	public String getJobName() {
62  		return jobName;
63  	}
64  	
65  	
66  
67  
68  	public String getStepName() {
69  		return stepName;
70  	}
71  	
72  	
73  
74  
75  	public String getExtension() {
76  		return extension;
77  	}
78  	
79  	
80  
81  
82  	public File getStepFile() {
83  		return stepFile;
84  	}			
85  	
86  	
87  
88  
89  	public String getName() {
90  		return jobName + STEP_FILE_NAME_SEPARATOR + stepName + STEP_FILE_EXTENSION_SEPARATOR + extension;
91  	}
92  	
93  	
94  
95  
96  	public String getNameNoExtension() {
97  		return jobName + STEP_FILE_NAME_SEPARATOR + stepName;
98  	}
99      
100 	
101 
102 
103 
104 
105     public String toString() {
106         return getJobName() +" "+ (stepIndex != null ? "STEP"+ stepIndex +"-":"") + getStepName();
107     }
108     
109     
110 
111 
112     public boolean isStepFileAnErrorResultFile() {
113     	return getName().endsWith(BatchStepFileDescriptor.getFileExtensionError());
114     }
115     
116     
117 
118 
119     public Integer getStepIndex() {
120         return stepIndex;
121     }
122     
123     
124 
125 
126     public void setStepIndex(Integer stepIndex) {
127         this.stepIndex = stepIndex;
128     }
129     
130     
131 
132 
133     public Date getStartedDate() {
134         return startedDate;
135     }
136     
137     
138 
139 
140     public void setStartedDate(Date startedDate) {
141         this.startedDate = startedDate;
142     }
143     
144     
145 
146 
147     public Date getCompletedDate() {
148         return completedDate;
149     }
150     
151     
152 
153 
154     public void setCompletedDate(Date completedDate) {
155         this.completedDate = completedDate;
156     }
157     
158     
159 
160 
161 
162 
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 
175 
176 
177 
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 
190 
191 
192 
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 
203 
204     public static String getFileExtensionRun() {
205     	return BatchStepFileDescriptor.STEP_FILE_SUFFIX_RUN;
206     }
207     
208     
209 
210 
211     public static String getFileExtensionSuccess() {
212     	return BatchStepFileDescriptor.STEP_FILE_SUFFIX_RESULT_SUCCESS;
213     }
214     
215     
216 
217 
218     public static String getFileExtensionError() {
219     	return BatchStepFileDescriptor.STEP_FILE_SUFFIX_RESULT_ERROR;
220     }    
221 }