Search in sources :

Example 1 with TemplateToResourceAdd

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());
        }
    }
}
Also used : Resource(org.wso2.carbon.identity.configuration.mgt.core.model.Resource) TemplateToResource(org.wso2.carbon.identity.template.mgt.function.TemplateToResource) TemplateToResourceAdd(org.wso2.carbon.identity.template.mgt.function.TemplateToResourceAdd) ConfigurationManagementException(org.wso2.carbon.identity.configuration.mgt.core.exception.ConfigurationManagementException) ConfigurationManager(org.wso2.carbon.identity.configuration.mgt.core.ConfigurationManager)

Aggregations

ConfigurationManager (org.wso2.carbon.identity.configuration.mgt.core.ConfigurationManager)1 ConfigurationManagementException (org.wso2.carbon.identity.configuration.mgt.core.exception.ConfigurationManagementException)1 Resource (org.wso2.carbon.identity.configuration.mgt.core.model.Resource)1 TemplateToResource (org.wso2.carbon.identity.template.mgt.function.TemplateToResource)1 TemplateToResourceAdd (org.wso2.carbon.identity.template.mgt.function.TemplateToResourceAdd)1