public class Context
extends java.lang.Object
Context
, which may be used to authenticate to the database and
obtain services in order to interact with the system.UserContext
and ServiceContext
. The UserContext is
lightweight and there is an instance for every user logged into the system. The ServiceContext is
heavier and contains each service class. This is more static and there is only one ServiceContext
per OpenMRS instance. UserContext
and the ServiceContext
should not be used directly. This
Context class has methods to pass through to the currently defined UserContext for the thread and
the currently defined ServiceContext. startup(String, String, String, Properties)
to let the Context contact
the databaseopenSession()
to start a "unit of work".authenticate(String, String)
to authenticate the current user on the
current threadcloseSession()
to end your "unit of work" and commit all changes to the
database.public static void main(String[] args) { Context.startup("jdbc:mysql://localhost:3306/db-name?autoReconnect=true", "openmrs-db-user", "3jknfjkn33ijt", new Properties()); try { Context.openSession(); Context.authenticate("admin", "test"); Listpatients = Context.getPatientService().getPatientsByName("Fred"); patients.get(0).setBirthdate(new Date()); Context.getPatientService().savePatient(patients.get(0)); ... } finally { Context.closeSession(); } }
UserContext
,
ServiceContext
Constructor and Description |
---|
Context()
Default public constructor
|
Modifier and Type | Method and Description |
---|---|
static void |
addAdvice(java.lang.Class cls,
org.aopalliance.aop.Advice advice)
Adds an AOP advice object around the given Class
cls |
static void |
addAdvisor(java.lang.Class cls,
org.springframework.aop.Advisor advisor)
Adds an AOP advisor around the given Class
cls |
static void |
addConfigProperty(java.lang.Object key,
java.lang.Object value)
Add or replace a property in the config properties list
|
static void |
addProxyPrivilege(java.lang.String privilege)
Convenience method.
|
static void |
authenticate(java.lang.String username,
java.lang.String password)
Used to authenticate user within the context
|
static void |
becomeUser(java.lang.String systemId)
Become a different user.
|
static void |
checkCoreDataset()
Runs through the core data (e.g.
|
static void |
clearSession()
Clears cached changes made so far during this unit of work without writing them to the
database.
|
static void |
clearUserContext()
Clears the user context from the threadlocal.
|
static void |
closeSession()
Used to define a unit of work.
|
static void |
closeSessionWithCurrentUser()
Used when the a unit of work which started with a call for openSessionWithCurrentUser has finished.
|
static void |
evictFromSession(java.lang.Object obj)
Used to clear a cached object out of a session in the middle of a unit of work.
|
static void |
flushSession()
Forces any changes made so far in this unit of work to be written to the database
|
static ActiveListService |
getActiveListService() |
static AdministrationService |
getAdministrationService() |
static AlertService |
getAlertService() |
static java.util.Set<Role> |
getAllRoles(User user)
Convenience method.
|
static ArdenService |
getArdenService() |
static User |
getAuthenticatedUser() |
static CohortService |
getCohortService() |
static ConceptService |
getConceptService() |
static java.util.Properties |
getConfigProperties()
Get the config properties that have been added to this OpenMRS instance
|
static DataSetService |
getDataSetService()
Deprecated.
see reportingcompatibility module
|
static DatatypeService |
getDatatypeService() |
static java.text.SimpleDateFormat |
getDateFormat()
Gets the simple date format for the current user's locale.
|
static java.text.SimpleDateFormat |
getDateTimeFormat()
Gets the simple datetime format for the current user's locale.
|
static EncounterService |
getEncounterService() |
static FormService |
getFormService() |
static HL7Service |
getHL7Service() |
static java.util.Locale |
getLocale()
Convenience method.
|
static LocationService |
getLocationService() |
static LogicService |
getLogicService() |
static MessageService |
getMessageService()
Get the message service.
|
static MessageSourceService |
getMessageSourceService() |
static java.util.List<OpenmrsService> |
getModuleOpenmrsServices(java.lang.String modulePackage) |
static ObsService |
getObsService() |
static OrderService |
getOrderService() |
static PatientService |
getPatientService() |
static PatientSetService |
getPatientSetService() |
static PersonService |
getPersonService() |
static ProgramWorkflowService |
getProgramWorkflowService() |
static ProviderService |
getProviderService() |
static <T> T |
getRegisteredComponent(java.lang.String beanName,
java.lang.Class<T> type) |
static <T> java.util.List<T> |
getRegisteredComponents(java.lang.Class<T> type) |
static ReportObjectService |
getReportObjectService()
Deprecated.
see reportingcompatibility module
|
static ReportService |
getReportService()
Deprecated.
see reportingcompatibility module
|
static java.util.Properties |
getRuntimeProperties()
Get the runtime properties that this OpenMRS instance was started with
|
static SchedulerService |
getSchedulerService() |
static SerializationService |
getSerializationService() |
static <T> T |
getService(java.lang.Class<? extends T> cls)
Used for getting services not in the previous get*Service() calls
|
static java.text.SimpleDateFormat |
getTimeFormat()
Gets the simple time format for the current user's locale.
|
static UserContext |
getUserContext()
Gets the user context from the thread local.
|
static UserService |
getUserService() |
static VisitService |
getVisitService() |
static java.lang.Object |
getVolatileUserData(java.lang.String key)
Get a piece of information for the currently authenticated user.
|
static boolean |
hasPrivilege(java.lang.String privilege)
Convenience method.
|
static boolean |
isAuthenticated() |
static boolean |
isRefreshingContext() |
static boolean |
isSessionOpen()
This method tells whether
openSession() has been called or not already. |
static boolean |
isUseSystemClassLoader() |
static java.lang.Class<?> |
loadClass(java.lang.String className)
Loads a class with an instance of the OpenmrsClassLoader.
|
static void |
logout()
logs out the "active" (authenticated) user within context
|
static void |
openSession()
Used to define a unit of work.
|
static void |
openSessionWithCurrentUser()
Used to define a unit of work which does not require clearing out the currently authenticated user.
|
static void |
refreshAuthenticatedUser()
Refresh the authenticated user object in the current UserContext.
|
static void |
removeAdvice(java.lang.Class cls,
org.aopalliance.aop.Advice advice)
Removes the given AOP advice object from Class
cls |
static void |
removeAdvisor(java.lang.Class cls,
org.springframework.aop.Advisor advisor)
Removes the given AOP advisor from Class
cls |
static void |
removeConfigProperty(java.lang.Object key)
Remove a property from the list of config properties
|
static void |
removeProxyPrivilege(java.lang.String privilege)
Convenience method.
|
static void |
requirePrivilege(java.lang.String privilege)
Throws an exception if the currently authenticated user does not have the specified
privilege.
|
void |
setContextDAO(ContextDAO dao)
Used to set the context's DAO for the application.
|
static void |
setDAO(ContextDAO dao) |
static void |
setLocale(java.util.Locale locale)
Convenience method.
|
static void |
setRuntimeProperties(java.util.Properties props)
Set the runtime properties to be used by this OpenMRS instance
|
void |
setServiceContext(ServiceContext ctx)
Sets the service context.
|
static void |
setUserContext(UserContext ctx)
Sets the user context on the thread local so that the service layer can perform
authentication/authorization checks.
TODO Make thread-safe because this might be accessed by several thread at the same time. |
static void |
setUseSystemClassLoader(boolean useSystemClassLoader) |
static void |
setVolatileUserData(java.lang.String key,
java.lang.Object value)
Set a piece of information for the currently authenticated user.
|
static void |
shutdown()
Stops the OpenMRS System Should be called after all activity has ended and application is
closing
|
static void |
startup(java.util.Properties props)
Starts the OpenMRS System Should be called prior to any kind of activity
If an InputRequiredException is thrown, a call to DatabaseUpdater#update(Map)
will be required with a mapping from question prompt to user answer before startup can be
called again. |
static void |
startup(java.lang.String url,
java.lang.String username,
java.lang.String password,
java.util.Properties properties)
Starts the OpenMRS System in a _non-webapp_ environment
If an InputRequiredException is thrown, a call to DatabaseUpdater#update(Map)
will be required with a mapping from question prompt to user answer before startup can be
called again. |
static void |
updateDatabase(java.util.Map<java.lang.String,java.lang.Object> userInput)
Updates the openmrs database to the latest.
|
public void setContextDAO(ContextDAO dao)
dao
- ContextDAO to setpublic static void setDAO(ContextDAO dao)
public static java.lang.Class<?> loadClass(java.lang.String className) throws java.lang.ClassNotFoundException
className
- the class to loadjava.lang.ClassNotFoundException
public static void setUserContext(UserContext ctx)
ctx
- UserContext to setpublic static void clearUserContext()
public static UserContext getUserContext()
public void setServiceContext(ServiceContext ctx)
ctx
- public static void authenticate(java.lang.String username, java.lang.String password) throws ContextAuthenticationException
username
- user's identifier token for loginpassword
- user's password for authenticating to contextContextAuthenticationException
public static void refreshAuthenticatedUser()
getAuthenticatedUser()
User object.public static void becomeUser(java.lang.String systemId) throws ContextAuthenticationException
systemId
- ContextAuthenticationException
public static java.util.Properties getRuntimeProperties()
public static void setRuntimeProperties(java.util.Properties props)
props
- runtime propertiespublic static ConceptService getConceptService()
public static EncounterService getEncounterService()
public static LocationService getLocationService()
public static ObsService getObsService()
public static PatientService getPatientService()
public static CohortService getCohortService()
public static PersonService getPersonService()
public static HL7Service getHL7Service()
public static PatientSetService getPatientSetService()
public static UserService getUserService()
public static OrderService getOrderService()
public static FormService getFormService()
public static SerializationService getSerializationService()
@Deprecated public static ReportService getReportService()
@Deprecated public static ReportObjectService getReportObjectService()
@Deprecated public static DataSetService getDataSetService()
public static LogicService getLogicService()
public static AdministrationService getAdministrationService()
public static MessageSourceService getMessageSourceService()
public static SchedulerService getSchedulerService()
public static AlertService getAlertService()
public static ArdenService getArdenService()
public static ProgramWorkflowService getProgramWorkflowService()
public static MessageService getMessageService()
public static ActiveListService getActiveListService()
public static User getAuthenticatedUser()
null
public static boolean isAuthenticated()
public static void logout()
public static java.util.Set<Role> getAllRoles(User user) throws java.lang.Exception
java.lang.Exception
public static boolean hasPrivilege(java.lang.String privilege)
public static void requirePrivilege(java.lang.String privilege) throws ContextAuthenticationException
privilege
- ContextAuthenticationException
public static void addProxyPrivilege(java.lang.String privilege)
UserContext.addProxyPrivilege(String)
public static void removeProxyPrivilege(java.lang.String privilege)
UserContext.removeProxyPrivilege(String)
public static void setLocale(java.util.Locale locale)
UserContext.setLocale(Locale)
public static java.util.Locale getLocale()
UserContext.getLocale()
public static void openSession()
public static void closeSession()
public static void openSessionWithCurrentUser()
public static void closeSessionWithCurrentUser()
public static void clearSession()
public static void flushSession()
public static boolean isSessionOpen()
openSession()
has been called or not already. If it hasn't
been called, some methods won't work correctly because a UserContext
isn't available.openSession()
has been called already.public static void evictFromSession(java.lang.Object obj)
obj
- The object to evict/remove from the sessionpublic static void startup(java.util.Properties props) throws DatabaseUpdateException, InputRequiredException, ModuleMustStartException
InputRequiredException
is thrown, a call to DatabaseUpdater#update(Map)
will be required with a mapping from question prompt to user answer before startup can be
called again.props
- Runtime properties to use for startupInputRequiredException
- if the DatabaseUpdater
has determined that updates
cannot continue without input from the userDatabaseUpdateException
- if database updates are required, see
DatabaseUpdater.executeChangelog()
ModuleMustStartException
- if a module that should be started is not able toInputRequiredException#getRequiredInput() for
the required question/datatypes
public static void startup(java.lang.String url, java.lang.String username, java.lang.String password, java.util.Properties properties) throws DatabaseUpdateException, InputRequiredException, ModuleMustStartException
InputRequiredException
is thrown, a call to DatabaseUpdater#update(Map)
will be required with a mapping from question prompt to user answer before startup can be
called again. openSession()
, so you must call
closeSession()
somewhere on the same thread of this application so as to not
leak memory.url
- database url like "jdbc:mysql://localhost:3306/openmrs?autoReconnect=true"username
- Connection usernamepassword
- Connection passwordproperties
- Other startup propertiesInputRequiredException
- if the DatabaseUpdater
has determined that updates
cannot continue without input from the userDatabaseUpdateException
- if the database must be updated. See DatabaseUpdater
ModuleMustStartException
- if a module that should start is not able tostartup(Properties)
,
InputRequiredException#getRequiredInput() for
the required question/datatypes
public static void shutdown()
public static <T> T getService(java.lang.Class<? extends T> cls)
cls
- The Class of the service to getpublic static void addAdvisor(java.lang.Class cls, org.springframework.aop.Advisor advisor)
cls
Advisors can wrap around a method and effect the method before or after
cls
- advisor
- public static void addAdvice(java.lang.Class cls, org.aopalliance.aop.Advice advice)
cls
Advice comes in the form of before or afterReturning methods
cls
- advice
- public static void removeAdvisor(java.lang.Class cls, org.springframework.aop.Advisor advisor)
cls
cls
- advisor
- public static void removeAdvice(java.lang.Class cls, org.aopalliance.aop.Advice advice)
cls
cls
- advice
- public static void checkCoreDataset()
public static void updateDatabase(java.util.Map<java.lang.String,java.lang.Object> userInput) throws DatabaseUpdateException, InputRequiredException
startup(String, String, String, Properties)
,
if that fails, call this method to get the database up to speed.userInput
- (can be null) responses from the user about needed inputDatabaseUpdateException
- if an error occurred while updatingInputRequiredException
- if user input is requiredpublic static java.lang.Object getVolatileUserData(java.lang.String key)
key
- identifying string for the informationpublic static void setVolatileUserData(java.lang.String key, java.lang.Object value)
key
- identifying string for this informationvalue
- information to be storedpublic static java.text.SimpleDateFormat getDateFormat()
OpenmrsUtil.getDateFormat(Locale)
public static java.text.SimpleDateFormat getTimeFormat()
OpenmrsUtil.getTimeFormat(Locale)
public static java.text.SimpleDateFormat getDateTimeFormat()
OpenmrsUtil.getDateTimeFormat(Locale)
public static boolean isRefreshingContext()
ServiceContext.isRefreshingContext()
public static <T> java.util.List<T> getRegisteredComponents(java.lang.Class<T> type)
ServiceContext.getRegisteredComponents(Class)
public static <T> T getRegisteredComponent(java.lang.String beanName, java.lang.Class<T> type) throws APIException
APIException
ServiceContext.getRegisteredComponent(String, Class)
public static java.util.List<OpenmrsService> getModuleOpenmrsServices(java.lang.String modulePackage)
ServiceContext.getModuleOpenmrsServices(String)
public static VisitService getVisitService()
ServiceContext.getVisitService()
public static ProviderService getProviderService()
ServiceContext.getProviderService()
public static DatatypeService getDatatypeService()
ServiceCotext#getDatatypeService()
public static void addConfigProperty(java.lang.Object key, java.lang.Object value)
key
- name of the propertyvalue
- value of the propertypublic static void removeConfigProperty(java.lang.Object key)
key
- name of the propertypublic static java.util.Properties getConfigProperties()
public static void setUseSystemClassLoader(boolean useSystemClassLoader)
ServiceContext.setUseSystemClassLoader(boolean)
public static boolean isUseSystemClassLoader()
ServiceContext.isUseSystemClassLoader()
Copyright © 2018 OpenMRS Inc.. All Rights Reserved.