package corepatterns.apps.psa.ejb;

import corepatterns.apps.psa.core.*;
import corepatterns.apps.psa.dao.*;
import java.sql.*;

import javax.sql.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;

public class ResourceEntityBean implements EntityBean {
  public String employeeId;
  public String lastName;
  public String firstName;
  public String departmentId;
   ...
  
  // Zbir obiektw zalenych BlockOutTime
  public Collection blockoutTimes;

  // Zbir obiektw zalenych SkillSet
  public Collection skillSets;

  ...

  private EntityContext context;

  // implementacja metod komponentu entity
  public String ejbCreate(ResourceTO resource) 
  throws CreateException {
    try {
      this.employeeId = resource.employeeId;
      setResourceData(resource);
      getResourceDAO().create(resource);
    } catch (Exception ex) {
      throw new EJBException("Powd:" + ...);
    }
    return this.employeeId;
  }
  
  public String ejbFindByPrimaryKey(String primaryKey) 
  throws FinderException {
    boolean result;
    try {
      ResourceDAO resourceDAO = getResourceDAO();
      result =  resourceDAO.findResource(primaryKey);
    } catch (Exception ex) {
      throw new EJBException("Powd:" + ...);
    }
    if (result) {
      return primaryKey;
    }
    else {
      throw new ObjectNotFoundException(...);
    }
  }
  
  public void ejbRemove() {
    try {
      // Usuwanie obiektw zalenych
      if (this.skillSets != null) {
        SkillSetDAO skillSetDAO = getSkillSetDAO();
        skillSetDAO.deleteAll(employeeId);
        skillSets = null;
      }
      if (this.blockoutTime != null) {
        BlockOutTimeDAO blockouttimeDAO =
          getBlockOutTimeDAO();
        blockouttimeDAO.deleteAll(employeeId);
        blockOutTimes = null;
      }

      // Usuwanie zasobu z trwaego magazynu
      ResourceDAO resourceDAO = new ResourceDAO();
      resourceDAO.delete(employeeId);
    } catch (ResourceException ex) {
      throw new EJBException("Reason:"+...);
    } catch (BlockOutTimeException ex) {
      throw new EJBException("Reason:"+...);
    } catch (Exception exception) {
      ...
    }
  }
  

  public void setEntityContext(EntityContext context) {
    this.context = context;
  }
  
  public void unsetEntityContext() {
    context = null;
  }
  
  public void ejbActivate() {
    employeeId = (String)context.getPrimaryKey();
  }
  
  public void ejbPassivate() {
    employeeId = null;
  }
  
  public void ejbLoad() {
    try {
      // wczytywanie informacji o zasobie
      ResourceDAO resourceDAO = getResourceDAO();
      setResourceData((ResourceTO) 
        resourceDAO.findResource(employeeId));
      
      // wczytywanie innych obiektw zalenych w razie koniecznoci
      ...
    } catch (Exception ex) {
      throw new EJBException("Powd:" + ...);
    }
  }
  
  public void ejbStore() {
    try {
      // Zapamitanie informacji zasobu
      getResourceDAO().update(getResourceData());

      // Zapamitanie w razie koniecznoci obiektw zaleznych
      ...
    } catch (SkillSetException ex) {
      throw new EJBException("Powd:" + ...);
    } catch (BlockOutTimeException ex) {
      throw new EJBException("Powd:" + ...);
    }
    ...
  }

  public void ejbPostCreate(ResourceTO resource) {
  }

  // metoda pobrania obiektu transferowego
  public ResourceTO getResourceTO() {
    // utworzenie nowego obiektu transferowego
    ResourceTO resourceTO = new ResourceTO(employeeId);

    // skopiowanie wszystkich wartoci 
    resourceTO.lastName = lastName;
    resourceTO.firstName = firstName;
    resourceTO.departmentId = departmentId;
    ...
    return resourceTO;
  }

  public void setResourceData(ResourceTO resourceTO) {
    // skopiowanie wartoci z obiektu tranasferowego do komponentu entity
    employeeId = resourceTO.employeeId;
    lastName = resourceTO.lastName;
    ...
  }

  // metody pobrania obiektw transferowych obiektw zalenych
  public Collection getSkillSetsData() {
    // jeli skillSets nie jest wczytany, wczytanie go
    // patrz implementacja strategii leniwego wczytywania
    return skillSets; 
  }
  ...

  // inne metody ustawiania i pobierania
  ...

  // metody biznesowe komponentu entity
  public void addBlockOutTimes(Collection moreBOTs) 
  throws BlockOutTimeException {
    // Uwaga: moreBOTs to zbir obiektw BlockOutTimeTO
    try {
      Iterator moreIter = moreBOTs.iterator();
      while (moreIter.hasNext()) {
        BlockOutTimeTO botTO =
          (BlockOutTimeTO) moreIter.next();
        if (! (blockOutTimeExists(botTO))) {
          // dodanie BlockOutTimeTO do zbioru
          botTO.setNew();
          blockOutTime.add(botTO);
        } else {
          // BlockOutTimeTO istnieje, nie mona doda
          throw new BlockOutTimeException(...);
        }
      }
    } catch (Exception exception) {
      throw new EJBException(...);
    }
  }

  public void addSkillSet(Collection moreSkills) 
  throws SkillSetException {
    // implementacja podobna do addBlockOutTime()
    ...
  }

  ...

  public void updateBlockOutTime(Collection updBOTs) 
  throws BlockOutTimeException {
    try {
      Iterator botIter = blockOutTimes.iterator();
      Iterator updIter = updBOTs.iterator();
      while (updIter.hasNext()) {
        BlockOutTimeTO botTO =
          (BlockOutTimeTO) updIter.next();
        while (botIter.hasNext()) {
          BlockOutTimeTO existingBOT = 
            (BlockOutTimeTO) botIter.next();
          // porwnanie wartoci kluczy w celu odnalezienia BlockOutTime
          if (existingBOT.equals(botTO)) {
            botTO.setDirty(); //modyfikacja sterego obiektu zalenego
            botTO.resetNew(); //nie jest to nowy obiekt zaleny
            existingBOT = botTO;
          }
        }
      }

    } catch (Exception exc) {
      throw new EJBException(...);
    }
  }

  public void updateSkillSet(Collection updSkills) 
  throws CommitmentException {
    // podobnie do updateBlockOutTime...
    ...
  }

  ...

}