|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.openmrs.api.context.Context
public class Context
Represents an OpenMRS Context
, which may be used to authenticate to the database and
obtain services in order to interact with the system.
The Context is split into a 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.
Both the 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.
To use the OpenMRS api there are four things that have to be done:
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 Summary | |
---|---|
Context()
Default public constructor |
Method Summary | ||
---|---|---|
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 |
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 |
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 DataSetService |
getDataSetService()
Deprecated. see reportingcompatibility module |
|
static java.text.SimpleDateFormat |
getDateFormat()
Gets the simple date 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 ObsService |
getObsService()
|
|
static OrderService |
getOrderService()
|
|
static PatientService |
getPatientService()
|
|
static PatientSetService |
getPatientSetService()
|
|
static PersonService |
getPersonService()
|
|
static ProgramWorkflowService |
getProgramWorkflowService()
|
|
static
|
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
|
getService(java.lang.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 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 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 |
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 |
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 |
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 |
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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Context()
Method Detail |
---|
public void setContextDAO(ContextDAO dao)
dao
- ContextDAO to setpublic static java.lang.Class<?> loadClass(java.lang.String className) throws java.lang.ClassNotFoundException
className
- the class to load
java.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 context
ContextAuthenticationException
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()
authenticate(java.lang.String, java.lang.String)
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 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 startup
InputRequiredException
- if the DatabaseUpdater
has determined that updates
cannot continue without input from the user
DatabaseUpdateException
- 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 properties
InputRequiredException
- if the DatabaseUpdater
has determined that updates
cannot continue without input from the user
DatabaseUpdateException
- 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 get
public 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 input
DatabaseUpdateException
- if an error occurred while updating
InputRequiredException
- if user input is requiredpublic static java.lang.Object getVolatileUserData(java.lang.String key)
key
- identifying string for the information
public 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 boolean isRefreshingContext()
ServiceContext.isRefreshingContext()
public static <T> java.util.List<T> getRegisteredComponents(java.lang.Class<T> type)
ServiceContext.getRegisteredComponents(Class)
|
OpenMRS-1.7.x | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |