Search in sources :

Example 1 with ERROR_CODE_INSERT_TEMPLATE

use of org.wso2.carbon.identity.template.mgt.TemplateMgtConstants.ErrorMessages.ERROR_CODE_INSERT_TEMPLATE 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)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 JdbcTemplate (org.wso2.carbon.database.utils.jdbc.JdbcTemplate)1 DataAccessException (org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)1 Template (org.wso2.carbon.identity.template.mgt.model.Template)1