<commit_before>package cz.cuni.mff.xrg.odalic.api.rest.resources;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.NavigableSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.ws.rs.BadRequestException;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Response.Status;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.base.Preconditions;
import cz.cuni.mff.xrg.odalic.api.rdf.TaskRdfSerializationService;
import cz.cuni.mff.xrg.odalic.api.rest.Secured;
import cz.cuni.mff.xrg.odalic.api.rest.responses.Message;
import cz.cuni.mff.xrg.odalic.api.rest.responses.Reply;
import cz.cuni.mff.xrg.odalic.api.rest.util.Security;
import cz.cuni.mff.xrg.odalic.api.rest.values.ConfigurationValue;
import cz.cuni.mff.xrg.odalic.api.rest.values.StatefulTaskValue;
import cz.cuni.mff.xrg.odalic.api.rest.values.TaskValue;
import cz.cuni.mff.xrg.odalic.api.rest.values.util.States;
import cz.cuni.mff.xrg.odalic.bases.BasesService;
import cz.cuni.mff.xrg.odalic.files.File;
import cz.cuni.mff.xrg.odalic.files.FileService;
import cz.cuni.mff.xrg.odalic.tasks.Task;
import cz.cuni.mff.xrg.odalic.tasks.TaskService;
import cz.cuni.mff.xrg.odalic.tasks.annotations.KnowledgeBase;
import cz.cuni.mff.xrg.odalic.tasks.configurations.Configuration;
import cz.cuni.mff.xrg.odalic.tasks.executions.ExecutionService;
import cz.cuni.mff.xrg.odalic.users.Role;
import cz.cuni.mff.xrg.odalic.users.UserService;

/**
 * Task resource definition.
 * 
 * @author <NAME>
 */
@Component
@Path("/")
@Secured({Role.ADMINISTRATOR, Role.USER})
public final class TasksResource {

  private static final String TURTLE_MIME_TYPE = "text/turtle";

  private final UserService userService;
  private final TaskService taskService;
  private final FileService fileService;
  private final ExecutionService executionService;
  private final BasesService basesService;
  private final TaskRdfSerializationService taskSerializationService;

  @Context
  private SecurityContext securityContext;

  @Context
  private UriInfo uriInfo;

  @Autowired
  public TasksResource(final UserService userService, final TaskService taskService,
      final FileService fileService, final ExecutionService executionService,
      final BasesService basesService, final TaskRdfSerializationService taskSerializationService) {
    Preconditions.checkNotNull(userService);
    Preconditions.checkNotNull(taskService);
    Preconditions.checkNotNull(fileService);
    Preconditions.checkNotNull(executionService);
    Preconditions.checkNotNull(basesService);
    Preconditions.checkNotNull(taskSerializationService);

    this.userService = userService;
    this.taskService = taskService;
    this.fileService = fileService;
    this.executionService = executionService;
    this.basesService = basesService;
    this.taskSerializationService = taskSerializationService;
  }

  @GET
  @Path("users/{userId}/tasks")
  @Produces(MediaType.APPLICATION_JSON)
  public Response getTasks(final @PathParam("userId") String userId,
      final @QueryParam("states") Boolean states, final @QueryParam("orderedBy") String orderedBy) {
    Security.checkAuthorization(securityContext, userId);

    final NavigableSet<Task> tasks;
    if (orderedBy == null) {
      tasks = taskService.getTasksSortedByIdInAscendingOrder(userId);
    } else {
      switch (orderedBy) {
        case "id":
          tasks = taskService.getTasksSortedByIdInAscendingOrder(userId);
          break;
        case "created":
          tasks = taskService.getTasksSortedByCreatedInDescendingOrder(userId);
        default:
          throw new BadRequestException("Invalid sorting key!");
      }
    }

    if (states == null || (!states)) {
      return Reply.data(Response.Status.OK, tasks, uriInfo).toResponse();
    } else {
      final Stream<StatefulTaskValue> statefulTasksStream = tasks.stream()
          .map(e -> new StatefulTaskValue(e, States.queryStateValue(executionService, e)));

      return Reply
          .data(Response.Status.OK, statefulTasksStream.collect(Collectors.toList()), uriInfo)
          .toResponse(); // List is fine, as it serializes in the same way, and no monkeying with
                         // comparators is needed.
    }
  }

  @GET
  @Path("tasks")
  @Produces(MediaType.APPLICATION_JSON)
  public Response getTasks(final @QueryParam("states") Boolean states,
      final @QueryParam("orderedBy") String orderedBy) {
    return getTasks(securityContext.getUserPrincipal().getName(), states, orderedBy);
  }

