package controller;
import java.util.Collections;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import model.array.SearchableObjectArray;
import model.array.tools.SearchableComparator;
import model.movie.Actor;
import model.movie.Movie;
import model.ontology.OntologyWriter;
import model.query.OntologyQueryer;
import view.Window;
import view.panels.SearchResultView;
import view.textfield.interfaces.FullSearchListener;
import view.textfield.interfaces.QuickSearchListener;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
public class MainController implements FullSearchListener, QuickSearchListener {
public static final boolean DEBUG = false;
private static MainController instance;
private Window window;
private UpdateController queryController;
private MainController() {
System.err.close();
OntologyWriter.ensureOntologyExistance(false);
queryController = new UpdateController();
window = new Window();
window.getSearchBar().addFullSearchListener(this);
window.getSearchBar().addQuickSearchListener(this);
}
public Window getWindow() {
return window;
}
public UpdateController getQueryController() {
return queryController;
}
@Override
public void performingQuickSearch(String searchText) {
// System.out.println("quicksearch");
}
@Override
public void performingFullSearch(String searchText) {
SearchableObjectArray results = new SearchableObjectArray();
try {
String queryMovieText = "SELECT DISTINCT ?uri ?title WHERE {" +
"?uri rdf:type h2mdb:Film . " +
"?uri h2mdb:name ?title . " +
"FILTER (REGEX(STR(?title), \"" + searchText + "\", \"i\")) " +
"}";
ResultSet resultSetMovies = OntologyQueryer.resultQuery(queryMovieText);
while (resultSetMovies.hasNext()) {
QuerySolution item1 = resultSetMovies.next();
results.add(new Movie(item1.get("uri").toString(), item1.get("title").toString().replace("@en", "")));
}
String queryActorText = "SELECT DISTINCT ?uri ?title WHERE { " +
"?uri rdf:type h2mdb:Actor . " +
"?uri h2mdb:name ?title . " +
"FILTER (REGEX(STR(?title), \"" + searchText + "\", \"i\")) " +
"}";
ResultSet resultSetActors = OntologyQueryer.resultQuery(queryActorText);
while (resultSetActors.hasNext()) {
QuerySolution item2 = resultSetActors.next();
results.add(new Actor(item2.get("uri").toString(), item2.get("title").toString().replace("@en", "")));
}
Collections.sort(results, new SearchableComparator());
window.getHistory().add(new SearchResultView(window.getPanelCount(), new SearchController(), results));
} catch (NullPointerException e) {
if (MainController.DEBUG) e.printStackTrace();
}
}
public static MainController getInstance() {
if (instance == null)
instance = new MainController();
return instance;
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
if (DEBUG) e.printStackTrace();
} catch (InstantiationException e) {
if (DEBUG) e.printStackTrace();
} catch (IllegalAccessException e) {
if (DEBUG) e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
if (DEBUG) e.printStackTrace();
}
getInstance();
}
}