All pastes #2065013 Raw Edit

CohortMemberREsource

public java v1 · immutable
#2065013 ·published 2011-05-19 20:13 UTC
rendered paste body
package org.openmrs.module.webservices.rest.web.resource;import java.util.ArrayList;import java.util.List;import org.openmrs.Cohort;import org.openmrs.Patient;import org.openmrs.annotation.Handler;import org.openmrs.api.context.Context;import org.openmrs.module.webservices.rest.web.RequestContext;import org.openmrs.module.webservices.rest.web.annotation.SubResource;import org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation;import org.openmrs.module.webservices.rest.web.representation.RefRepresentation;import org.openmrs.module.webservices.rest.web.representation.Representation;import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingSubResource;import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException;import org.openmrs.module.webservices.rest.web.response.ResponseException;/** * Sub-resource for cohort members */@SubResource(parent = CohortResource.class, parentProperty = "REMOVE-THIS-PROPERTY", path = "patients")@Handler(supports = Patient.class, order = 0)public class CohortMemberResource extends DelegatingSubResource<Patient, Cohort, CohortResource> {		@Override	public List<Patient> doGetAll(Cohort parent, RequestContext context) throws ResponseException {		List<Patient> members = new ArrayList<Patient>();		members.addAll(Context.getPatientSetService().getPatients(parent.getMemberIds()));		return members;	}		@Override	public Cohort getParent(Patient instance) {		return parent;	}		@Override	public void setParent(Patient instance, Cohort parent) {		this.parent = parent;	}		@Override	protected void delete(Patient delegate, String reason, RequestContext context) throws ResponseException {		removeMemberFromCohort(delegate);	}		@Override	public Patient getByUniqueId(String uniqueId) {		return Context.getPatientService().getPatientByUuid(uniqueId);	}		@Override	public DelegatingResourceDescription getRepresentationDescription(Representation rep) {		if (rep instanceof RefRepresentation) {			DelegatingResourceDescription description = new DelegatingResourceDescription();			description.addProperty("uuid");			description.addProperty("uri", findMethod("getUri"));			description.addProperty("display", findMethod("getDisplayString"));			return description;		} else if (rep instanceof DefaultRepresentation) {			DelegatingResourceDescription description = new DelegatingResourceDescription();			description.addProperty("uuid");			description.addProperty("gender");			description.addProperty("age");			description.addProperty("birthdate");			description.addProperty("birthdateEstimated");			description.addProperty("dead");			description.addProperty("deathDate");			description.addProperty("causeOfDeath", Representation.REF);			description.addProperty("preferredName", "personName", Representation.REF);			description.addProperty("personAddress", Representation.REF);			description.addProperty("activeIdentifiers", Representation.REF);			description.addProperty("activeAttributes", Representation.REF);			description.addProperty("uri", findMethod("getUri"));			return description;		}		return null;	}		@Override	protected Patient newDelegate() {		return new Patient();	}		@Override	public void purge(Patient delegate, RequestContext context) throws ResponseException {		throw new ResourceDoesNotSupportOperationException();	}		/**	 * @should add patient to cohort	 */	@Override	protected Patient save(Patient delegate) {				// TODO Auto-generated method stub		return null;	}		/** class CohortMembersHelper { */		private Cohort parent = null;		public void addMemberToCohort(Patient patient) {		getParent(patient).addMember(patient.getId());		Context.getCohortService().saveCohort(getParent(patient));	}		public void removeMemberFromCohort(Patient patient) {		getParent(patient).removeMember(patient.getId());		Context.getCohortService().saveCohort(getParent(patient));	}		//}	}