Search in sources :

Example 16 with DataAccessException

use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException 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 17 with DataAccessException

use of org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException 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)

Example 18 with DataAccessException

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

the class TemplateManagerDAOImpl method getAllTemplates.

/**
 * List {@link TemplateInfo} items for a given search criteria.
 *
 * @param tenantId Tenant Id to be searched.
 * @param limit    Maximum number of results expected.
 * @param offset   Result offset.
 * @return List of {@link TemplateInfo} entries.
 * @throws TemplateManagementException If error occurs while searching the Templates.
 */
public List<TemplateInfo> getAllTemplates(Integer tenantId, Integer limit, Integer offset) throws TemplateManagementException {
    List<TemplateInfo> templates;
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    try {
        String query;
        if (isH2DB() || isMySQLDB() || isPostgreSQLDB()) {
            query = LIST_PAGINATED_TEMPLATES_MYSQL;
        } else if (isDB2DB()) {
            query = LIST_PAGINATED_TEMPLATES_DB2;
            int initialOffset = offset;
            offset = offset + limit;
            limit = initialOffset + 1;
        } else if (isMSSqlDB()) {
            int initialOffset = offset;
            offset = limit + offset;
            limit = initialOffset + 1;
            query = LIST_PAGINATED_TEMPLATES_MSSQL;
        } else {
            // oracle
            query = LIST_PAGINATED_TEMPLATES_ORACLE;
            limit = offset + limit;
        }
        int finalLimit = limit;
        int finalOffset = offset;
        templates = jdbcTemplate.executeQuery(query, (resultSet, rowNumber) -> new TemplateInfo(resultSet.getString(1), resultSet.getString(2)), preparedStatement -> {
            preparedStatement.setInt(1, tenantId);
            preparedStatement.setInt(2, finalLimit);
            preparedStatement.setInt(3, finalOffset);
        });
    } catch (DataAccessException e) {
        throw new TemplateManagementServerException(String.format(ERROR_CODE_PAGINATED_LIST_TEMPLATES.getMessage(), tenantId, limit, offset), ERROR_CODE_PAGINATED_LIST_TEMPLATES.getCode(), e);
    }
    return templates;
}
Also used : LIST_PAGINATED_TEMPLATES_MYSQL(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.LIST_PAGINATED_TEMPLATES_MYSQL) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) ERROR_CODE_SELECT_TEMPLATE_BY_NAME(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_SELECT_TEMPLATE_BY_NAME) LIST_PAGINATED_TEMPLATES_MSSQL(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.LIST_PAGINATED_TEMPLATES_MSSQL) TemplateManagementSQLException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementSQLException) DELETE_TEMPLATE(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.DELETE_TEMPLATE) TemplateInfo(org.wso2.carbon.identity.template.mgt.model.TemplateInfo) ERROR_CODE_UPDATE_TEMPLATE(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_UPDATE_TEMPLATE) ERROR_CODE_INSERT_TEMPLATE(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_INSERT_TEMPLATE) JdbcUtils(org.wso2.carbon.identity.core.util.JdbcUtils) LIST_PAGINATED_TEMPLATES_DB2(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.LIST_PAGINATED_TEMPLATES_DB2) ERROR_CODE_PAGINATED_LIST_TEMPLATES(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_PAGINATED_LIST_TEMPLATES) ERROR_CODE_DELETE_TEMPLATE(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_DELETE_TEMPLATE) TemplateManagerDAO(org.wso2.carbon.identity.template.mgt.dao.TemplateManagerDAO) IOException(java.io.IOException) TemplateManagementServerException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementServerException) JdbcUtils.isMSSqlDB(org.wso2.carbon.identity.core.util.JdbcUtils.isMSSqlDB) GET_TEMPLATE_BY_NAME(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.GET_TEMPLATE_BY_NAME) JdbcUtils.isMySQLDB(org.wso2.carbon.identity.core.util.JdbcUtils.isMySQLDB) LIST_PAGINATED_TEMPLATES_ORACLE(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.SqlQueries.LIST_PAGINATED_TEMPLATES_ORACLE) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) JdbcUtils.isH2DB(org.wso2.carbon.identity.core.util.JdbcUtils.isH2DB) TemplateManagementException(org.wso2.carbon.identity.template.mgt.exception.TemplateManagementException) JdbcUtils.isPostgreSQLDB(org.wso2.carbon.identity.core.util.JdbcUtils.isPostgreSQLDB) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException) TemplateMgtConstants(org.wso2.carbon.identity.template.mgt.TemplateMgtConstants) TemplateMgtUtils(org.wso2.carbon.identity.template.mgt.util.TemplateMgtUtils) JdbcUtils.isDB2DB(org.wso2.carbon.identity.core.util.JdbcUtils.isDB2DB) Template(org.wso2.carbon.identity.template.mgt.model.Template) InputStream(java.io.InputStream) 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)

