⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 extracttimeentriesworkflowbean.java

📁 考勤管理系统源码
💻 JAVA
字号:
package com.wiley.compBooks.EJwithUML.TimeCardWorkflow;

import com.wiley.compBooks.EJwithUML.Base.EjbUtil.*;
import com.wiley.compBooks.EJwithUML.TimeCardDomain.*;
import com.wiley.compBooks.EJwithUML.Base.ApplicationExceptions.*;
import com.wiley.compBooks.EJwithUML.Base.*;
import com.wiley.compBooks.EJwithUML.Dtos.*;
import java.util.*;
import java.rmi.*;
import javax.ejb.*;
import javax.naming.*;


/**
 * The ExtractTimeEntriesWorkflow allows clients to retrieve time entries based
 * of extract criteria.
 * ExtractTimeEntriesWorkflowBean is the actual session bean implementation.
 */
public class ExtractTimeEntriesWorkflowBean extends BasicSessionBean
{
  public void ejbCreate() throws CreateException
  {
    System.out.println("created ExtractTimeEntriesWorkflowBean!");
  }

  public void ejbPostCreate()
  {
  }

  /**
   * Answers a HashMap of TimeEntryReportDTO objects for the given search
   * criteria for a single client.
   */
  public HashMap extractForCriteria(ExtractCriteriaDTO criteria) throws
  FatalApplicationException, DataNotFoundException, InvalidDataException,RemoteException
  {
    try
    {
      System.out.println("In extratcForCriteria()");
      Context initialContext = getInitialContext();
      UserLocalHome uhome = (UserLocalHome)initialContext.lookup(
                                                  EjbReferenceNames.USER_HOME);
      ClientLocalHome chome = (ClientLocalHome)initialContext.lookup(
                                                 EjbReferenceNames.CLIENT_HOME);
      Collection users = null;
      Collection clients = chome.findByName(criteria.getClient());
      //makes sure that only one client found.
      if (clients.size() > 1)
      {
        // the client name has more than one hit in the database.
        // requires one client.
        System.out.println("More than one client found by the name " +
                           criteria.getClient());
        throw new InvalidDataException(Origin.EXTRACT_TIME_ENTRY_WORKFLOW,
                                       "More than one client found.");
      }
      else if ( clients.size() == 0)
      {
        System.out.println("no client found by the name " +
                           criteria.getClient());
        throw new InvalidDataException(Origin.EXTRACT_TIME_ENTRY_WORKFLOW,
                                       "no client found.");
      }
      // uses a HashMap to collect all the time entries by user. User name is
      // key to the corresponding time entry report
      HashMap reports = new HashMap();
      ArrayList userNames = criteria.getUsers();
      if (userNames == null)
      {
        //all users
        System.out.println("extracting time entries for all users.");
        users = uhome.findAll();
        generateTimeReportsForUsers((ClientLocal)clients.iterator().next(), users,
        criteria.getStartDate().getTime(), criteria.getEndDate().getTime(), reports);
      }
      else
      {
        System.out.println("extracting time entries for " + userNames.size()
                           + " user(s).");
        //a list of pecific user
        for(int i = 0; i < userNames.size(); i++)
        {
          users = uhome.findByUserName((String)userNames.get(i));
          if (users.isEmpty())
          {
            System.out.println("warning:user(" + userNames.get(i) +
                               ") does not exists.");
          }
          generateTimeReportsForUsers((ClientLocal)clients.iterator().next(), users,
          criteria.getStartDate().getTime(), criteria.getEndDate().getTime(), reports);
        }
      }
      System.out.println("Done extratcForCriteria()");
      return reports;
    }
    catch (NamingException e)
    {
      throw new FatalApplicationException(Origin.EXTRACT_TIME_ENTRY_WORKFLOW, e,
                                             "User/Client Bean Not Found");
    }
    catch (FinderException e)
    {
      throw new DataNotFoundException(Origin.EXTRACT_TIME_ENTRY_WORKFLOW, e,
                                      "User/Client Not Found");
    }
  }

  /**
   * Answers a Collection of TimeCardDTO objects for a user.
   */
  public Collection getAllTimecardsForUser(String userName) throws
  FatalApplicationException, DataNotFoundException, InvalidDataException, RemoteException
  {
    try
    {
      System.out.println("In getAllTimecardsForUser()");
      Context initialContext = getInitialContext();
      UserLocalHome uhome = (UserLocalHome)initialContext.lookup(
                                                  EjbReferenceNames.USER_HOME);
      Collection users = uhome.findByUserName(userName);
      //makes sure that only one user found.
      if (users.size() > 1)
      {
        // the user name has more than one hit in the database.
        // requires one user.
        System.out.println("More than one user found by the name " +
                           userName);
        throw new InvalidDataException(Origin.EXTRACT_TIME_ENTRY_WORKFLOW,
                                       "More than one user found.");
      }
      else if (users.size() == 0)
      {
        System.out.println("no user found by the name " +
                           userName);
        throw new InvalidDataException(Origin.EXTRACT_TIME_ENTRY_WORKFLOW,
                                       "no user found by the name " + userName);
      }
      TimecardLocalHome tchome = (TimecardLocalHome)initialContext.lookup(
                                                  EjbReferenceNames.TIMECARD_HOME);
      Collection cards = tchome.findAllForUser(((UserPK)((UserLocal)
          users.iterator().next()).getPrimaryKey()).id);
      ArrayList cardDtos = new ArrayList();
      Iterator cardIterator = cards.iterator();
      System.out.println("total cards#" + cards.size());
      while(cardIterator.hasNext())
      {
        TimecardLocal card = (TimecardLocal) cardIterator.next();
        cardDtos.add(retrieveTimeEntriesForCard(card));
      }
      System.out.println("Done getAllTimecardsForUser()");
      return cardDtos;
    }
    catch (NamingException e)
    {
      throw new FatalApplicationException(Origin.EXTRACT_TIME_ENTRY_WORKFLOW, e,
                                            "User Bean Not Found");
    }
    catch (FinderException e)
    {
      throw new DataNotFoundException(Origin.EXTRACT_TIME_ENTRY_WORKFLOW, e,
                                     "User Not Found");
    }
  }

