class ConditionGenerator {

    public String generateScript(Condition condition) throws GenerateConditionException {
        if (condition == null) {
            throw new GenerateConditionException(MISSING_CONDITION_ERROR);
        }

        if (!isValidFunction(condition.getFunction())) {
            throw new GenerateConditionException(MessageFormat.format(FUNCTION_NOT_FOUND_ERROR, condition.getFunction()));
        }
        final String function = condition.getFunction().trim();
        final StringBuilder script = new StringBuilder();
        script.append("return ");
        script.append(ConditionParser.KIE_FUNCTIONS);
        script.append(function);
        script.append("(");
        boolean first = true;
        for (String param : condition.getParams()) {
            if (param == null || param.isEmpty()) {
                throw new GenerateConditionException(PARAMETER_NULL_EMPTY);
            }
            if (first) {
                //first parameter is always a process variable name.
                script.append(param);
                first = false;
            } else {
                //the other parameters are always string parameters.
                script.append(", ");
                script.append("\"");
                script.append(escapeJavaNonUTFChars(param));
                script.append("\"");
            }
        }
        script.append(");");
        return script.toString();
    }

}