use of org.wso2.carbon.identity.configuration.mgt.core.constant.ConfigurationConstants.ErrorMessages.ERROR_CODE_REPLACE_RESOURCE in project carbon-identity-framework by wso2.
the class ConfigurationDAOImpl method replaceResource.
/**
* {@inheritDoc}
*/
@Override
public void replaceResource(Resource resource) throws ConfigurationManagementException {
String resourceTypeId = getResourceTypeByName(resource.getResourceType()).getId();
Timestamp currentTime = new java.sql.Timestamp(new Date().getTime());
JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
try {
Timestamp createdTime = jdbcTemplate.withTransaction(template -> {
boolean isAttributeExists = resource.getAttributes() != null;
if (isH2DB()) {
updateMetadataForH2(resource, resourceTypeId, isAttributeExists, currentTime, useCreatedTimeField());
} else {
updateMetadataForMYSQL(resource, resourceTypeId, isAttributeExists, currentTime, useCreatedTimeField());
}
// Insert attributes.
if (isAttributeExists) {
// Delete existing attributes.
template.executeUpdate(DELETE_RESOURCE_ATTRIBUTES_SQL, preparedStatement -> preparedStatement.setString(1, resource.getResourceId()));
insertResourceAttributes(template, resource);
}
if (useCreatedTimeField()) {
return getCreatedTimeInResponse(resource, resourceTypeId);
} else {
return null;
}
});
resource.setLastModified(currentTime.toInstant().toString());
if (createdTime != null) {
resource.setCreatedTime(createdTime.toInstant().toString());
}
} catch (TransactionException e) {
throw handleServerException(ERROR_CODE_REPLACE_RESOURCE, resource.getResourceName(), e);
}
}
use of org.wso2.carbon.identity.configuration.mgt.core.constant.ConfigurationConstants.ErrorMessages.ERROR_CODE_REPLACE_RESOURCE in project carbon-identity-framework by wso2.
the class ConfigurationDAOImpl method replaceResourceWithFiles.
/**
* {@inheritDoc}
*/
@Override
public void replaceResourceWithFiles(Resource resource) throws ConfigurationManagementException {
String resourceTypeId = getResourceTypeByName(resource.getResourceType()).getId();
Timestamp currentTime = new java.sql.Timestamp(new Date().getTime());
JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate();
try {
Timestamp createdTime = jdbcTemplate.withTransaction(template -> {
boolean isAttributeExists = resource.getAttributes() != null && !resource.getAttributes().isEmpty();
boolean isFileExists = resource.getFiles() != null && !resource.getFiles().isEmpty();
// Update attributes.
if (isAttributeExists) {
// Delete existing attributes.
template.executeUpdate(DELETE_RESOURCE_ATTRIBUTES_SQL, preparedStatement -> preparedStatement.setString(1, resource.getResourceId()));
insertResourceAttributes(template, resource);
}
// Update Files.
if (isFileExists) {
template.executeUpdate(DELETE_FILES_SQL, (preparedStatement -> preparedStatement.setString(1, resource.getResourceId())));
for (ResourceFile file : resource.getFiles()) {
insertResourceFile(template, resource, file.getId(), file.getName(), file.getInputStream());
}
}
updateResourceMetadata(template, resource, isAttributeExists, isFileExists, currentTime);
if (useCreatedTimeField()) {
return getCreatedTimeInResponse(resource, resourceTypeId);
} else {
return null;
}
});
resource.setLastModified(currentTime.toInstant().toString());
if (createdTime != null) {
resource.setCreatedTime(createdTime.toInstant().toString());
}
} catch (TransactionException e) {
if (e.getCause() instanceof ConfigurationManagementException) {
throw (ConfigurationManagementException) e.getCause();
}
throw handleServerException(ERROR_CODE_REPLACE_RESOURCE, resource.getResourceName(), e);
}
}
Aggregations