<commit_before>package ai.susi.server.api.cms;

import ai.susi.DAO;
import ai.susi.json.JsonObjectWithDefault;
import ai.susi.server.*;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.json.JSONObject;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Objects;

/**
 * Created by chetankaushik on 07/06/17.
 * This Endpoint accepts 5 parameters. model,group,language,expert,content.
 * before modifying an expert the expert must exist in the directory.
 * !IMPORTANT! --> Content must be URL Encoded
 * http://localhost:4000/cms/modifyExpert.json?model=general&group=knowledge&expert=testing&content=What is stock price of *%3F|What is stock price of *|stock price of *|* stock price of *%0A!console%3A%24l%24%0A{%0A "url"%3A"http%3A%2F%2Ffinance.google.com%2Ffinance%2Finfo%3Fclient%3Dig%26q%3DNASDAQ%3A%241%24"%2C%0A "path"%3A"%24.[0]"%0A}%0Aeol%0A
 */
public class ModifyExpertService extends AbstractAPIHandler implements APIHandler {

    private static final long serialVersionUID = -1834363513093189312L;

    @Override
    public BaseUserRole getMinimalBaseUserRole() { return BaseUserRole.ANONYMOUS; }

    @Override
    public JSONObject getDefaultPermissions(BaseUserRole baseUserRole) {
        return null;
    }

    @Override
    public String getAPIPath() {
        return "/cms/modifyExpert.json";
    }

    @Override
    public ServiceResponse serviceImpl(Query call, HttpServletResponse response, Authorization rights, final JsonObjectWithDefault permissions) {

        String model_name = call.get("model", "general");
        File model = new File(DAO.model_watch_dir, model_name);
        String group_name = call.get("group", "knowledge");
        File group = new File(model, group_name);
        String language_name = call.get("language", "en");
        File language = new File(group, language_name);
        String expert_name = call.get("expert", null);
        File expert = new File(language, expert_name + ".txt");

        String commit_message = call.get("changelog", null);

        // Checking for file existence
        if(!expert.exists()){
            JSONObject error = new JSONObject();
            error.put("accepted", false);
            return new ServiceResponse(error);
        }
        // Reading Content for expert
        String content = call.get("content", "");
        if(Objects.equals(content, "")){
            JSONObject error = new JSONObject();
            error.put("accepted", false);
            return new ServiceResponse(error);
        }
        // Writing to File
        try (FileWriter file = new FileWriter(expert)) {
            file.write(content);
            JSONObject success = new JSONObject();
            success.put("accepted", true);
<commit_msg>Null check for commit message<commit_after>package ai.susi.server.api.cms;

import ai.susi.DAO;
import ai.susi.json.JsonObjectWithDefault;
import ai.susi.server.*;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.json.JSONObject;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Objects;

/**
 * Created by chetankaushik on 07/06/17.
 * This Endpoint accepts 5 parameters. model,group,language,expert,content.
 * before modifying an expert the expert must exist in the directory.
 * !IMPORTANT! --> Content must be URL Encoded
 * http://localhost:4000/cms/modifyExpert.json?model=general&group=knowledge&expert=testing&content=What is stock price of *%3F|What is stock price of *|stock price of *|* stock price of *%0A!console%3A%24l%24%0A{%0A "url"%3A"http%3A%2F%2Ffinance.google.com%2Ffinance%2Finfo%3Fclient%3Dig%26q%3DNASDAQ%3A%241%24"%2C%0A "path"%3A"%24.[0]"%0A}%0Aeol%0A
 */
public class ModifyExpertService extends AbstractAPIHandler implements APIHandler {

    private static final long serialVersionUID = -1834363513093189312L;

    @Override
    public BaseUserRole getMinimalBaseUserRole() { return BaseUserRole.ANONYMOUS; }

    @Override
    public JSONObject getDefaultPermissions(BaseUserRole baseUserRole) {
        return null;
    }

    @Override
    public String getAPIPath() {
        return "/cms/modifyExpert.json";
    }

    @Override
    public ServiceResponse serviceImpl(Query call, HttpServletResponse response, Authorization rights, final JsonObjectWithDefault permissions) {

        String model_name = call.get("model", "general");
        File model = new File(DAO.model_watch_dir, model_name);
        String group_name = call.get("group", "knowledge");
        File group = new File(model, group_name);
        String language_name = call.get("language", "en");
        File language = new File(group, language_name);
        String expert_name = call.get("expert", null);
        File expert = new File(language, expert_name + ".txt");

        String commit_message = call.get("changelog", null);
        if(commit_message==null){
            JSONObject error = new JSONObject();
            error.put("accepted", false);
            return new ServiceResponse(error);
        }
        // Checking for file existence
        if(!expert.exists()){
            JSONObject error = new JSONObject();
            error.put("accepted", false);
            return new ServiceResponse(error);
        }
        // Reading Content for expert
        String content = call.get("content", "");
        if(Objects.equals(content, "")){
            JSONObject error = new JSONObject();
            error.put("accepted", false);
            return new ServiceResponse(error);
        }
        // Writing to File
        try (FileWriter file = new FileWriter(expert)) {
            file.write(content);
            JSONObject success = new JSONObject();
            success.put("accepted", true);

{'fraction_non_alphanumeric': 0.11450008155276464, 'fraction_numerical': 0.021203718806067525, 'mean_word_length': 3.4020100502512562, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3013190', 'n_tokens_mistral': 1877, 'n_tokens_neox': 1797, 'n_words': 426}