Support old and new UI in one module

With OpenMRS 2.x also a new UI type (based on uiframeworks module) was introduced.

Now with modules already supporting the old UI it would be neat to be able to add support for the new UI without having to create a new module but being able to offer support for both UI types in one module. Meaning, if you don't have the modules installed that are required by the new UI, your module would continue working on the old UI. As soon as you are adding the modules required to run the new UI, your module would also use the new UI. This is one way to get it working:

 

Note: This approach requires the @OpenmrsProfile annotation. So you need to make sure your OpenMRS core version supports that.

 

1) Configure your config.xml such as you are aware of the modules needed for the new UI type. They are of course not required because if you only want the old UI you do not need those.

<aware_of_module version="3.3.1">org.openmrs.module.uiframework</aware_of_module>
<aware_of_module version="2.3">org.openmrs.module.appframework</aware_of_module>
<aware_of_module version="2.2">org.openmrs.module.providermanagement</aware_of_module>
<aware_of_module version="1.6">org.openmrs.module.uicommons</aware_of_module>

 

2) Create a class to initialize the UiConfiguration bean (In this example the standard configuration is used). The @OpenmrsProfile(modules = { "uiframework:*.*" }) annotation takes care, that the bean is only created if the UIFramwork module is loaded.  You also need to make sure that you do not have the UIConfiguration bean defined in the webModuleApplicationContext.xml. So if you put it there before, remove it.

import org.openmrs.annotation.OpenmrsProfile;
import org.openmrs.ui.framework.StandardModuleUiConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@OpenmrsProfile(modules = { "uiframework:*.*" })
public class UiConfigurationInventory {
    
    @Bean
    public StandardModuleUiConfiguration createUiConfigurationBean() {
            StandardModuleUiConfiguration standardModuleUiConfiguration = new StandardModuleUiConfiguration();
            standardModuleUiConfiguration.setModuleId("yourmodule");
            return standardModuleUiConfiguration;
    }
}

 

3) As the controllers for the new UI are using objects defined in the UIFramework module, make sure that those controllers are only considered if the UIFramework module is present. Thus add @OpenmrsProfile(modules = { "uiframework:*.*" }) to your controllers.

@Controller
@OpenmrsProfile(modules = { "uiframework:*.*" })
public class InventoryLandingPageController {

    public void get(PageModel model, @SpringBean("appFrameworkService") AppFrameworkService appFrameworkService, PageRequest request, UiUtils ui) throws IOException {
        List<Extension> extensions = appFrameworkService.getExtensionsForCurrentUser(ModuleWebConstants.LANDING_PAGE_EXTENSION_POINT_ID);
        model.addAttribute("extensions", extensions);
    }
}