Java tutorial
// begin: Meta Healthcare /** * The contents of this file are subject to the OpenMRS Public License * Version 1.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://license.openmrs.org * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the * License for the specific language governing rights and limitations * under the License. * * Copyright (C) OpenMRS, LLC. All Rights Reserved. */ package org.openmrs.module.webservices.rest.web.v1_0.controller; import org.openmrs.Patient; import org.openmrs.PatientProgram; import org.openmrs.Program; import org.openmrs.api.context.Context; import org.openmrs.module.webservices.rest.SimpleObject; import org.openmrs.module.webservices.rest.web.RestConstants; import org.openmrs.module.webservices.rest.web.response.ResponseException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; import java.util.List; /** * Controller for REST web service access to a program and it's members. */ @Controller @RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/program/{uuid}/member") public class ProgramMemberController extends BaseRestController { private static final String SQUARE_BRACKET_LEFT = "["; private static final String SQUARE_BRACKET_RIGHT = "]"; private static final String CURLY_BRACKET_LEFT = "{"; private static final String CURLY_BRACKET_RIGHT = "}"; private static final String COMMA = ","; private static final String SPACE = " "; private static final String LABEL_UUID = "uuid:"; private static final String LABEL_DISPLAY = "display:"; @RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/program/{uuid}/member") @ResponseBody public SimpleObject getMembers(@PathVariable("uuid") String uuid, HttpServletRequest request) throws ResponseException { Program p = Context.getProgramWorkflowService().getProgramByUuid(uuid); //i.e. c8e87b17-907f-4b50-8d23-0038e050bbbc List<PatientProgram> pplist = Context.getProgramWorkflowService().getPatientPrograms(null, p, null, null, null, null, false); // additional logic here eventually, state handling, etc. ... SimpleObject so = new SimpleObject(); List<Patient> patientList = new ArrayList<Patient>(); StringBuffer result = new StringBuffer(); for (PatientProgram pp : pplist) { patientList.add(pp.getPatient()); if (result.length() > 0) result.append(COMMA); result.append(CURLY_BRACKET_LEFT).append(LABEL_UUID).append(pp.getPatient().getUuid()).append(COMMA) .append(LABEL_DISPLAY).append(pp.getPatient().getGivenName()).append(SPACE) .append(pp.getPatient().getFamilyName()).append(CURLY_BRACKET_RIGHT); } String json = SQUARE_BRACKET_LEFT + result.append(SQUARE_BRACKET_RIGHT).toString(); so.add("results", json); // {"results":"[{uuid:68a8da76-7ca8-11e0-9450-00e0b6048124,display:TEST PATIENT}]"} return so; } } // end: Meta Healthcare