package com.technicolor.publisher;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.pubsub.AccessModel;
import org.jivesoftware.smackx.pubsub.ConfigureForm;
import org.jivesoftware.smackx.pubsub.FormType;
import org.jivesoftware.smackx.pubsub.Item;
import org.jivesoftware.smackx.pubsub.LeafNode;
import org.jivesoftware.smackx.pubsub.PayloadItem;
import org.jivesoftware.smackx.pubsub.PubSubManager;
import org.jivesoftware.smackx.pubsub.PublishModel;
import org.jivesoftware.smackx.pubsub.SimplePayload;

public class Publisher {

    XMPPConnection connection;
    PubSubManager pubSubManager;

    public Publisher() {
        ConnectionConfiguration config = new ConnectionConfiguration("cplx129.edegem.eu.thmulti.com");
        connection = new XMPPConnection(config);
        pubSubManager = new PubSubManager(connection);


    }

    protected void login(String userName, String password) throws XMPPException {
        
        connection.connect();
        
        System.out.println("Is it connected? " + connection.isConnected());
        System.out.println("Is it authenticated? " + connection.isAuthenticated());
        connection.login(userName, password);
        System.out.println("Is it authenticated? " + connection.isAuthenticated());
    }

    public void publish() throws XMPPException {

        SimplePayload payload = new SimplePayload("Gladiator", "Peliculas", "Es una pelicula sobre un gladiador");

        PayloadItem<SimplePayload> payloadItem = new PayloadItem<SimplePayload>(null, payload);
        ((LeafNode) pubSubManager.getNode("Musica")).publish(payloadItem);

    }

    public void addNode(String nodeName) throws XMPPException {
        if (!existNode(nodeName)) {
            ConfigureForm form = new ConfigureForm(FormType.submit);
            form.setPersistentItems(true);
            form.setDeliverPayloads(true);
            form.setNotifyDelete(true);
            form.setNotifyRetract(true);
            form.setAccessModel(AccessModel.open);
            form.setPublishModel(PublishModel.open);
            form.setSubscribe(true);
            pubSubManager.createNode(nodeName);
            LeafNode n = (LeafNode) pubSubManager.createNode("Trabalenguas", form);
        }
    }

    public boolean existNode(String nodeName) {
        try {
            pubSubManager.getNode(nodeName);
            return true;
        } catch (XMPPException ex) {
            System.out.println("El nodo" + nodeName + " no existe");
            return false;
        }
    }

    public void logout() {
        connection.disconnect();
    }
}
@@ -1499,24 +1499,26 @@
 xception %7B%0A%0A
+//
         Simp
@@ -1620,16 +1620,180 @@
 ador%22);%0A
+//%0A        %0A         SimplePayload payload = new SimplePayload(%22book%22,%22pubsub:test:book%22, %22%3Cbook xmlns='pubsub:test:book'%3E%3Ctitle%3ELord of the Rings%3C/title%3E%3C/book%3E%22);
 %0A       
@@ -1889,16 +1889,17 @@
         
+ 
 ((LeafNo
@@ -1956,16 +1956,268 @@
 adItem);
+%0A        %0A        %0A//        Item item = new Item(%22Kill Bill%22);%0A//        ((LeafNode)pubSubManager.getNode(%22Peliculas%22)).publish(item);%0A        %0A             %0A       System.out.println(((LeafNode) pubSubManager.getNode(%22Peliculas%22)).getItems().size());
 %0A%0A    %7D%0A
@@ -2422,19 +2422,20 @@
 ntItems(
-tru
+fals
 e);%0A    

Check sample publishing