Wiki Spaces
Documentation
Projects
Resources
Get Help from Others
Q&A: Ask OpenMRS
Discussion: OpenMRS Talk
Real-Time: IRC Chat | Slack
Everything is delivered from the database in the form of objects: Patient, User, Concept, etc java objects.
You can fetch/save these objects using what we call "services" (PatientService, UserService, ConceptService).
You can get the services at any point by calling Context.getPatientService(), .getUserService(), etc.
To print out the names of all the patients in the system:
List<Patient> patients = Context.getPatientService().getAllPatients(); for (Patient patient : patients) { System.out.println("Patient: " + patient.getGivenName() + " " + patient.getFamilyName()); }
See Web Services
We no longer test this use case so you may experience difficulties trying to use this approach. Please see Web Services as an alternative.
The openmrs-api-xxx.jar file can be used in stand-alone java applications. There are three simple steps you need to follow:
public static void main(String[] args) { File propsFile = new File(OpenmrsUtil.getApplicationDataDirectory(), "openmrs-runtime.properties");** Properties props = new Properties(); OpenmrsUtil.loadProperties(props, propsFile); Context.startup("jdbc:mysql://localhost:3306/db-name?autoReconnect=true", "openmrs-db-user", "3jknfjkn33ijt", props); try { Context.openSession(); Context.authenticate("admin", "test"); List<Patient> patients = Context.getPatientService().getPatients("John"); for (Patient patient : patients) { System.out.println("Found patient with name " + patient.getPersonName() + " and uuid: " + patient.getUuid()); } ... } finally { Context.closeSession(); } }
(the first 4 lines only have to be done once at startup. The rest are per request)
OpenMRS introduced Maven in 1.8. The easiest way to use the OpenMRS API is to also use maven for your project. Once you do that, simply use these in your pom.xml
<dependency> <groupId>org.openmrs.api</groupId> <artifactId>openmrs-api</artifactId> <version>${openMRSVersion}</version> <type>jar</type> <scope>provided</scope> </dependency> <dependency> <groupId>org.openmrs.api</groupId> <artifactId>openmrs-api</artifactId> <version>${openMRSVersion}</version> <type>test-jar</type> <scope>test</scope> </dependency> <dependency> <groupId>org.openmrs.web</groupId> <artifactId>openmrs-web</artifactId> <version>${openMRSVersion}</version> <type>jar</type> <scope>provided</scope> </dependency> <dependency> <groupId>org.openmrs.web</groupId> <artifactId>openmrs-web</artifactId> <version>${openMRSVersion}</version> <type>test-jar</type> <scope>test</scope> </dependency> <dependency> <groupId>org.openmrs.test</groupId> <artifactId>openmrs-test</artifactId> <version>${openMRSVersion}</version> <type>pom</type> <scope>test</scope> </dependency> <dependency> <groupId>org.openmrs.tools</groupId> <artifactId>openmrs-tools</artifactId> <version>${openMRSVersion}</version> </dependency> <properties> <openMRSVersion>1.8.1</openMRSVersion> </properties>
The openmrs-api-xxx.jar file can be used in stand-alone java applications. There are three simple steps you need to follow:
In order to start OpenMRS with all it's modules (especially necessary if "mandatory" modules as with OpenMRS 1.6 are present), it is not sufficient to simply include the required jar files. Choose one of the following approaches to get your app started:
module_repository_folder
with an absolute path to the modules directory of the OpenMRS webapp.application_data_directory
of the Overriding OpenMRS Default Runtime PropertiesNote that scheduled tasks can/may/will fail during initialization.
org.openmrs.web.OpenmrsFilter
(since the filter marks the boundaries around each HTTP request)Patient
object on one web page, you must reload that object, before using it on subsequent pages)