// Path: chapter09/src/main/java/com/packtpub/pf/blueprint/chat/Message.java
// public class Message {
//     
//     private String text;
//     private String user;
//     private boolean updateList;
// 
//     public Message() {
//     }
// 
//     public Message(String text) {
//         this.text = text;
//     }
//     
//     public Message(String text, boolean updateList) {
//         this.text = text;
//         this.updateList = updateList;
//     }
// 
//     public Message(String user, String text, boolean updateList) {
//         this.text = text;
//         this.user = user;
//         this.updateList = updateList;
//     }
//     
//     public String getText() {
//         return text;
//     }
// 
//     public Message setText(String text) {
//         this.text = text;
//         return this;
//     }
// 
//     public String getUser() {
//         return user;
//     }
// 
//     public Message setUser(String user) {
//         this.user = user;
//         return this;
//     }
// 
//     public boolean isUpdateList() {
//         return updateList;
//     }
// 
//     public void setUpdateList(boolean updateList) {
//         this.updateList = updateList;
//     }
// }
// 
// Path: chapter09/src/main/java/com/packtpub/pf/blueprint/chat/MessageDecoder.java
// public class MessageDecoder implements Decoder<String,Message> {
// 
//     @Override
//     public Message decode(String s) {
//         String[] userAndMessage = s.split(":");
//         if (userAndMessage.length >= 2) {
//             return new Message().setUser(userAndMessage[0]).setText(userAndMessage[1]);
//         } 
//         else {
//             return new Message(s);
//         }
//     }
// }
// 
// Path: chapter09/src/main/java/com/packtpub/pf/blueprint/chat/MessageEncoder.java
// public final class MessageEncoder implements Encoder<Message, String> {
// 
//     @Override
//     public String encode(Message message) {
//         return new JSONObject(message).toString();
//     }
// 
// }


import com.packtpub.pf.blueprint.chat.Message;
import com.packtpub.pf.blueprint.chat.MessageDecoder;
import com.packtpub.pf.blueprint.chat.MessageEncoder;
import org.primefaces.push.EventBus;
import org.primefaces.push.RemoteEndpoint;
import org.primefaces.push.annotation.OnClose;
import org.primefaces.push.annotation.OnMessage;
import org.primefaces.push.annotation.OnOpen;
import org.primefaces.push.annotation.PathParam;
import org.primefaces.push.annotation.PushEndpoint;
import org.primefaces.push.annotation.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import javax.servlet.ServletContext;

package com.packtpub.pf.blueprint.controller;


/**
 * Created with IntelliJ IDEA.
 * User: Ramkumar Pillai
 * Date: 1/8/14
 * Time: 9:00 AM
 * To change this template use File | Settings | File Templates.
 */
@PushEndpoint("/{room}/{user}")
@Singleton
public class ChatResource {

    private final Logger logger = LoggerFactory.getLogger(ChatResource.class);

    @PathParam("room")
    private String room;

    @PathParam("user")
    private String username;

    @Inject
    private ServletContext ctx;

    @OnOpen
    public void onOpen(RemoteEndpoint r, EventBus eventBus) {
        logger.info("OnOpen {}", r);
        eventBus.publish(room + "/*",
                new Message(String.format("%s has entered the room '%s'",
                        username, room), true));
    }

    @OnClose
    public void onClose(RemoteEndpoint r, EventBus eventBus) {
        ChatUsers users= (ChatUsers) ctx.getAttribute("chatUsers");
        users.remove(username);
        
        eventBus.publish(room + "/*", new Message(String.format("%s has left the room", username), true));
    }

// Path: chapter09/src/main/java/com/packtpub/pf/blueprint/chat/Message.java
// public class Message {
//     
//     private String text;
//     private String user;
//     private boolean updateList;
// 
//     public Message() {
//     }
// 
//     public Message(String text) {
//         this.text = text;
//     }
//     
//     public Message(String text, boolean updateList) {
//         this.text = text;
//         this.updateList = updateList;
//     }
// 
//     public Message(String user, String text, boolean updateList) {
//         this.text = text;
//         this.user = user;
//         this.updateList = updateList;
//     }
//     
//     public String getText() {
//         return text;
//     }
// 
//     public Message setText(String text) {
//         this.text = text;
//         return this;
//     }
// 
//     public String getUser() {
//         return user;
//     }
// 
//     public Message setUser(String user) {
//         this.user = user;
//         return this;
//     }
// 
//     public boolean isUpdateList() {
//         return updateList;
//     }
// 
//     public void setUpdateList(boolean updateList) {
//         this.updateList = updateList;
//     }
// }
// 
// Path: chapter09/src/main/java/com/packtpub/pf/blueprint/chat/MessageDecoder.java
// public class MessageDecoder implements Decoder<String,Message> {
// 
//     @Override
//     public Message decode(String s) {
//         String[] userAndMessage = s.split(":");
//         if (userAndMessage.length >= 2) {
//             return new Message().setUser(userAndMessage[0]).setText(userAndMessage[1]);
//         } 
//         else {
//             return new Message(s);
//         }
//     }
// }
// 
// Path: chapter09/src/main/java/com/packtpub/pf/blueprint/chat/MessageEncoder.java
// public final class MessageEncoder implements Encoder<Message, String> {
// 
//     @Override
//     public String encode(Message message) {
//         return new JSONObject(message).toString();
//     }
// 
// }


// Path: chapter09/src/main/java/com/packtpub/pf/blueprint/controller/ChatResource.java
import com.packtpub.pf.blueprint.chat.Message;
import com.packtpub.pf.blueprint.chat.MessageDecoder;
import com.packtpub.pf.blueprint.chat.MessageEncoder;
import org.primefaces.push.EventBus;
import org.primefaces.push.RemoteEndpoint;
import org.primefaces.push.annotation.OnClose;
import org.primefaces.push.annotation.OnMessage;
import org.primefaces.push.annotation.OnOpen;
import org.primefaces.push.annotation.PathParam;
import org.primefaces.push.annotation.PushEndpoint;
import org.primefaces.push.annotation.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import javax.servlet.ServletContext;


package com.packtpub.pf.blueprint.controller;


/**
 * Created with IntelliJ IDEA.
 * User: Ramkumar Pillai
 * Date: 1/8/14
 * Time: 9:00 AM
 * To change this template use File | Settings | File Templates.
 */
@PushEndpoint("/{room}/{user}")
@Singleton
public class ChatResource {

    private final Logger logger = LoggerFactory.getLogger(ChatResource.class);

    @PathParam("room")
    private String room;

    @PathParam("user")
    private String username;

    @Inject
    private ServletContext ctx;

    @OnOpen
    public void onOpen(RemoteEndpoint r, EventBus eventBus) {
        logger.info("OnOpen {}", r);
        eventBus.publish(room + "/*",
                new Message(String.format("%s has entered the room '%s'",
                        username, room), true));
    }

    @OnClose
    public void onClose(RemoteEndpoint r, EventBus eventBus) {
        ChatUsers users= (ChatUsers) ctx.getAttribute("chatUsers");
        users.remove(username);
        
        eventBus.publish(room + "/*", new Message(String.format("%s has left the room", username), true));
    }

    @OnMessage(decoders = {MessageDecoder.class}, encoders = {MessageEncoder.class})