| | 39 | /** |
|---|
| | 40 | * Hibernate specific Person database methods |
|---|
| | 41 | * |
|---|
| | 42 | * This class should not be used directly. All database calls |
|---|
| | 43 | * should go through the Service layer. |
|---|
| | 44 | * |
|---|
| | 45 | * Use: |
|---|
| | 46 | * <code> |
|---|
| | 47 | * PersonService ps = Context.getPersonService(); |
|---|
| | 48 | * ps.getPeople("name", false); |
|---|
| | 49 | * </code> |
|---|
| | 50 | * |
|---|
| | 51 | * @see org.openmrs.api.db.PersonDAO |
|---|
| | 52 | * @see org.openmrs.api.PersonService |
|---|
| | 53 | * @see org.openmrs.api.context.Context |
|---|
| | 54 | */ |
|---|
| 238 | | * @see org.openmrs.api.db.PersonDAO#getPersonAttributeTypes() |
|---|
| 239 | | */ |
|---|
| 240 | | @SuppressWarnings("unchecked") |
|---|
| 241 | | public List<PersonAttributeType> getPersonAttributeTypes() { |
|---|
| 242 | | return sessionFactory.getCurrentSession() |
|---|
| 243 | | .createQuery("from PersonAttributeType type order by type.name") |
|---|
| 244 | | .list(); |
|---|
| 245 | | } |
|---|
| 246 | | |
|---|
| 247 | | /** |
|---|
| 248 | | * @see org.openmrs.api.db.PersonDAO#updatePersonAttributeType(org.openmrs.PersonAttributeType) |
|---|
| 249 | | */ |
|---|
| 250 | | public void updatePersonAttributeType(PersonAttributeType type) { |
|---|
| 251 | | sessionFactory.getCurrentSession().merge(type); |
|---|
| 252 | | } |
|---|
| 253 | | |
|---|
| 254 | | /** |
|---|
| | 244 | * @see org.openmrs.api.PersonService#savePersonAttributeType(org.openmrs.PersonAttributeType) |
|---|
| | 245 | * @see org.openmrs.api.db.PersonDAO#savePersonAttributeType(org.openmrs.PersonAttributeType) |
|---|
| | 246 | */ |
|---|
| | 247 | public PersonAttributeType savePersonAttributeType(PersonAttributeType type) { |
|---|
| | 248 | sessionFactory.getCurrentSession().saveOrUpdate(type); |
|---|
| | 249 | return type; |
|---|
| | 250 | } |
|---|
| | 251 | |
|---|
| | 252 | /** |
|---|
| | 253 | * @see org.openmrs.api.PersonService#getPersonAttributeType(java.lang.Integer) |
|---|
| 264 | | return (PersonAttribute) sessionFactory.getCurrentSession() |
|---|
| 265 | | .get(PersonAttribute.class, id); |
|---|
| 266 | | } |
|---|
| 267 | | |
|---|
| 268 | | /** |
|---|
| 269 | | * @see org.openmrs.api.db.PersonDAO#getPersonAttributeType(java.lang.String) |
|---|
| 270 | | */ |
|---|
| 271 | | public PersonAttributeType getPersonAttributeType(String typeName) { |
|---|
| 272 | | Session session = sessionFactory.getCurrentSession(); |
|---|
| 273 | | Query q = session.createQuery("from PersonAttributeType t where t.name = :name") |
|---|
| 274 | | .setString("name", typeName); |
|---|
| 275 | | return (PersonAttributeType)q.uniqueResult(); |
|---|
| 276 | | } |
|---|
| 277 | | |
|---|
| 278 | | /** |
|---|
| 279 | | * @see org.openmrs.api.db.PatientService#getRelationship() |
|---|
| | 265 | return (PersonAttribute)sessionFactory.getCurrentSession().get(PersonAttribute.class, id); |
|---|
| | 266 | } |
|---|
| | 267 | |
|---|
| | 268 | /** |
|---|
| | 269 | * @see org.openmrs.api.PersonService#getAllPersonAttributeTypes(boolean) |
|---|
| | 270 | * @see org.openmrs.api.db.PersonDAO#getAllPersonAttributeTypes(boolean) |
|---|
| | 271 | */ |
|---|
| | 272 | public List<PersonAttributeType> getAllPersonAttributeTypes( |
|---|
| | 273 | boolean includeRetired) throws DAOException { |
|---|
| | 274 | Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PersonAttributeType.class, "r"); |
|---|
| | 275 | |
|---|
| | 276 | if (!includeRetired) { |
|---|
| | 277 | criteria.add(Expression.eq("retired", false)); |
|---|
| | 278 | } |
|---|
| | 279 | |
|---|
| | 280 | criteria.addOrder(Order.asc("name")); |
|---|
| | 281 | |
|---|
| | 282 | return criteria.list(); |
|---|
| | 283 | } |
|---|
| | 284 | |
|---|
| | 285 | /** |
|---|
| | 286 | * @see org.openmrs.api.db.PersonDAO#getPersonAttributeTypes(java.lang.String, java.lang.String, java.lang.Integer, java.lang.Boolean) |
|---|
| | 287 | */ |
|---|
| | 288 | public List<PersonAttributeType> getPersonAttributeTypes(String exactName, |
|---|
| | 289 | String format, Integer foreignKey, Boolean searchable) |
|---|
| | 290 | throws DAOException { |
|---|
| | 291 | Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PersonAttributeType.class, "r"); |
|---|
| | 292 | criteria.add(Expression.eq("retired", false)); |
|---|
| | 293 | |
|---|
| | 294 | if (exactName != null) |
|---|
| | 295 | criteria.add(Expression.eq("name", exactName)); |
|---|
| | 296 | |
|---|
| | 297 | if (format != null) |
|---|
| | 298 | criteria.add(Expression.eq("format", format)); |
|---|
| | 299 | |
|---|
| | 300 | if (foreignKey != null) |
|---|
| | 301 | criteria.add(Expression.eq("foreignKey", foreignKey)); |
|---|
| | 302 | |
|---|
| | 303 | if (searchable != null) |
|---|
| | 304 | criteria.add(Expression.eq("searchable", format)); |
|---|
| | 305 | |
|---|
| | 306 | return criteria.list(); |
|---|
| | 307 | } |
|---|
| | 308 | |
|---|
| | 309 | /** |
|---|
| | 310 | * @see org.openmrs.api.PatientService#getRelationship() |
|---|
| | 311 | * @see org.openmrs.api.db.PersonDAO#getRelationship(java.lang.Integer) |
|---|
| 291 | | * @see org.openmrs.api.db.PatientService#getRelationships() |
|---|
| 292 | | */ |
|---|
| 293 | | @SuppressWarnings("unchecked") |
|---|
| 294 | | public List<Relationship> getRelationships() throws DAOException { |
|---|
| 295 | | List<Relationship> relationships = sessionFactory.getCurrentSession() |
|---|
| 296 | | .createQuery("from Relationship r order by r.relationshipId asc") |
|---|
| 297 | | .list(); |
|---|
| 298 | | |
|---|
| 299 | | return relationships; |
|---|
| 300 | | } |
|---|
| 301 | | |
|---|
| 302 | | /** |
|---|
| 303 | | * @see org.openmrs.api.db.PatientService#getRelationships(org.openmrs.Person) |
|---|
| 304 | | */ |
|---|
| 305 | | @SuppressWarnings("unchecked") |
|---|
| 306 | | public List<Relationship> getRelationships(Person person, boolean showVoided) |
|---|
| 307 | | throws DAOException { |
|---|
| 308 | | Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Relationship.class, "r") |
|---|
| 309 | | .add(Expression.or(Expression.eq("personA", person), Expression.eq("personB", person))); |
|---|
| 310 | | |
|---|
| 311 | | if (!showVoided) { |
|---|
| 312 | | criteria.add(Expression.eq("voided", showVoided)); |
|---|
| 313 | | } |
|---|
| 314 | | |
|---|
| 315 | | return criteria.list(); |
|---|
| 316 | | } |
|---|
| 317 | | |
|---|
| 318 | | /** |
|---|
| 319 | | * @see org.openmrs.api.db.PatientService#getRelationshipType(java.lang.Integer) |
|---|
| | 323 | * @see org.openmrs.api.PersonService#getAllRelationships(boolean) |
|---|
| | 324 | * @see org.openmrs.api.db.PersonDAO#getAllRelationships(boolean) |
|---|
| | 325 | */ |
|---|
| | 326 | public List<Relationship> getAllRelationships(boolean includeVoided) |
|---|
| | 327 | throws DAOException { |
|---|
| | 328 | Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Relationship.class, "r"); |
|---|
| | 329 | |
|---|
| | 330 | if (!includeVoided) { |
|---|
| | 331 | criteria.add(Expression.eq("voided", false)); |
|---|
| | 332 | } |
|---|
| | 333 | |
|---|
| | 334 | return criteria.list(); |
|---|
| | 335 | } |
|---|
| | 336 | |
|---|
| | 337 | /** |
|---|
| | 338 | * @see org.openmrs.api.PersonService#getRelationships(org.openmrs.Person, org.openmrs.Person, org.openmrs.RelationshipType) |
|---|
| | 339 | * @see org.openmrs.api.db.PersonDAO#getRelationships(org.openmrs.Person, org.openmrs.Person, org.openmrs.RelationshipType) |
|---|
| | 340 | */ |
|---|
| | 341 | public List<Relationship> getRelationships(Person fromPerson, |
|---|
| | 342 | Person toPerson, RelationshipType relType) { |
|---|
| | 343 | Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Relationship.class, "r"); |
|---|
| | 344 | |
|---|
| | 345 | if (fromPerson != null) |
|---|
| | 346 | criteria.add(Expression.eq("personA", fromPerson)); |
|---|
| | 347 | if (toPerson != null) |
|---|
| | 348 | criteria.add(Expression.eq("personB", toPerson)); |
|---|
| | 349 | if (relType != null) |
|---|
| | 350 | criteria.add(Expression.eq("relationshipType", relType)); |
|---|
| | 351 | |
|---|
| | 352 | criteria.add(Expression.eq("voided", false)); |
|---|
| | 353 | |
|---|
| | 354 | return criteria.list(); |
|---|
| | 355 | } |
|---|
| | 356 | |
|---|
| | 357 | /** |
|---|
| | 358 | * @see org.openmrs.api.PatientService#getRelationshipType(java.lang.Integer) |
|---|
| | 359 | * @see org.openmrs.api.db.PatientDAO#getRelationshipType(java.lang.Integer) |
|---|
| 329 | | |
|---|
| 330 | | } |
|---|
| 331 | | |
|---|
| 332 | | public RelationshipType findRelationshipType(String relationshipTypeName) |
|---|
| 333 | | throws DAOException { |
|---|
| 334 | | RelationshipType ret = (RelationshipType) sessionFactory.getCurrentSession() |
|---|
| 335 | | .createQuery("from RelationshipType t where CONCAT(t.aIsToB, CONCAT('/', t.bIsToA)) = :toString order by weight") |
|---|
| 336 | | .setString("toString", |
|---|
| 337 | | relationshipTypeName) |
|---|
| 338 | | .uniqueResult(); |
|---|
| 339 | | |
|---|
| 340 | | return ret; |
|---|
| 341 | | } |
|---|
| 342 | | |
|---|
| 343 | | /** |
|---|
| 344 | | * @see org.openmrs.api.db.PatientService#getRelationshipTypes() |
|---|
| | 369 | } |
|---|
| | 370 | |
|---|
| | 371 | /** |
|---|
| | 372 | * @see org.openmrs.api.PersonService#getRelationshipTypes(java.lang.String, java.lang.Boolean) |
|---|
| | 373 | * @see org.openmrs.api.db.PersonDAO#getRelationshipTypes(java.lang.String, java.lang.Boolean) |
|---|
| 347 | | public List<RelationshipType> getRelationshipTypes() throws DAOException { |
|---|
| 348 | | List<RelationshipType> relationshipTypes; |
|---|
| 349 | | relationshipTypes = sessionFactory.getCurrentSession() |
|---|
| 350 | | .createQuery("from RelationshipType t order by t.weight") |
|---|
| 351 | | .list(); |
|---|
| 352 | | |
|---|
| 353 | | return relationshipTypes; |
|---|
| 354 | | } |
|---|
| 355 | | |
|---|
| 356 | | /** |
|---|
| 357 | | * @see org.openmrs.api.db.PersonService#createRelationshipType(org.openmrs.RelationshipType) |
|---|
| 358 | | */ |
|---|
| 359 | | public void createRelationshipType(RelationshipType relationshipType) |
|---|
| 360 | | throws DAOException { |
|---|
| 361 | | relationshipType.setCreator(Context.getAuthenticatedUser()); |
|---|
| 362 | | relationshipType.setDateCreated(new Date()); |
|---|
| 363 | | sessionFactory.getCurrentSession().save(relationshipType); |
|---|
| 364 | | } |
|---|
| 365 | | |
|---|
| 366 | | /** |
|---|
| 367 | | * @see org.openmrs.api.db.PersonService#updateRelationshipType(org.openmrs.RelationshipType) |
|---|
| 368 | | */ |
|---|
| 369 | | public void updateRelationshipType(RelationshipType relationshipType) |
|---|
| 370 | | throws DAOException { |
|---|
| 371 | | if (relationshipType.getRelationshipTypeId() == null) |
|---|
| 372 | | createRelationshipType(relationshipType); |
|---|
| 373 | | else |
|---|
| | 376 | public List<RelationshipType> getRelationshipTypes(String relationshipTypeName, Boolean preferred) throws DAOException { |
|---|
| | 377 | |
|---|
| | 378 | Criteria criteria = sessionFactory.getCurrentSession().createCriteria(RelationshipType.class, "t"); |
|---|
| | 379 | criteria.add(Expression.eq("retired", false)); |
|---|
| | 380 | criteria.add(Expression.sql("CONCAT(t.aIsToB, CONCAT('/', t.bIsToA))", relationshipTypeName, new StringType())); |
|---|
| | 381 | |
|---|
| | 382 | if (preferred != null) |
|---|
| | 383 | criteria.add(Expression.eq("preferred", preferred)); |
|---|
| | 384 | |
|---|
| | 385 | return criteria.list(); |
|---|
| | 386 | } |
|---|
| | 387 | |
|---|
| | 388 | /** |
|---|
| | 389 | * @see org.openmrs.api.PatientService#getRelationshipTypes() |
|---|
| | 390 | * @see org.openmrs.api.db.PersonDAO#getAllRelationshipTypes(boolean) |
|---|
| | 391 | */ |
|---|
| | 392 | @SuppressWarnings("unchecked") |
|---|
| | 393 | public List<RelationshipType> getAllRelationshipTypes(boolean includeRetired) throws DAOException { |
|---|
| | 394 | |
|---|
| | 395 | Criteria criteria = sessionFactory.getCurrentSession().createCriteria(RelationshipType.class, "t"); |
|---|
| | 396 | |
|---|
| | 397 | if (!includeRetired) |
|---|
| | 398 | criteria.add(Expression.eq("retired", false)); |
|---|
| | 399 | |
|---|
| | 400 | criteria.addOrder(Order.asc("weight")); |
|---|
| | 401 | |
|---|
| | 402 | return criteria.list(); |
|---|
| | 403 | } |
|---|
| | 404 | |
|---|
| | 405 | /** |
|---|
| | 406 | * @see org.openmrs.api.PersonService#saveRelationshipType(org.openmrs.RelationshipType) |
|---|
| | 407 | * @see org.openmrs.api.db.PersonDAO#saveRelationshipType(org.openmrs.RelationshipType) |
|---|
| | 408 | */ |
|---|
| | 409 | public RelationshipType saveRelationshipType(RelationshipType relationshipType) throws DAOException { |
|---|
| 415 | | } |
|---|
| 416 | | } |
|---|
| 417 | | |
|---|
| 418 | | /** |
|---|
| 419 | | * @see org.openmrs.api.db.PersonService#createRelationship(org.openmrs.Relationship) |
|---|
| 420 | | */ |
|---|
| 421 | | public void createRelationship(Relationship relationship) |
|---|
| 422 | | throws DAOException { |
|---|
| 423 | | relationship.setCreator(Context.getAuthenticatedUser()); |
|---|
| 424 | | relationship.setDateCreated(new Date()); |
|---|
| 425 | | sessionFactory.getCurrentSession().save(relationship); |
|---|
| 426 | | } |
|---|
| 427 | | |
|---|
| 428 | | /** |
|---|
| 429 | | * @see org.openmrs.api.db.PersonService#updateRelationship(org.openmrs.Relationship) |
|---|
| 430 | | */ |
|---|
| 431 | | public void updateRelationship(Relationship relationship) |
|---|
| 432 | | throws DAOException { |
|---|
| 433 | | if (relationship.getRelationshipId() == null) |
|---|
| 434 | | createRelationship(relationship); |
|---|
| 435 | | else |
|---|
| | 437 | return person; |
|---|
| | 438 | } |
|---|
| | 439 | |
|---|
| | 440 | /** |
|---|
| | 441 | * @see org.openmrs.api.PersonService#saveRelationship(org.openmrs.Relationship) |
|---|
| | 442 | * @see org.openmrs.api.db.PersonDAO#saveRelationship(org.openmrs.Relationship) |
|---|
| | 443 | */ |
|---|
| | 444 | public Relationship saveRelationship(Relationship relationship) throws DAOException { |
|---|
| 445 | | } |
|---|
| 446 | | |
|---|
| 447 | | /** |
|---|
| 448 | | * @see org.openmrs.api.db.PersonService#voidRelationship(org.openmrs.Relationship) |
|---|
| 449 | | */ |
|---|
| 450 | | public void voidRelationship(Relationship relationship) throws DAOException { |
|---|
| 451 | | relationship.setVoided(true); |
|---|
| 452 | | updateRelationship(relationship); |
|---|
| 453 | | } |
|---|
| 454 | | |
|---|
| 455 | | /** |
|---|
| 456 | | * @see org.openmrs.api.db.PersonService#unvoidRelationship(org.openmrs.Relationship) |
|---|
| 457 | | */ |
|---|
| 458 | | public void unvoidRelationship(Relationship relationship) |
|---|
| 459 | | throws DAOException { |
|---|
| 460 | | relationship.setVoided(false); |
|---|
| 461 | | relationship.setVoidedBy(null); |
|---|
| 462 | | relationship.setDateVoided(null); |
|---|
| 463 | | relationship.setVoidReason(null); |
|---|
| 464 | | updateRelationship(relationship); |
|---|