PageRenderTime 22ms CodeModel.GetById 8ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/foursquare/examples/push/util/Common.java

http://github.com/zackzachariah/Foursquare-Welcome-Screen
Java | 109 lines | 85 code | 19 blank | 5 comment | 9 complexity | 6be598ea29b839567ce8c413d503d8af MD5 | raw file
  1package com.foursquare.examples.push.util;
  2
  3import java.util.Date;
  4import java.util.List;
  5
  6import javax.jdo.JDOHelper;
  7import javax.jdo.PersistenceManager;
  8import javax.jdo.PersistenceManagerFactory;
  9
 10import com.foursquare.examples.push.models.LinkedUser;
 11import com.google.appengine.api.channel.ChannelMessage;
 12import com.google.appengine.api.channel.ChannelService;
 13import com.google.appengine.api.channel.ChannelServiceFactory;
 14import com.google.appengine.api.users.User;
 15import com.google.appengine.api.users.UserServiceFactory;
 16
 17import fi.foyt.foursquare.api.FoursquareApi;
 18
 19public class Common {
 20  private final static String CLIENT_ID = "YOUR CLIENT ID";
 21  private final static String CLIENT_SECRET = "YOUR CLIENT SECRET";
 22  private final static String CALLBACK = "YOUR CLIENT CALLBACK";
 23  public final static String PUSH_SECRET = "YOUR CLIENT PUSH SECRET";
 24  public final static String TARGET_VENUE = "THE FOURSQUARE ID OF THE VENUE YOU MANAGE";
 25  public final static String TARGET_VENUE_NAME = "THE NAME OF THE VENUE YOU MANAGE";
 26
 27  public static FoursquareApi getApi() { return getApi(null); }
 28  public static FoursquareApi getApi(String token) {
 29    FoursquareApi foursquareApi = new FoursquareApi(CLIENT_ID, CLIENT_SECRET, CALLBACK);
 30    if (token != null) foursquareApi.setoAuthToken(token);
 31    return foursquareApi;
 32  }
 33  public static FoursquareApi getCurrentApi(PersistenceManager pm) {
 34    User googler = getGoogleUser();
 35    if (googler != null) {
 36      LinkedUser luser = LinkedUser.loadOrCreate(pm, googler.getUserId());
 37      if (luser.foursquareAuth() != null) {
 38        return getApi(luser.foursquareAuth());
 39      }
 40    }
 41
 42    return null;
 43  }
 44
 45  // Convert the core aspects of a checkin to json
 46  public static String checkinToJson(String name, String photo, boolean isMayor) {
 47    StringBuilder sb = new StringBuilder();
 48    sb.append("{\"name\":\"");
 49    sb.append(name);
 50    sb.append("\",\"photo\":\"");
 51    sb.append(photo);
 52    sb.append("\",\"isMayor\":");
 53    sb.append(isMayor);
 54    sb.append("}");
 55    return sb.toString();
 56  }
 57
 58  /* Methods for getting at aspects of persistence */
 59  private static final PersistenceManagerFactory pmfInstance =
 60      JDOHelper.getPersistenceManagerFactory("transactions-optional");
 61
 62  private static PersistenceManagerFactory getPMF() {
 63    return pmfInstance;
 64  }
 65
 66  public static PersistenceManager getPM() {
 67    return getPMF().getPersistenceManager();
 68  }
 69
 70  /* Methods for user management tidbits */
 71  public static User getGoogleUser() {
 72    return UserServiceFactory.getUserService().getCurrentUser();
 73  }
 74
 75  public static String getGoogleLoginUrl() {
 76    return UserServiceFactory.getUserService().createLoginURL("/#retryAuth");
 77  }
 78
 79  public static String getFoursquareLoginUrl() {
 80    return "https://foursquare.com/oauth2/authenticate?client_id=" + CLIENT_ID +
 81           "&response_type=code&redirect_uri=" + CALLBACK;
 82  }
 83
 84  /* Methods for handling Channel Client ID creation and understanding */
 85  public static String createChannelToken(String vid) {
 86    ChannelService cService = ChannelServiceFactory.getChannelService();
 87    return cService.createChannel(getClientId(vid));
 88  }
 89
 90  private static String getClientId(String vid) {
 91    return vid + "-" + (new Date()).getTime();
 92  }
 93
 94  public static String parseClientId(String clientId) {
 95    int ind = clientId.indexOf('-');
 96    if (ind > 0) {
 97      return clientId.substring(0, ind);
 98    } else return null;
 99  }
100
101  // Actually handle sending out messages to a given list of clients
102  public static void sendUpdate(List<String> clients, String message) {
103    ChannelService cService = ChannelServiceFactory.getChannelService();
104
105    for (String client : clients) {
106      cService.sendMessage(new ChannelMessage(client, message));
107    }
108  }
109}