Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SYNCT-255: Added unifyService (#107)
* SYNCT-255: Added unifyService * SYNCT-255: Added handling patients in RESTClientHelper
- Loading branch information
Showing
7 changed files
with
328 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
api/src/main/java/org/openmrs/module/sync2/api/model/audit/AuditMessageResource2_0.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package org.openmrs.module.sync2.api.model.audit; | ||
|
||
import org.openmrs.api.context.Context; | ||
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.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.impl.DataDelegatingCrudResource; | ||
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription; | ||
import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException; | ||
import org.openmrs.module.webservices.rest.web.response.ResponseException; | ||
|
||
|
||
/** | ||
* {@link Resource} for {@link AuditMessage}, supporting standard CRUD operations | ||
*/ | ||
@Resource(name = RestConstants.VERSION_1 + "/auditmessage", supportedClass = AuditMessage.class, supportedOpenmrsVersions = { | ||
"2.0.*", "2.1.*", "2.2.*" }) | ||
public class AuditMessageResource2_0 extends DataDelegatingCrudResource<AuditMessage> { | ||
|
||
/** | ||
* @see org.openmrs.module.sync2.web.controller.rest.SyncAuditRestController | ||
*/ | ||
@Override | ||
public AuditMessage getByUniqueId(String s) { | ||
throw new ResourceDoesNotSupportOperationException(); | ||
} | ||
|
||
/** | ||
* @see org.openmrs.module.sync2.web.controller.rest.SyncAuditRestController | ||
*/ | ||
@Override | ||
protected void delete(AuditMessage auditMessage, String s, RequestContext requestContext) throws ResponseException { | ||
throw new ResourceDoesNotSupportOperationException(); | ||
} | ||
|
||
/** | ||
* @see org.openmrs.module.sync2.web.controller.rest.SyncAuditRestController | ||
*/ | ||
@Override | ||
public AuditMessage newDelegate() { | ||
throw new ResourceDoesNotSupportOperationException(); | ||
} | ||
|
||
/** | ||
* @see org.openmrs.module.sync2.web.controller.rest.SyncAuditRestController | ||
*/ | ||
@Override | ||
public AuditMessage save(AuditMessage auditMessage) { | ||
throw new ResourceDoesNotSupportOperationException(); | ||
} | ||
|
||
/** | ||
* @see org.openmrs.module.sync2.web.controller.rest.SyncAuditRestController | ||
*/ | ||
@Override | ||
public void purge(AuditMessage auditMessage, RequestContext requestContext) throws ResponseException { | ||
throw new ResourceDoesNotSupportOperationException(); | ||
} | ||
|
||
@Override | ||
public DelegatingResourceDescription getRepresentationDescription(Representation rep) { | ||
if (rep instanceof DefaultRepresentation) { | ||
DelegatingResourceDescription description = new DelegatingResourceDescription(); | ||
description.addProperty("uuid"); | ||
description.addProperty("display"); | ||
description.addProperty("voided"); | ||
description.addProperty("success"); | ||
description.addProperty("timestamp"); | ||
description.addProperty("resourceName"); | ||
description.addProperty("usedResourceUrl"); | ||
description.addProperty("availableResourceUrls"); | ||
description.addProperty("parentUrl"); | ||
description.addProperty("localUrl"); | ||
description.addProperty("details"); | ||
description.addProperty("action"); | ||
description.addProperty("operation"); | ||
description.addProperty("linkType"); | ||
description.addProperty("nextMessageUuid"); | ||
description.addSelfLink(); | ||
description.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL); | ||
return description; | ||
} else if (rep instanceof FullRepresentation) { | ||
DelegatingResourceDescription description = new DelegatingResourceDescription(); | ||
description.addProperty("uuid"); | ||
description.addProperty("display"); | ||
description.addProperty("voided"); | ||
description.addProperty("auditInfo"); | ||
description.addProperty("success"); | ||
description.addProperty("timestamp"); | ||
description.addProperty("resourceName"); | ||
description.addProperty("usedResourceUrl"); | ||
description.addProperty("availableResourceUrls"); | ||
description.addProperty("parentUrl"); | ||
description.addProperty("localUrl"); | ||
description.addProperty("details"); | ||
description.addProperty("action"); | ||
description.addProperty("operation"); | ||
description.addProperty("linkType"); | ||
description.addProperty("nextMessageUuid"); | ||
description.addSelfLink(); | ||
return description; | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* @param auditMessage | ||
* @return auditMessage's resourceName and date | ||
*/ | ||
@PropertyGetter("display") | ||
public String getDisplayString(AuditMessage auditMessage) { | ||
String ret = auditMessage.getResourceName() == null ? "?" : auditMessage.getResourceName(); | ||
ret += " "; | ||
ret += auditMessage.getTimestamp() == null ? "?" : Context.getDateFormat().format( | ||
auditMessage.getTimestamp()); | ||
return ret; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
api/src/main/java/org/openmrs/module/sync2/api/model/enums/CategoryEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package org.openmrs.module.sync2.api.model.enums; | ||
|
||
import org.openmrs.Allergy; | ||
import org.openmrs.Cohort; | ||
|
||
import org.openmrs.DrugOrder; | ||
import org.openmrs.Encounter; | ||
import org.openmrs.Form; | ||
import org.openmrs.Location; | ||
import org.openmrs.Obs; | ||
import org.openmrs.Patient; | ||
import org.openmrs.PatientProgram; | ||
import org.openmrs.Person; | ||
import org.openmrs.Privilege; | ||
import org.openmrs.Program; | ||
import org.openmrs.Provider; | ||
import org.openmrs.Relationship; | ||
import org.openmrs.TestOrder; | ||
import org.openmrs.User; | ||
import org.openmrs.Visit; | ||
import org.openmrs.VisitType; | ||
import org.openmrs.module.fhir.api.util.FHIRConstants; | ||
import org.openmrs.module.sync2.api.model.audit.AuditMessage; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public enum CategoryEnum { | ||
LOCATION(FHIRConstants.CATEGORY_LOCATION, Location.class), | ||
OBSERVATION(FHIRConstants.CATEGORY_OBSERVATION, Obs.class), | ||
ENCOUNTER(FHIRConstants.CATEGORY_ENCOUNTER, Encounter.class), | ||
VISIT(FHIRConstants.CATEGORY_VISIT, Visit.class), | ||
PROVIDER(FHIRConstants.CATEGORY_PROVIDER, Provider.class), | ||
DRUG_ORDER(FHIRConstants.CATEGORY_DRUG_ORDER, DrugOrder.class), | ||
TEST_ORDER(FHIRConstants.CATEGORY_TEST_ORDER, TestOrder.class), | ||
FORM("form", Form.class), | ||
PATIENT_PROGRAM("patient_program", PatientProgram.class), | ||
PERSON(FHIRConstants.CATEGORY_PERSON, Person.class), | ||
PATIENT(FHIRConstants.CATEGORY_PATIENT, Patient.class), | ||
RELATIONSHIP("relationship", Relationship.class), | ||
COHORT(FHIRConstants.CATEGORY_COHORT, Cohort.class), | ||
VISIT_TYPE("visit_type", VisitType.class), | ||
USER("user", User.class), | ||
PROGRAM("program", Program.class), | ||
PRIVILEGE("privilege", Privilege.class), | ||
AUDIT_MESSAGE("audit_message", AuditMessage.class), | ||
ALLERGY(FHIRConstants.CATEGORY_ALLERGY, Allergy.class); | ||
|
||
private static final Map<String, CategoryEnum> MAP; | ||
|
||
private final String category; | ||
|
||
private final Class clazz; | ||
|
||
CategoryEnum(String category, Class clazz) { | ||
this.category = category; | ||
this.clazz = clazz; | ||
} | ||
|
||
public String getCategory() { | ||
return category; | ||
} | ||
|
||
public Class getClazz() { | ||
return clazz; | ||
} | ||
|
||
static { | ||
MAP = new HashMap<>(); | ||
MAP.put(LOCATION.getCategory(), LOCATION); | ||
MAP.put(OBSERVATION.getCategory(), OBSERVATION); | ||
MAP.put(ENCOUNTER.getCategory(), ENCOUNTER); | ||
MAP.put(VISIT.getCategory(), VISIT); | ||
MAP.put(PROVIDER.getCategory(), PROVIDER); | ||
MAP.put(DRUG_ORDER.getCategory(), DRUG_ORDER); | ||
MAP.put(TEST_ORDER.getCategory(), TEST_ORDER); | ||
MAP.put(FORM.getCategory(), FORM); | ||
MAP.put(PATIENT_PROGRAM.getCategory(), PATIENT_PROGRAM); | ||
MAP.put(PERSON.getCategory(), PERSON); | ||
MAP.put(PATIENT.getCategory(), PATIENT); | ||
MAP.put(RELATIONSHIP.getCategory(), RELATIONSHIP); | ||
MAP.put(COHORT.getCategory(), COHORT); | ||
MAP.put(VISIT_TYPE.getCategory(), VISIT_TYPE); | ||
MAP.put(USER.getCategory(), USER); | ||
MAP.put(PROGRAM.getCategory(), PROGRAM); | ||
MAP.put(PRIVILEGE.getCategory(), PRIVILEGE); | ||
MAP.put(AUDIT_MESSAGE.getCategory(), AUDIT_MESSAGE); | ||
MAP.put(ALLERGY.getCategory(), ALLERGY); | ||
} | ||
|
||
public static CategoryEnum getByCategory(String category) { | ||
return MAP.get(category); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
api/src/main/java/org/openmrs/module/sync2/api/service/UnifyService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.openmrs.module.sync2.api.service; | ||
|
||
import javax.transaction.NotSupportedException; | ||
|
||
public interface UnifyService { | ||
|
||
Object unifyObject(Object object, String category, String clientName) throws NotSupportedException; | ||
} |
44 changes: 44 additions & 0 deletions
44
api/src/main/java/org/openmrs/module/sync2/api/service/impl/UnifyServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.openmrs.module.sync2.api.service.impl; | ||
|
||
import org.openmrs.module.fhir.api.helper.FHIRClientHelper; | ||
import org.openmrs.module.sync2.SyncConstants; | ||
import org.openmrs.module.sync2.api.model.audit.AuditMessage; | ||
import org.openmrs.module.sync2.api.model.enums.CategoryEnum; | ||
import org.openmrs.module.sync2.api.service.UnifyService; | ||
import org.openmrs.module.webservices.rest.SimpleObject; | ||
import org.openmrs.module.webservices.rest.web.ConversionUtil; | ||
import org.openmrs.module.webservices.rest.web.representation.Representation; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.transaction.NotSupportedException; | ||
|
||
@Service("sync2.unifyService") | ||
public class UnifyServiceImpl implements UnifyService { | ||
|
||
@Override | ||
public Object unifyObject(Object object, String category, String clientName) throws NotSupportedException { | ||
Object result = null; | ||
|
||
if (isObjectAlreadyUnified(object)) { | ||
result = object; | ||
} else { | ||
CategoryEnum cat = CategoryEnum.getByCategory(category); | ||
|
||
if (SyncConstants.FHIR_CLIENT.equals(clientName)) { | ||
FHIRClientHelper helper = new FHIRClientHelper(); | ||
result = helper.convertToOpenMrsObject(object, category); | ||
result = ConversionUtil.convertToRepresentation(result, Representation.FULL); | ||
} else if (AuditMessage.class.isAssignableFrom(cat.getClazz())) { | ||
result = ConversionUtil.convertToRepresentation(object, Representation.FULL); | ||
} else { | ||
throw new NotSupportedException(String.format("Category %s not supported.", category)); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private boolean isObjectAlreadyUnified(Object object) { | ||
return object instanceof SimpleObject; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.