Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. /**
  2. * The contents of this file are subject to the OpenMRS Public License
  3. * Version 1.0 (the "License"); you may not use this file except in
  4. * compliance with the License. You may obtain a copy of the License at
  5. * http://license.openmrs.org
  6. *
  7. * Software distributed under the License is distributed on an "AS IS"
  8. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  9. * License for the specific language governing rights and limitations
  10. * under the License.
  11. *
  12. * Copyright (C) OpenMRS, LLC. All Rights Reserved.
  13. */
  14.  
  15. package org.openmrs.module.metadatadeploy.handler.impl;
  16.  
  17. import java.lang.reflect.Method;
  18. import org.openmrs.PersonAttributeType;
  19. import org.openmrs.annotation.Handler;
  20. import org.openmrs.api.PersonService;
  21. import org.openmrs.api.db.hibernate.DbSessionFactory;
  22. import org.openmrs.module.metadatadeploy.handler.AbstractObjectDeployHandler;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.beans.factory.annotation.Qualifier;
  25.  
  26. /**
  27. * Deployment handler for person attribute types
  28. */
  29. @Handler(supports = { PersonAttributeType.class })
  30. public class PersonAttributeTypeDeployHandler extends AbstractObjectDeployHandler<PersonAttributeType> {
  31.  
  32. @Autowired
  33. @Qualifier("personService")
  34. private PersonService personService;
  35.  
  36. @Autowired
  37. private DbSessionFactory sessionFactory;
  38.  
  39. /**
  40. * @see org.openmrs.module.metadatadeploy.handler.ObjectDeployHandler#fetch(String)
  41. */
  42. @Override
  43. public PersonAttributeType fetch(String uuid) {
  44. return personService.getPersonAttributeTypeByUuid(uuid);
  45. }
  46.  
  47. /**
  48. * @see org.openmrs.module.metadatadeploy.handler.ObjectDeployHandler#save(org.openmrs.OpenmrsObject)
  49. */
  50. @Override
  51. public PersonAttributeType save(PersonAttributeType obj) {
  52. // The regular save method in the person service does some interesting stuff to
  53. // check name changes.. which breaks
  54. // our way of replacing existing objects. Our workaround is to ask Hibernate
  55. // directly to save the object
  56. getCurrentSession().saveOrUpdate(obj);
  57. return obj;
  58.  
  59. // return personService.savePersonAttributeType(obj);
  60. }
  61.  
  62. /**
  63. * @see org.openmrs.module.metadatadeploy.handler.ObjectDeployHandler#findAlternateMatch(org.openmrs.OpenmrsObject)
  64. */
  65. @Override
  66. public PersonAttributeType findAlternateMatch(PersonAttributeType incoming) {
  67. return personService.getPersonAttributeTypeByName(incoming.getName());
  68. }
  69.  
  70. /**
  71. * @see org.openmrs.module.metadatadeploy.handler.ObjectDeployHandler#uninstall(org.openmrs.OpenmrsObject,
  72. * String)
  73. * @param obj
  74. * the object to uninstall
  75. */
  76. @Override
  77. public void uninstall(PersonAttributeType obj, String reason) {
  78. personService.retirePersonAttributeType(obj, reason);
  79. }
  80.  
  81. /**
  82. * Gets the current hibernate session while taking care of the hibernate 3 and 4
  83. * differences.
  84. *
  85. * @return the current hibernate session.
  86. */
  87. private org.openmrs.api.db.hibernate.DbSession getCurrentSession() {
  88. try {
  89. return sessionFactory.getCurrentSession();
  90. } catch (NoSuchMethodError ex) {
  91. try {
  92. Method method = sessionFactory.getClass().getMethod("getCurrentSession", null);
  93. return (org.openmrs.api.db.hibernate.DbSession) method.invoke(sessionFactory, null);
  94. } catch (Exception e) {
  95. throw new RuntimeException("Failed to get the current hibernate session", e);
  96. }
  97. }
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement