package com.google.sps.servlets;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceConfig;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.firebase.auth.FirebaseAuth;
import com.google.sps.firebase.FirebaseAppManager;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** Servlet that creates a new class Datastore. */
@WebServlet("/notify-student")
public class NotifyStudent extends HttpServlet {
  FirebaseAuth authInstance;
  DatastoreService datastore;

  @Override
  public void init(ServletConfig config) throws ServletException {
    try {
      authInstance = FirebaseAuth.getInstance(FirebaseAppManager.getApp());
    } catch (IOException e) {
      throw new ServletException(e);
    }
  }

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Get the input from the form.
    // navigate to /_ah/admin to view Datastore

    datastore = DatastoreServiceFactory.getDatastoreService();
    System.setProperty(
        DatastoreServiceConfig.DATASTORE_EMPTY_LIST_SUPPORT, Boolean.TRUE.toString());
  }
}

@@ -242,34 +242,406 @@
 gle.
-firebase.auth.FirebaseAuth
+appengine.api.datastore.Entity;%0Aimport com.google.appengine.api.datastore.EntityNotFoundException;%0Aimport com.google.appengine.api.datastore.Key;%0Aimport com.google.appengine.api.datastore.KeyFactory;%0Aimport com.google.appengine.api.datastore.Transaction;%0Aimport com.google.firebase.auth.FirebaseAuth;%0Aimport com.google.firebase.auth.FirebaseAuthException;%0Aimport com.google.firebase.auth.UserRecord
 ;%0Aim
@@ -709,32 +709,110 @@
 io.IOException;%0A
+import java.util.ArrayList;%0Aimport java.util.ConcurrentModificationException;%0A
 import javax.ser
@@ -1765,16 +1765,16 @@
 operty(%0A
-
         
@@ -1851,15 +1851,1698 @@
 ring());
+%0A%0A    try %7B%0A      String classCode = request.getParameter(%22classCode%22).trim();%0A%0A      int retries = 10;%0A      while (true) %7B%0A        Transaction txn = datastore.beginTransaction();%0A        try %7B%0A          // Retrive class entity%0A          Key classKey = KeyFactory.stringToKey(classCode);%0A          Entity classEntity = datastore.get(txn, classKey);%0A%0A          // Get uID from uEmail%0A          String uEmail = request.getParameter(%22uEmail%22);%0A          UserRecord userRecord = authInstance.getUserByEmail(uEmail);%0A          String uID = userRecord.getUid();%0A%0A          // Update queue%0A          ArrayList%3CString%3E updatedQueue = (ArrayList) classEntity.getProperty(%22studentQueue%22);%0A          updatedQueue.remove(uID);%0A%0A          // Update beingHelped%0A          ArrayList%3CString%3E beingHelped = (ArrayList) classEntity.getProperty(%22beingHelped%22);%0A          beingHelped.add(uID);%0A%0A          classEntity.setProperty(%22studentQueue%22, updatedQueue);%0A          classEntity.setProperty(%22beingHelped%22, beingHelped);%0A          datastore.put(txn, classEntity);%0A%0A          txn.commit();%0A          break;%0A        %7D catch (ConcurrentModificationException e) %7B%0A          if (retries == 0) %7B%0A            throw e;%0A          %7D%0A          // Allow retry to occur%0A          --retries;%0A        %7D finally %7B%0A          if (txn.isActive()) %7B%0A            txn.rollback();%0A          %7D%0A        %7D%0A      %7D%0A    %7D catch (EntityNotFoundException e) %7B%0A      response.sendError(HttpServletResponse.SC_NOT_FOUND);%0A    %7D catch (IllegalArgumentException e) %7B%0A      response.sendError(HttpServletResponse.SC_BAD_REQUEST);%0A    %7D catch (FirebaseAuthException e) %7B%0A      response.sendError(HttpServletResponse.SC_FORBIDDEN);%0A    %7D
 %0A  %7D%0A%7D%0A

Add servlet to take student off of queue