Show TOC Start of Content Area

Procedure documentation Implementing the ProjectManagementService Session Bean  Locate the document in its SAP Library structure

Procedure

1. Injecting Resources to Bean’s Environment

...

       1.      Open the ProjectManagementService bean in the source code editor and define the following variables that hold the injected resources:

private EntityManager em;

private SessionContext sessionCtx;

private ProjectChangeSenderLocal jmsSender;

       2.      To inject the persistence context, select the em variable in the Outline view and choose Java EE Annotations General PersistenceContext from the context menu.

This generates the @PersistenceContext annotation.

This graphic is explained in the accompanying text

       3.      Enter ems/EJB3_EDM_DEMO_PU as a value of the unitName attribute of the annotation.

       4.      Inject the remaining two resources using the same procedure.

Variable

Context Menu Path

Attribute Values

sessionCtx

Java EE Annotations → References → Resource

 

jmsSender

Java EE Annotations References EJB

name="JMSSenderSessionLocal"

       5.      From the secondary mouse button (within the code content), select Source Organize Imports.

2. Querying the Database

Using the simplified EntityManager API, as well as the new named queries available in EJB 3.0 query language, it is easy to implement business methods that query the database. For example, to find a project by its ID, you do not have to write your own query. EntityManager’s find() method does that for you. You can also use EntityManager’s createNameQuery()method to execute any of the named queries you defined in the Employee entity.

Use the following code to define and implement all methods of the ProjectManagementService bean:

/**

    * Get a project by its id.

    * @return project entity

    */

 

   public CeraProject getProjectById(String pId){

      return em.find(CeraProject.class, Integer.valueOf(pId));

   }

  

   /**

    * Get all projects in a single list.

    * @return list of all projects

    */

   @SuppressWarnings("unchecked")

   public List<CeraProject> getAllProjects(){

      Query query = em.createNamedQuery("Project.getAll");

      List<CeraProject> result = query.getResultList();

      return result;

   }

 

   /**

    * Get all assigned projects.

    * @return list of all assigned projects

    */

   @SuppressWarnings("unchecked")

   public List<CeraProject> getAssignedProjects(int pEmployeeId){

      Query query = em.createNamedQuery("Project.getAllForEmployee");

      query.setParameter("employeeId", pEmployeeId);

      List<CeraProject> result = query.getResultList();

      return result;

   }

 

   /**

    * Removes a project.

    * @param pProject

    */

   public void removeProject(CeraProject pProject){

      em.remove(pProject);

   }

  

   /**

    * Persists a project (create or update) and sends a message to

    * the projectchangequeue to notify the system that the project

    * was changed

    */

   @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)

   public CeraProject createProject(CeraProject pProject){

      CeraProject result = em.merge(pProject);

      String caller = sessionCtx.getCallerPrincipal().getName();

      jmsSender.sendMessage(result, caller);

      return result;

   }

 

   /**

    * Get a list of projects by its name or description.

    * @param Name or description fragment

    * @return List of projects

    */

   @SuppressWarnings("unchecked")

   public List<CeraProject> findProjectByNameOrDescription(String pNameFragment){

      Query query = em.createNamedQuery("Project.findByNameOrDescPart");

      String parameter = pNameFragment.toLowerCase();

      query.setParameter("namepart", "%"+parameter+"%");

      query.setParameter("namepart1", "%"+parameter+"%");

      List<CeraProject> result = query.getResultList();

      return result;

   }

From the secondary mouse button (within the code content), select Source Organize Imports javax.persistence Next java.util.List and choose Finish.

3. Exposing Methods to the Local Business Interface

...

       1.      Select, for example, the getProjectById() method of the bean in the Outline view.

       2.      To add it to the local business interface, choose EJB Methods Add to Local Interfaces from the context menu.

This adds the method’s definition to the business interface of the bean, thereby making it visible to other enterprise beans in the application

This graphic is explained in the accompanying text

       3.      Repeat the same steps to expose all other methods of the ProjectManagementService bean to its business interface.

Result

The implementation of the bean should now look like the one shown in ProjectManagementService Source Code.

 

 

End of Content Area