Have you implemented OpenMRS? Please participate in the Implementation Site Survey. If you already have, thank you!
OpenMRS is intentionally built with multiple layers in mind. One of the layers is a java API that can be used in other projects just as easily as it can be inside of OpenMRS.
The API allows developers to interact with the complex OpenMRS Data Model with common Java objects. This provides greater data integrity as well as a simple to use approach.
Most of the tables in the data model have a one-to-one relationship with a Java object in the OpenMRS API.
To get data into and out of the database you go through the objects and services. If you want a person with id #1, you ask the PersonService for the Person object:
If you want to change data on this person and persist that in the database, you again use the personService:
The Services in the OpenMRS API make sure that only the correct columns are saved/updated in the database.
All services are accessed in a static way from the org.openmrs.api.context.Context object. You do not have to instantiate a new "Context" object and you do not ever call "new" on a service.
The primary usage of the Context is to get the services so you can fetch and persist things in the database:
The other usage is to access the currently logged in user and their locale settings:
The currently logged in user's current locale:
The currently logged in user's date pattern layout:
All of our services are interfaces. The default implementation of these services are named *ServiceImpl.java. The implementations can be found in the impl directory of the api package.
In order to create a new implementation of a service (for whatever unknown reason that would be in the future), we would only need to change the file specified in Spring's
Users are authenticated against the Context.
The current user's information is stored on the current thread. For the webapp, we have a special problem. Every request to the server is potentially on a new thread. Our custom OpenmrsFilter class wraps every request. It stores the user's UserContext on the user's session. Before each request the userContext is taken off the session and placed on the thread. After the entire request is complete, the userContext is removed from the thread (and placed back on the session).
Authorization is done through annotations on the interface:
Spring manages our transactions.
All calls within a session are considered to be on one transaction. If an error is encountered all calls are rolled back and an error is returned.
Within the webapp, a session is determined by Spring's OpenSessionInViewFilter. Every request is wrapped with an open session and close session, see the OpenmrsFilter class.
Outside the webapp a session should be wrapped with calls to Context.openSession() and Context.closeSession(). See How To Use the OpenMRS API
To wrap a service within a transaction we use annotations on the interface: