<commit_before>package org.wso2.carbon.business.rules.core.api.impl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.business.rules.core.api.ApiResponseMessage;
import org.wso2.carbon.business.rules.core.api.BusinessRulesApiService;
import org.wso2.carbon.business.rules.core.api.NotFoundException;
import org.wso2.carbon.business.rules.core.bean.BusinessRule;
import org.wso2.carbon.business.rules.core.bean.RuleTemplate;
import org.wso2.carbon.business.rules.core.bean.TemplateGroup;
import org.wso2.carbon.business.rules.core.bean.TemplateManagerInstance;
import org.wso2.carbon.business.rules.core.bean.scratch.BusinessRuleFromScratch;
import org.wso2.carbon.business.rules.core.bean.template.BusinessRuleFromTemplate;
import org.wso2.carbon.business.rules.core.exceptions.TemplateManagerException;
import org.wso2.carbon.business.rules.core.services.TemplateManagerService;
import org.wso2.carbon.business.rules.core.util.TemplateManagerConstants;
import org.wso2.carbon.business.rules.core.util.TemplateManagerHelper;

import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.Map;

@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaMSF4JServerCodegen",
        date = "2017-10-13T06:19:32.032Z")
public class BusinessRulesApiServiceImpl extends BusinessRulesApiService {
    private static final Logger log = LoggerFactory.getLogger(BusinessRulesApiServiceImpl.class);

    @Override
    public Response createBusinessRule(String businessRule, Boolean deploy
    ) throws NotFoundException {
        TemplateManagerService templateManagerService = TemplateManagerInstance.getInstance();

        // convert the string received from API, as a json object
        Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
        JsonObject businessRuleJson = gson.fromJson(businessRule, JsonObject.class);

        // Check the business rule type of the json object
        if (businessRuleJson.get("type").toString().equals("\""+TemplateManagerConstants
                .BUSINESS_RULE_TYPE_TEMPLATE+"\"")) {
            // Convert to business rule from template and create
            BusinessRuleFromTemplate businessRuleFromTemplate = TemplateManagerHelper
                    .jsonToBusinessRuleFromTemplate(businessRule);

            templateManagerService.createBusinessRuleFromTemplate(businessRuleFromTemplate);
        } else {
            BusinessRuleFromScratch businessRuleFromScratch = TemplateManagerHelper.jsonToBusinessRuleFromScratch
                    (businessRule);

            templateManagerService.createBusinessRuleFromScratch(businessRuleFromScratch);
        }

        return Response.ok().entity(new ApiResponseMessage(ApiResponseMessage.OK, "Successfully created!")).build();
    }

    @Override
    public Response deleteBusinessRule(String businessRuleInstanceID
            , Boolean forceDelete
    ) throws NotFoundException {
        TemplateManagerService templateManagerService = TemplateManagerInstance.getInstance();
        try {
            templateManagerService.deleteBusinessRule(businessRuleInstanceID);
        } catch (TemplateManagerException e) {
            log.error(e.getMessage(), e);
        }
        return Response.ok().entity(new ApiResponseMessage(ApiResponseMessage.OK, "Business Rule deleted " +
                "successfully!")).build();
    }

    @Override
    public Response getBusinessRules() throws NotFoundException {
        // todo: connect with 'loadBusinessRules()' in backend
        TemplateManagerService templateManagerService = TemplateManagerInstance.getInstance();
        Map<String, BusinessRule> businessRuleMap = templateManagerService.loadBusinessRules();

        ArrayList<BusinessRule> businessRulesWithoutUUID = new ArrayList();
        for(String businessRuleUUID : businessRuleMap.keySet()){
            businessRulesWithoutUUID.add(businessRuleMap.get(businessRuleUUID));
        }
        Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();

        return Response.ok().entity(gson.toJson(businessRulesWithoutUUID)).build();
    }

