Using the EntityManager Service

Information

This explains basic usage of the Sakai EntityManager Service. This service is used to find out things about Sakai legacy entities and look them up by references or contexts.

Accessing the EntityManager

  • You can use Spring Framework to inject the service or use the cover
  1. Using Spring to get the service for your class (e.g. YourAppClass) (recommended)
    1. Add the EntityManager bean to the bean for YourAppClass
      <bean id="org.sakaiproject.yourapp.logic.YourAppClass"
      		class="org.sakaiproject.yourapp.logic.impl.YourAppClassImpl">
      	<property name="entityManager"
      		ref="org.sakaiproject.entity.api.EntityManager" />
      </bean>
      
    2. Add a variable and setter to YourAppClass to use the service in like so:
      private EntityManager entityManager;
      public void setEntityManager(EntityManager entityManager) {
      	this.entityManager = entityManager;
      }
      
  2. Using the Component Manager to get the service
    • Note: This is not the recommended method, you should be using Spring to inject the service
    1. Use the CM cover to get the service
      import org.sakaiproject.component.cover.ComponentManager;
      import org.sakaiproject.entity.api.EntityManager;
      ...
        private EntityManager entityManager;
      ...
          entityManager = (EntityManager) ComponentManager.get(EntityManager.class);
      

Getting the Reference for an Entity

  1. Get the id of any Entity in Sakai
  2. Use the EntityManager service to get the Reference
  3. Check if the type of the reference is known
  4. Use a service (e.g. SiteService) to compare the type
String entityId = entity.getId(); // (1)
Reference r = entityManager.newReference(entityId); // (2)
if(r.isKnownType()) { // (3)
   if(r.getType().equals(SiteService.APPLICATION_ID)) { // (4)
      // do something since this is a site
   }
}