提交 0112bfc7 编写于 作者: R Ray

update to json

上级 d95ce08b
o2server
\ No newline at end of file
......@@ -47,7 +47,7 @@ public class JsonScriptingExecutor {
JsonElement jsonElement = JsonNull.INSTANCE;
try {
Object o = cs.eval(scriptContext);
jsonElement = XGsonBuilder.instance().toJsonTree(o);
jsonElement = XGsonBuilder.instance().fromJson(Objects.toString(o, ""), JsonElement.class);
} catch (ScriptException e) {
LOGGER.error(e);
}
......@@ -85,7 +85,7 @@ public class JsonScriptingExecutor {
public static String evalString(CompiledScript cs, ScriptContext scriptContext) {
JsonElement jsonElement = jsonElement(cs, scriptContext);
if (jsonElement.isJsonPrimitive()) {
return jsonElement.getAsString();
return jsonElement.getAsJsonPrimitive().getAsString();
}
return null;
}
......
......@@ -30,8 +30,7 @@ public class ScriptingFactory {
private static CompiledScript compiledScriptInitialServiceScriptText;
private static CompiledScript compiledScriptInitialScriptText;
public static final String BINDING_NAME_RESOURCES = "resources";
public static final String BINDING_NAME_EFFECTIVEPERSON = "effectivePerson";
public static final String BINDING_NAME_WORKCONTEXT = "workContext";
public static final String BINDING_NAME_GSON = "gson";
public static final String BINDING_NAME_DATA = "data";
......@@ -40,13 +39,11 @@ public class ScriptingFactory {
public static final String BINDING_NAME_DICTIONARY = "dictionary";
public static final String BINDING_NAME_ROUTES = "routes";
public static final String BINDING_NAME_ROUTE = "routes";
public static final String BINDING_NAME_APPLICATIONS = "applications";
public static final String BINDING_NAME_ASSIGNDATA = "assignData";
public static final String BINDING_NAME_IDENTITY = "identity";
public static final String BINDING_NAME_PARAMETERS = "parameters";
public static final String BINDING_NAME_JAXRSRESPONSE = "jaxrsResponse";
public static final String BINDING_NAME_JAXWSRESPONSE = "jaxwsResponse";
......@@ -59,9 +56,14 @@ public class ScriptingFactory {
public static final String BINDING_NAME_SERIAL = "serial";
public static final String BINDING_NAME_PROCESS = "process";
public static final String BINDING_NAME_REQUESTTEXT = "requestText";
public static final String BINDING_NAME_REQUEST = "request";
public static final String BINDING_NAME_CUSTOMRESPONSE = "customResponse";
public static final String BINDING_NAME_RESOURCES = "java_resources";
public static final String BINDING_NAME_EFFECTIVEPERSON = "java_effectivePerson";
public static final String BINDING_NAME_CUSTOMRESPONSE = "java_customResponse";
public static final String BINDING_NAME_REQUESTTEXT = "java_requestText";
public static final String BINDING_NAME_REQUEST = "java_request";
public static final String BINDING_NAME_PARAMETERS = "java_parameters";
public static ScriptEngine newScriptEngine() {
return (new ScriptEngineManager()).getEngineByName(Config.SCRIPTING_ENGINE_NAME);
......
package com.x.base.core.project.scripting;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.x.base.core.project.gson.XGsonBuilder;
public class TestClient {
public static void main(String[] args) {
String text = "\"1234\"";
Integer n = 5;
Boolean b = true;
Map<String, String> a = new HashMap<>();
a.put("ddd", "ccc");
// String abc = XGsonBuilder.toJson(a);
// System.out.println(abc);
JsonElement js = XGsonBuilder.instance().toJsonTree(text);
System.out.println(js);
//
System.out.println(js.isJsonPrimitive() + "!!");
System.out.println(js.getAsJsonPrimitive().isString());
System.out.println(js.getAsJsonPrimitive().getAsJsonPrimitive().isString());
System.out.println(js.getAsString());
}
}
......@@ -7,7 +7,6 @@ import java.util.concurrent.CopyOnWriteArrayList;
import javax.script.Bindings;
import javax.script.CompiledScript;
import javax.script.ScriptContext;
import javax.script.SimpleScriptContext;
import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
......@@ -21,7 +20,7 @@ import com.x.base.core.project.jaxrs.WoId;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.script.AbstractResources;
import com.x.base.core.project.script.ScriptFactory;
import com.x.base.core.project.scripting.ScriptingFactory;
import com.x.base.core.project.tools.DateTools;
import com.x.base.core.project.webservices.WebservicesClient;
import com.x.organization.core.express.Organization;
......@@ -31,7 +30,7 @@ import com.x.program.center.core.entity.Agent;
class ActionExecute extends BaseAction {
private static Logger logger = LoggerFactory.getLogger(ActionExecute.class);
private static final Logger LOGGER = LoggerFactory.getLogger(ActionExecute.class);
private static final CopyOnWriteArrayList<String> LOCK = new CopyOnWriteArrayList<>();
......@@ -39,7 +38,7 @@ class ActionExecute extends BaseAction {
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
Business business = new Business(emc);
/* 判断当前用户是否有权限访问 */
if(!business.serviceControlAble(effectivePerson)) {
if (!business.serviceControlAble(effectivePerson)) {
throw new ExceptionAccessDenied(effectivePerson.getDistinguishedName());
}
ActionResult<Wo> result = new ActionResult<>();
......@@ -56,30 +55,27 @@ class ActionExecute extends BaseAction {
emc.beginTransaction(Agent.class);
agent.setLastStartTime(new Date());
emc.commit();
ScriptContext scriptContext = new SimpleScriptContext();
ScriptContext scriptContext = ScriptingFactory.scriptContextEvalInitialServiceScript();
Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
Resources resources = new Resources();
resources.setEntityManagerContainer(emc);
resources.setContext(ThisApplication.context());
resources.setOrganization(new Organization(ThisApplication.context()));
resources.setApplications(ThisApplication.context().applications());
resources.setWebservicesClient(new WebservicesClient());
bindings.put(ScriptFactory.BINDING_NAME_RESOURCES, resources);
bindings.put(ScriptFactory.BINDING_NAME_APPLICATIONS, ThisApplication.context().applications());
bindings.put(ScriptingFactory.BINDING_NAME_RESOURCES, resources);
CacheCategory cacheCategory = new CacheCategory(Agent.class);
CacheKey cacheKey = new CacheKey(ActionExecute.class, agent.getId());
CompiledScript compiledScript = null;
Optional<?> optional = CacheManager.get(cacheCategory, cacheKey);
if (optional.isPresent()) {
compiledScript = (CompiledScript)optional.get();
}else {
compiledScript = ScriptFactory.compile(ScriptFactory.functionalization(agent.getText()));
compiledScript = (CompiledScript) optional.get();
} else {
compiledScript = ScriptingFactory.functionalizationCompile(agent.getText());
CacheManager.put(cacheCategory, cacheKey, compiledScript);
}
try {
ScriptFactory.initialServiceScriptText().eval(scriptContext);
compiledScript.eval(scriptContext);
} catch (Exception e) {
throw new ExceptionAgentEval(e, e.getMessage(), agent.getId(), agent.getName(),
......
......@@ -97,7 +97,6 @@ abstract class BaseAction extends StandardJaxrsAction {
bindings.put(ScriptingFactory.BINDING_NAME_REQUESTTEXT, gson.toJson(jsonElement));
bindings.put(ScriptingFactory.BINDING_NAME_REQUEST, request);
bindings.put(ScriptingFactory.BINDING_NAME_EFFECTIVEPERSON, effectivePerson);
bindings.put(ScriptingFactory.BINDING_NAME_APPLICATIONS, ThisApplication.context().applications());
bindings.put(ScriptingFactory.BINDING_NAME_CUSTOMRESPONSE, customResponse);
}
......
package com.x.program.center.jaxrs.schedule;
import java.util.Date;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
......@@ -23,27 +24,26 @@ import com.x.program.center.ThisApplication;
class ActionFire extends BaseAction {
private static Logger logger = LoggerFactory.getLogger(ActionFire.class);
private static final Logger LOGGER = LoggerFactory.getLogger(ActionFire.class);
ActionResult<Wo> execute(EffectivePerson effectivePerson, JsonElement jsonElement) throws Exception {
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
ActionResult<Wo> result = new ActionResult<>();
Wo wo = new Wo();
Wi wi = this.convertToWrapIn(jsonElement, Wi.class);
if (effectivePerson.isNotManager()) {
throw new ExceptionAccessDenied(effectivePerson);
}
Application application = ThisApplication.context().applications().get(wi.getApplication(), wi.getNode());
ScheduleRequest request = null;
for (ScheduleRequest o : application.getScheduleRequestList()) {
if (StringUtils.equalsIgnoreCase(wi.getClassName(), o.getClassName())) {
request = o;
break;
}
Optional<ScheduleRequest> optional = application.getScheduleRequestList().stream()
.filter(o -> StringUtils.equalsIgnoreCase(wi.getClassName(), o.getClassName())).findFirst();
if (optional.isPresent()) {
this.fire(effectivePerson, application, optional.get());
wo.setValue(true);
} else {
wo.setValue(false);
}
this.fire(effectivePerson, application, request);
Wo wo = new Wo();
wo.setValue(true);
result.setData(wo);
return result;
}
......@@ -56,7 +56,7 @@ class ActionFire extends BaseAction {
+ Applications.joinQueryUri("fireschedule", "classname", request.getClassName());
CipherConnectionAction.get(effectivePerson.getDebugger(), url);
request.setLastStartTime(new Date());
logger.info("fire schedule className: {}, application: {}, node: {}.", request.getClassName(),
LOGGER.info("fire schedule className: {}, application: {}, node: {}.", request.getClassName(),
application.getClassName(), application.getNode());
} catch (Exception e) {
throw new ExceptionFire(e, request.getClassName(), application.getClassName(), application.getNode());
......
package com.x.query.assemble.designer.jaxrs.statement;
import java.util.Objects;
import javax.persistence.EntityManager;
import javax.persistence.Parameter;
import javax.persistence.Query;
import javax.script.Bindings;
import javax.script.CompiledScript;
import javax.script.ScriptContext;
import javax.script.SimpleScriptContext;
import org.apache.commons.lang3.StringUtils;
......@@ -22,7 +20,8 @@ import com.x.base.core.project.exception.ExceptionEntityNotExist;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.script.AbstractResources;
import com.x.base.core.project.script.ScriptFactory;
import com.x.base.core.project.scripting.JsonScriptingExecutor;
import com.x.base.core.project.scripting.ScriptingFactory;
import com.x.base.core.project.webservices.WebservicesClient;
import com.x.organization.core.express.Organization;
import com.x.query.assemble.designer.Business;
......@@ -33,7 +32,7 @@ import com.x.query.core.express.statement.Runtime;
class ActionExecute extends BaseAction {
private final static String[] pageKeys = { "GROUP BY", " COUNT(" };
private static final String[] pageKeys = { "GROUP BY", " COUNT(" };
ActionResult<Object> execute(EffectivePerson effectivePerson, String flag, Integer page, Integer size,
JsonElement jsonElement) throws Exception {
......@@ -53,14 +52,12 @@ class ActionExecute extends BaseAction {
Object data = null;
switch (Objects.toString(statement.getFormat(), "")) {
case Statement.FORMAT_SCRIPT:
data = this.script(effectivePerson, business, statement, runtime);
break;
default:
data = this.jpql(effectivePerson, business, statement, runtime);
break;
if (StringUtils.equalsIgnoreCase(statement.getFormat(), Statement.FORMAT_SCRIPT)) {
data = this.script(effectivePerson, business, statement, runtime);
} else {
data = this.jpql(business, statement, runtime);
}
result.setData(data);
return result;
}
......@@ -69,17 +66,18 @@ class ActionExecute extends BaseAction {
private Object script(EffectivePerson effectivePerson, Business business, Statement statement, Runtime runtime)
throws Exception {
Object data = null;
ScriptContext scriptContext = this.scriptContext(effectivePerson, business, runtime);
ScriptFactory.initialServiceScriptText().eval(scriptContext);
Object o = ScriptFactory.scriptEngine.eval(ScriptFactory.functionalization(statement.getScriptText()),
scriptContext);
String text = ScriptFactory.asString(o);
ScriptContext scriptContext = this.scriptContext(effectivePerson, runtime);
CompiledScript cs = ScriptingFactory.functionalizationCompile(statement.getScriptText());
String text = JsonScriptingExecutor.evalString(cs, scriptContext);
// Object o = ScriptFactory.scriptEngine.eval(ScriptFactory.functionalization(statement.getScriptText()),
// scriptContext);
// String text = ScriptFactory.asString(o);
Class<? extends JpaObject> cls = this.clazz(business, statement);
EntityManager em;
if(StringUtils.equalsIgnoreCase(statement.getEntityCategory(), Statement.ENTITYCATEGORY_DYNAMIC)
&& StringUtils.equalsIgnoreCase(statement.getType(), Statement.TYPE_SELECT)){
if (StringUtils.equalsIgnoreCase(statement.getEntityCategory(), Statement.ENTITYCATEGORY_DYNAMIC)
&& StringUtils.equalsIgnoreCase(statement.getType(), Statement.TYPE_SELECT)) {
em = business.entityManagerContainer().get(DynamicBaseEntity.class);
}else{
} else {
em = business.entityManagerContainer().get(cls);
}
Query query = em.createQuery(text);
......@@ -89,7 +87,7 @@ class ActionExecute extends BaseAction {
}
}
if (StringUtils.equalsIgnoreCase(statement.getType(), Statement.TYPE_SELECT)) {
if(isPageSql(text)) {
if (isPageSql(text)) {
query.setFirstResult((runtime.page - 1) * runtime.size);
query.setMaxResults(runtime.size);
}
......@@ -102,15 +100,14 @@ class ActionExecute extends BaseAction {
return data;
}
private Object jpql(EffectivePerson effectivePerson, Business business, Statement statement, Runtime runtime)
throws Exception {
private Object jpql(Business business, Statement statement, Runtime runtime) throws Exception {
Object data = null;
Class<? extends JpaObject> cls = this.clazz(business, statement);
EntityManager em;
if(StringUtils.equalsIgnoreCase(statement.getEntityCategory(), Statement.ENTITYCATEGORY_DYNAMIC)
&& StringUtils.equalsIgnoreCase(statement.getType(), Statement.TYPE_SELECT)){
if (StringUtils.equalsIgnoreCase(statement.getEntityCategory(), Statement.ENTITYCATEGORY_DYNAMIC)
&& StringUtils.equalsIgnoreCase(statement.getType(), Statement.TYPE_SELECT)) {
em = business.entityManagerContainer().get(DynamicBaseEntity.class);
}else{
} else {
em = business.entityManagerContainer().get(cls);
}
Query query = em.createQuery(statement.getData());
......@@ -120,7 +117,7 @@ class ActionExecute extends BaseAction {
}
}
if (StringUtils.equalsIgnoreCase(statement.getType(), Statement.TYPE_SELECT)) {
if(isPageSql(statement.getData())) {
if (isPageSql(statement.getData())) {
query.setFirstResult((runtime.page - 1) * runtime.size);
query.setMaxResults(runtime.size);
}
......@@ -133,7 +130,7 @@ class ActionExecute extends BaseAction {
return data;
}
private boolean isPageSql(String sql){
private boolean isPageSql(String sql) {
sql = sql.toUpperCase().replaceAll("\\s{1,}", " ");
for (String key : pageKeys) {
if (sql.indexOf(key) > -1) {
......@@ -159,19 +156,17 @@ class ActionExecute extends BaseAction {
return cls;
}
private ScriptContext scriptContext(EffectivePerson effectivePerson, Business business, Runtime runtime)
throws Exception {
ScriptContext scriptContext = new SimpleScriptContext();
private ScriptContext scriptContext(EffectivePerson effectivePerson, Runtime runtime) throws Exception {
ScriptContext scriptContext = ScriptingFactory.scriptContextEvalInitialServiceScript();
Resources resources = new Resources();
resources.setEntityManagerContainer(business.entityManagerContainer());
resources.setContext(ThisApplication.context());
resources.setApplications(ThisApplication.context().applications());
resources.setWebservicesClient(new WebservicesClient());
resources.setOrganization(new Organization(ThisApplication.context()));
Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put(ScriptFactory.BINDING_NAME_RESOURCES, resources);
bindings.put(ScriptFactory.BINDING_NAME_EFFECTIVEPERSON, effectivePerson);
bindings.put(ScriptFactory.BINDING_NAME_PARAMETERS, gson.toJson(runtime.getParameters()));
bindings.put(ScriptingFactory.BINDING_NAME_RESOURCES, resources);
bindings.put(ScriptingFactory.BINDING_NAME_EFFECTIVEPERSON, effectivePerson);
bindings.put(ScriptingFactory.BINDING_NAME_PARAMETERS, gson.toJson(runtime.getParameters()));
return scriptContext;
}
......
......@@ -7,8 +7,8 @@ import javax.persistence.EntityManager;
import javax.persistence.Parameter;
import javax.persistence.Query;
import javax.script.Bindings;
import javax.script.CompiledScript;
import javax.script.ScriptContext;
import javax.script.SimpleScriptContext;
import org.apache.commons.lang3.StringUtils;
......@@ -25,7 +25,8 @@ import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.script.AbstractResources;
import com.x.base.core.project.script.ScriptFactory;
import com.x.base.core.project.scripting.JsonScriptingExecutor;
import com.x.base.core.project.scripting.ScriptingFactory;
import com.x.base.core.project.tools.ListTools;
import com.x.base.core.project.webservices.WebservicesClient;
import com.x.organization.core.express.Organization;
......@@ -37,11 +38,11 @@ import com.x.query.core.express.statement.Runtime;
class ActionExecuteV2 extends BaseAction {
private static Logger logger = LoggerFactory.getLogger(ActionExecuteV2.class);
private static final Logger LOGGER = LoggerFactory.getLogger(ActionExecuteV2.class);
private final static String[] pageKeys = { "GROUP BY", " COUNT(" };
private final static String JOIN_KEY = " JOIN ";
private final static String JOIN_ON_KEY = " ON ";
private static final String[] pageKeys = { "GROUP BY", " COUNT(" };
private static final String JOIN_KEY = " JOIN ";
private static final String JOIN_ON_KEY = " ON ";
ActionResult<Object> execute(EffectivePerson effectivePerson, String flag, String mode, Integer page, Integer size,
JsonElement jsonElement) throws Exception {
......@@ -61,74 +62,74 @@ class ActionExecuteV2 extends BaseAction {
Object data = null;
Object count = null;
switch (mode){
case Statement.MODE_DATA:
switch (Objects.toString(statement.getFormat(), "")) {
case Statement.FORMAT_SCRIPT:
data = this.script(effectivePerson, business, statement, runtime, mode);
break;
default:
data = this.jpql(effectivePerson, business, statement, runtime, mode);
break;
}
result.setData(data);
switch (mode) {
case Statement.MODE_DATA:
switch (Objects.toString(statement.getFormat(), "")) {
case Statement.FORMAT_SCRIPT:
data = this.script(effectivePerson, business, statement, runtime, mode);
break;
case Statement.MODE_COUNT:
switch (Objects.toString(statement.getFormat(), "")) {
case Statement.FORMAT_SCRIPT:
count = this.script(effectivePerson, business, statement, runtime, mode);
break;
default:
count = this.jpql(effectivePerson, business, statement, runtime, mode);
break;
}
result.setData(count);
result.setCount((Long)count);
default:
data = this.jpql(business, statement, runtime, mode);
break;
}
result.setData(data);
break;
case Statement.MODE_COUNT:
switch (Objects.toString(statement.getFormat(), "")) {
case Statement.FORMAT_SCRIPT:
count = this.script(effectivePerson, business, statement, runtime, mode);
break;
default:
count = this.jpql(business, statement, runtime, mode);
break;
}
result.setData(count);
result.setCount((Long) count);
break;
default:
switch (Objects.toString(statement.getFormat(), "")) {
case Statement.FORMAT_SCRIPT:
data = this.script(effectivePerson, business, statement, runtime, Statement.MODE_DATA);
count = this.script(effectivePerson, business, statement, runtime, Statement.MODE_COUNT);
break;
default:
switch (Objects.toString(statement.getFormat(), "")) {
case Statement.FORMAT_SCRIPT:
data = this.script(effectivePerson, business, statement, runtime, Statement.MODE_DATA);
count = this.script(effectivePerson, business, statement, runtime, Statement.MODE_COUNT);
break;
default:
data = this.jpql(effectivePerson, business, statement, runtime, Statement.MODE_DATA);
count = this.jpql(effectivePerson, business, statement, runtime, Statement.MODE_COUNT);
break;
}
result.setData(data);
result.setCount((Long)count);
data = this.jpql(business, statement, runtime, Statement.MODE_DATA);
count = this.jpql(business, statement, runtime, Statement.MODE_COUNT);
break;
}
result.setData(data);
result.setCount((Long) count);
}
return result;
}
}
private Object script(EffectivePerson effectivePerson, Business business, Statement statement, Runtime runtime, String mode)
throws Exception {
private Object script(EffectivePerson effectivePerson, Business business, Statement statement, Runtime runtime,
String mode) throws Exception {
Object data = null;
ScriptContext scriptContext = this.scriptContext(effectivePerson, business, runtime);
ScriptFactory.initialServiceScriptText().eval(scriptContext);
ScriptContext scriptContext = this.scriptContext(effectivePerson, runtime);
String scriptText = statement.getScriptText();
if(Statement.MODE_COUNT.equals(mode)) {
if (Statement.MODE_COUNT.equals(mode)) {
scriptText = statement.getCountScriptText();
}
Object o = ScriptFactory.scriptEngine.eval(ScriptFactory.functionalization(scriptText),
scriptContext);
String jpql = ScriptFactory.asString(o);
CompiledScript cs = ScriptingFactory.functionalizationCompile(scriptText);
String jpql = JsonScriptingExecutor.evalString(cs, scriptContext);
// Object o = ScriptFactory.scriptEngine.eval(ScriptFactory.functionalization(scriptText), scriptContext);
// String jpql = ScriptFactory.asString(o);
Class<? extends JpaObject> cls = this.clazz(business, statement);
EntityManager em;
if(StringUtils.equalsIgnoreCase(statement.getEntityCategory(), Statement.ENTITYCATEGORY_DYNAMIC)
&& StringUtils.equalsIgnoreCase(statement.getType(), Statement.TYPE_SELECT)){
if (StringUtils.equalsIgnoreCase(statement.getEntityCategory(), Statement.ENTITYCATEGORY_DYNAMIC)
&& StringUtils.equalsIgnoreCase(statement.getType(), Statement.TYPE_SELECT)) {
em = business.entityManagerContainer().get(DynamicBaseEntity.class);
}else{
} else {
em = business.entityManagerContainer().get(cls);
}
jpql = this.joinSql(jpql, business);
Query query;
String upJpql = jpql.toUpperCase();
if(upJpql.indexOf(JOIN_KEY) > -1 && upJpql.indexOf(JOIN_ON_KEY) > -1){
if (upJpql.indexOf(JOIN_KEY) > -1 && upJpql.indexOf(JOIN_ON_KEY) > -1) {
query = em.createNativeQuery(jpql);
}else{
} else {
query = em.createQuery(jpql);
}
for (Parameter<?> p : query.getParameters()) {
......@@ -137,10 +138,10 @@ class ActionExecuteV2 extends BaseAction {
}
}
if (StringUtils.equalsIgnoreCase(statement.getType(), Statement.TYPE_SELECT)) {
if(Statement.MODE_COUNT.equals(mode)) {
if (Statement.MODE_COUNT.equals(mode)) {
data = query.getSingleResult();
}else{
if(isPageSql(jpql)) {
} else {
if (isPageSql(jpql)) {
query.setFirstResult((runtime.page - 1) * runtime.size);
query.setMaxResults(runtime.size);
}
......@@ -154,27 +155,26 @@ class ActionExecuteV2 extends BaseAction {
return data;
}
private Object jpql(EffectivePerson effectivePerson, Business business, Statement statement, Runtime runtime, String mode)
throws Exception {
private Object jpql(Business business, Statement statement, Runtime runtime, String mode) throws Exception {
Object data = null;
Class<? extends JpaObject> cls = this.clazz(business, statement);
EntityManager em;
if(StringUtils.equalsIgnoreCase(statement.getEntityCategory(), Statement.ENTITYCATEGORY_DYNAMIC)
&& StringUtils.equalsIgnoreCase(statement.getType(), Statement.TYPE_SELECT)){
if (StringUtils.equalsIgnoreCase(statement.getEntityCategory(), Statement.ENTITYCATEGORY_DYNAMIC)
&& StringUtils.equalsIgnoreCase(statement.getType(), Statement.TYPE_SELECT)) {
em = business.entityManagerContainer().get(DynamicBaseEntity.class);
}else{
} else {
em = business.entityManagerContainer().get(cls);
}
String jpql = statement.getData();
if(Statement.MODE_COUNT.equals(mode)) {
if (Statement.MODE_COUNT.equals(mode)) {
jpql = statement.getCountData();
}
jpql = this.joinSql(jpql, business);
Query query;
String upJpql = jpql.toUpperCase();
if(upJpql.indexOf(JOIN_KEY) > -1 && upJpql.indexOf(JOIN_ON_KEY) > -1){
if (upJpql.indexOf(JOIN_KEY) > -1 && upJpql.indexOf(JOIN_ON_KEY) > -1) {
query = em.createNativeQuery(jpql);
}else{
} else {
query = em.createQuery(jpql);
}
for (Parameter<?> p : query.getParameters()) {
......@@ -183,10 +183,10 @@ class ActionExecuteV2 extends BaseAction {
}
}
if (StringUtils.equalsIgnoreCase(statement.getType(), Statement.TYPE_SELECT)) {
if(Statement.MODE_COUNT.equals(mode)) {
if (Statement.MODE_COUNT.equals(mode)) {
data = query.getSingleResult();
}else{
if(isPageSql(jpql)) {
} else {
if (isPageSql(jpql)) {
query.setFirstResult((runtime.page - 1) * runtime.size);
query.setMaxResults(runtime.size);
}
......@@ -200,7 +200,7 @@ class ActionExecuteV2 extends BaseAction {
return data;
}
private boolean isPageSql(String sql){
private boolean isPageSql(String sql) {
sql = sql.toUpperCase().replaceAll("\\s{1,}", " ");
for (String key : pageKeys) {
if (sql.indexOf(key) > -1) {
......@@ -226,30 +226,30 @@ class ActionExecuteV2 extends BaseAction {
return cls;
}
private ScriptContext scriptContext(EffectivePerson effectivePerson, Business business, Runtime runtime)
throws Exception {
ScriptContext scriptContext = new SimpleScriptContext();
private ScriptContext scriptContext(EffectivePerson effectivePerson, Runtime runtime) throws Exception {
ScriptContext scriptContext = ScriptingFactory.scriptContextEvalInitialServiceScript();
Resources resources = new Resources();
resources.setEntityManagerContainer(business.entityManagerContainer());
resources.setContext(ThisApplication.context());
resources.setApplications(ThisApplication.context().applications());
resources.setWebservicesClient(new WebservicesClient());
resources.setOrganization(new Organization(ThisApplication.context()));
Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put(ScriptFactory.BINDING_NAME_RESOURCES, resources);
bindings.put(ScriptFactory.BINDING_NAME_EFFECTIVEPERSON, effectivePerson);
bindings.put(ScriptFactory.BINDING_NAME_PARAMETERS, gson.toJson(runtime.getParameters()));
bindings.put(ScriptingFactory.BINDING_NAME_RESOURCES, resources);
bindings.put(ScriptingFactory.BINDING_NAME_EFFECTIVEPERSON, effectivePerson);
bindings.put(ScriptingFactory.BINDING_NAME_PARAMETERS, gson.toJson(runtime.getParameters()));
return scriptContext;
}
private String joinSql(String sql, Business business) throws Exception{
private String joinSql(String sql, Business business) throws Exception {
String upSql = sql.toUpperCase();
if(upSql.indexOf(JOIN_KEY) > -1 && upSql.indexOf(JOIN_ON_KEY) > -1){
if (upSql.indexOf(JOIN_KEY) > -1 && upSql.indexOf(JOIN_ON_KEY) > -1) {
sql = sql.replaceAll("\\.", ".x");
sql = sql.replaceAll("\\.x\\*", ".*");
List<Table> tables = business.entityManagerContainer().fetchEqual(Table.class, ListTools.toList(Table.name_FIELDNAME), Table.status_FIELDNAME, Table.STATUS_build);
for (Table table : tables){
sql = sql.replaceAll(" "+table.getName()+" ", " "+DynamicEntity.TABLE_PREFIX + table.getName().toUpperCase()+" ");
List<Table> tables = business.entityManagerContainer().fetchEqual(Table.class,
ListTools.toList(Table.name_FIELDNAME), Table.status_FIELDNAME, Table.STATUS_build);
for (Table table : tables) {
sql = sql.replaceAll(" " + table.getName() + " ",
" " + DynamicEntity.TABLE_PREFIX + table.getName().toUpperCase() + " ");
}
}
return sql;
......
......@@ -8,7 +8,7 @@ import javax.persistence.Query;
import javax.script.Bindings;
import javax.script.CompiledScript;
import javax.script.ScriptContext;
import javax.script.SimpleScriptContext;
import javax.script.ScriptException;
import org.apache.commons.lang3.StringUtils;
......@@ -25,7 +25,8 @@ import com.x.base.core.project.exception.ExceptionEntityNotExist;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.script.AbstractResources;
import com.x.base.core.project.script.ScriptFactory;
import com.x.base.core.project.scripting.JsonScriptingExecutor;
import com.x.base.core.project.scripting.ScriptingFactory;
import com.x.base.core.project.webservices.WebservicesClient;
import com.x.organization.core.express.Organization;
import com.x.query.assemble.surface.Business;
......@@ -36,7 +37,7 @@ import com.x.query.core.express.statement.Runtime;
class ActionExecute extends BaseAction {
private final static String[] pageKeys = { "GROUP BY", " COUNT(" };
private static final String[] pageKeys = { "GROUP BY", " COUNT(" };
ActionResult<Object> execute(EffectivePerson effectivePerson, String flag, Integer page, Integer size,
JsonElement jsonElement) throws Exception {
......@@ -61,7 +62,7 @@ class ActionExecute extends BaseAction {
if (StringUtils.equals(statement.getFormat(), Statement.FORMAT_SCRIPT)) {
data = this.script(effectivePerson, statement, runtime);
} else {
data = this.jpql(effectivePerson, statement, runtime);
data = this.jpql(statement, runtime);
}
result.setData(data);
return result;
......@@ -84,14 +85,14 @@ class ActionExecute extends BaseAction {
return statement;
}
private CompiledScript getCompiledScriptOfScriptText(Statement statement) throws Exception {
private CompiledScript getCompiledScriptOfScriptText(Statement statement) throws ScriptException {
CompiledScript compiledScript = null;
CacheKey cacheKey = new CacheKey(this.getClass(), statement.getId(), Statement.scriptText_FIELDNAME);
Optional<?> optional = CacheManager.get(cache, cacheKey);
if (optional.isPresent()) {
compiledScript = (CompiledScript) optional.get();
} else {
compiledScript = ScriptFactory.compile(ScriptFactory.functionalization(statement.getScriptText()));
compiledScript = ScriptingFactory.functionalizationCompile(statement.getScriptText());
CacheManager.put(cache, cacheKey, compiledScript);
}
return compiledScript;
......@@ -101,11 +102,9 @@ class ActionExecute extends BaseAction {
Object data = null;
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
Business business = new Business(emc);
ScriptContext scriptContext = this.scriptContext(effectivePerson, business, runtime);
ScriptFactory.initialServiceScriptText().eval(scriptContext);
ScriptContext scriptContext = this.scriptContext(effectivePerson, runtime);
CompiledScript compiledScript = this.getCompiledScriptOfScriptText(statement);
Object o = compiledScript.eval(scriptContext);
String text = ScriptFactory.asString(o);
String text = JsonScriptingExecutor.evalString(compiledScript, scriptContext);
Class<? extends JpaObject> cls = this.clazz(business, statement);
EntityManager em;
if (StringUtils.equalsIgnoreCase(statement.getEntityCategory(), Statement.ENTITYCATEGORY_DYNAMIC)
......@@ -121,7 +120,7 @@ class ActionExecute extends BaseAction {
}
}
if (StringUtils.equalsIgnoreCase(statement.getType(), Statement.TYPE_SELECT)) {
if(isPageSql(text)) {
if (isPageSql(text)) {
query.setFirstResult((runtime.page - 1) * runtime.size);
query.setMaxResults(runtime.size);
}
......@@ -135,7 +134,7 @@ class ActionExecute extends BaseAction {
return data;
}
private Object jpql(EffectivePerson effectivePerson, Statement statement, Runtime runtime) throws Exception {
private Object jpql(Statement statement, Runtime runtime) throws Exception {
Object data = null;
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
Business business = new Business(emc);
......@@ -155,7 +154,7 @@ class ActionExecute extends BaseAction {
}
}
if (StringUtils.equalsIgnoreCase(statement.getType(), Statement.TYPE_SELECT)) {
if(isPageSql(statement.getData())) {
if (isPageSql(statement.getData())) {
query.setFirstResult((runtime.page - 1) * runtime.size);
query.setMaxResults(runtime.size);
}
......@@ -169,7 +168,7 @@ class ActionExecute extends BaseAction {
return data;
}
private boolean isPageSql(String sql){
private boolean isPageSql(String sql) {
sql = sql.toUpperCase().replaceAll("\\s{1,}", " ");
for (String key : pageKeys) {
if (sql.indexOf(key) > -1) {
......@@ -196,19 +195,17 @@ class ActionExecute extends BaseAction {
return cls;
}
private ScriptContext scriptContext(EffectivePerson effectivePerson, Business business, Runtime runtime)
throws Exception {
ScriptContext scriptContext = new SimpleScriptContext();
private ScriptContext scriptContext(EffectivePerson effectivePerson, Runtime runtime) throws Exception {
ScriptContext scriptContext = ScriptingFactory.scriptContextEvalInitialServiceScript();
Resources resources = new Resources();
resources.setEntityManagerContainer(business.entityManagerContainer());
resources.setContext(ThisApplication.context());
resources.setApplications(ThisApplication.context().applications());
resources.setWebservicesClient(new WebservicesClient());
resources.setOrganization(new Organization(ThisApplication.context()));
Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put(ScriptFactory.BINDING_NAME_RESOURCES, resources);
bindings.put(ScriptFactory.BINDING_NAME_EFFECTIVEPERSON, effectivePerson);
bindings.put(ScriptFactory.BINDING_NAME_PARAMETERS, gson.toJson(runtime.getParameters()));
bindings.put(ScriptingFactory.BINDING_NAME_RESOURCES, resources);
bindings.put(ScriptingFactory.BINDING_NAME_EFFECTIVEPERSON, effectivePerson);
bindings.put(ScriptingFactory.BINDING_NAME_PARAMETERS, gson.toJson(runtime.getParameters()));
return scriptContext;
}
......
package com.x.query.assemble.surface.jaxrs.statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.persistence.EntityManager;
import javax.persistence.Parameter;
import javax.persistence.Query;
import javax.script.Bindings;
import javax.script.CompiledScript;
import javax.script.ScriptContext;
import org.apache.commons.collections4.list.TreeList;
import org.apache.commons.lang3.StringUtils;
import com.google.gson.JsonElement;
import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
......@@ -16,7 +30,8 @@ import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.script.AbstractResources;
import com.x.base.core.project.script.ScriptFactory;
import com.x.base.core.project.scripting.JsonScriptingExecutor;
import com.x.base.core.project.scripting.ScriptingFactory;
import com.x.base.core.project.tools.ListTools;
import com.x.base.core.project.webservices.WebservicesClient;
import com.x.organization.core.express.Organization;
......@@ -27,26 +42,14 @@ import com.x.query.core.entity.schema.Table;
import com.x.query.core.express.plan.Comparison;
import com.x.query.core.express.plan.FilterEntry;
import com.x.query.core.express.statement.Runtime;
import org.apache.commons.collections4.list.TreeList;
import org.apache.commons.lang3.StringUtils;
import javax.persistence.EntityManager;
import javax.persistence.Parameter;
import javax.persistence.Query;
import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.SimpleScriptContext;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class ActionExecuteV2 extends BaseAction {
private static Logger logger = LoggerFactory.getLogger(ActionExecuteV2.class);
private final static String[] keys = { "group by", "GROUP BY", "order by", "ORDER BY", "limit", "LIMIT" };
private final static String[] pageKeys = { "GROUP BY", " COUNT(" };
private final static String JOIN_KEY = " JOIN ";
private final static String JOIN_ON_KEY = " ON ";
private static final Logger LOGGER = LoggerFactory.getLogger(ActionExecuteV2.class);
private static final String[] keys = { "group by", "GROUP BY", "order by", "ORDER BY", "limit", "LIMIT" };
private static final String[] pageKeys = { "GROUP BY", " COUNT(" };
private static final String JOIN_KEY = " JOIN ";
private static final String JOIN_ON_KEY = " ON ";
ActionResult<Object> execute(EffectivePerson effectivePerson, String flag, String mode, Integer page, Integer size,
JsonElement jsonElement) throws Exception {
......@@ -74,7 +77,7 @@ class ActionExecuteV2 extends BaseAction {
data = this.script(effectivePerson, statement, runtime, mode, wi);
break;
default:
data = this.jpql(effectivePerson, statement, runtime, mode, wi);
data = this.jpql(statement, runtime, mode, wi);
break;
}
result.setData(data);
......@@ -85,7 +88,7 @@ class ActionExecuteV2 extends BaseAction {
count = this.script(effectivePerson, statement, runtime, mode, wi);
break;
default:
count = this.jpql(effectivePerson, statement, runtime, mode, wi);
count = this.jpql(statement, runtime, mode, wi);
break;
}
result.setData(count);
......@@ -98,8 +101,8 @@ class ActionExecuteV2 extends BaseAction {
count = this.script(effectivePerson, statement, runtime, Statement.MODE_COUNT, wi);
break;
default:
data = this.jpql(effectivePerson, statement, runtime, Statement.MODE_DATA, wi);
count = this.jpql(effectivePerson, statement, runtime, Statement.MODE_COUNT, wi);
data = this.jpql(statement, runtime, Statement.MODE_DATA, wi);
count = this.jpql(statement, runtime, Statement.MODE_COUNT, wi);
break;
}
result.setData(data);
......@@ -114,13 +117,14 @@ class ActionExecuteV2 extends BaseAction {
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
Business business = new Business(emc);
ScriptContext scriptContext = this.scriptContext(effectivePerson, business, runtime);
ScriptFactory.initialServiceScriptText().eval(scriptContext);
String scriptText = statement.getScriptText();
if (Statement.MODE_COUNT.equals(mode)) {
scriptText = statement.getCountScriptText();
}
Object o = ScriptFactory.scriptEngine.eval(ScriptFactory.functionalization(scriptText), scriptContext);
String jpql = ScriptFactory.asString(o);
CompiledScript cs = ScriptingFactory.functionalizationCompile(scriptText);
String jpql = JsonScriptingExecutor.evalString(cs, scriptContext);
// Object o = ScriptFactory.scriptEngine.eval(ScriptFactory.functionalization(), scriptContext);
// String jpql = ScriptFactory.asString(o);
Class<? extends JpaObject> cls = this.clazz(business, statement);
EntityManager em;
if (StringUtils.equalsIgnoreCase(statement.getEntityCategory(), Statement.ENTITYCATEGORY_DYNAMIC)
......@@ -130,7 +134,7 @@ class ActionExecuteV2 extends BaseAction {
em = business.entityManagerContainer().get(cls);
}
jpql = joinSql(jpql, wi, business);
logger.info("执行的sql:{}", jpql);
LOGGER.info("执行的sql:{}", jpql::toString);
Query query;
String upJpql = jpql.toUpperCase();
if (upJpql.indexOf(JOIN_KEY) > -1 && upJpql.indexOf(JOIN_ON_KEY) > -1) {
......@@ -147,7 +151,7 @@ class ActionExecuteV2 extends BaseAction {
if (Statement.MODE_COUNT.equals(mode)) {
data = query.getSingleResult();
} else {
if(isPageSql(jpql)) {
if (isPageSql(jpql)) {
query.setFirstResult((runtime.page - 1) * runtime.size);
query.setMaxResults(runtime.size);
}
......@@ -162,8 +166,7 @@ class ActionExecuteV2 extends BaseAction {
return data;
}
private Object jpql(EffectivePerson effectivePerson, Statement statement, Runtime runtime, String mode, Wi wi)
throws Exception {
private Object jpql(Statement statement, Runtime runtime, String mode, Wi wi) throws Exception {
Object data = null;
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
Business business = new Business(emc);
......@@ -180,7 +183,7 @@ class ActionExecuteV2 extends BaseAction {
jpql = statement.getCountData();
}
jpql = joinSql(jpql, wi, business);
logger.info("执行的sql:{}", jpql);
LOGGER.info("执行的sql:{}", jpql::toString);
Query query;
String upJpql = jpql.toUpperCase();
if (upJpql.indexOf(JOIN_KEY) > -1 && upJpql.indexOf(JOIN_ON_KEY) > -1) {
......@@ -197,7 +200,7 @@ class ActionExecuteV2 extends BaseAction {
if (Statement.MODE_COUNT.equals(mode)) {
data = query.getSingleResult();
} else {
if(isPageSql(jpql)) {
if (isPageSql(jpql)) {
query.setFirstResult((runtime.page - 1) * runtime.size);
query.setMaxResults(runtime.size);
}
......@@ -212,7 +215,7 @@ class ActionExecuteV2 extends BaseAction {
return data;
}
private boolean isPageSql(String sql){
private boolean isPageSql(String sql) {
sql = sql.toUpperCase().replaceAll("\\s{1,}", " ");
for (String key : pageKeys) {
if (sql.indexOf(key) > -1) {
......@@ -240,17 +243,16 @@ class ActionExecuteV2 extends BaseAction {
private ScriptContext scriptContext(EffectivePerson effectivePerson, Business business, Runtime runtime)
throws Exception {
ScriptContext scriptContext = new SimpleScriptContext();
ScriptContext scriptContext = ScriptingFactory.scriptContextEvalInitialServiceScript();
Resources resources = new Resources();
resources.setEntityManagerContainer(business.entityManagerContainer());
resources.setContext(ThisApplication.context());
resources.setApplications(ThisApplication.context().applications());
resources.setWebservicesClient(new WebservicesClient());
resources.setOrganization(new Organization(ThisApplication.context()));
Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put(ScriptFactory.BINDING_NAME_RESOURCES, resources);
bindings.put(ScriptFactory.BINDING_NAME_EFFECTIVEPERSON, effectivePerson);
bindings.put(ScriptFactory.BINDING_NAME_PARAMETERS, gson.toJson(runtime.getParameters()));
bindings.put(ScriptingFactory.BINDING_NAME_RESOURCES, resources);
bindings.put(ScriptingFactory.BINDING_NAME_EFFECTIVEPERSON, effectivePerson);
bindings.put(ScriptingFactory.BINDING_NAME_PARAMETERS, gson.toJson(runtime.getParameters()));
return scriptContext;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册