/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
- package org.openmrs.module.restmodule.web;
-
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.lang.reflect.Type;
- import java.util.Collection;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.openmrs.Patient;
- import org.openmrs.api.context.Context;
- import org.openmrs.module.restmodule.JsonPatient;
- import org.openmrs.module.restmodule.RestUtil;
- import org.openmrs.module.restmodule.XmlPatient;
-
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import com.google.gson.reflect.TypeToken;
-
- /**
- * Provides a simple RESTful access to patients
- */
- public class PatientResource implements RestResource {
-
- private static final long serialVersionUID = -5695748L;
- private Log log = LogFactory.getLog(this.getClass());
-
- /**
- * Handle all requests to this resource
- */
- public void handleRequest(Operation operation, OutputType outputType,
- String restRequest, HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
-
- PrintWriter out = response.getWriter();
-
- switch (operation) {
-
- case GET:
- Collection<Patient> patientList = Context.getPatientService()
- .getPatientsByIdentifier(restRequest, false);
-
- printPatientList(out, outputType, patientList);
-
- break;
-
- case POST:
- case PUT:
- case DELETE:
- response.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
- return;
- }
-
- }
-
- /**
- * Auto generated method comment
- *
- * @param out
- * @param outputType
- * @param patientList
- */
- public static void printPatientList(PrintWriter out, OutputType outputType,
- Collection<Patient> patientList) {
-
- if (outputType == OutputType.XML) {
- out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
- out.print("<patientList version=\""
- + RestServlet.PATIENT_LIST_XML_VERSION + "\">");
- int i = 0;
- int max = RestUtil.getMaxResults();
- for (Patient patient : patientList) {
- out.print(XmlPatient.encode(patient));
- i++;
- if (max > 0 && i >= max)
- break; // if max set, abort before exceeding
- }
- out.print("</patientList>");
- } else if (outputType == OutputType.JSON) {
- GsonBuilder gsonBuilder = new GsonBuilder().registerTypeAdapter(Patient.class, new PatientSerializer());
- Gson gson = gsonBuilder.create();
- Type listType = new TypeToken<Collection<Patient>>() {}.getType();
- out.print(gson.toJson(patientList, listType));
- // out.print("[");
- // int i = 0;
- // int max = RestUtil.getMaxResults();
- // for (Patient patient : patientList) {
- // if (i != 0)
- // out.print(",");
- // out.print(JsonPatient.encode(patient));
- // i++;
- // if (max > 0 && i >= max)
- // break; // if max set, abort before exceeding
- // }
- // out.print("]");
-
- }
- }
- }