All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.siwenyan.query.QueryBase Maven / Gradle / Ivy

package com.github.siwenyan.query;


import com.github.siwenyan.common.EasyJson;
import com.github.siwenyan.common.StringTools;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class QueryBase {

    private static Map buffer = new HashMap<>();

    public static String simplestJsonResult(Object result) {
        if (result instanceof List) {
            List objList = null;
            List> mapList = null;
            try {
                objList = (List) result;
            } catch (Exception e) {
                throw new RuntimeException("Unknown type in list: " + result);
            }
            try {
                mapList = (List>) result;
            } catch (Exception e) {
                // do nothing
            }

            if (null == mapList) {
                if (0 == objList.size()) {
                    return "";
                } else if (1 == objList.size()) {
                    return EasyJson.prettyJson(objList.get(0));
                } else {
                    return EasyJson.prettyJson(objList);
                }
            } else {
                if (0 == mapList.size()) {
                    return "";
                } else if (1 == mapList.size()) {
                    return EasyJson.prettyJson(mapList.get(0));
                } else {
                    return EasyJson.prettyJson(mapList);
                }
            }
        } else {
            String s = null == result ? "" : StringTools.peel(result.toString());
            return s;
        }
    }

    public abstract Object query(String path);

    public static QueryBase jsonpathSource(String body) {
        try {
            if (!buffer.containsKey(body)) {
                buffer.put(body, new JsonpathQuery(body));
            }
            return buffer.get(body);
        } catch (Exception e) {
            return null;
        }
    }

    public static QueryBase textSource(String text0) {
        if (!buffer.containsKey(text0)) {
            buffer.put(text0, new QueryBase() {

                String text = null;
                String[] lines = null;

                {
                    this.text = null == text0 ? "" : text0;
                    this.lines = this.text.split(System.getProperty("line.separator"));
                }

                @Override
                public String query(String lineNum) {
                    return lines[Integer.parseInt(lineNum) - 1];
                }

                @Override
                public String toString() {
                    return this.text;
                }

            });
        }
        return buffer.get(text0);
    }

    public static QueryBase mybatisSource(String factoryClassName) {
        try {
            //do not hit buffer for db queries
            //- data source may change
            return new MyBatisQuery(MyBatisUtils.getSqlSessionFactory(factoryClassName));
        } catch (Exception e) {
            return null;
        }
    }

}