diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/FhirQuestionnaireService.java b/api/src/main/java/org/openmrs/module/fhir2/api/FhirQuestionnaireService.java index 20e51fe79..34366add4 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/FhirQuestionnaireService.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/FhirQuestionnaireService.java @@ -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 { - - //IBundleProvider searchForQuestionnaire(QuestionnaireSearchParams questionnaireSearchParams); + + IBundleProvider getQuestionnaireEverything(); } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/FhirQuestionnaireDao.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/FhirQuestionnaireDao.java index 5c7f4aff2..935eda690 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/FhirQuestionnaireDao.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/FhirQuestionnaireDao.java @@ -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 { +public interface FhirQuestionnaireDao extends FhirDao
{ @Override @Authorized(PrivilegeConstants.GET_FORMS) - FormResourceAuditable get(@Nonnull String uuid); + Form get(@Nonnull String uuid); @Override @Authorized(PrivilegeConstants.GET_FORMS) - List getSearchResults(@Nonnull SearchParameterMap theParams); + List getSearchResults(@Nonnull SearchParameterMap theParams); } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirQuestionnaireDaoImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirQuestionnaireDaoImpl.java index 41eea5786..02a86b4aa 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirQuestionnaireDaoImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirQuestionnaireDaoImpl.java @@ -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 implements FhirQuestionnaireDao { +public class FhirQuestionnaireDaoImpl extends BaseFhirDao 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); } } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirQuestionnaireServiceImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirQuestionnaireServiceImpl.java index 5dfb744e6..84117f757 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirQuestionnaireServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirQuestionnaireServiceImpl.java @@ -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 implements FhirQuestionnaireService { +public class FhirQuestionnaireServiceImpl extends BaseFhirService implements FhirQuestionnaireService { @Autowired private FhirQuestionnaireDao dao; @@ -39,18 +45,23 @@ public class FhirQuestionnaireServiceImpl extends BaseFhirService searchQueryInclude; @Autowired - private SearchQuery> searchQuery; - - /* + private SearchQuery> 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 revIncludes = new HashSet<>(); + theParams.addParameter(FhirConstants.REVERSE_INCLUDE_SEARCH_HANDLER, revIncludes); } } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/QuestionnaireTranslator.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/QuestionnaireTranslator.java index 8fae97898..e67189bb2 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/QuestionnaireTranslator.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/QuestionnaireTranslator.java @@ -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 { +public interface QuestionnaireTranslator extends OpenmrsFhirUpdatableTranslator { /** * 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); } diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/QuestionnaireTranslatorImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/QuestionnaireTranslatorImpl.java index 0bda206e2..3d27773ba 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/QuestionnaireTranslatorImpl.java +++ b/api/src/main/java/org/openmrs/module/fhir2/api/translators/impl/QuestionnaireTranslatorImpl.java @@ -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"); diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/util/FormResourceAuditable.java b/api/src/main/java/org/openmrs/module/fhir2/api/util/FormResourceAuditable.java deleted file mode 100644 index 50b140a18..000000000 --- a/api/src/main/java/org/openmrs/module/fhir2/api/util/FormResourceAuditable.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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.util; - -import java.util.Date; - -import org.openmrs.Auditable; -import org.openmrs.FormResource; -import org.openmrs.User; - -public class FormResourceAuditable extends FormResource implements Auditable { - - public FormResourceAuditable() { - }; - - public FormResourceAuditable(FormResource formResource) { - super(formResource); - }; - - @Override - public User getCreator() { - return null; - } - - @Override - public void setCreator(User user) { - - } - - @Override - public Date getDateCreated() { - return null; - } - - @Override - public void setDateCreated(Date date) { - - } -} diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r4/QuestionnaireFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/QuestionnaireFhirResourceProvider.java index 79e3681b4..f582cbc5b 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/providers/r4/QuestionnaireFhirResourceProvider.java +++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/QuestionnaireFhirResourceProvider.java @@ -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; @@ -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 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(); + } + }