    @Override
    public Response getRuleTemplate(String templateGroupID
            , String ruleTemplateID
    ) throws NotFoundException {
        TemplateManagerService templateManagerService = TemplateManagerInstance.getInstance();
        try {
            RuleTemplate ruleTemplate = templateManagerService.getRuleTemplate(templateGroupID, ruleTemplateID);
            return Response.ok().entity(ruleTemplate).build();
        } catch (TemplateManagerException e) {
            // todo: do error handlings like this everywhere
            return Response.status(404).entity(new ApiResponseMessage(ApiResponseMessage.ERROR,
                    e.getMessage())).build();
        }
    }

    @Override
    public Response getRuleTemplates(String templateGroupID
 ) throws NotFoundException {
        // do some magic!
        TemplateManagerService templateManagerService = TemplateManagerInstance.getInstance();
        try {
            // Get rule templates and store without UUIDs
            Map<String, RuleTemplate> ruleTemplates = templateManagerService.getRuleTemplates(templateGroupID);
            ArrayList<RuleTemplate> ruleTemplatesWithoutUUID = new ArrayList();
            for (String ruleTemplateUUID : ruleTemplates.keySet()) {
                ruleTemplatesWithoutUUID.add(ruleTemplates.get(ruleTemplateUUID));
            }
            Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();

            return Response.ok().entity(gson.toJson(ruleTemplatesWithoutUUID)).build();
        } catch (TemplateManagerException e) {
            return Response.status(404).build();
        }
    }

    @Override
    public Response getTemplateGroup(String templateGroupID
    ) throws NotFoundException {
        TemplateManagerService templateManagerService = TemplateManagerInstance.getInstance();
        try {
            TemplateGroup templateGroup = templateManagerService.getTemplateGroup(templateGroupID);
            return Response.ok().entity(templateManagerService.getTemplateGroup(templateGroupID)).build();
        } catch (TemplateManagerException e) {
            return Response.status(404).build();
        }
    }
<commit_msg>Handling possible exceptions in BusinessRulesApi calls<commit_after>package org.wso2.carbon.business.rules.core.api.impl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.business.rules.core.api.ApiResponseMessage;
import org.wso2.carbon.business.rules.core.api.BusinessRulesApiService;
import org.wso2.carbon.business.rules.core.api.NotFoundException;
import org.wso2.carbon.business.rules.core.bean.BusinessRule;
import org.wso2.carbon.business.rules.core.bean.RuleTemplate;
import org.wso2.carbon.business.rules.core.bean.TemplateGroup;
import org.wso2.carbon.business.rules.core.bean.TemplateManagerInstance;
import org.wso2.carbon.business.rules.core.bean.scratch.BusinessRuleFromScratch;
import org.wso2.carbon.business.rules.core.bean.template.BusinessRuleFromTemplate;
import org.wso2.carbon.business.rules.core.exceptions.TemplateManagerException;
import org.wso2.carbon.business.rules.core.services.TemplateManagerService;
import org.wso2.carbon.business.rules.core.util.TemplateManagerConstants;
import org.wso2.carbon.business.rules.core.util.TemplateManagerHelper;

import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.Map;

@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaMSF4JServerCodegen",
        date = "2017-10-13T06:19:32.032Z")
public class BusinessRulesApiServiceImpl extends BusinessRulesApiService {
    private static final Logger log = LoggerFactory.getLogger(BusinessRulesApiServiceImpl.class);

    @Override
    public Response createBusinessRule(String businessRule, Boolean deploy
    ) throws NotFoundException {
        TemplateManagerService templateManagerService = TemplateManagerInstance.getInstance();

        // convert the string received from API, as a json object
        Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
        JsonObject businessRuleJson = gson.fromJson(businessRule, JsonObject.class);

        // Check the business rule type of the json object
        if (businessRuleJson.get("type").toString().equals("\""+TemplateManagerConstants
                .BUSINESS_RULE_TYPE_TEMPLATE+"\"")) {
            // Convert to business rule from template and create
            BusinessRuleFromTemplate businessRuleFromTemplate = TemplateManagerHelper
                    .jsonToBusinessRuleFromTemplate(businessRule);

            templateManagerService.createBusinessRuleFromTemplate(businessRuleFromTemplate);
        } else {
            BusinessRuleFromScratch businessRuleFromScratch = TemplateManagerHelper.jsonToBusinessRuleFromScratch
                    (businessRule);

            templateManagerService.createBusinessRuleFromScratch(businessRuleFromScratch);
        }

        return Response.ok().entity(new ApiResponseMessage(ApiResponseMessage.OK, "Successfully created!")).build();
    }

