package Sercultur.lists;
import Sercultur.classes.Cinema;
import Sercultur.classes.Filme;
import XmlUtils.XmlUtil;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class FilmesList {
//Lista de Exposicoes
public static ArrayList<Filme> getAll(){
Document doc = getServiceDoc("", "");
return getListFromDoc(doc);
}
//Lista de Exposicoes By Distrito
public static ArrayList<Filme> getByDistrito(String numeroDistrito){
Document doc = getServiceDoc(numeroDistrito, "");
return getListFromDoc(doc);
}
//Lista de Exposicoes By Concelho
public static ArrayList<Filme> getByConcelho(String numeroConcelho){
Document doc = getServiceDoc("", numeroConcelho);
return getListFromDoc(doc);
}
//Lista de Exposicoes
public static ArrayList<Filme> getListFromDoc(Document doc){
LinkedHashSet<Integer> idsEstreias = getEstreiasId();
ArrayList<Filme> list = new ArrayList<Filme>();
//cada exposicao
Element cinema = (Element)doc.getElementsByTagName("Cinema").item(0);
NodeList rows = cinema.getElementsByTagName("Row");
Node firstRowNode = rows.item(0);
for(int i = 0; i < rows.getLength(); i++ ){
Element e = (Element)rows.item(i);
if( getTagText(e, "Filme") != null ){
//Get cinemas para este filme
LinkedHashSet<Cinema> listCinemas = new LinkedHashSet<Cinema>();
if( e.getElementsByTagName("EmExibicao").getLength() > 0 ){
Element emExibicao = (Element) e.getElementsByTagName("EmExibicao").item(0);
NodeList rowsExibicao = emExibicao.getElementsByTagName("Row");
for( int j = 0; j < rowsExibicao.getLength(); j++ ){
Element c = (Element)rowsExibicao.item(j);
listCinemas.add(new Cinema(
getTagText(c, "Cinema"),
getTagText(c, "Morada"),
getTagText(c, "NumeroConcelho"),
getTagText(c, "NumeroDistrito"),
getTagText(c, "Horario"),
getTagText(c, "PrecoEntrada"),
getTagText(c, "WebSite"),
getTagText(c, "DataInicio"),
getTagText(c, "DataFim")
));
}
}
//Check if estreia
boolean isEstreia = false;
String t = getTagText(e, "NumeroFilme");
if( !t.isEmpty() ){
if( idsEstreias.contains( Integer.parseInt( t ) ) ){
isEstreia = true;
}
}
//Add filme to list
list.add(new Filme(
getTagText(e, "Filme"),
getTagText(e, "TituloOriginal"),
getTagText(e, "Genero"),
getTagText(e, "Pais"),
getTagText(e, "Ano"),
getTagText(e, "Imagem"),
getTagText(e, "ImagemPequena"),
getTagText(e, "Sumario"),
getTagText(e, "Descricao"),
getTagText(e, "Duracao"),
getTagText(e, "Website"),
isEstreia,
listCinemas
));
}
}
Collections.sort(list);
return list;
}
private static String getTagText(Element parentElement, String tagName){
if( parentElement.getElementsByTagName(tagName) != null ){
Element e = (Element) parentElement.getElementsByTagName(tagName).item(0);
if( e != null ){
return e.getTextContent();
}
return null;
}
return null;
}
//Ids das estreias (exposições)
private static LinkedHashSet<Integer> getEstreiasId(){
LinkedHashSet<Integer> ids = new LinkedHashSet<Integer>();
SerculturServices.Sws service = new SerculturServices.Sws();
QName portQName = new QName("http://ws.sercultur.pt/", "SwsSoap12");
String req = "<EstreiasCinema xmlns=\"http://ws.sercultur.pt/\"><Semana></Semana><NumeroSemanas></NumeroSemanas></EstreiasCinema>";
try { // Call Web Service Operation
Dispatch<Source> sourceDispatch = null;
sourceDispatch = service.createDispatch(portQName, Source.class, Service.Mode.PAYLOAD);
Source result = sourceDispatch.invoke(new StreamSource(new StringReader(req)));
Document doc = XmlUtil.getDocFromSource(result);
NodeList list = doc.getElementsByTagName("Numero");
for( int i = 0; i < list.getLength(); i++ ){
ids.add( Integer.parseInt( list.item(i).getTextContent() ) );
}
} catch (Exception ex) {
System.out.println("Error:"+ex.getMessage());
}
return ids;
}
private static Document getServiceDoc(String numeroDistrito, String numeroConcelho){
SerculturServices.Sws service = new SerculturServices.Sws();
QName portQName = new QName("http://ws.sercultur.pt/", "SwsSoap12");
String req = "<XMLCartazCinema xmlns=\"http://ws.sercultur.pt/\"><Pais></Pais><Genero></Genero><Distrito>"+numeroDistrito+"</Distrito><Concelho>"+numeroConcelho+"</Concelho><DiasProgramacao></DiasProgramacao><Destaques></Destaques><Opcionais></Opcionais><EspacoAcademico></EspacoAcademico></XMLCartazCinema>";
try { // Call Web Service Operation
Dispatch<Source> sourceDispatch = null;
sourceDispatch = service.createDispatch(portQName, Source.class, Service.Mode.PAYLOAD);
Source result = sourceDispatch.invoke(new StreamSource(new StringReader(req)));
return XmlUtil.getDocFromSource(result);
} catch (Exception ex) {
System.out.println("Error:"+ex.getMessage());
}
return null;
}
}