package com.google.sps.servlets;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.EntityNotFoundException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import com.google.gson.Gson;
import com.google.sps.tool.ResponseHandler;
import com.google.sps.data.Folder;

@WebServlet("/editfolder")
public class EditFolderServlet extends HttpServlet{
  
  @Override
  public void doPut(HttpServletRequest request, HttpServletResponse response) throws IOException {
    UserService userService = UserServiceFactory.getUserService();

    if (userService.isUserLoggedIn()) {
      String newFolderName = request.getParameter("folderName");
      String newFolderDefaultLanguage = request.getParameter("folderDefaultLanguage");
      String folderKey = request.getParameter("folderKey");
      updateFolder(response, newFolderName, newFolderDefaultLanguage, folderKey);
    }
  }

  private void updateFolder(HttpServletResponse response, String newFolderName, String newFolderDefaultLanguage, String folderKey) throws IOException {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Entity folderEntity = getExistingFolderInDatastore(response, datastore, folderKey);
    
    if (folderEntity == null) {
        ResponseHandler.sendErrorMessage(response, "Cannot edit Folder at the moment");
    }
    
    folderEntity.setProperty("folderName", newFolderName);
    folderEntity.setProperty("folderDefaultLanguage", newFolderDefaultLanguage);

    datastore.put(folderEntity);
  }

  private Entity getExistingFolderInDatastore(HttpServletResponse response, DatastoreService datastore, String folderKey) throws IOException {
    Entity folder;
    try {
      return datastore.get(KeyFactory.stringToKey(folderKey));
    } catch (EntityNotFoundException e) {
      return null;
    }
  }
}

@@ -1312,165 +1312,278 @@
 %22);%0A
+%0A
       
-updateFolder(response, newFolderName, newFolderDefaultLanguage, folderKey);%0A    %7D%0A  %7D%0A%0A  private void updateFolder(HttpServletResponse response, String
+Entity folderEntity = getExistingFolderInDatastore(response, datastore, folderKey);%0A%0A      if (folderEntity == null) %7B%0A        ResponseHandler.sendErrorMessage(response, %22Cannot edit Folder at the moment%22);%0A      %7D else %7B%0A        updateFolder(response, datastore,
  new
@@ -1593,23 +1593,16 @@
 derName,
- String
  newFold
@@ -1619,23 +1619,16 @@
 anguage,
- String
  folderK
@@ -1634,174 +1634,121 @@
 Key)
- throws IOException %7B%0A    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();%0A    Entity folderEntity = getExistingFolderInDatastore(response,
+;%0A      %7D%0A    %7D%0A  %7D%0A%0A  private void updateFolder(%0A        HttpServletResponse response, %0A        DatastoreService
  dat
@@ -1759,150 +1759,127 @@
 re, 
-folderKey);
 %0A    
-%0A
     
-if (folderEntity == null) %7B%0A        ResponseHandler.sendErrorMessage(response, %22Cannot edit Folder at the moment%22);%0A    %7D
+String newFolderName,%0A        String newFolderDefaultLanguage, %0A        String folderKey) throws IOException %7B
 %0A   

Fix logical ordering