Search in sources :

Example 1 with ERROR_CODE_ADD_RESOURCE

use of org.wso2.carbon.identity.configuration.mgt.core.constant.ConfigurationConstants.ErrorMessages.ERROR_CODE_ADD_RESOURCE in project carbon-identity-framework by wso2.

the class ConfigurationDAOImpl method addResource.

/**
 * {@inheritDoc}
 */
@Override
public void addResource(Resource resource) throws ConfigurationManagementException {
    String resourceTypeId = getResourceTypeByName(resource.getResourceType()).getId();
    Timestamp currentTime = new java.sql.Timestamp(new Date().getTime());
    JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
    try {
        boolean isOracleOrMssql = isOracleDB() || isMSSqlDB();
        jdbcTemplate.withTransaction(template -> {
            boolean isAttributeExists = resource.getAttributes() != null;
            boolean isFileExists = resource.getFiles() != null && !resource.getFiles().isEmpty();
            // Insert resource metadata.
            template.executeInsert(useCreatedTimeField() ? INSERT_RESOURCE_SQL : INSERT_RESOURCE_SQL_WITHOUT_CREATED_TIME, preparedStatement -> {
                int initialParameterIndex = 1;
                preparedStatement.setString(initialParameterIndex, resource.getResourceId());
                preparedStatement.setInt(++initialParameterIndex, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
                preparedStatement.setString(++initialParameterIndex, resource.getResourceName());
                if (useCreatedTimeField()) {
                    preparedStatement.setTimestamp(++initialParameterIndex, currentTime, calendar);
                }
                preparedStatement.setTimestamp(++initialParameterIndex, currentTime, calendar);
                if (isOracleOrMssql) {
                    preparedStatement.setInt(++initialParameterIndex, isFileExists ? 1 : 0);
                    preparedStatement.setInt(++initialParameterIndex, isAttributeExists ? 1 : 0);
                } else {
                    preparedStatement.setBoolean(++initialParameterIndex, isFileExists);
                    preparedStatement.setBoolean(++initialParameterIndex, isAttributeExists);
                }
                preparedStatement.setString(++initialParameterIndex, resourceTypeId);
            }, resource, false);
            // Insert attributes.
            if (isAttributeExists) {
                insertResourceAttributes(template, resource);
            }
            // Insert files.
            if (isFileExists) {
                for (ResourceFile file : resource.getFiles()) {
                    insertResourceFile(template, resource, file.getId(), file.getName(), file.getInputStream());
                }
            }
            return null;
        });
        resource.setLastModified(currentTime.toInstant().toString());
        if (useCreatedTimeField()) {
            resource.setCreatedTime(currentTime.toInstant().toString());
        }
    } catch (TransactionException e) {
        throw handleServerException(ERROR_CODE_ADD_RESOURCE, resource.getResourceName(), e);
    } catch (DataAccessException e) {
        throw handleServerException(ERROR_CODE_CHECK_DB_METADATA, e.getMessage(), e);
    }
}
Also used : ResourceFile(org.wso2.carbon.identity.configuration.mgt.core.model.ResourceFile) TransactionException(org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException) JdbcTemplate(org.wso2.carbon.database.utils.jdbc.JdbcTemplate) Timestamp(java.sql.Timestamp) Date(java.util.Date) DataAccessException(org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)

Aggregations

Timestamp (java.sql.Timestamp)1 Date (java.util.Date)1 JdbcTemplate (org.wso2.carbon.database.utils.jdbc.JdbcTemplate)1 DataAccessException (org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)1 TransactionException (org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException)1 ResourceFile (org.wso2.carbon.identity.configuration.mgt.core.model.ResourceFile)1