Search in sources :

Example 1 with AlbumInfo

use of com.github.hakko.musiccabinet.domain.model.music.AlbumInfo in project musiccabinet by hakko.

the class AlbumInfoService method updateSearchIndex.

@Override
protected void updateSearchIndex() throws ApplicationException {
    List<Album> albums = albumInfoDao.getAlbumsWithoutInfo();
    List<AlbumInfo> albumInfos = new ArrayList<>();
    setTotalOperations(albums.size());
    for (Album album : albums) {
        try {
            WSResponse wsResponse = albumInfoClient.getAlbumInfo(album);
            if (wsResponse.wasCallAllowed() && wsResponse.wasCallSuccessful()) {
                StringUtil stringUtil = new StringUtil(wsResponse.getResponseBody());
                AlbumInfoParser aiParser = new AlbumInfoParserImpl(stringUtil.getInputStream());
                albumInfos.add(aiParser.getAlbumInfo());
                if (albumInfos.size() == BATCH_SIZE) {
                    albumInfoDao.createAlbumInfo(albumInfos);
                    albumInfos.clear();
                }
            }
        } catch (ApplicationException e) {
            LOG.warn("Fetching album info for " + album.getName() + " failed.", e);
        }
        addFinishedOperation();
    }
    albumInfoDao.createAlbumInfo(albumInfos);
}
Also used : AlbumInfoParserImpl(com.github.hakko.musiccabinet.parser.lastfm.AlbumInfoParserImpl) ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) AlbumInfo(com.github.hakko.musiccabinet.domain.model.music.AlbumInfo) ArrayList(java.util.ArrayList) AlbumInfoParser(com.github.hakko.musiccabinet.parser.lastfm.AlbumInfoParser) Album(com.github.hakko.musiccabinet.domain.model.music.Album) WSResponse(com.github.hakko.musiccabinet.ws.lastfm.WSResponse) StringUtil(com.github.hakko.musiccabinet.util.StringUtil)

Example 2 with AlbumInfo

use of com.github.hakko.musiccabinet.domain.model.music.AlbumInfo in project musiccabinet by hakko.

the class JdbcAlbumInfoDaoTest method createAndValidateUpdatedArtistInfos.

@Test
public void createAndValidateUpdatedArtistInfos() throws ApplicationException {
    deleteAlbumInfos();
    String newSmallUrl = "http://userserve-ak.last.fm/serve/34s/1234567.png";
    int newListeners = 1234567;
    dao.createAlbumInfo(Arrays.asList(aiNirvana, aiSchuller));
    aiNirvana.setSmallImageUrl(newSmallUrl);
    aiNirvana.setListeners(newListeners);
    dao.createAlbumInfo(Arrays.asList(aiNirvana, aiSchuller));
    AlbumInfo dbNirvana = dao.getAlbumInfo(aiNirvana.getAlbum());
    Assert.assertEquals(newSmallUrl, dbNirvana.getSmallImageUrl());
    Assert.assertEquals(newListeners, dbNirvana.getListeners());
}
Also used : AlbumInfo(com.github.hakko.musiccabinet.domain.model.music.AlbumInfo) Test(org.junit.Test)

Example 3 with AlbumInfo

use of com.github.hakko.musiccabinet.domain.model.music.AlbumInfo in project musiccabinet by hakko.

the class JdbcAlbumInfoDaoTest method loadFunctionDependency.

@Before
public void loadFunctionDependency() throws ApplicationException {
    PostgreSQLUtil.loadFunction(dao, UPDATE_ALBUMINFO);
    aiNirvana = new AlbumInfoParserImpl(new ResourceUtil(AI_NIRVANA_FILE).getInputStream()).getAlbumInfo();
    aiHurts = new AlbumInfoParserImpl(new ResourceUtil(AI_HURTS_FILE).getInputStream()).getAlbumInfo();
    aiSchuller = new AlbumInfoParserImpl(new ResourceUtil(AI_SCHULLER_FILE).getInputStream()).getAlbumInfo();
    aiNirvana2 = new AlbumInfoParserImpl(new ResourceUtil(AI_NIRVANA2_FILE).getInputStream()).getAlbumInfo();
    aiNirvana3 = new AlbumInfoParserImpl(new ResourceUtil(AI_NIRVANA3_FILE).getInputStream()).getAlbumInfo();
    deleteArtists();
    deleteLibraryTracks();
    createLibraryTracks(aiNirvana, aiNirvana2, aiNirvana3, aiHurts, aiSchuller);
    for (AlbumInfo ai : asList(aiNirvana, aiHurts, aiSchuller)) {
        ai.getAlbum().getArtist().setId(musicDao.getArtistId(ai.getAlbum().getArtist()));
    }
}
Also used : ResourceUtil(com.github.hakko.musiccabinet.util.ResourceUtil) AlbumInfoParserImpl(com.github.hakko.musiccabinet.parser.lastfm.AlbumInfoParserImpl) AlbumInfo(com.github.hakko.musiccabinet.domain.model.music.AlbumInfo) Before(org.junit.Before)

