twitter/src/main/java/org/apidesign/html/demo/twitter/TwitterClient.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 20 Jun 2013 13:18:10 +0200
branchclassloader
changeset 36 7ff8ac49cd8c
parent 16 41a76f55fcc6
permissions -rw-r--r--
Rewriting to be Grizzly independent and also ajusting to Twitter's removal of 1.0 APIs
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@0
    47
public 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@36
    54
    @Model(className = "User", properties = {
jtulach@36
    55
        @Property(name = "name", type = String.class),
jtulach@36
    56
        @Property(name = "profile_image_url", type = String.class),
jtulach@36
    57
        @Property(name = "id", type = int.class)
jtulach@36
    58
    })
jtulach@36
    59
    static class Usr {
jtulach@36
    60
    }
jtulach@0
    61
    @Model(className = "Tweet", properties = {
jtulach@36
    62
        @Property(name = "user", type = User.class),
jtulach@0
    63
        @Property(name = "profile_image_url", type = String.class),
jtulach@0
    64
        @Property(name = "text", type = String.class),
jtulach@0
    65
        @Property(name = "created_at", type = String.class),
jtulach@0
    66
    })
jtulach@0
    67
    static final class Twt {
jtulach@36
    68
        @ComputedProperty static String from_user(User user) {
jtulach@36
    69
            return user != null ? user.getName() : null;
jtulach@36
    70
        }
jtulach@36
    71
        
jtulach@36
    72
        @ComputedProperty static String imageUrl(User user) {
jtulach@36
    73
            return user != null ? user.getProfile_image_url() : null;
jtulach@36
    74
        }
jtulach@36
    75
        
jtulach@0
    76
        @ComputedProperty static String html(String text) {
jtulach@0
    77
            StringBuilder sb = new StringBuilder(320);
jtulach@0
    78
            for (int pos = 0;;) {
jtulach@0
    79
                int http = text.indexOf("http", pos);
jtulach@0
    80
                if (http == -1) {
jtulach@0
    81
                    sb.append(text.substring(pos));
jtulach@0
    82
                    return sb.toString();
jtulach@0
    83
                }
jtulach@0
    84
                int spc = text.indexOf(' ', http);
jtulach@0
    85
                if (spc == -1) {
jtulach@0
    86
                    spc = text.length();
jtulach@0
    87
                }
jtulach@0
    88
                sb.append(text.substring(pos, http));
jtulach@0
    89
                String url = text.substring(http, spc);
jtulach@0
    90
                sb.append("<a href='").append(url).append("'>").append(url).append("</a>");
jtulach@0
    91
                pos = spc;
jtulach@0
    92
            }
jtulach@0
    93
        }
jtulach@0
    94
        
jtulach@36
    95
        @ComputedProperty static String userUrl(User user) {
jtulach@36
    96
            return "http://twitter.com/" + user.getName();
jtulach@0
    97
        }
jtulach@0
    98
    }
jtulach@0
    99
    @Model(className = "TwitterQuery", properties = {
jtulach@36
   100
        @Property(array = true, name = "statuses", type = Twt.class)
jtulach@0
   101
    })
jtulach@0
   102
    public static final class TwttrQr {
jtulach@0
   103
    }
jtulach@0
   104
    
jtulach@36
   105
    @OnReceive(url="{url}?{query}&callback={me}", jsonp="me")
jtulach@0
   106
    static void queryTweets(TwitterModel page, TwitterQuery q) {
jtulach@0
   107
        page.getCurrentTweets().clear();
jtulach@36
   108
        page.getCurrentTweets().addAll(q.getStatuses());
jtulach@16
   109
        page.setLoading(false);
jtulach@0
   110
    }
jtulach@0
   111
    
jtulach@0
   112
    @OnPropertyChange("activeTweetersName")
jtulach@0
   113
    static void changeTweetersList(TwitterModel model) {
jtulach@0
   114
        Tweeters people = findByName(model.getSavedLists(), model.getActiveTweetersName());        
jtulach@0
   115
        model.getActiveTweeters().clear();
jtulach@0
   116
        model.getActiveTweeters().addAll(people.getUserNames());
jtulach@0
   117
    }
jtulach@0
   118
    
jtulach@0
   119
    @OnPropertyChange({ "activeTweeters", "activeTweetersCount" })
jtulach@0
   120
    static void refreshTweets(TwitterModel model) {
jtulach@0
   121
        StringBuilder sb = new StringBuilder();
jtulach@36
   122
        sb.append("users=");
jtulach@0
   123
        String sep = "";
jtulach@0
   124
        for (String p : model.getActiveTweeters()) {
jtulach@0
   125
            sb.append(sep);
jtulach@0
   126
            sb.append(p);
jtulach@36
   127
            sep = "%7C";
jtulach@36
   128
        }
jtulach@36
   129
        if (sep.isEmpty()) {
jtulach@36
   130
            return;
jtulach@0
   131
        }
jtulach@16
   132
        model.setLoading(true);
jtulach@36
   133
        model.queryTweets("http://services.netbeans.org/synergy/server/api/twitter.php", sb.toString());
jtulach@0
   134
    }
jtulach@0
   135
    
jtulach@0
   136
    static {
jtulach@36
   137
        try {
jtulach@36
   138
            init();
jtulach@36
   139
        } catch (Throwable ex) {
jtulach@36
   140
            ex.printStackTrace();
jtulach@36
   141
        }
jtulach@36
   142
    }
