Produced by Araxis Merge on 2/7/2017 12:14:05 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\cbs | CbsDelegateMultiItemWriter.java | Wed Feb 1 21:07:30 2017 UTC |
2 | cpss.zip\cpss\src\main\java\gov\va\cpss\job\cbs | CbsDelegateMultiItemWriter.java | Fri Feb 3 20:32:20 2017 UTC |
Description | Between Files 1 and 2 |
|
---|---|---|
Text Blocks | Lines | |
Unchanged | 2 | 682 |
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.cbs; | |
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. cbs.CbsPro cessingCon stants.CBS _TOTAL_INI TIAL_STATE MENT_COUNT _KEY; | |
7 | import sta tic gov.va .cpss.job. cbs.CbsPro cessingCon stants.CBS _TOTAL_REP LACED_STAT EMENT_COUN T_KEY; | |
8 | ||
9 | import jav a.util.Arr ayList; | |
10 | import jav a.util.Lis t; | |
11 | ||
12 | import org .apache.lo g4j.Logger ; | |
13 | import org .springfra mework.bat ch.core.Ex itStatus; | |
14 | import org .springfra mework.bat ch.core.It emWriteLis tener; | |
15 | import org .springfra mework.bat ch.core.Jo bExecution ; | |
16 | import org .springfra mework.bat ch.core.St epExecutio n; | |
17 | import org .springfra mework.bat ch.core.St epExecutio nListener; | |
18 | import org .springfra mework.bat ch.item.It emWriter; | |
19 | ||
20 | import gov .va.cpss.m odel.cbs.C BSSitePati ent; | |
21 | import gov .va.cpss.m odel.cbs.C BSSiteStmt ; | |
22 | import gov .va.cpss.m odel.cbs.C BSSiteTran s; | |
23 | import gov .va.cpss.m odel.cbs.C BSStmt; | |
24 | import gov .va.cpss.s ervice.Cbs Service; | |
25 | ||
26 | /** | |
27 | * Impleme ntation of ItemWrite r used to handle wri ting of co nsolidated | |
28 | * stateme nts when p rocessing. | |
29 | * | |
30 | * @author DN S P II | |
31 | */ | |
32 | public cla ss CbsDele gateMultiI temWriter | |
33 | impl ements Ite mWriter<CB SStmt>, It emWriteLis tener<CBSS tmt>, Step ExecutionL istener { | |
34 | ||
35 | pr ivate fina l Logger w riterLogge r = Logger .getLogger (CbsDelega teMultiIte mWriter.cl ass.getCan onicalName ()); | |
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 | /* | |
44 | * Count of statements written s uccessfull y. | |
45 | * / | |
46 | pr ivate long initialSt atementCou nt = 0; | |
47 | ||
48 | /* | |
49 | * Count of replaced s tatements updated su ccessfully . | |
50 | * / | |
51 | pr ivate long replacedS tatementCo unt = 0; | |
52 | ||
53 | ||
54 | ||
55 | pr ivate JobE xecution j obExecutio n; | |
56 | ||
57 | pr ivate CbsS ervice cbs Service; | |
58 | ||
59 | /* | |
60 | * Delegate writer use d to take advantage of batch u pdating re placed | |
61 | * statement s. | |
62 | * / | |
63 | pr ivate Item Writer<Lon g> cbsStmt DatabaseIt emUpdater; | |
64 | ||
65 | /* | |
66 | * Delegate writer use d to take advantage of batch w riting sit e transact ion | |
67 | * records. | |
68 | * / | |
69 | pr ivate Item Writer<CBS SiteTrans> cbsSiteTr ansDatabas eItemWrite r; | |
70 | ||
71 | pu blic CbsSe rvice getC bsService( ) { | |
72 | retu rn cbsServ ice; | |
73 | } | |
74 | ||
75 | pu blic void setCbsServ ice(CbsSer vice cbsSe rvice) { | |
76 | this .cbsServic e = cbsSer vice; | |
77 | } | |
78 | ||
79 | pu blic ItemW riter<Long > getCbsSt mtDatabase ItemUpdate r() { | |
80 | retu rn cbsStmt DatabaseIt emUpdater; | |
81 | } | |
82 | ||
83 | pu blic void setCbsStmt DatabaseIt emUpdater( ItemWriter <Long> cbs StmtDataba seItemUpda ter) { | |
84 | this .cbsStmtDa tabaseItem Updater = cbsStmtDat abaseItemU pdater; | |
85 | } | |
86 | ||
87 | pu blic ItemW riter<CBSS iteTrans> getCbsSite TransDatab aseItemWri ter() { | |
88 | retu rn cbsSite TransDatab aseItemWri ter; | |
89 | } | |
90 | ||
91 | pu blic void setCbsSite TransDatab aseItemWri ter(ItemWr iter<CBSSi teTrans> c bsSiteTran sDatabaseI temWriter) { | |
92 | this .cbsSiteTr ansDatabas eItemWrite r = cbsSit eTransData baseItemWr iter; | |
93 | } | |
94 | ||
95 | @O verride | |
96 | pu blic void write(List <? extends CBSStmt> items) thr ows Except ion { | |
97 | ||
98 | writ erLogger.i nfo("Begin Write"); | |
99 | ||
100 | // D o not atte mpt to wri te if forc ed stop. | |
101 | if ( forceStop) { | |
102 | return ; | |
103 | } | |
104 | ||
105 | try { | |
106 | // Tra ck list of replaced statements . | |
107 | List<L ong> repla cedStateme ntL = new ArrayList< >(); | |
108 | ||
109 | // Loo p through all of the statement s to write . | |
110 | for (O bject item : items) { | |
111 | ||
112 | CBSStmt statement = (CBSStmt ) item; | |
113 | ||
114 | // Batch won't ret urn genera ted keys s o use DAO. | |
115 | writerLo gger.info( "Saving CB SStmt: " + statement .toString( )); | |
116 | cbsServi ce.saveSta tement(sta tement); | |
117 | ||
118 | // Incre ment the w rite count for tabul ating expe cted total s | |
119 | // durin g final co mpletion t ask. | |
120 | ++initia lStatement Count; | |
121 | ||
122 | // Loop through al l of the S iteStmt. | |
123 | for (CBS SiteStmt s iteStateme nt : state ment.getSi teStmtL()) { | |
124 | ||
125 | // Save th e database auto-gene rated stat ement ID i n the site | |
126 | // stateme nt. | |
127 | siteStatem ent.setStm tId(statem ent.getId( )); | |
128 | ||
129 | // Batch w on't retur n generate d keys so use servic e. | |
130 | writerLogg er.info("S aving CBSS iteStmt"); | |
131 | cbsService .saveSiteS tatement(s iteStateme nt); | |
132 | ||
133 | // Update the site p atient wit h the asso ciated sit e | |
134 | // stateme nt ID. | |
135 | writerLogg er.info("S aving CBSS itePatient "); | |
136 | CBSSitePat ient siteP atient = s iteStateme nt.getSite Patient(); | |
137 | sitePatien t.setSiteS tmtId(site Statement. getId()); | |
138 | ||
139 | // Since o ne to one ratio to s ite statem ent just u se the | |
140 | // service to save. | |
141 | cbsService .savePatie nt(sitePat ient); | |
142 | ||
143 | // Update the list o f site tra ns with th e associat ed site | |
144 | // stateme nt ID. | |
145 | List<CBSSi teTrans> s iteTransL = siteStat ement.getS iteTransL( ); | |
146 | siteTransL .stream(). forEach(u -> u.setSi teStmtId(s iteStateme nt.getId() )); | |
147 | ||
148 | // We don' t need gen erated key s so we ca n use the batch for | |
149 | // this in sert. | |
150 | writerLogg er.info("S aving list of CBSSit eTrans"); | |
151 | cbsSiteTra nsDatabase ItemWriter .write(sit eTransL); | |
152 | } | |
153 | ||
154 | // Appen d to repla ced statem ent list i f necessar y. | |
155 | if (stat ement.getR eplacedStm tId() != 0 ) { | |
156 | replacedSt atementL.a dd(stateme nt.getRepl acedStmtId ()); | |
157 | } | |
158 | ||
159 | } | |
160 | ||
161 | // We can use ba tch to upd ate replac ed stateme nts. | |
162 | if (!r eplacedSta tementL.is Empty()) { | |
163 | writerLo gger.info( "Updating Replaced S tatements" ); | |
164 | cbsStmtD atabaseIte mUpdater.w rite(repla cedStateme ntL); | |
165 | // Save the count for tabula ting expec ted totals during fi nal | |
166 | // compl etion task . | |
167 | replaced StatementC ount = rep lacedState mentL.size (); | |
168 | } | |
169 | ||
170 | } ca tch (Excep tion e) { | |
171 | ||
172 | String Builder er ror = new StringBuil der(); | |
173 | error. append("Er ror during write bec ause of ") ; | |
174 | error. append(e.g etClass(). getSimpleN ame()); | |
175 | error. append("\n Cause: "); | |
176 | error. append(e.g etCause(). getMessage ().trim()) ; | |
177 | ||
178 | // Unr ecoverable error so stop the j ob. | |
179 | stopJo b(WRITE_FA ILURE_STAT US, error. toString() ); | |
180 | } | |
181 | ||
182 | writ erLogger.i nfo("End W rite"); | |
183 | } | |
184 | ||
185 | @O verride | |
186 | pu blic void beforeWrit e(List<? e xtends CBS Stmt> item s) { | |
187 | // R eset the w rite count . | |
188 | init ialStateme ntCount = 0; | |
189 | repl acedStatem entCount = 0; | |
190 | ||
191 | forc eStop = fa lse; | |
192 | if ( jobExecuti on.getExec utionConte xt().conta insKey(JOB _FAILURE_K EY)) { | |
193 | writer Logger.err or("System failure d etected.") ; | |
194 | forceS top = true ; | |
195 | } | |
196 | } | |
197 | ||
198 | @O verride | |
199 | pu blic void afterWrite (List<? ex tends CBSS tmt> items ) { | |
200 | ||
201 | // S aving runn ing total of the wri tten count s in the j ob executi on | |
202 | // c ontext for completio n task ref erence. | |
203 | if ( (items != null) && ! items.isEm pty()) { | |
204 | ||
205 | if (!u pdateIniti alStatemen tCount()) { | |
206 | // Set f ailure and message. | |
207 | stopJob( WRITE_FAIL URE_STATUS , "Unable to update running to tal initia l statemen t count"); | |
208 | } else if (!upda teReplaced StatementC ount()) { | |
209 | // Set f ailure and message. | |
210 | stopJob( WRITE_FAIL URE_STATUS , "Unable to update running to tal replac ed stateme nt count") ; | |
211 | } | |
212 | } | |
213 | } | |
214 | ||
215 | /* * | |
216 | * Update th e running total of i nitial sta tements wr itten in t he job | |
217 | * execution context. | |
218 | * | |
219 | * @return B oolean fla g indicati ng if succ essful or not. | |
220 | * / | |
221 | pr ivate bool ean update InitialSta tementCoun t() { | |
222 | ||
223 | bool ean succes sful = fal se; | |
224 | ||
225 | if ( jobExecuti on.getExec utionConte xt().conta insKey(CBS _TOTAL_INI TIAL_STATE MENT_COUNT _KEY)) { | |
226 | jobExe cution.get ExecutionC ontext().p utLong(CBS _TOTAL_INI TIAL_STATE MENT_COUNT _KEY, | |
227 | jobExecuti on.getExec utionConte xt().getLo ng(CBS_TOT AL_INITIAL _STATEMENT _COUNT_KEY ) | |
228 | + in itialState mentCount) ; | |
229 | succes sful = tru e; | |
230 | } | |
231 | ||
232 | retu rn success ful; | |
233 | } | |
234 | ||
235 | /* * | |
236 | * Update th e running total of r eplaced st atements u pdated in the job | |
237 | * execution context. | |
238 | * | |
239 | * @return B oolean fla g indicati ng if succ essful or not. | |
240 | * / | |
241 | pr ivate bool ean update ReplacedSt atementCou nt() { | |
242 | ||
243 | bool ean succes sful = fal se; | |
244 | ||
245 | if ( jobExecuti on.getExec utionConte xt().conta insKey(CBS _TOTAL_REP LACED_STAT EMENT_COUN T_KEY)) { | |
246 | jobExe cution.get ExecutionC ontext().p utLong(CBS _TOTAL_REP LACED_STAT EMENT_COUN T_KEY, | |
247 | jobExecuti on.getExec utionConte xt().getLo ng(CBS_TOT AL_REPLACE D_STATEMEN T_COUNT_KE Y) | |
248 | + re placedStat ementCount ); | |
249 | succes sful = tru e; | |
250 | } | |
251 | ||
252 | retu rn success ful; | |
253 | } | |
254 | ||
255 | @O verride | |
256 | pu blic void onWriteErr or(Excepti on e, List <? extends CBSStmt> items) { | |
257 | ||
258 | writ erLogger.e rror("Writ er encount ered syste m error an d forced s top"); | |
259 | ||
260 | Stri ngBuilder error = ne w StringBu ilder(); | |
261 | erro r.append(" Unable to write item because o f "); | |
262 | erro r.append(e .getClass( ).getSimpl eName()); | |
263 | erro r.append(" \nMessage: "); | |
264 | erro r.append(e .getMessag e()); | |
265 | if ( (e.getCaus e() != nul l) && (e.g etCause(). getMessage () != null )) { | |
266 | error. append("\n Cause: "); | |
267 | error. append(e.g etCause(). getMessage ().trim()) ; | |
268 | } | |
269 | ||
270 | // S et failure and messa ge. | |
271 | stop Job(WRITE_ FAILURE_ST ATUS, "Unr ecoverable writer er ror"); | |
272 | } | |
273 | ||
274 | @O verride | |
275 | pu blic void beforeStep (StepExecu tion stepE xecution) { | |
276 | writ erLogger.i nfo("Befor e Step Exe cution"); | |
277 | // S ave the jo b executio n at the b eginning o f the step . | |
278 | // T he executi on context will be u sed to set key value s as data is | |
279 | // p rocessed. | |
280 | jobE xecution = stepExecu tion.getJo bExecution (); | |
281 | } | |
282 | ||
283 | @O verride | |
284 | pu blic ExitS tatus afte rStep(Step Execution stepExecut ion) { | |
285 | // D o not need to do any thing afte r step. | |
286 | retu rn null; | |
287 | } | |
288 | ||
289 | /* * | |
290 | * Forcefull y stop the job proce ssing beca use a fail ure was de tected. | |
291 | * | |
292 | * @param st atus | |
293 | * The stat us for the failure. | |
294 | * @param me ssage | |
295 | * The mess age associ ated with the status failure. | |
296 | * / | |
297 | pr ivate void stopJob(f inal Strin g status, final Stri ng message ) { | |
298 | ||
299 | // S et the fla g to indic ate the jo b has been forcefull y stopped. | |
300 | forc eStop = tr ue; | |
301 | ||
302 | // L og message . | |
303 | writ erLogger.e rror("Writ er executi on encount ered unrec overable e rror and f orced stop "); | |
304 | ||
305 | // S et failure and messa ge. | |
306 | setF ailureStat us(status) ; | |
307 | ||
308 | // S et failure message. | |
309 | setF ailureMess age(messag e); | |
310 | } | |
311 | ||
312 | /* * | |
313 | * Set the f ailure in the job ex ecution co ntext. | |
314 | * | |
315 | * @param st atus | |
316 | * The fail ure status . | |
317 | * / | |
318 | pr ivate void setFailur eStatus(fi nal String status) { | |
319 | ||
320 | // L og job fai lure statu s. | |
321 | writ erLogger.e rror("Job failed wit h status: " + status ); | |
322 | ||
323 | // S et job fai lure. | |
324 | jobE xecution.g etExecutio nContext() .putString (JOB_FAILU RE_KEY, st atus); | |
325 | } | |
326 | ||
327 | /* * | |
328 | * Set the f ailure mes sage in th e job exec ution cont ext. | |
329 | * | |
330 | * @param me ssage | |
331 | * The mess age to ass ociate wit h the erro r status. | |
332 | * / | |
333 | pr ivate void setFailur eMessage(f inal Strin g message) { | |
334 | ||
335 | // L og job fai lure messa ge. | |
336 | writ erLogger.e rror("Job failure me ssage: " + message); | |
337 | ||
338 | // S et job fai lure messa ge. | |
339 | jobE xecution.g etExecutio nContext() .putString (JOB_FAILU RE_MESSAGE _KEY, mess age); | |
340 | } | |
341 | ||
342 | } |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.