public class Context extends 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(Class cls,
org.aopalliance.aop.Advice advice)
Adds an AOP advice object around the given Class
cls |
static void |
addAdvisor(Class cls,
org.springframework.aop.Advisor advisor)
Adds an AOP advisor around the given Class
cls |
static void |
addProxyPrivilege(String privilege)
Convenience method.
|
static void |
authenticate(String username,
String password)
Used to authenticate user within the context
|
static void |
becomeUser(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 |
evictFromSession(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 Set<Role> |
getAllRoles(User user)
Convenience method.
|
static ArdenService |
getArdenService() |
static User |
getAuthenticatedUser() |
static CohortService |
getCohortService() |
static ConceptService |
getConceptService() |
static DataSetService |
getDataSetService()
Deprecated.
see reportingcompatibility module
|
static SimpleDateFormat |
getDateFormat()
Gets the simple date format for the current user's locale.
|
static EncounterService |
getEncounterService() |
static FormService |
getFormService() |
static HL7Service |
getHL7Service() |
static Locale |
getLocale()
Convenience method.
|
static LocationService |
getLocationService() |
static LogicService |
getLogicService() |
static MessageService |
getMessageService()
Get the message service.
|
static MessageSourceService |
getMessageSourceService() |
static ObsService |
getObsService() |
static OrderService |
getOrderService() |
static PatientService |
getPatientService() |
static PatientSetService |
getPatientSetService() |
static PersonService |
getPersonService() |
static ProgramWorkflowService |
getProgramWorkflowService() |
static <T> List<T> |
getRegisteredComponents(Class<T> type) |
static ReportObjectService |
getReportObjectService()
Deprecated.
see reportingcompatibility module
|
static ReportService |
getReportService()
Deprecated.
see reportingcompatibility module
|
static Properties |
getRuntimeProperties()
Get the runtime properties that this OpenMRS instance was started with
|
static SchedulerService |
getSchedulerService() |
static SerializationService |
getSerializationService() |
static <T> T |
getService(Class<? extends T> cls)
Used for getting services not in the previous get*Service() calls
|
static UserContext |
getUserContext()
Gets the user context from the thread local.
|
static UserService |
getUserService() |
static Object |
getVolatileUserData(String key)
Get a piece of information for the currently authenticated user.
|
static boolean |
hasPrivilege(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 Class<?> |
loadClass(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 |
refreshAuthenticatedUser()
Refresh the authenticated user object in the current UserContext.
|
static void |
removeAdvice(Class cls,
org.aopalliance.aop.Advice advice)
Removes the given AOP advice object from Class
cls |
static void |
removeAdvisor(Class cls,
org.springframework.aop.Advisor advisor)
Removes the given AOP advisor from Class
cls |
static void |
removeProxyPrivilege(String privilege)
Convenience method.
|
static void |
requirePrivilege(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 |
setLocale(Locale locale)
Convenience method.
|
static void |
setRuntimeProperties(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(String key,
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(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(String url,
String username,
String password,
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(Map<String,Object> userInput)
Updates the openmrs database to the latest.
|
public void setContextDAO(ContextDAO dao)
dao
- ContextDAO to setpublic static Class<?> loadClass(String className) throws ClassNotFoundException
className
- the class to loadClassNotFoundException
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(String username, 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(String systemId) throws ContextAuthenticationException
systemId
- ContextAuthenticationException
public static Properties getRuntimeProperties()
public static void setRuntimeProperties(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()
authenticate(java.lang.String, java.lang.String)
public static Set<Role> getAllRoles(User user) throws Exception
Exception
public static boolean hasPrivilege(String privilege)
public static void requirePrivilege(String privilege) throws ContextAuthenticationException
privilege
- ContextAuthenticationException
public static void addProxyPrivilege(String privilege)
UserContext.addProxyPrivilege(String)
public static void removeProxyPrivilege(String privilege)
UserContext.removeProxyPrivilege(String)
public static void setLocale(Locale locale)
UserContext.setLocale(Locale)
public static Locale getLocale()
UserContext.getLocale()
public static void openSession()
public static void closeSession()
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(Object obj)
obj
- The object to evict/remove from the sessionpublic static void startup(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(String url, String username, String password, 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(Class<? extends T> cls)
cls
- The Class of the service to getpublic static void addAdvisor(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(Class cls, org.aopalliance.aop.Advice advice)
cls
Advice comes in the form of before or afterReturning methods
cls
- advice
- public static void removeAdvisor(Class cls, org.springframework.aop.Advisor advisor)
cls
cls
- advisor
- public static void removeAdvice(Class cls, org.aopalliance.aop.Advice advice)
cls
cls
- advice
- public static void checkCoreDataset()
public static void updateDatabase(Map<String,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 Object getVolatileUserData(String key)
key
- identifying string for the informationpublic static void setVolatileUserData(String key, Object value)
key
- identifying string for this informationvalue
- information to be storedpublic static SimpleDateFormat getDateFormat()
OpenmrsUtil.getDateFormat(Locale)
public static boolean isRefreshingContext()
ServiceContext.isRefreshingContext()
public static <T> List<T> getRegisteredComponents(Class<T> type)
ServiceContext.getRegisteredComponents(Class)
public static void setUseSystemClassLoader(boolean useSystemClassLoader)
ServiceContext.setUseSystemClassLoader(boolean)
public static boolean isUseSystemClassLoader()
ServiceContext.isUseSystemClassLoader()
Copyright © 2018 OpenMRS LLC.. All Rights Reserved.