chat/client/src/main/java/org/apidesign/html/chatserver/client/ChatClient.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 09 May 2013 22:41:43 +0200
changeset 15 91219d000cb8
parent 13 fd9a16bbfd0e
child 57 9984b9f7d8c6
permissions -rw-r--r--
Uses POST method to send new messages to the server
jtulach@5
     1
/**
jtulach@5
     2
 * The MIT License (MIT)
jtulach@5
     3
 *
jtulach@5
     4
 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@5
     5
 *
jtulach@5
     6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
jtulach@5
     7
 * of this software and associated documentation files (the "Software"), to deal
jtulach@5
     8
 * in the Software without restriction, including without limitation the rights
jtulach@5
     9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jtulach@5
    10
 * copies of the Software, and to permit persons to whom the Software is
jtulach@5
    11
 * furnished to do so, subject to the following conditions:
jtulach@5
    12
 *
jtulach@5
    13
 * The above copyright notice and this permission notice shall be included in
jtulach@5
    14
 * all copies or substantial portions of the Software.
jtulach@5
    15
 *
jtulach@5
    16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jtulach@5
    17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jtulach@5
    18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jtulach@5
    19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jtulach@5
    20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jtulach@5
    21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jtulach@5
    22
 * THE SOFTWARE.
jtulach@5
    23
 */
jtulach@13
    24
package org.apidesign.html.chatserver.client;
jtulach@5
    25
jtulach@5
    26
import net.java.html.json.ComputedProperty;
jtulach@5
    27
import net.java.html.json.Context;
jtulach@5
    28
import net.java.html.json.Function;
jtulach@5
    29
import net.java.html.json.Model;
jtulach@9
    30
import net.java.html.json.OnReceive;
jtulach@5
    31
import net.java.html.json.Property;
jtulach@13
    32
import org.apidesign.html.chatserver.model.Message;
jtulach@13
    33
import org.apidesign.html.chatserver.model.Query;
jtulach@5
    34
jtulach@5
    35
/**
jtulach@5
    36
 *
jtulach@5
    37
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@5
    38
 */
jtulach@5
    39
@Model(className = "ChatModel", properties = {
jtulach@5
    40
    @Property(name = "user", type = String.class),
jtulach@11
    41
    @Property(name = "comment", type = String.class),
jtulach@5
    42
    @Property(name = "msgs", type = Message.class, array = true)
jtulach@5
    43
})
jtulach@5
    44
class ChatClient {
jtulach@5
    45
    @ComputedProperty
jtulach@11
    46
    static boolean sendEnabled(String user, String comment) {
jtulach@11
    47
        boolean res = user != null && comment != null && !user.isEmpty() && !comment.isEmpty();
jtulach@5
    48
        return res;
jtulach@5
    49
    }
jtulach@5
    50
    
jtulach@5
    51
    @Function
jtulach@5
    52
    static void submit(ChatModel m) {
jtulach@11
    53
        if (!sendEnabled(m.getUser(), m.getComment())) {
jtulach@5
    54
            return;
jtulach@5
    55
        }
jtulach@15
    56
        Message msg = new Message(CNTX);
jtulach@15
    57
        msg.setUser(m.getUser());
jtulach@15
    58
        msg.setComment(m.getComment());
jtulach@15
    59
        m.postComment(msg);
jtulach@11
    60
    }
jtulach@11
    61
    
jtulach@15
    62
    @OnReceive(url = "/chat", method = "POST", data = Message.class)
jtulach@11
    63
    static void postComment(ChatModel m, Message addedMessage) {
jtulach@11
    64
        if (addedMessage.getComment().equals(m.getComment())) {
jtulach@11
    65
            m.setComment("");
jtulach@11
    66
        }
jtulach@5
    67
    }
jtulach@5
    68
    
jtulach@9
    69
    @OnReceive(url = "/chat?since=0")
jtulach@9
    70
    static void initialRead(ChatModel m, Query q) {
jtulach@9
    71
        m.getMsgs().clear();
jtulach@9
    72
        m.getMsgs().addAll(q.getMessages());
jtulach@11
    73
        moreMessages(m);
jtulach@11
    74
    }
jtulach@11
    75
    
jtulach@11
    76
    @OnReceive(url = "/chat?since={since}")
jtulach@11
    77
    static void updateMsgs(ChatModel m, Query q) {
jtulach@11
    78
        m.getMsgs().addAll(q.getMessages());
jtulach@11
    79
        moreMessages(m);
jtulach@11
    80
    }
jtulach@11
    81
    
jtulach@11
    82
    private static void moreMessages(ChatModel m) {
jtulach@13
    83
        long now = 0;
jtulach@13
    84
        for (Message msg : m.getMsgs()) {
jtulach@13
    85
            if (now < msg.getSince()) {
jtulach@13
    86
                now = msg.getSince();
jtulach@13
    87
            }
jtulach@13
    88
        }
jtulach@13
    89
        m.updateMsgs("" + (now + 1));
jtulach@9
    90
    }
jtulach@9
    91
    
jtulach@5
    92
    static final Context CNTX = Context.findDefault(ChatClient.class);
jtulach@5
    93
    static {
jtulach@5
    94
        ChatModel chm = new ChatModel(CNTX);
jtulach@5
    95
        Message m = new Message(CNTX);
jtulach@9
    96
        m.setComment("Waiting for messages from the server...");
jtulach@9
    97
        m.setUser("system");
jtulach@5
    98
        chm.getMsgs().add(m);
jtulach@5
    99
        chm.applyBindings();
jtulach@11
   100
        try {
jtulach@11
   101
            // XXX: this should not be in static initializer -
jtulach@11
   102
            // XXX: prevents unit testing!
jtulach@11
   103
            chm.initialRead();
jtulach@11
   104
        } catch (Throwable ex) {
jtulach@11
   105
        }
jtulach@5
   106
    }
jtulach@5
   107
}