org.openmrs.api.context
Class Context

java.lang.Object
  extended by org.openmrs.api.context.Context

public class Context
extends java.lang.Object

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.

See Also:
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 Advice comes in the form of before or afterReturning methods
static void addAdvisor(java.lang.Class cls, org.springframework.aop.Advisor advisor)
          Adds an AOP advisor around the given Class cls Advisors can wrap around a method and effect the method before or after
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()
          Used to clear cached objects out of a session in the middle of a unit of work.
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 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()
           
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 ObsService getObsService()
           
static OrderService getOrderService()
           
static PatientService getPatientService()
           
static PatientSetService getPatientSetService()
           
static PersonService getPersonService()
           
static ProgramWorkflowService getProgramWorkflowService()
           
static ReportObjectService getReportObjectService()
           
static ReportService getReportService()
           
static java.util.Properties getRuntimeProperties()
          Get the runtime properties that this OpenMRS instance was started with
static SchedulerService getSchedulerService()
           
static java.lang.Object getService(java.lang.Class 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 void logout()
          logs out the "active" (authenticated) user within context
static void openSession()
          Used to define a unit of work.
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.
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
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
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Context

public Context()
Default public constructor

Method Detail

setContextDAO

public void setContextDAO(ContextDAO dao)
Used to set the context's DAO for the application.

Parameters:
daoContext -

setUserContext

public 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. Making this thread safe might make this a bottleneck.

Parameters:
userContext -

clearUserContext

public static void clearUserContext()
Clears the user context from the threadlocal.


getUserContext

public static UserContext getUserContext()
Gets the user context from the thread local. This might be accessed by several threads at the same time.

Returns:

setServiceContext

public void setServiceContext(ServiceContext ctx)
Sets the service context.

Parameters:
ctx -

authenticate

public static void authenticate(java.lang.String username,
                                java.lang.String password)
                         throws ContextAuthenticationException
Used to authenticate user within the context

Parameters:
username - user's identifier token for login
password - user's password for authenticating to context
Throws:
ContextAuthenticationException

becomeUser

public static void becomeUser(java.lang.String systemId)
                       throws ContextAuthenticationException
Become a different user. (You should only be able to do this as a superuser.)

Parameters:
systemId -
Throws:
ContextAuthenticationException

getRuntimeProperties

public static java.util.Properties getRuntimeProperties()
Get the runtime properties that this OpenMRS instance was started with

Returns:
copy of the runtime properties

setRuntimeProperties

public static void setRuntimeProperties(java.util.Properties props)
Set the runtime properties to be used by this OpenMRS instance

Parameters:
props - runtime properties

getConceptService

public static ConceptService getConceptService()
Returns:
concept dictionary-related services

getEncounterService

public static EncounterService getEncounterService()
Returns:
encounter-related services

getLocationService

public static LocationService getLocationService()
Returns:
location services

getObsService

public static ObsService getObsService()
Returns:
observation services

getPatientService

public static PatientService getPatientService()
Returns:
patient-related services

getCohortService

public static CohortService getCohortService()

getPersonService

public static PersonService getPersonService()
Returns:
person-related services

getHL7Service

public static HL7Service getHL7Service()
Returns:
Returns the hl7Service.

getPatientSetService

public static PatientSetService getPatientSetService()
Returns:
patientset-related services

getUserService

public static UserService getUserService()
Returns:
user-related services

getOrderService

public static OrderService getOrderService()
Returns:
order service

getFormService

public static FormService getFormService()
Returns:
form service

getReportService

public static ReportService getReportService()
Returns:
report service

getReportObjectService

public static ReportObjectService getReportObjectService()
Returns:
report object service

getDataSetService

public static DataSetService getDataSetService()
Returns:
dataset service

getLogicService

public static LogicService getLogicService()
Returns:
logic service

getAdministrationService

public static AdministrationService getAdministrationService()
Returns:
admin-related services

getSchedulerService

public static SchedulerService getSchedulerService()
Returns:
scheduler service

getAlertService

public static AlertService getAlertService()
Returns:
alert service

getArdenService

public static ArdenService getArdenService()
Returns:
arden service

getProgramWorkflowService

public static ProgramWorkflowService getProgramWorkflowService()
Returns:
program- and workflow-related services

getMessageService

public static MessageService getMessageService()
Get the message service.

Returns:
message service

getAuthenticatedUser

public static User getAuthenticatedUser()
Returns:
"active" user who has been authenticated, otherwise null

isAuthenticated

public static boolean isAuthenticated()
Returns:
true if user has been authenticated in this context

logout

public static void logout()
logs out the "active" (authenticated) user within context

See Also:
authenticate(java.lang.String, java.lang.String)

getAllRoles

public static java.util.Set<Role> getAllRoles(User user)
                                       throws java.lang.Exception
Convenience method. Passes through to userContext.getAllRoles(User)

Throws:
java.lang.Exception

hasPrivilege

public static boolean hasPrivilege(java.lang.String privilege)
Convenience method. Passes through to userContext.hasPrivilege(String)


requirePrivilege

public static void requirePrivilege(java.lang.String privilege)
                             throws ContextAuthenticationException
Throws an exception if the currently authenticated user does not have the specified privilege.

Parameters:
privilege -
Throws:
ContextAuthenticationException

addProxyPrivilege

public static void addProxyPrivilege(java.lang.String privilege)
Convenience method. Passes through to @see org.openmrs.api.context.UserContext#addProxyPrivilege(java.lang.String)


removeProxyPrivilege

public static void removeProxyPrivilege(java.lang.String privilege)
Convenience method. Passes through to userContext.removeProxyPrivilege(String)


setLocale

public static void setLocale(java.util.Locale locale)
Convenience method. Passes through to userContext.setLocale(Locale)


getLocale

public static java.util.Locale getLocale()
Convenience method. Passes through to userContext.getLocale()


openSession

public static void openSession()
Used to define a unit of work. All "units of work" should be surrounded by openSession and closeSession calls.


closeSession

public static void closeSession()
Used to define a unit of work. All "units of work" should be surrounded by openSession and closeSession calls.


clearSession

public static void clearSession()
Used to clear cached objects out of a session in the middle of a unit of work.


evictFromSession

public 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. Future updates to this object will not be saved. Future gets of this object will not fetch this cached copy

Parameters:
obj - The object to evict/remove from the session

startup

public static void startup(java.util.Properties props)
Starts the OpenMRS System Should be called prior to any kind of activity

Parameters:
Properties - runtime properties to use for startup

startup

public 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

Parameters:
url - database url like "jdbc:mysql://localhost:3306/openmrs?autoReconnect=true"
username - connection username
password - connection password
Properties - other startup properties

shutdown

public static void shutdown()
Stops the OpenMRS System Should be called after all activity has ended and application is closing


getService

public static java.lang.Object getService(java.lang.Class cls)
Used for getting services not in the previous get*Service() calls

Parameters:
cls -
Returns:

addAdvisor

public static void addAdvisor(java.lang.Class cls,
                              org.springframework.aop.Advisor advisor)
Adds an AOP advisor around the given Class cls Advisors can wrap around a method and effect the method before or after

Parameters:
cls -
advisor -

addAdvice

public static void addAdvice(java.lang.Class cls,
                             org.aopalliance.aop.Advice advice)
Adds an AOP advice object around the given Class cls Advice comes in the form of before or afterReturning methods

Parameters:
cls -
advice -

removeAdvisor

public static void removeAdvisor(java.lang.Class cls,
                                 org.springframework.aop.Advisor advisor)
Removes the given AOP advisor from Class cls

Parameters:
cls -
advisor -

removeAdvice

public static void removeAdvice(java.lang.Class cls,
                                org.aopalliance.aop.Advice advice)
Removes the given AOP advice object from Class cls

Parameters:
cls -
advice -

checkCoreDataset

public static void checkCoreDataset()
Runs through the core data (e.g. privileges and global properties) and adds them if necessary.


getVolatileUserData

public static java.lang.Object getVolatileUserData(java.lang.String key)
Get a piece of information for the currently authenticated user. This information is stored only temporarily. When a new module is loaded or the server is restarted, this information will disappear. If there is not information by this key, null is returned TODO: This needs to be refactored/removed

Parameters:
key - identifying string for the information
Returns:
the information stored

setVolatileUserData

public static void setVolatileUserData(java.lang.String key,
                                       java.lang.Object value)
Set a piece of information for the currently authenticated user. This information is stored only temporarily. When a new module is loaded or the server is restarted, this information will disappear TODO: This needs to be refactored/removed

Parameters:
key - identifying string for this information
value - information to be stored

getDateFormat

public static java.text.SimpleDateFormat getDateFormat()
Gets the simple date format for the current user's locale. The format will be similar in size to mm/dd/yyyy

Returns:
SimpleDateFormat
See Also:
OpenmrsConstants.OPENMRS_LOCALE_DATE_PATTERNS()

isRefreshingContext

public static boolean isRefreshingContext()
Returns:
true/false whether the service context is currently being refreshed
See Also:
ServiceContext.isRefreshingContext()

OpenMRS-trunk

Generated May 29 2008 02:01 AM. NOTE - these libraries are in active development and subject to change