package com.pugh.sockso.web.action.browse; import com.pugh.sockso.Utils; import com.pugh.sockso.db.Database; import com.pugh.sockso.music.Album; import com.pugh.sockso.music.Artist; import com.pugh.sockso.music.Track; import com.pugh.sockso.templates.web.browse.TAlbum; import com.pugh.sockso.web.BadRequestException; import com.pugh.sockso.web.Request; import com.pugh.sockso.web.action.BaseAction; import java.io.IOException; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; /** * shows an album and it's tracks * */ public class Albumer extends BaseAction { /** * browses an album * * @param req the request object * @param res the response object * * @throws IOException * @throws SQLException * @throws BadRequestException * */ @Override public void handleRequest() throws IOException, SQLException, BadRequestException { final Request req = getRequest(); final int id = Integer.parseInt( req.getUrlParam(2) ); final Album album = getAlbum ( id ); final List<Track> tracks = getAlbumTracks( id ); showAlbum( album, tracks ); } /** * shows the album page listing it's tracks * * @param album * @param tracks * * @throws java.io.IOException * */ protected void showAlbum( final Album album, final List<Track> tracks ) throws IOException, SQLException { final TAlbum tpl = new TAlbum(); tpl.setAlbum( album ); tpl.setTracks( tracks ); getResponse().showHtml( tpl ); } /** * fetches the tracks from an album * * @param albumId * * @return * * @throws java.sql.SQLException * */ protected List<Track> getAlbumTracks( final int albumId ) throws SQLException { ResultSet rs = null; PreparedStatement st = null; try { final Database db = getDatabase(); final String sql = Track.getSelectFromSql() + " where t.album_id = ? " + " order by t.track_no asc "; st = db.prepare( sql ); st.setInt( 1, albumId ); rs = st.executeQuery(); return Track.createListFromResultSet( rs ); } finally { Utils.close( rs ); Utils.close( st ); } } /** * fetches an album by id, if it's not found then a BadRequestException * is thrown * * @param id * * @return * * @throws java.sql.SQLException * @throws com.pugh.sockso.web.BadRequestException * */ protected Album getAlbum( final int id ) throws SQLException, BadRequestException { ResultSet rs = null; PreparedStatement st = null; try { final Database db = getDatabase(); final String sql = " select ar.id as artistId, ar.name as artistName, " + " al.id as albumId, al.name as albumName, al.year as albumYear, " + " al.date_added, ( " + " select count(*) " + " from play_log l " + " inner join tracks t " + " on t.id = l.track_id " + " where t.album_id = al.id " + " ) as playCount " + " from albums al " + " inner join artists ar " + " on ar.id = al.artist_id " + " where al.id = ? " + " limit 1 "; st = db.prepare( sql ); st.setInt( 1, id ); rs = st.executeQuery(); if ( !rs.next() ) throw new BadRequestException( "album not found", 404 ); final Artist artist = new Artist.Builder() .id(rs.getInt("artistId")) .name(rs.getString("artistName")) .build(); return new Album.Builder() .artist( artist ) .id( rs.getInt("albumId") ) .name( rs.getString("albumName") ) .year( rs.getString("albumYear") ) .dateAdded( rs.getDate("date_added") ) .playCount( rs.getInt("playCount") ) .build(); } finally { Utils.close( rs ); Utils.close( st ); } } }