<commit_before>package pl.project13.janbanery.core.dao;

import com.google.common.base.Predicate;
import com.google.gson.Gson;
import com.ning.http.client.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.project13.janbanery.config.Configuration;
import pl.project13.janbanery.config.gson.GsonTypeTokens;
import pl.project13.janbanery.core.RestClient;
import pl.project13.janbanery.core.flow.*;
import pl.project13.janbanery.exceptions.NotYetImplementedException;
import pl.project13.janbanery.exceptions.kanbanery.CanNotDeleteNotEmptyColumnException;
import pl.project13.janbanery.resources.*;
import pl.project13.janbanery.resources.additions.TaskLocation;

import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.google.common.collect.Collections2.filter;
import static com.google.common.collect.Lists.newArrayList;

/**
 * @author <NAME>
 */
public class TasksImpl implements Tasks {

  private Logger log = LoggerFactory.getLogger(getClass());

  private Configuration conf;
  private RestClient    restClient;
  private Gson          gson;

  private Workspace currentWorkspace;
  private Project   currentProject;

  public TasksImpl(Configuration conf, RestClient restClient, Gson gson) {
    this.conf = conf;
    this.restClient = restClient;
    this.gson = gson;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public TaskFlow create(Task task) throws IOException {
    String url = getDefaultGetUrl();

    Task newTask = restClient.doPost(url, task, GsonTypeTokens.TASK);

    return new TaskFlowImpl(this, newTask);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void delete(Task task) throws IOException {
    String url = getTaskUrl(task);

    restClient.doDelete(url);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void archive(Task task) throws IOException {
    throw new NotYetImplementedException(); // todo
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void archiveAllInLastColumn() throws IOException, CanNotDeleteNotEmptyColumnException {
    throw new NotYetImplementedException(); // todo
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public TaskMoveFlow move(Task task) {
    return new TaskMoveFlowImpl(this, task);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public TaskFlow move(Task task, TaskLocation location) throws IOException {
    String url = getTaskUrl(task);
    String moveRequest = location.requestBody();

    Task movedTask = restClient.doPut(url, moveRequest, GsonTypeTokens.TASK);

    return new TaskFlowImpl(this, movedTask);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public TaskFlow move(Task task, Column column) throws IOException {
    String url = getTaskUrl(task);
    Task requestObject = new Task();
    requestObject.setColumnId(column.getId());

    Task movedTask = restClient.doPut(url, requestObject, GsonTypeTokens.TASK);

    return new TaskFlowImpl(this, movedTask);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public TaskUpdateFlow update(Task task) {
    return new TaskUpdateFlowImpl(this, task);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public TaskFlow update(Task task, Task newValues) throws IOException {
    String url = getTaskUrl(task);

    Task newTask = restClient.doPut(url, newValues, GsonTypeTokens.TASK);

    return new TaskFlowImpl(this, newTask);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public TaskMarkFlow mark(Task task) {
    return new TaskMarkFlowImpl(this, task);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public TaskFlow markAsReadyToPull(Task task) throws IOException {
    Task commandObject = new Task();
    commandObject.setReadyToPull(true);

    return update(task, commandObject);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public TaskFlow markAsNotReadyToPull(Task task) throws IOException {
    Task commandObject = new Task();
    commandObject.setReadyToPull(false);

    return update(task, commandObject);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public List<Task> all() throws IOException {
    // todo port this to *Flow
    String url = getDefaultGetUrl();

    Response response = restClient.doGet(url);
    String responseBody = response.getResponseBody();

    return gson.fromJson(responseBody, GsonTypeTokens.LIST_TASK);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public List<Task> allIn(Column column) throws IOException {
    String url = getColumnTasksUrl(column.getId());
    return restClient.doGet(url, GsonTypeTokens.LIST_TASK);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public Task byId(Long taskId) throws IOException {
    String url = getDefaultGetUrl();
    Response response = restClient.doGet(url);
    String responseBody = response.getResponseBody();

    return gson.fromJson(responseBody, GsonTypeTokens.TASK);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public List<Task> byTitle(String taskTitle) throws IOException {
    List<Task> tasks = all();
    Collection<Task> filteredTasks = filter(tasks, new TaskByTitlePredicate(taskTitle));
    return newArrayList(filteredTasks);
  }

  /**
   * {@inheritDoc}
   */<commit_msg>fixed find by id<commit_after>package pl.project13.janbanery.core.dao;

import com.google.common.base.Predicate;
import com.google.gson.Gson;
import com.ning.http.client.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.project13.janbanery.config.Configuration;
import pl.project13.janbanery.config.gson.GsonTypeTokens;
import pl.project13.janbanery.core.RestClient;
import pl.project13.janbanery.core.flow.*;
import pl.project13.janbanery.exceptions.EntityNotFoundException;
import pl.project13.janbanery.exceptions.NotYetImplementedException;
import pl.project13.janbanery.exceptions.kanbanery.CanNotDeleteNotEmptyColumnException;
import pl.project13.janbanery.resources.*;
import pl.project13.janbanery.resources.additions.TaskLocation;

import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.google.common.collect.Collections2.filter;
import static com.google.common.collect.Lists.newArrayList;

/**
 * @author <NAME>
 */
public class TasksImpl implements Tasks {

  private Logger log = LoggerFactory.getLogger(getClass());

  private Configuration conf;
  private RestClient    restClient;
  private Gson          gson;

  private Workspace currentWorkspace;
  private Project   currentProject;

  public TasksImpl(Configuration conf, RestClient restClient, Gson gson) {
    this.conf = conf;
    this.restClient = restClient;
    this.gson = gson;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public TaskFlow create(Task task) throws IOException {
    String url = getDefaultGetUrl();

    Task newTask = restClient.doPost(url, task, GsonTypeTokens.TASK);

    return new TaskFlowImpl(this, newTask);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void delete(Task task) throws IOException {
    String url = getTaskUrl(task);

    restClient.doDelete(url);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void archive(Task task) throws IOException {
    throw new NotYetImplementedException(); // todo
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void archiveAllInLastColumn() throws IOException, CanNotDeleteNotEmptyColumnException {
    throw new NotYetImplementedException(); // todo
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public TaskMoveFlow move(Task task) {
    return new TaskMoveFlowImpl(this, task);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public TaskFlow move(Task task, TaskLocation location) throws IOException {
    String url = getTaskUrl(task);
    String moveRequest = location.requestBody();

    Task movedTask = restClient.doPut(url, moveRequest, GsonTypeTokens.TASK);

    return new TaskFlowImpl(this, movedTask);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public TaskFlow move(Task task, Column column) throws IOException {
    String url = getTaskUrl(task);
    Task requestObject = new Task();
    requestObject.setColumnId(column.getId());

    Task movedTask = restClient.doPut(url, requestObject, GsonTypeTokens.TASK);

    return new TaskFlowImpl(this, movedTask);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public TaskUpdateFlow update(Task task) {
    return new TaskUpdateFlowImpl(this, task);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public TaskFlow update(Task task, Task newValues) throws IOException {
    String url = getTaskUrl(task);

    Task newTask = restClient.doPut(url, newValues, GsonTypeTokens.TASK);

    return new TaskFlowImpl(this, newTask);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public TaskMarkFlow mark(Task task) {
    return new TaskMarkFlowImpl(this, task);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public TaskFlow markAsReadyToPull(Task task) throws IOException {
    Task commandObject = new Task();
    commandObject.setReadyToPull(true);

    return update(task, commandObject);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public TaskFlow markAsNotReadyToPull(Task task) throws IOException {
    Task commandObject = new Task();
    commandObject.setReadyToPull(false);

    return update(task, commandObject);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public List<Task> all() throws IOException {
    // todo port this to *Flow
    String url = getDefaultGetUrl();

    Response response = restClient.doGet(url);
    String responseBody = response.getResponseBody();

    return gson.fromJson(responseBody, GsonTypeTokens.LIST_TASK);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public List<Task> allIn(Column column) throws IOException {
    String url = getColumnTasksUrl(column.getId());
    return restClient.doGet(url, GsonTypeTokens.LIST_TASK);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public Task byId(Long taskId) throws IOException {
    Task task = new Task();
    task.setId(taskId);
    String url = getTaskUrl(task);

    Response response = restClient.doGet(url);
    String responseBody = response.getResponseBody();

    return gson.fromJson(responseBody, GsonTypeTokens.TASK);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public List<Task> byTitle(String taskTitle) throws IOException {
    List<Task> tasks = all();
    Collection<Task> filteredTasks = filter(tasks, new TaskByTitlePredicate(taskTitle));
    return newArrayList(filteredTasks);
  }

  /**
   * {@inheritDoc}
   */
{'fraction_non_alphanumeric': 0.11312607944732297, 'fraction_numerical': 0.00422183841872961, 'mean_word_length': 3.7248413417951043, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '4938258', 'n_tokens_mistral': 3261, 'n_tokens_neox': 3178, 'n_words': 706}