jtulach@36
   143
    private static void init() {
jtulach@36
   144
        System.setProperty("http.proxyHost", "www-proxy.uk.oracle.com");
jtulach@36
   145
        System.setProperty("http.proxyPort", "80");
jtulach@36
   146
        
jtulach@36
   147
        final TwitterModel model = new TwitterModel();
jtulach@0
   148
        final List<Tweeters> svdLst = model.getSavedLists();
jtulach@0
   149
        svdLst.add(newTweeters("API Design", "JaroslavTulach"));
jtulach@0
   150
        svdLst.add(newTweeters("Celebrities", "JohnCleese", "MCHammer", "StephenFry", "algore", "StevenSanderson"));
jtulach@0
   151
        svdLst.add(newTweeters("Microsoft people", "BillGates", "shanselman", "ScottGu"));
jtulach@36
   152
        svdLst.add(newTweeters("NetBeans", "GeertjanW","monacotoni", "NetBeans", "petrjiricka", "ladariha"));
jtulach@0
   153
        svdLst.add(newTweeters("Tech pundits", "Scobleizer", "LeoLaporte", "techcrunch", "BoingBoing", "timoreilly", "codinghorror"));
jtulach@0
   154
jtulach@0
   155
        model.setActiveTweetersName("NetBeans");
jtulach@0
   156
jtulach@0
   157
        model.applyBindings();
jtulach@0
   158
    }
jtulach@0
   159
    
jtulach@0
   160
    @ComputedProperty
jtulach@0
   161
    static boolean hasUnsavedChanges(List<String> activeTweeters, List<Tweeters> savedLists, String activeTweetersName) {
jtulach@0
   162
        Tweeters tw = findByName(savedLists, activeTweetersName);
jtulach@0
   163
        if (activeTweeters == null) {
jtulach@0
   164
            return false;
jtulach@0
   165
        }
jtulach@0
   166
        return !tw.getUserNames().equals(activeTweeters);
jtulach@0
   167
    }
jtulach@0
   168
    
jtulach@0
   169
    @ComputedProperty
jtulach@0
   170
    static int activeTweetersCount(List<String> activeTweeters) {
jtulach@0
   171
        return activeTweeters.size();
jtulach@0
   172
    }
jtulach@0
   173
    
jtulach@0
   174
    @ComputedProperty
jtulach@0
   175
    static boolean userNameToAddIsValid(
jtulach@0
   176
        String userNameToAdd, String activeTweetersName, List<Tweeters> savedLists, List<String> activeTweeters
jtulach@0
   177
    ) {
jtulach@0
   178
        return userNameToAdd != null && 
jtulach@0
   179
            userNameToAdd.matches("[a-zA-Z0-9_]{1,15}") &&
jtulach@0
   180
            !activeTweeters.contains(userNameToAdd);
jtulach@0
   181
    }
jtulach@0
   182
    
jtulach@0
   183
    @Function
jtulach@0
   184
    static void deleteList(TwitterModel model) {
jtulach@0
   185
        final List<Tweeters> sl = model.getSavedLists();
jtulach@0
   186
        sl.remove(findByName(sl, model.getActiveTweetersName()));
jtulach@0
   187
        if (sl.isEmpty()) {
jtulach@36
   188
            final Tweeters t = new Tweeters();
jtulach@0
   189
            t.setName("New");
jtulach@0
   190
            sl.add(t);
jtulach@0
   191
        }
jtulach@0
   192
        model.setActiveTweetersName(sl.get(0).getName());
jtulach@0
   193
    }
jtulach@0
   194
    
jtulach@0
   195
    @Function
jtulach@0
   196
    static void saveChanges(TwitterModel model) {
jtulach@0
   197
        Tweeters t = findByName(model.getSavedLists(), model.getActiveTweetersName());
jtulach@0
   198
        int indx = model.getSavedLists().indexOf(t);
jtulach@0
   199
        if (indx != -1) {
jtulach@0
   200
            t.setName(model.getActiveTweetersName());
jtulach@0
   201
            t.getUserNames().clear();
jtulach@0
   202
            t.getUserNames().addAll(model.getActiveTweeters());
jtulach@0
   203
        }
jtulach@0
   204
    }
jtulach@0
   205
    
jtulach@0
   206
    @Function
jtulach@0
   207
    static void addUser(TwitterModel model) {
jtulach@0
   208
        String n = model.getUserNameToAdd();
jtulach@0
   209
        model.getActiveTweeters().add(n);
jtulach@0
   210
    }
jtulach@0
   211
    @Function
jtulach@0
   212
    static void removeUser(String data, TwitterModel model) {
jtulach@0
   213
        model.getActiveTweeters().remove(data);
jtulach@0
   214
    }
jtulach@0
   215
    
jtulach@0
   216
    private static Tweeters findByName(List<Tweeters> list, String name) {
jtulach@0
   217
        for (Tweeters l : list) {
jtulach@0
   218
            if (l.getName() != null && l.getName().equals(name)) {
jtulach@0
   219
                return l;
jtulach@0
   220
            }
jtulach@0
   221
        }
jtulach@36
   222
        return list.isEmpty() ? new Tweeters() : list.get(0);
jtulach@0
   223
    }
jtulach@0
   224
    
jtulach@0
   225
    private static Tweeters newTweeters(String listName, String... userNames) {
jtulach@36
   226
        Tweeters t = new Tweeters();
jtulach@0
   227
        t.setName(listName);
jtulach@0
   228
        t.getUserNames().addAll(Arrays.asList(userNames));
jtulach@0
   229
        return t;
jtulach@0
   230
    }
jtulach@0
   231
}