use of org.wso2.carbon.identity.configuration.mgt.core.constant.SQLConstants.INSERT_RESOURCE_SQL 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.SQLConstants.INSERT_RESOURCE_SQL 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);
}
}
Aggregations