Skip to content

Commit

Permalink
Initial work to support CRUD through REST
Browse files Browse the repository at this point in the history
  • Loading branch information
ojwanganto committed Jul 3, 2024
1 parent 4108bb9 commit 193941a
Show file tree
Hide file tree
Showing 11 changed files with 467 additions and 11 deletions.
4 changes: 4 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
<description>API project for Data Filter Module</description>

<dependencies>
<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest-omod-common</artifactId>
</dependency>
<dependency>
<groupId>org.openmrs.api</groupId>
<artifactId>openmrs-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.datafilter.impl.api;

import java.util.Date;
import java.util.Map;

public class DataFilterDefaultResponse {

private String uuid;

private Date dateCreated;

private Map entity;

private Map basis;

private Map creator;

public String getUuid() {
return uuid;
}

public void setUuid(String uuid) {
this.uuid = uuid;
}

public Date getDateCreated() {
return dateCreated;
}

public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}

public Map getEntity() {
return entity;
}

public void setEntity(Map entity) {
this.entity = entity;
}

public Map getBasis() {
return basis;
}

public void setBasis(Map basis) {
this.basis = basis;
}

public Map getCreator() {
return creator;
}

public void setCreator(Map creator) {
this.creator = creator;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.datafilter.impl.api;

import org.openmrs.Location;
import org.openmrs.Patient;
import org.openmrs.Person;
import org.openmrs.User;
import org.openmrs.api.LocationService;
import org.openmrs.api.PatientService;
import org.openmrs.api.UserService;
import org.openmrs.module.datafilter.impl.EntityBasisMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class EntityBasisMapResponseMapper {

@Autowired
LocationService locationService;

@Autowired
UserService userService;

@Autowired
PatientService patientService;

/**
* Handles only User, Patient, and Location objects TODO: Make this implementation generic to handle
* other OpenMRS objects
*
* @param a
* @param response
* @return
*/
private DataFilterDefaultResponse mapToDefaultResponse(EntityBasisMap a, DataFilterDefaultResponse response) {
response.setUuid(a.getUuid());
response.setDateCreated(a.getDateCreated());
response.setCreator(createUserMap(a.getCreator()));
if (a.getEntityType().equals("org.openmrs.User")) {
response.setEntity(createUserMap(userService.getUser(Integer.valueOf(a.getEntityIdentifier()))));
} else if (a.getEntityType().equals("org.openmrs.Patient")) {
response.setEntity(createPatientMap(patientService.getPatient(Integer.valueOf(a.getEntityIdentifier()))));
}

if (a.getBasisType().equals("org.openmrs.Location")) {
response.setBasis(createLocationMap(locationService.getLocation(Integer.valueOf(a.getBasisIdentifier()))));
}
return response;
}

public List<DataFilterDefaultResponse> constructResponse(List<EntityBasisMap> entityBasisMaps) {
return entityBasisMaps.stream().map(as -> this.mapToDefaultResponse(as, new DataFilterDefaultResponse()))
.collect(Collectors.toList());
}

private Map createLocationMap(Location l) {
Map locationMap = null;
if (l != null) {
locationMap = new HashMap();
locationMap.put("name", l.getName());
locationMap.put("uuid", l.getUuid());
}
return locationMap;
}

private Map createPatientMap(Patient p) {
Map map = new HashMap();
map.put("name", p.getPersonName().getFullName());
map.put("uuid", p.getUuid());
map.put("identifier", p.getPatientIdentifier().getIdentifier());
map.put("age", p.getAge());
map.put("gender", p.getGender());
map.putAll(p.getActiveIdentifiers().stream().filter(e -> e.getIdentifierType() != null)
.collect(Collectors.toMap(e -> e.getIdentifierType().toString().replaceAll("[- ]", ""),
e -> e.getIdentifier(), (e1, e2) -> e1 + "," + e2)));
return map;
}

private Map createUserMap(User u) {
Person p = u.getPerson();
Map map = new HashMap();
map.put("name", p.getPersonName().getFullName());
map.put("uuid", p.getUuid());
return map;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.datafilter.impl.api;

/**
* A model for search
*/
public class EntityBasisMapSearchRequest {

private String entityIdentifier;

private String entityType;

private String basisIdentifier;

private String basisType;

public String getEntityIdentifier() {
return entityIdentifier;
}

public void setEntityIdentifier(String entityIdentifier) {
this.entityIdentifier = entityIdentifier;
}

public String getEntityType() {
return entityType;
}

public void setEntityType(String entityType) {
this.entityType = entityType;
}

public String getBasisIdentifier() {
return basisIdentifier;
}

public void setBasisIdentifier(String basisIdentifier) {
this.basisIdentifier = basisIdentifier;
}

public String getBasisType() {
return basisType;
}

public void setBasisType(String basisType) {
this.basisType = basisType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void setSessionFactory(SessionFactory sessionFactory) {
*/
@Override
public EntityBasisMap getEntityBasisMap(String entityIdentifier, String entityType, String basisIdentifier,
String basisType) {
String basisType) {

Criteria criteria = sessionFactory.getCurrentSession().createCriteria(EntityBasisMap.class);
criteria.add(Restrictions.eq("entityIdentifier", entityIdentifier).ignoreCase());
Expand Down Expand Up @@ -83,22 +83,16 @@ public Collection<EntityBasisMap> getEntityBasisMaps(String entityIdentifier, St
}

@Override
public List<EntityBasisMap> getEntityBasisMapsByBasis(String entityType, String basisType,
String basisIdentifier) {
public List<EntityBasisMap> getEntityBasisMapsByBasis(String entityType, String basisType, String basisIdentifier) {

Session session = sessionFactory.getCurrentSession();

CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<EntityBasisMap> cq = cb.createQuery(EntityBasisMap.class);

Root<EntityBasisMap> root = cq.from(EntityBasisMap.class);
cq.select(root).where(
cb.and(
cb.equal(root.get("entityType"), entityType),
cb.equal(root.get("basisType"), basisType),
cb.equal(root.get("basisIdentifier"), basisIdentifier)
)
);
cq.select(root).where(cb.and(cb.equal(root.get("entityType"), entityType),
cb.equal(root.get("basisType"), basisType), cb.equal(root.get("basisIdentifier"), basisIdentifier)));

Query<EntityBasisMap> query = session.createQuery(cq);
return query.getResultList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public Collection<EntityBasisMap> getEntityBasisMaps(OpenmrsObject entity, Strin

@Override
public Collection<EntityBasisMap> getEntityBasisMapsByBasis(Class<? extends OpenmrsObject> entityClass,
OpenmrsObject basis) {
OpenmrsObject basis) {
return getEntityBasisMapsByBasis(entityClass.getName(), basis);
}

Expand Down
5 changes: 5 additions & 0 deletions omod/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
<description>OMOD project for Data Filter Module</description>

<dependencies>
<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest-omod-common</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>${project.parent.artifactId}-api</artifactId>
Expand Down
Loading

0 comments on commit 193941a

Please sign in to comment.