/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Sercultur.lists;
import Sercultur.classes.Concelho;
import XmlUtils.XmlUtil;
import java.io.StringReader;
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;
/**
*
* @author LAP01
*/
public class ConcelhoList {
public static LinkedHashSet<Concelho> getAll(){
Document doc = getServiceDoc("");
return getListFromDoc(doc);
}
public static LinkedHashSet<Concelho> getByNumeroDistrito(String numeroDistrito){
Document doc = getServiceDoc(numeroDistrito);
return getListFromDoc(doc);
}
public static LinkedHashSet<Concelho> getListFromDoc(Document doc){
LinkedHashSet<Concelho> list = new LinkedHashSet<Concelho>();
NodeList rows = doc.getElementsByTagName("Row");
for(int i = 0; i < rows.getLength(); i++ ){
Element e = (Element)rows.item(i);
Element numero = (Element) e.getElementsByTagName("Numero").item(0);
Element concelho = (Element) e.getElementsByTagName("Concelho").item(0);
Element idDistrito = (Element) e.getElementsByTagName("NumeroDistrito").item(0);
list.add(
new Concelho( Integer.parseInt(numero.getTextContent()),
concelho.getTextContent(),
Integer.parseInt( idDistrito.getTextContent()) )
);
}
return list;
}
private static Document getServiceDoc(String numeroDistrito){
SerculturServices.Sws service = new SerculturServices.Sws();
QName portQName = new QName("http://ws.sercultur.pt/", "SwsSoap12");
String req = "<Concelhos xmlns=\"http://ws.sercultur.pt/\"><Distrito>"+numeroDistrito+"</Distrito></Concelhos>";
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(ex.getMessage());
}
return null;
}
}