Search in sources :

Example 1 with INSERT_SECRET

use of org.wso2.carbon.identity.secret.mgt.core.constant.SQLConstants.INSERT_SECRET in project carbon-identity-framework by wso2.

the class SecretDAOImpl method addSecret.

@Override
public void addSecret(Secret secret) throws SecretManagementException {
    SecretType secretType = getSecretTypeByName(secret.getSecretType());
    Timestamp currentTime = new java.sql.Timestamp(new Date().getTime());
    NamedJdbcTemplate jdbcTemplate = getNewTemplate();
    try {
        jdbcTemplate.withTransaction(template -> {
            // Insert secret metadata.
            template.executeInsert(INSERT_SECRET, preparedStatement -> {
                preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ID, secret.getSecretId());
                preparedStatement.setInt(DB_SCHEMA_COLUMN_NAME_TENANT_ID, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
                preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_SECRET_NAME, secret.getSecretName());
                preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_SECRET_VALUE, secret.getSecretValue());
                preparedStatement.setTimeStamp(DB_SCHEMA_COLUMN_NAME_CREATED_TIME, currentTime, calendar);
                preparedStatement.setTimeStamp(DB_SCHEMA_COLUMN_NAME_LAST_MODIFIED, currentTime, calendar);
                preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_TYPE, secretType.getId());
                preparedStatement.setString(DB_SCHEMA_COLUMN_NAME_DESCRIPTION, secret.getDescription());
            }, secret, false);
            return null;
        });
        secret.setLastModified(currentTime.toInstant().toString());
        secret.setCreatedTime(currentTime.toInstant().toString());
        secret.setSecretType(secretType.getName());
    } catch (TransactionException e) {
        throw handleServerException(ERROR_CODE_ADD_SECRET, secret.getSecretName(), e);
    }
}
Also used : TransactionException(org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException) SecretType(org.wso2.carbon.identity.secret.mgt.core.model.SecretType) NamedJdbcTemplate(org.wso2.carbon.database.utils.jdbc.NamedJdbcTemplate) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Aggregations

Timestamp (java.sql.Timestamp)1 Date (java.util.Date)1 NamedJdbcTemplate (org.wso2.carbon.database.utils.jdbc.NamedJdbcTemplate)1 TransactionException (org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException)1 SecretType (org.wso2.carbon.identity.secret.mgt.core.model.SecretType)1