use of org.wso2.carbon.identity.configuration.mgt.core.constant.ConfigurationConstants.ErrorMessages.ERROR_CODE_CHECK_DB_METADATA in project carbon-identity-framework by wso2.
the class ConfigurationDAOImpl method updateMetadataForH2.
private void updateMetadataForH2(Resource resource, String resourceTypeId, boolean isAttributeExists, Timestamp currentTime, boolean useCreatedTime) throws TransactionException, ConfigurationManagementServerException {
JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
if (isResourceExists(resource, resourceTypeId)) {
jdbcTemplate.withTransaction(template -> template.executeInsert(UPDATE_RESOURCE_H2, preparedStatement -> {
int initialParameterIndex = 1;
preparedStatement.setInt(initialParameterIndex, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
preparedStatement.setString(++initialParameterIndex, resource.getResourceName());
preparedStatement.setTimestamp(++initialParameterIndex, currentTime, calendar);
/*
Resource files are uploaded using a separate endpoint. Therefore resource creation
does not create files. It is allowed to create a resource without files or attributes
in order to allow file uploadafter resource creation.
*/
preparedStatement.setBoolean(++initialParameterIndex, false);
preparedStatement.setBoolean(++initialParameterIndex, isAttributeExists);
preparedStatement.setString(++initialParameterIndex, resourceTypeId);
preparedStatement.setString(++initialParameterIndex, resource.getResourceId());
}, resource, false));
} else {
try {
boolean isOracleOrMsssql = isOracleDB() || isMSSqlDB();
jdbcTemplate.withTransaction(template -> template.executeInsert(useCreatedTime ? 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 (useCreatedTime) {
preparedStatement.setTimestamp(++initialParameterIndex, currentTime, calendar);
}
preparedStatement.setTimestamp(++initialParameterIndex, currentTime, calendar);
if (isOracleOrMsssql) {
preparedStatement.setInt(++initialParameterIndex, 0);
preparedStatement.setInt(++initialParameterIndex, isAttributeExists ? 1 : 0);
} else {
preparedStatement.setBoolean(++initialParameterIndex, false);
preparedStatement.setBoolean(++initialParameterIndex, isAttributeExists);
}
preparedStatement.setString(++initialParameterIndex, resourceTypeId);
}, resource, false));
} catch (DataAccessException e) {
throw handleServerException(ERROR_CODE_CHECK_DB_METADATA, e.getMessage(), e);
}
}
}
use of org.wso2.carbon.identity.configuration.mgt.core.constant.ConfigurationConstants.ErrorMessages.ERROR_CODE_CHECK_DB_METADATA 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);
}
}
use of org.wso2.carbon.identity.configuration.mgt.core.constant.ConfigurationConstants.ErrorMessages.ERROR_CODE_CHECK_DB_METADATA in project carbon-identity-framework by wso2.
the class ConfigurationDAOImpl method updateMetadataForMYSQL.
private void updateMetadataForMYSQL(Resource resource, String resourceTypeId, boolean isAttributeExists, Timestamp currentTime, boolean useCreatedTime) throws TransactionException, ConfigurationManagementServerException {
String query = INSERT_OR_UPDATE_RESOURCE_MYSQL;
try {
if (isPostgreSQLDB()) {
query = INSERT_OR_UPDATE_RESOURCE_POSTGRESQL;
} else if (isMSSqlDB() || isDB2DB()) {
query = INSERT_OR_UPDATE_RESOURCE_MSSQL_OR_DB2;
} else if (isOracleDB()) {
query = INSERT_OR_UPDATE_RESOURCE_ORACLE;
}
boolean isOracleOrMssql = isOracleDB() || isMSSqlDB();
JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
final String finalQuery = query;
jdbcTemplate.withTransaction(template -> template.executeInsert(useCreatedTime ? finalQuery : INSERT_OR_UPDATE_RESOURCE_MYSQL_WITHOUT_CREATED_TIME, preparedStatement -> {
int initialParameterIndex = 1;
preparedStatement.setString(initialParameterIndex, resource.getResourceId());
preparedStatement.setInt(++initialParameterIndex, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
preparedStatement.setString(++initialParameterIndex, resource.getResourceName());
if (useCreatedTime) {
preparedStatement.setTimestamp(++initialParameterIndex, currentTime, calendar);
}
preparedStatement.setTimestamp(++initialParameterIndex, currentTime, calendar);
if (isOracleOrMssql) {
preparedStatement.setInt(++initialParameterIndex, 0);
preparedStatement.setInt(++initialParameterIndex, isAttributeExists ? 1 : 0);
} else {
preparedStatement.setBoolean(++initialParameterIndex, false);
preparedStatement.setBoolean(++initialParameterIndex, isAttributeExists);
}
preparedStatement.setString(++initialParameterIndex, resourceTypeId);
}, resource, false));
} catch (DataAccessException e) {
throw handleServerException(ERROR_CODE_CHECK_DB_METADATA, e.getMessage(), e);
}
}
use of org.wso2.carbon.identity.configuration.mgt.core.constant.ConfigurationConstants.ErrorMessages.ERROR_CODE_CHECK_DB_METADATA in project carbon-identity-framework by wso2.
the class ConfigurationDAOImpl method deleteFileById.
@Override
public void deleteFileById(String resourceType, String resourceName, String fileId) throws ConfigurationManagementException {
JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
try {
boolean isOracleOrMssql = isOracleDB() || isMSSqlDB();
jdbcTemplate.withTransaction(template -> {
// Get resource id for the deleting file.
String sqlStmt = isH2DB() ? SQLConstants.GET_FILE_BY_ID_SQL_H2 : SQLConstants.GET_FILE_BY_ID_SQL;
String resourceId = template.fetchSingleRecord(sqlStmt, (resultSet, rowNumber) -> resultSet.getString(DB_SCHEMA_COLUMN_NAME_RESOURCE_ID), preparedStatement -> {
preparedStatement.setString(1, fileId);
preparedStatement.setString(2, resourceName);
preparedStatement.setString(3, resourceType);
});
template.executeUpdate(DELETE_FILE_SQL, (preparedStatement -> preparedStatement.setString(1, fileId)));
List<String> availableFilesForTheResource = template.executeQuery(GET_FILES_BY_RESOURCE_ID_SQL, ((resultSet, rowNumber) -> resultSet.getString(DB_SCHEMA_COLUMN_NAME_ID)), preparedStatement -> preparedStatement.setString(1, resourceId));
if (availableFilesForTheResource.isEmpty()) {
template.executeUpdate(UPDATE_HAS_FILE_SQL, preparedStatement -> {
if (isOracleOrMssql) {
preparedStatement.setInt(1, 0);
} else {
preparedStatement.setBoolean(1, false);
}
preparedStatement.setString(2, resourceId);
});
}
updateResourceLastModified(template, resourceId);
return null;
});
} catch (TransactionException e) {
throw handleServerException(ERROR_CODE_DELETE_FILE, fileId, e);
} catch (DataAccessException e) {
throw handleServerException(ERROR_CODE_CHECK_DB_METADATA, e.getMessage(), e);
}
}
use of org.wso2.carbon.identity.configuration.mgt.core.constant.ConfigurationConstants.ErrorMessages.ERROR_CODE_CHECK_DB_METADATA in project carbon-identity-framework by wso2.
the class ConfigurationDAOImpl method addFile.
@Override
public void addFile(String fileId, String resourceId, String fileName, InputStream fileStream) throws ConfigurationManagementException {
JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
try {
boolean isOracleOrMssql = isOracleDB() || isMSSqlDB();
boolean isPostgreSQL = isPostgreSQLDB();
String sqlStmt = isH2DB() ? SQLConstants.INSERT_FILE_SQL_H2 : SQLConstants.INSERT_FILE_SQL;
jdbcTemplate.withTransaction(template -> {
template.executeUpdate(sqlStmt, preparedStatement -> {
preparedStatement.setString(1, fileId);
if (isPostgreSQL) {
preparedStatement.setBinaryStream(2, fileStream);
} else {
preparedStatement.setBlob(2, fileStream);
}
preparedStatement.setString(3, resourceId);
preparedStatement.setString(4, fileName);
});
template.executeUpdate(SQLConstants.UPDATE_HAS_FILE_SQL, preparedStatement -> {
if (isOracleOrMssql) {
preparedStatement.setInt(1, 1);
} else {
preparedStatement.setBoolean(1, true);
}
preparedStatement.setString(2, resourceId);
});
updateResourceLastModified(template, resourceId);
return null;
});
} catch (TransactionException e) {
throw handleServerException(ERROR_CODE_INSERT_FILE, fileId, e);
} catch (DataAccessException e) {
throw handleServerException(ERROR_CODE_CHECK_DB_METADATA, e.getMessage(), e);
}
}
Aggregations