Module Application Context File


This file allows modules to override/append to the current spring application context definition. It must be named moduleApplicationContext.xml and be located in the metadata folder

The doctype should be set to that of the current spring library's doctype. Currently, that is

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

The mapping file must have a unique id associated with it.

In order to create your own module's service, the following bean must be used. This is unfortunately a lot of xml code to write, but there isn't a way around it. There are only a few tags at which values need to be manipulated (in bold):

<bean parent="serviceContext">
	<property name="moduleService">
		<list>
			<value>org.openmrs.module.formentry.FormEntryService</value> 
			<bean class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
				<property name="transactionManager"><ref bean="transactionManager"/></property>
				<property name="target">
					<bean class="org.openmrs.module.formentry.impl.FormEntryServiceImpl">
						<property name="formEntryDAO">
							<bean class="org.openmrs.module.formentry.db.hibernate.HibernateFormEntryDAO">
								<property name="sessionFactory"><ref bean="sessionFactory"/></property>	
							</bean>
						</property>	
					</bean>
				</property>
				<property name="preInterceptors">
					<list>
						<ref bean="authorizationInterceptor"/>
					</list>
				</property>
				<property name="transactionAttributeSource">
					<bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"/>
				</property>
			</bean>
		</list>
	</property>
</bean>

Redirects can be defined with this file. If we have a file in our formEntry module's web folder named formTaskpane.jsp. Without anything added to the spring context file, this could only be accessed via /openmrs/module/formEntry/formTaskpane.htm. However, if we add a mapping for this file like:

<bean id="formEntryUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
	<property name="mappings">
       	<props>
			<prop key="**/formTaskpane.htm">formTaskpaneRedirect</prop>
		</props>
	</property>
</bean>

and another bean like so:

<bean id="formTaskpaneRedirect" class="org.openmrs.web.controller.RedirectController">
	<property name="redirectView"><value>/module/formEntry/formTaskpane</value></property>
</bean>

then we can link to /openmrs/formTaskpane.htm and get redirected internally (unbeknownst to the user) to the right jsp file.

Overriding a jsp page

A module can override any page listed in the openmrs-servlet.xml file. Your moduleApplicationContext.xml file just has to define a prop with the same key as the original page. Your applicationContext file also has to have an "order" value lower than 99 so that spring will process your module before the core openmrs-servlet.xml file.

This example will override the encounter form that is shown on the administration page with a custom jsp from this module:

<bean id="myModuleMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
     <property name="order"><value>1</value></property>
     <property name="mappings">
        <props>
           <prop key="admin/encounters/encounter.form">encounterFormController</prop>
        </props>
     </property>
</bean>

<bean id="encounterFormController" class="@MODULE_PACKAGE@.web.controller.EncounterFormController">
     <property name="commandName"><value>encounter</value></property>
     <property name="formView"><value>/module/@MODULE_ID@/encounterForm</value></property>
     <property name="successView"><value>../../patientDashboard.form</value></property>
</bean>