package com.eactive.eai.authserver.provider;

import javax.sql.DataSource;

import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;

/**
 * ** Oracle DDL
 * @formatter:off
 * CRATE TABLE tseaiau05 (
 * 	   TOKEN_ID VARCHAR(255),
 * 	   TOKEN BLOB,
 * 	   AUTHENTICATION_ID VARCHAR(255) PRIMARY KEY,
 * 	   USER_NAME VARCHAR(255),
 * 	   CLIENT_ID VARCHAR(255),
 * 	   AUTHENTICATION BLOB,
 * 	   REFRESH_TOKEN VARCHAR(255),
 * 	   CREATE_TIME timestamp default systimestamp
 * );
 * 
 * CREATE TABLE tseaiau06 (
 * 	   TOKEN_ID VARCHAR(255),
 * 	   TOKEN BLOB,
 * 	   AUTHENTICATION BLOB,
 * 	   CREATE_TIME timestamp default systimestamp
 * );
 * 
 * CREATE INDEX IX_TSEAIAU05_01 ON TSEAIAU05 (CREATE_TIME);
 * CREATE INDEX IX_TSEAIAU06_01 ON TSEAIAU06 (CREATE_TIME);
 * @formatter:on
 */
public class ElinkJdbcTokenStore extends JdbcTokenStore {
	private static final String ELINK_ACCESS_TOKEN_INSERT_STATEMENT = "insert into tseaiau05 (token_id, token, authentication_id, user_name, client_id, authentication, refresh_token) values (?, ?, ?, ?, ?, ?, ?)";
	private static final String ELINK_ACCESS_TOKEN_SELECT_STATEMENT = "select token_id, token from tseaiau05 where token_id = ?";
	private static final String ELINK_ACCESS_TOKEN_AUTHENTICATION_SELECT_STATEMENT = "select token_id, authentication from tseaiau05 where token_id = ?";
	private static final String ELINK_ACCESS_TOKEN_FROM_AUTHENTICATION_SELECT_STATEMENT = "select token_id, token from tseaiau05 where authentication_id = ?";
	private static final String ELINK_ACCESS_TOKENS_FROM_USERNAME_AND_CLIENT_SELECT_STATEMENT = "select token_id, token from tseaiau05 where user_name = ? and client_id = ?";
	private static final String ELINK_ACCESS_TOKENS_FROM_USERNAME_SELECT_STATEMENT = "select token_id, token from tseaiau05 where user_name = ?";
	private static final String ELINK_ACCESS_TOKENS_FROM_CLIENTID_SELECT_STATEMENT = "select token_id, token from tseaiau05 where client_id = ?";
	private static final String ELINK_ACCESS_TOKEN_DELETE_STATEMENT = "delete from tseaiau05 where token_id = ?";
	private static final String ELINK_ACCESS_TOKEN_DELETE_FROM_REFRESH_TOKEN_STATEMENT = "delete from tseaiau05 where refresh_token = ?";
	private static final String ELINK_REFRESH_TOKEN_INSERT_STATEMENT = "insert into tseaiau06 (token_id, token, authentication) values (?, ?, ?)";
	private static final String ELINK_REFRESH_TOKEN_SELECT_STATEMENT = "select token_id, token from tseaiau06 where token_id = ?";
	private static final String ELINK_REFRESH_TOKEN_AUTHENTICATION_SELECT_STATEMENT = "select token_id, authentication from tseaiau06 where token_id = ?";
	private static final String ELINK_REFRESH_TOKEN_DELETE_STATEMENT = "delete from tseaiau06 where token_id = ?";
	private String insertAccessTokenSql = ELINK_ACCESS_TOKEN_INSERT_STATEMENT;
	private String selectAccessTokenSql = ELINK_ACCESS_TOKEN_SELECT_STATEMENT;
	private String selectAccessTokenAuthenticationSql = ELINK_ACCESS_TOKEN_AUTHENTICATION_SELECT_STATEMENT;
	private String selectAccessTokenFromAuthenticationSql = ELINK_ACCESS_TOKEN_FROM_AUTHENTICATION_SELECT_STATEMENT;
	private String selectAccessTokensFromUserNameAndClientIdSql = ELINK_ACCESS_TOKENS_FROM_USERNAME_AND_CLIENT_SELECT_STATEMENT;
	private String selectAccessTokensFromUserNameSql = ELINK_ACCESS_TOKENS_FROM_USERNAME_SELECT_STATEMENT;
	private String selectAccessTokensFromClientIdSql = ELINK_ACCESS_TOKENS_FROM_CLIENTID_SELECT_STATEMENT;
	private String deleteAccessTokenSql = ELINK_ACCESS_TOKEN_DELETE_STATEMENT;
	private String insertRefreshTokenSql = ELINK_REFRESH_TOKEN_INSERT_STATEMENT;
	private String selectRefreshTokenSql = ELINK_REFRESH_TOKEN_SELECT_STATEMENT;
	private String selectRefreshTokenAuthenticationSql = ELINK_REFRESH_TOKEN_AUTHENTICATION_SELECT_STATEMENT;
	private String deleteRefreshTokenSql = ELINK_REFRESH_TOKEN_DELETE_STATEMENT;
	private String deleteAccessTokenFromRefreshTokenSql = ELINK_ACCESS_TOKEN_DELETE_FROM_REFRESH_TOKEN_STATEMENT;

	public ElinkJdbcTokenStore(DataSource dataSource) {
		super(dataSource);

		super.setInsertAccessTokenSql(insertAccessTokenSql);
		super.setSelectAccessTokenSql(selectAccessTokenSql);
		super.setDeleteAccessTokenSql(deleteAccessTokenSql);
		super.setInsertRefreshTokenSql(insertRefreshTokenSql);
		super.setSelectRefreshTokenSql(selectRefreshTokenSql);
		super.setDeleteRefreshTokenSql(deleteRefreshTokenSql);
		super.setSelectAccessTokenAuthenticationSql(selectAccessTokenAuthenticationSql);
		super.setSelectRefreshTokenAuthenticationSql(selectRefreshTokenAuthenticationSql);
		super.setSelectAccessTokenFromAuthenticationSql(selectAccessTokenFromAuthenticationSql);
		super.setDeleteAccessTokenFromRefreshTokenSql(deleteAccessTokenFromRefreshTokenSql);
		super.setSelectAccessTokensFromUserNameSql(selectAccessTokensFromUserNameSql);
		super.setSelectAccessTokensFromUserNameAndClientIdSql(selectAccessTokensFromUserNameAndClientIdSql);
		super.setSelectAccessTokensFromClientIdSql(selectAccessTokensFromClientIdSql);
	}
}
