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

com.kukababy.plus.utils.JsUtil Maven / Gradle / Ivy

The newest version!
/**
 * 
 */
package com.kukababy.plus.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.kukababy.plus.exception.PlusRuntimeException;
import com.kukababy.plus.pager.CustSqlInfo;
import com.kukababy.plus.pager.SqlInfo;
import com.kukababy.plus.pojo.P;

/**
 * 描述: 
*
* * @author [email protected] * @date 2019年6月29日 上午10:04:17 */ public class JsUtil { private static final Logger log = LoggerFactory.getLogger(JsUtil.class); private static final ScriptEngineManager engineManager = new ScriptEngineManager(); public static String mergeJs(String classPath, Class currClass) { // mapper/sys/ String path = currClass.getClassLoader().getResource(classPath).getPath(); if (log.isDebugEnabled()) { // log.debug(Constant.LogPre + path); } File dirFile = new File(path); String[] filelist = dirFile.list(); StringBuilder sbJs = new StringBuilder(); for (int i = 0; i < filelist.length; i++) { File readfile = new File(path + filelist[i]); BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(readfile)); String tempString = null; while ((tempString = reader.readLine()) != null) { sbJs.append(tempString + System.getProperty("line.separator")); } } catch (IOException e) { e.printStackTrace(); System.exit(1); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } } return sbJs.toString(); } public static ScriptEngine getScriptEngine(String jsContent) { ScriptEngine engine = engineManager.getEngineByName("nashorn"); try { Object a1 = engine.eval(jsContent); } catch (ScriptException e) { e.printStackTrace(); System.exit(1); } return engine; } public static String[] getParamsName(String functionName, ScriptEngine engine) { String funContent = engine.get(functionName).toString(); int startPos = funContent.indexOf("("); int endPos = funContent.indexOf(")"); String paramStr = funContent.substring(startPos + 1, endPos); String paramNames[] = paramStr.split(","); return paramNames; } public static String getHoldSqlBak(String functionName, String[] paramNames, ScriptEngine engine, List

params) { StringBuilder jsFun = new StringBuilder(); jsFun.append(functionName + "("); for (int i = 0; i < paramNames.length; i++) { String pName = paramNames[i].trim(); for (P param : params) { if (param.getCol().equalsIgnoreCase(pName)) { if (i > 0) { jsFun.append(","); } engine.put(pName, param.getVal()); jsFun.append(pName); } } } jsFun.append(")"); try { String sqlHold = (String) engine.eval(jsFun.toString()); return sqlHold; } catch (ScriptException e) { throw new PlusRuntimeException(e); } } public static String getHoldSql(String functionName, String[] paramNames, ScriptEngine engine, List

params) { StringBuilder jsFun = new StringBuilder(); jsFun.append(functionName + "("); for (int i = 0; i < paramNames.length; i++) { String pName = paramNames[i].trim(); for (P param : params) { if (param.getCol().equalsIgnoreCase(pName)) { if (i > 0) { jsFun.append(","); } if (param.getVal() instanceof Number) { jsFun.append(param.getVal()); } else { jsFun.append("'" + param.getVal() + "'"); } } } } jsFun.append(")"); try { log.info(jsFun.toString()); String sqlHold = (String) engine.eval(jsFun.toString()); return sqlHold; } catch (ScriptException e) { throw new PlusRuntimeException(e); } } public static CustSqlInfo getCustSqlInfo(String functionName,ScriptEngine engine, List

keyVals) { String paramNames[] = getParamsName(functionName, engine); String sqlHold = getHoldSql(functionName, paramNames, engine, keyVals); CustSqlInfo sqlInfo = HoldUtil.getCustSqlAndVals(sqlHold, keyVals); return sqlInfo; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy