Show TOC Start of Content Area

Syntax documentation EmployeeManagementService Source Code  Locate the document in its SAP Library structure

 

package com.sap.nwce.ra.edm.ejb.services;

 

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

 

import javax.annotation.Resource;

import javax.ejb.Local;

import javax.ejb.SessionContext;

import javax.ejb.Stateless;

import javax.ejb.TransactionAttribute;

import javax.ejb.TransactionAttributeType;

import javax.ejb.TransactionManagement;

import javax.ejb.TransactionManagementType;

import javax.persistence.EntityManager;

import javax.persistence.NoResultException;

import javax.persistence.PersistenceContext;

import javax.persistence.Query;

 

import com.sap.nwce.ra.edm.ejb.entity.CeraDepartment;

import com.sap.nwce.ra.edm.ejb.entity.CeraEmployee;

import com.sap.nwce.ra.edm.ejb.entity.CeraNavigation;

import com.sap.nwce.ra.edm.ejb.entity.CeraSkill;

import com.sap.nwce.ra.edm.ejb.entity.CeraUsergroup;

 

 

/**

 * Employee management session facade. provides operations on employees and related objects.

 */

@Stateless

@Local ({EmployeeManagementServiceLocal.class})

@SuppressWarnings("boxing")

@TransactionManagement(value=TransactionManagementType.CONTAINER)

public class EmployeeManagementServiceBean implements EmployeeManagementServiceLocal {

 

   @Resource

   private SessionContext sessionCtx;

 

   @PersistenceContext(unitName = "ems/EJB3_EDM_DEMO_PU")

    private EntityManager em;

  

   /**

    * Get all employees in a single list.

    * @return list of all employees

    */

   @SuppressWarnings("unchecked")

   public List<CeraEmployee> getAllEmployees(){

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

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

        return result;

   }

  

   /**

    * Get an employee by its primary key.

    * @return employee entity

    */

   public CeraEmployee getEmployeeByID(String pId){

      ReferenceApplicationEJBLogger.logInfo("search for employee by Id: " + pId);

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

   }

  

   /**

    * Get a list of employees with matching lastname.

    * @return list of employees

    */

   @SuppressWarnings("unchecked")

   public List<CeraEmployee> findEmployeeByNamePart(String pNameFragment) {

      Query query = em.createNamedQuery("Employee.findByNamePart");

      //String parameter = pNameFragment.toLowerCase(); // native datasource variant

      String parameter = pNameFragment;

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

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

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

      return result;

   }

  

   /**

    * Get all skills.

    * @return

    */

   @SuppressWarnings("unchecked")

   public List<CeraSkill> getAllSkills() {

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

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

        return result;

   }

  

   /**

    * Persist the state of an Employee.

    * @param pEmployee

    */

