use of org.wso2.carbon.identity.template.mgt.function.TemplateToResourceAdd in project carbon-identity-framework by wso2.
the class ConfigStoreBasedTemplateHandler method addTemplateToConfigStore.
private String addTemplateToConfigStore(Template template) throws TemplateManagementException {
if (!isValidTemplateType(template.getTemplateType().toString())) {
throw handleClientException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_INVALID_TEMPLATE_TYPE, template.getTemplateType().toString());
}
ConfigurationManager configManager = TemplateManagerDataHolder.getInstance().getConfigurationManager();
try {
Resource resource = configManager.addResource(template.getTemplateType().toString(), new TemplateToResourceAdd().apply(template));
configManager.addFile(template.getTemplateType().toString(), template.getTemplateName(), template.getTemplateName() + "_template_object", IOUtils.toInputStream(template.getTemplateScript()));
return resource.getResourceId();
} catch (ConfigurationManagementException e) {
if (ConfigurationConstants.ErrorMessages.ERROR_CODE_RESOURCE_ALREADY_EXISTS.getCode().equals(e.getErrorCode())) {
throw handleClientException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_TEMPLATE_ALREADY_EXIST, e, template.getTemplateName());
} else if (ConfigurationConstants.ErrorMessages.ERROR_CODE_RESOURCE_TYPE_DOES_NOT_EXISTS.getCode().equals(e.getErrorCode())) {
// in the database, create the resource-type and retry the template creation.
try {
createResourceType(template.getTemplateType().toString());
return addTemplateToConfigStore(template);
} catch (ConfigurationManagementException e1) {
throw handleServerException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_INSERT_TEMPLATE, e, template.getTemplateName());
}
} else {
throw handleServerException(TemplateMgtConstants.ErrorMessages.ERROR_CODE_INSERT_TEMPLATE, e, template.getTemplateName());
}
}
}
Aggregations