/servers/openmrs-modules/rest/branches/gsoc10module/trunk/web/src/org/openmrs/module/restmodule/web/PatientResource.java

http://moca.googlecode.com/ · Java · 103 lines · 61 code · 17 blank · 25 comment · 9 complexity · 3639378cbf7aacc2b8116264fee75a8f MD5 · raw file

  1. package org.openmrs.module.restmodule.web;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import java.lang.reflect.Type;
  5. import java.util.Collection;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import org.apache.commons.logging.Log;
  10. import org.apache.commons.logging.LogFactory;
  11. import org.openmrs.Patient;
  12. import org.openmrs.api.context.Context;
  13. import org.openmrs.module.restmodule.JsonPatient;
  14. import org.openmrs.module.restmodule.RestUtil;
  15. import org.openmrs.module.restmodule.XmlPatient;
  16. import com.google.gson.Gson;
  17. import com.google.gson.GsonBuilder;
  18. import com.google.gson.reflect.TypeToken;
  19. /**
  20. * Provides a simple RESTful access to patients
  21. */
  22. public class PatientResource implements RestResource {
  23. private static final long serialVersionUID = -5695748L;
  24. private Log log = LogFactory.getLog(this.getClass());
  25. /**
  26. * Handle all requests to this resource
  27. */
  28. public void handleRequest(Operation operation, OutputType outputType,
  29. String restRequest, HttpServletRequest request,
  30. HttpServletResponse response) throws ServletException, IOException {
  31. PrintWriter out = response.getWriter();
  32. switch (operation) {
  33. case GET:
  34. Collection<Patient> patientList = Context.getPatientService()
  35. .getPatientsByIdentifier(restRequest, false);
  36. printPatientList(out, outputType, patientList);
  37. break;
  38. case POST:
  39. case PUT:
  40. case DELETE:
  41. response.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
  42. return;
  43. }
  44. }
  45. /**
  46. * Auto generated method comment
  47. *
  48. * @param out
  49. * @param outputType
  50. * @param patientList
  51. */
  52. public static void printPatientList(PrintWriter out, OutputType outputType,
  53. Collection<Patient> patientList) {
  54. if (outputType == OutputType.XML) {
  55. out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  56. out.print("<patientList version=\""
  57. + RestServlet.PATIENT_LIST_XML_VERSION + "\">");
  58. int i = 0;
  59. int max = RestUtil.getMaxResults();
  60. for (Patient patient : patientList) {
  61. out.print(XmlPatient.encode(patient));
  62. i++;
  63. if (max > 0 && i >= max)
  64. break; // if max set, abort before exceeding
  65. }
  66. out.print("</patientList>");
  67. } else if (outputType == OutputType.JSON) {
  68. GsonBuilder gsonBuilder = new GsonBuilder().registerTypeAdapter(Patient.class, new PatientSerializer());
  69. Gson gson = gsonBuilder.create();
  70. Type listType = new TypeToken<Collection<Patient>>() {}.getType();
  71. out.print(gson.toJson(patientList, listType));
  72. // out.print("[");
  73. // int i = 0;
  74. // int max = RestUtil.getMaxResults();
  75. // for (Patient patient : patientList) {
  76. // if (i != 0)
  77. // out.print(",");
  78. // out.print(JsonPatient.encode(patient));
  79. // i++;
  80. // if (max > 0 && i >= max)
  81. // break; // if max set, abort before exceeding
  82. // }
  83. // out.print("]");
  84. }
  85. }
  86. }