Produced by Araxis Merge on 2/7/2017 12:14:06 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\fps | FpsDelegateMultiItemWriter.java | Wed Feb 1 21:07:30 2017 UTC |
2 | cpss.zip\cpss\src\main\java\gov\va\cpss\job\fps | FpsDelegateMultiItemWriter.java | Fri Feb 3 20:43:50 2017 UTC |
Description | Between Files 1 and 2 |
|
---|---|---|
Text Blocks | Lines | |
Unchanged | 2 | 772 |
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.fps; | |
2 | ||
3 | import sta tic gov.va .cpss.job. CbssJobPro cessingCon stants.JOB _FAILURE_K EY; | |
4 | import sta tic gov.va .cpss.job. CbssJobPro cessingCon stants.JOB _FAILURE_M ESSAGE_KEY ; | |
5 | import sta tic gov.va .cpss.job. CbssJobPro cessingCon stants.WRI TE_FAILURE _STATUS; | |
6 | import sta tic gov.va .cpss.job. fps.FpsPro cessingCon stants.PAT IENT_ID_KE Y; | |
7 | import sta tic gov.va .cpss.job. fps.FpsPro cessingCon stants.SIT E_ID_KEY; | |
8 | import sta tic gov.va .cpss.job. fps.FpsPro cessingCon stants.TOT AL_SITE_CO UNT_KEY; | |
9 | import sta tic gov.va .cpss.job. fps.FpsPro cessingCon stants.TOT AL_STATEME NT_COUNT_K EY; | |
10 | ||
11 | import jav a.util.Arr ayList; | |
12 | import jav a.util.Lis t; | |
13 | ||
14 | import org .apache.lo g4j.Logger ; | |
15 | import org .springfra mework.bat ch.core.Ex itStatus; | |
16 | import org .springfra mework.bat ch.core.It emWriteLis tener; | |
17 | import org .springfra mework.bat ch.core.Jo bExecution ; | |
18 | import org .springfra mework.bat ch.core.St epExecutio n; | |
19 | import org .springfra mework.bat ch.core.St epExecutio nListener; | |
20 | import org .springfra mework.bat ch.item.It emWriter; | |
21 | ||
22 | import gov .va.cpss.m odel.fps.P SDetails; | |
23 | import gov .va.cpss.m odel.fps.P SPatient; | |
24 | import gov .va.cpss.m odel.fps.P SRecord; | |
25 | import gov .va.cpss.m odel.fps.P SSite; | |
26 | import gov .va.cpss.s ervice.Fps Service; | |
27 | ||
28 | /** | |
29 | * Impleme ntation of ItemWrite r used to handle wri ting of ra w records when | |
30 | * process ing. | |
31 | * | |
32 | * @author DN S P II | |
33 | */ | |
34 | public cla ss FpsDele gateMultiI temWriter | |
35 | impl ements Ite mWriter<PS Record>, I temWriteLi stener<PSR ecord>, St epExecutio nListener { | |
36 | ||
37 | /* | |
38 | * Flag to i ndicate th at the job has been forcefully stopped a nd should no | |
39 | * longer at tempt writ es. | |
40 | * / | |
41 | pr ivate bool ean forceS top = fals e; | |
42 | ||
43 | pr ivate fina l Logger w riterLogge r = Logger .getLogger (FpsDelega teMultiIte mWriter.cl ass.getCan onicalName ()); | |
44 | ||
45 | pr ivate JobE xecution j obExecutio n; | |
46 | ||
47 | pr ivate FpsS ervice fps Service; | |
48 | ||
49 | pr ivate Long siteId; | |
50 | pr ivate Long patientId ; | |
51 | ||
52 | /* | |
53 | * Delegate writer use d to take advantage of batch w riting pat ient detai ls | |
54 | * records. | |
55 | * / | |
56 | pr ivate Item Writer<PSD etails> ps DetailsDat abaseItemW riter; | |
57 | ||
58 | pu blic FpsSe rvice getF psService( ) { | |
59 | retu rn fpsServ ice; | |
60 | } | |
61 | ||
62 | pu blic void setFpsServ ice(FpsSer vice fpsSe rvice) { | |
63 | this .fpsServic e = fpsSer vice; | |
64 | } | |
65 | ||
66 | pu blic ItemW riter<PSDe tails> get PsDetailsD atabaseIte mWriter() { | |
67 | retu rn psDetai lsDatabase ItemWriter ; | |
68 | } | |
69 | ||
70 | pu blic void setPsDetai lsDatabase ItemWriter (ItemWrite r<PSDetail s> psDetai lsDatabase ItemWriter ) { | |
71 | this .psDetails DatabaseIt emWriter = psDetails DatabaseIt emWriter; | |
72 | } | |
73 | ||
74 | @O verride | |
75 | pu blic void write(List <? extends PSRecord> items) th rows Excep tion { | |
76 | ||
77 | writ erLogger.i nfo("Begin Write"); | |
78 | ||
79 | // D o not atte mpt to wri te if forc ed stop. | |
80 | if ( forceStop) { | |
81 | return ; | |
82 | } | |
83 | ||
84 | try { | |
85 | ||
86 | // Lis t of detai ls that ca n be writt en using b atch write . | |
87 | List<P SDetails> detailsL = new Array List<>(); | |
88 | ||
89 | // Loo p through all of the statement s to write . | |
90 | for (O bject item : items) { | |
91 | ||
92 | // Updat e appropri ate object s with the appropria te FK valu es. | |
93 | // The a ssumption is the row s are orde red proper ly. | |
94 | ||
95 | // The d ata is pro cessed as: | |
96 | // PS, t hen subseq uent 1-* P H. | |
97 | // PH, t hen subseq uent 1-* P D. | |
98 | ||
99 | if (item instanceo f PSSite) { | |
100 | ||
101 | PSSite sit e = (PSSit e) item; | |
102 | ||
103 | // If a PS Site row t hen reset the child keys to nu ll to | |
104 | // prevent | |
105 | // erroneo us foreign key refer ences. | |
106 | resetSiteF K(); | |
107 | ||
108 | // Batch w on't retur n generate d keys so use DAO. | |
109 | writerLogg er.info("S aving PSSi te: " + si te.toStrin g()); | |
110 | fpsService .saveSite( site); | |
111 | ||
112 | // Save th is record ID for lat er referen ce for chi ldren | |
113 | // PSPatie nt records . | |
114 | updateSite Processing Data(site. getId()); | |
115 | ||
116 | } else i f (item in stanceof P SPatient) { | |
117 | ||
118 | PSPatient patient = (PSPatient ) item; | |
119 | ||
120 | // The Sit eId FK nee ds to be s et in the associated PSPatient | |
121 | // object. | |
122 | // Save th e PSSite I D. | |
123 | if (jobExe cution.get ExecutionC ontext().c ontainsKey (SITE_ID_K EY)) { | |
124 | si teId = job Execution. getExecuti onContext( ).getLong( SITE_ID_KE Y); | |
125 | } | |
126 | ||
127 | // If a PS Patient ro w then res et the chi ld keys to null to | |
128 | // prevent | |
129 | // erroneo us foreign key refer ences. | |
130 | resetPatie ntFK(); | |
131 | ||
132 | if (siteId != null) { | |
133 | ||
134 | // For PSPat ient recor ds, set th e database | |
135 | // auto-gene rated | |
136 | // Site ID i n the pati ent record . | |
137 | pa tient.setP sSite(new PSSite(sit eId)); | |
138 | ||
139 | // Batch won 't return generated keys so us e service. | |
140 | wr iterLogger .info("Sav ing PSPati ent: " + p atient.toS tring()); | |
141 | fp sService.s avePatient (patient); | |
142 | ||
143 | // Save this record ID for later reference for child ren | |
144 | // PSDetails records. | |
145 | up datePatien tProcessin gData(pati ent.getId( )); | |
146 | } else { | |
147 | ||
148 | // Unrecover able error so stop t he job. | |
149 | st opJob(WRIT E_FAILURE_ STATUS, "A ttempted t o process PH row wit h a null P S FK"); | |
150 | br eak; | |
151 | } | |
152 | ||
153 | } else i f (item in stanceof P SDetails) { | |
154 | ||
155 | PSDetails details = (PSDetails ) item; | |
156 | ||
157 | // The Pat ientId FK needs to b e set in t he associa ted | |
158 | // PSDetai ls | |
159 | // object. | |
160 | // Save th e PSPatien t ID. | |
161 | if (jobExe cution.get ExecutionC ontext().c ontainsKey (PATIENT_I D_KEY)) { | |
162 | pa tientId = jobExecuti on.getExec utionConte xt().getLo ng(PATIENT _ID_KEY); | |
163 | } | |
164 | ||
165 | // At this point pat ientId FK should not be null. | |
166 | if (patien tId != nul l) { | |
167 | ||
168 | // For PSDet ails recor ds, set th e database | |
169 | // auto-gene rated | |
170 | // Patient I D in the d etails rec ord. | |
171 | de tails.setP sPatientId (patientId ); | |
172 | ||
173 | // Because w e don't ne ed generat ed keys so we can us e | |
174 | // the | |
175 | // batch for | |
176 | // this inse rt. They w ill be wri tten to th e database | |
177 | // after | |
178 | // the recor d loop has finished. | |
179 | de tailsL.add (details); | |
180 | } else { | |
181 | ||
182 | // Unrecover able error so stop t he job. | |
183 | st opJob(WRIT E_FAILURE_ STATUS, "A ttempted t o process PD row wit h a null P H FK"); | |
184 | br eak; | |
185 | } | |
186 | ||
187 | } else { | |
188 | ||
189 | // Unrecov erable err or so stop the job. | |
190 | stopJob(WR ITE_FAILUR E_STATUS, "Attempted to write unknown re cord"); | |
191 | break; | |
192 | } | |
193 | } | |
194 | ||
195 | // We can use ba tch to upd ate replac ed stateme nts. | |
196 | if (!f orceStop & & !details L.isEmpty( )) { | |
197 | writerLo gger.info( "Saving li st of CBSS iteTrans") ; | |
198 | psDetail sDatabaseI temWriter. write(deta ilsL); | |
199 | } | |
200 | ||
201 | } ca tch (Excep tion e) { | |
202 | ||
203 | String Builder er ror = new StringBuil der(); | |
204 | error. append("Er ror during write bec ause of ") ; | |
205 | error. append(e.g etClass(). getSimpleN ame()); | |
206 | error. append("\n Message: " ); | |
207 | error. append(e.g etMessage( )); | |
208 | if ((e .getCause( ) != null) && (e.get Cause().ge tMessage() != null)) { | |
209 | error.ap pend("\nCa use: "); | |
210 | error.ap pend(e.get Cause().ge tMessage() .trim()); | |
211 | } | |
212 | ||
213 | // Unr ecoverable error so stop the j ob. | |
214 | stopJo b(WRITE_FA ILURE_STAT US, error. toString() ); | |
215 | } | |
216 | ||
217 | writ erLogger.i nfo("End W rite"); | |
218 | } | |
219 | ||
220 | /* * | |
221 | * Reset the site valu es from th e job exec ution to p repare to process ne xt | |
222 | * site. | |
223 | * / | |
224 | pr ivate void resetSite FK() { | |
225 | site Id = null; | |
226 | if ( jobExecuti on.getExec utionConte xt().conta insKey(SIT E_ID_KEY)) { | |
227 | jobExe cution.get ExecutionC ontext().r emove(SITE _ID_KEY); | |
228 | } | |
229 | ||
230 | rese tPatientFK (); | |
231 | } | |
232 | ||
233 | /* * | |
234 | * Reset the patient v alues from the job e xecution t o prepare to process | |
235 | * next pati ent. | |
236 | * / | |
237 | pr ivate void resetPati entFK() { | |
238 | pati entId = nu ll; | |
239 | if ( jobExecuti on.getExec utionConte xt().conta insKey(PAT IENT_ID_KE Y)) { | |
240 | jobExe cution.get ExecutionC ontext().r emove(PATI ENT_ID_KEY ); | |
241 | } | |
242 | } | |
243 | ||
244 | /* * | |
245 | * Update si te process ing metada ta. | |
246 | * / | |
247 | pr ivate bool ean update SiteProces singData(f inal Long primaryKey Result) { | |
248 | ||
249 | bool ean succes sful = tru e; | |
250 | ||
251 | writ erLogger.i nfo("Wrote PS(" + pr imaryKeyRe sult + ")" ); | |
252 | jobE xecution.g etExecutio nContext() .putLong(S ITE_ID_KEY , primaryK eyResult); | |
253 | ||
254 | if ( jobExecuti on.getExec utionConte xt().conta insKey(TOT AL_SITE_CO UNT_KEY)) { | |
255 | jobExe cution.get ExecutionC ontext().p utLong(TOT AL_SITE_CO UNT_KEY, | |
256 | jobExecuti on.getExec utionConte xt().getLo ng(TOTAL_S ITE_COUNT_ KEY) + 1); | |
257 | } el se { | |
258 | setFai lureMessag e("Unable to increme nt total s ite count" ); | |
259 | succes sful = fal se; | |
260 | } | |
261 | ||
262 | retu rn success ful; | |
263 | } | |
264 | ||
265 | /* * | |
266 | * Update pa tient proc essing met adata. | |
267 | * / | |
268 | pr ivate bool ean update PatientPro cessingDat a(final Lo ng primary KeyResult) { | |
269 | ||
270 | bool ean succes sful = tru e; | |
271 | ||
272 | writ erLogger.i nfo("Wrote PH(" + pr imaryKeyRe sult + ")" ); | |
273 | jobE xecution.g etExecutio nContext() .putLong(P ATIENT_ID_ KEY, prima ryKeyResul t); | |
274 | ||
275 | if ( jobExecuti on.getExec utionConte xt().conta insKey(TOT AL_STATEME NT_COUNT_K EY)) { | |
276 | jobExe cution.get ExecutionC ontext().p utLong(TOT AL_STATEME NT_COUNT_K EY, | |
277 | jobExecuti on.getExec utionConte xt().getLo ng(TOTAL_S TATEMENT_C OUNT_KEY) + 1); | |
278 | } el se { | |
279 | setFai lureMessag e("Unable to increme nt total s tatement c ount"); | |
280 | succes sful = fal se; | |
281 | } | |
282 | ||
283 | retu rn success ful; | |
284 | } | |
285 | ||
286 | @O verride | |
287 | pu blic void beforeWrit e(List<? e xtends PSR ecord> ite ms) { | |
288 | forc eStop = fa lse; | |
289 | if ( jobExecuti on.getExec utionConte xt().conta insKey(JOB _FAILURE_K EY)) { | |
290 | writer Logger.err or("System failure d etected.") ; | |
291 | forceS top = true ; | |
292 | } | |
293 | } | |
294 | ||
295 | @O verride | |
296 | pu blic void afterWrite (List<? ex tends PSRe cord> item s) { | |
297 | // D o not need to do any thing. | |
298 | } | |
299 | ||
300 | @O verride | |
301 | pu blic void onWriteErr or(Excepti on e, List <? extends PSRecord> items) { | |
302 | ||
303 | writ erLogger.e rror("Writ er encount ered syste m error an d forced s top"); | |
304 | ||
305 | Stri ngBuilder error = ne w StringBu ilder(); | |
306 | erro r.append(" Unable to write item because o f "); | |
307 | erro r.append(e .getClass( ).getSimpl eName()); | |
308 | erro r.append(" \nMessage: "); | |
309 | erro r.append(e .getMessag e()); | |
310 | if ( (e.getCaus e() != nul l) && (e.g etCause(). getMessage () != null )) { | |
311 | error. append("\n Cause: "); | |
312 | error. append(e.g etCause(). getMessage ().trim()) ; | |
313 | } | |
314 | ||
315 | // S et failure and messa ge. | |
316 | stop Job(WRITE_ FAILURE_ST ATUS, "Unr ecoverable writer er ror"); | |
317 | } | |
318 | ||
319 | @O verride | |
320 | pu blic void beforeStep (StepExecu tion stepE xecution) { | |
321 | writ erLogger.i nfo("Befor e Step Exe cution"); | |
322 | // S ave the jo b executio n at the b eginning o f the step . | |
323 | // T he executi on context will be u sed to set key value s as data is | |
324 | // p rocessed. | |
325 | jobE xecution = stepExecu tion.getJo bExecution (); | |
326 | } | |
327 | ||
328 | @O verride | |
329 | pu blic ExitS tatus afte rStep(Step Execution stepExecut ion) { | |
330 | // D o not need to do any thing afte r step. | |
331 | retu rn null; | |
332 | } | |
333 | ||
334 | /* * | |
335 | * Forcefull y stop the job proce ssing beca use a fail ure was de tected. | |
336 | * | |
337 | * @param st atus | |
338 | * The stat us for the failure. | |
339 | * @param me ssage | |
340 | * The mess age associ ated with the status failure. | |
341 | * / | |
342 | pr ivate void stopJob(f inal Strin g status, final Stri ng message ) { | |
343 | ||
344 | // S et the fla g to indic ate the jo b has been forcefull y stopped. | |
345 | forc eStop = tr ue; | |
346 | ||
347 | // L og message . | |
348 | writ erLogger.e rror("Writ er executi on encount ered unrec overable e rror and f orced stop "); | |
349 | ||
350 | // S et failure and messa ge. | |
351 | setF ailureStat us(status) ; | |
352 | ||
353 | // S et failure message. | |
354 | setF ailureMess age(messag e); | |
355 | } | |
356 | ||
357 | /* * | |
358 | * Set the f ailure in the job ex ecution co ntext. | |
359 | * | |
360 | * @param st atus | |
361 | * The fail ure status . | |
362 | * / | |
363 | pr ivate void setFailur eStatus(fi nal String status) { | |
364 | ||
365 | // L og job fai lure statu s. | |
366 | writ erLogger.e rror("Job failed wit h status: " + status ); | |
367 | ||
368 | // S et job fai lure. | |
369 | jobE xecution.g etExecutio nContext() .putString (JOB_FAILU RE_KEY, st atus); | |
370 | } | |
371 | ||
372 | /* * | |
373 | * Set the f ailure mes sage in th e job exec ution cont ext. | |
374 | * | |
375 | * @param me ssage | |
376 | * The mess age to ass ociate wit h the erro r status. | |
377 | * / | |
378 | pr ivate void setFailur eMessage(f inal Strin g message) { | |
379 | ||
380 | // L og job fai lure messa ge. | |
381 | writ erLogger.e rror("Job failure me ssage: " + message); | |
382 | ||
383 | // S et job fai lure messa ge. | |
384 | jobE xecution.g etExecutio nContext() .putString (JOB_FAILU RE_MESSAGE _KEY, mess age); | |
385 | } | |
386 | ||
387 | } |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.