Example 19 with DataAccessException

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

the class ApplicationTemplateDAOImpl method deleteApplicationTemplate.

@Override
public void deleteApplicationTemplate(String templateName, String tenantDomain) throws IdentityApplicationManagementException {
    if (log.isDebugEnabled()) {
        log.debug(String.format("Deleting application template: %s in tenant: %s", templateName, tenantDomain));
    }
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    try {
        jdbcTemplate.executeUpdate(DELETE_SP_TEMPLATE_BY_NAME, preparedStatement -> {
            preparedStatement.setString(1, templateName);
            preparedStatement.setInt(2, getTenantID(tenantDomain));
        });
    } catch (DataAccessException e) {
        throw new IdentityApplicationManagementException(String.format("An error occurred while deleting the " + "application template : %s in tenant: %s", templateName, tenantDomain), e);
    }
}
Also used : IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 20 with DataAccessException

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

the class ApplicationTemplateDAOImpl method updateApplicationTemplate.

@Override
public void updateApplicationTemplate(String templateName, SpTemplate spTemplate, String tenantDomain) throws IdentityApplicationManagementException {
    if (log.isDebugEnabled()) {
        log.debug(String.format("Updating application template: %s in tenant: %s", spTemplate.getName(), tenantDomain));
    }
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    try {
        jdbcTemplate.executeUpdate(UPDATE_SP_TEMPLATE_BY_NAME, preparedStatement -> {
            preparedStatement.setString(1, spTemplate.getName());
            preparedStatement.setString(2, spTemplate.getDescription());
            try {
                setBlobValue(spTemplate.getContent(), preparedStatement, 3);
            } catch (IOException e) {
                throw new SQLException(String.format("Could not set application template: %s content as " + "a Blob in tenant: %s.", spTemplate.getName(), tenantDomain), e);
            }
            preparedStatement.setString(4, templateName);
            preparedStatement.setInt(5, getTenantID(tenantDomain));
        });
    } catch (DataAccessException e) {
        throw new IdentityApplicationManagementException(String.format("An error occurred while updating the" + " application template : %s in tenant: %s", spTemplate.getName(), tenantDomain), e);
    }
}
Also used : SQLException(java.sql.SQLException) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) IOException(java.io.IOException) 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)79 JdbcTemplate (org.wso2.carbon.database.utils.jdbc.JdbcTemplate)58 PreparedStatement (java.sql.PreparedStatement)33 SQLException (java.sql.SQLException)33 List (java.util.List)31 Log (org.apache.commons.logging.Log)29 LogFactory (org.apache.commons.logging.LogFactory)29 TransactionException (org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException)28 ArrayList (java.util.ArrayList)26 SQLIntegrityConstraintViolationException (java.sql.SQLIntegrityConstraintViolationException)25 Map (java.util.Map)25 HashMap (java.util.HashMap)24 HashSet (java.util.HashSet)22 Set (java.util.Set)22 StringUtils (org.apache.commons.lang.StringUtils)22 Timestamp (java.sql.Timestamp)21 IdentityTenantUtil (org.wso2.carbon.identity.core.util.IdentityTenantUtil)21 Date (java.util.Date)19 Calendar (java.util.Calendar)18 JdbcUtils.isH2DB (org.wso2.carbon.identity.core.util.JdbcUtils.isH2DB)18