    @Override
    public Response deleteBusinessRule(String businessRuleInstanceID
            , Boolean forceDelete
    ) throws NotFoundException {
        TemplateManagerService templateManagerService = TemplateManagerInstance.getInstance();
        try {
            templateManagerService.deleteBusinessRule(businessRuleInstanceID);
        } catch (TemplateManagerException e) {
            log.error(e.getMessage(), e);
        }
        return Response.ok().entity(new ApiResponseMessage(ApiResponseMessage.OK, "Business Rule deleted " +
                "successfully!")).build();
    }

    @Override
    public Response getBusinessRules() throws NotFoundException {
        // todo: connect with 'loadBusinessRules()' in backend
        TemplateManagerService templateManagerService = TemplateManagerInstance.getInstance();
        Map<String, BusinessRule> businessRuleMap = templateManagerService.loadBusinessRules();
        if (businessRuleMap == null) {
            return Response.serverError().build();
        }

        ArrayList<BusinessRule> businessRulesWithoutUUID = new ArrayList();
        for(String businessRuleUUID : businessRuleMap.keySet()){
            businessRulesWithoutUUID.add(businessRuleMap.get(businessRuleUUID));
        }
        Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();

        return Response.ok().entity(gson.toJson(businessRulesWithoutUUID)).build();
    }

    @Override
    public Response getRuleTemplate(String templateGroupID, String ruleTemplateID ) throws NotFoundException {
        TemplateManagerService templateManagerService = TemplateManagerInstance.getInstance();
        try {
            RuleTemplate ruleTemplate = templateManagerService.getRuleTemplate(templateGroupID, ruleTemplateID);
            return Response.ok().entity(ruleTemplate).build();
        } catch (TemplateManagerException e) {
            return Response.status(404).entity(new ApiResponseMessage(ApiResponseMessage.ERROR,
                    e.getMessage())).build();
        }
    }

    @Override
    public Response getRuleTemplates(String templateGroupID ) throws NotFoundException {
        // do some magic!
        TemplateManagerService templateManagerService = TemplateManagerInstance.getInstance();
        try {
            // Get rule templates and store without UUIDs
            Map<String, RuleTemplate> ruleTemplates = templateManagerService.getRuleTemplates(templateGroupID);
            ArrayList<RuleTemplate> ruleTemplatesWithoutUUID = new ArrayList();
            for (String ruleTemplateUUID : ruleTemplates.keySet()) {
                ruleTemplatesWithoutUUID.add(ruleTemplates.get(ruleTemplateUUID));
            }
            Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();

            return Response.ok().entity(gson.toJson(ruleTemplatesWithoutUUID)).build();
        } catch (TemplateManagerException e) {
            return Response.status(404).build();
        }
    }

    @Override
    public Response getTemplateGroup(String templateGroupID
    ) throws NotFoundException {
        TemplateManagerService templateManagerService = TemplateManagerInstance.getInstance();
        try {
            TemplateGroup templateGroup = templateManagerService.getTemplateGroup(templateGroupID);
            return Response.ok().entity(templateManagerService.getTemplateGroup(templateGroupID)).build();
        } catch (TemplateManagerException e) {
            return Response.status(404).build();
        }
    }

{'fraction_non_alphanumeric': 0.08910127948204101, 'fraction_numerical': 0.006628641899182982, 'mean_word_length': 3.6875, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4676135', 'n_tokens_mistral': 3324, 'n_tokens_neox': 3205, 'n_words': 569}