Search in sources :

Example 11 with JdbcTemplate

use of org.wso2.carbon.database.utils.jdbc.JdbcTemplate in project carbon-identity-framework by wso2.

the class UserFunctionalityManagerDAOImpl method getFunctionalityLockStatus.

/**
 * {@inheritDoc}
 */
@Override
public FunctionalityLockStatus getFunctionalityLockStatus(String userId, int tenantId, String functionalityIdentifier) throws UserFunctionalityManagementServerException {
    FunctionalityLockStatus functionalityLockStatus;
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    try {
        functionalityLockStatus = jdbcTemplate.fetchSingleRecord(UserFunctionalityMgtConstants.SqlQueries.GET_FUNCTIONALITY_LOCK_STATUS, ((resultSet, i) -> new FunctionalityLockStatus(resultSet.getBoolean("IS_FUNCTIONALITY_LOCKED"), resultSet.getLong("FUNCTIONALITY_UNLOCK_TIME"), resultSet.getString("FUNCTIONALITY_LOCK_REASON_CODE"), resultSet.getString("FUNCTIONALITY_LOCK_REASON"))), preparedStatement -> {
            preparedStatement.setString(1, userId);
            preparedStatement.setInt(2, tenantId);
            preparedStatement.setString(3, functionalityIdentifier);
        });
    } catch (DataAccessException e) {
        String message = String.format("Error occurred while retrieving functionality lock status from DB for " + "functionality identifier: %s, user Id: %s and tenant Id: %d.", functionalityIdentifier, userId, tenantId);
        if (log.isDebugEnabled()) {
            log.debug(message, e);
        }
        throw new UserFunctionalityManagementServerException(message, e);
    }
    return functionalityLockStatus;
}
Also used : UserFunctionalityMgtConstants(org.wso2.carbon.identity.user.functionality.mgt.UserFunctionalityMgtConstants) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) UserFunctionalityManagementException(org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementException) HashMap(java.util.HashMap) UserFunctionalityManager(org.wso2.carbon.identity.user.functionality.mgt.UserFunctionalityManager) UUID(java.util.UUID) FunctionalityLockStatus(org.wso2.carbon.identity.user.functionality.mgt.model.FunctionalityLockStatus) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) UserFunctionalityManagementServerException(org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementServerException) Log(org.apache.commons.logging.Log) JdbcUtils(org.wso2.carbon.identity.core.util.JdbcUtils) LogFactory(org.apache.commons.logging.LogFactory) UserFunctionalityManagerImpl(org.wso2.carbon.identity.user.functionality.mgt.UserFunctionalityManagerImpl) UserFunctionalityManagerDAO(org.wso2.carbon.identity.user.functionality.mgt.dao.UserFunctionalityManagerDAO) FunctionalityLockStatus(org.wso2.carbon.identity.user.functionality.mgt.model.FunctionalityLockStatus) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) UserFunctionalityManagementServerException(org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementServerException) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 12 with JdbcTemplate

use of org.wso2.carbon.database.utils.jdbc.JdbcTemplate in project carbon-identity-framework by wso2.

the class UserFunctionalityPropertyDAOImpl method deleteAllPropertiesForTenant.

/**
 * {@inheritDoc}
 */
@Override
public void deleteAllPropertiesForTenant(int tenantId) throws UserFunctionalityManagementServerException {
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    try {
        String query;
        if (isOracleDB()) {
            if (StringUtils.isEmpty(TABLE_NAME)) {
                TABLE_NAME = getOracleTableName();
            }
            query = String.format(UserFunctionalityMgtConstants.SqlQueries.DELETE_ALL_PROPERTIES_FOR_TENANT_ORACLE, TABLE_NAME);
        } else {
            query = UserFunctionalityMgtConstants.SqlQueries.DELETE_ALL_PROPERTIES_FOR_TENANT;
        }
        jdbcTemplate.executeUpdate(query, preparedStatement -> {
            preparedStatement.setInt(1, tenantId);
        });
    } catch (DataAccessException e) {
        String message = String.format("Error occurred while deleting functionality lock properties from DB for tenant" + " Id: %d.", tenantId);
        if (log.isDebugEnabled()) {
            log.debug(message, e);
        }
        throw new UserFunctionalityManagementServerException(message, e);
    }
}
Also used : JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) UserFunctionalityManagementServerException(org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementServerException) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 13 with JdbcTemplate

use of org.wso2.carbon.database.utils.jdbc.JdbcTemplate in project carbon-identity-framework by wso2.

the class UserFunctionalityPropertyDAOImpl method addProperties.

/**
 * {@inheritDoc}
 */
