Search in sources :

Example 21 with DataAccessException

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

the class DefaultAuthSeqMgtDAOImpl method doGetDefaultAuthSeq.

private DefaultAuthenticationSequence doGetDefaultAuthSeq(String sequenceName, String tenantDomain, JdbcTemplate jdbcTemplate) throws DataAccessException {
    return jdbcTemplate.fetchSingleRecord(GET_DEFAULT_SEQ, (resultSet, rowNumber) -> {
        DefaultAuthenticationSequence sequence = new DefaultAuthenticationSequence();
        sequence.setName(resultSet.getString(1));
        sequence.setDescription(resultSet.getString(2));
        try {
            byte[] requestBytes = resultSet.getBytes(3);
            ByteArrayInputStream bais = new ByteArrayInputStream(requestBytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            Object objectRead = ois.readObject();
            if (objectRead instanceof LocalAndOutboundAuthenticationConfig) {
                sequence.setContent((LocalAndOutboundAuthenticationConfig) objectRead);
            }
        } catch (IOException | ClassNotFoundException e) {
            throw new SQLException("Could not get content of default authentication sequence as a " + "Blob.", e);
        }
        return sequence;
    }, (PreparedStatement preparedStatement) -> {
        preparedStatement.setString(1, sequenceName);
        preparedStatement.setInt(2, getTenantID(tenantDomain));
    });
}
Also used : DefaultAuthenticationSequence(org.wso2.carbon.identity.application.common.model.DefaultAuthenticationSequence) LocalAndOutboundAuthenticationConfig(org.wso2.carbon.identity.application.common.model.LocalAndOutboundAuthenticationConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 22 with DataAccessException

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

the class ConfigurationDAOImpl method getAttributeByKey.

/**
 * {@inheritDoc}
 */
@Override
public Attribute getAttributeByKey(String resourceId, String attributeKey) throws ConfigurationManagementException {
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    try {
        return jdbcTemplate.fetchSingleRecord(SQLConstants.GET_ATTRIBUTE_SQL, (resultSet, rowNumber) -> new Attribute(resultSet.getString(DB_SCHEMA_COLUMN_NAME_ATTRIBUTE_KEY), resultSet.getString(DB_SCHEMA_COLUMN_NAME_ATTRIBUTE_VALUE), resultSet.getString(DB_SCHEMA_COLUMN_NAME_ID)), preparedStatement -> {
            int initialParameterIndex = 1;
            preparedStatement.setString(initialParameterIndex, attributeKey);
            preparedStatement.setString(++initialParameterIndex, resourceId);
        });
    } catch (DataAccessException e) {
        throw handleServerException(ERROR_CODE_GET_ATTRIBUTE, attributeKey, e);
    }
}
Also used : Attribute(org.wso2.carbon.identity.configuration.mgt.core.model.Attribute) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 23 with DataAccessException

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

the class ConfigurationDAOImpl method addResourceType.

/**
 * {@inheritDoc}
 */
@Override
public void addResourceType(ResourceType resourceType) throws ConfigurationManagementException {
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    try {
        jdbcTemplate.executeInsert(SQLConstants.INSERT_RESOURCE_TYPE_SQL, preparedStatement -> {
            int initialParameterIndex = 1;
            preparedStatement.setString(initialParameterIndex, resourceType.getId());
            preparedStatement.setString(++initialParameterIndex, resourceType.getName());
            preparedStatement.setString(++initialParameterIndex, resourceType.getDescription());
        }, resourceType, false);
    } catch (DataAccessException e) {
        throw handleServerException(ERROR_CODE_ADD_RESOURCE_TYPE, resourceType.getName(), e);
    }
}
Also used : JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 24 with DataAccessException

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

the class ConfigurationDAOImpl method getResourcesByType.

@Override
public List<Resource> getResourcesByType(int tenantId, String resourceTypeId) throws ConfigurationManagementServerException {
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    try {
        String resourceTypeName = jdbcTemplate.fetchSingleRecord(SQLConstants.GET_RESOURCE_TYPE_BY_ID_SQL, (resultSet, rowNumber) -> resultSet.getString(DB_SCHEMA_COLUMN_NAME_NAME), preparedStatement -> preparedStatement.setString(1, resourceTypeId));
        return jdbcTemplate.executeQuery(GET_RESOURCES_BY_RESOURCE_TYPE_ID_SQL, (LambdaExceptionUtils.rethrowRowMapper((resultSet, rowNumber) -> {
            String resourceId = resultSet.getString(DB_SCHEMA_COLUMN_NAME_ID);
            String resourceName = resultSet.getString(DB_SCHEMA_COLUMN_NAME_NAME);
            String resourceLastModified = resultSet.getString(DB_SCHEMA_COLUMN_NAME_LAST_MODIFIED);
            String resourceCreatedTime = resultSet.getString(DB_SCHEMA_COLUMN_NAME_CREATED_TIME);
            String resourceHasFile = resultSet.getString(DB_SCHEMA_COLUMN_NAME_HAS_FILE);
            if (StringUtils.equals(resourceHasFile, "1") && (isOracleDB() || isMSSqlDB() || isDB2DB())) {
                resourceHasFile = "true";
            }
            String resourceHasAttribute = resultSet.getString(DB_SCHEMA_COLUMN_NAME_HAS_ATTRIBUTE);
            Resource resource = new Resource();
            resource.setCreatedTime(resourceCreatedTime);
            resource.setHasAttribute(Boolean.valueOf(resourceHasAttribute));
            resource.setResourceId(resourceId);
            resource.setResourceName(resourceName);
            resource.setLastModified(resourceLastModified);
            resource.setHasFile(Boolean.valueOf(resourceHasFile));
            resource.setTenantDomain(IdentityTenantUtil.getTenantDomain(tenantId));
            resource.setFiles(getFiles(resourceId, resourceTypeName, resourceName));
            resource.setAttributes(getAttributesByResourceId(resourceId));
            return resource;
        })), preparedStatement -> {
            preparedStatement.setString(1, resourceTypeId);
            preparedStatement.setInt(2, tenantId);
        });
    } catch (DataAccessException e) {
        throw handleServerException(ERROR_CODE_RESOURCES_DOES_NOT_EXISTS, e);
    }
}
Also used : Resource(org.wso2.carbon.identity.configuration.mgt.core.model.Resource) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Example 25 with DataAccessException

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

the class ConfigurationDAOImpl method insertResourceAttributes.

private void insertResourceAttributes(Template<?> template, Resource resource) throws DataAccessException, ConfigurationManagementClientException {
    // Create sql query for attribute parameters.
    String attributesQuery = buildQueryForAttributes(resource);
    template.executeInsert(attributesQuery, preparedStatement -> {
        int attributeUpdateParameterIndex = 0;
        for (Attribute attribute : resource.getAttributes()) {
            preparedStatement.setString(++attributeUpdateParameterIndex, generateUniqueID());
            preparedStatement.setString(++attributeUpdateParameterIndex, resource.getResourceId());
            preparedStatement.setString(++attributeUpdateParameterIndex, attribute.getKey());
            preparedStatement.setString(++attributeUpdateParameterIndex, attribute.getValue());
        }
    }, resource, false);
}
Also used : Attribute(org.wso2.carbon.identity.configuration.mgt.core.model.Attribute)

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