ko-archetype/src/main/resources/archetype-resources/src/main/java/TwitterClient.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/ko-archetype/src/main/resources/archetype-resources/src/main/java/TwitterClient.java Mon May 13 14:25:37 2013 +0200
1.3 @@ -0,0 +1,177 @@
1.4 +package ${package};
1.5 +
1.6 +import java.util.Arrays;
1.7 +import java.util.List;
1.8 +import net.java.html.json.ComputedProperty;
1.9 +import net.java.html.json.Context;
1.10 +import net.java.html.json.Function;
1.11 +import net.java.html.json.Model;
1.12 +import net.java.html.json.OnPropertyChange;
1.13 +import net.java.html.json.OnReceive;
1.14 +import net.java.html.json.Property;
1.15 +
1.16 +@Model(className="TwitterModel", properties={
1.17 + @Property(name="savedLists", type=Tweeters.class, array = true),
1.18 + @Property(name="activeTweetersName", type=String.class),
1.19 + @Property(name="activeTweeters", type=String.class, array = true),
1.20 + @Property(name="userNameToAdd", type=String.class),
1.21 + @Property(name="currentTweets", type=Tweet.class, array = true)
1.22 +})
1.23 +public class TwitterClient {
1.24 + @Model(className = "Tweeters", properties = {
1.25 + @Property(name="name", type = String.class),
1.26 + @Property(name="userNames", type = String.class, array = true)
1.27 + })
1.28 + static class Twttrs {
1.29 + }
1.30 + @Model(className = "Tweet", properties = {
1.31 + @Property(name = "from_user", type = String.class),
1.32 + @Property(name = "from_user_id", type = int.class),
1.33 + @Property(name = "profile_image_url", type = String.class),
1.34 + @Property(name = "text", type = String.class),
1.35 + @Property(name = "created_at", type = String.class),
1.36 + })
1.37 + static final class Twt {
1.38 + @ComputedProperty static String html(String text) {
1.39 + StringBuilder sb = new StringBuilder(320);
1.40 + for (int pos = 0;;) {
1.41 + int http = text.indexOf("http", pos);
1.42 + if (http == -1) {
1.43 + sb.append(text.substring(pos));
1.44 + return sb.toString();
1.45 + }
1.46 + int spc = text.indexOf(' ', http);
1.47 + if (spc == -1) {
1.48 + spc = text.length();
1.49 + }
1.50 + sb.append(text.substring(pos, http));
1.51 + String url = text.substring(http, spc);
1.52 + sb.append("<a href='").append(url).append("'>").append(url).append("</a>");
1.53 + pos = spc;
1.54 + }
1.55 + }
1.56 +
1.57 + @ComputedProperty static String userUrl(String from_user) {
1.58 + return "http://twitter.com/" + from_user;
1.59 + }
1.60 + }
1.61 + @Model(className = "TwitterQuery", properties = {
1.62 + @Property(array = true, name = "results", type = Twt.class)
1.63 + })
1.64 + public static final class TwttrQr {
1.65 + }
1.66 +
1.67 + @OnReceive(url="{root}/search.json?{query}&callback={me}", jsonp="me")
1.68 + static void queryTweets(TwitterModel page, TwitterQuery q) {
1.69 + page.getCurrentTweets().clear();
1.70 + page.getCurrentTweets().addAll(q.getResults());
1.71 + }
1.72 +
1.73 + @OnPropertyChange("activeTweetersName")
1.74 + static void changeTweetersList(TwitterModel model) {
1.75 + Tweeters people = findByName(model.getSavedLists(), model.getActiveTweetersName());
1.76 + model.getActiveTweeters().clear();
1.77 + model.getActiveTweeters().addAll(people.getUserNames());
1.78 + }
1.79 +
1.80 + @OnPropertyChange({ "activeTweeters", "activeTweetersCount" })
1.81 + static void refreshTweets(TwitterModel model) {
1.82 + StringBuilder sb = new StringBuilder();
1.83 + sb.append("rpp=25&q=");
1.84 + String sep = "";
1.85 + for (String p : model.getActiveTweeters()) {
1.86 + sb.append(sep);
1.87 + sb.append("from:");
1.88 + sb.append(p);
1.89 + sep = " OR ";
1.90 + }
1.91 + model.queryTweets("http://search.twitter.com", sb.toString());
1.92 + }
1.93 +
1.94 + private static final Context DEFAULT = Context.findDefault(TwitterClient.class);
1.95 + static {
1.96 + final TwitterModel model = new TwitterModel(DEFAULT);
1.97 + final List<Tweeters> svdLst = model.getSavedLists();
1.98 + svdLst.add(newTweeters("API Design", "JaroslavTulach"));
1.99 + svdLst.add(newTweeters("Celebrities", "JohnCleese", "MCHammer", "StephenFry", "algore", "StevenSanderson"));
1.100 + svdLst.add(newTweeters("Microsoft people", "BillGates", "shanselman", "ScottGu"));
1.101 + svdLst.add(newTweeters("NetBeans", "GeertjanW","monacotoni", "NetBeans", "petrjiricka"));
1.102 + svdLst.add(newTweeters("Tech pundits", "Scobleizer", "LeoLaporte", "techcrunch", "BoingBoing", "timoreilly", "codinghorror"));
1.103 +
1.104 + model.setActiveTweetersName("NetBeans");
1.105 +
1.106 + model.applyBindings();
1.107 + }
1.108 +
1.109 + @ComputedProperty
1.110 + static boolean hasUnsavedChanges(List<String> activeTweeters, List<Tweeters> savedLists, String activeTweetersName) {
1.111 + Tweeters tw = findByName(savedLists, activeTweetersName);
1.112 + if (activeTweeters == null) {
1.113 + return false;
1.114 + }
1.115 + return !tw.getUserNames().equals(activeTweeters);
1.116 + }
1.117 +
1.118 + @ComputedProperty
1.119 + static int activeTweetersCount(List<String> activeTweeters) {
1.120 + return activeTweeters.size();
1.121 + }
1.122 +
1.123 + @ComputedProperty
1.124 + static boolean userNameToAddIsValid(
1.125 + String userNameToAdd, String activeTweetersName, List<Tweeters> savedLists, List<String> activeTweeters
1.126 + ) {
1.127 + return userNameToAdd != null &&
1.128 + userNameToAdd.matches("[a-zA-Z0-9_]{1,15}") &&
1.129 + !activeTweeters.contains(userNameToAdd);
1.130 + }
1.131 +
1.132 + @Function
1.133 + static void deleteList(TwitterModel model) {
1.134 + final List<Tweeters> sl = model.getSavedLists();
1.135 + sl.remove(findByName(sl, model.getActiveTweetersName()));
1.136 + if (sl.isEmpty()) {
1.137 + final Tweeters t = new Tweeters(DEFAULT);
1.138 + t.setName("New");
1.139 + sl.add(t);
1.140 + }
1.141 + model.setActiveTweetersName(sl.get(0).getName());
1.142 + }
1.143 +
1.144 + @Function
1.145 + static void saveChanges(TwitterModel model) {
1.146 + Tweeters t = findByName(model.getSavedLists(), model.getActiveTweetersName());
1.147 + int indx = model.getSavedLists().indexOf(t);
1.148 + if (indx != -1) {
1.149 + t.setName(model.getActiveTweetersName());
1.150 + t.getUserNames().clear();
1.151 + t.getUserNames().addAll(model.getActiveTweeters());
1.152 + }
1.153 + }
1.154 +
1.155 + @Function
1.156 + static void addUser(TwitterModel model) {
1.157 + String n = model.getUserNameToAdd();
1.158 + model.getActiveTweeters().add(n);
1.159 + }
1.160 + @Function
1.161 + static void removeUser(String data, TwitterModel model) {
1.162 + model.getActiveTweeters().remove(data);
1.163 + }
1.164 +
1.165 + private static Tweeters findByName(List<Tweeters> list, String name) {
1.166 + for (Tweeters l : list) {
1.167 + if (l.getName() != null && l.getName().equals(name)) {
1.168 + return l;
1.169 + }
1.170 + }
1.171 + return list.isEmpty() ? new Tweeters(DEFAULT) : list.get(0);
1.172 + }
1.173 +
1.174 + private static Tweeters newTweeters(String listName, String... userNames) {
1.175 + Tweeters t = new Tweeters(DEFAULT);
1.176 + t.setName(listName);
1.177 + t.getUserNames().addAll(Arrays.asList(userNames));
1.178 + return t;
1.179 + }
1.180 +}