/*
* Jampa
* Copyright (C) 2008-2009 J. Devauchelle and contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
package org.jampa.model.library;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jampa.controllers.Controller;
import org.jampa.logging.Log;
import org.jampa.model.SortMode;
import org.jampa.model.comparators.LibraryItemComparator;
import org.jampa.preferences.PreferenceConstants;
import org.jampa.utils.Constants;
public class Library extends AbstractLibraryItem {
private Map<String, ILibraryItem> _listByAlbum;
private Map<String, ILibraryItem> _listByGenre;
private List<ILibraryItem> _titleList;
private LibraryMode _libraryMode;
public Library() {
super(null, ""); //$NON-NLS-1$
_libraryMode = LibraryMode.getValueOf(Controller.getInstance().getPreferenceStore().getString(PreferenceConstants.LIBRARY_DISPLAY_MODE));
_librarySortMode = SortMode.getValueOf(Controller.getInstance().getPreferenceStore().getString(PreferenceConstants.LIBRARY_SORT_MODE));
_listByAlbum = new Hashtable<String, ILibraryItem>();
_listByGenre = new Hashtable<String, ILibraryItem>();
_titleList = new ArrayList<ILibraryItem>();
loadDataFromResultSet();
}
private void loadDataFromResultSet() {
String artist;
String album;
String title;
String trackNumber;
String path;
String genre;
int totalTime = 0;
ILibraryItem genreItem;
ILibraryItem artistItem;
ILibraryItem albumItem;
ILibraryItem titleItem;
Log.getInstance(Library.class).debug("Library construction started."); //$NON-NLS-1$
ResultSet results = Controller.getInstance().getHSQLController().getLibraryData();
Log.getInstance(Library.class).debug("Library construction: Data fetched from database."); //$NON-NLS-1$
try {
while (results.next()) {
artist = results.getString("ARTIST"); //$NON-NLS-1$
album = results.getString("ALBUM"); //$NON-NLS-1$
title = results.getString("TITLE"); //$NON-NLS-1$
trackNumber = results.getString("TRACKNUMBER"); //$NON-NLS-1$
path = results.getString("PATH"); //$NON-NLS-1$
genre = results.getString("GENRE"); //$NON-NLS-1$
totalTime = totalTime + results.getInt("TRACKLENGTH"); //$NON-NLS-1$
if (artist.isEmpty())
artist = Constants.UNKNOWN_STRING;
if (album.isEmpty())
album = Constants.UNKNOWN_STRING;
if (title.isEmpty())
title = Constants.UNKNOWN_STRING;
if (genre.isEmpty())
genre = Constants.UNKNOWN_STRING;
// Fill default list: by artist/album/title.
if (_list.containsKey(artist)) {
artistItem = _list.get(artist);
} else {
artistItem = new ArtistItem(null, artist);
_list.put(artist, artistItem);
}
if (artistItem.getList().containsKey(album)) {
albumItem = artistItem.getList().get(album);
} else {
albumItem = new AlbumItem(artistItem, album);
artistItem.getList().put(album, albumItem);
}
if (!albumItem.getList().containsKey(path)) {
titleItem = new TitleItem(albumItem, title, genre, artist, trackNumber, path);
albumItem.getList().put(path, titleItem);
}
// Fill second list: by album/artist/title.
if (_listByAlbum.containsKey(album)) {
albumItem = _listByAlbum.get(album);
} else {
albumItem = new AlbumItem(null, album);
_listByAlbum.put(album, albumItem);
}
if (albumItem.getList().containsKey(artist)) {
artistItem = albumItem.getList().get(artist);
} else {
artistItem = new ArtistItem(albumItem, artist);
albumItem.getList().put(artist, artistItem);
}
if (!artistItem.getList().containsKey(path)) {
titleItem = new TitleItem(artistItem, title, genre, artist, trackNumber, path);
artistItem.getList().put(path, titleItem);
}
// Fill third list: list by genre/artist/album/title.
if (_listByGenre.containsKey(genre)) {
genreItem = _listByGenre.get(genre);
} else {
genreItem = new GenreItem(null, genre);
_listByGenre.put(genre, genreItem);
}
if (genreItem.getList().containsKey(artist)) {
artistItem = genreItem.getList().get(artist);
} else {
artistItem = new ArtistItem(genreItem, artist);
genreItem.getList().put(artist, artistItem);
}
if (artistItem.getList().containsKey(album)) {
albumItem = artistItem.getList().get(album);
} else {
albumItem = new AlbumItem(artistItem, album);
artistItem.getList().put(album, albumItem);
}
if (!albumItem.getList().containsKey(path)) {
titleItem = new TitleItem(albumItem, title, genre, artist, trackNumber, path);
albumItem.getList().put(path, titleItem);
}
titleItem = new TitleItem(albumItem, title, genre, artist, trackNumber, path);
_titleList.add(titleItem);
}
Controller.getInstance().getStatisticsController().setLibraryTotalTime(totalTime);
Log.getInstance(Library.class).debug("End of library construction."); //$NON-NLS-1$
} catch (SQLException e) {
Log.getInstance(Library.class).error(e.getMessage());
}
}
// Override the one in AbstractLibraryItem.
public List<ILibraryItem> getChildrenList() {
ArrayList<ILibraryItem> tmpList = new ArrayList<ILibraryItem>();
Map<String, ILibraryItem> currentList;
switch (_libraryMode) {
case ArtistMode:
currentList = _list;
break;
case AlbumMode:
currentList = _listByAlbum;
break;
case GenreMode:
currentList = _listByGenre;
break;
default:
currentList = _list;
}
Set<String> keys = currentList.keySet();
Iterator<String> iter = keys.iterator();
ILibraryItem item;
while (iter.hasNext()) {
item = currentList.get(iter.next());
tmpList.add(item);
}
Collections.sort(tmpList, new LibraryItemComparator(_librarySortMode));
return tmpList;
}
public LibraryMode getLibraryMode() {
return _libraryMode;
}
public void setLibraryMode(LibraryMode mode) {
_libraryMode = mode;
Controller.getInstance().getPreferenceStore().setValue(PreferenceConstants.LIBRARY_DISPLAY_MODE, _libraryMode.getValue());
}
public void setLibrarySortMode(SortMode mode) {
super.setLibrarySortMode(mode);
Controller.getInstance().getPreferenceStore().setValue(PreferenceConstants.LIBRARY_SORT_MODE, _librarySortMode.getValue());
}
private List<String> getKeyList(Map<String, ILibraryItem> itemsList) {
List<String> tmpList = new ArrayList<String>();
Iterator<String> iter = itemsList.keySet().iterator();
while (iter.hasNext()) {
tmpList.add(iter.next());
}
Collections.sort(tmpList);
return tmpList;
}
public List<String> getArtistList() {
return getKeyList(_list);
}
public List<String> getAlbumList() {
return getKeyList(_listByAlbum);
}
public List<String> getGenreList() {
return getKeyList(_listByGenre);
}
public List<ILibraryItem> getTitleList() {
return _titleList;
}
}