// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.sps.servlets;

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.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;

@WebServlet("/Info")
public class InfoServlet extends HttpServlet {
  // The class stores the user' personal information data
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    HttpSession session = request.getSession();
    String firstName = request.getParameter("firstName");
    session.setAttribute("firstName", firstName);
    String lastName = request.getParameter("lastName");
    session.setAttribute("lastName", lastName);
    int dayBirth = Integer.parseInt(request.getParameter("dayBirth"));
    session.setAttribute("dayBirth", dayBirth);
    int monthBirth = Integer.parseInt(request.getParameter("monthBirth"));
    session.setAttribute("monthBirth", monthBirth);
    int yearBirth = Integer.parseInt(request.getParameter("yearBirth"));
    session.setAttribute("yearBirth", yearBirth);

    UserService userService = UserServiceFactory.getUserService();
    String email = userService.getCurrentUser().getEmail();
    String id = userService.getCurrentUser().getUserId();

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Query query =
        new Query("UserInfo")
            .setFilter(new Query.FilterPredicate("id", Query.FilterOperator.EQUAL, id));
    PreparedQuery results = datastore.prepare(query);
    Entity entity = results.asSingleEntity();
    entity.setProperty("firstName", firstName);
    entity.setProperty("lastName", lastName);
    entity.setProperty("dayBirth", dayBirth);
    entity.setProperty("monthBirth", monthBirth);
    entity.setProperty("yearBirth", yearBirth);
    datastore.put(entity);

    // RequestDispatcher requestDispatcher = request.getRequestDispatcher("profile.jsp");
    // requestDispatcher.forward(request, response);
    response.sendRedirect("prefForm.jsp");
  }
}

/** Personal Information: 
    Date/Month/Year: Allow people to put 2-digit numbers only
    Picture: Allow users to add pictures
    Gender: Choose among different gender options
    Organization/ Country
*/

// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.sps.servlets;

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.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;

@WebServlet("/Info")
public class InfoServlet extends HttpServlet {
  // The class stores the user' personal information data
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    HttpSession session = request.getSession();
    String firstName = request.getParameter("firstName");
    session.setAttribute("firstName", firstName);
    String lastName = request.getParameter("lastName");
    session.setAttribute("lastName", lastName);
    int dayBirth = Integer.parseInt(request.getParameter("dayBirth"));
    session.setAttribute("dayBirth", dayBirth);
    int monthBirth = Integer.parseInt(request.getParameter("monthBirth"));
    session.setAttribute("monthBirth", monthBirth);
    int yearBirth = Integer.parseInt(request.getParameter("yearBirth"));
    session.setAttribute("yearBirth", yearBirth);

    UserService userService = UserServiceFactory.getUserService();
    String email = userService.getCurrentUser().getEmail();
    String id = userService.getCurrentUser().getUserId();

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Query query =
        new Query("UserInfo")
            .setFilter(new Query.FilterPredicate("id", Query.FilterOperator.EQUAL, id));
    PreparedQuery results = datastore.prepare(query);
    Entity entity = results.asSingleEntity();
    entity.setProperty("firstName", firstName);
    entity.setProperty("lastName", lastName);
    entity.setProperty("dayBirth", dayBirth);
    entity.setProperty("monthBirth", monthBirth);
    entity.setProperty("yearBirth", yearBirth);
    datastore.put(entity);
    response.sendRedirect("prefForm.jsp");
  }
}

/** Personal Information: 
    Date/Month/Year: Allow people to put 2-digit numbers only
    Picture: Allow users to add pictures
    Gender: Choose among different gender options
    Organization/ Country
*/

Update InfoServlet.java