1:package is346;
   2:
   3:import javax.ejb.SessionBean;
   4:import javax.ejb.SessionContext;
   5:import javax.ejb.CreateException;
   6:import javax.ejb.*;
   7:import java.util.List;
   8:import java.util.ArrayList;
   9:import java.util.Iterator;
  10:import java.util.Collection;
  11:
  12:public class ArtistSessionFacadeBean
  13:    implements SessionBean {
  14:  SessionContext sessionContext;
  15:  private ArtistHome artistHome;
  16:  public void ejbCreate() throws CreateException {
  17:  }
  18:
  19:  public void ejbRemove() {
  20:  }
  21:
  22:  public void ejbActivate() {
  23:  }
  24:
  25:  public void ejbPassivate() {
  26:  }
  27:
  28:  public void setSessionContext(SessionContext sessionContext) {
  29:    this.sessionContext = sessionContext;
  30:    try {
  31:      findArtistHome();
  32:    }
  33:    catch (Exception e) {
  34:      throw new EJBException(e.getMessage());
  35:    }
  36:  }
  37:
  38:  public Integer createArtist(ArtistDto artistDto) throws EJBException {
  39:    if (artistDto == null) {
  40:      return null;
  41:    }
  42:    try {
  43:      Artist artist = artistHome.create(artistDto.getName());
  44:      setArtistFromArtistDto(artist, artistDto);
  45:      
  46:      
  47:      
  48:      
  49:      return artist.getId();
  50:    }
  51:    catch (Exception e) {
  52:      throw new EJBException(e.getMessage());
  53:    }
  54:  }
  55:
  56:  public void removeArtist(Integer id) throws EJBException {
  57:    try {
  58:      artistHome.remove(id);
  59:    }
  60:    catch (Exception e) {
  61:      throw new EJBException(e.getMessage());
  62:    }
  63:  }
  64:
  65:  public void removeArtist(ArtistDto artistDto) throws EJBException {
  66:    if (artistDto != null) {
  67:      Integer id = artistDto.getId();
  68:      removeArtist(id);
  69:    }
  70:  }
  71:
  72:  public void updateArtist(ArtistDto artistDto) throws EJBException {
  73:    if (artistDto != null) {
  74:      Integer id = artistDto.getId();
  75:      try {
  76:        Artist artist = artistHome.findByPrimaryKey(id);
  77:        setArtistFromArtistDto(artist, artistDto);
  78:      }
  79:      catch (Exception e) {
  80:        throw new EJBException(e.getMessage());
  81:      }
  82:    }
  83:  }
  84:
  85:  public void updateArtists(ArtistDto[] artistDtos) throws EJBException {
  86:    if (artistDtos != null) {
  87:      for (int i = 0; i < artistDtos.length; i++) {
  88:        updateArtist(artistDtos[i]);
  89:      }
  90:    }
  91:  }
  92:
  93:  public ArtistDto artistFindByPrimaryKey(Integer id) throws EJBException {
  94:    try {
  95:      return assembleArtistDto(artistHome.findByPrimaryKey(id));
  96:    }
  97:    catch (Exception e) {
  98:      throw new EJBException(e.getMessage());
  99:    }
 100:  }
 101:
 102:  private void setArtistFromArtistDto(Artist artist, ArtistDto artistDto) {
 103:    artist.setName(artistDto.getName());
 104:  }
 105:
 106:  private void findArtistHome() throws EJBException {
 107:    final String ENTITY_NAME = "java:comp/env/ejb/artist";
 108:    if (artistHome == null) {
 109:      try {
 110:        ServiceLocator locator = ServiceLocator.getInstance();
 111:        artistHome = (ArtistHome) locator.getEjbLocalHome(ENTITY_NAME);
 112:      }
 113:      catch (ServiceLocatorException e) {
 114:        throw new EJBException(e.getMessage());
 115:      }
 116:    }
 117:  }
 118:
 119:  private ArtistDto assembleArtistDto(Artist artist) {
 120:    return ArtistDtoAssembler.createDto(artist);
 121:  }
 122:
 123:  private ArtistDto[] assembleArtistDtos(Collection artists) {
 124:    List list = new ArrayList();
 125:    if (artists != null) {
 126:      Iterator iterator = artists.iterator();
 127:      while (iterator.hasNext()) {
 128:        Artist artist = (Artist) iterator.next();
 129:        list.add(assembleArtistDto(artist));
 130:      }
 131:    }
 132:    ArtistDto[] returnArray = new ArtistDto[list.size()];
 133:    return (ArtistDto[]) list.toArray(returnArray);
 134:  }
 135:}