<commit_before>/**
 * Copyright 2015 LinkedIn Corp. All rights reserved.
 *
 * 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
 *
 * http://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.
 */
package controllers.api.v1;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import controllers.Application;
import dao.AbstractMySQLOpenSourceDAO;
import dao.DatasetsDAO;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import play.Logger;
import play.cache.Cache;
import play.libs.F.Promise;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
import wherehows.dao.table.DatasetClassificationDao;
import wherehows.dao.table.DatasetsDao;
import wherehows.dao.view.DatasetViewDao;
import wherehows.dao.view.OwnerViewDao;
import wherehows.models.table.DatasetClassification;
import wherehows.models.view.DatasetColumn;
import wherehows.models.table.DatasetCompliance;
import wherehows.models.table.DatasetDependency;
import wherehows.models.view.DatasetOwner;
import wherehows.models.table.ImpactDataset;


public class Dataset extends Controller {
  private static final JdbcTemplate JDBC_TEMPLATE = AbstractMySQLOpenSourceDAO.getJdbcTemplate();

  private static final NamedParameterJdbcTemplate NAMED_JDBC_TEMPLATE =
      AbstractMySQLOpenSourceDAO.getNamedParameterJdbcTemplate();

  private static final DatasetsDao DATASETS_DAO = Application.DAO_FACTORY.getDatasetsDao();

  private static final DatasetClassificationDao CLASSIFICATION_DAO =
      Application.DAO_FACTORY.getDatasetClassificationDao();

  private static final DatasetViewDao DATASET_VIEW_DAO = Application.DAO_FACTORY.getDatasetViewDao();

  private static final OwnerViewDao OWNER_VIEW_DAO = Application.DAO_FACTORY.getOwnerViewDao();

  private static final String URN_CACHE_KEY = "wh.urn.cache.";
  private static final int URN_CACHE_PERIOD = 24 * 3600; // cache for 24 hours

  public static Result getDatasetOwnerTypes() {
    ObjectNode result = Json.newObject();

    result.put("status", "ok");
    result.set("ownerTypes", Json.toJson(DatasetsDAO.getDatasetOwnerTypes()));
    return ok(result);
  }

  public static Result getPagedDatasets() {
    ObjectNode result = Json.newObject();
    String urn = request().getQueryString("urn");

    int page = 1;
    String pageStr = request().getQueryString("page");
    if (StringUtils.isBlank(pageStr)) {
      page = 1;
    } else {
      try {
        page = Integer.parseInt(pageStr);
      } catch (NumberFormatException e) {
        Logger.error("Dataset Controller getPagedDatasets wrong page parameter. Error message: " + e.getMessage());
        page = 1;
      }
    }

    int size = 10;
    String sizeStr = request().getQueryString("size");
    if (StringUtils.isBlank(sizeStr)) {
      size = 10;
    } else {
      try {
        size = Integer.parseInt(sizeStr);
      } catch (NumberFormatException e) {
        Logger.error("Dataset Controller getPagedDatasets wrong size parameter. Error message: " + e.getMessage());
        size = 10;
      }
    }

    result.put("status", "ok");
    String username = session("user");
    result.set("data", DatasetsDAO.getPagedDatasets(urn, page, size, username));
    return ok(result);
  }

  public static Result getDatasetByID(int id) {
    String username = session("user");
    wherehows.models.table.Dataset dataset = DatasetsDAO.getDatasetByID(id, username);

    ObjectNode result = Json.newObject();

    if (dataset != null) {
      result.put("status", "ok");
      result.set("dataset", Json.toJson(dataset));
    } else {
      result.put("status", "error");
      result.put("message", "record not found");
    }

    return ok(result);
  }

  public static Result getDatasetIdByUrn(String urn) {
    String cacheKey = URN_CACHE_KEY + urn;

    Integer datasetId = (Integer) Cache.get(cacheKey);
    if (datasetId != null && datasetId > 0) {
      response().setHeader("DatasetId", datasetId.toString());
      return ok();
    }

    datasetId = DATASETS_DAO.getDatasetIdByUrn(JDBC_TEMPLATE, urn);
    if (datasetId > 0) {
      Cache.set(cacheKey, datasetId, URN_CACHE_PERIOD);
      response().setHeader("DatasetId", datasetId.toString());
      return ok();
    } else {
      return notFound();
    }
  }

