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

Implement the FHIR endpoints in openmrs Stock Management #27

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@

<!-- End OpenMRS core -->

<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>fhir2-api</artifactId>
</dependency>

<dependency>
<groupId>org.openmrs.fhir</groupId>
<artifactId>fhir-structures-backport-r4</artifactId>
</dependency>

<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.openmrs.module.stockmanagement.api;

import org.openmrs.module.stockmanagement.fhir.InventoryItem;

import java.util.List;

public interface InventoryItemService {
InventoryItem read(String id);
List<InventoryItem> search(String code, String status);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.openmrs.module.stockmanagement.api.impl;

import org.openmrs.module.stockmanagement.api.InventoryItemService;
import org.openmrs.module.stockmanagement.fhir.InventoryItem;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class InventoryItemServiceImpl implements InventoryItemService {

private List<InventoryItem> inventoryItems;

public InventoryItemServiceImpl() {
this.inventoryItems = new ArrayList<>();
}

public InventoryItemServiceImpl(List<InventoryItem> inventoryItems) {
this.inventoryItems = inventoryItems;
}

@Override
public InventoryItem read(String id) {
return inventoryItems.stream()
.filter(item -> item.getIdentifierFirstRep().getValue().equals(id))
.findFirst()
.orElse(null);
}

@Override
public List<InventoryItem> search(String code, String status) {
return inventoryItems.stream()
.filter(item -> (code == null || item.getCodeFirstRep().getText().equals(code)) &&
(status == null || (item.getStatus() != null && item.getStatus().toCode().equals(status))))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.openmrs.module.stockmanagement.fhir;

import org.hl7.fhir.r4.model.CodeableConcept;
import org.hl7.fhir.r4.model.Identifier;
import org.hl7.fhir.r4.model.Quantity;
import org.hl7.fhir.r4.model.ResourceType;
import org.hl7.fhir.r4.model.DomainResource;
import org.openmrs.fhir.InventoryItem.InventoryItemStatusCodes;

import ca.uhn.fhir.model.api.annotation.ResourceDef;
import ca.uhn.fhir.model.api.annotation.SearchParamDefinition;
import ca.uhn.fhir.rest.gclient.TokenClientParam;

import java.util.List;
@ResourceDef(name = "InventoryItem", profile = "http://hl7.org/fhir/StructureDefinition/InventoryItem")
public class InventoryItem extends DomainResource {
private InventoryItemStatusCodes status;
private List<CodeableConcept> code;
private Quantity netContent;
private List<Identifier> identifier;

@SearchParamDefinition(name = "code", path = "InventoryItem.code", description = "Search for products that match this code", type = "token")
public static final String SP_CODE = "code";

@SearchParamDefinition(name = "status", path = "InventoryItem.status", description = "The status of the item", type = "token")
public static final String SP_STATUS = "status";

public static final TokenClientParam CODE = new TokenClientParam(SP_CODE);
public static final TokenClientParam STATUS = new TokenClientParam(SP_STATUS);

// Getters and Setters
public InventoryItemStatusCodes getStatus() {
return status;
}

public void setStatus(InventoryItemStatusCodes status) {
this.status = status;
}

public List<CodeableConcept> getCode() {
return code;
}

public void setCode(List<CodeableConcept> code) {
this.code = code;
}

public Quantity getNetContent() {
return netContent;
}

public void setNetContent(Quantity netContent) {
this.netContent = netContent;
}

public List<Identifier> getIdentifier() {
return identifier;
}

public void setIdentifier(List<Identifier> identifier) {
this.identifier = identifier;
}

public Identifier getIdentifierFirstRep() {
return identifier != null && !identifier.isEmpty() ? identifier.get(0) : null;
}

public CodeableConcept getCodeFirstRep() {
return code != null && !code.isEmpty() ? code.get(0) : null;
}

@Override
public ResourceType getResourceType() {
return ResourceType.Basic;
}

@Override
public InventoryItem copy() {
InventoryItem copy = new InventoryItem();
copy.status = this.status;
copy.code = this.code;
copy.netContent = this.netContent;
copy.identifier = this.identifier;
return copy;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@

package org.openmrs.module.stockmanagement.fhir;

import org.hl7.fhir.r4.model.CodeableConcept;
import org.hl7.fhir.r4.model.Identifier;
import org.hl7.fhir.r4.model.Quantity;
import org.junit.jupiter.api.Test;
import org.openmrs.fhir.InventoryItem.InventoryItemStatusCodes;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import org.hl7.fhir.r4.model.ResourceType;


public class InventoryItemTest {

@Test
public void testGetStatus() {
InventoryItem item = new InventoryItem();
item.setStatus(InventoryItemStatusCodes.ACTIVE);
assertEquals(InventoryItemStatusCodes.ACTIVE, item.getStatus());
}

@Test
public void testSetStatus() {
InventoryItem item = new InventoryItem();
item.setStatus(InventoryItemStatusCodes.INACTIVE);
assertEquals(InventoryItemStatusCodes.INACTIVE, item.getStatus());
}

@Test
public void testGetCode() {
InventoryItem item = new InventoryItem();
List<CodeableConcept> codes = new ArrayList<>();
codes.add(new CodeableConcept().setText("Code1"));
item.setCode(codes);
assertEquals(codes, item.getCode());
}

@Test
public void testSetCode() {
InventoryItem item = new InventoryItem();
List<CodeableConcept> codes = new ArrayList<>();
codes.add(new CodeableConcept().setText("Code2"));
item.setCode(codes);
assertEquals(codes, item.getCode());
}

@Test
public void testGetNetContent() {
InventoryItem item = new InventoryItem();
Quantity quantity = new Quantity().setValue(10);
item.setNetContent(quantity);
assertEquals(quantity, item.getNetContent());
}

@Test
public void testSetNetContent() {
InventoryItem item = new InventoryItem();
Quantity quantity = new Quantity().setValue(20);
item.setNetContent(quantity);
assertEquals(quantity, item.getNetContent());
}

@Test
public void testGetIdentifier() {
InventoryItem item = new InventoryItem();
List<Identifier> identifiers = new ArrayList<>();
identifiers.add(new Identifier().setSystem("System1").setValue("Value1"));
item.setIdentifier(identifiers);
assertEquals(identifiers, item.getIdentifier());
}

@Test
public void testSetIdentifier() {
InventoryItem item = new InventoryItem();
List<Identifier> identifiers = new ArrayList<>();
identifiers.add(new Identifier().setSystem("System2").setValue("Value2"));
item.setIdentifier(identifiers);
assertEquals(identifiers, item.getIdentifier());
}

@Test
public void testGetIdentifierFirstRep() {
InventoryItem item = new InventoryItem();
List<Identifier> identifiers = new ArrayList<>();
Identifier identifier = new Identifier().setSystem("System3").setValue("Value3");
identifiers.add(identifier);
item.setIdentifier(identifiers);
assertEquals(identifier, item.getIdentifierFirstRep());
}

@Test
public void testGetCodeFirstRep() {
InventoryItem item = new InventoryItem();
List<CodeableConcept> codes = new ArrayList<>();
CodeableConcept code = new CodeableConcept().setText("Code3");
codes.add(code);
item.setCode(codes);
assertEquals(code, item.getCodeFirstRep());
}

@Test
public void testCopy() {
InventoryItem item = new InventoryItem();
item.setStatus(InventoryItemStatusCodes.ACTIVE);
List<CodeableConcept> codes = new ArrayList<>();
codes.add(new CodeableConcept().setText("Code4"));
item.setCode(codes);
Quantity quantity = new Quantity().setValue(30);
item.setNetContent(quantity);
List<Identifier> identifiers = new ArrayList<>();
identifiers.add(new Identifier().setSystem("System4").setValue("Value4"));
item.setIdentifier(identifiers);

InventoryItem copy = item.copy();
assertEquals(item.getStatus(), copy.getStatus());
assertEquals(item.getCode(), copy.getCode());
assertEquals(item.getNetContent(), copy.getNetContent());
assertEquals(item.getIdentifier(), copy.getIdentifier());
}

@Test
public void testGetResourceType() {
InventoryItem item = new InventoryItem();
assertEquals(ResourceType.Basic, item.getResourceType());
}
}
46 changes: 45 additions & 1 deletion omod/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.openmrs.module</groupId>
Expand Down Expand Up @@ -41,6 +43,48 @@

<!-- End OpenMRS core -->

<!-- JUnit 5 dependency -->

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>

<!-- Mockito dependency -->

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.6.28</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.4.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>fhir2-api</artifactId>
</dependency>

<dependency>
<groupId>org.openmrs.fhir</groupId>
<artifactId>fhir-structures-backport-r4</artifactId>
</dependency>

<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest-omod</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.openmrs.module.stockmanagement.config;

import ca.uhn.fhir.context.FhirContext;
import org.openmrs.module.stockmanagement.web.resource.InventoryItemProvider;
import org.openmrs.fhir.InventoryItem;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class StockManagementFhirResourceConfig {

@Bean
public FhirContext fhirContext() {
FhirContext fhirContext = FhirContext.forR4();
fhirContext.getResourceDefinition(InventoryItem.class);
return fhirContext;
}

@Bean
public InventoryItemProvider InventoryItemProvider() {
return new InventoryItemProvider();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.openmrs.module.stockmanagement.web.controller;

import org.openmrs.module.stockmanagement.api.InventoryItemService;
import org.openmrs.module.stockmanagement.fhir.InventoryItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/openmrs/ws/fhir2/R4/InventoryItem")
public class InventoryItemController {

@Autowired
private InventoryItemService inventoryItemService;

@GetMapping("/{id}")
public InventoryItem getInventoryItem(@PathVariable String id) {
return inventoryItemService.read(id);
}

@GetMapping
public List<InventoryItem> searchInventoryItems(@RequestParam(required = false) String code,
@RequestParam(required = false) String status) {
return inventoryItemService.search(code, status);
}
}
Loading
Loading