Search in sources :

Example 1 with ReservedSession

use of com.google.samples.apps.iosched.server.userdata.db.ReservedSession in project iosched by google.

the class SyncReservationsQueueWorker method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ServletContext context = getServletContext();
    String serviceAccountKey = context.getInitParameter("accountKey");
    LOG.info("accountKey: " + serviceAccountKey);
    InputStream serviceAccount = context.getResourceAsStream(serviceAccountKey);
    LOG.info("serviceAccount: " + serviceAccount);
    String accessToken = firebaseWrapper.getAccessToken(serviceAccount);
    if (accessToken == null) {
        LOG.severe("Unable to get access token");
        return;
    }
    // Get all existing reservations from RTDB.
    JsonArray firebaseReservations = getFirebaseReservations(accessToken);
    // For each user update reservations in datastore with reservations from RTDB.
    for (int i = 0; firebaseReservations != null && i < firebaseReservations.size(); i++) {
        JsonObject jReservation = firebaseReservations.get(i).getAsJsonObject();
        String userId = jReservation.get("userId").getAsString();
        try {
            // Deserialize reservations from RTDB.
            Map<String, ReservedSession> reservedSessions = new HashMap<>();
            if (jReservation.has(RESERVATIONS_KEY)) {
                JsonArray jUserReservations = jReservation.get(RESERVATIONS_KEY).getAsJsonArray();
                for (int j = 0; j < jUserReservations.size(); j++) {
                    JsonObject jUserReservation = jUserReservations.get(j).getAsJsonObject();
                    String sessionId = jUserReservation.get("sessionId").getAsString();
                    Status status = mapToDatastoreStatus(jUserReservation.get("status").getAsString());
                    ReservedSession reservedSession = new ReservedSession(sessionId, status, System.currentTimeMillis());
                    reservedSessions.put(sessionId, reservedSession);
                }
            }
            // Update reservations in datastore with those from RTDB.
            UserData userData = ofy().load().type(UserData.class).id(userId).safe();
            for (String sessionId : userData.reservedSessions.keySet()) {
                if (reservedSessions.containsKey(sessionId)) {
                    userData.reservedSessions.put(sessionId, reservedSessions.get(sessionId));
                    reservedSessions.remove(sessionId);
                } else {
                    // Mark DELETED if reservation does not exist in RTDB.
                    userData.reservedSessions.get(sessionId).status = Status.DELETED;
                    userData.reservedSessions.get(sessionId).timestampUTC = System.currentTimeMillis();
                }
            }
            // Add reservations from RTDB that do not exist in datastore.
            for (ReservedSession reservedSession : reservedSessions.values()) {
                userData.reservedSessions.put(reservedSession.sessionID, reservedSession);
            }
            ofy().save().entity(userData).now();
        } catch (NotFoundException e) {
            LOG.severe(e.getMessage());
        }
    }
}
Also used : JsonArray(com.google.gson.JsonArray) Status(com.google.samples.apps.iosched.server.userdata.db.ReservedSession.Status) ReservedSession(com.google.samples.apps.iosched.server.userdata.db.ReservedSession) HashMap(java.util.HashMap) UserData(com.google.samples.apps.iosched.server.userdata.db.UserData) InputStream(java.io.InputStream) ServletContext(javax.servlet.ServletContext) JsonObject(com.google.gson.JsonObject) NotFoundException(com.googlecode.objectify.NotFoundException)

Example 2 with ReservedSession

use of com.google.samples.apps.iosched.server.userdata.db.ReservedSession in project iosched by google.

the class UserdataEndpoint method addReservedSession.

/**
 * Add a reserved session for the specified user. If the session is already in the user's feed,
 * it will be annotated with status=RESERVED.
 *
 * @param user         Service account making the request (injected by Endpoints)
 * @param userId       User ID of user that reserved a session.
 * @param sessionId    Session ID to mark as reserved.
 * @param timestampUTC The time (in millis, UTC) when the user performed this action. May be
 *                     different than the time this method is called if offline sync is
 *                     implemented. MUST BE ACCURATE - COMPENSATE FOR CLOCK DRIFT!
 * @return The list of reserved sessions for the user
 */