@Override
public void addProperties(String userId, int tenantId, String functionalityIdentifier, Map<String, String> propertiesToAdd) throws UserFunctionalityManagementServerException {
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    for (Map.Entry<String, String> entry : propertiesToAdd.entrySet()) {
        String propertyName = entry.getKey();
        String propertyValue = entry.getValue();
        try {
            String query;
            if (isOracleDB()) {
                if (StringUtils.isEmpty(TABLE_NAME)) {
                    TABLE_NAME = getOracleTableName();
                }
                query = String.format(UserFunctionalityMgtConstants.SqlQueries.INSERT_PROPERTY_ORACLE, TABLE_NAME);
            } else {
                query = UserFunctionalityMgtConstants.SqlQueries.INSERT_PROPERTY;
            }
            jdbcTemplate.executeUpdate(query, preparedStatement -> {
                preparedStatement.setString(1, UUID.randomUUID().toString());
                preparedStatement.setString(2, userId);
                preparedStatement.setInt(3, tenantId);
                preparedStatement.setString(4, functionalityIdentifier);
                preparedStatement.setString(5, propertyName);
                preparedStatement.setString(6, propertyValue);
            });
        } catch (DataAccessException e) {
            String message = String.format("Error occurred while adding the property: %s for functionality: %s in user: %s," + " tenant id: %d", propertyName, functionalityIdentifier, userId, tenantId);
            if (log.isDebugEnabled()) {
                log.debug(message, e);
            }
            throw new UserFunctionalityManagementServerException(message, e);
        }
    }
}
Also used : JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) HashMap(java.util.HashMap) Map(java.util.Map) UserFunctionalityManagementServerException(org.wso2.carbon.identity.user.functionality.mgt.exception.UserFunctionalityManagementServerException) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 14 with JdbcTemplate

use of org.wso2.carbon.database.utils.jdbc.JdbcTemplate in project carbon-identity-framework by wso2.

the class TemplateManagerDAOImpl method addTemplate.

/**
 * Add a {@link Template}.
 *
 * @param template {@link Template} to insert.
 * @return Inserted {@link TemplateInfo}.
 * @throws TemplateManagementException If error occurs while adding the {@link Template}.
 */
public Template addTemplate(Template template) throws TemplateManagementException {
    Template templateResult;
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    try {
        jdbcTemplate.executeUpdate(TemplateMgtConstants.SqlQueries.INSERT_TEMPLATE, preparedStatement -> {
            preparedStatement.setInt(1, template.getTenantId());
            preparedStatement.setString(2, template.getTemplateName());
            preparedStatement.setString(3, template.getDescription());
            InputStream inputStream = IOUtils.toInputStream(template.getTemplateScript());
            try {
                preparedStatement.setBinaryStream(4, inputStream, inputStream.available());
            } catch (IOException e) {
                // SQLException is thrown since the QueryFilter throws an SQLException
                throw TemplateMgtUtils.handleSQLException(ERROR_CODE_INSERT_TEMPLATE, template.getTemplateName(), e);
            }
        });
    } catch (DataAccessException e) {
        throw TemplateMgtUtils.handleServerException(ERROR_CODE_INSERT_TEMPLATE, template.getTemplateName(), e);
    }
    templateResult = new Template(template.getTenantId(), template.getTemplateName(), template.getDescription(), template.getTemplateScript());
    return templateResult;
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) Template(org.wso2.carbon.identity.template.mgt.model.Template)

Example 15 with JdbcTemplate

use of org.wso2.carbon.database.utils.jdbc.JdbcTemplate in project carbon-identity-framework by wso2.

the class TemplateManagerDAOImpl method deleteTemplate.

/**
 * Delete {@link Template} for a given template name and a tenant Id.
 *
 * @param templateName name of the {@link Template} to be deleted.the tenant
 * @param tenantId     tenant Id of the tenant which the {@link Template} resides.
 * @return TemplateInfo of the deleted {@link Template}.
 * @throws TemplateManagementException If error occurs while deleting the {@link Template}
 */
public TemplateInfo deleteTemplate(String templateName, Integer tenantId) throws TemplateManagementException {
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    try {
        jdbcTemplate.executeUpdate(DELETE_TEMPLATE, preparedStatement -> {
            preparedStatement.setString(1, templateName);
            preparedStatement.setInt(2, tenantId);
        });
    } catch (DataAccessException e) {
        throw new TemplateManagementServerException(String.format(ERROR_CODE_DELETE_TEMPLATE.getMessage(), tenantId.toString(), templateName), ERROR_CODE_DELETE_TEMPLATE.getCode(), e);
    }
    return new TemplateInfo(tenantId, templateName);
}
Also used : TemplateInfo(org.wso2.carbon.identity.template.mgt.model.TemplateInfo) TemplateManagementServerException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementServerException) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Aggregations

DataAccessException (org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)76 JdbcTemplate (org.wso2.carbon.database.utils.jdbc.JdbcTemplate)71 TransactionException (org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException)43 List (java.util.List)38 Log (org.apache.commons.logging.Log)37 LogFactory (org.apache.commons.logging.LogFactory)37 SQLIntegrityConstraintViolationException (java.sql.SQLIntegrityConstraintViolationException)33 Map (java.util.Map)32 HashMap (java.util.HashMap)31 Timestamp (java.sql.Timestamp)30 Date (java.util.Date)30 HashSet (java.util.HashSet)30 Set (java.util.Set)30 ArrayList (java.util.ArrayList)29 PreparedStatement (java.sql.PreparedStatement)26 SQLException (java.sql.SQLException)26 StringUtils (org.apache.commons.lang.StringUtils)25 IdentityTenantUtil (org.wso2.carbon.identity.core.util.IdentityTenantUtil)24 Calendar (java.util.Calendar)21 TimeZone (java.util.TimeZone)21