  public static Result getDatasetColumnByID(int datasetId, int columnId) {
    List<DatasetColumn> columns = DATASET_VIEW_DAO.getDatasetColumnByID(datasetId, columnId);

    ObjectNode result = Json.newObject();

    if (columns != null && columns.size() > 0) {
      result.put("status", "ok");
      result.set("columns", Json.toJson(columns));
    } else {<commit_msg>change DatasetIdByURN response key to lowercase (#760)<commit_after>/**
 * Copyright 2015 LinkedIn Corp. All rights reserved.
 *
 * 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
 *
 * http://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.
 */
package controllers.api.v1;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import controllers.Application;
import dao.AbstractMySQLOpenSourceDAO;
import dao.DatasetsDAO;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import play.Logger;
import play.cache.Cache;
import play.libs.F.Promise;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
import wherehows.dao.table.DatasetClassificationDao;
import wherehows.dao.table.DatasetsDao;
import wherehows.dao.view.DatasetViewDao;
import wherehows.dao.view.OwnerViewDao;
import wherehows.models.table.DatasetClassification;
import wherehows.models.view.DatasetColumn;
import wherehows.models.table.DatasetCompliance;
import wherehows.models.table.DatasetDependency;
import wherehows.models.view.DatasetOwner;
import wherehows.models.table.ImpactDataset;


public class Dataset extends Controller {
  private static final JdbcTemplate JDBC_TEMPLATE = AbstractMySQLOpenSourceDAO.getJdbcTemplate();

  private static final NamedParameterJdbcTemplate NAMED_JDBC_TEMPLATE =
      AbstractMySQLOpenSourceDAO.getNamedParameterJdbcTemplate();

  private static final DatasetsDao DATASETS_DAO = Application.DAO_FACTORY.getDatasetsDao();

  private static final DatasetClassificationDao CLASSIFICATION_DAO =
      Application.DAO_FACTORY.getDatasetClassificationDao();

  private static final DatasetViewDao DATASET_VIEW_DAO = Application.DAO_FACTORY.getDatasetViewDao();

  private static final OwnerViewDao OWNER_VIEW_DAO = Application.DAO_FACTORY.getOwnerViewDao();

  private static final String URN_CACHE_KEY = "wh.urn.cache.";
  private static final int URN_CACHE_PERIOD = 24 * 3600; // cache for 24 hours

  public static Result getDatasetOwnerTypes() {
    ObjectNode result = Json.newObject();

    result.put("status", "ok");
    result.set("ownerTypes", Json.toJson(DatasetsDAO.getDatasetOwnerTypes()));
    return ok(result);
  }

  public static Result getPagedDatasets() {
    ObjectNode result = Json.newObject();
    String urn = request().getQueryString("urn");

    int page = 1;
    String pageStr = request().getQueryString("page");
    if (StringUtils.isBlank(pageStr)) {
      page = 1;
    } else {
      try {
        page = Integer.parseInt(pageStr);
      } catch (NumberFormatException e) {
        Logger.error("Dataset Controller getPagedDatasets wrong page parameter. Error message: " + e.getMessage());
        page = 1;
      }
    }

    int size = 10;
    String sizeStr = request().getQueryString("size");
    if (StringUtils.isBlank(sizeStr)) {
      size = 10;
    } else {
      try {
        size = Integer.parseInt(sizeStr);
      } catch (NumberFormatException e) {
        Logger.error("Dataset Controller getPagedDatasets wrong size parameter. Error message: " + e.getMessage());
        size = 10;
      }
    }

    result.put("status", "ok");
    String username = session("user");
    result.set("data", DatasetsDAO.getPagedDatasets(urn, page, size, username));
    return ok(result);
  }

  public static Result getDatasetByID(int id) {
    String username = session("user");
    wherehows.models.table.Dataset dataset = DatasetsDAO.getDatasetByID(id, username);

    ObjectNode result = Json.newObject();

    if (dataset != null) {
      result.put("status", "ok");
      result.set("dataset", Json.toJson(dataset));
    } else {
      result.put("status", "error");
      result.put("message", "record not found");
    }

    return ok(result);
  }

  public static Result getDatasetIdByUrn(String urn) {
    String cacheKey = URN_CACHE_KEY + urn;

    Integer datasetId = (Integer) Cache.get(cacheKey);
    if (datasetId != null && datasetId > 0) {
      response().setHeader("datasetid", datasetId.toString());
      return ok();
    }

    datasetId = DATASETS_DAO.getDatasetIdByUrn(JDBC_TEMPLATE, urn);
    if (datasetId > 0) {
      Cache.set(cacheKey, datasetId, URN_CACHE_PERIOD);
      response().setHeader("datasetid", datasetId.toString());
      return ok();
    } else {
      return notFound();
    }
  }

  public static Result getDatasetColumnByID(int datasetId, int columnId) {
    List<DatasetColumn> columns = DATASET_VIEW_DAO.getDatasetColumnByID(datasetId, columnId);

    ObjectNode result = Json.newObject();

    if (columns != null && columns.size() > 0) {
      result.put("status", "ok");
      result.set("columns", Json.toJson(columns));
    } else {
{'fraction_non_alphanumeric': 0.10929313356006987, 'fraction_numerical': 0.005974813861568159, 'mean_word_length': 4.14907714150497, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 10}, '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': '2556211', 'n_tokens_mistral': 3373, 'n_tokens_neox': 3290, 'n_words': 766}