  private TimecardDTO retrieveTimeEntriesForCard(TimecardLocal timecard)
      throws InvalidDataException
  {
    System.out.println("In retrieveTimeEntriesForCard()");
    TimecardDTO tcDTO = new TimecardDTO(new Date(timecard.getStartDate()),
                                        new Date(timecard.getEndDate()));
    Collection timeEntries = timecard.getTimeEntries();
    Iterator timeEntryIterator = timeEntries.iterator();
    System.out.println("total entries#" + timeEntries.size());
    while(timeEntryIterator.hasNext())
    {
      TimeEntryLocal timeEntry = (TimeEntryLocal) timeEntryIterator.next();
      ChargeCodeLocal chargeCode = (ChargeCodeLocal) timeEntry.getChargeCode();
      TimeEntryDTO timeEntryDTO = new TimeEntryDTO(
          ((TimeEntryPK)timeEntry.getPrimaryKey()).id,
          chargeCode.getProject().getClient().getName(),
          chargeCode.getName(), chargeCode.getProject().getName(),
          timeEntry.getHours(), timeEntry.getDate());
      tcDTO.addEntry(timeEntryDTO);
    }
    System.out.println(tcDTO);
    System.out.println("Done retrieveTimeEntriesForCard()");
    return tcDTO;
  }

  private void generateTimeReportsForUsers(ClientLocal client, Collection users,
  long start, long end, HashMap reportsByUsers) throws FatalApplicationException,
                                                RemoteException
  {
    try
    {
      System.out.println("In generateTimeReportsForUsers()");
      Context initialContext = getInitialContext();
      TimecardLocalHome tchome = (TimecardLocalHome)initialContext.lookup(
                                                 EjbReferenceNames.TIMECARD_HOME);
      Collection projects = client.getProjects();
      Iterator userIterator = users.iterator();
      while(userIterator.hasNext())
      {
        UserLocal user = (UserLocal) userIterator.next();
        System.out.println("for user- " + user.getName() + " ..." );
        TimeEntryReportDTO report = new TimeEntryReportDTO(user.getName());
        try
        {
          Collection timecards = tchome.findTimecard(((UserPK)user.getPrimaryKey()).id,
                                                   start, end);
          if (timecards.isEmpty())
          {
            System.out.println("no timecard found for " + user.getName());
          }
          else
          {
            System.out.println( user.getName() + "'s timecard count#" + timecards.size());
            filterTimeEntries(report, projects, timecards);
          }
        }
        catch (FinderException e)
        {
          // there may not be any time entered by the user for timecards bewteen
          // start and end. Igonre the exception.
          System.out.println("no timecard found for " + user.getName() +
          " between " + DateUtil.toDateString(new Date(start)) + "and " +
          DateUtil.toDateString(new Date(end)));
        }
        reportsByUsers.put(user.getName(), report);
      } // end of while
      System.out.println("Done generateTimeReportsForUsers()");
    }
    catch (NamingException e)
    {
      throw new FatalApplicationException(Origin.EXTRACT_TIME_ENTRY_WORKFLOW, e,
                                             "Timecard Bean Not Found");
    }
  }

  /**
   * Populates TimeEntryReportDTO object with all the entries that has charge
   * code belonging to one of the projects provided.
   */
  private void filterTimeEntries(TimeEntryReportDTO report, Collection projects,
                                 Collection timecards)
  {
    System.out.println("In filterTimeEntries()");
    Iterator cardIterator = timecards.iterator();
    int cardCount = 0;
    //loops through each timecard
    while(cardIterator.hasNext())
    {
      cardCount++;
      System.out.println("timecard#" + cardCount);
      TimecardLocal card = (TimecardLocal) cardIterator.next();
      Collection entries = card.getTimeEntries();
      Iterator entryIterator = entries.iterator();
      int entryCount = 0;
      System.out.println("entry count#" + entries.size());
      //loops through each entry in the timecard
      while(entryIterator.hasNext())
      {
        entryCount++;
        System.out.println("time entry#" + entryCount);
        TimeEntryLocal entry = (TimeEntryLocal) entryIterator.next();
        Iterator projectIterator =  projects.iterator();
        // adds the entry to the report if the entry charged to any of the project
        // in the list. Breaks out of the loop as sson as we have a hit.
        while(projectIterator.hasNext())
        {
          ProjectLocal project = (ProjectLocal) projectIterator.next();
          if (project.getChargeCodes().contains(entry.getChargeCode()))
          {
            System.out.println("adding entry...");
            TimeEntryDTO entryDto = new TimeEntryDTO(((TimeEntryPK)entry.getPrimaryKey()).id,
            project.getClient().getName(), entry.getChargeCode().getName(),
            project.getName(), entry.getHours(), entry.getDate());
            report.addTimeEntry(entryDto);
            break;
          }
        } // end of project loop
      } // end of time entry loop
    } // end of timecard loop

    System.out.println("Done filterTimeEntries()");
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -