- Timestamp:
- 05/19/08 23:47:49 (6 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openmrs/branches/complex-obs/src/api/org/openmrs/api/impl/ObsServiceImpl.java
r4239 r4265 62 62 * Report handlers that have been registered. 63 63 * 64 * This is filled via {@link #setHandlers(Map)} and spring's applicationContext-service.xml object 64 * This is filled via {@link #setHandlers(Map)} and spring's 65 * applicationContext-service.xml object 65 66 */ 66 67 private static Map<String, ComplexObsHandler> handlers = null; 67 68 68 69 69 public ObsServiceImpl() { 70 70 } … … 78 78 if (!Context.hasPrivilege(OpenmrsConstants.PRIV_VIEW_OBS)) 79 79 throw new APIAuthenticationException("Privilege required: " 80 + OpenmrsConstants.PRIV_VIEW_OBS);80 + OpenmrsConstants.PRIV_VIEW_OBS); 81 81 82 82 return dao; … … 99 99 } 100 100 setRequiredObsProperties(obs); 101 101 102 102 getObsDAO().createObs(obs); 103 103 } 104 104 105 105 /** 106 106 * Sets the creator and dateCreated properties on the Obs object … … 114 114 if (obs.getDateCreated() == null) 115 115 obs.setDateCreated(new Date()); 116 116 117 117 if (obs.getGroupMembers() != null) { 118 118 for (Obs member : obs.getGroupMembers()) { 119 119 // if statement does a quick sanity check to 120 120 // avoid the simplest of infinite loops 121 if (member.getCreator() == null ||122 member.getDateCreated() == null)123 setRequiredObsProperties(member);121 if (member.getCreator() == null 122 || member.getDateCreated() == null) 123 setRequiredObsProperties(member); 124 124 } 125 125 } … … 129 129 * 130 130 * Correct use case: 131 * 131 132 * <pre> 132 133 * Obs parent = new Obs(); … … 138 139 * </pre> 139 140 * 140 * @deprecated This method should no longer need to be called on the api. This 141 * was meant as temporary until we created a true ObsGroup pojo. 141 * @deprecated This method should no longer need to be called on the api. 142 * This was meant as temporary until we created a true ObsGroup 143 * pojo. 142 144 * 143 145 * @see org.openmrs.api.ObsService#createObsGroup(org.openmrs.Obs[]) … … 146 148 if (obs == null || obs.length < 1) 147 149 return; // silently tolerate calls with missing/empty parameter 148 149 String conceptIdStr = Context.getAdministrationService(). 150 getGlobalProperty( 151 OpenmrsConstants.GLOBAL_PROPERTY_MEDICAL_RECORD_OBSERVATIONS, 152 "1238"); 150 151 String conceptIdStr = Context.getAdministrationService() 152 .getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_MEDICAL_RECORD_OBSERVATIONS, 153 "1238"); 153 154 // fail silently if a default obs group is not defined 154 155 if (conceptIdStr == null || conceptIdStr.length() == 0) 155 156 return; 156 157 157 158 Integer conceptId = Integer.valueOf(conceptIdStr); 158 Concept defaultObsGroupConcept = Context.getConceptService().getConcept(conceptId); 159 159 Concept defaultObsGroupConcept = Context.getConceptService() 160 .getConcept(conceptId); 161 160 162 // if they defined a bad concept, bail 161 163 if (defaultObsGroupConcept == null) 162 throw new APIException("There is no concept defined with concept id: " + conceptIdStr + 163 "You should correctly define the default obs group concept id with the global propery" + 164 OpenmrsConstants.GLOBAL_PROPERTY_MEDICAL_RECORD_OBSERVATIONS); 165 164 throw new APIException("There is no concept defined with concept id: " 165 + conceptIdStr 166 + "You should correctly define the default obs group concept id with the global propery" 167 + OpenmrsConstants.GLOBAL_PROPERTY_MEDICAL_RECORD_OBSERVATIONS); 168 166 169 Obs obsGroup = new Obs(); 167 170 obsGroup.setConcept(defaultObsGroupConcept); 168 171 169 172 for (Obs member : obs) { 170 obsGroup.addGroupMember(member);171 }172 173 updateObs(obsGroup);173 obsGroup.addGroupMember(member); 174 } 175 176 updateObs(obsGroup); 174 177 } 175 178 … … 180 183 return getObsDAO().getObs(obsId); 181 184 } 182 185 183 186 /** 184 187 * @see org.openmrs.api.ObsService#getComplexObs(java.lang.Integer) 185 188 */ 186 public Obs getComplexObs(Integer obsId ) throws APIException {189 public Obs getComplexObs(Integer obsId, String view) throws APIException { 187 190 Obs obs = getObsDAO().getObs(obsId); 188 191 if (obs.isComplex()) { 189 return getHandler(obs).getObs(obs );192 return getHandler(obs).getObs(obs, view); 190 193 } 191 194 return obs; 192 195 } 196 197 /** 198 * @see org.openmrs.api.ObsService#getAllComplexObs() 199 */ 200 public List<Obs> getAllComplexObs() throws APIException { 201 List<Obs> obs = getObsDAO().getAllComplexObs(); 202 return obs; 203 } 204 205 /** 206 * @see org.openmrs.api.ObsService#getComplexData(java.lang.Integer) 207 */ 208 public ComplexData getComplexData(Integer obsId, String view) 209 throws APIException { 210 return this.getComplexObs(obsId, view).getComplexData(); 211 } 212 213 /** 214 * @see org.openmrs.api.ObsService#getComplexData(org.openmrs.Obs) 215 */ 216 public ComplexData getComplexData(Obs obs, String view) throws APIException { 217 return this.getComplexObs(obs.getObsId(), view).getComplexData(); 218 } 193 219 194 220 /** 195 * @see org.openmrs.api.ObsService#getComplexData(java.lang.Integer) 196 */ 197 public ComplexData getComplexData(Integer obsId) throws APIException { 198 return this.getComplexObs(obsId).getComplexData(); 199 } 200 201 /** 202 * @see org.openmrs.api.ObsService#getComplexData(org.openmrs.Obs) 203 */ 204 public ComplexData getComplexData(Obs obs) throws APIException { 205 return this.getComplexObs(obs.getObsId()).getComplexData(); 206 } 207 221 * @see org.openmrs.api.ObsService#purgeComplexData(org.openmrs.Obs) 222 */ 223 public boolean purgeComplexData(Obs obs) throws APIException { 224 if (obs.isComplex()) { 225 ComplexObsHandler handler = this.getHandler(obs); 226 if (null != handler) { 227 return handler.purgeComplexData(obs); 228 } 229 } 230 return false; 231 } 232 208 233 /** 209 234 * @see org.openmrs.api.ObsService#updateObs(org.openmrs.Obs) 210 235 */ 211 236 public void updateObs(Obs obs) throws APIException { 237 if (obs.isComplex() && null != obs.getComplexData().getData()) { 238 ComplexObsHandler handler = getHandler(obs); 239 if (null != handler) { 240 handler.saveObs(obs); 241 } 242 } 212 243 if (obs.isVoided() && obs.getVoidedBy() == null) 213 244 voidObs(obs, obs.getVoidReason()); … … 226 257 * If the Obs argument is an obsGroup, all group members will be voided. 227 258 * 228 * @see org.openmrs.api.ObsService#voidObs(org.openmrs.Obs, java.lang.String) 259 * @see org.openmrs.api.ObsService#voidObs(org.openmrs.Obs, 260 * java.lang.String) 229 261 * @param Obs obs the Obs to void 230 262 * @param String reason the void reason 231 263 * @throws APIException 232 264 */ 233 265 234 266 public void voidObs(Obs obs, String reason) throws APIException { 235 267 Set<Obs> obsToVoid = new HashSet<Obs>(); … … 258 290 if (null != handler) { 259 291 handler.saveObs(obs); 260 } 292 } 261 293 getObsDAO().updateObs(obs); 262 294 } … … 265 297 * Unvoids an Obs 266 298 * 267 * If the Obs argument is an obsGroup, all group members 268 * with the samedateVoided will also be unvoided.299 * If the Obs argument is an obsGroup, all group members with the same 300 * dateVoided will also be unvoided. 269 301 * 270 302 * @see org.openmrs.api.ObsService#unvoidObs(org.openmrs.Obs) … … 303 335 */ 304 336 public void deleteObs(Obs obs) throws APIException { 337 this.purgeComplexData(obs); 305 338 getObsDAO().deleteObs(obs); 306 339 } 307 340 308 341 /** 309 342 * @see org.openmrs.api.ObsService#getMimeTypes() … … 321 354 322 355 /** 323 * @see org.openmrs.api.ObsService#getObservations(org.openmrs.Person, boolean includeVoided) 356 * @see org.openmrs.api.ObsService#getObservations(org.openmrs.Person, 357 * boolean includeVoided) 324 358 */ 325 359 public Set<Obs> getObservations(Person who, boolean includeVoided) { … … 328 362 329 363 /** 330 * @see org.openmrs.api.ObsService#getObservations(org.openmrs.Concept, org.openmrs.Location, java.lang.String, java.lang.Integer, boolean includeVoided) 331 */ 332 public List<Obs> getObservations(Concept c, Location loc, String sort, Integer personType, boolean includeVoided) { 364 * @see org.openmrs.api.ObsService#getObservations(org.openmrs.Concept, 365 * org.openmrs.Location, java.lang.String, java.lang.Integer, boolean 366 * includeVoided) 367 */ 368 public List<Obs> getObservations(Concept c, Location loc, String sort, 369 Integer personType, boolean includeVoided) { 333 370 return getObsDAO().getObservations(c, loc, sort, personType); 334 371 } 335 372 336 373 /** 337 * @see org.openmrs.api.ObsService#getObservations(org.openmrs.Person, org.openmrs.Concept, boolean includeVoided) 338 */ 339 public Set<Obs> getObservations(Person who, Concept question, boolean includeVoided) { 374 * @see org.openmrs.api.ObsService#getObservations(org.openmrs.Person, 375 * org.openmrs.Concept, boolean includeVoided) 376 */ 377 public Set<Obs> getObservations(Person who, Concept question, 378 boolean includeVoided) { 340 379 return getObsDAO().getObservations(who, question, includeVoided); 341 380 } 342 381 343 382 /** 344 * @see org.openmrs.api.ObsService#getLastNObservations(java.lang.Integer, org.openmrs.Person, org.openmrs.Concept, boolean includeVoided) 383 * @see org.openmrs.api.ObsService#getLastNObservations(java.lang.Integer, 384 * org.openmrs.Person, org.openmrs.Concept, boolean includeVoided) 345 385 */ 346 386 public List<Obs> getLastNObservations(Integer n, Person who, 347 Concept question, boolean includeVoided) {387 Concept question, boolean includeVoided) { 348 388 return getObsDAO().getLastNObservations(n, who, question); 349 389 } 350 390 351 391 /** 352 * @see org.openmrs.api.ObsService#getObservations(org.openmrs.Concept, java.lang.String, java.lang.Integer, boolean includeVoided) 353 */ 354 public List<Obs> getObservations(Concept question, String sort, Integer personType, boolean includeVoided) { 392 * @see org.openmrs.api.ObsService#getObservations(org.openmrs.Concept, 393 * java.lang.String, java.lang.Integer, boolean includeVoided) 394 */ 395 public List<Obs> getObservations(Concept question, String sort, 396 Integer personType, boolean includeVoided) { 355 397 return getObsDAO().getObservations(question, sort, personType); 356 398 } 357 358 /** 359 * @see org.openmrs.api.ObsService#getObservationsAnsweredByConcept(org.openmrs.Concept, java.lang.Integer, boolean includeVoided) 360 */ 361 public List<Obs> getObservationsAnsweredByConcept(Concept answer, Integer personType, boolean includeVoided) { 399 400 /** 401 * @see org.openmrs.api.ObsService#getObservationsAnsweredByConcept(org.openmrs.Concept, 402 * java.lang.Integer, boolean includeVoided) 403 */ 404 public List<Obs> getObservationsAnsweredByConcept(Concept answer, 405 Integer personType, boolean includeVoided) { 362 406 return getObsDAO().getObservationsAnsweredByConcept(answer, personType); 363 407 } 364 365 /** 366 * @see org.openmrs.api.ObsService#getNumericAnswersForConcept(org.openmrs.Concept, java.lang.Boolean, java.lang.Integer, boolean includeVoided) 367 */ 368 public List<Object[]> getNumericAnswersForConcept(Concept answer, Boolean sortByValue, Integer personType, boolean includeVoided) { 369 return getObsDAO().getNumericAnswersForConcept(answer, sortByValue, personType); 370 } 371 408 409 /** 410 * @see org.openmrs.api.ObsService#getNumericAnswersForConcept(org.openmrs.Concept, 411 * java.lang.Boolean, java.lang.Integer, boolean includeVoided) 412 */ 413 public List<Object[]> getNumericAnswersForConcept(Concept answer, 414 Boolean sortByValue, Integer personType, boolean includeVoided) { 415 return getObsDAO().getNumericAnswersForConcept(answer, 416 sortByValue, 417 personType); 418 } 372 419 373 420 /** … … 386 433 387 434 /** 388 * @see org.openmrs.api.ObsService#findObservations(java.lang.String, boolean, java.lang.Integer) 389 */ 390 public List<Obs> findObservations(String search, boolean includeVoided, Integer personType) { 435 * @see org.openmrs.api.ObsService#findObservations(java.lang.String, 436 * boolean, java.lang.Integer) 437 */ 438 public List<Obs> findObservations(String search, boolean includeVoided, 439 Integer personType) { 391 440 List<Obs> obs = new Vector<Obs>(); 392 for (Person p : Context.getPatientService() .getPatientsByIdentifier(393 search, includeVoided)) {441 for (Person p : Context.getPatientService() 442 .getPatientsByIdentifier(search, includeVoided)) { 394 443 obs.addAll(getObsDAO().findObservations(p.getPersonId(), 395 includeVoided, personType)); 444 includeVoided, 445 personType)); 396 446 } 397 447 try { 398 448 Integer i = Integer.valueOf(search); 399 449 if (i != null) 400 obs.addAll(getObsDAO().findObservations(i, includeVoided, personType)); 450 obs.addAll(getObsDAO().findObservations(i, 451 includeVoided, 452 personType)); 401 453 } catch (Exception e) { 402 454 } … … 406 458 407 459 /** 408 * @see org.openmrs.api.ObsService#getDistinctObservationValues(org.openmrs.Concept, java.lang.Integer) 409 */ 410 public List<String> getDistinctObservationValues(Concept question, Integer personType) { 460 * @see org.openmrs.api.ObsService#getDistinctObservationValues(org.openmrs.Concept, 461 * java.lang.Integer) 462 */ 463 public List<String> getDistinctObservationValues(Concept question, 464 Integer personType) { 411 465 // todo: make this efficient, and add a sort option 412 466 … … 428 482 429 483 /** 430 * @see org.openmrs.api.ObsService#getObservations(org.openmrs.Person, org.openmrs.logic.Aggregation, org.openmrs.Concept, org.openmrs.logic.Constraint) 484 * @see org.openmrs.api.ObsService#getObservations(org.openmrs.Person, 485 * org.openmrs.logic.Aggregation, org.openmrs.Concept, 486 * org.openmrs.logic.Constraint) 431 487 */ 432 488 public List<Obs> getObservations(Person who, Aggregation aggregation, 433 Concept question, Constraint constraint) { 434 return getObsDAO().getObservations(who, aggregation, question, constraint); 435 } 436 437 /** 438 * @see org.openmrs.api.ObsService#getObservations(java.util.List<org.openmrs.Concept>, java.util.Date, java.util.Data, boolean) 439 */ 440 public List<Obs> getObservations(List<Concept> concepts, Date fromDate, Date toDate, boolean includeVoided) { 441 return getObsDAO().getObservations(concepts, fromDate, toDate, includeVoided); 442 } 443 444 /** 445 * @see org.openmrs.api.ObsService#getObservations(java.util.List<org.openmrs.Concept>, java.util.Date, java.util.Data) 446 */ 447 public List<Obs> getObservations(List<Concept> concepts, Date fromDate, Date toDate) { 489 Concept question, Constraint constraint) { 490 return getObsDAO().getObservations(who, 491 aggregation, 492 question, 493 constraint); 494 } 495 496 /** 497 * @see org.openmrs.api.ObsService#getObservations(java.util.List<org.openmrs.Concept>, 498 * java.util.Date, java.util.Data, boolean) 499 */ 500 public List<Obs> getObservations(List<Concept> concepts, Date fromDate, 501 Date toDate, boolean includeVoided) { 502 return getObsDAO().getObservations(concepts, 503 fromDate, 504 toDate, 505 includeVoided); 506 } 507 508 /** 509 * @see org.openmrs.api.ObsService#getObservations(java.util.List<org.openmrs.Concept>, 510 * java.util.Date, java.util.Data) 511 */ 512 public List<Obs> getObservations(List<Concept> concepts, Date fromDate, 513 Date toDate) { 448 514 return this.getObservations(concepts, fromDate, toDate, false); 449 515 } 450 451 /** 452 * @see org.openmrs.api.ObsService#getObservations(PatientSet patients, List<Concept> concepts, Date fromDate, Date toDate) 453 */ 454 public List<Obs> getObservations(PatientSet patients, List<Concept> concepts, Date fromDate, Date toDate) { 516 517 /** 518 * @see org.openmrs.api.ObsService#getObservations(PatientSet patients, List<Concept> 519 * concepts, Date fromDate, Date toDate) 520 */ 521 public List<Obs> getObservations(PatientSet patients, 522 List<Concept> concepts, Date fromDate, Date toDate) { 455 523 return getObsDAO().getObservations(patients, concepts, fromDate, toDate); 456 524 } 457 458 /** 459 * Convenience method to get the ComplexObsHandler associated with a 460 * complexObs. Returns the ComplexObsHandler. Returns null if the525 526 /** 527 * Convenience method to get the ComplexObsHandler associated with a complex 528 * Obs. Returns the ComplexObsHandler. Returns null if the 461 529 * Obs.isComplexObs() is false or there is an error instantiating the 462 * handler class. 463 * 464 * TODO: Should ComplexObsHandler be an abstract class and 465 * put this method there instead? 530 * handler class. 466 531 * 467 532 * @param obs A complex Obs. … … 470 535 public ComplexObsHandler getHandler(Obs obs) { 471 536 if (obs.getConcept().isComplex()) { 472 // return getHandler( 473 String key = ((ConceptComplex)obs.getConcept()).getHandler(); 474 return this.getHandler(key); 537 // Get the ConceptComplex from the ConceptService then return its 538 // handler. 539 return this.getHandler(Context.getConceptService() 540 .getConceptComplex(obs.getConcept() 541 .getConceptId()) 542 .getHandler()); 475 543 } 476 544 return null; 477 478 /*479 String className = ((ConceptComplex) obs.getConcept()).getHandler();480 ComplexObsHandler handler = null;481 try {482 Class c = OpenmrsClassLoader.getInstance().loadClass(className);483 handler = (ComplexObsHandler)c.newInstance();484 //Class handlerClass = Class.forName(className);485 //handler = (ComplexObsHandler)className.getClass().newInstance();486 //handler = (ComplexObsHandler) handlerClass.newInstance();487 } catch (ClassNotFoundException cnfe) {488 return null;489 } catch (InstantiationException ie) {490 return null;491 } catch (IllegalAccessException iae) {492 return null;493 }494 return handler;495 */496 545 } 497 546 … … 507 556 } 508 557 } 509 558 510 559 public List<ComplexObsHandler> getComplexObsHandlers() { 511 // List handlers = new ArrayList<ComplexObsHandler>();512 // handlers.addAll(getHandlers().values());560 // List handlers = new ArrayList<ComplexObsHandler>(); 561 // handlers.addAll(getHandlers().values()); 513 562 return new ArrayList<ComplexObsHandler>(getHandlers().values()); 514 //return handlers; 515 } 516 563 // return handlers; 564 } 517 565 518 566 /** … … 521 569 public ComplexObsHandler getHandler(String key) { 522 570 try { 523 //System.out.println(this.getHandler(key).getClass().getCanonicalName()); 524 //return handlers.get(OpenmrsClassLoader.getInstance().loadClass(this.getHandler(key).getClass().getCanonicalName())); 571 // System.out.println(this.getHandler(key).getClass().getCanonicalName()); 572 // return 573 // handlers.get(OpenmrsClassLoader.getInstance().loadClass(this.getHandler(key).getClass().getCanonicalName())); 525 574 return handlers.get(key); 526 575 } catch (Exception ex) { 527 log.error("Failed to get ComplexObsHandler for " + this.getHandler(key).getClass().getCanonicalName(), ex); 576 log.error("Failed to get ComplexObsHandler for " 577 + this.getHandler(key).getClass().getCanonicalName(), ex); 528 578 return null; 529 579 } 530 }531 580 } 581 532 582 /** 533 583 * ADDs handlers...doesn't replace them. 584 * 534 585 * @see org.openmrs.api.ObsService#setHandlers(Map) 535 586 */ 536 public void setHandlers(Map<String, ComplexObsHandler> newHandlers) throws APIException { 587 public void setHandlers(Map<String, ComplexObsHandler> newHandlers) 588 throws APIException { 537 589 for (Map.Entry<String, ComplexObsHandler> entry : newHandlers.entrySet()) { 538 590 registerHandler(entry.getKey(), entry.getValue()); 539 591 } 540 592 } 541 593 542 594 /** 543 595 * @see org.openmrs.api.ObsService#getHandlers() … … 546 598 if (handlers == null) 547 599 handlers = new LinkedHashMap<String, ComplexObsHandler>(); 548 600 549 601 return handlers; 550 602 } 551 603 552 604 /** 553 * @see org.openmrs.api.ObsService#registerHandler(java.lang.Class, org.openmrs.obs.ComplexObsHandler) 554 */ 555 public void registerHandler(String key, ComplexObsHandler handler) throws APIException { 556 /*TODO: 557 * This is a total hack in order to register the ComplexObsHandler by a "key" word instead of the class name. 558 * Spring seems to insist on using the className no matter how you map it in Application-context.xml 559 * 560 */ 561 //String[] keys = key.split("."); 562 //String name = (keys.length < 2) ? keys[0] : keys[keys.length-1]; 563 System.out.println(key + " ---> " + handler.toString()); 564 System.out.println(key.substring(key.lastIndexOf(".")+1, key.length()) + " ---> " + handler.toString()); 565 getHandlers().put(key.substring(key.lastIndexOf(".")+1, key.length()), handler); 566 } 567 605 * @see org.openmrs.api.ObsService#registerHandler(java.lang.Class, 606 * org.openmrs.obs.ComplexObsHandler) 607 */ 608 public void registerHandler(String key, ComplexObsHandler handler) 609 throws APIException { 610 getHandlers().put(key, handler); 611 } 612 568 613 /** 569 614 * @see org.openmrs.api.ObsService#registerHandler(java.lang.String) 570 615 */ 571 616 @SuppressWarnings("unchecked") 572 public void registerHandler(String key, String handlerClass) throws APIException { 617 public void registerHandler(String key, String handlerClass) 618 throws APIException { 573 619 try { 574 Class loadedClass = OpenmrsClassLoader.getInstance().loadClass(handlerClass); 575 registerHandler(key, (ComplexObsHandler)loadedClass.newInstance()); 576 577 } catch (Exception e) { 578 throw new APIException("Unable to load and instantiate handler", e); 579 } 580 } 581 620 Class loadedClass = OpenmrsClassLoader.getInstance() 621 .loadClass(handlerClass); 622 registerHandler(key, (ComplexObsHandler) loadedClass.newInstance()); 623 624 } catch (Exception e) { 625 throw new APIException("Unable to load and instantiate handler", e); 626 } 627 } 628 582 629 /** 583 630 * @see org.openmrs.api.ObsService#removeHandler(java.lang.Class)