   @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)

   public CeraEmployee saveEmployee(CeraEmployee pEmployee){

      ReferenceApplicationEJBLogger.logInfo("persist employee: " + pEmployee.getEmployeeId());

      return em.merge(pEmployee);

   }

 

   /**

    * Persist the state of an Skill.

    * @param pEmployee

    */

   @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)

   public CeraSkill saveSkill(CeraSkill pSkill){

      ReferenceApplicationEJBLogger.logInfo("persist skill: " + pSkill.getSkillId());

      return em.merge(pSkill);

   }

  

   public void removeSkill(CeraSkill pSkill){

      em.remove(pSkill);

   }

   /**

    * Get an employee by its login.

    * @param login of employee

    * @return Employee or null if no employee was found

    */

   @SuppressWarnings("unchecked")

   public CeraEmployee findEmployeeByLogin(String pLogin) {

      ReferenceApplicationEJBLogger.logInfo("find employee for login name: " + pLogin);

      CeraEmployee  result = null;

      Query query = em.createNamedQuery("Employee.findByLogin");

      query.setParameter("login", pLogin);

      try {

         result = (CeraEmployee)query.getSingleResult();

      } catch (NoResultException e) {

         ReferenceApplicationEJBLogger.logInfo("No Employee found for login name: " + pLogin);

         return null;

      }

      return result;

   }

  

   /**

    * Get employee associated with the current user by its login.

    */

   @SuppressWarnings("unchecked")

   public CeraEmployee findEmployeeByCurrentUserId() {

      return findEmployeeByLogin(sessionCtx.getCallerPrincipal().getName());

   }

 

   public CeraSkill getSkillById(String sklId){

      return em.find(CeraSkill.class, Integer.parseInt(sklId));

   }

  

   @SuppressWarnings("unchecked")

   public List<CeraSkill> findSkillByName(String pNameFragment){

      Query query = em.createNamedQuery("Skill.findByNamePart");

      //query.setParameter("namepart", "%"+pNameFragment.toLowerCase()+"%");

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

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

      return result;

   }

 

   /**

    * Get all departments.

    * @return list of departments

    */

   @SuppressWarnings("unchecked")

   public List<CeraDepartment> getAllDepartments(){

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

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

        return result;

   }

 

   /**

    * Get a department by its id.

    * @param depId

    * @return

    */

   public CeraDepartment getDepartmentById(String departmentId) {

      return em.find(CeraDepartment.class, departmentId);

   }

  

    /**

     * Get the list of all navigations for the employee.

     * @param employee login name to get the allowed navigations for

     * @return ordered list of navigations, empty if login was unknown

     */

   public List<CeraNavigation> getOrderedNavigations(String pLogin) {

      CeraEmployee emp = this.findEmployeeByLogin(pLogin);

      List<CeraNavigation> result = new ArrayList<CeraNavigation>();

      if(emp != null) {

         //if login was found return lsit, otherwise return empty list

         try {

            for(CeraUsergroup ug: emp.getGroups()){

               for(CeraNavigation nav: ug.getNavigations()){

                  if(!result.contains(nav)){

                     result.add(nav);

                  }

               }

            }

         } catch (Exception e) {

            // unexpected results?

            ReferenceApplicationEJBLogger.logInfo("getOrderedNavigations unexpected: " + e.getMessage());

         }

         Collections.sort(result, new NavigationComparator());

      }

      return result;

   }

   

    /**

     * This utility class allows for comparing Navigation entities by their position attributes.

     * @author D042764

     */

   private class NavigationComparator implements Comparator<CeraNavigation>{

 

      public int compare(CeraNavigation o1, CeraNavigation o2) {

         return o1.getPosition() - o2.getPosition();

      }

     

    }

  

   /**

    * Get the list of UserGroups of an employee.

    * @param employee id

    * @return list of UserGroup

    */

   public List<CeraUsergroup> getUserGroupsForEmployee(String pId){

      ReferenceApplicationEJBLogger.logInfo("get usergroups for employee id: " + pId);

      try {

         CeraEmployee e = em.find(CeraEmployee.class, Integer.valueOf(pId));

         if(e!=null) {

            return e.getGroups();

         }

      } catch (Exception e) {

         // unexpected problems

         ReferenceApplicationEJBLogger.logInfo("getUserGroupsForEmployee unexpected exception: " + e.getMessage());

         ReferenceApplicationEJBLogger.logException(e);

      }

      return new ArrayList<CeraUsergroup>();

   }

}

 

 

To combine the code you need to create additinal utility class ReferenceApplicationEJBLogger and copy and paste the code below. You can do this in a package named com.sap.nwce.ra.edm.util.

 

package com.sap.nwce.ra.edm.util;

 

import com.sap.tc.logging.Category;

import com.sap.tc.logging.FileLog;

import com.sap.tc.logging.Location;

import com.sap.tc.logging.Severity;

 

/**

 * Allow logging on the session bean side of the application.

 *

 */

public class ReferenceApplicationEJBLogger {

 

   private static final Location LOCATION = Location.getLocation("com.sap.nwce.ra.edm");

   private static final Category CATEGORY = Category.getCategory(Category.APPLICATIONS, "Reference Application");

  

   static{

      LOCATION.setMinimumSeverity(CATEGORY, Severity.INFO);

      LOCATION.setMaximumSeverity(CATEGORY, Severity.ALL);

      CATEGORY.addLog(new FileLog("./log/application/edmProject.%g.trc"));

   }

 

   /**

    * log a caught exception.

    * @param eMessage

    */

    public static void logException (Exception eMessage){

      logThrowable(eMessage);

    }

   

    /**

    * log a caught throwable.

    * @param eMessage

    */

    public static void logThrowable (Throwable eMessage){

      CATEGORY.logThrowableT(Severity.WARNING, LOCATION, eMessage.getMessage(), eMessage);

    }

   

    /**

     * trace an exception when application remains operable.

     * @param logWarning

     * @param eMessage

     */

    public static void logWarning (String logWarning, Exception eMessage) {

      CATEGORY.warningT(LOCATION, logWarning, new Object[]{eMessage});

    }

 

    /**

     * trace an information.

     * @param logString

     */

    public static void logInfo (String logString){

      CATEGORY.infoT(LOCATION, logString);

    }

   

    /**

     * trace an information.

     * @param logString

     */

    public static void logWarn (String logString){

      CATEGORY.warningT(LOCATION, logString);

    }

}

 

 

 

End of Content Area