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 TrackSessionFacadeBean 13: implements SessionBean { 14: SessionContext sessionContext; 15: private TrackHome trackHome; 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: findTrackHome(); 32: } 33: catch (Exception e) { 34: throw new EJBException(e.getMessage()); 35: } 36: } 37: 38: public Integer createTrack(TrackDto trackDto) throws EJBException { 39: if (trackDto == null) { 40: return null; 41: } 42: try { 43: Track track = trackHome.create( trackDto.getDuration(),trackDto.getTitle()); 44: setTrackFromTrackDto(track, trackDto); 45: return track.getId(); 46: } 47: catch (Exception e) { 48: throw new EJBException(e.getMessage()); 49: } 50: } 51: 52: public void removeTrack(Integer id) throws EJBException { 53: try { 54: trackHome.remove(id); 55: } 56: catch (Exception e) { 57: throw new EJBException(e.getMessage()); 58: } 59: } 60: 61: public void removeTrack(TrackDto trackDto) throws EJBException { 62: if (trackDto != null) { 63: Integer id = trackDto.getId(); 64: removeTrack(id); 65: } 66: } 67: 68: public void updateTrack(TrackDto trackDto) throws EJBException { 69: if (trackDto != null) { 70: Integer id = trackDto.getId(); 71: try { 72: Track track = trackHome.findByPrimaryKey(id); 73: setTrackFromTrackDto(track, trackDto); 74: } 75: catch (Exception e) { 76: throw new EJBException(e.getMessage()); 77: } 78: } 79: } 80: 81: public void updateTracks(TrackDto[] trackDtos) throws EJBException { 82: if (trackDtos != null) { 83: for (int i = 0; i < trackDtos.length; i++) { 84: updateTrack(trackDtos[i]); 85: } 86: } 87: } 88: 89: public TrackDto trackFindByPrimaryKey(Integer id) throws EJBException { 90: try { 91: return assembleTrackDto(trackHome.findByPrimaryKey(id)); 92: } 93: catch (Exception e) { 94: throw new EJBException(e.getMessage()); 95: } 96: } 97: 98: private void setTrackFromTrackDto(Track track, TrackDto trackDto) throws 99: FinderException, ServiceLocatorException { 100: track.setTitle(trackDto.getTitle()); 101: track.setDuration(trackDto.getDuration()); 102: ArrayList artists=new ArrayList(); 103: ServiceLocator servicelocator=ServiceLocator.getInstance(); 104: 105: for(int i=0;i<trackDto.getArtists().length;i++) 106: { 107: 108: 109: 110: ArtistHome artistHome= (ArtistHome) servicelocator.getEjbLocalHome("Artist"); 111: Artist artist= artistHome.findByPrimaryKey(trackDto.getArtists()[i].getId()); 112: 113: artists.add(artist); 114: } 115: track.setArtist(artists); 116: } 117: 118: private void findTrackHome() throws EJBException { 119: final String ENTITY_NAME = "java:comp/env/ejb/track"; 120: if (trackHome == null) { 121: try { 122: ServiceLocator locator = ServiceLocator.getInstance(); 123: trackHome = (TrackHome) locator.getEjbLocalHome(ENTITY_NAME); 124: } 125: catch (ServiceLocatorException e) { 126: throw new EJBException(e.getMessage()); 127: } 128: } 129: } 130: 131: private TrackDto assembleTrackDto(Track track) { 132: return TrackDtoAssembler.createDto(track); 133: } 134: 135: private TrackDto[] assembleTrackDtos(Collection tracks) { 136: List list = new ArrayList(); 137: if (tracks != null) { 138: Iterator iterator = tracks.iterator(); 139: while (iterator.hasNext()) { 140: Track track = (Track) iterator.next(); 141: list.add(assembleTrackDto(track)); 142: } 143: } 144: TrackDto[] returnArray = new TrackDto[list.size()]; 145: return (TrackDto[]) list.toArray(returnArray); 146: } 147:}