Skip to content

Commit

Permalink
HTML-768: In HFE-UI, support form versions when loading via definitio… (
Browse files Browse the repository at this point in the history
#42)

* HTML-768: In HFE-UI, support form versions when loading via definitionUIResource

* follow-up commit
  • Loading branch information
mogoodrich authored Jan 28, 2021
1 parent 1787495 commit d015d5a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.openmrs.module.htmlformentryui;

import org.apache.commons.lang3.StringUtils;
import org.openmrs.Encounter;
import org.openmrs.EncounterType;
import org.openmrs.Form;
import org.openmrs.Patient;
Expand All @@ -37,16 +38,29 @@
*/
public class HtmlFormUtil {

public static HtmlForm getHtmlFormFromUiResource(ResourceFactory resourceFactory, FormService formService, HtmlFormEntryService htmlFormEntryService, String providerAndPath) throws IOException {
public static HtmlForm getHtmlFormFromUiResource(ResourceFactory resourceFactory, FormService formService, HtmlFormEntryService htmlFormEntryService, String providerAndPath, Encounter encounter) throws IOException {
int ind = providerAndPath.indexOf(':');
String provider = providerAndPath.substring(0, ind);
String path = providerAndPath.substring(ind + 1);
return getHtmlFormFromUiResource(resourceFactory, formService, htmlFormEntryService, provider, path);
return getHtmlFormFromUiResource(resourceFactory, formService, htmlFormEntryService, provider, path, encounter);
}

public static HtmlForm getHtmlFormFromUiResource(ResourceFactory resourceFactory, FormService formService, HtmlFormEntryService htmlFormEntryService, String providerName, String resourcePath) throws IOException {
String xml = resourceFactory.getResourceAsString(providerName, resourcePath);
// should be of the format <htmlform formUuid="..." formVersion="..." formEncounterType="...">...</htmlform>
public static HtmlForm getHtmlFormFromUiResource(ResourceFactory resourceFactory, FormService formService, HtmlFormEntryService htmlFormEntryService, String providerName, String resourcePath, Encounter encounter) throws IOException {

String xml = null;

// first, see if there is a specific version of the form referenced by version number
if (encounter != null && encounter.getForm() != null && encounter.getForm().getVersion() != null) {
String resourcePathWithVersion = resourcePath.replaceAll("\\.xml$", "") + "_v" + encounter.getForm().getVersion() + ".xml";
xml = resourceFactory.getResourceAsString(providerName, resourcePathWithVersion);
// should be of the format <htmlform formUuid="..." formVersion="..." formEncounterType="...">...</htmlform>
}

// if not, use the bare resource path (without version number appended) to fetch the form
if (xml == null) {
xml = resourceFactory.getResourceAsString(providerName, resourcePath);

}

if (xml == null) {
throw new IllegalArgumentException("No resource found at " + providerName + ":" + resourcePath);
Expand All @@ -55,6 +69,17 @@ public static HtmlForm getHtmlFormFromUiResource(ResourceFactory resourceFactory
return getHtmlFormFromResourceXml(formService, htmlFormEntryService, xml);
}


// the new method above with "encounter" is preferred if an encounter is available, see: https://issues.openmrs.org/browse/HTML-768
public static HtmlForm getHtmlFormFromUiResource(ResourceFactory resourceFactory, FormService formService, HtmlFormEntryService htmlFormEntryService, String providerAndPath) throws IOException {
return getHtmlFormFromUiResource(resourceFactory, formService, htmlFormEntryService, providerAndPath, (Encounter) null);
}

// the new method above with "encounter" is preferred if an encounter is available, see: https://issues.openmrs.org/browse/HTML-768
public static HtmlForm getHtmlFormFromUiResource(ResourceFactory resourceFactory, FormService formService, HtmlFormEntryService htmlFormEntryService, String providerName, String resourcePath) throws IOException {
return getHtmlFormFromUiResource(resourceFactory, formService, htmlFormEntryService, providerName, resourcePath, null);
}

public static HtmlForm getHtmlFormFromResourceXml(FormService formService, HtmlFormEntryService htmlFormEntryService, String xml) {
try {
Document doc = HtmlFormEntryUtil.stringToDocument(xml);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@

package org.openmrs.module.htmlformentryui.fragment.controller.htmlform;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -61,6 +52,15 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*
*/
Expand Down Expand Up @@ -121,7 +121,7 @@ public void controller(FragmentConfiguration config,
form = formService.getFormByUuid(formUuid);
hf = htmlFormEntryService.getHtmlFormByForm(form);
} else if (StringUtils.isNotBlank(definitionUiResource)) {
hf = HtmlFormUtil.getHtmlFormFromUiResource(resourceFactory, formService, htmlFormEntryService, definitionUiResource);
hf = HtmlFormUtil.getHtmlFormFromUiResource(resourceFactory, formService, htmlFormEntryService, definitionUiResource, encounter);
}
}
if (hf == null && encounter != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private FormEntrySession getFormEntrySession(HtmlFormEntryService htmlFormEntryS
VisitDomainWrapper visitDomainWrapper, FeatureToggleProperties featureToggleProperties) throws Exception {
if (hf == null) {
if (StringUtils.isNotBlank(definitionUiResource)) {
hf = HtmlFormUtil.getHtmlFormFromUiResource(resourceFactory, formService, htmlFormEntryService, definitionUiResource);
hf = HtmlFormUtil.getHtmlFormFromUiResource(resourceFactory, formService, htmlFormEntryService, definitionUiResource, encounter);
if (hf == null) {
throw new IllegalArgumentException("No form found for resource " + definitionUiResource);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@
import org.openmrs.module.htmlformentry.HtmlFormEntryService;
import org.openmrs.module.htmlformentryui.HtmlFormUtil;
import org.openmrs.ui.framework.UiUtils;
import org.openmrs.ui.framework.annotation.FragmentParam;
import org.openmrs.ui.framework.annotation.SpringBean;
import org.openmrs.ui.framework.page.PageModel;
import org.openmrs.ui.framework.resource.ResourceFactory;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;

public class BaseEditHtmlFormPageController {

public void get(@RequestParam("encounterId") Encounter encounter,
Expand All @@ -41,7 +38,7 @@ public void get(@RequestParam("encounterId") Encounter encounter,
HtmlForm htmlForm = null;

if (org.apache.commons.lang.StringUtils.isNotBlank(definitionUiResource)) {
htmlForm = HtmlFormUtil.getHtmlFormFromUiResource(resourceFactory, formService, htmlFormEntryService, definitionUiResource);
htmlForm = HtmlFormUtil.getHtmlFormFromUiResource(resourceFactory, formService, htmlFormEntryService, definitionUiResource, encounter);
if (htmlForm == null) {
throw new IllegalArgumentException("No form found for resource " + definitionUiResource);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public void controller(@RequestParam(value="patientId", required=false) Patient
*/
protected HtmlForm getHtmlFormFromResource(String resource, ResourceFactory rf, FormService fs, HtmlFormEntryService hfs) {
try {
// TODO figure out how flowsheets might work with: https://issues.openmrs.org/browse/HTML-768
HtmlForm form = HtmlFormUtil.getHtmlFormFromUiResource(rf, fs, hfs, resource);
if (form == null) {
throw new IllegalArgumentException("No form found for resource " + resource);
Expand Down

0 comments on commit d015d5a

Please sign in to comment.