Package Sercultur.lists

Source Code of Sercultur.lists.TeatroList

package Sercultur.lists;

import Sercultur.classes.Teatro;
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.NodeList;


public class TeatroList {
   
    //Lista de Teatro
    public static ArrayList<Teatro> getAll(){
        Document doc = getServiceDoc(new String(), new String());
        return getListFromDoc(doc);
    }
   
    //Lista de Teatro By Distrito
    public static ArrayList<Teatro> getByDistrito(String numeroDistrito){
        Document doc = getServiceDoc(numeroDistrito, "" );
        return getListFromDoc(doc);
    }
   
    //Lista de Teatro By Concelho
    public static ArrayList<Teatro> getByConcelho(String numeroConcelho){
        Document doc = getServiceDoc("", numeroConcelho);
        return getListFromDoc(doc);
    }
   
    //Lista de Teatro from Doc
    public static ArrayList<Teatro> getListFromDoc(Document doc){
       
        LinkedHashSet<Integer> idsEstreias = getEstreiasId();
        ArrayList<Teatro> list = new ArrayList<Teatro>();
        
        //cada exposicao
        NodeList rows = doc.getElementsByTagName("Row");
       
        for(int i = 0; i < rows.getLength(); i++ ){
           
            Element e = (Element)rows.item(i);
           
            Element tema = (Element) e.getElementsByTagName("TemaEvento").item(0);
            Element titulo = (Element) e.getElementsByTagName("Evento").item(0);
            Element imagemPequena = (Element) e.getElementsByTagName("ImagemPequena").item(0);
            Element descricao = (Element) e.getElementsByTagName("Descricao").item(0);
            Element numeroConcelho = (Element) e.getElementsByTagName("NumeroConcelho").item(0);
            Element numeroDistrito = (Element) e.getElementsByTagName("NumeroDistrito").item(0);
            Element horario = (Element) e.getElementsByTagName("Horario").item(0);
            Element precoEntrada = (Element) e.getElementsByTagName("PrecoEntrada").item(0);
            Element local = (Element) e.getElementsByTagName("Local").item(0);
            Element websiteLocal = (Element) e.getElementsByTagName("WebSiteLocal").item(0);
            Element emailLocal = (Element) e.getElementsByTagName("EmailLocal").item(0);
            Element minData = (Element) e.getElementsByTagName("MinData").item(0);
            Element maxData = (Element) e.getElementsByTagName("MaxData").item(0);
            Element numero = (Element) e.getElementsByTagName("Numero").item(0);
           
            list.add(new Teatro(
                        tema.getTextContent(),
                        titulo.getTextContent(),
                        imagemPequena.getTextContent(),
                        descricao.getTextContent(),
                        numeroConcelho.getTextContent(),
                        numeroDistrito.getTextContent(),
                        horario.getTextContent(),
                        precoEntrada.getTextContent(),
                        local.getTextContent(),
                        websiteLocal.getTextContent(),
                        emailLocal.getTextContent(),
                        minData.getTextContent(),
                        maxData.getTextContent(),
                        idsEstreias.contains( Integer.parseInt( numero.getTextContent() ) ) ? true : false
                    ));
           
        }
       
        Collections.sort(list);
       
        return list;
       
    }
   
    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 = "<EstreiasTeatro  xmlns=\"http://ws.sercultur.pt/\"><Concelho></Concelho></EstreiasTeatro>";

        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 = "<Agenda  xmlns=\"http://ws.sercultur.pt/\"><Area></Area><Tipo>"+Integer.toString(5)+"</Tipo><Tema></Tema><CicloFestival></CicloFestival><Distrito>"+numeroDistrito+"</Distrito><Concelho>"+numeroConcelho+"</Concelho><DiasProgramacao></DiasProgramacao><Destaques></Destaques><Opcionais></Opcionais><EspacoAcademico></EspacoAcademico><Internacional></Internacional><Local></Local><Evento></Evento></Agenda>";

        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;
       
    }
   
}
TOP

Related Classes of Sercultur.lists.TeatroList

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.