<commit_before>/*
 * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.wso2.carbon.business.rules.core.internal.services;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.business.rules.core.internal.bean.BusinessRule;
import org.wso2.carbon.business.rules.core.internal.bean.RuleTemplate;
import org.wso2.carbon.business.rules.core.internal.bean.RuleTemplateProperty;
import org.wso2.carbon.business.rules.core.internal.bean.Template;
import org.wso2.carbon.business.rules.core.internal.bean.TemplateGroup;
import org.wso2.carbon.business.rules.core.internal.bean.businessRulesFromScratch.BusinessRuleFromScratch;
import org.wso2.carbon.business.rules.core.internal.bean.businessRulesFromTemplate.BusinessRuleFromTemplate;
import org.wso2.carbon.business.rules.core.internal.exceptions.TemplateManagerException;
import org.wso2.carbon.business.rules.core.internal.services.businessRulesFromTemplate.BusinessRulesFromTemplate;
import org.wso2.carbon.business.rules.core.internal.util.TemplateManagerConstants;
import org.wso2.carbon.business.rules.core.internal.util.TemplateManagerHelper;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * The exposed Template Manager service, which contains methods related to
 * Business Rules from template, and Business Rules from scratch
 */
public class TemplateManagerService implements BusinessRulesService {
    private static final Logger log = LoggerFactory.getLogger(TemplateManagerService.class);
    // Available Template Groups from the directory
    private Map<String, TemplateGroup> availableTemplateGroups;
    private Map<String, BusinessRule> availableBusinessRules;

    public TemplateManagerService() {
        // Load & store available Template Groups & Business Rules at the time of instantiation
        this.availableTemplateGroups = loadTemplateGroups();
        this.availableBusinessRules = loadBusinessRules();
    }

    public void createBusinessRuleFromTemplate(BusinessRuleFromTemplate businessRuleFromTemplate) {
        // todo: CHECK THIS METHOD
        // Derive templates from the given business rule from template
        Map<String, Template> derivedTemplates = null;
        try {
            derivedTemplates = deriveTemplates(businessRuleFromTemplate);
        } catch (TemplateManagerException e) {
            log.error("Error in deriving templates", e);
        }
        String businessRuleUUID = businessRuleFromTemplate.getUuid();
        try {
            saveBusinessRuleDefinition(businessRuleUUID, businessRuleFromTemplate); // todo : Implement method
            // Deploy all derived templates, only if saving Business Rule definition is successful
            for (String templateUUID : derivedTemplates.keySet()) {
                try {
                    deployTemplate(templateUUID, derivedTemplates.get(templateUUID));
                } catch (TemplateManagerException e) {
                    log.error(e.getMessage()); // Exception is thrown from the above method itself
                }
            }
        } catch (TemplateManagerException e) {
            // Saving definition is unsuccessful
            log.error(e.getMessage()); // Exception is thrown from the saveBusinessRuleDefinition method itself
        }
    }

    public void editBusinessRuleFromTemplate(String uuid, BusinessRuleFromTemplate businessRuleFromTemplate) { // todo: verify next lower level
        Map<String, Template> derivedTemplates = null;
        try {
            derivedTemplates = deriveTemplates(businessRuleFromTemplate);
        } catch (TemplateManagerException e) {
            e.printStackTrace(); // todo: implement
        }

        try {
            overwriteBusinessRuleDefinition(uuid, businessRuleFromTemplate);
            // Load all available Business Rules again
            this.availableBusinessRules = loadBusinessRules();<commit_msg>Passed exception object to log.error()<commit_after>/*
 * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.wso2.carbon.business.rules.core.internal.services;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.business.rules.core.internal.bean.BusinessRule;
import org.wso2.carbon.business.rules.core.internal.bean.RuleTemplate;
import org.wso2.carbon.business.rules.core.internal.bean.RuleTemplateProperty;
import org.wso2.carbon.business.rules.core.internal.bean.Template;
import org.wso2.carbon.business.rules.core.internal.bean.TemplateGroup;
import org.wso2.carbon.business.rules.core.internal.bean.businessRulesFromScratch.BusinessRuleFromScratch;
import org.wso2.carbon.business.rules.core.internal.bean.businessRulesFromTemplate.BusinessRuleFromTemplate;
import org.wso2.carbon.business.rules.core.internal.exceptions.TemplateManagerException;
import org.wso2.carbon.business.rules.core.internal.services.businessRulesFromTemplate.BusinessRulesFromTemplate;
import org.wso2.carbon.business.rules.core.internal.util.TemplateManagerConstants;
import org.wso2.carbon.business.rules.core.internal.util.TemplateManagerHelper;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * The exposed Template Manager service, which contains methods related to
 * Business Rules from template, and Business Rules from scratch
 */
public class TemplateManagerService implements BusinessRulesService {
    private static final Logger log = LoggerFactory.getLogger(TemplateManagerService.class);
    // Available Template Groups from the directory
    private Map<String, TemplateGroup> availableTemplateGroups;
    private Map<String, BusinessRule> availableBusinessRules;

    public TemplateManagerService() {
        // Load & store available Template Groups & Business Rules at the time of instantiation
        this.availableTemplateGroups = loadTemplateGroups();
        this.availableBusinessRules = loadBusinessRules();
    }

    public void createBusinessRuleFromTemplate(BusinessRuleFromTemplate businessRuleFromTemplate) {
        // todo: CHECK THIS METHOD
        // Derive templates from the given business rule from template
        Map<String, Template> derivedTemplates = null;
        try {
            derivedTemplates = deriveTemplates(businessRuleFromTemplate);
        } catch (TemplateManagerException e) {
            log.error("Error in deriving templates", e);
        }
        String businessRuleUUID = businessRuleFromTemplate.getUuid();
        try {
            saveBusinessRuleDefinition(businessRuleUUID, businessRuleFromTemplate); // todo : Implement method
            // Deploy all derived templates, only if saving Business Rule definition is successful
            for (String templateUUID : derivedTemplates.keySet()) {
                try {
                    deployTemplate(templateUUID, derivedTemplates.get(templateUUID));
                } catch (TemplateManagerException e) {
                    log.error(e.getMessage(),e);
                }
            }
        } catch (TemplateManagerException e) {
            // Saving definition is unsuccessful
            log.error(e.getMessage()); // Exception is thrown from the saveBusinessRuleDefinition method itself
        }
    }

    public void editBusinessRuleFromTemplate(String uuid, BusinessRuleFromTemplate businessRuleFromTemplate) { // todo: verify next lower level
        Map<String, Template> derivedTemplates = null;
        try {
            derivedTemplates = deriveTemplates(businessRuleFromTemplate);
        } catch (TemplateManagerException e) {
            e.printStackTrace(); // todo: implement
        }

        try {
            overwriteBusinessRuleDefinition(uuid, businessRuleFromTemplate);
            // Load all available Business Rules again
            this.availableBusinessRules = loadBusinessRules();
{'fraction_non_alphanumeric': 0.081153804757292, 'fraction_numerical': 0.005381552039608223, 'mean_word_length': 3.5437652811735942, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5156136', 'n_tokens_mistral': 2354, 'n_tokens_neox': 2277, 'n_words': 637}