Produced by Araxis Merge on 2/7/2017 12:14:07 PM Eastern Standard Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.
# | Location | File | Last Modified |
---|---|---|---|
1 | cpss.zip\cpss\src\main\java\gov\va\cpss\job\sendcbs | SendCBSProcessFileCompletionTasklet.java | Wed Feb 1 21:07:30 2017 UTC |
2 | cpss.zip\cpss\src\main\java\gov\va\cpss\job\sendcbs | SendCBSProcessFileCompletionTasklet.java | Fri Feb 3 20:49:51 2017 UTC |
Description | Between Files 1 and 2 |
|
---|---|---|
Text Blocks | Lines | |
Unchanged | 2 | 806 |
Changed | 1 | 2 |
Inserted | 0 | 0 |
Removed | 0 | 0 |
Whitespace | |
---|---|
Character case | Differences in character case are significant |
Line endings | Differences in line endings (CR and LF characters) are ignored |
CR/LF characters | Not shown in the comparison detail |
No regular expressions were active.
1 | package go v.va.cpss. job.sendcb s; | |
2 | ||
3 | import sta tic gov.va .cpss.job. CbssJobPro cessingCon stants.DAT A_ERROR_ST ATUS; | |
4 | import sta tic gov.va .cpss.job. CbssJobPro cessingCon stants.JOB _FAILURE_K EY; | |
5 | import sta tic gov.va .cpss.job. CbssJobPro cessingCon stants.WRI TE_FAILURE _STATUS; | |
6 | import sta tic gov.va .cpss.job. sendcbs.Se ndCBSProce ssingConst ants.CBS_M ESSAGE_ID_ KEY; | |
7 | import sta tic gov.va .cpss.job. sendcbs.Se ndCBSProce ssingConst ants.CBS_O UTPUT_FILE _NAME_KEY; | |
8 | import sta tic gov.va .cpss.job. sendcbs.Se ndCBSProce ssingConst ants.CBS_O UTPUT_RESO URCE_KEY; | |
9 | import sta tic gov.va .cpss.job. sendcbs.Se ndCBSProce ssingConst ants.CBS_T OTAL_PATIE NTS_INCLUD ED_KEY; | |
10 | import sta tic gov.va .cpss.job. sendcbs.Se ndCBSProce ssingConst ants.SBS_O UTPUT_FILE _NAME_KEY; | |
11 | import sta tic gov.va .cpss.job. sendcbs.Se ndCBSProce ssingConst ants.SBS_O UTPUT_RESO URCE_KEY; | |
12 | ||
13 | import org .springfra mework.bat ch.core.Jo bExecution ; | |
14 | import org .springfra mework.bat ch.core.sc ope.contex t.ChunkCon text; | |
15 | ||
16 | import gov .va.cpss.j ob.CbssJob BaseTransa ctionTaskl et; | |
17 | import gov .va.cpss.m odel.Proce ssStatus; | |
18 | import gov .va.cpss.s ervice.Sen dCBSServic e; | |
19 | import gov .va.cpss.s ervice.Sft pService; | |
20 | import gov .va.cpss.t ime.TimeUt ils; | |
21 | ||
22 | /** | |
23 | * Complet e processi ng by tran sferring f ile to sft p server w ith tempor ary name, | |
24 | * setting appropria te message status an d statemen t status, and renami ng | |
25 | * tempora ry file on sftp serv er with co rrect name . If any e rrors occu r the | |
26 | * databas e updates are rolled back and the batch job status is set to error | |
27 | * conditi on. | |
28 | * | |
29 | * @author D N S | |
30 | */ | |
31 | public cla ss SendCBS ProcessFil eCompletio nTasklet e xtends Cbs sJobBaseTr ansactionT asklet { | |
32 | ||
33 | pr ivate fina l String T IMESTAMP_F ORMAT = "H H_mm_ss"; | |
34 | ||
35 | pr ivate Stri ng tempFil enamePostf ix; | |
36 | ||
37 | pr ivate Stri ng serverT argetDirec tory; | |
38 | ||
39 | pr ivate Sftp Service sf tpService; | |
40 | ||
41 | pr ivate Send CBSService sendCBSSe rvice; | |
42 | ||
43 | pu blic Strin g getTempF ilenamePos tfix() { | |
44 | retu rn tempFil enamePostf ix; | |
45 | } | |
46 | ||
47 | pu blic void setTempFil enamePostf ix(String tempFilena mePostfix) { | |
48 | this .tempFilen amePostfix = tempFil enamePostf ix; | |
49 | } | |
50 | ||
51 | pu blic Strin g getServe rTargetDir ectory() { | |
52 | retu rn serverT argetDirec tory; | |
53 | } | |
54 | ||
55 | pu blic void setServerT argetDirec tory(Strin g serverTa rgetDirect ory) { | |
56 | this .serverTar getDirecto ry = serve rTargetDir ectory; | |
57 | } | |
58 | ||
59 | pu blic SftpS ervice get SftpServic e() { | |
60 | retu rn sftpSer vice; | |
61 | } | |
62 | ||
63 | pu blic void setSftpSer vice(SftpS ervice sft pService) { | |
64 | this .sftpServi ce = sftpS ervice; | |
65 | } | |
66 | ||
67 | pu blic SendC BSService getSendCBS Service() { | |
68 | retu rn sendCBS Service; | |
69 | } | |
70 | ||
71 | pu blic void setSendCBS Service(Se ndCBSServi ce sendCBS Service) { | |
72 | this .sendCBSSe rvice = se ndCBSServi ce; | |
73 | } | |
74 | ||
75 | @O verride | |
76 | pr otected bo olean exec uteLogic(C hunkContex t chunkCon text) { | |
77 | ||
78 | bool ean succes sful = tru e; | |
79 | ||
80 | task letLogger. info("File Completio n Tasklet Running!") ; | |
81 | ||
82 | // S ave the jo bExecution for refer ence. | |
83 | JobE xecution j obExecutio n = chunkC ontext.get StepContex t().getSte pExecution ().getJobE xecution() ; | |
84 | ||
85 | // O btain the temporary local file paths as they will be needed regardless of failur e. | |
86 | // C onsolidate d statemen ts file lo cal path | |
87 | fina l String c bsOutputRe sourceLoca lTempFileP ath = getO utputResou rce(jobExe cution, CB S_OUTPUT_R ESOURCE_KE Y); | |
88 | if ( (cbsOutput ResourceLo calTempFil ePath == n ull) || cb sOutputRes ourceLocal TempFilePa th.isEmpty ()) { | |
89 | // Ret urn now an d roll bac k! | |
90 | return false; | |
91 | } | |
92 | ||
93 | // S ingle stat ements fil e local pa th | |
94 | fina l String s bsOutputRe sourceLoca lTempFileP ath = getO utputResou rce(jobExe cution, SB S_OUTPUT_R ESOURCE_KE Y); | |
95 | if ( (sbsOutput ResourceLo calTempFil ePath == n ull) || sb sOutputRes ourceLocal TempFilePa th.isEmpty ()) { | |
96 | // Ret urn now an d roll bac k! | |
97 | return false; | |
98 | } | |
99 | ||
100 | // I f the job has been s uccessful then trans fer files to server and | |
101 | // a ttempt to set approp riate fina l statemen t and mess age status es. | |
102 | if ( !jobExecut ion.getExe cutionCont ext().cont ainsKey(JO B_FAILURE_ KEY)) { | |
103 | ||
104 | // Get filename values fro m the jobE xecution o r jobParam eters. | |
105 | ||
106 | // The consolida ted statem ents outpu t filename . | |
107 | final String cbs OutputFile Name = get JobParamet er(jobExec ution, CBS _OUTPUT_FI LE_NAME_KE Y); | |
108 | if ((c bsOutputFi leName == null) || c bsOutputFi leName.isE mpty()) { | |
109 | // Flag to do roll back. | |
110 | successf ul = false ; | |
111 | } | |
112 | // The temporary consolida ted statem ents filen ame used t o transfer to the sf tp server. | |
113 | final String cbs TargetTemp oraryFileN ame = getT emporaryFi lename(cbs OutputFile Name); | |
114 | if (su ccessful) { | |
115 | if (cbsT argetTempo raryFileNa me != null ) { | |
116 | taskletLog ger.info(" cbsTargetT emporaryFi leName: " + cbsTarge tTemporary FileName); | |
117 | } else { | |
118 | ||
119 | // This is an unexpe cted and u nrecoverab le error. | |
120 | final Stri ng error = "Rollback Triggered - Could n ot create a temporar y consolid ated state ments serv er filenam e"; | |
121 | setFailure Status(job Execution, DATA_ERRO R_STATUS, error); | |
122 | ||
123 | // Flag to do rollba ck. | |
124 | successful = false; | |
125 | } | |
126 | } | |
127 | ||
128 | // The single st atements o utput file name. | |
129 | final String sbs OutputFile Name = get JobParamet er(jobExec ution, SBS _OUTPUT_FI LE_NAME_KE Y); | |
130 | if ((s bsOutputFi leName == null) || s bsOutputFi leName.isE mpty()) { | |
131 | // Flag to do roll back. | |
132 | successf ul = false ; | |
133 | } | |
134 | // The temporary single st atements f ilename us ed to tran sfer to th e sftp ser ver. | |
135 | final String sbs TargetTemp oraryFileN ame = getT emporaryFi lename(sbs OutputFile Name); | |
136 | if (su ccessful) { | |
137 | if (sbsT argetTempo raryFileNa me != null ) { | |
138 | taskletLog ger.info(" sbsTargetT emporaryFi leName: " + sbsTarge tTemporary FileName); | |
139 | } else { | |
140 | ||
141 | // This is an unexpe cted and u nrecoverab le error. | |
142 | final Stri ng error = "Rollback Triggered - Could n ot create a temporar y single s tatements server fil ename"; | |
143 | setFailure Status(job Execution, DATA_ERRO R_STATUS, error); | |
144 | ||
145 | // Flag to do rollba ck. | |
146 | successful = false; | |
147 | } | |
148 | } | |
149 | ||
150 | ||
151 | // Get latest Me ssage ID f rom the jo bExecution context. | |
152 | final Long msgId = jobExec ution.getJ obParamete rs().getLo ng(CBS_MES SAGE_ID_KE Y); | |
153 | if (su ccessful) { | |
154 | if ((msg Id == null ) || (msgI d < 1)) { | |
155 | ||
156 | // This is an unexpe cted and u nrecoverab le error. | |
157 | final Stri ng error = "Rollback Triggered - Could n ot obtain a valid me ssage ID: " + msgId; | |
158 | setFailure Status(job Execution, DATA_ERRO R_STATUS, error); | |
159 | ||
160 | // Flag to do rollba ck. | |
161 | successful = false; | |
162 | } | |
163 | } | |
164 | ||
165 | // Sen d consolid ated state ments file with temp orary file name (with timestamp extension ). | |
166 | // Exa mple: | |
167 | // 'CB S-06022016 .txt' woul d be tempo rarily sen t as | |
168 | // 'CB S-06022016 .txt.<time stamp>.sen dcbs' | |
169 | if (su ccessful) { | |
170 | if (sftp Service.ft pFileToSer verWithNam e(cbsOutpu tResourceL ocalTempFi lePath, cb sTargetTem poraryFile Name, serv erTargetDi rectory)) { | |
171 | taskletLog ger.info(" Sent file to server sucessfull y: " + cbs TargetTemp oraryFileN ame); | |
172 | } else { | |
173 | ||
174 | // This is an unexpe cted and u nrecoverab le error. | |
175 | final Stri ng error = "Rollback Triggered - Unsucce ssful file transfer: " + cbsTa rgetTempor aryFileNam e; | |
176 | setFailure Status(job Execution, WRITE_FAI LURE_STATU S, error); | |
177 | ||
178 | // Flag to do rollba ck. | |
179 | successful = false; | |
180 | } | |
181 | } | |
182 | ||
183 | // Sen d single s tatements file with temporary filename ( with times tamp exten sion). | |
184 | // Exa mple: | |
185 | // 'SB S-06022016 .txt' woul d be tempo rarily sen t as | |
186 | // 'SB S-06022016 .txt.<time stamp>.sen dcbs' | |
187 | if (su ccessful) { | |
188 | if (sftp Service.ft pFileToSer verWithNam e(sbsOutpu tResourceL ocalTempFi lePath, sb sTargetTem poraryFile Name, serv erTargetDi rectory)) { | |
189 | taskletLog ger.info(" Sent file to server sucessfull y: " + sbs TargetTemp oraryFileN ame); | |
190 | } else { | |
191 | ||
192 | // This is an unexpe cted and u nrecoverab le error. | |
193 | final Stri ng error = "Rollback Triggered - Unsucce ssful file transfer: " + sbsTa rgetTempor aryFileNam e; | |
194 | setFailure Status(job Execution, WRITE_FAI LURE_STATU S, error); | |
195 | ||
196 | // Flag to do rollba ck. | |
197 | successful = false; | |
198 | } | |
199 | } | |
200 | ||
201 | // Mar k the CBSS tmt status es to sent . (SendCBS Service:L4 07) | |
202 | long t otalPatien tsIncluded = -1; | |
203 | if (su ccessful) { | |
204 | totalPat ientsInclu ded = send CBSService .updateAll StatementS tatusToSen tForMessag eId(msgId) ; | |
205 | if (tota lPatientsI ncluded >= 0) { | |
206 | ||
207 | taskletLog ger.info(" Updated al l statemen t status t o sent for message I D: " + msg Id); | |
208 | ||
209 | } else { | |
210 | ||
211 | // This is an unexpe cted and u nrecoverab le error. | |
212 | final Stri ng error = "Rollback Triggered - Unsucce ssful atte mpting to update all statement status to sent for message ID : " | |
213 | + ms gId; | |
214 | setFailure Status(job Execution, DATA_ERRO R_STATUS, error); | |
215 | ||
216 | // Flag to do rollba ck. | |
217 | successful = false; | |
218 | } | |
219 | } | |
220 | ||
221 | // Upd ate Messag e status t o successf ul! | |
222 | if (su ccessful) { | |
223 | if (send CBSService .setMessag eStatus(ms gId, Proce ssStatus.S tatus.SUCC ESS)) { | |
224 | taskletLog ger.info(" Updated me ssage stat us to succ ess for me ssage ID: " + msgId) ; | |
225 | } else { | |
226 | ||
227 | // This is an unexpe cted and u nrecoverab le error. | |
228 | final Stri ng error = "Rollback Triggered - Unsucce ssful atte mpting to update mes sage statu s to succe ss for mes sage ID: " | |
229 | + ms gId; | |
230 | setFailure Status(job Execution, DATA_ERRO R_STATUS, error); | |
231 | ||
232 | // Flag to do rollba ck. | |
233 | successful = false; | |
234 | } | |
235 | } | |
236 | ||
237 | // Ren ame the co nsolidated statement s file on the server (by remov ing the te mporary | |
238 | // '.< timestamp> .sendcbs' extension) . | |
239 | if (su ccessful) { | |
240 | if (sftp Service.ft pRenameFil eInDirecto ry(cbsTarg etTemporar yFileName, cbsOutput FileName, serverTarg etDirector y)) { | |
241 | taskletLog ger.info(" Renamed fi le on serv er sucessf ully: " + cbsOutputF ileName); | |
242 | } else { | |
243 | ||
244 | // This is an unexpe cted and u nrecoverab le error. | |
245 | final Stri ng error = "Rollback Triggered - Unsucce ssful file rename: " + cbsOutp utFileName ; | |
246 | setFailure Status(job Execution, WRITE_FAI LURE_STATU S, error); | |
247 | ||
248 | // Flag to do rollba ck. | |
249 | successful = false; | |
250 | } | |
251 | } | |
252 | ||
253 | // Ren ame the si ngle state ments file on the se rver (by r emoving th e temporar y | |
254 | // '.< timestamp> .sendcbs' extension) . | |
255 | if (su ccessful) { | |
256 | if (sftp Service.ft pRenameFil eInDirecto ry(sbsTarg etTemporar yFileName, sbsOutput FileName, serverTarg etDirector y)) { | |
257 | taskletLog ger.info(" Renamed fi le on serv er sucessf ully: " + sbsOutputF ileName); | |
258 | } else { | |
259 | ||
260 | // This is an unexpe cted and u nrecoverab le error. | |
261 | final Stri ng error = "Rollback Triggered - Unsucce ssful file rename: " + sbsOutp utFileName ; | |
262 | setFailure Status(job Execution, WRITE_FAI LURE_STATU S, error); | |
263 | ||
264 | // Flag to do rollba ck. | |
265 | successful = false; | |
266 | } | |
267 | } | |
268 | ||
269 | // Sen d a done f ile for co nsolidated statement s to the s erver. | |
270 | final String cbs OutputDone FileName = getDoneFi lename(cbs OutputFile Name); | |
271 | if (su ccessful) { | |
272 | if (sftp Service.ft pEmptyFile ToServerWi thName(cbs OutputDone FileName, serverTarg etDirector y)) { | |
273 | taskletLog ger.info(" Transfer d one file t o server s ucessfully : " + cbsO utputDoneF ileName); | |
274 | } else { | |
275 | ||
276 | // This is an unexpe cted and u nrecoverab le error. | |
277 | final Stri ng error = "Rollback Triggered - Unsucce ssful done file tran sfer: " + cbsOutputD oneFileNam e; | |
278 | setFailure Status(job Execution, WRITE_FAI LURE_STATU S, error); | |
279 | ||
280 | // Flag to do rollba ck. | |
281 | successful = false; | |
282 | } | |
283 | } | |
284 | ||
285 | // Sen d a done f ile for co nsolidated statement s to the s erver. | |
286 | final String sbs OutputDone FileName = getDoneFi lename(sbs OutputFile Name); | |
287 | if (su ccessful) { | |
288 | if (sftp Service.ft pEmptyFile ToServerWi thName(sbs OutputDone FileName, serverTarg etDirector y)) { | |
289 | taskletLog ger.info(" Transfer d one file t o server s ucessfully : " + sbsO utputDoneF ileName); | |
290 | } else { | |
291 | ||
292 | // This is an unexpe cted and u nrecoverab le error. | |
293 | final Stri ng error = "Rollback Triggered - Unsucce ssful done file tran sfer: " + sbsOutputD oneFileNam e; | |
294 | setFailure Status(job Execution, WRITE_FAI LURE_STATU S, error); | |
295 | ||
296 | // Flag to do rollba ck. | |
297 | successful = false; | |
298 | } | |
299 | } | |
300 | ||
301 | // If successful then save final sta tistics. | |
302 | if (su ccessful) { | |
303 | // Save batch job completion results. | |
304 | jobExecu tion.getEx ecutionCon text().put Long(CBS_T OTAL_PATIE NTS_INCLUD ED_KEY, to talPatient sIncluded) ; | |
305 | } | |
306 | } | |
307 | ||
308 | // R egardless of failure or succes sful batch run, dele te local s tatement f iles if ex ist. | |
309 | if ( sendCBSSer vice.delet eCBSOutput Resource(c bsOutputRe sourceLoca lTempFileP ath)) { | |
310 | taskle tLogger.in fo("Delete d temporar y local fi le sucessf ully: " + cbsOutputR esourceLoc alTempFile Path); | |
311 | } el se { | |
312 | ||
313 | // Don 't make th is a failu re in case transfer had alread y been | |
314 | // suc cessful. | |
315 | taskle tLogger.wa rn("Unable to delete the tempo rary local file: " + cbsOutput ResourceLo calTempFil ePath); | |
316 | } | |
317 | ||
318 | if ( sendCBSSer vice.delet eCBSOutput Resource(s bsOutputRe sourceLoca lTempFileP ath)) { | |
319 | taskle tLogger.in fo("Delete d temporar y local fi le sucessf ully: " + sbsOutputR esourceLoc alTempFile Path); | |
320 | } el se { | |
321 | ||
322 | // Don 't make th is a failu re in case transfer had alread y been | |
323 | // suc cessful. | |
324 | taskle tLogger.wa rn("Unable to delete the tempo rary local file: " + sbsOutput ResourceLo calTempFil ePath); | |
325 | } | |
326 | ||
327 | retu rn success ful; | |
328 | } | |
329 | ||
330 | pr ivate Stri ng getOutp utResource (final Job Execution jobExecuti on, final String out putResourc eKey) { | |
331 | ||
332 | fina l String o utputResou rce = jobE xecution.g etExecutio nContext() .getString (outputRes ourceKey); | |
333 | ||
334 | // I f an error getting t he resourc e then set the failu re status and trigge r roll bac k. | |
335 | if ( (outputRes ource != n ull) && !o utputResou rce.isEmpt y()) { | |
336 | ||
337 | taskle tLogger.in fo(outputR esourceKey + ": " + outputReso urce); | |
338 | ||
339 | } el se { | |
340 | ||
341 | // Thi s is an un expected a nd unrecov erable err or. | |
342 | final String err or = "Roll back Trigg ered - Cou ld not obt ain the re source, " + outputRe sourceKey; | |
343 | setFai lureStatus (jobExecut ion, DATA_ ERROR_STAT US, error) ; | |
344 | ||
345 | } | |
346 | ||
347 | retu rn outputR esource; | |
348 | } | |
349 | ||
350 | pr ivate Stri ng getJobP arameter(f inal JobEx ecution jo bExecution , final St ring jobPa rameterKey ) { | |
351 | ||
352 | fina l String j obParamete r = jobExe cution.get JobParamet ers().getS tring(jobP arameterKe y); | |
353 | ||
354 | // I f an error getting t he jobPara meter then set the f ailure sta tus and tr igger roll back. | |
355 | if ( (jobParame ter != nul l) && !job Parameter. isEmpty()) { | |
356 | ||
357 | taskle tLogger.in fo(jobPara meterKey + ": " + jo bParameter ); | |
358 | ||
359 | } el se { | |
360 | ||
361 | // Thi s is an un expected a nd unrecov erable err or. | |
362 | final String err or = "Roll back Trigg ered - Cou ld not obt ain the jo b paramete r, " + job ParameterK ey; | |
363 | setFai lureStatus (jobExecut ion, DATA_ ERROR_STAT US, error) ; | |
364 | ||
365 | } | |
366 | ||
367 | retu rn jobPara meter; | |
368 | } | |
369 | ||
370 | /* * | |
371 | * Build a t emporary f ilename fo r use on t he target sftp serve r. The for mat | |
372 | * of the te mporary fi lename: Gi ven filena me: 'CBS-0 6022016.tx t' would | |
373 | * return te mporary fi lename: 'C BS-0602201 6.txt.<tim estamp>.se ndcbs' | |
374 | * | |
375 | * @param fi lename | |
376 | * The base filename. | |
377 | * @return T he tempora ry filenam e. | |
378 | * / | |
379 | pr ivate Stri ng getTemp oraryFilen ame(final String fil ename) { | |
380 | if ( (filename != null) & & !filenam e.isEmpty( )) { | |
381 | String Builder te mpFilename = new Str ingBuilder (filename) ; | |
382 | tempFi lename.app end("."); | |
383 | tempFi lename.app end(TimeUt ils.getTim estamp(TIM ESTAMP_FOR MAT)); | |
384 | tempFi lename.app end("."); | |
385 | tempFi lename.app end(tempFi lenamePost fix); | |
386 | return tempFilen ame.toStri ng(); | |
387 | } | |
388 | retu rn null; | |
389 | } | |
390 | ||
391 | /* * | |
392 | * Get the d one filena me based o n the outp ut filenam e. The don e filename is | |
393 | * the same base filen ame but wi th ".don" extension instead of ".txt" | |
394 | * | |
395 | * @param fi lename | |
396 | * The outp ut filenam e. | |
397 | * @return T he done fi lename. | |
398 | * / | |
399 | pr ivate Stri ng getDone Filename(f inal Strin g filename ) { | |
400 | ||
401 | retu rn filenam e.substrin g(0, filen ame.lastIn dexOf(".tx t")) + ".d on"; | |
402 | } | |
403 | ||
404 | } |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.