chat/client/src/main/java/org/apidesign/demo/chat/ChatClient.java
branchNewChat
changeset 238 a0f15cb8c730
child 239 f57278ddd70e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/chat/client/src/main/java/org/apidesign/demo/chat/ChatClient.java	Fri Apr 22 05:56:47 2016 +0200
     1.3 @@ -0,0 +1,107 @@
     1.4 +/**
     1.5 + * The MIT License (MIT)
     1.6 + *
     1.7 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.8 + *
     1.9 + * Permission is hereby granted, free of charge, to any person obtaining a copy
    1.10 + * of this software and associated documentation files (the "Software"), to deal
    1.11 + * in the Software without restriction, including without limitation the rights
    1.12 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    1.13 + * copies of the Software, and to permit persons to whom the Software is
    1.14 + * furnished to do so, subject to the following conditions:
    1.15 + *
    1.16 + * The above copyright notice and this permission notice shall be included in
    1.17 + * all copies or substantial portions of the Software.
    1.18 + *
    1.19 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    1.20 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    1.21 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    1.22 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    1.23 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    1.24 + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    1.25 + * THE SOFTWARE.
    1.26 + */
    1.27 +package org.apidesign.demo.chat;
    1.28 +
    1.29 +import net.java.html.json.ComputedProperty;
    1.30 +import net.java.html.json.Function;
    1.31 +import net.java.html.json.Model;
    1.32 +import net.java.html.json.OnReceive;
    1.33 +import net.java.html.json.Property;
    1.34 +import org.apidesign.demo.chat.shared.Message;
    1.35 +import org.apidesign.demo.chat.shared.Query;
    1.36 +
    1.37 +/**
    1.38 + *
    1.39 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    1.40 + */
    1.41 +@Model(className = "ChatModel", targetId = "", properties = {
    1.42 +    @Property(name = "user", type = String.class),
    1.43 +    @Property(name = "comment", type = String.class),
    1.44 +    @Property(name = "url", type = String.class),
    1.45 +    @Property(name = "msgs", type = Message.class, array = true)
    1.46 +})
    1.47 +class ChatClient {
    1.48 +    @ComputedProperty
    1.49 +    static boolean sendEnabled(String user, String comment) {
    1.50 +        boolean res = user != null && comment != null && !user.isEmpty() && !comment.isEmpty();
    1.51 +        return res;
    1.52 +    }
    1.53 +    
    1.54 +    @Function
    1.55 +    static void submit(ChatModel m) {
    1.56 +        if (!sendEnabled(m.getUser(), m.getComment())) {
    1.57 +            return;
    1.58 +        }
    1.59 +        Message msg = new Message();
    1.60 +        msg.setUser(m.getUser());
    1.61 +        msg.setComment(m.getComment());
    1.62 +        m.postComment(m.getUrl(), msg);
    1.63 +    }
    1.64 +    
    1.65 +    static void cantChat(ChatModel model, Exception err) {
    1.66 +        model.getMsgs().add(new Message("System", err.getMessage(), 0));
    1.67 +    }
    1.68 +    
    1.69 +    
    1.70 +    @OnReceive(url = "{url}/chat", method = "POST", data = Message.class, onError = "cantChat")
    1.71 +    static void postComment(ChatModel m, Message addedMessage) {
    1.72 +        if (addedMessage.getComment().equals(m.getComment())) {
    1.73 +            m.setComment("");
    1.74 +        }
    1.75 +    }
    1.76 +    
    1.77 +    @OnReceive(url = "{url}/chat?since=0")
    1.78 +    static void initialRead(ChatModel m, Query q) {
    1.79 +        m.getMsgs().clear();
    1.80 +        m.getMsgs().addAll(q.getMessages());
    1.81 +        moreMessages(m);
    1.82 +    }
    1.83 +    
    1.84 +    @OnReceive(url = "{url}/chat?since={since}")
    1.85 +    static void updateMsgs(ChatModel m, Query q) {
    1.86 +        m.getMsgs().addAll(q.getMessages());
    1.87 +        moreMessages(m);
    1.88 +    }
    1.89 +    
    1.90 +    private static void moreMessages(ChatModel m) {
    1.91 +        long now = 0;
    1.92 +        for (Message msg : m.getMsgs()) {
    1.93 +            if (now < msg.getSince()) {
    1.94 +                now = msg.getSince();
    1.95 +            }
    1.96 +        }
    1.97 +        m.updateMsgs(m.getUrl(), "" + (now + 1));
    1.98 +    }
    1.99 +    
   1.100 +    public static void onPageLoad() {
   1.101 +        ChatModel chm = new ChatModel();
   1.102 +        chm.setUrl("http://localhost:8080");
   1.103 +        Message m = new Message();
   1.104 +        m.setComment("Waiting for messages from the server...");
   1.105 +        m.setUser("system");
   1.106 +        chm.getMsgs().add(m);
   1.107 +        chm.applyBindings();
   1.108 +        chm.initialRead(chm.getUrl());        
   1.109 +    }
   1.110 +}