| | 450 | |
|---|
| | 451 | /** |
|---|
| | 452 | * Static convenience method to get the ComplexObsHandler associated with a |
|---|
| | 453 | * complex Obs. Returns the ComplexObsHandler. Returns null if the |
|---|
| | 454 | * Obs.isComplexObs() is false or there is an error instantiating the |
|---|
| | 455 | * handler class. |
|---|
| | 456 | * |
|---|
| | 457 | * TODO: Should ComplexObsHandler be an abstract class and |
|---|
| | 458 | * put this method there instead? |
|---|
| | 459 | * |
|---|
| | 460 | * @param obs A complex Obs. |
|---|
| | 461 | * @return ComplexObsHandler for the complex Obs. or null on error. |
|---|
| | 462 | */ |
|---|
| | 463 | public ComplexObsHandler getHandler(Obs obs) { |
|---|
| | 464 | if (obs.getConcept().isComplex()) { |
|---|
| | 465 | return getHandler( ((ConceptComplex)obs.getConcept()).getHandler() ); |
|---|
| | 466 | } |
|---|
| | 467 | return null; |
|---|
| | 468 | |
|---|
| | 469 | /* |
|---|
| | 470 | String className = ((ConceptComplex) obs.getConcept()).getHandler(); |
|---|
| | 471 | ComplexObsHandler handler = null; |
|---|
| | 472 | try { |
|---|
| | 473 | Class c = OpenmrsClassLoader.getInstance().loadClass(className); |
|---|
| | 474 | handler = (ComplexObsHandler)c.newInstance(); |
|---|
| | 475 | //Class handlerClass = Class.forName(className); |
|---|
| | 476 | //handler = (ComplexObsHandler)className.getClass().newInstance(); |
|---|
| | 477 | //handler = (ComplexObsHandler) handlerClass.newInstance(); |
|---|
| | 478 | } catch (ClassNotFoundException cnfe) { |
|---|
| | 479 | return null; |
|---|
| | 480 | } catch (InstantiationException ie) { |
|---|
| | 481 | return null; |
|---|
| | 482 | } catch (IllegalAccessException iae) { |
|---|
| | 483 | return null; |
|---|
| | 484 | } |
|---|
| | 485 | return handler; |
|---|
| | 486 | */ |
|---|
| | 487 | } |
|---|
| | 488 | |
|---|
| | 489 | /** |
|---|
| | 490 | * @see org.openmrs.api.ObsService#getHandler(java.lang.Class) |
|---|
| | 491 | */ |
|---|
| | 492 | public ComplexObsHandler getHandler(Class<? extends ComplexObsHandler> clazz) { |
|---|
| | 493 | try { |
|---|
| | 494 | return handlers.get(clazz); |
|---|
| | 495 | } catch (Exception ex) { |
|---|
| | 496 | log.error("Failed to get report renderer for " + clazz, ex); |
|---|
| | 497 | return null; |
|---|
| | 498 | } |
|---|
| | 499 | } |
|---|
| | 500 | |
|---|
| | 501 | public List<ComplexObsHandler> getComplexObsHandlers() { |
|---|
| | 502 | //List handlers = new ArrayList<ComplexObsHandler>(); |
|---|
| | 503 | //handlers.addAll(getHandlers().values()); |
|---|
| | 504 | return new ArrayList<ComplexObsHandler>(getHandlers().values()); |
|---|
| | 505 | //return handlers; |
|---|
| | 506 | } |
|---|
| | 507 | |
|---|
| | 508 | |
|---|
| | 509 | /** |
|---|
| | 510 | * @see org.openmrs.api.ObsService#getHandler(java.lang.String) |
|---|
| | 511 | */ |
|---|
| | 512 | public ComplexObsHandler getHandler(String className) { |
|---|
| | 513 | try { |
|---|
| | 514 | return handlers.get(OpenmrsClassLoader.getInstance().loadClass(className)); |
|---|
| | 515 | } catch (Exception ex) { |
|---|
| | 516 | log.error("Failed to get report renderer for " + className, ex); |
|---|
| | 517 | return null; |
|---|
| | 518 | } |
|---|
| | 519 | } |
|---|
| | 520 | |
|---|
| | 521 | /** |
|---|
| | 522 | * ADDs handlers...doesn't replace them. |
|---|
| | 523 | * @see org.openmrs.api.ObsService#setHandlers(Map) |
|---|
| | 524 | */ |
|---|
| | 525 | public void setHandlers(Map<Class<? extends ComplexObsHandler>, ComplexObsHandler> newHandlers) throws APIException { |
|---|
| | 526 | for (Map.Entry<Class<? extends ComplexObsHandler>, ComplexObsHandler> entry : newHandlers.entrySet()) { |
|---|
| | 527 | registerHandler(entry.getKey(), entry.getValue()); |
|---|
| | 528 | } |
|---|
| | 529 | } |
|---|
| | 530 | |
|---|
| | 531 | /** |
|---|
| | 532 | * @see org.openmrs.api.ObsService#getHandlers() |
|---|
| | 533 | */ |
|---|
| | 534 | public Map<Class<? extends ComplexObsHandler>, ComplexObsHandler> getHandlers() throws APIException { |
|---|
| | 535 | if (handlers == null) |
|---|
| | 536 | handlers = new LinkedHashMap<Class<? extends ComplexObsHandler>, ComplexObsHandler>(); |
|---|
| | 537 | |
|---|
| | 538 | return handlers; |
|---|
| | 539 | } |
|---|
| | 540 | |
|---|
| | 541 | /** |
|---|
| | 542 | * @see org.openmrs.api.ObsService#registerHandler(java.lang.Class, org.openmrs.obs.ComplexObsHandler) |
|---|
| | 543 | */ |
|---|
| | 544 | public void registerHandler(Class<? extends ComplexObsHandler> handlerClass, ComplexObsHandler handler) throws APIException { |
|---|
| | 545 | getHandlers().put(handlerClass, handler); |
|---|
| | 546 | } |
|---|
| | 547 | |
|---|
| | 548 | /** |
|---|
| | 549 | * @see org.openmrs.api.ObsService#registerHandler(java.lang.String) |
|---|
| | 550 | */ |
|---|
| | 551 | @SuppressWarnings("unchecked") |
|---|
| | 552 | public void registerHandler(String handlerClass) throws APIException { |
|---|
| | 553 | try { |
|---|
| | 554 | Class loadedClass = OpenmrsClassLoader.getInstance().loadClass(handlerClass); |
|---|
| | 555 | registerHandler(loadedClass, (ComplexObsHandler)loadedClass.newInstance()); |
|---|
| | 556 | |
|---|
| | 557 | } catch (Exception e) { |
|---|
| | 558 | throw new APIException("Unable to load and instantiate handler", e); |
|---|
| | 559 | } |
|---|
| | 560 | } |
|---|
| | 561 | |
|---|
| | 562 | /** |
|---|
| | 563 | * @see org.openmrs.api.ObsService#removeHandler(java.lang.Class) |
|---|
| | 564 | */ |
|---|
| | 565 | public void removeHandler(Class<? extends ComplexObsHandler> renderingClass) { |
|---|
| | 566 | handlers.remove(renderingClass); |
|---|
| | 567 | } |
|---|