Example 4 with AlbumInfo

use of com.github.hakko.musiccabinet.domain.model.music.AlbumInfo in project musiccabinet by hakko.

the class JdbcAlbumInfoDaoTest method createAndValidateArtistInfos.

@Test
public void createAndValidateArtistInfos() throws ApplicationException {
    deleteAlbumInfos();
    List<AlbumInfo> albumInfos = new ArrayList<>();
    albumInfos.add(aiNirvana);
    albumInfos.add(aiHurts);
    dao.createAlbumInfo(albumInfos);
    AlbumInfo dbNirvana = dao.getAlbumInfo(aiNirvana.getAlbum());
    AlbumInfo dbHurts = dao.getAlbumInfo(aiHurts.getAlbum());
    Assert.assertEquals(aiNirvana, dbNirvana);
    Assert.assertEquals(aiHurts, dbHurts);
}
Also used : AlbumInfo(com.github.hakko.musiccabinet.domain.model.music.AlbumInfo) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with AlbumInfo

use of com.github.hakko.musiccabinet.domain.model.music.AlbumInfo in project musiccabinet by hakko.

the class JdbcAlbumInfoDao method getAlbumInfosForIds.

@Override
public Map<Integer, AlbumInfo> getAlbumInfosForIds(List<Integer> paths) {
    String sql = "select alb.album_name_capitalization, ai.mediumimageurl," + " ai.largeimageurl, ai.extralargeimageurl, alb.id from music.albuminfo ai" + " inner join music.album alb on ai.album_id = alb.id" + " where alb.id in (" + getParameters(paths.size()) + ")";
    final Map<Integer, AlbumInfo> albumInfos = new HashMap<>();
    try {
        jdbcTemplate.query(sql, paths.toArray(), new RowCallbackHandler() {

            @Override
            public void processRow(ResultSet rs) throws SQLException {
                AlbumInfo ai = new AlbumInfo();
                String albumName = rs.getString(1);
                ai.setMediumImageUrl(rs.getString(2));
                ai.setLargeImageUrl(rs.getString(3));
                ai.setExtraLargeImageUrl(rs.getString(4));
                int albumId = rs.getInt(5);
                ai.setAlbum(new Album(albumName));
                albumInfos.put(albumId, ai);
            }
        });
    } catch (DataAccessException e) {
        LOG.warn("Could not fetch album infos for paths " + paths + "!", e);
    }
    return albumInfos;
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) AlbumInfo(com.github.hakko.musiccabinet.domain.model.music.AlbumInfo) ResultSet(java.sql.ResultSet) Album(com.github.hakko.musiccabinet.domain.model.music.Album) RowCallbackHandler(org.springframework.jdbc.core.RowCallbackHandler) DataAccessException(org.springframework.dao.DataAccessException)

Aggregations

AlbumInfo (com.github.hakko.musiccabinet.domain.model.music.AlbumInfo)14 Test (org.junit.Test)5 Album (com.github.hakko.musiccabinet.domain.model.music.Album)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 Artist (com.github.hakko.musiccabinet.domain.model.music.Artist)2 AlbumInfoParserImpl (com.github.hakko.musiccabinet.parser.lastfm.AlbumInfoParserImpl)2 ResourceUtil (com.github.hakko.musiccabinet.util.ResourceUtil)2 DataAccessException (org.springframework.dao.DataAccessException)2 File (com.github.hakko.musiccabinet.domain.model.library.File)1 ApplicationException (com.github.hakko.musiccabinet.exception.ApplicationException)1 AlbumInfoParser (com.github.hakko.musiccabinet.parser.lastfm.AlbumInfoParser)1 StringUtil (com.github.hakko.musiccabinet.util.StringUtil)1 UnittestLibraryUtil.getFile (com.github.hakko.musiccabinet.util.UnittestLibraryUtil.getFile)1 WSResponse (com.github.hakko.musiccabinet.ws.lastfm.WSResponse)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Before (org.junit.Before)1 RowCallbackHandler (org.springframework.jdbc.core.RowCallbackHandler)1