<commit_before>package au.org.biodiversity.nsl

import au.org.biodiversity.nsl.tree.Message
import au.org.biodiversity.nsl.tree.ServiceException
import grails.converters.JSON
import org.hibernate.JDBCException

import java.sql.SQLException

/**
 * This utility class recurses through a top-level tree services message object
 * and creates the 'displayMessage' array. This makes everything simple.
 *
 * Created by ibis on 3/1/17.
 */
class TreeServiceMessageUtil {
    static List unpackMessage(Message m, status = "danger") {
        List msg = [];
        if (m != null) {
            msg << [msg: m.getHumanReadableMessage(), status: status]
            unrollAllNested(msg, m)
        }
        return msg
    }

    static List unpackThrowable(Throwable t, String status = 'danger') {
        if (t == null) {
            return []
        } else if (t instanceof ServiceException) {
            return unpackServiceException((ServiceException) t, status);
        } else if (t instanceof JDBCException) {
            return unpackJDBCException((JDBCException) t, status);
        } else {
            return unpackOtherException(t, status);
        }

    }

    private static List unpackJDBCException(JDBCException ex, String status) {
        List msg = []

        if (ex != null) {
            msg << [msg: ex.getClass().getSimpleName(), body: ex.getLocalizedMessage(), status: status]

            for (SQLException sex = ex.getSQLException(); sex != null; sex = sex.getNextException()) {
                msg += unpackThrowable(sex, 'info')
            }
        }
        return msg
    }

    private static List unpackServiceException(ServiceException ex, String status) {
        List msg = []
        if (ex != null) {
            msg += unpackMessage(ex.message, status)
            if (ex.getCause() && ex.getCause() != ex) {
                msg += unpackThrowable(ex.getCause(), status);
            }
        }
        return msg
    }

    private static List unpackOtherException(Throwable t, String status) {
        if(t==null) return [];

        List msg = [
                [msg: t.getClass().getSimpleName(), body: t.getLocalizedMessage(), status: status]
        ]
        if (t.getCause() && t.getCause() != t) {
            msg += unpackThrowable(t.getCause(), status);
        }
        return msg;
    }

    static List unpackStacktrace(Throwable t) {
        if(t==null) return [];
        return t.getStackTrace().findAll {
            StackTraceElement it -> it.fileName && it.lineNumber != -1 && it.className.startsWith('au.org.biodiversity.nsl.')
        }.collect {
            StackTraceElement it -> [file: it.fileName, line: it.lineNumber, method: it.methodName, clazz: it.className]<commit_msg>Stuff.<commit_after>package au.org.biodiversity.nsl

import au.org.biodiversity.nsl.tree.Message
import au.org.biodiversity.nsl.tree.ServiceException
import grails.converters.JSON
import org.hibernate.JDBCException

import java.sql.SQLException

/**
 * This utility class recurses through a top-level tree services message object
 * and creates the 'displayMessage' array. This makes everything simple.
 *
 * Created by ibis on 3/1/17.
 */
class TreeServiceMessageUtil {
    static List unpackMessage(Message m, status = "danger") {
        List msg = [];
        if (m != null) {
            msg << [msg: m.getHumanReadableMessage(), status: status]
            unrollAllNested(msg, m)
        }
        return msg
    }

    static List unpackThrowable(Throwable t, String status = 'danger') {
        if (t == null) {
            return []
        } else if (t instanceof ServiceException) {
            return unpackServiceException((ServiceException) t, status);
        } else if (t instanceof JDBCException) {
            return unpackJDBCException((JDBCException) t, status);
        } else {
            return unpackOtherException(t, status);
        }

    }

    private static List unpackJDBCException(JDBCException ex, String status) {
        List msg = []

        if (ex != null) {
            msg << [msg: ex.getClass().getSimpleName(), body: ex.getLocalizedMessage(), status: status]

            for (SQLException sex = ex.getSQLException(); sex != null; sex = sex.getNextException()) {
                msg += unpackThrowable(sex, 'info')
            }
        }
        return msg
    }

    private static List unpackServiceException(ServiceException ex, String status) {
        List msg = []
        if (ex != null) {
            msg += unpackMessage(ex.msg, status)
            if (ex.getCause() && ex.getCause() != ex) {
                msg += unpackThrowable(ex.getCause(), status);
            }
        }
        return msg
    }

    private static List unpackOtherException(Throwable t, String status) {
        if(t==null) return [];

        List msg = [
                [msg: t.getClass().getSimpleName(), body: t.getLocalizedMessage(), status: status]
        ]
        if (t.getCause() && t.getCause() != t) {
            msg += unpackThrowable(t.getCause(), status);
        }
        return msg;
    }

    static List unpackStacktrace(Throwable t) {
        if(t==null) return [];
        return t.getStackTrace().findAll {
            StackTraceElement it -> it.fileName && it.lineNumber != -1 && it.className.startsWith('au.org.biodiversity.nsl.')
        }.collect {
            StackTraceElement it -> [file: it.fileName, line: it.lineNumber, method: it.methodName, clazz: it.className]
{'fraction_non_alphanumeric': 0.11720607057963064, 'fraction_numerical': 0.0018284878405558603, 'mean_word_length': 2.283313325330132, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5325066', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1500, 'n_words': 397}