<commit_before>// 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.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.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.ArrayList;
import java.util.HashSet;
import com.google.gson.Gson;
import java.io.PrintWriter;
import com.google.sps.Objects.User;
import com.google.sps.Objects.Group;

/**
 * Servlet to handle returning User data from the Datastore.
 */
@WebServlet("/user")
public class UserDataServlet extends HttpServlet {

  /**
   * Gets User data from the Datastore and returns it.
   */
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    UserService userService = UserServiceFactory.getUserService();
    if (userService.isUserLoggedIn()) {
      String userId = userService.getCurrentUser().getUserId();
    } else {
      // display error - this shouldn't happen
    }

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Key entityKey = KeyFactory.createKey("User", userId);
    User currentUser;
    try {
      currentUser = getUserObject(datastore.get(entityKey));
    } catch (EntityNotFoundException e) {
      // If the user doesn't exist in Datastore - display error, this shouldn't happen
    }

    // Convert to json
    response.setContentType("application/json;");
    response.getWriter().println(new Gson().toJson(currentUser));
  }

  /**
   * Creates and returns a User object given a user Entity.
   */
  public User getUserObject(Entity entity) {
    String userId = (String) entity.getProperty("userId");
    String firstName = (String) entity.getProperty("firstName");
    String lastName = (String) entity.getProperty("lastName");
    String email = (String) entity.getProperty("email");
    String phoneNumber = (String) entity.getProperty("phoneNumber");
    String profilePic = ""; // todo: add profilePic url to datastore/figure out Blobstore
    ArrayList<String> interests = (ArrayList<String>) entity.getProperty("interests");

    LinkedHashSet<String> badgeIds = (entity.getProperty("badges") == null)
        ? new LinkedHashSet<>()
        : new LinkedHashSet<String>((ArrayList<String>) entity.getProperty("badges"));
    LinkedHashSet<String> groupIds = (entity.getProperty("groups") == null)
        ? new LinkedHashSet<>()<commit_msg>Basic functionality for user data servlet<commit_after>// 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.sps.Objects.User;
import com.google.sps.Objects.Group;
import com.google.sps.Objects.Badge;
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.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.EntityNotFoundException;
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 java.util.List;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import com.google.gson.Gson;

/**
 * Servlet to handle returning User data from the Datastore.
 */
@WebServlet("/user")
public class UserDataServlet extends HttpServlet {

  /**
   * Gets User data from the Datastore and returns it.
   */
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    UserService userService = UserServiceFactory.getUserService();
    User currentUser = null;

    if (userService.isUserLoggedIn()) {
      String userId = userService.getCurrentUser().getUserId();
      DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
      Key entityKey = KeyFactory.createKey("User", userId);
      try {
        currentUser = getUserObject(datastore.get(entityKey));
      }   catch (EntityNotFoundException e) {
        // display error, this shouldn't happen
      }
    } else {
      // display error - this shouldn't happen
    }

    // Convert to json
    response.setContentType("application/json;");
    response.getWriter().println(new Gson().toJson(currentUser));
  }

  /**
   * Creates and returns a User object given a user Entity.
   */
  public User getUserObject(Entity entity) {
    String userId = (String) entity.getProperty("userId");
    String firstName = (String) entity.getProperty("firstName");
    String lastName = (String) entity.getProperty("lastName");
    String email = (String) entity.getProperty("email");
    String phoneNumber = (String) entity.getProperty("phoneNumber");
    String profilePic = ""; // todo: add profilePic url to datastore/figure out Blobstore
    ArrayList<String> interests = (ArrayList<String>) entity.getProperty("interests");

    LinkedHashSet<String> badgeIds = (entity.getProperty("badges") == null)
        ? new LinkedHashSet<>()
        : new LinkedHashSet<String>((ArrayList<String>) entity.getProperty("badges"));
    LinkedHashSet<String> groupIds = (entity.getProperty("groups") == null)
        ? new LinkedHashSet<>()
{'fraction_non_alphanumeric': 0.10965272080940662, 'fraction_numerical': 0.0021875854525567405, 'mean_word_length': 4.339416058394161, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6615008', 'n_tokens_mistral': 2028, 'n_tokens_neox': 2034, 'n_words': 546}