Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bah 406: Add new event type for all sellables #44

Merged
merged 4 commits into from Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -4,6 +4,7 @@
import org.openmrs.module.emrapi.utils.CustomJsonDateSerializer;

import java.util.Date;
import java.util.HashMap;

public class Resource {
private String id;
Expand All @@ -12,6 +13,9 @@ public class Resource {
private String name;
private Boolean isActive;

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
private HashMap<String, Object> properties;

public String getName() {
return name;
}
Expand Down Expand Up @@ -54,4 +58,12 @@ public void setLastUpdated(Date lastUpdated) {
this.lastUpdated = lastUpdated;
}

public HashMap<String, Object> getProperties() {
return properties;
}

public void setProperties(HashMap<String, Object> properties) {
this.properties = properties;
}

}
@@ -0,0 +1,27 @@
package org.bahmni.module.referencedata.labconcepts.mapper;

import org.openmrs.Concept;
import org.bahmni.module.referencedata.labconcepts.contract.Resource;

import java.util.HashMap;

public class AttributableResourceMapper extends ResourceMapper {

public AttributableResourceMapper() {super(null);}

protected AttributableResourceMapper(String parentConceptName) {
super(parentConceptName);
}

@Override
public Resource map(Concept concept) {
Resource resource = new Resource();
mapResource(resource, concept);
HashMap<String, Object> properties = new HashMap<>();
concept.getActiveAttributes().stream().forEach(a -> properties.put(a.getAttributeType().getName(), a.getValueReference()));
if (!properties.isEmpty()) {
resource.setProperties(properties);
}
return resource;
}
}
Expand Up @@ -17,6 +17,7 @@
import static org.bahmni.module.referencedata.labconcepts.model.event.ConceptServiceEventFactory.radiologyTestEvent;
import static org.bahmni.module.referencedata.labconcepts.model.event.ConceptServiceEventFactory.sampleEvent;
import static org.bahmni.module.referencedata.labconcepts.model.event.ConceptServiceEventFactory.testEvent;
import static org.bahmni.module.referencedata.labconcepts.model.event.ConceptServiceEventFactory.sellableTypeEvent;

public class Operation {

Expand All @@ -29,7 +30,8 @@ public class Operation {
labConceptSetEvent(),
allTestsAndPanelsConceptSetEvent(),
drugEvent(),
radiologyTestEvent()
radiologyTestEvent(),
sellableTypeEvent()
);

public Operation(Method method) {
Expand Down
Expand Up @@ -11,6 +11,7 @@ public class ConceptServiceEventFactory {
public static final String TESTS_AND_PANEL = "all-tests-and-panels";
public static final String DRUG = "drug";
public static final String RADIOLOGY = "radiology";
public static final String SELLABLE = "sellable";

public static ConceptServiceOperationEvent sampleEvent() {
return new SampleEvent(CONCEPT_URL, LAB, SAMPLE);
Expand Down Expand Up @@ -38,4 +39,8 @@ public static ConceptServiceOperationEvent drugEvent() {
public static ConceptServiceOperationEvent radiologyTestEvent() {
return new RadiologyTestEvent(CONCEPT_URL, LAB, RADIOLOGY);
}

public static ConceptServiceOperationEvent sellableTypeEvent() {
return new SellableTypeEvent(CONCEPT_URL, SELLABLE);
}
}
@@ -0,0 +1,45 @@
package org.bahmni.module.referencedata.labconcepts.model.event;

import org.openmrs.Concept;
import org.ict4h.atomfeed.server.service.Event;
import org.joda.time.DateTime;
import org.openmrs.ConceptAttribute;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

public class SellableTypeEvent implements ConceptServiceOperationEvent {

public static final String RESOURCE_TITLE = "reference data";
public static final String SELLABLE_ATTR_NAME = "sellable";
private final String url;
private final String category;
private List<String> supportedOperations = Arrays.asList("saveConcept", "updateConcept", "retireConcept", "purgeConcept");

public SellableTypeEvent(String url, String category) {
this.url = url;
this.category = category;
}

@Override
public Event asAtomFeedEvent(Object[] arguments) throws URISyntaxException {
Concept concept = (Concept) arguments[0];
String url = String.format(this.url, "resources", concept.getUuid());
return new Event(UUID.randomUUID().toString(), RESOURCE_TITLE, DateTime.now(), new URI(url), url, this.category);
}

@Override
public Boolean isApplicable(String operation, Object[] arguments) {
if (supportedOperations.contains(operation)
&& arguments.length > 0 && arguments[0] instanceof Concept) {
Concept concept = (Concept) arguments[0];
Collection<ConceptAttribute> activeAttributes = concept.getActiveAttributes();
return activeAttributes.stream().filter(a -> a.getAttributeType().getName().equalsIgnoreCase(SELLABLE_ATTR_NAME)).findFirst().isPresent();
}
return false;
}
}
@@ -0,0 +1,36 @@
package org.bahmni.module.referencedata.web.controller;

import org.bahmni.module.referencedata.labconcepts.mapper.AttributableResourceMapper;
import org.openmrs.Concept;
import org.bahmni.module.referencedata.labconcepts.contract.Resource;
import org.openmrs.api.ConceptService;
import org.openmrs.module.emrapi.encounter.exception.ConceptNotFoundException;
import org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "/rest/v1/reference-data/resources")
public class ResourcesController extends BaseRestController {
public static final String UNIDENTIFIED_RESOURCE = "No resource was found for specified uuid";
private ConceptService conceptService;
AttributableResourceMapper attributeResourceMapper;

@Autowired
public ResourcesController(ConceptService conceptService) {
this.conceptService = conceptService;
this.attributeResourceMapper = new AttributableResourceMapper();
}

@RequestMapping(value = "/{uuid}", method = RequestMethod.GET)
public Resource getResourceFromConcept(@PathVariable("uuid") String uuid) {
final Concept concept = conceptService.getConceptByUuid(uuid);
if (concept == null) {
throw new ConceptNotFoundException(UNIDENTIFIED_RESOURCE + uuid);
}
return attributeResourceMapper.map(concept);
}
}
31 changes: 31 additions & 0 deletions reference-data/omod/src/main/resources/liquibase.xml
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">

<!--
See http://www.liquibase.org/manual/home#available_database_refactorings
for a list of supported elements and attributes
-->

<changeSet id="ref-data-201902211530" author="angshu" context="0.93">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">
SELECT COUNT(*) FROM concept_attribute_type where name = 'sellable';
</sqlCheck>
</preConditions>
<comment>adding concept attribute type sellable</comment>
<insert tableName="concept_attribute_type">
<column name="name" value="sellable"/>
<column name="description" value="Reference concepts which are deemed sellable, serviceable"/>
<column name="datatype" value="org.openmrs.customdatatype.datatype.BooleanDatatype"/>
<column name="min_occurs" value="0"/>
<column name="creator" value="1"/>
<column name="date_created" valueComputed="curdate()"/>
<column name="retired" value="0"/>
<column name="uuid" valueComputed="uuid()"/>
</insert>
</changeSet>
</databaseChangeLog>
@@ -0,0 +1,41 @@
package org.bahmni.module.referencedata.labconcepts.model.event;

import org.ict4h.atomfeed.server.service.Event;
import org.junit.Assert;
import org.openmrs.Concept;
import org.junit.Test;
import org.openmrs.ConceptAttribute;
import org.openmrs.ConceptAttributeType;

import static org.bahmni.module.referencedata.labconcepts.model.event.ConceptServiceEventFactory.CONCEPT_URL;
import static org.bahmni.module.referencedata.labconcepts.model.event.ConceptServiceEventFactory.SELLABLE;

public class SellableTypeEventTest {

@Test
public void shouldRaiseEventForConceptWithSellableAttribute() throws Exception {
ConceptAttributeType cat = new ConceptAttributeType();
cat.setDatatypeClassname("org.openmrs.customdatatype.datatype.BooleanDatatype");
cat.setName("sellable");

Concept procedureConcept = new org.bahmni.test.builder.ConceptBuilder()
.withClass("Procedure")
.withUUID("9d583329-5fb1-4e50-9420-dcbbf6991fbc")
.withName("Dressing Procedure")
.build();

ConceptAttribute ca = new ConceptAttribute();
ca.setAttributeType(cat);
ca.setVoided(false);
ca.setValue(true);
procedureConcept.addAttribute(ca);

SellableTypeEvent sellableTypeEvent = new SellableTypeEvent(CONCEPT_URL, SELLABLE);
Assert.assertEquals(true, sellableTypeEvent.isApplicable("saveConcept", new Object[]{procedureConcept}));

Event event = sellableTypeEvent.asAtomFeedEvent(new Object[]{procedureConcept});
Assert.assertNotNull(event);
Assert.assertEquals(SELLABLE, event.getCategory());
Assert.assertEquals("/openmrs/ws/rest/v1/reference-data/resources/9d583329-5fb1-4e50-9420-dcbbf6991fbc", event.getContents());
}
}
Expand Up @@ -3,10 +3,7 @@
package org.bahmni.module.referencedata.web.controller;

import org.bahmni.module.referencedata.BaseIntegrationTest;
import org.bahmni.module.referencedata.labconcepts.contract.Department;
import org.bahmni.module.referencedata.labconcepts.contract.LabTest;
import org.bahmni.module.referencedata.labconcepts.contract.RadiologyTest;
import org.bahmni.module.referencedata.labconcepts.contract.Sample;
import org.bahmni.module.referencedata.labconcepts.contract.*;
import org.junit.Before;
import org.junit.Test;
import org.openmrs.Concept;
Expand Down Expand Up @@ -81,4 +78,14 @@ public void shouldPublishRadiologyTest() throws Exception {
assertEquals(radiologyTestConcept.getName(Context.getLocale()).getName(), testResponse.getName());
assertNotEquals(radiologyTestConcept.getRetired(), testResponse.getIsActive());
}

@Test
public void shouldReturnGenericConceptResource() throws Exception {
MockHttpServletRequest request = newGetRequest("/rest/v1/reference-data/resources/fe334cb7-t3tb-0037-70f7-kjditree2222");
MockHttpServletResponse response = handle(request);
Resource resource = deserialize(response, Resource.class);
assertEquals("fe334cb7-t3tb-0037-70f7-kjditree2222", resource.getId());
assertEquals("Dressing Procedure", resource.getName());
assertEquals("true", resource.getProperties().get("sellable"));
}
}
10 changes: 10 additions & 0 deletions reference-data/omod/src/test/resources/labDataSetup.xml
Expand Up @@ -176,4 +176,14 @@
<concept_name concept_id="1313" name="Answer2" locale="en" creator="1" date_created="2005-01-01 00:00:00.0"
concept_name_id="1313" voided="false" uuid="hgfd4cb7-hg8b-4007-free-0ebb94022222"
concept_name_type="FULLY_SPECIFIED" locale_preferred="0"/>

<concept_class concept_class_id="127" name="Procedure" description="Procedure Concept Class" creator="1"
date_created="2005-01-01 00:00:00.0" retired="false" uuid="9c3807b2-c2cc-11de-8d13-0010c6dffd1f"/>
<concept concept_id="1410" retired="0" datatype_id="2" class_id="11" is_set="0" creator="1"
date_created="2013-12-04 11:36:42" uuid="fe334cb7-t3tb-0037-70f7-kjditree2222"/>
<concept_name concept_id="1410" name="Dressing Procedure" locale="en" creator="1" date_created="2005-01-01 00:00:00.0"
concept_name_id="1413" voided="false" uuid="ghee4cb7-hg8b-4007-free-0ebb94022222"
concept_name_type="FULLY_SPECIFIED" locale_preferred="0"/>
<concept_attribute_type concept_attribute_type_id="11" name="sellable" datatype="org.openmrs.customdatatype.datatype.BooleanDatatype" uuid="8425cc50-6f9f-11e0-8414-001e378eb66f" creator="1" date_created="2005-01-01 00:00:00.0" min_occurs="0" retired="false" />
<concept_attribute concept_attribute_id="101" concept_id="1410" attribute_type_id="11" value_reference="true" uuid="2b1bdb18-6faa-11e0-8414-001e378eb67e" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" />
</dataset>