1 23 24 package org.objectweb.medor.eval.lib; 25 26 import org.objectweb.jorm.api.PExceptionIO; 27 import org.objectweb.jorm.api.PException; 28 import org.objectweb.jorm.naming.api.PNameGetter; 29 import org.objectweb.jorm.naming.api.PNameCoder; 30 import org.objectweb.jorm.type.api.PExceptionTyping; 31 import org.objectweb.medor.api.MedorException; 32 import org.objectweb.medor.tuple.api.Tuple; 33 import org.objectweb.medor.expression.api.Operand; 34 import org.objectweb.util.monolog.api.Logger; 35 import org.objectweb.util.monolog.api.BasicLevel; 36 37 import java.util.Date ; 38 import java.util.Map ; 39 import java.util.HashMap ; 40 import java.math.BigDecimal ; 41 import java.math.BigInteger ; 42 43 55 public class TuplePNameGetter implements PNameGetter { 57 58 66 private Map fields; 67 68 private PNameGetter png; 69 70 private Operand pnc; 71 72 transient private Logger logger; 73 74 public TuplePNameGetter(TuplePNameGetter tpng) { 75 this.logger = tpng.logger; 76 this.fields = new HashMap (tpng.fields); 77 this.logger = tpng.logger; 78 this.pnc = tpng.pnc; 79 } 80 86 public TuplePNameGetter(Map fieldName2index, Operand nc, Logger logger) { 87 this.logger = logger; 88 this.fields = fieldName2index; 89 pnc = nc; 90 } 91 92 97 public void setMap(Map fieldName2index) { 98 this.fields = fieldName2index; 99 } 100 101 114 public byte[] pngetByteArrayField(String fn, Object context) 115 throws PException { 116 Tuple currentTuple = (Tuple) context; 117 if (currentTuple == null) { 118 throw new PExceptionTyping("The context must be the tuple: " + context); 119 } 120 try { 121 Integer in = (Integer ) fields.get(fn); 122 if (in == null) 123 throw new PExceptionTyping("Field " + fn + " has not been found"); 124 return currentTuple.getByteArray(in.intValue()); 125 } 126 catch (MedorException e) { 127 throw new PException(e.getMessage()); 128 } 129 } 130 131 143 public byte pngetByteField(String fn, Object context) 144 throws PException { 145 Tuple currentTuple = (Tuple) context; 146 if (currentTuple == null) { 147 throw new PExceptionTyping("The context must be the tuple: " + context); 148 } 149 try { 150 Integer in = (Integer ) fields.get(fn); 151 if (in == null) 152 throw new PExceptionTyping("Field " + fn + " has not been found"); 153 if (currentTuple.isDefined(in.intValue())) { 154 return currentTuple.getByte(in.intValue()); 155 } else { 156 if (png == null) { 157 png = (PNameGetter) 158 ((PNameCoder)pnc.getObject()).getNull().encodeAbstract(); 159 } 160 return png.pngetByteField(fn, null); 161 } 162 } 163 catch (MedorException e) { 164 throw new PException(e.getMessage()); 165 } 166 } 167 168 180 public char pngetCharField(String fn, Object context) 181 throws PException { 182 Tuple currentTuple = (Tuple) context; 183 if (currentTuple == null) { 184 throw new PExceptionTyping("The context must be the tuple: " + context); 185 } 186 try { 187 Integer in = (Integer ) fields.get(fn); 188 if (in == null) 189 throw new PExceptionTyping("Field " + fn + " has not been found"); 190 if (currentTuple.isDefined(in.intValue())) { 191 return currentTuple.getChar(in.intValue()); 192 } else { 193 return png.pngetCharField(fn, null); 194 } 195 } 196 catch (MedorException e) { 197 throw new PException(e.getMessage()); 198 } 199 } 200 201 213 public char[] pngetCharArrayField(String fn, Object context) 214 throws PException { 215 Tuple currentTuple = (Tuple) context; 216 if (currentTuple == null) { 217 throw new PExceptionTyping("The context must be the tuple: " + context); 218 } 219 try { 220 Integer in = (Integer ) fields.get(fn); 221 if (in == null) 222 throw new PExceptionTyping("Field " + fn + " has not been found"); 223 return currentTuple.getCharArray(in.intValue()); 224 } 225 catch (MedorException e) { 226 throw new PException(e.getMessage()); 227 } 228 } 229 230 242 public short pngetShortField(String fn, Object context) 243 throws PException { 244 Tuple currentTuple = (Tuple) context; 245 if (currentTuple == null) { 246 throw new PExceptionTyping("The context must be the tuple: " + context); 247 } 248 try { 249 Integer in = (Integer ) fields.get(fn); 250 if (in == null) 251 throw new PExceptionTyping("Field " + fn + " has not been found"); 252 if (currentTuple.isDefined(in.intValue())) { 253 return currentTuple.getShort(in.intValue()); 254 } else { 255 if (png == null) { 256 png = (PNameGetter) 257 ((PNameCoder)pnc.getObject()).getNull().encodeAbstract(); 258 } 259 return png.pngetShortField(fn, null); 260 } 261 } 262 catch (MedorException e) { 263 throw new PException(e.getMessage()); 264 } 265 } 266 267 278 public Byte pngetObyteField(String fn, Object ctxt) 279 throws PException { 280 Tuple currentTuple = (Tuple) ctxt; 281 if (currentTuple == null) { 282 throw new PExceptionTyping("The context must be the tuple: " + ctxt); 283 } 284 try { 285 Integer in = (Integer ) fields.get(fn); 286 if (in == null) 287 throw new PExceptionTyping("Field " + fn + " has not been found"); 288 return new Byte (currentTuple.getByte(in.intValue())); 289 } 290 catch (MedorException e) { 291 throw new PException(e.getMessage()); 292 } 293 294 } 295 296 307 public Character pngetOcharField(String fn, Object ctxt) 308 throws PException { 309 Tuple currentTuple = (Tuple) ctxt; 310 if (currentTuple == null) { 311 throw new PExceptionTyping("The context must be the tuple: " + ctxt); 312 } 313 try { 314 Integer in = (Integer ) fields.get(fn); 315 if (in == null) 316 throw new PExceptionTyping("Field " + fn + " has not been found"); 317 return new Character (currentTuple.getChar(in.intValue())); 318 } 319 catch (MedorException e) { 320 throw new PException(e.getMessage()); 321 } 322 } 323 324 335 public Short pngetOshortField(String fn, Object ctxt) 336 throws PException { 337 Tuple currentTuple = (Tuple) ctxt; 338 if (currentTuple == null) { 339 throw new PExceptionTyping("The context must be the tuple: " + ctxt); 340 } 341 try { 342 Integer in = (Integer ) fields.get(fn); 343 if (in == null) 344 throw new PExceptionTyping("Field " + fn + " has not been found"); 345 return new Short (currentTuple.getShort(in.intValue())); 346 } 347 catch (MedorException e) { 348 throw new PException(e.getMessage()); 349 } 350 } 351 352 363 public Integer pngetOintField(String fn, Object ctxt) 364 throws PException { 365 Tuple currentTuple = (Tuple) ctxt; 366 if (currentTuple == null) { 367 throw new PExceptionTyping("The context must be the tuple: " + ctxt); 368 } 369 try { 370 Integer in = (Integer ) fields.get(fn); 371 if (in == null) 372 throw new PExceptionTyping("Field " + fn + " has not been found"); 373 return new Integer (currentTuple.getInt(in.intValue())); 374 } 375 catch (MedorException e) { 376 throw new PException(e.getMessage()); 377 } 378 } 379 380 391 public Long pngetOlongField(String fn, Object ctxt) 392 throws PExceptionTyping, PExceptionIO { 393 Tuple currentTuple = (Tuple) ctxt; 394 if (currentTuple == null) { 395 throw new PExceptionTyping("The context must be the tuple: " + ctxt); 396 } 397 try { 398 Integer in = (Integer ) fields.get(fn); 399 if (in == null) 400 throw new PExceptionTyping("Field " + fn + " has not been found"); 401 return new Long (currentTuple.getLong(in.intValue())); 402 } 403 catch (MedorException e) { 404 throw new PExceptionTyping(e.getMessage()); 405 } 406 } 407 408 419 public Date pngetDateField(String fn, Object ctxt) 420 throws PExceptionTyping, PExceptionIO { 421 Tuple currentTuple = (Tuple) ctxt; 422 if (currentTuple == null) { 423 throw new PExceptionTyping("The context must be the tuple: " + ctxt); 424 } 425 try { 426 Integer in = (Integer ) fields.get(fn); 427 if (in == null) 428 throw new PExceptionTyping("Field " + fn + " has not been found"); 429 return currentTuple.getDate(in.intValue()); 430 } 431 catch (MedorException e) { 432 throw new PExceptionTyping(e.getMessage()); 433 } 434 } 435 436 448 public int pngetIntField(String fn, Object context) 449 throws PException { 450 Tuple currentTuple = (Tuple) context; 451 if (currentTuple == null) { 452 throw new PExceptionTyping("The context must be the tuple: " + context); 453 } 454 try { 455 Integer in = (Integer ) fields.get(fn); 456 if (in == null) 457 throw new PExceptionTyping("Field " + fn + " has not been found"); 458 if (currentTuple.isDefined(in.intValue())) { 459 return currentTuple.getInt(in.intValue()); 460 } else { 461 if (png == null) { 462 png = (PNameGetter) 463 ((PNameCoder)pnc.getObject()).getNull().encodeAbstract(); 464 } 465 return png.pngetIntField(fn, null); 466 } 467 } 468 catch (MedorException e) { 469 throw new PException(e.getMessage()); 470 } 471 } 472 473 485 public BigDecimal pngetBigDecimalField(String fn, Object context) 486 throws PException { 487 Tuple currentTuple = (Tuple) context; 488 if (currentTuple == null) { 489 throw new PExceptionTyping("The context must be the tuple: " + context); 490 } 491 try { 492 Integer in = (Integer ) fields.get(fn); 493 if (in == null) 494 throw new PExceptionTyping("Field " + fn + " has not been found"); 495 return currentTuple.getBigDecimal(in.intValue()); 496 } catch (MedorException e) { 497 throw new PException(e.getMessage()); 498 } 499 } 500 501 513 public BigInteger pngetBigIntegerField(String fn, Object context) 514 throws PException { 515 Tuple currentTuple = (Tuple) context; 516 if (currentTuple == null) { 517 throw new PExceptionTyping("The context must be the tuple: " + context); 518 } 519 try { 520 Integer in = (Integer ) fields.get(fn); 521 if (in == null) 522 throw new PExceptionTyping("Field " + fn + " has not been found"); 523 return currentTuple.getBigInteger(in.intValue()); 524 } 525 catch (MedorException e) { 526 throw new PException(e.getMessage()); 527 } 528 } 529 530 542 public long pngetLongField(String fn, Object context) 543 throws PException { 544 Tuple currentTuple = (Tuple) context; 545 if (currentTuple == null) { 546 throw new PExceptionTyping("The context must be the tuple: " + context); 547 } 548 try { 549 Integer in = (Integer ) fields.get(fn); 550 if (in == null) 551 throw new PExceptionTyping("Field " + fn + " has not been found"); 552 if (currentTuple.isDefined(in.intValue())) { 553 long res = currentTuple.getLong(in.intValue()); 554 if (logger.isLoggable(BasicLevel.DEBUG)) { 555 logger.log(BasicLevel.DEBUG, "PName field " + fn + ", value=" + res); 556 } 557 return res; 558 } else { 559 if (png == null) { 560 png = (PNameGetter) 561 ((PNameCoder)pnc.getObject()).getNull().encodeAbstract(); 562 } 563 return png.pngetLongField(fn, null); 564 } 565 } 566 catch (MedorException e) { 567 throw new PException(e.getMessage()); 568 } 569 } 570 571 583 public String pngetStringField(String fn, Object context) 584 throws PExceptionTyping, PExceptionIO { 585 Tuple currentTuple = (Tuple) context; 586 if (currentTuple == null) { 587 throw new PExceptionTyping("The context must be the tuple: " + context); 588 } 589 try { 590 Integer in = (Integer ) fields.get(fn); 591 if (in == null) 592 throw new PExceptionTyping("Field " + fn + " has not been found"); 593 return currentTuple.getString(in.intValue()); 594 } 595 catch (MedorException e) { 596 throw new PExceptionTyping(e.getMessage()); 597 } 598 } 599 } 600 | Popular Tags |