Using the EntityManager Service

Information

This explains basic usage of the Sakai EntityManager Service. This service is used to find out things about Sakai Sites 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 cover to get the service
    • Note: This is not the recommended method, you should be using Spring to inject the service
    1. Setup a variable to hold the instance from the cover
      private EntityManager entityManager;
    2. Get access to the service using the cover
      entityManager = org.sakaiproject.entity.cover.EntityManager.getInstance();

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
   }
}
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Nov 05, 2007

    James Marca says:

    possible typo in the code block. Should be entityId, not authzGroupId, as in: ...

    possible typo in the code block. Should be entityId, not authzGroupId, as in:

    String entityId = (String) it.next(); // (1)
    Reference r = entityManager.newReference( entityId); // (2)
    

    1. Nov 06, 2007

      Aaron Zeckoski says:

      Good point. Example updated.

      Good point. Example updated.