@ApiMethod(name = "addReservedSession", path = "reservations", httpMethod = ApiMethod.HttpMethod.PUT, clientIds = { Ids.SERVICE_ACCOUNT_CLIENT_ID })
public Map<String, ReservedSession> addReservedSession(User user, @Named("userId") String userId, @Named("sessionId") String sessionId, @Named("timestampUTC") long timestampUTC) throws UnauthorizedException {
    UserData data = getUser(user, userId);
    ReservedSession s = new ReservedSession(sessionId, Status.RESERVED, timestampUTC);
    data.reservedSessions.put(sessionId, s);
    save(data);
    // notify user's clients of reservation change change
    new GCMPing().notifyUserSync(data.userId);
    return data.reservedSessions;
}
Also used : GCMPing(com.google.samples.apps.iosched.server.schedule.server.GCMPing) ReservedSession(com.google.samples.apps.iosched.server.userdata.db.ReservedSession) UserData(com.google.samples.apps.iosched.server.userdata.db.UserData) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Example 3 with ReservedSession

use of com.google.samples.apps.iosched.server.userdata.db.ReservedSession in project iosched by google.

the class UserdataEndpoint method removeReservedSession.

/**
 * Remove a reserved session for the specified user. The session will still be
 * attached to the user's feed, but will be annotated with status=DELETED.
 *
 * @param user         Service account making the request (injected by Endpoints)
 * @param userId       User ID of user that reserved a session.
 * @param sessionId    Session ID to mark as not reserved.
 * @param timestampUTC The time (in millis, UTC) when the user performed this action. May be
 *                     different than the time this method is called if offline sync is
 *                     implemented. MUST BE ACCURATE - COMPENSATE FOR CLOCK DRIFT!
 */
@ApiMethod(name = "removeReservedSession", path = "reservations", httpMethod = ApiMethod.HttpMethod.DELETE, clientIds = { Ids.SERVICE_ACCOUNT_CLIENT_ID })
public void removeReservedSession(User user, @Named("userId") String userId, @Named("sessionId") String sessionId, @Named("timestampUTC") long timestampUTC) throws UnauthorizedException {
    UserData data = getUser(user, userId);
    ReservedSession s = new ReservedSession(sessionId, Status.DELETED, timestampUTC);
    data.reservedSessions.put(sessionId, s);
    save(data);
    // notify user's clients of reservation change change
    new GCMPing().notifyUserSync(data.userId);
}
Also used : GCMPing(com.google.samples.apps.iosched.server.schedule.server.GCMPing) ReservedSession(com.google.samples.apps.iosched.server.userdata.db.ReservedSession) UserData(com.google.samples.apps.iosched.server.userdata.db.UserData) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Example 4 with ReservedSession

use of com.google.samples.apps.iosched.server.userdata.db.ReservedSession in project iosched by google.

the class UserdataEndpoint method addWaitlistedSession.

/**
 * Add a waitlisted session for the specified user. If the session is already in the user's feed,
 * it will be annotated with status=WAITLISTED.
 *
 * @param user         Service account making the request (injected by Endpoints)
 * @param userId       User ID of user that reserved a session.
 * @param sessionId    Session ID to mark as reserved.
 * @param timestampUTC The time (in millis, UTC) when the user performed this action. May be
 *                     different than the time this method is called if offline sync is
 *                     implemented. MUST BE ACCURATE - COMPENSATE FOR CLOCK DRIFT!
 * @return The list of reserved sessions for the user
 */
@ApiMethod(name = "addWaitlistedSession", path = "reservations/waitlist", httpMethod = ApiMethod.HttpMethod.PUT, clientIds = { Ids.SERVICE_ACCOUNT_CLIENT_ID })
public Map<String, ReservedSession> addWaitlistedSession(User user, @Named("userId") String userId, @Named("sessionId") String sessionId, @Named("timestampUTC") long timestampUTC) throws UnauthorizedException {
    UserData data = getUser(user, userId);
    ReservedSession s = new ReservedSession(sessionId, Status.WAITLISTED, timestampUTC);
    data.reservedSessions.put(sessionId, s);
    save(data);
    // notify user's clients of reservation change change
    new GCMPing().notifyUserSync(data.userId);
    return data.reservedSessions;
}
Also used : GCMPing(com.google.samples.apps.iosched.server.schedule.server.GCMPing) ReservedSession(com.google.samples.apps.iosched.server.userdata.db.ReservedSession) UserData(com.google.samples.apps.iosched.server.userdata.db.UserData) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Aggregations

ReservedSession (com.google.samples.apps.iosched.server.userdata.db.ReservedSession)4 UserData (com.google.samples.apps.iosched.server.userdata.db.UserData)4 ApiMethod (com.google.api.server.spi.config.ApiMethod)3 GCMPing (com.google.samples.apps.iosched.server.schedule.server.GCMPing)3 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 Status (com.google.samples.apps.iosched.server.userdata.db.ReservedSession.Status)1 NotFoundException (com.googlecode.objectify.NotFoundException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 ServletContext (javax.servlet.ServletContext)1