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

FHIR Create R4 Questionnaire API #541

Closed
wants to merge 38 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
bf4a2cb
Added Support for Questionnaire
icrc-loliveira Jun 3, 2024
aa6ef78
Added Support for Questionnaire
icrc-loliveira Jun 4, 2024
501a3bd
Added Support for Questionnaire
icrc-loliveira Jun 4, 2024
256b63d
Added Support for Questionnaire
icrc-loliveira Jun 6, 2024
6d0f137
Added Support for Questionnaire
icrc-loliveira Jun 6, 2024
4b13b4d
Added Support for Questionnaire
icrc-loliveira Jun 6, 2024
1396995
Added Support for Questionnaire
icrc-loliveira Jun 6, 2024
edd43b7
Added Support for Questionnaire
icrc-loliveira Jun 6, 2024
44d4d8c
Added Support for Questionnaire
icrc-loliveira Jun 6, 2024
5601d4a
Added Support for Questionnaire
icrc-loliveira Jun 6, 2024
3b983de
Added Support for Questionnaire
icrc-loliveira Jun 21, 2024
f53ac5a
Added Support for Questionnaire
icrc-loliveira Jun 21, 2024
eff1b46
Added Support for Questionnaire
icrc-loliveira Jun 21, 2024
65ab767
Added Support for Questionnaire
icrc-loliveira Jun 21, 2024
61142c8
Added Support for Questionnaire
icrc-loliveira Jun 24, 2024
e7e8b1f
Added Support for Questionnaire
icrc-loliveira Jun 24, 2024
ae2e774
Added Support for Questionnaire
icrc-loliveira Jun 24, 2024
86f332f
Merge branch 'master' of https://github.com/openmrs/openmrs-module-fh…
icrc-loliveira Jul 8, 2024
efb91c8
Added Support for Questionnaire
icrc-loliveira Jul 9, 2024
538eb33
changed code to use Form instead of form resource.
icrc-loliveira Jul 9, 2024
602c780
Added support to questionnaires search
icrc-loliveira Jul 10, 2024
a4a7f9a
Added support to questionnaires search
icrc-loliveira Jul 10, 2024
0fb444e
Added support to questionnaires search
icrc-loliveira Jul 11, 2024
6868571
Added support to questionnaires search
icrc-loliveira Jul 12, 2024
5f15c02
Added tests for FhirQuestionnaireServiceImpl
icrc-loliveira Jul 15, 2024
5d69935
Added tests for FhirQuestionnaireDaoImpl
icrc-loliveira Jul 15, 2024
2da6d0a
Added tests for FhirQuestionnaireDaoImpl
icrc-loliveira Jul 15, 2024
340b1cc
mvn clean package
icrc-loliveira Jul 16, 2024
227926e
Merge branch 'master' of https://github.com/openmrs/openmrs-module-fh…
icrc-loliveira Jul 16, 2024
6938c3a
mvn clean package
icrc-loliveira Jul 16, 2024
3256945
mvn clean package
icrc-loliveira Jul 16, 2024
986ccf2
mvn clean package
icrc-loliveira Jul 17, 2024
8b52360
Fixed FhirQuestionnaireDaoImplTest testes
icrc-loliveira Jul 17, 2024
17576a8
Fixed QuestionnaireTranslatorImplTest testes
icrc-loliveira Jul 17, 2024
13436cc
mvn clean package
icrc-loliveira Jul 17, 2024
ed5c0ac
Added tests for QuestionnaireFhirResourceProvider
icrc-loliveira Jul 17, 2024
0d1c9d4
Added tests for FhirQuestionnaireServiceImpl
icrc-loliveira Jul 17, 2024
617d5c6
removed unnecessary mock from FhirQuestionnaireServiceImplTest
icrc-loliveira Jul 17, 2024
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
5 changes: 5 additions & 0 deletions api/src/main/java/org/openmrs/module/fhir2/FhirConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,9 @@ private FhirConstants() {
public static final String EXACT_TOTAL_SEARCH_PARAMETER = "_exactTotal";

public static final String COUNT_QUERY_CACHE = "countQueryCache";

public static final String QUESTIONNAIRE = "Questionnaire";

public static final String FHIR_QUESTIONNAIRE_TYPE = "FHIR Questionnaire";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.fhir2.api;

import javax.annotation.Nonnull;

import java.util.Collection;
import java.util.List;

import ca.uhn.fhir.rest.api.server.IBundleProvider;
import ca.uhn.fhir.rest.param.TokenParam;
import org.hl7.fhir.r4.model.Questionnaire;
import org.openmrs.module.fhir2.api.search.param.QuestionnaireSearchParams;

public interface FhirQuestionnaireService extends FhirService<Questionnaire> {

List<Questionnaire> getQuestionnairesByIds(@Nonnull Collection<Integer> ids);

Questionnaire getById(@Nonnull Integer id);

IBundleProvider searchForQuestionnaires(QuestionnaireSearchParams questionnaireSearchParams);

IBundleProvider getQuestionnaireEverything(TokenParam identifier);

IBundleProvider getQuestionnaireEverything();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.fhir2.api.dao;

import javax.annotation.Nonnull;

import java.util.Collection;
import java.util.List;

import org.openmrs.Form;
import org.openmrs.annotation.Authorized;
import org.openmrs.module.fhir2.api.search.param.SearchParameterMap;
import org.openmrs.util.PrivilegeConstants;

public interface FhirQuestionnaireDao extends FhirDao<Form> {

@Authorized(PrivilegeConstants.GET_FORMS)
Form getQuestionnaireById(@Nonnull Integer id);

@Authorized(PrivilegeConstants.GET_FORMS)
List<Form> getQuestionnairesByIds(@Nonnull Collection<Integer> ids);

@Override
@Authorized(PrivilegeConstants.GET_FORMS)
Form get(@Nonnull String uuid);

@Override
@Authorized(PrivilegeConstants.GET_FORMS)
List<Form> getSearchResults(@Nonnull SearchParameterMap theParams);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* 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.fhir2.api.dao.impl;

import static org.hibernate.criterion.Restrictions.*;

import javax.annotation.Nonnull;
import javax.persistence.criteria.*;
import javax.persistence.criteria.CriteriaBuilder;

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

import ca.uhn.fhir.rest.param.StringAndListParam;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.openmrs.Form;
import org.openmrs.FormResource;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.dao.FhirQuestionnaireDao;
import org.openmrs.module.fhir2.api.search.param.SearchParameterMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
@Setter(AccessLevel.PACKAGE)
public class FhirQuestionnaireDaoImpl extends BaseFhirDao<Form> implements FhirQuestionnaireDao {

@Autowired
@Getter(AccessLevel.PUBLIC)
@Setter(AccessLevel.PUBLIC)
@Qualifier("sessionFactory")
private SessionFactory sessionFactory;

@Override
public Form getQuestionnaireById(@Nonnull Integer id) {
return (Form) getSessionFactory().getCurrentSession().createCriteria(Form.class).add(eq("formId", id))
.uniqueResult();
}

@Override
@SuppressWarnings("unchecked")
public List<Form> getQuestionnairesByIds(@Nonnull Collection<Integer> ids) {
return getSessionFactory().getCurrentSession().createCriteria(Form.class).add(in("id", ids)).list();
}

@SuppressWarnings("unchecked")
@Override
public List<Form> getSearchResults(@Nonnull SearchParameterMap theParams) {
Session session = sessionFactory.getCurrentSession();

// Create CriteriaBuilder
CriteriaBuilder builder = session.getCriteriaBuilder();

// Create CriteriaQuery for main query
CriteriaQuery<Form> query = builder.createQuery(Form.class);
Root<Form> formRoot = query.from(Form.class);
formRoot.alias("f");

// Create Subquery for FormResource
Subquery<Long> subquery = query.subquery(Long.class);
Root<FormResource> resourceRoot = subquery.from(FormResource.class);
resourceRoot.alias("fr");
subquery.select(resourceRoot.get("formResourceId")); // Selecting formId to match in main query

// Add predicates to the subquery
List<Predicate> subqueryPredicates = new ArrayList<>();
subqueryPredicates.add(builder.equal(resourceRoot.get("form").get("formId"), formRoot.get("formId")));
subqueryPredicates.add(builder.equal(resourceRoot.get("name"), FhirConstants.FHIR_QUESTIONNAIRE_TYPE));
subquery.where(builder.and(subqueryPredicates.toArray(new Predicate[0])));

// Main query predicates
List<Predicate> mainPredicates = new ArrayList<>();
setupSearchParams(mainPredicates, builder, formRoot, theParams);
mainPredicates.add(builder.equal(formRoot.get("retired"), false));
mainPredicates.add(builder.exists(subquery));

// Add predicates to the query
query.select(formRoot).where(builder.and(mainPredicates.toArray(new Predicate[0])));

List<Form> results = session.createQuery(query).getResultList();
return results.stream().map(this::deproxyResult).collect(Collectors.toList());
}

protected void setupSearchParams(List<Predicate> predicates, CriteriaBuilder builder, Root<Form> root,
SearchParameterMap theParams) {
theParams.getParameters().forEach(entry -> {
switch (entry.getKey()) {
case FhirConstants.NAME_SEARCH_HANDLER:
entry.getValue().forEach(param -> {
((StringAndListParam) param.getParam()).getValuesAsQueryTokens().stream()
.forEach(l -> l.getValuesAsQueryTokens().stream().forEach(v -> {
predicates.add(builder.equal(root.get("name"), v.getValue()));
}));
});
break;
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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.fhir2.api.impl;

import javax.annotation.Nonnull;

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

import ca.uhn.fhir.rest.api.server.IBundleProvider;
import ca.uhn.fhir.rest.param.TokenAndListParam;
import ca.uhn.fhir.rest.param.TokenParam;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import org.hl7.fhir.r4.model.Questionnaire;
import org.openmrs.Form;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.FhirQuestionnaireService;
import org.openmrs.module.fhir2.api.dao.FhirQuestionnaireDao;
import org.openmrs.module.fhir2.api.search.SearchQuery;
import org.openmrs.module.fhir2.api.search.SearchQueryInclude;
import org.openmrs.module.fhir2.api.search.param.QuestionnaireSearchParams;
import org.openmrs.module.fhir2.api.search.param.SearchParameterMap;
import org.openmrs.module.fhir2.api.translators.QuestionnaireTranslator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Transactional
@Getter(AccessLevel.PROTECTED)
@Setter(AccessLevel.PACKAGE)
public class FhirQuestionnaireServiceImpl extends BaseFhirService<Questionnaire, Form> implements FhirQuestionnaireService {

@Autowired
private FhirQuestionnaireDao dao;

@Autowired
private QuestionnaireTranslator translator;

@Autowired
private SearchQueryInclude<Questionnaire> searchQueryInclude;

Check warning on line 51 in api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirQuestionnaireServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirQuestionnaireServiceImpl.java#L51

Added line #L51 was not covered by tests

@Autowired
private SearchQuery<Form, Questionnaire, FhirQuestionnaireDao, QuestionnaireTranslator, SearchQueryInclude<Questionnaire>> searchQuery;

Check warning on line 54 in api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirQuestionnaireServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirQuestionnaireServiceImpl.java#L54

Added line #L54 was not covered by tests

@Override
public List<Questionnaire> getQuestionnairesByIds(@Nonnull Collection<Integer> ids) {
List<org.openmrs.Form> questionnaires = dao.getQuestionnairesByIds(ids);
return questionnaires.stream().map(translator::toFhirResource).collect(Collectors.toList());
}

@Override
public Questionnaire getById(@Nonnull Integer id) {
return translator.toFhirResource(dao.getQuestionnaireById(id));
}

@Override
@Transactional(readOnly = true)
public IBundleProvider searchForQuestionnaires(QuestionnaireSearchParams questionnaireSearchParams) {
return searchQuery.getQueryResults(questionnaireSearchParams.toSearchParameterMap(), dao, translator,
searchQueryInclude);
}

@Override
@Transactional(readOnly = true)
public IBundleProvider getQuestionnaireEverything(TokenParam questionnairId) {
SearchParameterMap theParams = new SearchParameterMap().addParameter(FhirConstants.EVERYTHING_SEARCH_HANDLER, "")
.addParameter(FhirConstants.COMMON_SEARCH_HANDLER, FhirConstants.ID_PROPERTY,
new TokenAndListParam().addAnd(questionnairId));

populateEverythingOperationParams(theParams);
return searchQuery.getQueryResults(theParams, dao, translator, searchQueryInclude);
}

@Override
public IBundleProvider getQuestionnaireEverything() {
SearchParameterMap theParams = new SearchParameterMap().addParameter(FhirConstants.EVERYTHING_SEARCH_HANDLER, "");

populateEverythingOperationParams(theParams);
return searchQuery.getQueryResults(theParams, dao, translator, searchQueryInclude);
}

private void populateEverythingOperationParams(SearchParameterMap theParams) {
// Do nothing
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.fhir2.api.search.param;

import ca.uhn.fhir.rest.api.SortSpec;
import ca.uhn.fhir.rest.param.DateRangeParam;
import ca.uhn.fhir.rest.param.StringAndListParam;
import ca.uhn.fhir.rest.param.TokenAndListParam;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.openmrs.module.fhir2.FhirConstants;

@Data
@NoArgsConstructor

Check warning on line 23 in api/src/main/java/org/openmrs/module/fhir2/api/search/param/QuestionnaireSearchParams.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/openmrs/module/fhir2/api/search/param/QuestionnaireSearchParams.java#L22-L23

Added lines #L22 - L23 were not covered by tests
@EqualsAndHashCode(callSuper = true)
public class QuestionnaireSearchParams extends BaseResourceSearchParams {

private StringAndListParam name;

@Builder

Check warning on line 29 in api/src/main/java/org/openmrs/module/fhir2/api/search/param/QuestionnaireSearchParams.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/openmrs/module/fhir2/api/search/param/QuestionnaireSearchParams.java#L29

Added line #L29 was not covered by tests
public QuestionnaireSearchParams(StringAndListParam name, TokenAndListParam id, DateRangeParam lastUpdated,
SortSpec sort) {

super(id, lastUpdated, sort, null, null);

this.name = name;
}

@Override
public SearchParameterMap toSearchParameterMap() {
return baseSearchParameterMap().addParameter(FhirConstants.NAME_SEARCH_HANDLER, FhirConstants.NAME_PROPERTY,
getName());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.fhir2.api.translators;

import javax.annotation.Nonnull;

import org.hl7.fhir.r4.model.Questionnaire;
import org.openmrs.Form;

public interface QuestionnaireTranslator extends OpenmrsFhirTranslator<Form, Questionnaire> {

/**
* Maps an {@link org.openmrs.Form} to a {@link Questionnaire}
*
* @param form the form to translate
* @return the corresponding FHIR questionnaire resource
*/
@Override
Questionnaire toFhirResource(@Nonnull Form form);

}
Loading