Search in sources :

Example 1 with TaskCompletionSource

use of com.google.firebase.tasks.TaskCompletionSource in project iosched by google.

the class LoadSessionsServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    // Check that only admins or other allowed users can make this call.
    if (!performBasicChecking(req, resp)) {
        return;
    }
    // Pull existing session and room data from Google Cloud Storage.
    JsonParser jsonParser = new JsonParser();
    String manifestStr = IOUtils.toString(new URL(Config.CLOUD_STORAGE_BASE_URL + MANIFEST_FILENAME).openStream());
    JsonElement jManifest = jsonParser.parse(manifestStr);
    JsonArray jDataFiles = jManifest.getAsJsonObject().get("data_files").getAsJsonArray();
    String sessionDataFileName = null;
    for (int i = 0; i < jDataFiles.size(); i++) {
        String filename = jDataFiles.get(i).getAsString();
        if (filename.startsWith("session_data")) {
            sessionDataFileName = filename;
            break;
        }
    }
    if (sessionDataFileName == null) {
        // Unable to find session data to load.
        resp.setContentType("text/plain");
        resp.getWriter().println("Unable to find session data to load.");
        return;
    }
    // Get session and room data from file in GCS.
    String sessionDataStr = IOUtils.toString(new URL(Config.CLOUD_STORAGE_BASE_URL + sessionDataFileName).openStream());
    JsonElement jSessionData = jsonParser.parse(sessionDataStr);
    // Extract rooms and sessions
    final JsonArray jRooms = jSessionData.getAsJsonObject().get(ROOMS_KEY).getAsJsonArray();
    final JsonArray jSessions = jSessionData.getAsJsonObject().get(SESSIONS_KEY).getAsJsonArray();
    // Only sessions that are of type TYPE_SESSIONS can be reserved so remove those that do not
    // have this type.
    List<JsonElement> sessionsToRemove = new ArrayList<>();
    for (JsonElement jSession : jSessions) {
        // TODO: keynotes have a better type.
        if (jSession.getAsJsonObject().get("id").getAsString().startsWith("__keynote")) {
            sessionsToRemove.add(jSession);
            continue;
        }
        JsonArray jTags = jSession.getAsJsonObject().get("tags").getAsJsonArray();
        boolean isReservable = false;
        for (JsonElement jTag : jTags) {
            if (jTag.getAsString().equals("TYPE_SESSIONS")) {
                isReservable = true;
                break;
            }
        }
        if (!isReservable) {
            sessionsToRemove.add(jSession);
        }
    }
    for (JsonElement jsonElement : sessionsToRemove) {
        jSessions.remove(jsonElement);
    }
    log.info("Non-Reservable session count: " + sessionsToRemove.size());
    log.info("Reservable session count: " + jSessions.size());
    resp.setContentType("text/plain");
    resp.getWriter().println("Room and session data retrieved.");
    // Initialize Firebase app with service account credentials.
    FirebaseOptions options = new FirebaseOptions.Builder().setCredential(FirebaseCredentials.fromCertificate(getServletContext().getResourceAsStream("/WEB-INF/io2017-backend-dev-serv-cred.json"))).setDatabaseUrl("https://io2017-backend-dev.firebaseio.com/").build();
    try {
        FirebaseApp.initializeApp(options);
        log.info("Initialized Firebase");
    } catch (Exception e) {
        // Firebase Instance already exists.
        log.info("Firebase already initialized");
    }
    // Session retrieval task.
    final TaskCompletionSource<Map<String, Session>> sessionsTaskCompletionSource = new TaskCompletionSource<>();
    final Task<Map<String, Session>> sessionsTask = sessionsTaskCompletionSource.getTask();
    // Get Firebase Database reference to sessions path, add listener for single event.
    final FirebaseDatabase defaultDatabase = FirebaseDatabase.getInstance();
    defaultDatabase.getReference(PATH_SESSIONS).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Map<String, Session> sessions = new HashMap<>();
            for (DataSnapshot sessionSnapshot : dataSnapshot.getChildren()) {
                Session s = sessionSnapshot.getValue(Session.class);
                sessions.put(sessionSnapshot.getKey(), s);
            }
            sessionsTaskCompletionSource.setResult(sessions);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            log.warning("RTDB error: " + databaseError.getMessage());
        }
    });
    try {
        // Wait for the sessions from RTDB.
        Map<String, Session> rtdbSessions = Tasks.await(sessionsTask);
        // Update sessions in RTDB with values from GCS.
        for (String sessionId : rtdbSessions.keySet()) {
            // Update session task.
            final TaskCompletionSource<Void> updateSessionTCS = new TaskCompletionSource<>();
            final Task<Void> updateSessionTCSTask = updateSessionTCS.getTask();
            // Check that GCS has a matching session and room as the one from RTDB.
            JsonObject jSession = getSession(sessionId, jSessions);
            if (jSession != null) {
                JsonObject jRoom = getRoom(jSession, jRooms);
                if (jRoom != null) {
                    final int gcsCap = Long.valueOf(Math.round(jRoom.get(CAPACITY_KEY).getAsInt() * RESERVABLE_CAPACITY_PERCENTAGE)).intValue();
                    final String gcsRoomName = jRoom.get(NAME_KEY).getAsString();
                    final long gcsStartTime = getTimeInMillis(jSession, START_TIME_KEY);
                    final long gcsEndTime = getTimeInMillis(jSession, END_TIME_KEY);
                    final String gcsTitle = jSession.get(TITLE_KEY).getAsString();
                    // Update session in a transaction.
                    defaultDatabase.getReference().child(PATH_SESSIONS).child(sessionId).runTransaction(new Handler() {

                        @Override
                        public Result doTransaction(MutableData mutableData) {
                            Session session = mutableData.getValue(Session.class);
                            if (session != null) {
                                // Update start and end times of session in RTDB.
                                session.time_start = gcsStartTime;
                                session.time_end = gcsEndTime;
                                // Update session title.
                                session.title = gcsTitle;
                                // Update session room name.
                                session.room_name = gcsRoomName;
                                int currResCount = session.seats.reserved;
                                boolean currHasSeats = session.seats.seats_available;
                                if (currResCount > gcsCap) {
                                    // If there are to many reservations move extras to waitlist.
                                    int resToMove = currResCount - gcsCap;
                                    moveReservationsToWaitList(resToMove, session);
                                } else if (currResCount < gcsCap && !currHasSeats) {
                                    // If there is space and a waitlist, promote as many as possible from the
                                    // waitlist.
                                    int numSeatsAvailable = gcsCap - currResCount;
                                    promoteFromWaitList(currResCount, numSeatsAvailable, session);
                                }
                                // Update session capacity.
                                session.seats.capacity = gcsCap;
                                mutableData.setValue(session);
                            }
                            return success(mutableData);
                        }

                        @Override
                        public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
                            // Signal that session update is complete.
                            updateSessionTCS.setResult(null);
                        }
                    });
                    // Wait for session update to complete.
                    Tasks.await(updateSessionTCSTask);
                }
                // Remove updated sessions from list of sessions to be added.
                jSessions.remove(jSession);
            }
        }
    } catch (ExecutionException | InterruptedException e) {
        e.printStackTrace();
    }
    // Add all sessions that were retrieved from GCS but did not already in RTDB.
    for (int i = 0; i < jSessions.size(); i++) {
        JsonObject jSession = jSessions.get(i).getAsJsonObject();
        String sessionId = jSession.get(ID_KEY).getAsString();
        String sessionTitle = jSession.get(TITLE_KEY).getAsString();
        String roomId = jSession.get(ROOM_KEY).getAsString();
        JsonObject jRoom = getRoom(roomId, jRooms);
        int capacity = Long.valueOf(Math.round(jRoom.get(CAPACITY_KEY).getAsInt() * RESERVABLE_CAPACITY_PERCENTAGE)).intValue();
        String sessionRoomName = jRoom.get(NAME_KEY).getAsString();
        long startTime = getTimeInMillis(jSession, START_TIME_KEY);
        long endTime = getTimeInMillis(jSession, END_TIME_KEY);
        Session session = new Session();
        session.title = sessionTitle;
        session.room_name = sessionRoomName;
        session.time_end = endTime;
        session.time_start = startTime;
        Seats seats = new Seats();
        seats.capacity = capacity;
        seats.reserved = 0;
        seats.seats_available = true;
        seats.waitlisted = false;
        session.seats = seats;
        defaultDatabase.getReference(PATH_SESSIONS).child(sessionId).setValue(session);
    }
    resp.getWriter().println("Sessions added to RTDB.");
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) DataSnapshot(com.google.firebase.database.DataSnapshot) URL(java.net.URL) Result(com.google.firebase.database.Transaction.Result) TaskCompletionSource(com.google.firebase.tasks.TaskCompletionSource) ValueEventListener(com.google.firebase.database.ValueEventListener) MutableData(com.google.firebase.database.MutableData) ExecutionException(java.util.concurrent.ExecutionException) JsonParser(com.google.gson.JsonParser) Handler(com.google.firebase.database.Transaction.Handler) FirebaseOptions(com.google.firebase.FirebaseOptions) ServletException(javax.servlet.ServletException) ParseException(java.text.ParseException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Seats(com.google.samples.apps.iosched.server.schedule.reservations.model.Seats) JsonArray(com.google.gson.JsonArray) DatabaseError(com.google.firebase.database.DatabaseError) JsonElement(com.google.gson.JsonElement) HashMap(java.util.HashMap) Map(java.util.Map) Session(com.google.samples.apps.iosched.server.schedule.reservations.model.Session)

Example 2 with TaskCompletionSource

use of com.google.firebase.tasks.TaskCompletionSource in project iosched by google.

the class RegistrationEndpoint method registrationStatus.

@ApiMethod(path = "status", httpMethod = ApiMethod.HttpMethod.GET)
public RegistrationResult registrationStatus(ServletContext context, @Named("firebaseUserToken") String firebaseUserToken) throws IOException, ForbiddenException, ExecutionException, InterruptedException, InternalServerErrorException {
    String databaseUrl = context.getInitParameter("databaseUrl");
    LOG.info("databaseUrl: " + databaseUrl);
    String serviceAccountKey = context.getInitParameter("accountKey");
    LOG.info("accountKey: " + serviceAccountKey);
    InputStream serviceAccount = context.getResourceAsStream(serviceAccountKey);
    LOG.info("serviceAccount: " + serviceAccount);
    firebaseWrapper.initFirebase(databaseUrl, serviceAccount);
    firebaseWrapper.authenticateFirebaseUser(firebaseUserToken);
    if (!firebaseWrapper.isUserAuthenticated()) {
        throw new ForbiddenException("Not authenticated");
    }
    boolean isRegistered = isUserRegistered(context);
    final TaskCompletionSource<Boolean> isRegisteredTCS = new TaskCompletionSource<>();
    final Task<Boolean> isRegisteredTCSTask = isRegisteredTCS.getTask();
    // Update the user registration state in the Real-time Database.
    DatabaseReference dbRef = firebaseWrapper.getDatabaseReference();
    int rtdbRetries = 0;
    while (rtdbRetries < RTDB_RETRY_LIMIT) {
        dbRef.child("users").child(firebaseWrapper.getUserId()).setValue(isRegistered).addOnCompleteListener(new OnCompleteListener<Void>() {

            @Override
            public void onComplete(Task<Void> task) {
                if (task.isSuccessful()) {
                    isRegisteredTCS.setResult(true);
                } else {
                    isRegisteredTCS.setResult(false);
                }
            }
        });
        // If writing to RTDB was successful break out.
        if (Tasks.await(isRegisteredTCSTask)) {
            break;
        }
        LOG.info("Writing to RTDB has failed.");
        rtdbRetries++;
    }
    // indeed registered.
    if (rtdbRetries >= RTDB_RETRY_LIMIT) {
        throw new InternalServerErrorException("Unable to write registration status to RTDB.");
    } else {
        // Return the user registration state.
        return new RegistrationResult(isRegistered);
    }
}
Also used : ForbiddenException(com.google.api.server.spi.response.ForbiddenException) DatabaseReference(com.google.firebase.database.DatabaseReference) InputStream(java.io.InputStream) TaskCompletionSource(com.google.firebase.tasks.TaskCompletionSource) InternalServerErrorException(com.google.api.server.spi.response.InternalServerErrorException) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Aggregations

TaskCompletionSource (com.google.firebase.tasks.TaskCompletionSource)2 ApiMethod (com.google.api.server.spi.config.ApiMethod)1 ForbiddenException (com.google.api.server.spi.response.ForbiddenException)1 InternalServerErrorException (com.google.api.server.spi.response.InternalServerErrorException)1 FirebaseOptions (com.google.firebase.FirebaseOptions)1 DataSnapshot (com.google.firebase.database.DataSnapshot)1 DatabaseError (com.google.firebase.database.DatabaseError)1 DatabaseReference (com.google.firebase.database.DatabaseReference)1 FirebaseDatabase (com.google.firebase.database.FirebaseDatabase)1 MutableData (com.google.firebase.database.MutableData)1 Handler (com.google.firebase.database.Transaction.Handler)1 Result (com.google.firebase.database.Transaction.Result)1 ValueEventListener (com.google.firebase.database.ValueEventListener)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 Seats (com.google.samples.apps.iosched.server.schedule.reservations.model.Seats)1 Session (com.google.samples.apps.iosched.server.schedule.reservations.model.Session)1 IOException (java.io.IOException)1