package controller;
import java.util.List;
import javax.swing.SwingWorker;
import model.array.ActorArray;
import model.array.MovieArray;
import model.movie.Actor;
import model.movie.Movie;
import model.ontology.OntologyReader;
import model.ontology.OntologyWriter;
import model.query.DBPediaQueryer;
import model.query.OntologyQueryer;
import view.bar.ProgressBar;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.shared.JenaException;
public class UpdateController implements Runnable {
private Thread thread;
public UpdateController() {
thread = new Thread(this);
thread.start();
}
@Override
public void run() {
try {
Thread.sleep(3000);
MovieArray movieList = getMovieUpdateList();
ActorArray actorList = getActorUpdateList(movieList);
new CreateMovieModel(movieList, actorList).execute();
} catch (Exception e) {
ProgressBar progessBar = MainController.getInstance().getWindow().getProgessBar();
progessBar.addProgressBar(0);
progessBar.setProgress("The connection to DBpedia was not successful. The progam will try to update at next startup.");
}
}
public MovieArray getMovieUpdateList() {
MovieArray movieUpdateList = new MovieArray();
MovieArray localMovieList = OntologyQueryer.getOntologoyMovieList();
String queryDBPediaText = "SELECT DISTINCT ?uri WHERE { " +
"?uri rdf:type yago:NorwegianFilms . " +
"}";
ResultSet resultSet = DBPediaQueryer.resultQuery(queryDBPediaText);
while(resultSet.hasNext()) {
Movie movie = new Movie(resultSet.next().get("uri").toString());
if (!localMovieList.contains(movie))
movieUpdateList.add(movie);
}
return movieUpdateList;
}
public ActorArray getActorUpdateList(MovieArray movieList) {
ActorArray actorUpdateList = new ActorArray();
ActorArray localActorList = OntologyQueryer.getOntologoyActorList();
for (Movie m : movieList) {
String query = "SELECT ?actor WHERE { " +
"<" + m.getUri() + "> " + "dbpedia-owl:starring ?actor . " +
"} ";
ResultSet resultSet = DBPediaQueryer.resultQuery(query);
while(resultSet.hasNext()) {
Actor actor = new Actor(resultSet.next().get("actor").toString());
if (!localActorList.contains(actor))
actorUpdateList.add(actor);
}
}
return actorUpdateList;
}
private class CreateMovieModel extends SwingWorker<String, Integer> {
private static final int PROGRESS = 1;
private MovieArray movieList;
private ActorArray actorList;
private ProgressBar progressBar;
public CreateMovieModel(MovieArray movieList, ActorArray actorList) {
this.movieList = movieList;
this.actorList = actorList;
this.progressBar = MainController.getInstance().getWindow().getProgessBar();
}
@Override
protected String doInBackground() throws Exception {
progressBar.addProgressBar(movieList.size() + actorList.size());
if (!movieList.isEmpty() && !actorList.isEmpty()) {
OntModel model = OntologyReader.read();
if (!movieList.isEmpty())
addMovies(movieList, model);
else progressBar.setProgress("No new Norwegian movies at DBpedia.");
if (!actorList.isEmpty())
addActors(actorList, model);
else progressBar.setProgress("No new actors starring Norwegian movies at DBpedia.");
progressBar.setProgress("Database has been updated.");
publish(movieList.size() + actorList.size() + PROGRESS);
OntologyWriter.write(model, false);
} else {
progressBar.setProgress("No new Norwegian movies or actors in Norwegian movies at DBPedia.");
publish(movieList.size() + actorList.size() + PROGRESS);
}
return null;
}
private void addMovies(MovieArray movieList, OntModel model) {
for (Movie m : movieList) {
try {
model.add(DBPediaQueryer.describeMovie(m));
} catch (JenaException e) {
if (MainController.DEBUG) e.printStackTrace();
} catch (RuntimeException e) {
if (MainController.DEBUG) e.printStackTrace();
}
progressBar.setProgress("Added movie '" + m.getUri() + "' to the database.");
publish(PROGRESS);
}
}
private void addActors(ActorArray actorList, OntModel model) {
for (Actor a : actorList) {
try {
model.add(DBPediaQueryer.describeActor(a));
} catch (JenaException e) {
if (MainController.DEBUG) e.printStackTrace();
} catch (RuntimeException e) {
if (MainController.DEBUG) e.printStackTrace();
}
progressBar.setProgress("Added actor '" + a.getUri() + "' to the database.");
publish(PROGRESS);
}
}
@Override
protected void process(List<Integer> chunks) {
for (Integer percent : chunks)
MainController.getInstance().getWindow().getProgessBar().setPercent(percent);
super.process(chunks);
}
}
}