User Directory Service Exercise

These steps outline a basic approach for calling the User Directory Service from within a stock Hello World application created using the Sakai App Builder plugin for eclipse. Among other things, the User Directory Service provides information about the current user, which is what we will display in the following example.

  1. Create a new RSF Hello World tool using the Sakai App Builder.
  2. Add the user-api and entity-api projects to the java build path for the project.
  3. Spring needs to know to inject the userDirectoryService into your producer. Add the following definition inside the producer bean element in tool/src/webapp/WEB-INF/requestContext.xml:
    <property name="userDirectoryService" ref="org.sakaiproject.user.api.UserDirectoryService"/>
  4. Add the new dependences to tool/project.xml:
    <dependency>
    	<groupId>sakaiproject</groupId>
    	<artifactId>sakai-user-api</artifactId>
    	<version>${sakai.version}</version>
    </dependency>
    
    <dependency>
    	<groupId>sakaiproject</groupId>
    	<artifactId>sakai-entity-api</artifactId>
    	<version>${sakai.version}</version>
    </dependency>
    
  5. You need to add a setter to receive the userDirectoryService from Spring. Add the following code to the producer, inside the class definition, but outside of any other method:
    private UserDirectoryService userDirectoryService;
    public void setUserDirectoryService(UserDirectoryService userDirectoryService) {
    	this.userDirectoryService = userDirectoryService;
    }
    and add the following code to your fillComponents method:
    User currentUser = userDirectoryService.getCurrentUser();
    UIOutput.make(tofill, "current-displayname", currentUser.getDisplayName());
    UIOutput.make(tofill, "current-email", currentUser.getEmail());
  6. Now update the template to add a place holder for the new data. Replace the default header with:
    <h3>Hello, <span rsf:id="current-displayname">User</span></h3>
    
    <p>Your email address is &quot;<span rsf:id="current-email">email@your.domain</span>&quot;.</p>
  7. Rebuild, restart and test.
  8. If desired, you can detect and provide a default for empty email addresses by editing fillComponents method of the producer again, replacing the single UIOutput call with:
    if (currentUser.getEmail().length() > 0) {
    	UIOutput.make(tofill, "current-email", currentUser.getEmail());
    }			
    else {
    	UIOutput.make(tofill, "current-email", "unknown");
    }
  9. Rebuild, restart and test.

Download the source code for the completed exercise if you want to see all of the above changes in context.