<commit_before>/*
 * Copyright 2012-2013 Continuuity,Inc. All Rights Reserved.
 */

package com.continuuity.passport.dal.db;

import com.continuuity.common.db.DBConnectionPoolManager;
import com.continuuity.passport.core.exceptions.StaleNonceException;
import com.continuuity.passport.core.utils.NonceUtils;
import com.continuuity.passport.dal.NonceDAO;
import com.google.common.base.Throwables;
import com.google.inject.Inject;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

/**
 *
 */
public class NonceDBAccess extends DBAccess implements NonceDAO {

  private final DBConnectionPoolManager poolManager;
  private static final int SHORT_EXPIRATION_MILLS = 1000 * 60 * 10;
  private static final int LONG_EXPIRATION_MILLIS = 1000 * 60 * 60 * 24 * 3;


  @Inject
  public NonceDBAccess(DBConnectionPoolManager poolManager) {
    this.poolManager = poolManager;
  }

  /**
   * Generate a random nonce and update in DB.
   * @param id id
   * @param expiration expiration time in seconds
   * @return integer random nonce
   */
  private int updateRandomNonce(String id, int expiration) {
    int nonce = NonceUtils.getNonce();
    try {
      insertOrUpdateNonce(id, expiration, nonce);
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
    return nonce;
  }

  /**
   * Generate a hashed nonce and update in DB.
   * @param id id
   * @param expiration expiration time in seconds
   * @return integer random nonce
   */
  private int updateHashedNonce(String id, int expiration) {
    int nonce = NonceUtils.getNonce(id);
    try {
      insertOrUpdateNonce(id, expiration, nonce);
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
    return nonce;
  }

  /**
   * Insert or update Nonce in DB.
   * @param id id
   * @param expiration expiration time in seconds
   * @param nonce nonce value to be updated
   * @return integer random nonce
   */
  private void insertOrUpdateNonce(String id, int expiration, int nonce) {
    Connection connection = this.poolManager.getValidConnection();;
    String sql = String.format("SELECT COUNT(*) FROM %s where %s = ?",
                               DBUtils.Nonce.TABLE_NAME, DBUtils.Nonce.ID_COLUMN);
    try {
      PreparedStatement ps = connection.prepareStatement(sql);
      try {
        ps.setString(1, id);
        ResultSet rs = ps.executeQuery();
        if (rs.next()){<commit_msg>Remove return javadoc<commit_after>/*
 * Copyright 2012-2013 Continuuity,Inc. All Rights Reserved.
 */

package com.continuuity.passport.dal.db;

import com.continuuity.common.db.DBConnectionPoolManager;
import com.continuuity.passport.core.exceptions.StaleNonceException;
import com.continuuity.passport.core.utils.NonceUtils;
import com.continuuity.passport.dal.NonceDAO;
import com.google.common.base.Throwables;
import com.google.inject.Inject;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

/**
 *
 */
public class NonceDBAccess extends DBAccess implements NonceDAO {

  private final DBConnectionPoolManager poolManager;
  private static final int SHORT_EXPIRATION_MILLS = 1000 * 60 * 10;
  private static final int LONG_EXPIRATION_MILLIS = 1000 * 60 * 60 * 24 * 3;


  @Inject
  public NonceDBAccess(DBConnectionPoolManager poolManager) {
    this.poolManager = poolManager;
  }

  /**
   * Generate a random nonce and update in DB.
   * @param id id
   * @param expiration expiration time in seconds
   * @return integer random nonce
   */
  private int updateRandomNonce(String id, int expiration) {
    int nonce = NonceUtils.getNonce();
    try {
      insertOrUpdateNonce(id, expiration, nonce);
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
    return nonce;
  }

  /**
   * Generate a hashed nonce and update in DB.
   * @param id id
   * @param expiration expiration time in seconds
   * @return integer random nonce
   */
  private int updateHashedNonce(String id, int expiration) {
    int nonce = NonceUtils.getNonce(id);
    try {
      insertOrUpdateNonce(id, expiration, nonce);
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
    return nonce;
  }

  /**
   * Insert or update Nonce in DB.
   * @param id id
   * @param expiration expiration time in seconds
   * @param nonce nonce value to be updated
   */
  private void insertOrUpdateNonce(String id, int expiration, int nonce) {
    Connection connection = this.poolManager.getValidConnection();;
    String sql = String.format("SELECT COUNT(*) FROM %s where %s = ?",
                               DBUtils.Nonce.TABLE_NAME, DBUtils.Nonce.ID_COLUMN);
    try {
      PreparedStatement ps = connection.prepareStatement(sql);
      try {
        ps.setString(1, id);
        ResultSet rs = ps.executeQuery();
        if (rs.next()){
{'fraction_non_alphanumeric': 0.0955852571891454, 'fraction_numerical': 0.011340623734305387, 'mean_word_length': 3.3286590709903594, 'pattern_counts': {'":': 0, '<': 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': '3874546', 'n_tokens_mistral': 1609, 'n_tokens_neox': 1516, 'n_words': 411}