Skip to content

Commit

Permalink
changed code to use Form instead of form resource.
Browse files Browse the repository at this point in the history
  • Loading branch information
icrc-loliveira committed Jul 9, 2024
1 parent efb91c8 commit 538eb33
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
*/
package org.openmrs.module.fhir2.api;

import ca.uhn.fhir.rest.api.server.IBundleProvider;
import org.hl7.fhir.r4.model.Questionnaire;

public interface FhirQuestionnaireService extends FhirService<Questionnaire> {
//IBundleProvider searchForQuestionnaire(QuestionnaireSearchParams questionnaireSearchParams);

IBundleProvider getQuestionnaireEverything();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@

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.module.fhir2.api.util.FormResourceAuditable;
import org.openmrs.util.PrivilegeConstants;

public interface FhirQuestionnaireDao extends FhirDao<FormResourceAuditable> {
public interface FhirQuestionnaireDao extends FhirDao<Form> {

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

@Override
@Authorized(PrivilegeConstants.GET_FORMS)
List<FormResourceAuditable> getSearchResults(@Nonnull SearchParameterMap theParams);
List<Form> getSearchResults(@Nonnull SearchParameterMap theParams);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,27 @@
package org.openmrs.module.fhir2.api.dao.impl;

import static org.hibernate.criterion.Restrictions.eq;
import static org.openmrs.module.fhir2.FhirConstants.TITLE_SEARCH_HANDLER;

import javax.annotation.Nonnull;

import ca.uhn.fhir.rest.param.StringAndListParam;
import lombok.AccessLevel;
import lombok.Setter;
import org.hibernate.Criteria;
import org.openmrs.Form;
import org.openmrs.api.FormService;
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.openmrs.module.fhir2.api.util.FormResourceAuditable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Autowired
private FormService formService;

@Override
public FormResourceAuditable get(@Nonnull String uuid) {
Form form = formService.getFormByUuid(uuid);
return new FormResourceAuditable(formService.getFormResource(form, FhirConstants.FHIR_QUESTIONNAIRE_TYPE));
}

@Override
protected void setupSearchParams(Criteria criteria, SearchParameterMap theParams) {
criteria.add(eq("set", true));
theParams.getParameters().forEach(entry -> {
switch (entry.getKey()) {
case TITLE_SEARCH_HANDLER:
entry.getValue().forEach(param -> handleTitle(criteria, (StringAndListParam) param.getParam()));
break;
}
});
}

protected void handleTitle(Criteria criteria, StringAndListParam titlePattern) {
criteria.createAlias("names", "cn");
handleAndListParam(titlePattern, (title) -> propertyLike("cn.name", title)).ifPresent(criteria::add);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,31 @@
*/
package org.openmrs.module.fhir2.api.impl;

import ca.uhn.fhir.model.api.Include;
import ca.uhn.fhir.rest.api.server.IBundleProvider;
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.SearchParameterMap;
import org.openmrs.module.fhir2.api.translators.QuestionnaireTranslator;
import org.openmrs.module.fhir2.api.util.FormResourceAuditable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;

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

@Autowired
private FhirQuestionnaireDao dao;
Expand All @@ -39,18 +45,23 @@ public class FhirQuestionnaireServiceImpl extends BaseFhirService<Questionnaire,
private SearchQueryInclude<Questionnaire> searchQueryInclude;

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

/*
private SearchQuery<Form, Questionnaire, FhirQuestionnaireDao, QuestionnaireTranslator, SearchQueryInclude<Questionnaire>> searchQuery;

@Override
public IBundleProvider searchForQuestionnaire(QuestionnaireSearchParams questionnaireSearchParams) {
return searchQuery.getQueryResults(questionnaireSearchParams.toSearchParameterMap(), dao, translator,
searchQueryInclude);
protected boolean isVoided(Form form) {
return form.getRetired();
}
*/


@Override
protected boolean isVoided(FormResourceAuditable formResource) {
return formResource.getForm().getRetired();
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) {
HashSet<Include> revIncludes = new HashSet<>();
theParams.addParameter(FhirConstants.REVERSE_INCLUDE_SEARCH_HANDLER, revIncludes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,36 @@
import javax.annotation.Nonnull;

import org.hl7.fhir.r4.model.Questionnaire;
import org.openmrs.module.fhir2.api.util.FormResourceAuditable;
import org.openmrs.Form;

public interface QuestionnaireTranslator extends OpenmrsFhirUpdatableTranslator<FormResourceAuditable, Questionnaire> {
public interface QuestionnaireTranslator extends OpenmrsFhirUpdatableTranslator<Form, Questionnaire> {

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

/**
* Maps a {@link Questionnaire} to an {@link org.openmrs.FormResource}
* Maps a {@link Questionnaire} to an {@link org.openmrs.Form}
*
* @param questionnaire the FHIR questionnaire to translate
* @return the corresponding OpenMRS FormResource
* @return the corresponding OpenMRS Form
*/
@Override
FormResourceAuditable toOpenmrsType(@Nonnull Questionnaire questionnaire);
Form toOpenmrsType(@Nonnull Questionnaire questionnaire);

/**
* Maps a {@link Questionnaire} to an existing {@link org.openmrs.FormResource}
* Maps a {@link Questionnaire} to an existing {@link org.openmrs.Form}
*
* @param currentFormResource the existing OpenMRS formResource to update
* @param currentForm the existing OpenMRS form to update
* @param questionnaire the FHIR questionnaire to translate
* @return the updated OpenMRS formResource
*/
@Override
FormResourceAuditable toOpenmrsType(@Nonnull FormResourceAuditable currentFormResource,
Form toOpenmrsType(@Nonnull Form currentForm,
@Nonnull Questionnaire questionnaire);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,42 @@
import lombok.AccessLevel;
import lombok.Setter;
import org.hl7.fhir.r4.model.*;
import org.openmrs.Form;
import org.openmrs.FormResource;
import org.openmrs.api.FormService;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.translators.*;
import org.openmrs.module.fhir2.api.util.FormResourceAuditable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Setter(AccessLevel.PACKAGE)
public class QuestionnaireTranslatorImpl implements QuestionnaireTranslator {


@Autowired
FormService formService;

@Override
public Questionnaire toFhirResource(@Nonnull FormResourceAuditable openmrsFormResource) {
notNull(openmrsFormResource, "The Openmrs FormResource object should not be null");
public Questionnaire toFhirResource(@Nonnull Form openmrsForm) {
notNull(openmrsForm, "The Openmrs Form object should not be null");

FormResource resource = formService.getFormResource(openmrsForm, FhirConstants.FHIR_QUESTIONNAIRE_TYPE);
notNull(resource, "The Openmrs Form doesn't contain an FHIR Questionnaire");

FhirContext ctx = FhirContext.forR4();
IParser p = ctx.newJsonParser();
return p.parseResource(Questionnaire.class, openmrsFormResource.getValue().toString());
return p.parseResource(Questionnaire.class, resource.getValue().toString());
}

@Override
public FormResourceAuditable toOpenmrsType(@Nonnull org.hl7.fhir.r4.model.Questionnaire questionnaire) {
public Form toOpenmrsType(@Nonnull org.hl7.fhir.r4.model.Questionnaire questionnaire) {
notNull(questionnaire, "The Questionnaire object should not be null");
return toOpenmrsType(new FormResourceAuditable(), questionnaire);
return toOpenmrsType(new Form(), questionnaire);
}

@Override
public FormResourceAuditable toOpenmrsType(@Nonnull FormResourceAuditable openmrsForm,
public Form toOpenmrsType(@Nonnull Form openmrsForm,
@Nonnull org.hl7.fhir.r4.model.Questionnaire questionnaire) {
notNull(openmrsForm, "The existing Openmrs Form object should not be null");
notNull(questionnaire, "The Questionnaire object should not be null");
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

import static lombok.AccessLevel.PACKAGE;

import ca.uhn.fhir.model.valueset.BundleTypeEnum;
import ca.uhn.fhir.rest.annotation.*;
import ca.uhn.fhir.rest.api.server.IBundleProvider;
import ca.uhn.fhir.rest.server.IResourceProvider;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import lombok.Setter;
Expand Down Expand Up @@ -45,20 +47,15 @@ public Questionnaire getQuestionnaireById(@IdParam IdType id) {
}
return questionnaire;
}

/*
@Search
@SuppressWarnings("unused")
public IBundleProvider searchQuestionnaire(@OptionalParam(name = Questionnaire.SP_NAME) StringAndListParam name,
@OptionalParam(name = Questionnaire.SP_RES_ID) TokenAndListParam id,
@OptionalParam(name = "_lastUpdated") DateRangeParam lastUpdated, @Sort SortSpec sort,
HashSet<Include> includes) {
if (CollectionUtils.isEmpty(includes)) {
includes = null;
}
return fhirQuestionnaireService
.searchForQuestionnaire(new QuestionnaireSearchParams(name, id, lastUpdated, sort, includes));
}
*/

/**
* The $everything operation fetches all the information related to all the questionnaires
*
* @return a bundle of resources which reference to or are referenced from the questionnaires
*/
@Operation(name = "everything", idempotent = true, type = Questionnaire.class, bundleType = BundleTypeEnum.SEARCHSET)
public IBundleProvider getQuestionnaireEverything() {
return fhirQuestionnaireService.getQuestionnaireEverything();
}

}

0 comments on commit 538eb33

Please sign in to comment.