  @GET
  @Path("users/{userId}/tasks/{taskId}")
  @Produces(MediaType.APPLICATION_JSON)
  public Response getTaskById(final @PathParam("userId") String userId,<commit_msg>Forgotten switch break for task ordering added.<commit_after>package cz.cuni.mff.xrg.odalic.api.rest.resources;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.NavigableSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.ws.rs.BadRequestException;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Response.Status;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.base.Preconditions;
import cz.cuni.mff.xrg.odalic.api.rdf.TaskRdfSerializationService;
import cz.cuni.mff.xrg.odalic.api.rest.Secured;
import cz.cuni.mff.xrg.odalic.api.rest.responses.Message;
import cz.cuni.mff.xrg.odalic.api.rest.responses.Reply;
import cz.cuni.mff.xrg.odalic.api.rest.util.Security;
import cz.cuni.mff.xrg.odalic.api.rest.values.ConfigurationValue;
import cz.cuni.mff.xrg.odalic.api.rest.values.StatefulTaskValue;
import cz.cuni.mff.xrg.odalic.api.rest.values.TaskValue;
import cz.cuni.mff.xrg.odalic.api.rest.values.util.States;
import cz.cuni.mff.xrg.odalic.bases.BasesService;
import cz.cuni.mff.xrg.odalic.files.File;
import cz.cuni.mff.xrg.odalic.files.FileService;
import cz.cuni.mff.xrg.odalic.tasks.Task;
import cz.cuni.mff.xrg.odalic.tasks.TaskService;
import cz.cuni.mff.xrg.odalic.tasks.annotations.KnowledgeBase;
import cz.cuni.mff.xrg.odalic.tasks.configurations.Configuration;
import cz.cuni.mff.xrg.odalic.tasks.executions.ExecutionService;
import cz.cuni.mff.xrg.odalic.users.Role;
import cz.cuni.mff.xrg.odalic.users.UserService;

/**
 * Task resource definition.
 * 
 * @author <NAME>
 */
@Component
@Path("/")
@Secured({Role.ADMINISTRATOR, Role.USER})
public final class TasksResource {

  private static final String TURTLE_MIME_TYPE = "text/turtle";

  private final UserService userService;
  private final TaskService taskService;
  private final FileService fileService;
  private final ExecutionService executionService;
  private final BasesService basesService;
  private final TaskRdfSerializationService taskSerializationService;

  @Context
  private SecurityContext securityContext;

  @Context
  private UriInfo uriInfo;

  @Autowired
  public TasksResource(final UserService userService, final TaskService taskService,
      final FileService fileService, final ExecutionService executionService,
      final BasesService basesService, final TaskRdfSerializationService taskSerializationService) {
    Preconditions.checkNotNull(userService);
    Preconditions.checkNotNull(taskService);
    Preconditions.checkNotNull(fileService);
    Preconditions.checkNotNull(executionService);
    Preconditions.checkNotNull(basesService);
    Preconditions.checkNotNull(taskSerializationService);

    this.userService = userService;
    this.taskService = taskService;
    this.fileService = fileService;
    this.executionService = executionService;
    this.basesService = basesService;
    this.taskSerializationService = taskSerializationService;
  }

  @GET
  @Path("users/{userId}/tasks")
  @Produces(MediaType.APPLICATION_JSON)
  public Response getTasks(final @PathParam("userId") String userId,
      final @QueryParam("states") Boolean states, final @QueryParam("orderedBy") String orderedBy) {
    Security.checkAuthorization(securityContext, userId);

    final NavigableSet<Task> tasks;
    if (orderedBy == null) {
      tasks = taskService.getTasksSortedByIdInAscendingOrder(userId);
    } else {
      switch (orderedBy) {
        case "id":
          tasks = taskService.getTasksSortedByIdInAscendingOrder(userId);
          break;
        case "created":
          tasks = taskService.getTasksSortedByCreatedInDescendingOrder(userId);
          break;
        default:
          throw new BadRequestException("Invalid sorting key!");
      }
    }

    if (states == null || (!states)) {
      return Reply.data(Response.Status.OK, tasks, uriInfo).toResponse();
    } else {
      final Stream<StatefulTaskValue> statefulTasksStream = tasks.stream()
          .map(e -> new StatefulTaskValue(e, States.queryStateValue(executionService, e)));

      return Reply
          .data(Response.Status.OK, statefulTasksStream.collect(Collectors.toList()), uriInfo)
          .toResponse(); // List is fine, as it serializes in the same way, and no monkeying with
                         // comparators is needed.
    }
  }

  @GET
  @Path("tasks")
  @Produces(MediaType.APPLICATION_JSON)
  public Response getTasks(final @QueryParam("states") Boolean states,
      final @QueryParam("orderedBy") String orderedBy) {
    return getTasks(securityContext.getUserPrincipal().getName(), states, orderedBy);
  }

  @GET
  @Path("users/{userId}/tasks/{taskId}")
  @Produces(MediaType.APPLICATION_JSON)
  public Response getTaskById(final @PathParam("userId") String userId,
{'fraction_non_alphanumeric': 0.10990052108005685, 'fraction_numerical': 0.0, 'mean_word_length': 4.512271540469974, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 11, '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': '5047267', 'n_tokens_mistral': 3344, 'n_tokens_neox': 3297, 'n_words': 592}