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.
Trunk javadocs:
Trunk source location: https://source.sakaiproject.org/svn/entity/trunk/
Accessing the EntityManager
You can use Spring Framework to inject the service or use the cover
Using Spring to get the service for your class (e.g. YourAppClass) (recommended)
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>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; }
Using the Component Manager to get the service
Note: This is not the recommended method, you should be using Spring to inject the service
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
Get the id of any Entity in Sakai
Use the EntityManager service to get the Reference
Check if the type of the reference is known
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
}
}