diff --git a/api/src/main/java/org/openmrs/module/ipd/api/dao/CareTeamDAO.java b/api/src/main/java/org/openmrs/module/ipd/api/dao/CareTeamDAO.java new file mode 100644 index 0000000..f07d2e8 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/ipd/api/dao/CareTeamDAO.java @@ -0,0 +1,14 @@ +package org.openmrs.module.ipd.api.dao; + +import org.openmrs.Visit; +import org.openmrs.api.db.DAOException; +import org.openmrs.module.ipd.api.model.CareTeam; +import org.openmrs.module.ipd.api.model.Schedule; + +public interface CareTeamDAO { + + CareTeam saveCareTeam(CareTeam careTeam) throws DAOException; + + CareTeam getCareTeamByVisit(Visit visit) throws DAOException; + +} diff --git a/api/src/main/java/org/openmrs/module/ipd/api/dao/impl/HibernateCareTeamDAO.java b/api/src/main/java/org/openmrs/module/ipd/api/dao/impl/HibernateCareTeamDAO.java new file mode 100644 index 0000000..f696dbc --- /dev/null +++ b/api/src/main/java/org/openmrs/module/ipd/api/dao/impl/HibernateCareTeamDAO.java @@ -0,0 +1,41 @@ +package org.openmrs.module.ipd.api.dao.impl; + +import org.hibernate.SessionFactory; +import org.hibernate.query.Query; +import org.openmrs.Visit; +import org.openmrs.api.db.DAOException; +import org.openmrs.module.ipd.api.dao.CareTeamDAO; +import org.openmrs.module.ipd.api.model.CareTeam; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; + +@Repository +public class HibernateCareTeamDAO implements CareTeamDAO { + + private static final Logger log = LoggerFactory.getLogger(HibernateCareTeamDAO.class); + private final SessionFactory sessionFactory; + + @Autowired + public HibernateCareTeamDAO(SessionFactory sessionFactory) { + this.sessionFactory = sessionFactory; + } + + @Override + public CareTeam saveCareTeam(CareTeam careTeam) throws DAOException { + sessionFactory.getCurrentSession().saveOrUpdate(careTeam); + return careTeam; + } + + @Override + public CareTeam getCareTeamByVisit(Visit visit) throws DAOException { + Query query = sessionFactory.getCurrentSession() + .createQuery("FROM CareTeam careteam " + + "WHERE careteam.visit = :visit "); + + query.setParameter("visit", visit); + + return (CareTeam) query.uniqueResult(); + } +} diff --git a/api/src/main/java/org/openmrs/module/ipd/api/model/CareTeam.java b/api/src/main/java/org/openmrs/module/ipd/api/model/CareTeam.java new file mode 100644 index 0000000..9f6f47c --- /dev/null +++ b/api/src/main/java/org/openmrs/module/ipd/api/model/CareTeam.java @@ -0,0 +1,105 @@ +package org.openmrs.module.ipd.api.model; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import org.openmrs.BaseChangeableOpenmrsData; +import org.openmrs.Concept; +import org.openmrs.Patient; +import org.openmrs.Visit; + +import javax.persistence.*; +import java.util.Date; +import java.util.Set; + +@Data +@NoArgsConstructor +@EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = false) +@Entity +@Table(name = "care_team") +public class CareTeam extends BaseChangeableOpenmrsData { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "care_team_id") + private Integer careTeamId; + + /** + * FHIR:subject + * Patient for Whom care team is for + */ + @ManyToOne(optional = true) + @JoinColumn(name = "patient_id") + private Patient patient; + + @OneToOne + @JoinColumn(name = "visit_id", referencedColumnName = "visit_id") + private Visit visit; + + /** + * FHIR:period.start + * Starting time with inclusive boundary + */ + @Column(name = "start_time") + private Date startTime; + + /** + * FHIR:period.end + * Ending time with inclusive boundary, if not ongoing + */ + @Column(name = "end_time") + private Date endTime; + + /** + * FHIR:performer + * @see + * https://build.fhir.org/careteam-definitions.html#CareTeam.participant + * Identifies all people and organizations who are expected to be involved in the care team. + */ + @OneToMany(cascade = CascadeType.ALL) + @JoinColumn(name = "care_team_id") + private Set participants; + + + @Override + public Integer getId() { + return this.careTeamId; + } + + @Override + public void setId(Integer careTeamId) { + this.careTeamId=careTeamId; + } + + public Patient getPatient() { + return patient; + } + + public void setPatient(Patient patient) { + this.patient = patient; + } + + public Visit getVisit() { + return visit; + } + + public void setVisit(Visit visit) { + this.visit = visit; + } + + public Date getStartTime() { + return startTime; + } + + public void setStartTime(Date startTime) { + this.startTime = startTime; + } + + public Date getEndTime() { + return endTime; + } + + public void setEndTime(Date endTime) { + this.endTime = endTime; + } +} diff --git a/api/src/main/java/org/openmrs/module/ipd/api/model/CareTeamParticipant.java b/api/src/main/java/org/openmrs/module/ipd/api/model/CareTeamParticipant.java new file mode 100644 index 0000000..250ad6f --- /dev/null +++ b/api/src/main/java/org/openmrs/module/ipd/api/model/CareTeamParticipant.java @@ -0,0 +1,119 @@ +package org.openmrs.module.ipd.api.model; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import org.openmrs.BaseChangeableOpenmrsData; +import org.openmrs.Concept; +import org.openmrs.Provider; + +import javax.persistence.*; +import java.util.Date; +import java.util.Objects; + +@Data +@NoArgsConstructor +@EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = false) +@Entity +@Table(name = "care_team_participant") +public class CareTeamParticipant extends BaseChangeableOpenmrsData { + + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "care_team_participant_id") + private Integer careTeamParticipantId; + + /** + * FHIR:member + * Indicates Who is involved . + */ + @OneToOne(optional = false) + @JoinColumn(name = "provider_id") + private Provider provider; + + /** + * FHIR:function + * @see + * https://hl7.org/fhir/valueset-participant-role.html + * + * i.e. performer, verifier, witness + */ + @ManyToOne(optional = true) + @JoinColumn(name = "role") + private Concept role; + + /** + * FHIR:coverage.coveragePeriod.start + * Starting time with inclusive boundary + */ + @Column(name = "start_time") + private Date startTime; + + /** + * FHIR:coverage.coveragePeriod.end + * Ending time with inclusive boundary, if not ongoing + */ + @Column(name = "end_time") + private Date endTime; + + @Override + public Integer getId() { + return getCareTeamParticipantId(); + } + + @Override + public void setId(Integer careTeamParticipantId) { + this.careTeamParticipantId=careTeamParticipantId; + } + + public Provider getProvider() { + return provider; + } + + public void setProvider(Provider provider) { + this.provider = provider; + } + + public Concept getRole() { + return role; + } + + public void setRole(Concept role) { + this.role = role; + } + + public Date getStartTime() { + return startTime; + } + + public void setStartTime(Date startTime) { + this.startTime = startTime; + } + + public Date getEndTime() { + return endTime; + } + + public void setEndTime(Date endTime) { + this.endTime = endTime; + } + + public boolean equals(Object obj) { + if (this == obj) { + return true; + } else if (obj != null && this.getClass() == obj.getClass()) { + CareTeamParticipant other = (CareTeamParticipant) obj; + return Objects.equals(this.getUuid(), other.getUuid()); + } else { + return false; + } + } + + public int hashCode() { + int hash = Objects.hash(new Object[]{this.getUuid()}); + return hash; + } + +} diff --git a/api/src/main/java/org/openmrs/module/ipd/api/service/CareTeamService.java b/api/src/main/java/org/openmrs/module/ipd/api/service/CareTeamService.java new file mode 100644 index 0000000..83b235c --- /dev/null +++ b/api/src/main/java/org/openmrs/module/ipd/api/service/CareTeamService.java @@ -0,0 +1,18 @@ +package org.openmrs.module.ipd.api.service; + +import org.openmrs.Visit; +import org.openmrs.api.APIException; +import org.openmrs.module.ipd.api.model.CareTeam; +import org.openmrs.module.ipd.api.model.Schedule; +import org.springframework.stereotype.Service; +import org.openmrs.api.OpenmrsService; + + +@Service +public interface CareTeamService extends OpenmrsService { + + CareTeam saveCareTeam(CareTeam careTeam) throws APIException; + + CareTeam getCareTeamByVisit(Visit visit) throws APIException; + +} diff --git a/api/src/main/java/org/openmrs/module/ipd/api/service/impl/CareTeamServiceImpl.java b/api/src/main/java/org/openmrs/module/ipd/api/service/impl/CareTeamServiceImpl.java new file mode 100644 index 0000000..84c253e --- /dev/null +++ b/api/src/main/java/org/openmrs/module/ipd/api/service/impl/CareTeamServiceImpl.java @@ -0,0 +1,39 @@ +package org.openmrs.module.ipd.api.service.impl; + + +import org.openmrs.Visit; +import org.openmrs.api.APIException; +import org.openmrs.api.impl.BaseOpenmrsService; +import org.openmrs.module.ipd.api.dao.CareTeamDAO; +import org.openmrs.module.ipd.api.model.CareTeam; +import org.openmrs.module.ipd.api.model.Schedule; +import org.openmrs.module.ipd.api.service.CareTeamService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional +public class CareTeamServiceImpl extends BaseOpenmrsService implements CareTeamService { + + private static final Logger log = LoggerFactory.getLogger(CareTeamServiceImpl.class); + + private final CareTeamDAO careTeamDAO; + + @Autowired + public CareTeamServiceImpl(CareTeamDAO careTeamDAO) { + this.careTeamDAO = careTeamDAO; + } + + @Override + public CareTeam saveCareTeam(CareTeam careTeam) throws APIException { + return careTeamDAO.saveCareTeam(careTeam); + } + + @Override + public CareTeam getCareTeamByVisit(Visit visit) throws APIException { + return careTeamDAO.getCareTeamByVisit(visit); + } +} diff --git a/api/src/main/java/org/openmrs/module/ipd/api/util/DateTimeUtil.java b/api/src/main/java/org/openmrs/module/ipd/api/util/DateTimeUtil.java index 34f533a..7588d4b 100644 --- a/api/src/main/java/org/openmrs/module/ipd/api/util/DateTimeUtil.java +++ b/api/src/main/java/org/openmrs/module/ipd/api/util/DateTimeUtil.java @@ -2,6 +2,7 @@ import java.time.*; import java.util.Date; +import java.util.concurrent.TimeUnit; public class DateTimeUtil { public static LocalDateTime convertEpocUTCToLocalTimeZone(long utcTime) { @@ -19,4 +20,10 @@ public static LocalDateTime convertDateToLocalDateTime(Date date) { public static Date convertLocalDateTimeDate(LocalDateTime localDateTime) { return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); } + + public static Date convertEpochTimeToDate(Long utcTime) { + return new Date(TimeUnit.SECONDS.toMillis(utcTime)); + } + + } \ No newline at end of file diff --git a/api/src/main/resources/liquibase.xml b/api/src/main/resources/liquibase.xml index a38f065..ec5ae8e 100644 --- a/api/src/main/resources/liquibase.xml +++ b/api/src/main/resources/liquibase.xml @@ -300,4 +300,94 @@ + + + + + + + Creating care_team table + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/api/src/test/java/org/openmrs/module/ipd/api/dao/impl/HibernateCareTeamDAOIntegrationTest.java b/api/src/test/java/org/openmrs/module/ipd/api/dao/impl/HibernateCareTeamDAOIntegrationTest.java new file mode 100644 index 0000000..98294be --- /dev/null +++ b/api/src/test/java/org/openmrs/module/ipd/api/dao/impl/HibernateCareTeamDAOIntegrationTest.java @@ -0,0 +1,95 @@ +package org.openmrs.module.ipd.api.dao.impl; + +import org.hibernate.SessionFactory; +import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.openmrs.*; +import org.openmrs.api.context.Context; +import org.openmrs.module.ipd.api.BaseIntegrationTest; +import org.openmrs.module.ipd.api.dao.CareTeamDAO; +import org.openmrs.module.ipd.api.model.CareTeam; +import org.openmrs.module.ipd.api.model.CareTeamParticipant; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.*; + +public class HibernateCareTeamDAOIntegrationTest extends BaseIntegrationTest { + + @Autowired + private CareTeamDAO careTeamDAO; + + @Autowired + private SessionFactory sessionFactory; + + @Test + public void shouldSaveTheCareTeamCreatedForPatientGivenPatientVisit() { + + executeDataSet("CareTeamDAOTestData.xml"); + + Concept testConcept = Context.getConceptService().getConceptByName("UNKNOWN"); + Visit visit=Context.getVisitService().getVisitByUuid("84d8b838-1111-11e3-b47b-c6959a448789"); + Provider provider=Context.getProviderService().getProviderByUuid("2bdc3f7d-d911-401a-84e9-5494dda83e8e"); + + + CareTeamParticipant participant = new CareTeamParticipant(); + participant.setRole(testConcept); + participant.setStartTime(visit.getStartDatetime()); + participant.setProvider(provider); + Set participantSet=new HashSet<>(); + participantSet.add(participant); + + CareTeam careTeam = new CareTeam(); + careTeam.setPatient(visit.getPatient()); + careTeam.setStartTime(visit.getStartDatetime()); + careTeam.setVisit(visit); + careTeam.setParticipants(participantSet); + + CareTeam savedCareTeam = careTeamDAO.saveCareTeam(careTeam); + + List participantsList = new ArrayList<>(savedCareTeam.getParticipants()); + + Assertions.assertEquals(visit.getPatient().getPatientId(), savedCareTeam.getPatient().getPatientId()); + Assertions.assertEquals(provider, participantsList.get(0).getProvider()); + Assertions.assertEquals(testConcept, participantsList.get(0).getRole()); + Assertions.assertEquals(visit, savedCareTeam.getVisit()); + + sessionFactory.getCurrentSession().delete(savedCareTeam); + } + + + @Test + public void shouldGetCareTeamGivenPatientVisit() { + + executeDataSet("CareTeamDAOTestData.xml"); + + Concept testConcept = Context.getConceptService().getConceptByName("UNKNOWN"); + Visit visit=Context.getVisitService().getVisitByUuid("84d8b838-1111-11e3-b47b-c6959a448789"); + Provider provider=Context.getProviderService().getProviderByUuid("2bdc3f7d-d911-401a-84e9-5494dda83e8e"); + + + CareTeamParticipant participant = new CareTeamParticipant(); + participant.setRole(testConcept); + participant.setStartTime(visit.getStartDatetime()); + participant.setProvider(provider); + Set participantSet=new HashSet<>(); + participantSet.add(participant); + + CareTeam careTeam = new CareTeam(); + careTeam.setPatient(visit.getPatient()); + careTeam.setStartTime(visit.getStartDatetime()); + careTeam.setVisit(visit); + careTeam.setParticipants(participantSet); + + CareTeam savedCareTeam=careTeamDAO.saveCareTeam(careTeam); + + CareTeam careTeamByVisit= careTeamDAO.getCareTeamByVisit(visit); + + Assertions.assertEquals(savedCareTeam.getCareTeamId(),careTeamByVisit.getCareTeamId()); + Assertions.assertEquals(visit,careTeamByVisit.getVisit()); + Assertions.assertEquals(1,careTeam.getParticipants().size()); + + sessionFactory.getCurrentSession().delete(savedCareTeam); + } + + +} \ No newline at end of file diff --git a/api/src/test/java/org/openmrs/module/ipd/api/service/impl/CareTeamServiceImplTest.java b/api/src/test/java/org/openmrs/module/ipd/api/service/impl/CareTeamServiceImplTest.java new file mode 100644 index 0000000..468423b --- /dev/null +++ b/api/src/test/java/org/openmrs/module/ipd/api/service/impl/CareTeamServiceImplTest.java @@ -0,0 +1,59 @@ +package org.openmrs.module.ipd.api.service.impl; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.openmrs.Concept; +import org.openmrs.ConceptName; +import org.openmrs.Patient; +import org.openmrs.Visit; +import org.openmrs.module.ipd.api.dao.CareTeamDAO; +import org.openmrs.module.ipd.api.dao.ScheduleDAO; +import org.openmrs.module.ipd.api.model.CareTeam; +import org.openmrs.module.ipd.api.model.Reference; +import org.openmrs.module.ipd.api.model.Schedule; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + +@RunWith(MockitoJUnitRunner.class) +public class CareTeamServiceImplTest { + + @InjectMocks + private CareTeamServiceImpl careTeamService; + + @Mock + private CareTeamDAO careTeamDAO; + + @Test + public void shouldInvokeSaveCareTeamWithGivenCareTeam() { + CareTeam careTeam = new CareTeam(); + CareTeam expectedCareTeam = new CareTeam(); + expectedCareTeam.setId(1); + + Mockito.when(careTeamDAO.saveCareTeam(careTeam)).thenReturn(expectedCareTeam); + + careTeamService.saveCareTeam(careTeam); + + Mockito.verify(careTeamDAO, Mockito.times(1)).saveCareTeam(careTeam); + } + + @Test + public void shouldInvokeGetCareTeamWithGivenVisit() { + CareTeam expectedCareTeam = new CareTeam(); + Visit visit = new Visit(); + expectedCareTeam.setId(1); + + Mockito.when(careTeamDAO.getCareTeamByVisit(visit)).thenReturn(expectedCareTeam); + + careTeamService.getCareTeamByVisit(visit); + + Mockito.verify(careTeamDAO, Mockito.times(1)).getCareTeamByVisit(visit); + } + +} diff --git a/api/src/test/resources/CareTeamDAOTestData.xml b/api/src/test/resources/CareTeamDAOTestData.xml new file mode 100644 index 0000000..222818f --- /dev/null +++ b/api/src/test/resources/CareTeamDAOTestData.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/omod/src/main/java/org/openmrs/module/ipd/contract/CareTeamParticipantRequest.java b/omod/src/main/java/org/openmrs/module/ipd/contract/CareTeamParticipantRequest.java new file mode 100644 index 0000000..f3469b8 --- /dev/null +++ b/omod/src/main/java/org/openmrs/module/ipd/contract/CareTeamParticipantRequest.java @@ -0,0 +1,25 @@ +package org.openmrs.module.ipd.contract; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.Date; +import java.util.List; +import java.util.concurrent.TimeUnit; + +@AllArgsConstructor +@NoArgsConstructor +@Getter +@Builder +public class CareTeamParticipantRequest { + + private String uuid; + private String role; + private Long startTime; + private Long endTime; + private String providerUuid; + private Boolean voided; + +} diff --git a/omod/src/main/java/org/openmrs/module/ipd/contract/CareTeamParticipantResponse.java b/omod/src/main/java/org/openmrs/module/ipd/contract/CareTeamParticipantResponse.java new file mode 100644 index 0000000..427c6c5 --- /dev/null +++ b/omod/src/main/java/org/openmrs/module/ipd/contract/CareTeamParticipantResponse.java @@ -0,0 +1,30 @@ +package org.openmrs.module.ipd.contract; + +import lombok.Builder; +import lombok.Getter; +import org.openmrs.module.ipd.api.model.CareTeamParticipant; +import org.openmrs.module.ipd.api.util.DateTimeUtil; +import org.openmrs.module.webservices.rest.web.ConversionUtil; +import org.openmrs.module.webservices.rest.web.representation.Representation; + +@Getter +@Builder +public class CareTeamParticipantResponse { + + private String uuid; + private Object provider; + private Long startTime; + private Long endTime; + private Boolean voided; + + public static CareTeamParticipantResponse createFrom(CareTeamParticipant careTeamParticipant) { + return CareTeamParticipantResponse.builder(). + uuid(careTeamParticipant.getUuid()). + provider(ConversionUtil.convertToRepresentation(careTeamParticipant.getProvider(), Representation.REF)). + startTime(careTeamParticipant.getStartTime().getTime()). + endTime(careTeamParticipant.getEndTime().getTime()). + voided(careTeamParticipant.getVoided()). + build(); + } + +} diff --git a/omod/src/main/java/org/openmrs/module/ipd/contract/CareTeamRequest.java b/omod/src/main/java/org/openmrs/module/ipd/contract/CareTeamRequest.java new file mode 100644 index 0000000..4073924 --- /dev/null +++ b/omod/src/main/java/org/openmrs/module/ipd/contract/CareTeamRequest.java @@ -0,0 +1,15 @@ +package org.openmrs.module.ipd.contract; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Getter +@AllArgsConstructor +@NoArgsConstructor +public class CareTeamRequest { + private String patientUuid; + private List careTeamParticipantsRequest; +} diff --git a/omod/src/main/java/org/openmrs/module/ipd/contract/CareTeamResponse.java b/omod/src/main/java/org/openmrs/module/ipd/contract/CareTeamResponse.java new file mode 100644 index 0000000..ade81b7 --- /dev/null +++ b/omod/src/main/java/org/openmrs/module/ipd/contract/CareTeamResponse.java @@ -0,0 +1,28 @@ +package org.openmrs.module.ipd.contract; + +import lombok.Builder; +import lombok.Getter; +import org.openmrs.module.ipd.api.model.CareTeam; + +import java.util.List; +import java.util.stream.Collectors; + +@Getter +@Builder +public class CareTeamResponse { + + private String uuid; + private String patientUuid; + private List participants; + + public static CareTeamResponse createFrom(CareTeam careTeam){ + return CareTeamResponse.builder(). + uuid(careTeam.getUuid()). + patientUuid(careTeam.getPatient().getUuid()). + participants(careTeam.getParticipants().stream(). + filter(careTeamParticipant -> !careTeamParticipant.getVoided()). + map(CareTeamParticipantResponse::createFrom).collect(Collectors.toList())). + build(); + } + +} diff --git a/omod/src/main/java/org/openmrs/module/ipd/controller/IPDCareTeamController.java b/omod/src/main/java/org/openmrs/module/ipd/controller/IPDCareTeamController.java new file mode 100644 index 0000000..e713dcd --- /dev/null +++ b/omod/src/main/java/org/openmrs/module/ipd/controller/IPDCareTeamController.java @@ -0,0 +1,47 @@ +package org.openmrs.module.ipd.controller; + +import lombok.extern.slf4j.Slf4j; +import org.openmrs.module.ipd.api.model.CareTeam; +import org.openmrs.module.ipd.contract.CareTeamRequest; +import org.openmrs.module.ipd.contract.CareTeamResponse; +import org.openmrs.module.ipd.contract.ScheduleMedicationResponse; +import org.openmrs.module.ipd.service.IPDCareTeamService; +import org.openmrs.module.webservices.rest.web.RestConstants; +import org.openmrs.module.webservices.rest.web.RestUtil; +import org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import static org.springframework.http.HttpStatus.BAD_REQUEST; +import static org.springframework.http.HttpStatus.OK; + + +@Controller +@RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/ipd/careteam") +@Slf4j +public class IPDCareTeamController extends BaseRestController { + + private final IPDCareTeamService ipdCareTeamService; + + @Autowired + public IPDCareTeamController(IPDCareTeamService ipdCareTeamService) { + this.ipdCareTeamService = ipdCareTeamService; + } + + @RequestMapping(value = "/participants", method = RequestMethod.POST) + @ResponseBody + public ResponseEntity createMedicationSchedule(@RequestBody CareTeamRequest careTeamRequest) { + try { + CareTeam careTeam = ipdCareTeamService.saveCareTeamParticipants(careTeamRequest); + return new ResponseEntity<>(CareTeamResponse.createFrom(careTeam), OK); + } catch (Exception e) { + log.error("Runtime error while trying to create new schedule", e); + return new ResponseEntity<>(RestUtil.wrapErrorResponse(e, e.getMessage()), BAD_REQUEST); + } + } + +} diff --git a/omod/src/main/java/org/openmrs/module/ipd/factory/CareTeamFactory.java b/omod/src/main/java/org/openmrs/module/ipd/factory/CareTeamFactory.java new file mode 100644 index 0000000..2c41b49 --- /dev/null +++ b/omod/src/main/java/org/openmrs/module/ipd/factory/CareTeamFactory.java @@ -0,0 +1,71 @@ +package org.openmrs.module.ipd.factory; + +import org.openmrs.Patient; +import org.openmrs.Visit; +import org.openmrs.api.APIException; +import org.openmrs.api.context.Context; +import org.openmrs.module.ipd.api.model.CareTeam; +import org.openmrs.module.ipd.api.model.CareTeamParticipant; +import org.openmrs.module.ipd.api.util.DateTimeUtil; +import org.openmrs.module.ipd.contract.CareTeamParticipantRequest; +import org.openmrs.module.ipd.contract.CareTeamRequest; +import org.springframework.stereotype.Component; + +import java.util.*; + +@Component +public class CareTeamFactory { + + + public CareTeam createCareTeamFromRequest(CareTeamRequest request, Patient patient, Visit visit) { + CareTeam careTeam = new CareTeam(); + careTeam.setVisit(visit); + careTeam.setPatient(patient); + careTeam.setStartTime(visit.getStartDatetime()); + + if (request.getCareTeamParticipantsRequest() !=null){ + List careTeamParticipants = new ArrayList<>(); + for (CareTeamParticipantRequest participantRequest: request.getCareTeamParticipantsRequest()) { + CareTeamParticipant participant = new CareTeamParticipant(); + participant.setStartTime(DateTimeUtil.convertEpochTimeToDate(participantRequest.getStartTime())); + participant.setEndTime(DateTimeUtil.convertEpochTimeToDate(participantRequest.getEndTime())); + participant.setProvider(Context.getProviderService().getProviderByUuid(participantRequest.getProviderUuid())); + participant.setRole(Context.getConceptService().getConceptByName(participantRequest.getRole())); + careTeamParticipants.add(participant); + } + careTeam.setParticipants(new HashSet<>(careTeamParticipants)); + } + return careTeam; + } + + public CareTeam updateCareTeamFromRequest(CareTeamRequest careTeamRequest, CareTeam careTeam) { + if (careTeamRequest.getCareTeamParticipantsRequest() !=null){ + List careTeamParticipants = new ArrayList<>(); + for (CareTeamParticipantRequest participantRequest: careTeamRequest.getCareTeamParticipantsRequest()) { + if (participantRequest.getUuid()!=null){ + CareTeamParticipant careTeamParticipant = careTeam.getParticipants().stream() + .filter(participant -> participant.getUuid().equals(participantRequest.getUuid())) + .findFirst().get(); + careTeamParticipant.setStartTime(DateTimeUtil.convertEpochTimeToDate(participantRequest.getStartTime())); + careTeamParticipant.setEndTime(DateTimeUtil.convertEpochTimeToDate(participantRequest.getEndTime())); + careTeamParticipant.setProvider(Context.getProviderService().getProviderByUuid(participantRequest.getProviderUuid())); + careTeamParticipant.setRole(Context.getConceptService().getConceptByName(participantRequest.getRole())); + if (participantRequest.getVoided() != null) careTeamParticipant.setVoided(participantRequest.getVoided()); + } + else { + Boolean participantAlreadyExists = careTeam.getParticipants().stream(). + anyMatch(participant -> participant.getVoided()!=true + && DateTimeUtil.convertEpochTimeToDate(participantRequest.getStartTime()).equals(participant.getStartTime())); + if (participantAlreadyExists) throw new APIException("Participant Already exists for given time frame"); + CareTeamParticipant participant = new CareTeamParticipant(); + participant.setStartTime(DateTimeUtil.convertEpochTimeToDate(participantRequest.getStartTime())); + participant.setEndTime(DateTimeUtil.convertEpochTimeToDate(participantRequest.getEndTime())); + participant.setProvider(Context.getProviderService().getProviderByUuid(participantRequest.getProviderUuid())); + participant.setRole(Context.getConceptService().getConceptByName(participantRequest.getRole())); + careTeam.getParticipants().add(participant); + } + } + } + return careTeam; + } +} diff --git a/omod/src/main/java/org/openmrs/module/ipd/service/IPDCareTeamService.java b/omod/src/main/java/org/openmrs/module/ipd/service/IPDCareTeamService.java new file mode 100644 index 0000000..42243a8 --- /dev/null +++ b/omod/src/main/java/org/openmrs/module/ipd/service/IPDCareTeamService.java @@ -0,0 +1,12 @@ +package org.openmrs.module.ipd.service; + +import org.openmrs.module.ipd.api.model.CareTeam; +import org.openmrs.module.ipd.contract.CareTeamParticipantRequest; +import org.openmrs.module.ipd.contract.CareTeamRequest; + +import java.util.List; + +public interface IPDCareTeamService { + + CareTeam saveCareTeamParticipants(CareTeamRequest careTeamRequest); +} diff --git a/omod/src/main/java/org/openmrs/module/ipd/service/impl/IPDCareTeamServiceImpl.java b/omod/src/main/java/org/openmrs/module/ipd/service/impl/IPDCareTeamServiceImpl.java new file mode 100644 index 0000000..48e53db --- /dev/null +++ b/omod/src/main/java/org/openmrs/module/ipd/service/impl/IPDCareTeamServiceImpl.java @@ -0,0 +1,49 @@ +package org.openmrs.module.ipd.service.impl; + +import org.openmrs.Patient; +import org.openmrs.Visit; +import org.openmrs.api.ConceptService; +import org.openmrs.api.PatientService; +import org.openmrs.api.VisitService; +import org.openmrs.module.ipd.api.model.CareTeam; +import org.openmrs.module.ipd.api.service.CareTeamService; +import org.openmrs.module.ipd.contract.CareTeamRequest; +import org.openmrs.module.ipd.factory.CareTeamFactory; +import org.openmrs.module.ipd.service.IPDCareTeamService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional +public class IPDCareTeamServiceImpl implements IPDCareTeamService { + + private final CareTeamService careTeamService; + private final CareTeamFactory careTeamFactory; + private final ConceptService conceptService; + private final VisitService visitService; + private final PatientService patientService; + + @Autowired + public IPDCareTeamServiceImpl(CareTeamService careTeamService, CareTeamFactory careTeamFactory, ConceptService conceptService, VisitService visitService, PatientService patientService) { + this.careTeamService = careTeamService; + this.careTeamFactory = careTeamFactory; + this.conceptService = conceptService; + this.visitService = visitService; + this.patientService = patientService; + } + + @Override + public CareTeam saveCareTeamParticipants(CareTeamRequest careTeamRequest) { + Patient patient = patientService.getPatientByUuid(careTeamRequest.getPatientUuid()); + Visit visit = visitService.getActiveVisitsByPatient(patient).get(0); + CareTeam careTeam = careTeamService.getCareTeamByVisit(visit); + if (careTeam==null || careTeam.getCareTeamId()==null){ + careTeam = careTeamFactory.createCareTeamFromRequest(careTeamRequest,patient,visit); + } + else { + careTeam = careTeamFactory.updateCareTeamFromRequest(careTeamRequest, careTeam); + } + return careTeamService.saveCareTeam(careTeam); + } +}