/*
* 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.gui.runnables;
import java.lang.reflect.InvocationTargetException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.jampa.controllers.Controller;
import org.jampa.gui.translations.Messages;
import org.jampa.logging.Log;
import org.jampa.model.playlists.AudioItem;
public class SearchRunnable implements IRunnableWithProgress {
private final String queryPattern = "SELECT PATH FROM LIBRARY WHERE "; //$NON-NLS-1$
private boolean _boResult = false;
private boolean _boCaseSensitive;
private boolean _boWholeWord;
private List<AudioItem> _results = null;
private String _errorMessage = null;
private String _searchTerm;
private boolean _boAdvancedMode;
private boolean _boSearchInTitle;
private boolean _boSearchInArtists;
private boolean _boSearchInAlbums;
private boolean _boSearchInGenres;
private boolean _boSearchInYears;
public SearchRunnable(String searchTerm) {
_searchTerm = searchTerm;
_boAdvancedMode = true;
}
public SearchRunnable(String searchTerm, boolean caseSensitive, boolean wholeWord, boolean searchInTitle, boolean searchInArtists, boolean searchInAlbums, boolean searchInGenres, boolean searchInYears) {
_searchTerm = searchTerm;
_boAdvancedMode = false;
_boCaseSensitive = caseSensitive;
_boWholeWord = wholeWord;
_boSearchInTitle = searchInTitle;
_boSearchInArtists = searchInArtists;
_boSearchInAlbums = searchInAlbums;
_boSearchInGenres = searchInGenres;
_boSearchInYears = searchInYears;
}
private String getModifiedFieldName(String fieldName) {
if (_boCaseSensitive) {
return fieldName;
} else {
return "LCASE(" + fieldName + ")"; //$NON-NLS-1$ //$NON-NLS-2$
}
}
private String getCondition() {
String searchTerm;
if (_boCaseSensitive) {
searchTerm = _searchTerm;
} else {
searchTerm = _searchTerm.toLowerCase();
}
if (_boWholeWord) {
return " = '" + searchTerm.replaceAll("'", "''") + "'"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
} else {
return " LIKE '%" + searchTerm.replaceAll("'", "''") + "%'"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
}
}
private String buildQuery() {
if (_boAdvancedMode) {
return queryPattern + _searchTerm;
} else {
StringBuilder sb = new StringBuilder();
sb.append(queryPattern);
String condition = getCondition();
if (_boSearchInTitle) {
sb.append("(" + getModifiedFieldName("TITLE") + condition + ")" + (_boSearchInArtists || _boSearchInAlbums || _boSearchInGenres || _boSearchInYears ? " OR " : "")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
}
if (_boSearchInArtists) {
sb.append("(" + getModifiedFieldName("ARTIST") + condition + ")" + (_boSearchInAlbums || _boSearchInGenres || _boSearchInYears ? " OR " : "")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
}
if (_boSearchInAlbums) {
sb.append("(" + getModifiedFieldName("ALBUM") + condition + ")" + (_boSearchInGenres || _boSearchInYears ? " OR " : "")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
}
if (_boSearchInGenres) {
sb.append("(" + getModifiedFieldName("GENRE") + condition + ")" + (_boSearchInYears ? " OR " : "")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
}
if (_boSearchInYears) {
sb.append("(" + getModifiedFieldName("YEAR") + condition + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
return sb.toString();
}
}
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
monitor.beginTask(Messages.getString("Controller.SearchLibrary"), 3); //$NON-NLS-1$
monitor.subTask(Messages.getString("Controller.SearchLibraryBuildQuery")); //$NON-NLS-1$
String query = buildQuery();
Log.getInstance(SearchRunnable.class).debug("Search query: " + query); //$NON-NLS-1$
monitor.worked(1);
monitor.subTask(Messages.getString("Controller.SearchLibraryExecuteQuery")); //$NON-NLS-1$
ResultSet rs = Controller.getInstance().getHSQLController().executeSelectionQuery(query);
monitor.worked(1);
monitor.subTask(Messages.getString("Controller.SearchLibraryFetchResults")); //$NON-NLS-1$
if (rs != null) {
_boResult = true;
_results = new ArrayList<AudioItem>();
try {
while (rs.next()) {
_results.add(new AudioItem(rs.getString("PATH"), false)); //$NON-NLS-1$
}
} catch (SQLException e) {
_boResult = false;
_errorMessage = e.getMessage();
Log.getInstance(SearchRunnable.class).error("Error reading query results: " + _errorMessage); //$NON-NLS-1$
}
} else {
_boResult = false;
}
monitor.worked(1);
monitor.done();
}
public boolean getQueryStatus() {
return _boResult;
}
public List<AudioItem> getResults() {
return _results;
}
}