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.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.PropertyProjection;
import com.google.appengine.api.datastore.Query;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthException;
import com.google.firebase.auth.FirebaseToken;
import com.google.sps.firebase.FirebaseAppManager;
import java.io.IOException;
import java.util.Collections;
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("/newclass")
public class NewClass 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());

    try {
      String className = request.getParameter("className").trim();

      Query query = new Query("Class");
      query.addProjection(new PropertyProjection("name", String.class));

      Boolean canAdd = true;

      PreparedQuery results = datastore.prepare(query);
      for (Entity entity : results.asIterable()) {
        String cName = (String) entity.getProperty("name");
        if (cName.equals(className)) {
          canAdd = false;
        }
      }

      if (canAdd) {
        String idToken = request.getParameter("idToken");
        FirebaseToken decodedToken = authInstance.verifyIdToken(idToken);

        Entity classEntity = new Entity("Class");
        classEntity.setProperty("owner", decodedToken.getUid());
        classEntity.setProperty("name", className);
        classEntity.setProperty("beingHelped", "");
        classEntity.setProperty("studentQueue", Collections.emptyList());
        classEntity.setProperty("visitKey", "");

        datastore.put(classEntity);

        Entity visitEntity = new Entity("Visit");
        visitEntity.setProperty("classKey", KeyFactory.keyToString(classEntity.getKey()));
        visitEntity.setProperty("numVisits", 0);

        datastore.put(visitEntity);

        Entity updateClassEntity = datastore.get(classEntity.getKey());
        updateClassEntity.setProperty("visitKey", KeyFactory.keyToString(visitEntity.getKey()));
        datastore.put(updateClassEntity);

      } else {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
      }

    } catch (FirebaseAuthException e) {
      response.sendError(HttpServletResponse.SC_FORBIDDEN);
    } catch (EntityNotFoundException e) {
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
  }
}

@@ -433,24 +433,16 @@
 tastore.
-Prepared
 Query;%0Ai
@@ -482,34 +482,36 @@
 tastore.
-PropertyProjection
+Query.FilterOperator
 ;%0Aimport
@@ -543,32 +543,48 @@
 .datastore.Query
+.FilterPredicate
 ;%0Aimport com.goo
@@ -1947,24 +1947,69 @@
 %22).trim();%0A%0A
+      // prevents creating duplicate classes%0A
       Query 
@@ -2015,16 +2015,26 @@
  query =
+%0A         
  new Que
@@ -2048,84 +2048,97 @@
 ss%22)
-;
 %0A
+   
       
-query.addProjection(new PropertyProjection(%22name%22, String.
+     .setFilter(new FilterPredicate(%22name%22, FilterOperator.EQUAL, 
 class
+Name
 ));%0A
-%0A
     
@@ -2166,217 +2166,67 @@
 ue;%0A
-%0A
       
-PreparedQuery results = datastore.prepare(query);%0A      for (Entity entity : results.asIterable()) %7B%0A        String cName = (String) entity.getProperty(%22name%22);%0A        if (cName.equals(className)
+if (datastore.prepare(query).countEntities() != 0
 ) %7B%0A
-  
     
@@ -2249,18 +2249,8 @@
 se;%0A
-        %7D%0A
     

Refactor check for duplicate classes