Produced by Araxis Merge on 12/20/2017 5:56: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 | Genisis_2.0_v6_bld6.zip\Source Code\TS\Service\term-service\src\main\java\gov\va\genisis2\ts\service\impl | UpdateTripleService.java | Thu Dec 14 19:57:20 2017 UTC |
| 2 | Genisis_2.0_v6_bld6.zip\Source Code\TS\Service\term-service\src\main\java\gov\va\genisis2\ts\service\impl | UpdateTripleService.java | Wed Dec 20 20:38:39 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 3 | 702 |
| Changed | 2 | 4 |
| 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.genis is2.ts.ser vice.impl; | |
| 2 | ||
| 3 | import jav a.util.Arr ayList; | |
| 4 | import jav a.util.Lis t; | |
| 5 | import jav a.util.str eam.Collec tors; | |
| 6 | ||
| 7 | import org .apache.je na.update. UpdateFact ory; | |
| 8 | import org .apache.je na.update. UpdateRequ est; | |
| 9 | import org .apache.lo gging.log4 j.LogManag er; | |
| 10 | import org .apache.lo gging.log4 j.Logger; | |
| 11 | import org .springfra mework.bea ns.factory .annotatio n.Autowire d; | |
| 12 | import org .springfra mework.ste reotype.Se rvice; | |
| 13 | ||
| 14 | import gov .va.genisi s2.ts.comm on.dto.Tri pleDTO; | |
| 15 | import gov .va.genisi s2.ts.comm on.dto.Upd ateRespons eDTO; | |
| 16 | import gov .va.genisi s2.ts.comm on.dto.Upd ateTripleD TO; | |
| 17 | import gov .va.genisi s2.ts.conv erter.Upda teResponse Converter; | |
| 18 | import gov .va.genisi s2.ts.fuse ki.FusekiC lient; | |
| 19 | import gov .va.genisi s2.ts.serv ice.IUpdat eTripleSer vice; | |
| 20 | import gov .va.genisi s2.ts.util s.TSProper tiesUtil; | |
| 21 | ||
| 22 | @Service | |
| 23 | public cla ss UpdateT ripleServi ce impleme nts IUpdat eTripleSer vice { | |
| 24 | ||
| 25 | pr ivate stat ic final L ogger LOGG ER = LogMa nager.getL ogger(Upda teTripleSe rvice.clas s); | |
| 26 | @A utowired | |
| 27 | pr ivate Fuse kiClient f usekiClien t; | |
| 28 | ||
| 29 | @A utowired | |
| 30 | pr ivate TSPr opertiesUt il propsUt il; | |
| 31 | ||
| 32 | @A utowired | |
| 33 | pr ivate Upda teResponse Converter updateResp onseConvet er; | |
| 34 | ||
| 35 | @A utowired | |
| 36 | pr ivate MvpU riGenHelpe r mvpUriGe nHelper; | |
| 37 | ||
| 38 | pr ivate stat ic final S tring MVP_ PREFIX = " http:// URL "; | |
| 39 | ||
| 40 | pr ivate stat ic final S tring MVP_ PREFIX_BRA CKET = "<h ttp:// URL "; | |
| 41 | ||
| 42 | pr ivate stat ic final S tring INSE RT = "INSE RT DATA"; | |
| 43 | ||
| 44 | pr ivate stat ic final S tring DELE TE = "DELE TE DATA"; | |
| 45 | ||
| 46 | pu blic List< UpdateResp onseDTO> d eleteTripl e(UpdateTr ipleDTO up dateTriple DTO) { | |
| 47 | retu rn updateT riple(DELE TE, update TripleDTO) ; | |
| 48 | } | |
| 49 | ||
| 50 | pu blic List< UpdateResp onseDTO> a ddTriple(U pdateTripl eDTO updat eTripleDTO ) { | |
| 51 | retu rn updateT riple(INSE RT, update TripleDTO) ; | |
| 52 | } | |
| 53 | ||
| 54 | pu blic List< UpdateResp onseDTO> u pdateTripl e(String c ommand, Up dateTriple DTO update TripleDTO) { | |
| 55 | ||
| 56 | // r emoving al l triples that have invalid pr efixes in all cases | |
| 57 | ||
| 58 | // T ODO: dropp ing triple s that hav e no prefi x...it may be best t o handle t his | |
| 59 | // f ilter | |
| 60 | // i n the erro r processi ng logic f or each ca se | |
| 61 | List <TripleDTO > mvpTripl es = valid ateTriples ForPrefix( updateTrip leDTO.getP roperties( )); | |
| 62 | ||
| 63 | // p opulate UR Is for new concepts | |
| 64 | mvpU riGenHelpe r.populate UrisForNew Concepts(m vpTriples) ; | |
| 65 | ||
| 66 | upda teTripleDT O.setPrope rties(mvpT riples); | |
| 67 | ||
| 68 | List <UpdateRes ponse> que ryResponse s = create QueriesAnd Response(c ommand, up dateTriple DTO); | |
| 69 | ||
| 70 | if ( updateTrip leDTO.isAl lowErrors( )) { | |
| 71 | ||
| 72 | if (co mmand.equa lsIgnoreCa se(INSERT) ) { | |
| 73 | List<Upd ateRespons eDTO> dtoR esponses = allowErro rs(queryRe sponses).s tream().ma p(dto -> u pdateRespo nseConvete r.convert( dto)).coll ect(Collec tors.toLis t()); | |
| 74 | ||
| 75 | return d toResponse s; | |
| 76 | } else { | |
| 77 | List<Upd ateRespons eDTO> dtoR esponses = allowErro rsDelete(q ueryRespon ses).strea m().map(dt o -> updat eResponseC onveter.co nvert(dto) ).collect( Collectors .toList()) ; | |
| 78 | return d toResponse s; | |
| 79 | } | |
| 80 | } el se if (upd ateTripleD TO.isCheck Only()) { | |
| 81 | // che ck and log for excet ions as re quests are processed ..do not e xecute | |
| 82 | List<U pdateRespo nseDTO> dt oResponses = check(q ueryRespon ses).strea m().map(dt o -> updat eResponseC onveter.co nvert(dto) ).collect( Collectors .toList()) ; | |
| 83 | ||
| 84 | return dtoRespon ses; | |
| 85 | ||
| 86 | } el se if (upd ateTripleD TO.isNoErr ors()) { | |
| 87 | if (co mmand.equa lsIgnoreCa se(INSERT) ) { | |
| 88 | List<Upd ateRespons eDTO> dtoR esponses = noErrors( queryRespo nses).stre am().map(d to -> upda teResponse Conveter.c onvert(dto )).collect (Collector s.toList() ); | |
| 89 | ||
| 90 | return d toResponse s; | |
| 91 | } else { | |
| 92 | List<Upd ateRespons eDTO> dtoR esponses = noErrorsD elete(quer yResponses ).stream() .map(dto - > updateRe sponseConv eter.conve rt(dto)).c ollect(Col lectors.to List()); | |
| 93 | ||
| 94 | return d toResponse s; | |
| 95 | } | |
| 96 | } el se { | |
| 97 | // fal l back to ignore err ors case | |
| 98 | List<U pdateRespo nseDTO> dt oResponses = ignoreE rrors(quer yResponses ).stream() .map(dto - > updateRe sponseConv eter.conve rt(dto)).c ollect(Col lectors.to List()); | |
| 99 | ||
| 100 | return dtoRespon ses; | |
| 101 | } | |
| 102 | } | |
| 103 | ||
| 104 | /* * | |
| 105 | * Checks fo r valid pr efixes. If a triple does not h ave the co rrect mvp pattern | |
| 106 | * it is rem oved | |
| 107 | * | |
| 108 | * @param pr operties | |
| 109 | * @return | |
| 110 | * / | |
| 111 | pr ivate List <TripleDTO > validate TriplesFor Prefix(Lis t<TripleDT O> propert ies) { | |
| 112 | ||
| 113 | List <TripleDTO > newTripl es = prope rties.stre am() | |
| 114 | .filter( t -> (t.ge tS().start sWith(MVP_ PREFIX) || t.getS(). startsWith (MVP_PREFI X_BRACKET) ) || (t.ge tP().start sWith(MVP_ PREFIX) || t.getP(). startsWith (MVP_PREFI X_BRACKET) )) | |
| 115 | .collect (Collector s.toList() ); | |
| 116 | retu rn newTrip les; | |
| 117 | } | |
| 118 | ||
| 119 | /* * | |
| 120 | * Creates m ulitple qu ery reques ts from dt o informat ion | |
| 121 | * | |
| 122 | * @param co mmand | |
| 123 | * @param dt o | |
| 124 | * @return | |
| 125 | * / | |
| 126 | pu blic Updat eRequest c onvertDTOt oRequests( String com mand, Upda teTripleDT O dto) { | |
| 127 | Upda teRequest request = UpdateFact ory.create (); | |
| 128 | ||
| 129 | for (TripleDTO prop : dt o.getPrope rties()) { | |
| 130 | String query = c ommand + " { " + quo teUri(prop .getS()) + " " + quo teUri(prop .getP()) + " " + quo teLiteral( prop.getO( )) + ";}"; | |
| 131 | reques t.add(quer y); | |
| 132 | } | |
| 133 | retu rn request ; | |
| 134 | } | |
| 135 | ||
| 136 | /* * | |
| 137 | * Format qu eries from DTO objec t into que ry form | |
| 138 | * | |
| 139 | * @param co mmand | |
| 140 | * @param dt o | |
| 141 | * @return | |
| 142 | * / | |
| 143 | pu blic List< String> cr eateQuerie s(String c ommand, Up dateTriple DTO dto) { | |
| 144 | List <String> q ueries = n ew ArrayLi st<String> (); | |
| 145 | ||
| 146 | for (TripleDTO prop : dt o.getPrope rties()) { | |
| 147 | querie s.add(comm and + " { " + quoteU ri(prop.ge tS()) + " " + quoteU ri(prop.ge tP()) + " " + quoteL iteral(pro p.getO()) + ";}"); | |
| 148 | } | |
| 149 | retu rn queries ; | |
| 150 | } | |
| 151 | ||
| 152 | pu blic List< UpdateResp onse> chec k(List<Upd ateRespons e> queryRe sponse) { | |
| 153 | // c heck prefi x | |
| 154 | Upda teRequest request = UpdateFact ory.create (); | |
| 155 | ||
| 156 | for (UpdateRes ponse qr : queryResp onse) { | |
| 157 | try { | |
| 158 | request. add(qr.get Query()); | |
| 159 | // since this call goes over the netwo rk would o nly like t o execute | |
| 160 | // if th e query is syntatica lly correc t | |
| 161 | if (fuse kiClient.p erformAsk( qr.getAskF orm(), pro psUtil.get SparqlEndp oint())) { | |
| 162 | // duplica te query! | |
| 163 | qr.setDupl icate(true ); | |
| 164 | } | |
| 165 | } catc h (Excepti on e) { | |
| 166 | qr.setEr ror(e.getM essage()); | |
| 167 | } | |
| 168 | } | |
| 169 | retu rn queryRe sponse; | |
| 170 | } | |
| 171 | ||
| 172 | pu blic List< UpdateResp onse> noEr rors(List< UpdateResp onse> quer yResponse) { | |
| 173 | Upda teRequest request = UpdateFact ory.create (); | |
| 174 | ||
| 175 | bool ean errors = false; | |
| 176 | for (UpdateRes ponse qr : queryResp onse) { | |
| 177 | try { | |
| 178 | // check s to see i f there is a duplica te note th e endpoint used! | |
| 179 | if (fuse kiClient.p erformAsk( qr.getAskF orm(), pro psUtil.get SparqlEndp oint())) { | |
| 180 | // duplica te query! | |
| 181 | qr.setDupl icate(true ); | |
| 182 | errors = t rue; | |
| 183 | break; // no errors are allowe d so break out | |
| 184 | } | |
| 185 | request. add(qr.get Query()); | |
| 186 | } catc h (Excepti on e) { | |
| 187 | errors = true; | |
| 188 | qr.setEr ror(e.getM essage()); | |
| 189 | } | |
| 190 | } | |
| 191 | /// if there a re no erro rs then ex ecute them ! | |
| 192 | if ( errors) { | |
| 193 | return queryResp onse; | |
| 194 | } el se { | |
| 195 | try { | |
| 196 | fusekiCl ient.perfo rmUpdate(r equest, pr opsUtil.ge tSparqlEnd pointUpdat e()); | |
| 197 | } catc h (Excepti on e) { | |
| 198 | LOGGER.e rror("Erro r in execu tion updat e SPARQL q uery", e); | |
| 199 | throw e; | |
| 200 | } | |
| 201 | return queryResp onse; | |
| 202 | } | |
| 203 | } | |
| 204 | ||
| 205 | pu blic List< UpdateResp onse> noEr rorsDelete (List<Upda teResponse > queryRes ponse) { | |
| 206 | Upda teRequest request = UpdateFact ory.create (); | |
| 207 | ||
| 208 | bool ean errors = false; | |
| 209 | for (UpdateRes ponse qr : queryResp onse) { | |
| 210 | try { | |
| 211 | // check s to see i f there is not a dup licate not e the endp oint used! | |
| 212 | if (!fus ekiClient. performAsk (qr.getAsk Form(), pr opsUtil.ge tSparqlEnd point())) { | |
| 213 | // duplica te query! | |
| 214 | qr.setDupl icate(true ); | |
| 215 | errors = t rue; | |
| 216 | break; // no errors are allowe d so break out | |
| 217 | } | |
| 218 | request. add(qr.get Query()); | |
| 219 | } catc h (Excepti on e) { | |
| 220 | errors = true; | |
| 221 | qr.setEr ror(e.getM essage()); | |
| 222 | } | |
| 223 | } | |
| 224 | /// if there a re no erro rs then ex ecute them ! | |
| 225 | if ( errors) { | |
| 226 | return queryResp onse; | |
| 227 | } el se { | |
| 228 | try { | |
| 229 | fusekiCl ient.perfo rmUpdate(r equest, pr opsUtil.ge tSparqlEnd pointUpdat e()); | |
| 230 | } catc h (Excepti on e) { | |
| 231 | LOGGER.e rror("Erro r in execu tion updat e SPARQL q uery", e); | |
| 232 | throw e; | |
| 233 | } | |
| 234 | return queryResp onse; | |
| 235 | } | |
| 236 | } | |
| 237 | ||
| 238 | pu blic List< UpdateResp onse> allo wErrors(Li st<UpdateR esponse> q ueryRespon se) { | |
| 239 | Upda teRequest request = UpdateFact ory.create (); | |
| 240 | ||
| 241 | for (UpdateRes ponse qr : queryResp onse) { | |
| 242 | try { | |
| 243 | // check s to see i f there is a duplica te. Note t he endpoin t used! | |
| 244 | if (fuse kiClient.p erformAsk( qr.getAskF orm(), pro psUtil.get SparqlEndp oint())) { | |
| 245 | // duplica te query! | |
| 246 | qr.setDupl icate(true ); | |
| 247 | continue; // we do n ot want to add this request be cause it i s a duplic ate | |
| 248 | } | |
| 249 | request. add(qr.get Query()); | |
| 250 | } catc h (Excepti on e) { | |
| 251 | qr.setEr ror(e.getM essage()); | |
| 252 | } | |
| 253 | } | |
| 254 | ||
| 255 | try { | |
| 256 | fuseki Client.per formUpdate (request, propsUtil. getSparqlE ndpointUpd ate()); | |
| 257 | } ca tch (Excep tion e) { | |
| 258 | LOGGER .error("Er ror in exe cution upd ate SPARQL query", e ); | |
| 259 | throw e; | |
| 260 | } | |
| 261 | retu rn queryRe sponse; | |
| 262 | } | |
| 263 | ||
| 264 | pu blic List< UpdateResp onse> allo wErrorsDel ete(List<U pdateRespo nse> query Response) { | |
| 265 | Upda teRequest request = UpdateFact ory.create (); | |
| 266 | ||
| 267 | for (UpdateRes ponse qr : queryResp onse) { | |
| 268 | try { | |
| 269 | // check s to see i f there is not a dup licate not e the endp oint used! | |
| 270 | if (!fus ekiClient. performAsk (qr.getAsk Form(), pr opsUtil.ge tSparqlEnd point())) { | |
| 271 | // duplica te query! | |
| 272 | qr.setDupl icate(fals e); | |
| 273 | continue; // we do n ot want to add this request be cause it i s a duplic ate | |
| 274 | } | |
| 275 | request. add(qr.get Query()); | |
| 276 | } catc h (Excepti on e) { | |
| 277 | qr.setEr ror(e.getM essage() + "\n"); | |
| 278 | } | |
| 279 | } | |
| 280 | try { | |
| 281 | fuseki Client.per formUpdate (request, propsUtil. getSparqlE ndpointUpd ate()); | |
| 282 | ||
| 283 | } ca tch (Excep tion e) { | |
| 284 | LOGGER .error("Er ror in exe cution upd ate SPARQL query", e ); | |
| 285 | throw e; | |
| 286 | } | |
| 287 | retu rn queryRe sponse; | |
| 288 | } | |
| 289 | ||
| 290 | pu blic List< UpdateResp onse> igno reErrors(L ist<Update Response> queryRespo nse) { | |
| 291 | Upda teRequest request = UpdateFact ory.create (); | |
| 292 | ||
| 293 | for (UpdateRes ponse qr : queryResp onse) | |
| 294 | reques t.add(qr.g etQuery()) ; | |
| 295 | ||
| 296 | try { | |
| 297 | fuseki Client.per formUpdate (request, propsUtil. getSparqlE ndpointUpd ate()); | |
| 298 | ||
| 299 | } ca tch (Excep tion e) { | |
| 300 | LOGGER .error("Er ror in exe cution upd ate SPARQL query", e ); | |
| 301 | throw e; | |
| 302 | } | |
| 303 | retu rn queryRe sponse; | |
| 304 | } | |
| 305 | ||
| 306 | /* * | |
| 307 | * Create sp arql forme d queries for the gi ven triple s | |
| 308 | * | |
| 309 | * @param co mmand | |
| 310 | * @param dt o | |
| 311 | * @return | |
| 312 | * / | |
| 313 | pu blic List< UpdateResp onse> crea teQueriesA ndResponse (String co mmand, Upd ateTripleD TO dto) { | |
| 314 | List <UpdateRes ponse> que ries = new ArrayList <UpdateRes ponse>(); | |
| 315 | ||
| 316 | for (TripleDTO prop : dt o.getPrope rties()) { | |
| 317 | String askForm = "ASK { " + quoteUri (prop.getS ()) + " " + quoteUri (prop.getP ()) + " " + quoteLit eral(prop. getO()) + ";}"; | |
| 318 | String query = c ommand + " { " + quo teUri(prop .getS()) + " " + quo teUri(prop .getP()) + " " + quo teLiteral( prop.getO( )) + ";}"; | |
| 319 | ||
| 320 | querie s.add(new UpdateResp onse(query , "", askF orm)); | |
| 321 | } | |
| 322 | retu rn queries ; | |
| 323 | } | |
| 324 | ||
| 325 | /* * | |
| 326 | * Adds angu lar bracke ts to reso urces.. Th is is done because S PARQL need s | |
| 327 | * resource to be in a ngular bra ckets | |
| 328 | * | |
| 329 | * @param ur i | |
| 330 | * @return | |
| 331 | * / | |
| 332 | pu blic Strin g quoteUri (String ur i) { | |
| 333 | if ( uri.starts With("<")) { | |
| 334 | return uri; | |
| 335 | } | |
| 336 | ||
| 337 | retu rn "<" + u ri + ">"; | |
| 338 | } | |
| 339 | ||
| 340 | /* * | |
| 341 | * Adds doub le quotes to literal s.. This i s done bec ause SPARQ L needs li terals | |
| 342 | * to be dou ble quoted | |
| 343 | * | |
| 344 | * @param qu ery | |
| 345 | * @return | |
| 346 | * / | |
| 347 | pu blic Strin g quoteLit eral(Strin g query) { | |
| 348 | if ( query.star tsWith("<" )) { | |
| 349 | return query; | |
| 350 | } | |
| 351 | retu rn "\"" + query + "\ ""; | |
| 352 | } | |
| 353 | } |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.