<commit_before>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ncedu.tlt.servlet;

import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ru.ncedu.tlt.controllers.RoleController;
import ru.ncedu.tlt.controllers.UserController;
import ru.ncedu.tlt.entity.User;
import ru.ncedu.tlt.entity.UserRole;

/**
 *
 * @author pavel.tretyakov
 */
@WebServlet(name = "UserAdministrationServlet", urlPatterns = {"/userProcess/*"})
public class UserControllerServlet extends HttpServlet {

    @EJB
    UserController userController;
    @EJB
    RoleController roleController;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        executeCommand(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        executeCommand(request, response);
    }

    protected void executeCommand(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String userManageRequest = request.getRequestURI().split("/")[request.getRequestURI().split("/").length - 1];
        response.setContentType("text/html");
        PrintWriter rs = response.getWriter();
        switch (userManageRequest) {
            case "getAllUsers": {
                Gson gson = new Gson();
                try {
                    String json = gson.toJson(userController.getAllUsers());
                    rs.print(json);
                } catch (SQLException ex) {
                    System.out.println("UserControllerServlet, getAllUsers: "+ex);
                }
                break;
            }
            case "getUserData": {
                Integer userId = Integer.valueOf(request.getParameter("userId"));
                User user;
                Gson gson = new Gson();
                try {
                    user = userController.findUser(userId);
                    rs.print(gson.toJson(user));
                } catch (SQLException ex) {
                    System.out.println("UserControllerServlet, getUserData: "+ex);
                }
                break;
            }
            case "getUserLoggedData": {
                Integer userId = (Integer) request.getSession().getAttribute("userId");
                User user;
                Gson gson = new Gson();
                try {
                    user = userController.findUser(userId);
                    rs.print(gson.toJson(user));<commit_msg>добавил метод получения списка юзеров имеющих доступ к файлу<commit_after>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ncedu.tlt.servlet;

import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ru.ncedu.tlt.controllers.RoleController;
import ru.ncedu.tlt.controllers.UserController;
import ru.ncedu.tlt.entity.User;
import ru.ncedu.tlt.entity.UserRole;

/**
 *
 * @author pavel.tretyakov
 */
@WebServlet(name = "UserAdministrationServlet", urlPatterns = {"/userProcess/*"})
public class UserControllerServlet extends HttpServlet {

    @EJB
    UserController userController;
    @EJB
    RoleController roleController;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        executeCommand(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        executeCommand(request, response);
    }

    protected void executeCommand(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String userManageRequest = request.getRequestURI().split("/")[request.getRequestURI().split("/").length - 1];
        response.setContentType("text/html");
        PrintWriter rs = response.getWriter();
        switch (userManageRequest) {
            case "getAllUsers": {
                Gson gson = new Gson();
                try {
                    String json = gson.toJson(userController.getAllUsers());
                    rs.print(json);
                } catch (SQLException ex) {
                    System.out.println("UserControllerServlet, getAllUsers: "+ex);
                }
                break;
            }
            case "getUserData": {
                Integer userId = Integer.valueOf(request.getParameter("userId"));
                User user;
                Gson gson = new Gson();
                try {
                    user = userController.findUser(userId);
                    rs.print(gson.toJson(user));
                } catch (SQLException ex) {
                    System.out.println("UserControllerServlet, getUserData: "+ex);
                }
                break;
            }
            case "getFileUsers": {
                Gson gson = new Gson();
                Integer fileId = Integer.valueOf(request.getParameter("fileId"));
                try {
                    String json = gson.toJson(userController.getUsersByFile(fileId));
                    rs.print(json);
                } catch (Exception ex) {
                    System.out.println("UserControllerServlet, getFileUsers: "+ex);
                }
                break;
            }
            case "getUserLoggedData": {
                Integer userId = (Integer) request.getSession().getAttribute("userId");
                User user;
                Gson gson = new Gson();
                try {
                    user = userController.findUser(userId);
                    rs.print(gson.toJson(user));
{'fraction_non_alphanumeric': 0.09689695550351288, 'fraction_numerical': 0.0008782201405152225, 'mean_word_length': 2.2476235741444865, 'pattern_counts': {'":': 7, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3318144', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1725, 'n_words': 382}