Skip to content

Instantly share code, notes, and snippets.

@themoonraker13
Created January 16, 2017 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themoonraker13/afef82df23b007c7e24d9ea78d106015 to your computer and use it in GitHub Desktop.
Save themoonraker13/afef82df23b007c7e24d9ea78d106015 to your computer and use it in GitHub Desktop.
InformationRequestResource1_11 REST Resource File
package org.openmrs.module.informationsharing.rest.openmrs1_11;
import org.openmrs.Person;
import org.openmrs.api.context.Context;
import org.openmrs.module.informationsharing.InformationRequest;
import org.openmrs.module.informationsharing.InformationRequestType;
import org.openmrs.module.informationsharing.InformationResourceType;
import org.openmrs.module.informationsharing.api.InformationSharingService;
import org.openmrs.module.informationsharing.rest.controller.InformationSharingRestController;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter;
import org.openmrs.module.webservices.rest.web.annotation.PropertySetter;
import org.openmrs.module.webservices.rest.web.annotation.Resource;
import org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation;
import org.openmrs.module.webservices.rest.web.representation.FullRepresentation;
import org.openmrs.module.webservices.rest.web.representation.Representation;
import org.openmrs.module.webservices.rest.web.resource.api.PageableResult;
import org.openmrs.module.webservices.rest.web.resource.impl.DataDelegatingCrudResource;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging;
import org.openmrs.module.webservices.rest.web.response.ResponseException;
import org.openmrs.util.PrivilegeConstants;
@Resource(name = RestConstants.VERSION_1 + InformationSharingRestController.INFORMATION_SHARING_REST_NAMESPACE + "/informationrequest", supportedClass = InformationRequest.class, supportedOpenmrsVersions = {"1.11.*", "1.12.*"})
public class InformationRequestResource1_11 extends DataDelegatingCrudResource<InformationRequest> {
@Override
public DelegatingResourceDescription getRepresentationDescription(Representation representation) {
if (representation instanceof DefaultRepresentation) {
DelegatingResourceDescription description = new DelegatingResourceDescription();
description.addProperty("uuid");
description.addProperty("informationRequestType", Representation.DEFAULT);
description.addProperty("informationResourceId");
description.addProperty("informationResourceType", Representation.DEFAULT);
description.addProperty("sender", Representation.DEFAULT);
description.addProperty("recipient", Representation.DEFAULT);
description.addProperty("responded");
description.addProperty("allowed");
description.addSelfLink();
description.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL);
return description;
} else if (representation instanceof FullRepresentation) {
DelegatingResourceDescription description = new DelegatingResourceDescription();
description.addProperty("uuid");
description.addProperty("informationRequestType", Representation.FULL);
description.addProperty("informationResourceId");
description.addProperty("informationResourceType", Representation.FULL);
description.addProperty("sender", Representation.FULL);
description.addProperty("recipient", Representation.FULL);
description.addProperty("responded");
description.addProperty("allowed");
description.addProperty("auditInfo", findMethod("getAuditInfo"));
description.addSelfLink();
return description;
}
return null;
}
@Override
public DelegatingResourceDescription getCreatableProperties() {
DelegatingResourceDescription description = new DelegatingResourceDescription();
description.addProperty("informationRequestType");
description.addProperty("informationResourceId");
description.addProperty("informationResourceType");
description.addProperty("sender");
description.addProperty("recipient");
return description;
}
@Override
public DelegatingResourceDescription getUpdatableProperties() {
DelegatingResourceDescription description = new DelegatingResourceDescription();
description.addProperty("responded");
description.addProperty("allowed");
return description;
}
@Override
public InformationRequest getByUniqueId(String uuid) {
return Context.getService(InformationSharingService.class).getInformationRequestByUuid(uuid);
}
@Override
protected void delete(InformationRequest informationRequest, String s, RequestContext requestContext) throws ResponseException {
Context.getService(InformationSharingService.class).deleteInformationRequest(informationRequest);
}
@Override
public void purge(InformationRequest informationRequest, RequestContext requestContext) throws ResponseException {
Context.getService(InformationSharingService.class).deleteInformationRequest(informationRequest);
}
@Override
public InformationRequest newDelegate() {
return new InformationRequest();
}
@Override
public InformationRequest save(InformationRequest informationRequest) {
return Context.getService(InformationSharingService.class).saveInformationRequest(informationRequest);
}
@Override
protected PageableResult doGetAll(RequestContext context) throws ResponseException {
InformationSharingService service = Context.getService(InformationSharingService.class);
return new NeedsPaging<InformationRequest>(service.getAllInformationRequests(), context);
}
@PropertyGetter("informationRequestType")
public static InformationRequestType getInformationRequestType(InformationRequest informationRequest) {
return informationRequest.getInformationRequestType();
}
@PropertySetter("informationRequestType")
public static void setInformationRequestType(InformationRequest informationRequest, String requestTypeUuid) {
informationRequest.setInformationRequestType(Context.getService(InformationSharingService.class).getInformationRequestTypeByUuid(requestTypeUuid));
}
@PropertyGetter("informationResourceType")
public static InformationResourceType getInformationResourceType(InformationRequest informationRequest) {
return informationRequest.getInformationResourceType();
}
@PropertySetter("informationResourceType")
public static void setInformationResourceType(InformationRequest informationRequest, String resourceTypeUuid) {
informationRequest.setInformationResourceType(Context.getService(InformationSharingService.class).getInformationResourceTypeByUuid(resourceTypeUuid));
}
@PropertyGetter("sender")
public static Person getSender(InformationRequest informationRequest) {
return informationRequest.getSender();
}
@PropertySetter("sender")
public static void setSender(InformationRequest informationRequest, String senderUuid) {
Context.addProxyPrivilege(PrivilegeConstants.GET_PERSONS);
try {
informationRequest.setSender(Context.getPersonService().getPersonByUuid(senderUuid));
} finally {
Context.removeProxyPrivilege(PrivilegeConstants.GET_PERSONS);
}
}
@PropertyGetter("recipient")
public static Person getRecipient(InformationRequest informationRequest) {
return informationRequest.getRecipient();
}
@PropertySetter("recipient")
public static void setRecipient(InformationRequest informationRequest, String recipientUuid) {
Context.addProxyPrivilege(PrivilegeConstants.GET_PERSONS);
try {
informationRequest.setRecipient(Context.getPersonService().getPersonByUuid(recipientUuid));
} finally {
Context.removeProxyPrivilege(PrivilegeConstants.GET_PERSONS);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment