twitter/src/main/java/org/apidesign/html/demo/twitter/TwitterClient.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 13 Aug 2013 21:11:28 +0200
changeset 44 e3305312184c
parent 16 41a76f55fcc6
permissions -rw-r--r--
Simplified, rewritten to 0.4 and made compilable on JDK8
jtulach@0
     1
/**
jtulach@0
     2
 * The MIT License (MIT)
jtulach@0
     3
 *
jtulach@0
     4
 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@0
     5
 *
jtulach@0
     6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
jtulach@0
     7
 * of this software and associated documentation files (the "Software"), to deal
jtulach@0
     8
 * in the Software without restriction, including without limitation the rights
jtulach@0
     9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jtulach@0
    10
 * copies of the Software, and to permit persons to whom the Software is
jtulach@0
    11
 * furnished to do so, subject to the following conditions:
jtulach@0
    12
 *
jtulach@0
    13
 * The above copyright notice and this permission notice shall be included in
jtulach@0
    14
 * all copies or substantial portions of the Software.
jtulach@0
    15
 *
jtulach@0
    16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jtulach@0
    17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jtulach@0
    18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jtulach@0
    19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jtulach@0
    20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jtulach@0
    21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jtulach@0
    22
 * THE SOFTWARE.
jtulach@0
    23
 */
jtulach@0
    24
package org.apidesign.html.demo.twitter;
jtulach@0
    25
jtulach@0
    26
import java.util.Arrays;
jtulach@0
    27
import java.util.List;
jtulach@0
    28
import net.java.html.json.ComputedProperty;
jtulach@0
    29
import net.java.html.json.Function;
jtulach@0
    30
import net.java.html.json.Model;
jtulach@0
    31
import net.java.html.json.OnPropertyChange;
jtulach@0
    32
import net.java.html.json.OnReceive;
jtulach@0
    33
import net.java.html.json.Property;
jtulach@0
    34
jtulach@0
    35
/** Controller class for access to Twitter.
jtulach@0
    36
 * 
jtulach@0
    37
 * @author Jaroslav Tulach
jtulach@0
    38
 */
jtulach@0
    39
@Model(className="TwitterModel", properties={
jtulach@0
    40
    @Property(name="savedLists", type=Tweeters.class, array = true),
jtulach@0
    41
    @Property(name="activeTweetersName", type=String.class),
jtulach@0
    42
    @Property(name="activeTweeters", type=String.class, array = true),
jtulach@0
    43
    @Property(name="userNameToAdd", type=String.class),
jtulach@16
    44
    @Property(name="currentTweets", type=Tweet.class, array = true),
jtulach@16
    45
    @Property(name="loading", type=boolean.class)
jtulach@0
    46
})
jtulach@44
    47
final class TwitterClient {
jtulach@0
    48
    @Model(className = "Tweeters", properties = {
jtulach@0
    49
        @Property(name="name", type = String.class),
jtulach@0
    50
        @Property(name="userNames", type = String.class, array = true)
jtulach@0
    51
    })
jtulach@0
    52
    static class Twttrs {
jtulach@0
    53
    }
jtulach@0
    54
    @Model(className = "Tweet", properties = {
jtulach@0
    55
        @Property(name = "from_user", type = String.class),
jtulach@0
    56
        @Property(name = "from_user_id", type = int.class),
jtulach@0
    57
        @Property(name = "profile_image_url", type = String.class),
jtulach@0
    58
        @Property(name = "text", type = String.class),
jtulach@0
    59
        @Property(name = "created_at", type = String.class),
jtulach@0
    60
    })
jtulach@0
    61
    static final class Twt {
jtulach@0
    62
        @ComputedProperty static String html(String text) {
jtulach@0
    63
            StringBuilder sb = new StringBuilder(320);
jtulach@0
    64
            for (int pos = 0;;) {
jtulach@0
    65
                int http = text.indexOf("http", pos);
jtulach@0
    66
                if (http == -1) {
jtulach@0
    67
                    sb.append(text.substring(pos));
jtulach@0
    68
                    return sb.toString();
jtulach@0
    69
                }
jtulach@0
    70
                int spc = text.indexOf(' ', http);
jtulach@0
    71
                if (spc == -1) {
jtulach@0
    72
                    spc = text.length();
jtulach@0
    73
                }
jtulach@0
    74
                sb.append(text.substring(pos, http));
jtulach@0
    75
                String url = text.substring(http, spc);
jtulach@0
    76
                sb.append("<a href='").append(url).append("'>").append(url).append("</a>");
jtulach@0
    77
                pos = spc;
jtulach@0
    78
            }
jtulach@0
    79
        }
jtulach@0
    80
        
jtulach@0
    81
        @ComputedProperty static String userUrl(String from_user) {
jtulach@0
    82
            return "http://twitter.com/" + from_user;
jtulach@0
    83
        }
jtulach@0
    84
    }
jtulach@0
    85
    @Model(className = "TwitterQuery", properties = {
jtulach@0
    86
        @Property(array = true, name = "results", type = Twt.class)
jtulach@0
    87
    })
jtulach@0
    88
    public static final class TwttrQr {
jtulach@0
    89
    }
jtulach@0
    90
    
jtulach@0
    91
    @OnReceive(url="{root}/search.json?{query}&callback={me}", jsonp="me")
jtulach@0
    92
    static void queryTweets(TwitterModel page, TwitterQuery q) {
jtulach@0
    93
        page.getCurrentTweets().clear();
jtulach@0
    94
        page.getCurrentTweets().addAll(q.getResults());
jtulach@16
    95
        page.setLoading(false);
jtulach@0
    96
    }
jtulach@0
    97
    
jtulach@0
    98
    @OnPropertyChange("activeTweetersName")
jtulach@0
    99
    static void changeTweetersList(TwitterModel model) {
jtulach@0
   100
        Tweeters people = findByName(model.getSavedLists(), model.getActiveTweetersName());        
jtulach@0
   101
        model.getActiveTweeters().clear();
jtulach@0
   102
        model.getActiveTweeters().addAll(people.getUserNames());
jtulach@0
   103
    }
jtulach@0
   104
    
jtulach@0
   105
    @OnPropertyChange({ "activeTweeters", "activeTweetersCount" })
jtulach@0
   106
    static void refreshTweets(TwitterModel model) {
jtulach@0
   107
        StringBuilder sb = new StringBuilder();
jtulach@0
   108
        sb.append("rpp=25&q=");
jtulach@0
   109
        String sep = "";
jtulach@0
   110
        for (String p : model.getActiveTweeters()) {
jtulach@0
   111
            sb.append(sep);
jtulach@0
   112
            sb.append("from:");
jtulach@0
   113
            sb.append(p);
jtulach@0
   114
            sep = " OR ";
jtulach@0
   115
        }
jtulach@16
   116
        model.setLoading(true);
jtulach@0
   117
        model.queryTweets("http://search.twitter.com", sb.toString());
jtulach@0
   118
    }
jtulach@0
   119
    
jtulach@44
   120
    static void init() {
jtulach@44
   121
        final TwitterModel model = new TwitterModel();
jtulach@0
   122
        final List<Tweeters> svdLst = model.getSavedLists();
jtulach@0
   123
        svdLst.add(newTweeters("API Design", "JaroslavTulach"));
jtulach@0
   124
        svdLst.add(newTweeters("Celebrities", "JohnCleese", "MCHammer", "StephenFry", "algore", "StevenSanderson"));
jtulach@0
   125
        svdLst.add(newTweeters("Microsoft people", "BillGates", "shanselman", "ScottGu"));
jtulach@0
   126
        svdLst.add(newTweeters("NetBeans", "GeertjanW","monacotoni", "NetBeans", "petrjiricka"));
jtulach@0
   127
        svdLst.add(newTweeters("Tech pundits", "Scobleizer", "LeoLaporte", "techcrunch", "BoingBoing", "timoreilly", "codinghorror"));
jtulach@0
   128
jtulach@0
   129
        model.setActiveTweetersName("NetBeans");
jtulach@0
   130
jtulach@0
   131
        model.applyBindings();
jtulach@0
   132
    }
jtulach@0
   133
    
jtulach@0
   134
    @ComputedProperty
jtulach@0
   135
    static boolean hasUnsavedChanges(List<String> activeTweeters, List<Tweeters> savedLists, String activeTweetersName) {
jtulach@0
   136
        Tweeters tw = findByName(savedLists, activeTweetersName);
jtulach@0
   137
        if (activeTweeters == null) {
jtulach@0
   138
            return false;
jtulach@0
   139
        }
jtulach@0
   140
        return !tw.getUserNames().equals(activeTweeters);
jtulach@0
   141
    }
jtulach@0
   142
    
jtulach@0
   143
    @ComputedProperty
jtulach@0
   144
    static int activeTweetersCount(List<String> activeTweeters) {
jtulach@0
   145
        return activeTweeters.size();
jtulach@0
   146
    }
jtulach@0
   147
    
jtulach@0
   148
    @ComputedProperty
jtulach@0
   149
    static boolean userNameToAddIsValid(
jtulach@0
   150
        String userNameToAdd, String activeTweetersName, List<Tweeters> savedLists, List<String> activeTweeters
jtulach@0
   151
    ) {
jtulach@0
   152
        return userNameToAdd != null && 
jtulach@0
   153
            userNameToAdd.matches("[a-zA-Z0-9_]{1,15}") &&
jtulach@0
   154
            !activeTweeters.contains(userNameToAdd);
jtulach@0
   155
    }
jtulach@0
   156
    
jtulach@0
   157
    @Function
jtulach@0
   158
    static void deleteList(TwitterModel model) {
jtulach@0
   159
        final List<Tweeters> sl = model.getSavedLists();
jtulach@0
   160
        sl.remove(findByName(sl, model.getActiveTweetersName()));
jtulach@0
   161
        if (sl.isEmpty()) {
jtulach@44
   162
            final Tweeters t = new Tweeters();
jtulach@0
   163
            t.setName("New");
jtulach@0
   164
            sl.add(t);
jtulach@0
   165
        }
jtulach@0
   166
        model.setActiveTweetersName(sl.get(0).getName());
jtulach@0
   167
    }
jtulach@0
   168
    
jtulach@0
   169
    @Function
jtulach@0
   170
    static void saveChanges(TwitterModel model) {
jtulach@0
   171
        Tweeters t = findByName(model.getSavedLists(), model.getActiveTweetersName());
jtulach@0
   172
        int indx = model.getSavedLists().indexOf(t);
jtulach@0
   173
        if (indx != -1) {
jtulach@0
   174
            t.setName(model.getActiveTweetersName());
jtulach@0
   175
            t.getUserNames().clear();
jtulach@0
   176
            t.getUserNames().addAll(model.getActiveTweeters());
jtulach@0
   177
        }
jtulach@0
   178
    }
jtulach@0
   179
    
jtulach@0
   180
    @Function
jtulach@0
   181
    static void addUser(TwitterModel model) {
jtulach@0
   182
        String n = model.getUserNameToAdd();
jtulach@0
   183
        model.getActiveTweeters().add(n);
jtulach@0
   184
    }
jtulach@0
   185
    @Function
jtulach@0
   186
    static void removeUser(String data, TwitterModel model) {
jtulach@0
   187
        model.getActiveTweeters().remove(data);
jtulach@0
   188
    }
jtulach@0
   189
    
jtulach@0
   190
    private static Tweeters findByName(List<Tweeters> list, String name) {
jtulach@0
   191
        for (Tweeters l : list) {
jtulach@0
   192
            if (l.getName() != null && l.getName().equals(name)) {
jtulach@0
   193
                return l;
jtulach@0
   194
            }
jtulach@0
   195
        }
jtulach@44
   196
        return list.isEmpty() ? new Tweeters() : list.get(0);
jtulach@0
   197
    }
jtulach@0
   198
    
jtulach@0
   199
    private static Tweeters newTweeters(String listName, String... userNames) {
jtulach@44
   200
        Tweeters t = new Tweeters();
jtulach@0
   201
        t.setName(listName);
jtulach@0
   202
        t.getUserNames().addAll(Arrays.asList(userNames));
jtulach@0
   203
        return t;
jtulach@0
   204
    }
jtulach@0
   205
}