package view.panels;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import model.array.SearchableObjectArray;
import model.movie.Actor;
import model.movie.SearchableObject;
import model.query.OntologyQueryer;
import view.layout.VerticalLayout;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import controller.MainController;
public class MovieView extends HistoryPanel {
private static final long serialVersionUID = 5259757941496789040L;
public static final double MAX_RUNTIME_BEFORE_CONVERTING_TO_MINUTES = 300;
private JLabel titlePanel, runtimePanel, yearPanel, starringPanel;
private JTextArea abstractText;
private JPanel container;
private JScrollPane scroll, absScroll;
public MovieView(int panelNumber, String uri) {
super(panelNumber, new BorderLayout());
titlePanel = new JLabel("<html><h1>Title: N/A</h1></html>");
abstractText = new JTextArea("Summary: N/A");
abstractText.setEditable(false);
abstractText.setLineWrap(true);
abstractText.setWrapStyleWord(true);
absScroll = new JScrollPane(abstractText);
absScroll.getHorizontalScrollBar().setEnabled(false);
absScroll.setPreferredSize(new Dimension(500, 300));
runtimePanel = new JLabel("<html><h3>Runtime: N/A</h3></html>");
yearPanel = new JLabel("<html><h3>Released: N/A</h3></html>");
starringPanel = new JLabel("<html><h3>Starring: N/A</h3></html>");
container = new JPanel(new VerticalLayout());
container.add(titlePanel);
container.add(absScroll);
container.add(runtimePanel);
container.add(yearPanel);
container.add(starringPanel);
scroll = new JScrollPane(container);
scroll.getHorizontalScrollBar().setEnabled(false);
add(scroll, BorderLayout.CENTER);
ResultSet rs = getMovie(uri);
if (rs.hasNext()) {
QuerySolution result = rs.next();
if (result.contains("title"))
titlePanel.setText(titlePanel.getText().replace("N/A", removeLiteral(result.get("title").toString())));
if (result.contains("abstract"))
abstractText.setText(removeLiteral(result.get("abstract").toString()));
if (result.contains("runtime"))
runtimePanel.setText(runtimePanel.getText().replace("N/A", correctRuntime(Double.parseDouble(removeLiteral(result.get("runtime").toString()))) + " minutes"));
if (result.contains("year"))
yearPanel.setText(yearPanel.getText().replace("N/A", removeLiteral(result.get("year").toString())));
SearchableObjectArray actors = getActors(uri);
if (!actors.isEmpty()) {
String html = "<ul>";
for (SearchableObject actor : actors) {
html += "<li>" + actor.getTitle() + "</li>";
} html += "</ul>";
starringPanel.setText(starringPanel.getText().replace("N/A", html));
}
} else
if(MainController.DEBUG)
System.out.println("MovieView.java QuerySolution is empty, check query. URI " + uri);
MainController.getInstance().getWindow().getHistory().add(this);
}
private ResultSet getMovie(String uri) {
String query = "PREFIX uri: <" + uri + "> " +
"SELECT ?title ?abstract ?runtime ?year " +
"WHERE { " +
"OPTIONAL {uri: h2mdb:name ?title .} " +
"OPTIONAL {uri: h2mdb:summary ?abstract .}" +
"OPTIONAL {uri: h2mdb:release ?year. }." +
"OPTIONAL {uri: h2mdb:duration ?runtime .}" +
"}";
return OntologyQueryer.resultQuery(query);
}
private SearchableObjectArray getActors(String uri) {
String queryStarring = "PREFIX uri: <" + uri + "> " +
"SELECT DISTINCT ?actor ?name " +
"WHERE { " +
"uri: dbpedia-owl:starring ?actor ." +
"?actor h2mdb:name ?name." +
"}";
ResultSet rs2 = OntologyQueryer.resultQuery(queryStarring);
SearchableObjectArray list = new SearchableObjectArray();
while(rs2.hasNext()) {
QuerySolution q = rs2.next();
list.add(new Actor(q.get("actor").toString(), removeLiteral(q.get("name").toString())));
}
return list;
}
/**
* Used to correct min/sec inconsistencies in runtime. If runtime is larger than {@link #MAX_RUNTIME_BEFORE_CONVERTING_TO_MINUTES} the value will be divided by 60.
* @param runtime,
* @return runtime in minutes.
*/
private String correctRuntime(double runtime){
if(runtime>MAX_RUNTIME_BEFORE_CONVERTING_TO_MINUTES)
return String.valueOf(runtime/60);
return String.valueOf(runtime);
}
}