diff --git a/o2server/person.json b/o2server/person.json new file mode 100644 index 0000000000000000000000000000000000000000..e471743863051ce92d70dcfe7be158fbeb3b5799 --- /dev/null +++ b/o2server/person.json @@ -0,0 +1,27 @@ +{ + "MAX_PASSWORDPERIOD": 3650, + "captchaLogin": false, + "codeLogin": true, + "bindLogin": true, + "faceLogin": true, + "password": "(var v \\u003d person.getMobile();\\u000a return v.substring(v.length - 6))", + "passwordPeriod": 0, + "passwordRegex": "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,}$", + "passwordRegexHint": "6位以上,包含数字和字母.", + "register": "disable", + "superPermission": true, + "mobileRegex": "(^(\\+)?0{0,2}852\\d{8}$)|(^(\\+)?0{0,2}853\\d{8}$)|(^(\\+)?0{0,2}886\\d{9}$)|(^1(3|4|5|6|7|8|9)\\d{9}$)", + "loginPage": { + "enable": true, + "portal": "f4fbfb29-98ae-45c5-a9ac-a4d141049d78", + "page": "" + }, + "failureInterval": 10, + "failureCount": 5, + "tokenExpiredMinutes": 21600, + "tokenCookieHttpOnly": false, + "tokenName": "x-token", + "personUnitOrderByAsc": true, + "language": "zh-CN", + "captchaFont": "" +} \ No newline at end of file diff --git a/o2server/pom.xml b/o2server/pom.xml index e20523365f1ce0ef04122cc7738e2d5c87e469d0..9aaaa050df56ecc2e781e211f67f35e154aa11c3 100644 --- a/o2server/pom.xml +++ b/o2server/pom.xml @@ -75,6 +75,12 @@ org.apache.openjpa openjpa + + + org.apache.geronimo.specs + geronimo-jms_1.1_spec + + org.apache.commons @@ -875,11 +881,6 @@ jedis 3.3.0 - - com.sun.mail - javax.mail - 1.5.6 - org.apache.commons commons-email diff --git a/o2server/x_base_core_project/src/main/java/com/x/base/core/project/build/CreateConfigSample.java b/o2server/x_base_core_project/src/main/java/com/x/base/core/project/build/CreateConfigSample.java index 9d3496f13035b1d6377dafd556b60143d72005dd..46a27cd0fb060987c45aae197dfbb0cfd06f15ea 100644 --- a/o2server/x_base_core_project/src/main/java/com/x/base/core/project/build/CreateConfigSample.java +++ b/o2server/x_base_core_project/src/main/java/com/x/base/core/project/build/CreateConfigSample.java @@ -35,6 +35,7 @@ import com.x.base.core.project.config.LogLevel; import com.x.base.core.project.config.MPweixin; import com.x.base.core.project.config.Meeting; import com.x.base.core.project.config.MessageApi; +import com.x.base.core.project.config.MessageJdbc; import com.x.base.core.project.config.MessageMail; import com.x.base.core.project.config.MessageMq; import com.x.base.core.project.config.MessageRestful; @@ -91,6 +92,7 @@ public class CreateConfigSample { classes.add(MessageMail.class); classes.add(MessageMq.class); classes.add(MessageApi.class); + classes.add(MessageJdbc.class); classes.add(Mock.class); classes.add(MPweixin.class); classes.add(Node.class); diff --git a/o2server/x_base_core_project/src/main/java/com/x/base/core/project/config/Config.java b/o2server/x_base_core_project/src/main/java/com/x/base/core/project/config/Config.java index 05ef27866f62e312bf1b27c8627514f0dbbf62af..fbefb6ceef0da820b7bf4bcaf08cd54718936a6e 100644 --- a/o2server/x_base_core_project/src/main/java/com/x/base/core/project/config/Config.java +++ b/o2server/x_base_core_project/src/main/java/com/x/base/core/project/config/Config.java @@ -79,6 +79,7 @@ public class Config { public static final String PATH_CONFIG_MESSAGEMAIL = "config/messageMail.json"; public static final String PATH_CONFIG_MESSAGEAPI = "config/messageApi.json"; public static final String PATH_CONFIG_MESSAGEJDBC = "config/messageJdbc.json"; + public static final String PATH_CONFIG_MESSAGETABLE = "config/messageTable.json"; public static final String PATH_CONFIG_SSLKEYSTORE = "config/keystore"; public static final String PATH_CONFIG_SSLKEYSTORESAMPLE = "config/sample/keystore"; public static final String PATH_CONFIG_STARTIMAGE = "config/startImage.png"; @@ -1000,6 +1001,19 @@ public class Config { return instance().messageJdbc; } + private MessageTable messageTable; + + public static synchronized MessageTable messageTable() throws Exception { + if (null == instance().messageTable) { + MessageTable obj = BaseTools.readConfigObject(PATH_CONFIG_MESSAGETABLE, MessageTable.class); + if (null == obj) { + obj = MessageTable.defaultInstance(); + } + instance().messageTable = obj; + } + return instance().messageTable; + } + private String messageSendRuleScript; public static synchronized String messageSendRuleScript() throws Exception { diff --git a/o2server/x_base_core_project/src/main/java/com/x/base/core/project/config/MessageJdbc.java b/o2server/x_base_core_project/src/main/java/com/x/base/core/project/config/MessageJdbc.java index bdda49fe2db721b3412716f76b71c71d475b0496..5ab57083de4c467d3c1735592ec57d44e73f683c 100644 --- a/o2server/x_base_core_project/src/main/java/com/x/base/core/project/config/MessageJdbc.java +++ b/o2server/x_base_core_project/src/main/java/com/x/base/core/project/config/MessageJdbc.java @@ -2,8 +2,6 @@ package com.x.base.core.project.config; import java.util.LinkedHashMap; -import org.apache.commons.lang3.StringUtils; - import com.x.base.core.project.annotation.FieldDescribe; public class MessageJdbc extends LinkedHashMap { @@ -11,63 +9,56 @@ public class MessageJdbc extends LinkedHashMap { private static final long serialVersionUID = 2536141863287117519L; public static MessageJdbc defaultInstance() { - MessageJdbc messageMq = new MessageJdbc(); - messageMq.put("o2oa", new Item()); - return messageMq; + MessageJdbc messageJdbc = new MessageJdbc(); + messageJdbc.put("o2oa", new Item()); + return messageJdbc; } public static class Item { public Item() { - + this.driverClass = DEFAULT_DRIVERCLASS; + this.url = DEFAULT_URL; + this.username = DEFAULT_USERNAME; + this.password = DEFAULT_PASSWORD; + this.catalog = DEFAULT_CATALOG; + this.schema = DEFAULT_SCHEMA; + this.table = DEFAULT_TABLE; } - public static final String TYPE_KAFKA = "kafka"; - public static final String TYPE_ACTIVEMQ = "activeMQ"; - - public static final String DEFAULT_TYPE = TYPE_KAFKA; - - public static final String DEFAULT_KAFKABOOTSTRAPSERVERS = ""; - - public static final String DEFAULT_KAFKATOPIC = ""; - - public static final String DEFAULT_KAFKAACKS = ""; - - public static final Integer DEFAULT_KAFKARETRIES = 3; + public static final String DEFAULT_DRIVERCLASS = "com.mysql.cj.jdbc.Driver"; - public static final String DEFAULT_KAFKABATCHSIZE = ""; + public static final String DEFAULT_URL = "jdbc:mysql://127.0.0.1:3306/TEST?autoReconnect=true&allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8"; - public static final Integer DEFAULT_KAFKALINGERMS = 5000; + public static final String DEFAULT_USERNAME = "root"; - public static final String DEFAULT_KAFKABUFFERMEMORY = ""; + public static final String DEFAULT_PASSWORD = "password"; - public static final String DEFAULT_ACTIVEMQUSERNAME = ""; + public static final String DEFAULT_CATALOG = ""; - public static final String DEFAULT_ACTIVEMQPASSWORD = ""; + public static final String DEFAULT_SCHEMA = ""; - public static final String DEFAULT_ACTIVEMQURL = ""; + public static final String DEFAULT_TABLE = "NEWTABLE"; - public static final String DEFAULT_ACTIVEMQQUEUENAME = ""; - - @FieldDescribe("类型,kafka或者activeMQ") + @FieldDescribe("驱动类") private String driverClass; - @FieldDescribe("服务器地址") + @FieldDescribe("地址") private String url; - @FieldDescribe("主题") + @FieldDescribe("用户名") private String username; - @FieldDescribe("用户名") + @FieldDescribe("密码") private String password; - @FieldDescribe("密码") + @FieldDescribe("catalog") private String catalog; - @FieldDescribe("服务器地址") + @FieldDescribe("schema") private String schema; - @FieldDescribe("消息队列名") + @FieldDescribe("表名") private String table; public String getDriverClass() { diff --git a/o2server/x_base_core_project/src/main/java/com/x/base/core/project/config/MessageTable.java b/o2server/x_base_core_project/src/main/java/com/x/base/core/project/config/MessageTable.java new file mode 100644 index 0000000000000000000000000000000000000000..7ccd42c25d1edb24e2b2f1f50aca677eb4d26710 --- /dev/null +++ b/o2server/x_base_core_project/src/main/java/com/x/base/core/project/config/MessageTable.java @@ -0,0 +1,36 @@ +package com.x.base.core.project.config; + +import java.util.LinkedHashMap; + +import org.apache.commons.lang3.StringUtils; + +import com.x.base.core.project.annotation.FieldDescribe; + +public class MessageTable extends LinkedHashMap { + + private static final long serialVersionUID = 2536141863287117519L; + + public static MessageTable defaultInstance() { + MessageTable messageTable = new MessageTable(); + messageTable.put("o2oa", new Item()); + return messageTable; + } + + public static class Item { + + private static final String DEFAULT_TABLE = ""; + + public Item() { + this.table = DEFAULT_TABLE; + } + + @FieldDescribe("自建表") + private String table; + + public String getTable() { + return StringUtils.isBlank(this.table) ? DEFAULT_TABLE : this.table; + } + + } + +} diff --git a/o2server/x_message_assemble_communicate/pom.xml b/o2server/x_message_assemble_communicate/pom.xml index d939d9902d0fbe4ad730e853abdee2c056d5d961..279745d8e43777f8cd74738a61b8a819433c2ff0 100644 --- a/o2server/x_message_assemble_communicate/pom.xml +++ b/o2server/x_message_assemble_communicate/pom.xml @@ -1,8 +1,5 @@ - + 4.0.0 o2oa @@ -30,36 +27,24 @@ - org.apache.kafka - kafka-clients - 2.6.0 - - - - org.apache.activemq - activemq-client - 5.14.5 + org.apache.kafka + kafka-clients + 2.6.0 + + + org.apache.activemq + activemq-client + 5.14.5 + + + org.apache.geronimo.specs + geronimo-jms_1.1_spec + + - - + org.codehaus.mojo exec-maven-plugin @@ -161,4 +146,4 @@ - + \ No newline at end of file diff --git a/o2server/x_message_assemble_communicate/src/main/java/com/x/message/assemble/communicate/ExceptionTableReturn.java b/o2server/x_message_assemble_communicate/src/main/java/com/x/message/assemble/communicate/ExceptionTableReturn.java new file mode 100644 index 0000000000000000000000000000000000000000..e62786ee4c1a26dfde91fde34ea852a015ab1263 --- /dev/null +++ b/o2server/x_message_assemble_communicate/src/main/java/com/x/message/assemble/communicate/ExceptionTableReturn.java @@ -0,0 +1,12 @@ +package com.x.message.assemble.communicate; + +import com.x.base.core.project.exception.PromptException; + +class ExceptionTableReturn extends PromptException { + + private static final long serialVersionUID = 6235890108470383271L; + + ExceptionTableReturn(String id, String title, String table) { + super("execute table message return is not true, message:{}, title:{}, table:{}.", id, title, table); + } +} diff --git a/o2server/x_message_assemble_communicate/src/main/java/com/x/message/assemble/communicate/JdbcConsumeQueue.java b/o2server/x_message_assemble_communicate/src/main/java/com/x/message/assemble/communicate/JdbcConsumeQueue.java index c1bbbdb9d7fe9a1457a7844c978947ff8c31eb76..77877c642240248b3cb150a33c2999f32c96b508 100644 --- a/o2server/x_message_assemble_communicate/src/main/java/com/x/message/assemble/communicate/JdbcConsumeQueue.java +++ b/o2server/x_message_assemble_communicate/src/main/java/com/x/message/assemble/communicate/JdbcConsumeQueue.java @@ -1,5 +1,6 @@ package com.x.message.assemble.communicate; +import java.math.BigDecimal; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; @@ -8,14 +9,24 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Optional; -import org.apache.commons.collections4.ListUtils; +import javax.persistence.EntityManager; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; + +import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.math.NumberUtils; +import org.apache.commons.lang3.time.DateUtils; import com.google.gson.Gson; import com.google.gson.JsonElement; @@ -23,13 +34,17 @@ import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; import com.x.base.core.container.EntityManagerContainer; import com.x.base.core.container.factory.EntityManagerContainerFactory; +import com.x.base.core.entity.JpaObject_; import com.x.base.core.project.config.Config; import com.x.base.core.project.config.MessageJdbc; import com.x.base.core.project.gson.XGsonBuilder; import com.x.base.core.project.logger.Logger; import com.x.base.core.project.logger.LoggerFactory; +import com.x.base.core.project.message.MessageConnector; import com.x.base.core.project.queue.AbstractQueue; +import com.x.base.core.project.tools.DateTools; import com.x.message.core.entity.Message; +import com.x.message.core.entity.Message_; public class JdbcConsumeQueue extends AbstractQueue { @@ -68,78 +83,551 @@ public class JdbcConsumeQueue extends AbstractQueue { private void update(Message message) { try { MessageJdbc.Item item = Config.messageJdbc().get(message.getItem()); - String catalog = ""; - String schema = ""; - String table = ""; - Class.forName(""); + Class.forName(item.getDriverClass()); try (Connection connection = DriverManager.getConnection(item.getUrl(), item.getUsername(), item.getPassword()); Statement statement = connection.createStatement()) { - List columns = this.columnNames(connection, item); - Map map = values(columns, null); - - List params = new ArrayList<>(); - for (int i = 0; i < map.keySet().size(); i++) { - params.add("?"); - } - String sql = "INSERT INTO XXXXX (" + StringUtils.join(map.keySet(), ",") + ") VALUES (" - + StringUtils.join(params, ",") + ")"; - + List columns = this.columns(connection, item); + Map map = values(columns, gson.fromJson(message.getBody(), JsonObject.class)); + String sql = createSql(item.getSchema(), item.getTable(), map); PreparedStatement preparedStatement = connection.prepareStatement(sql); - int idx = 1; - for (Entry en : map.entrySet()) { - preparedStatement.setObject(idx++, getObject(en.getValue())); + for (Entry en : map.entrySet()) { + preparedStatement.setObject(idx++, en.getValue()); } preparedStatement.executeUpdate(); } - + success(message.getId()); } catch (Exception e) { failure(message.getId(), e); LOGGER.error(e); } + + } + + private String createSql(String schema, String table, Map map) { + List aux = new ArrayList<>(); + for (int i = 0; i < map.keySet().size(); i++) { + aux.add("?"); + } + return "INSERT INTO " + (StringUtils.isEmpty(schema) ? table : (schema + "." + table)) + " (" + + StringUtils.join(map.keySet(), ",") + ") VALUES (" + StringUtils.join(aux, ",") + ")"; + } + + private void success(String id) { + try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) { + Message message = emc.find(id, Message.class); + if (null != message) { + emc.beginTransaction(Message.class); + message.setConsumed(true); + emc.commit(); + } + } catch (Exception e) { + LOGGER.error(e); + } + } + + private void failure(String id, Exception exception) { + try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) { + Message message = emc.find(id, Message.class); + if (null != message) { + emc.beginTransaction(Message.class); + Integer failure = message.getProperties().getFailure(); + failure = (null == failure) ? 1 : failure + 1; + message.getProperties().setFailure(failure); + message.getProperties().setError(exception.getMessage()); + emc.commit(); + } + } catch (Exception e) { + LOGGER.error(e); + } + } + + private Map values(List columns, JsonObject jsonObject) { + Map auxLowerCase = new HashMap<>(); + jsonObject.entrySet().forEach(en -> { + auxLowerCase.put(StringUtils.lowerCase(en.getKey()), en.getKey()); + }); + Map map = new LinkedHashMap<>(); + String key; + for (Column column : columns) { + key = auxLowerCase.get(StringUtils.lowerCase(column.getName())); + if (StringUtils.isNotEmpty(key)) { + map.put(column.getName(), getValue(column, jsonObject.get(key))); + } + } + return map; } - private Object getObject(JsonElement jsonElement) { + private Object getValue(Column column, JsonElement jsonElement) { if (null == jsonElement || jsonElement.isJsonNull()) { return null; } + switch (column.getDataType()) { + case java.sql.Types.ARRAY: + return toARRAY(); + case java.sql.Types.BIGINT: + return toBIGINT(jsonElement); + case java.sql.Types.BINARY: + return toBINARY(); + case java.sql.Types.BIT: + return toBIT(jsonElement); + case java.sql.Types.BLOB: + return toBLOB(); + case java.sql.Types.BOOLEAN: + return toBOOLEAN(jsonElement); + case java.sql.Types.CHAR: + return toCHAR(jsonElement); + case java.sql.Types.CLOB: + return toCLOB(jsonElement); + case java.sql.Types.DATALINK: + return toDATALINK(); + case java.sql.Types.DATE: + return toDATE(jsonElement); + case java.sql.Types.DECIMAL: + return toDECIMAL(jsonElement); + case java.sql.Types.DISTINCT: + return toDISTINCT(); + case java.sql.Types.DOUBLE: + return toDOUBLE(jsonElement); + case java.sql.Types.FLOAT: + return toFLOAT(jsonElement); + case java.sql.Types.INTEGER: + return toINTEGER(jsonElement); + case java.sql.Types.JAVA_OBJECT: + return toJAVA_OBJECT(); + case java.sql.Types.LONGNVARCHAR: + return toLONGNVARCHAR(jsonElement); + case java.sql.Types.LONGVARBINARY: + return toLONGVARBINARY(); + case java.sql.Types.LONGVARCHAR: + return toLONGVARCHAR(jsonElement); + case java.sql.Types.NCHAR: + return toNCHAR(jsonElement); + case java.sql.Types.NCLOB: + return toNCLOB(jsonElement); + case java.sql.Types.NULL: + return toNULL(); + case java.sql.Types.NUMERIC: + return toNUMERIC(jsonElement); + case java.sql.Types.NVARCHAR: + return toNVARCHAR(jsonElement); + case java.sql.Types.OTHER: + return toOTHER(); + case java.sql.Types.REAL: + return toREAL(); + case java.sql.Types.REF: + return toREF(); + case java.sql.Types.REF_CURSOR: + return toREF_CURSOR(); + case java.sql.Types.ROWID: + return toROWID(); + case java.sql.Types.SMALLINT: + return toSMALLINT(jsonElement); + case java.sql.Types.SQLXML: + return toSQLXML(jsonElement); + case java.sql.Types.STRUCT: + return toSTRUCT(); + case java.sql.Types.TIME: + return toTIME(jsonElement); + case java.sql.Types.TIME_WITH_TIMEZONE: + return toTIME_WITH_TIMEZONE(); + case java.sql.Types.TIMESTAMP: + return toTIMESTAMP(jsonElement); + case java.sql.Types.TIMESTAMP_WITH_TIMEZONE: + return toTIMESTAMP_WITH_TIMEZONE(); + case java.sql.Types.TINYINT: + return toTINYINT(jsonElement); + case java.sql.Types.VARBINARY: + return toVARBINARY(); + case java.sql.Types.VARCHAR: + return toVARCHAR(jsonElement); + default: + return null; + } + } + + private Object toARRAY() { + return null; + } + + private Object toBIGINT(JsonElement jsonElement) { if (jsonElement.isJsonPrimitive()) { - JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); - if (jsonPrimitive.isBoolean()) { - return jsonPrimitive.getAsBoolean(); - } else if (jsonPrimitive.isString()) { - return jsonPrimitive.getAsString(); - } else if (jsonPrimitive.isNumber()) { - return jsonPrimitive.getAsNumber(); + try { + return Integer.valueOf(jsonElement.getAsJsonPrimitive().getAsNumber().intValue()); + } catch (Exception e) { + LOGGER.error(e); } } return null; } - private Map values(List columns, JsonObject jsonObject) { - Map map = new LinkedHashMap<>(); - jsonObject.entrySet().forEach(en -> { - int idx = ListUtils.indexOf(columns, o -> { - return StringUtils.equalsIgnoreCase(en.getKey(), o); - }); - if (idx > 0) { - map.put(columns.get(idx), en.getValue()); + private Object toBINARY() { + return null; + } + + private Object toBIT(JsonElement jsonElement) { + if (jsonElement.isJsonPrimitive()) { + try { + JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); + if (jsonPrimitive.isBoolean()) { + return Integer.valueOf(jsonPrimitive.getAsBoolean() ? 1 : 0); + } else if (jsonPrimitive.isNumber()) { + return Integer.valueOf((jsonPrimitive.getAsInt() > 0) ? 1 : 0); + } else if (jsonPrimitive.isString()) { + String str = jsonPrimitive.getAsString(); + if (NumberUtils.isCreatable(str)) { + return Integer.valueOf((NumberUtils.toInt(str) > 0) ? 1 : 0); + } else { + return Integer.valueOf(BooleanUtils.toBoolean(str) ? 1 : 0); + } + } + } catch (Exception e) { + LOGGER.error(e); } - }); - return map; + } + return null; + } + + private Object toBLOB() { + return null; + } + + private Object toBOOLEAN(JsonElement jsonElement) { + if (jsonElement.isJsonPrimitive()) { + try { + JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); + if (jsonPrimitive.isBoolean()) { + return jsonElement.getAsBoolean(); + } else if (jsonPrimitive.isNumber()) { + return Boolean.valueOf((jsonPrimitive.getAsNumber().intValue() > 0) ? true : false); + } else if (jsonPrimitive.isString()) { + String str = jsonPrimitive.getAsString(); + if (NumberUtils.isCreatable(str)) { + return Boolean.valueOf((NumberUtils.toInt(str) > 0) ? true : false); + } else { + return BooleanUtils.toBooleanObject(str); + } + } + } catch (Exception e) { + LOGGER.error(e); + } + } + return null; + } + + private Object toCHAR(JsonElement jsonElement) { + if (jsonElement.isJsonPrimitive()) { + try { + return jsonElement.getAsJsonPrimitive().getAsCharacter(); + } catch (Exception e) { + LOGGER.error(e); + } + } + return null; + } + + private Object toCLOB(JsonElement jsonElement) { + if (jsonElement.isJsonPrimitive()) { + try { + return jsonElement.getAsJsonPrimitive().getAsString(); + } catch (Exception e) { + LOGGER.error(e); + } + } + return null; + } + + private Object toDATALINK() { + return null; + } + + private Object toDATE(JsonElement jsonElement) { + if (jsonElement.isJsonPrimitive()) { + try { + return DateTools.parse(jsonElement.getAsJsonPrimitive().getAsString()); + } catch (Exception e) { + LOGGER.error(e); + } + } + return null; + } + + private Object toDECIMAL(JsonElement jsonElement) { + if (jsonElement.isJsonPrimitive()) { + try { + return BigDecimal.valueOf(jsonElement.getAsJsonPrimitive().getAsNumber().doubleValue()); + } catch (Exception e) { + LOGGER.error(e); + } + } + return null; + } + + private Object toDISTINCT() { + return null; + } + + private Object toDOUBLE(JsonElement jsonElement) { + if (jsonElement.isJsonPrimitive()) { + try { + return Double.valueOf(jsonElement.getAsJsonPrimitive().getAsNumber().doubleValue()); + } catch (Exception e) { + LOGGER.error(e); + } + } + return null; + } + + private Object toFLOAT(JsonElement jsonElement) { + if (jsonElement.isJsonPrimitive()) { + try { + return Float.valueOf(jsonElement.getAsJsonPrimitive().getAsNumber().floatValue()); + } catch (Exception e) { + LOGGER.error(e); + } + } + return null; + } + + private Object toINTEGER(JsonElement jsonElement) { + if (jsonElement.isJsonPrimitive()) { + try { + return Integer.valueOf(jsonElement.getAsJsonPrimitive().getAsNumber().intValue()); + } catch (Exception e) { + LOGGER.error(e); + } + } + return null; + } + + private Object toJAVA_OBJECT() { + return null; + } + + private Object toLONGNVARCHAR(JsonElement jsonElement) { + if (jsonElement.isJsonPrimitive()) { + try { + return jsonElement.getAsJsonPrimitive().getAsString(); + } catch (Exception e) { + LOGGER.error(e); + } + } + return null; + } + + private Object toLONGVARBINARY() { + return null; + } + + private Object toLONGVARCHAR(JsonElement jsonElement) { + if (jsonElement.isJsonPrimitive()) { + try { + return jsonElement.getAsJsonPrimitive().getAsString(); + } catch (Exception e) { + LOGGER.error(e); + } + } + return null; + } + + private Object toNCHAR(JsonElement jsonElement) { + if (jsonElement.isJsonPrimitive()) { + try { + return jsonElement.getAsJsonPrimitive().getAsString(); + } catch (Exception e) { + LOGGER.error(e); + } + } + return null; + } + + private Object toNCLOB(JsonElement jsonElement) { + if (jsonElement.isJsonPrimitive()) { + try { + return jsonElement.getAsJsonPrimitive().getAsString(); + } catch (Exception e) { + LOGGER.error(e); + } + } + return null; + } + + private Object toNULL() { + return null; + } + + private Object toNUMERIC(JsonElement jsonElement) { + if (jsonElement.isJsonPrimitive()) { + try { + return jsonElement.getAsJsonPrimitive().getAsBigDecimal(); + } catch (Exception e) { + LOGGER.error(e); + } + } + return null; + } + + private Object toNVARCHAR(JsonElement jsonElement) { + if (jsonElement.isJsonPrimitive()) { + try { + return jsonElement.getAsJsonPrimitive().getAsString(); + } catch (Exception e) { + LOGGER.error(e); + } + } + return null; + } + + private Object toOTHER() { + return null; + } + + private Object toREAL() { + return null; + } + + private Object toREF() { + return null; + } + + private Object toREF_CURSOR() { + return null; + } + + private Object toROWID() { + return null; + } + + private Object toSMALLINT(JsonElement jsonElement) { + if (jsonElement.isJsonPrimitive()) { + try { + return Short.valueOf(jsonElement.getAsJsonPrimitive().getAsNumber().shortValue()); + } catch (Exception e) { + LOGGER.error(e); + } + } + return null; } - private List columnNames(Connection connection, MessageJdbc.Item item) throws SQLException { + private Object toSQLXML(JsonElement jsonElement) { + if (jsonElement.isJsonPrimitive()) { + try { + return jsonElement.getAsJsonPrimitive().getAsString(); + } catch (Exception e) { + LOGGER.error(e); + } + } + return null; + } + + private Object toSTRUCT() { + return null; + } + + private Object toTIME(JsonElement jsonElement) { + if (jsonElement.isJsonPrimitive()) { + try { + return DateTools.parse(jsonElement.getAsJsonPrimitive().getAsString()); + } catch (Exception e) { + LOGGER.error(e); + } + } + return null; + } + + private Object toTIME_WITH_TIMEZONE() { + return null; + } + + private Object toTIMESTAMP(JsonElement jsonElement) { + if (jsonElement.isJsonPrimitive()) { + try { + return DateTools.parse(jsonElement.getAsJsonPrimitive().getAsString()); + } catch (Exception e) { + LOGGER.error(e); + } + } + return null; + } + + private Object toTIMESTAMP_WITH_TIMEZONE() { + return null; + } + + private Object toTINYINT(JsonElement jsonElement) { + if (jsonElement.isJsonPrimitive()) { + try { + return Short.valueOf(jsonElement.getAsJsonPrimitive().getAsNumber().shortValue()); + } catch (Exception e) { + LOGGER.error(e); + } + } + return null; + } + + private Object toVARBINARY() { + return null; + } + + private Object toVARCHAR(JsonElement jsonElement) { + if (jsonElement.isJsonPrimitive()) { + try { + return jsonElement.getAsJsonPrimitive().getAsString(); + } catch (Exception e) { + LOGGER.error(e); + } + } + return null; + } + + private List columns(Connection connection, MessageJdbc.Item item) throws SQLException { DatabaseMetaData databaseMetaData = connection.getMetaData(); - String catalog = ""; - String schema = ""; - String table = ""; - List list = new ArrayList<>(); + String catalog = StringUtils.isEmpty(item.getCatalog()) ? null : item.getCatalog(); + String schema = StringUtils.isEmpty(item.getSchema()) ? "%" : item.getSchema(); + String table = item.getTable(); + List list = new ArrayList<>(); try (ResultSet resultSet = databaseMetaData.getColumns(catalog, schema, table, "%")) { while (resultSet.next()) { - list.add(resultSet.getString("COLUMN_NAME")); + list.add(new Column(resultSet.getString("COLUMN_NAME"), resultSet.getInt("DATA_TYPE"))); } } return list; } + + private List listOverStay() { + try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) { + EntityManager em = emc.get(Message.class); + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery cq = cb.createQuery(String.class); + Root root = cq.from(Message.class); + Predicate p = cb.equal(root.get(Message_.consumer), MessageConnector.CONSUME_JDBC); + p = cb.and(p, cb.notEqual(root.get(Message_.consumed), true)); + p = cb.and(p, cb.lessThan(root.get(JpaObject_.updateTime), DateUtils.addMinutes(new Date(), -20))); + cq.select(root.get(Message_.id)).where(p); + return em.createQuery(cq).setMaxResults(20).getResultList(); + } catch (Exception e) { + LOGGER.error(e); + } + return new ArrayList<>(); + } + + public static class Column { + + public Column(String name, int dataType) { + this.name = name; + this.dataType = dataType; + } + + private String name; + private int dataType; + + public String getName() { + return name; + } + + public int getDataType() { + return dataType; + } + + } } diff --git a/o2server/x_message_assemble_communicate/src/main/java/com/x/message/assemble/communicate/TableConsumeQueue.java b/o2server/x_message_assemble_communicate/src/main/java/com/x/message/assemble/communicate/TableConsumeQueue.java new file mode 100644 index 0000000000000000000000000000000000000000..5d50838ec0edfb91bb47064322a4b7257d587d98 --- /dev/null +++ b/o2server/x_message_assemble_communicate/src/main/java/com/x/message/assemble/communicate/TableConsumeQueue.java @@ -0,0 +1,133 @@ +package com.x.message.assemble.communicate; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Optional; + +import javax.persistence.EntityManager; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.time.DateUtils; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.x.base.core.container.EntityManagerContainer; +import com.x.base.core.container.factory.EntityManagerContainerFactory; +import com.x.base.core.entity.JpaObject_; +import com.x.base.core.project.Applications; +import com.x.base.core.project.x_query_service_processing; +import com.x.base.core.project.config.Config; +import com.x.base.core.project.config.MessageTable; +import com.x.base.core.project.gson.XGsonBuilder; +import com.x.base.core.project.jaxrs.WrapBoolean; +import com.x.base.core.project.logger.Logger; +import com.x.base.core.project.logger.LoggerFactory; +import com.x.base.core.project.message.MessageConnector; +import com.x.base.core.project.queue.AbstractQueue; +import com.x.message.core.entity.Message; +import com.x.message.core.entity.Message_; + +public class TableConsumeQueue extends AbstractQueue { + + private static final Logger LOGGER = LoggerFactory.getLogger(TableConsumeQueue.class); + + private static final Gson gson = XGsonBuilder.instance(); + + protected void execute(Message message) throws Exception { + if (null != message && StringUtils.isNotEmpty(message.getItem())) { + update(message); + } + List ids = listOverStay(); + if (!ids.isEmpty()) { + LOGGER.info("滞留 table 消息数量:{}.", ids.size()); + for (String id : ids) { + Optional optional = find(id); + if (optional.isPresent()) { + message = optional.get(); + if (StringUtils.isNotEmpty(message.getItem())) { + update(message); + } + } + } + } + } + + private Optional find(String id) { + try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) { + return Optional.of(emc.find(id, Message.class)); + } catch (Exception e) { + LOGGER.error(e); + } + return Optional.empty(); + } + + private void update(Message message) { + try { + MessageTable.Item item = Config.messageTable().get(message.getItem()); + JsonElement jsonElement = gson.fromJson(message.getBody(), JsonElement.class); + WrapBoolean resp = ThisApplication.context().applications() + .postQuery(x_query_service_processing.class, + Applications.joinQueryUri("table", item.getTable(), "insert"), jsonElement) + .getData(WrapBoolean.class); + if (!resp.getValue()) { + throw new ExceptionTableReturn(message.getId(), message.getTitle(), item.getTable()); + } + success(message.getId()); + } catch (Exception e) { + failure(message.getId(), e); + LOGGER.error(e); + } + + } + + private void success(String id) { + try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) { + Message message = emc.find(id, Message.class); + if (null != message) { + emc.beginTransaction(Message.class); + message.setConsumed(true); + emc.commit(); + } + } catch (Exception e) { + LOGGER.error(e); + } + } + + private void failure(String id, Exception exception) { + try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) { + Message message = emc.find(id, Message.class); + if (null != message) { + emc.beginTransaction(Message.class); + Integer failure = message.getProperties().getFailure(); + failure = (null == failure) ? 1 : failure + 1; + message.getProperties().setFailure(failure); + message.getProperties().setError(exception.getMessage()); + emc.commit(); + } + } catch (Exception e) { + LOGGER.error(e); + } + } + + private List listOverStay() { + try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) { + EntityManager em = emc.get(Message.class); + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery cq = cb.createQuery(String.class); + Root root = cq.from(Message.class); + Predicate p = cb.equal(root.get(Message_.consumer), MessageConnector.CONSUME_RESTFUL); + p = cb.and(p, cb.notEqual(root.get(Message_.consumed), true)); + p = cb.and(p, cb.lessThan(root.get(JpaObject_.updateTime), DateUtils.addMinutes(new Date(), -20))); + cq.select(root.get(Message_.id)).where(p); + return em.createQuery(cq).setMaxResults(20).getResultList(); + } catch (Exception e) { + LOGGER.error(e); + } + return new ArrayList<>(); + } +} \ No newline at end of file diff --git a/o2server/x_message_assemble_communicate/src/main/java/com/x/message/assemble/communicate/ThisApplication.java b/o2server/x_message_assemble_communicate/src/main/java/com/x/message/assemble/communicate/ThisApplication.java index 4207164c6c2ed8a7c13d36e915ffb018908aebd2..061db9024819f869de4a1f84daa3860c7a3bc893 100644 --- a/o2server/x_message_assemble_communicate/src/main/java/com/x/message/assemble/communicate/ThisApplication.java +++ b/o2server/x_message_assemble_communicate/src/main/java/com/x/message/assemble/communicate/ThisApplication.java @@ -43,6 +43,10 @@ public class ThisApplication { public static final ApiConsumeQueue apiConsumeQueue = new ApiConsumeQueue(); + public static final JdbcConsumeQueue jdbcConsumeQueue = new JdbcConsumeQueue(); + + public static final TableConsumeQueue tableConsumeQueue = new TableConsumeQueue(); + public static Context context() { return context; } @@ -70,6 +74,8 @@ public class ThisApplication { context().startQueue(apiConsumeQueue); context().startQueue(mailConsumeQueue); context().startQueue(mqConsumeQueue); + context().startQueue(jdbcConsumeQueue); + context().startQueue(tableConsumeQueue); if (BooleanUtils.isTrue(Config.qiyeweixin().getEnable()) && BooleanUtils.isTrue(Config.qiyeweixin().getMessageEnable())) { context().startQueue(qiyeweixinConsumeQueue); diff --git a/o2server/x_message_assemble_communicate/src/main/java/com/x/message/assemble/communicate/jaxrs/connector/ActionCreate.java b/o2server/x_message_assemble_communicate/src/main/java/com/x/message/assemble/communicate/jaxrs/connector/ActionCreate.java index a5a90457a44b0486c26bd4427188452504ac3644..fb6c41e348a669978c1f220e80e7e7426017f44e 100644 --- a/o2server/x_message_assemble_communicate/src/main/java/com/x/message/assemble/communicate/jaxrs/connector/ActionCreate.java +++ b/o2server/x_message_assemble_communicate/src/main/java/com/x/message/assemble/communicate/jaxrs/connector/ActionCreate.java @@ -566,7 +566,7 @@ class ActionCreate extends BaseAction { case MessageConnector.CONSUME_CALENDAR: message = this.v3CalendarMessage(wi, consumer); break; - // restful, mq, api, mail, custom_消息没有其他判断条件 + // restful, mq, api, mail, jdbc, custom_消息没有其他判断条件 default: message = this.v3Message(wi, consumer); break; @@ -817,6 +817,9 @@ class ActionCreate extends BaseAction { case MessageConnector.CONSUME_API: ThisApplication.apiConsumeQueue.send(message); break; + case MessageConnector.CONSUME_JDBC: + ThisApplication.jdbcConsumeQueue.send(message); + break; default: break; } diff --git a/o2server/x_message_assemble_communicate/src/main/java/com/x/message/assemble/communicate/schedule/TriggerMessageConsumeQueue.java b/o2server/x_message_assemble_communicate/src/main/java/com/x/message/assemble/communicate/schedule/TriggerMessageConsumeQueue.java index 989f84f4eb774f5cbfe0b39a5eadd5c2a841c03d..55085567bfca4ab75b5b3146a2f222249d7be901 100644 --- a/o2server/x_message_assemble_communicate/src/main/java/com/x/message/assemble/communicate/schedule/TriggerMessageConsumeQueue.java +++ b/o2server/x_message_assemble_communicate/src/main/java/com/x/message/assemble/communicate/schedule/TriggerMessageConsumeQueue.java @@ -13,11 +13,14 @@ public class TriggerMessageConsumeQueue extends AbstractJob { @Override public void schedule(JobExecutionContext jobExecutionContext) throws Exception { - LOGGER.debug("schedule trigger restfulConsumeQueue, mqConsumeQueue, mailConsumeQueue, apiConsumeQueue."); + LOGGER.debug( + "schedule trigger restfulConsumeQueue, mqConsumeQueue, mailConsumeQueue, apiConsumeQueue, jdbcConsumeQueue, tableConsumeQueue."); ThisApplication.restfulConsumeQueue.send(null); ThisApplication.mqConsumeQueue.send(null); ThisApplication.mailConsumeQueue.send(null); ThisApplication.apiConsumeQueue.send(null); + ThisApplication.jdbcConsumeQueue.send(null); + ThisApplication.tableConsumeQueue.send(null); } } \ No newline at end of file diff --git a/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/table/ActionInsert.java b/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/table/ActionInsert.java new file mode 100644 index 0000000000000000000000000000000000000000..172b6e2550089d362c3a3f7809bbd82a374bf267 --- /dev/null +++ b/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/table/ActionInsert.java @@ -0,0 +1,60 @@ +package com.x.query.service.processing.jaxrs.table; + +import org.apache.commons.beanutils.PropertyUtilsBean; + +import com.google.gson.JsonElement; +import com.x.base.core.container.EntityManagerContainer; +import com.x.base.core.container.factory.EntityManagerContainerFactory; +import com.x.base.core.entity.JpaObject; +import com.x.base.core.entity.annotation.CheckPersistType; +import com.x.base.core.entity.dynamic.DynamicEntity; +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.jaxrs.WrapBoolean; +import com.x.base.core.project.logger.Logger; +import com.x.base.core.project.logger.LoggerFactory; +import com.x.query.core.entity.schema.Table; +import com.x.query.service.processing.Business; + +class ActionInsert extends BaseAction { + + private static final Logger LOGGER = LoggerFactory.getLogger(ActionInsert.class); + + ActionResult execute(EffectivePerson effectivePerson, String flag, JsonElement jsonElement) throws Exception { + + LOGGER.debug("execute:{}, flag:{}.", effectivePerson::getDistinguishedName, () -> flag); + + ClassLoader classLoader = Business.getDynamicEntityClassLoader(); + Thread.currentThread().setContextClassLoader(classLoader); + ActionResult result = new ActionResult<>(); + + try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) { + LOGGER.debug("execute:{}, flag:{}.", effectivePerson::getDistinguishedName, () -> flag); + Table table = emc.flag(flag, Table.class); + if (null == table) { + throw new ExceptionEntityNotExist(flag, Table.class); + } + DynamicEntity dynamicEntity = new DynamicEntity(table.getName()); + @SuppressWarnings("unchecked") + Class cls = (Class) classLoader + .loadClass(dynamicEntity.className()); + JpaObject o = update(jsonElement, cls); + emc.beginTransaction(cls); + emc.persist(o); + emc.check(o, CheckPersistType.all); + emc.commit(); + Wo wo = new Wo(); + wo.setValue(true); + result.setData(wo); + } + return result; + } + + public static class Wo extends WrapBoolean { + + private static final long serialVersionUID = 7570720614789228246L; + + } + +} \ No newline at end of file diff --git a/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/table/ActionUpdate.java b/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/table/ActionUpdate.java deleted file mode 100644 index 75d82c165afe36165753ed1c730593a23c3c88d3..0000000000000000000000000000000000000000 --- a/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/table/ActionUpdate.java +++ /dev/null @@ -1,466 +0,0 @@ -package com.x.query.service.processing.jaxrs.table; - -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -import javax.persistence.Column; - -import org.apache.commons.beanutils.PropertyUtilsBean; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.reflect.FieldUtils; -import org.apache.openjpa.persistence.jdbc.ElementColumn; - -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; -import com.x.base.core.container.EntityManagerContainer; -import com.x.base.core.container.factory.EntityManagerContainerFactory; -import com.x.base.core.entity.JpaObject; -import com.x.base.core.entity.annotation.CheckPersistType; -import com.x.base.core.entity.dynamic.DynamicEntity; -import com.x.base.core.entity.tools.JpaObjectTools; -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.jaxrs.WrapBoolean; -import com.x.base.core.project.logger.Logger; -import com.x.base.core.project.logger.LoggerFactory; -import com.x.base.core.project.tools.DateTools; -import com.x.base.core.project.tools.StringTools; -import com.x.query.core.entity.schema.Table; -import com.x.query.service.processing.Business; - -class ActionUpdate extends BaseAction { - - private static final Logger LOGGER = LoggerFactory.getLogger(ActionUpdate.class); - - private PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean(); - - ActionResult execute(EffectivePerson effectivePerson, String flag, String bundle, JsonElement jsonElement) - throws Exception { - - LOGGER.debug("execute:{}, flag:{}, bundle:{}.", effectivePerson::getDistinguishedName, () -> flag, - () -> bundle); - - ClassLoader classLoader = Business.getDynamicEntityClassLoader(); - Thread.currentThread().setContextClassLoader(classLoader); - ActionResult result = new ActionResult<>(); - - if (StringUtils.isEmpty(bundle)) { - throw new ExceptionBundleEmpty(); - } - - try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) { - LOGGER.debug("execute:{}, flag:{}.", effectivePerson::getDistinguishedName, () -> flag); - Table table = emc.flag(flag, Table.class); - if (null == table) { - throw new ExceptionEntityNotExist(flag, Table.class); - } - DynamicEntity dynamicEntity = new DynamicEntity(table.getName()); - @SuppressWarnings("unchecked") - Class cls = (Class) classLoader - .loadClass(dynamicEntity.className()); - JpaObject o = update(jsonElement, cls); - JpaObject obj = emc.find(bundle, cls); - emc.beginTransaction(cls); - if (null != obj) { - o.copyTo(obj, JpaObject.FieldsUnmodify); - emc.check(obj, CheckPersistType.all); - } else { - emc.persist(o); - emc.check(o, CheckPersistType.all); - } - emc.commit(); - Wo wo = new Wo(); - wo.setValue(true); - result.setData(wo); - } - return result; - } - - private T update(JsonElement jsonElement, Class cls) - throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException, - IllegalArgumentException, SecurityException { - List fields = FieldUtils.getFieldsListWithAnnotation(cls, Column.class); - T t = cls.getConstructor().newInstance(); - JsonObject jsonObject = null; - if ((jsonElement != null) && jsonElement.isJsonObject()) { - jsonObject = jsonElement.getAsJsonObject(); - } - for (Field field : fields) { - switch (JpaObjectTools.type(field)) { - case JpaObject.TYPE_STRING: - propertyUtilsBean.setProperty(t, field.getName(), extractStringOrDistinguishedName( - getJsonElement(jsonObject, field), getColumnDefinedStringLength(field))); - break; - case JpaObject.TYPE_INTEGER: - propertyUtilsBean.setProperty(t, field.getName(), extractInteger(getJsonElement(jsonObject, field))); - break; - case JpaObject.TYPE_LONG: - propertyUtilsBean.setProperty(t, field.getName(), extractLong(getJsonElement(jsonObject, field))); - break; - case JpaObject.TYPE_FLOAT: - propertyUtilsBean.setProperty(t, field.getName(), extractFloat(getJsonElement(jsonObject, field))); - break; - case JpaObject.TYPE_DOUBLE: - propertyUtilsBean.setProperty(t, field.getName(), extractDouble(getJsonElement(jsonObject, field))); - break; - case JpaObject.TYPE_DATETIME: - propertyUtilsBean.setProperty(t, field.getName(), extractDateTime(getJsonElement(jsonObject, field))); - break; - case JpaObject.TYPE_DATE: - propertyUtilsBean.setProperty(t, field.getName(), extractDate(getJsonElement(jsonObject, field))); - break; - case JpaObject.TYPE_BOOLEAN: - propertyUtilsBean.setProperty(t, field.getName(), extractBoolean(getJsonElement(jsonObject, field))); - break; - case JpaObject.TYPE_STRINGLIST: - propertyUtilsBean.setProperty(t, field.getName(), extractStringOrDistinguishedNameList( - getJsonElement(jsonObject, field), getElementColumnDefinedStringLength(field))); - break; - case JpaObject.TYPE_INTEGERLIST: - propertyUtilsBean.setProperty(t, field.getName(), - extractIntegerList(getJsonElement(jsonObject, field))); - break; - case JpaObject.TYPE_LONGLIST: - propertyUtilsBean.setProperty(t, field.getName(), - extractLongList(getJsonElement(jsonObject, field))); - break; - case JpaObject.TYPE_FLOATLIST: - propertyUtilsBean.setProperty(t, field.getName(), - extractFloatList(getJsonElement(jsonObject, field))); - break; - case JpaObject.TYPE_DOUBLELIST: - propertyUtilsBean.setProperty(t, field.getName(), - extractDoubleList(getJsonElement(jsonObject, field))); - break; - case JpaObject.TYPE_DATETIMELIST: - propertyUtilsBean.setProperty(t, field.getName(), - extractDateTimeList(getJsonElement(jsonObject, field))); - break; - case JpaObject.TYPE_BOOLEANLIST: - propertyUtilsBean.setProperty(t, field.getName(), - extractBooleanList(getJsonElement(jsonObject, field))); - break; - default: - break; - } - } - return t; - } - - private int getColumnDefinedStringLength(Field field) { - Column column = field.getAnnotation(Column.class); - return (null != column) ? column.length() : JpaObject.length_255B; - } - - private int getElementColumnDefinedStringLength(Field field) { - ElementColumn elementColumn = field.getAnnotation(ElementColumn.class); - return (null != elementColumn) ? elementColumn.length() : JpaObject.length_255B; - } - - private JsonElement getJsonElement(JsonObject jsonObject, Field field) { - JsonElement element = null; - if ((null != jsonObject) && jsonObject.has(field.getName())) { - element = jsonObject.get(field.getName()); - } - return element; - } - - private String extractStringOrDistinguishedName(JsonElement jsonElement, int maxLength) { - String value = null; - if (null != jsonElement) { - if (jsonElement.isJsonPrimitive()) { - value = jsonElement.getAsString(); - } else if (jsonElement.isJsonObject()) { - JsonObject o = jsonElement.getAsJsonObject(); - if (o.has(JpaObject.DISTINGUISHEDNAME)) { - value = o.get(JpaObject.DISTINGUISHEDNAME).getAsString(); - } - } else if (jsonElement.isJsonArray()) { - JsonArray os = jsonElement.getAsJsonArray(); - if (os.size() > 0) { - value = extractStringOrDistinguishedName(os.get(0), maxLength); - } - } - } - return StringTools.utf8SubString(value, maxLength); - } - - private List extractStringOrDistinguishedNameList(JsonElement jsonElement, int maxLength) { - List values = new ArrayList<>(); - if (null != jsonElement) { - if (jsonElement.isJsonPrimitive()) { - values.add(StringTools.utf8SubString(jsonElement.getAsString(), maxLength)); - } else if (jsonElement.isJsonObject()) { - JsonObject o = jsonElement.getAsJsonObject(); - if (o.has(JpaObject.DISTINGUISHEDNAME)) { - values.add(StringTools.utf8SubString(o.get(JpaObject.DISTINGUISHEDNAME).getAsString(), maxLength)); - } - } else if (jsonElement.isJsonArray()) { - JsonArray os = jsonElement.getAsJsonArray(); - for (JsonElement o : os) { - values.add(extractStringOrDistinguishedName(o, maxLength)); - } - } - } - return values; - } - - private Integer extractInteger(JsonElement jsonElement) { - Integer value = null; - if (null != jsonElement) { - if (jsonElement.isJsonPrimitive()) { - JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); - if (jsonPrimitive.isNumber()) { - value = jsonPrimitive.getAsInt(); - } - } else if (jsonElement.isJsonArray()) { - JsonArray os = jsonElement.getAsJsonArray(); - if (os.size() > 0) { - value = extractInteger(os.get(0)); - } - } - } - return value; - } - - private List extractIntegerList(JsonElement jsonElement) { - List values = new ArrayList<>(); - if (null != jsonElement) { - if (jsonElement.isJsonPrimitive()) { - JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); - if (jsonPrimitive.isNumber()) { - values.add(jsonPrimitive.getAsInt()); - } - } else if (jsonElement.isJsonArray()) { - JsonArray os = jsonElement.getAsJsonArray(); - for (JsonElement o : os) { - values.add(extractInteger(o)); - } - } - } - return values; - } - - private Long extractLong(JsonElement jsonElement) { - Long value = null; - if (null != jsonElement) { - if (jsonElement.isJsonPrimitive()) { - JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); - if (jsonPrimitive.isNumber()) { - value = jsonPrimitive.getAsLong(); - } - } else if (jsonElement.isJsonArray()) { - JsonArray os = jsonElement.getAsJsonArray(); - if (os.size() > 0) { - value = extractLong(os.get(0)); - } - } - } - return value; - } - - private List extractLongList(JsonElement jsonElement) { - List values = new ArrayList<>(); - if (null != jsonElement) { - if (jsonElement.isJsonPrimitive()) { - JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); - if (jsonPrimitive.isNumber()) { - values.add(jsonPrimitive.getAsLong()); - } - } else if (jsonElement.isJsonArray()) { - JsonArray os = jsonElement.getAsJsonArray(); - for (JsonElement o : os) { - values.add(extractLong(o)); - } - } - } - return values; - } - - private Float extractFloat(JsonElement jsonElement) { - Float value = null; - if (null != jsonElement) { - if (jsonElement.isJsonPrimitive()) { - JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); - if (jsonPrimitive.isNumber()) { - value = jsonPrimitive.getAsFloat(); - } - } else if (jsonElement.isJsonArray()) { - JsonArray os = jsonElement.getAsJsonArray(); - if (os.size() > 0) { - value = extractFloat(os.get(0)); - } - } - } - return value; - } - - private List extractFloatList(JsonElement jsonElement) { - List values = new ArrayList<>(); - if (null != jsonElement) { - if (jsonElement.isJsonPrimitive()) { - JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); - if (jsonPrimitive.isNumber()) { - values.add(jsonPrimitive.getAsFloat()); - } - } else if (jsonElement.isJsonArray()) { - JsonArray os = jsonElement.getAsJsonArray(); - for (JsonElement o : os) { - values.add(extractFloat(o)); - } - } - } - return values; - } - - private Double extractDouble(JsonElement jsonElement) { - Double value = null; - if (null != jsonElement) { - if (jsonElement.isJsonPrimitive()) { - JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); - if (jsonPrimitive.isNumber()) { - value = jsonPrimitive.getAsDouble(); - } - } else if (jsonElement.isJsonArray()) { - JsonArray os = jsonElement.getAsJsonArray(); - if (os.size() > 0) { - value = extractDouble(os.get(0)); - } - } - } - return value; - } - - private List extractDoubleList(JsonElement jsonElement) { - List values = new ArrayList<>(); - if (null != jsonElement) { - if (jsonElement.isJsonPrimitive()) { - JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); - if (jsonPrimitive.isNumber()) { - values.add(jsonPrimitive.getAsDouble()); - } - } else if (jsonElement.isJsonArray()) { - JsonArray os = jsonElement.getAsJsonArray(); - for (JsonElement o : os) { - values.add(extractDouble(o)); - } - } - } - return values; - } - - private Date extractDateTime(JsonElement jsonElement) { - Date value = null; - if (null != jsonElement) { - if (jsonElement.isJsonPrimitive()) { - JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); - if (jsonPrimitive.isString()) { - try { - value = DateTools.parseDateTime(jsonPrimitive.getAsString()); - } catch (Exception e) { - LOGGER.error(e); - } - } - } else if (jsonElement.isJsonArray()) { - JsonArray os = jsonElement.getAsJsonArray(); - if (os.size() > 0) { - value = extractDateTime(os.get(0)); - } - } - } - return value; - } - - private List extractDateTimeList(JsonElement jsonElement) { - List values = new ArrayList<>(); - if (null != jsonElement) { - if (jsonElement.isJsonPrimitive()) { - JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); - if (jsonPrimitive.isString()) { - try { - values.add(DateTools.parseDateTime(jsonPrimitive.getAsString())); - } catch (Exception e) { - LOGGER.error(e); - } - } - } else if (jsonElement.isJsonArray()) { - JsonArray os = jsonElement.getAsJsonArray(); - for (JsonElement o : os) { - values.add(extractDateTime(o)); - } - } - } - return values; - } - - private Date extractDate(JsonElement jsonElement) { - Date value = null; - if (null != jsonElement) { - if (jsonElement.isJsonPrimitive()) { - JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); - if (jsonPrimitive.isString()) { - try { - value = DateTools.parseDate(jsonPrimitive.getAsString()); - } catch (Exception e) { - LOGGER.error(e); - } - } - } else if (jsonElement.isJsonArray()) { - JsonArray os = jsonElement.getAsJsonArray(); - if (os.size() > 0) { - value = extractDate(os.get(0)); - } - } - } - return value; - } - - private Boolean extractBoolean(JsonElement jsonElement) { - Boolean value = null; - if (null != jsonElement) { - if (jsonElement.isJsonPrimitive()) { - JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); - if (jsonPrimitive.isNumber()) { - value = jsonPrimitive.getAsBoolean(); - } - } else if (jsonElement.isJsonArray()) { - JsonArray os = jsonElement.getAsJsonArray(); - if (os.size() > 0) { - value = extractBoolean(os.get(0)); - } - } - } - return value; - } - - private List extractBooleanList(JsonElement jsonElement) { - List values = new ArrayList<>(); - if (null != jsonElement) { - if (jsonElement.isJsonPrimitive()) { - JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); - if (jsonPrimitive.isNumber()) { - values.add(jsonPrimitive.getAsBoolean()); - } - } else if (jsonElement.isJsonArray()) { - JsonArray os = jsonElement.getAsJsonArray(); - for (JsonElement o : os) { - values.add(extractBoolean(o)); - } - } - } - return values; - } - - public static class Wo extends WrapBoolean { - - private static final long serialVersionUID = 7570720614789228246L; - - } - -} \ No newline at end of file diff --git a/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/table/ActionUpdateWithBundle.java b/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/table/ActionUpdateWithBundle.java new file mode 100644 index 0000000000000000000000000000000000000000..83a44c88f55f35a14d00a103eb03a01d80f84611 --- /dev/null +++ b/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/table/ActionUpdateWithBundle.java @@ -0,0 +1,72 @@ +package com.x.query.service.processing.jaxrs.table; + +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; +import com.x.base.core.entity.JpaObject; +import com.x.base.core.entity.annotation.CheckPersistType; +import com.x.base.core.entity.dynamic.DynamicEntity; +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.jaxrs.WrapBoolean; +import com.x.base.core.project.logger.Logger; +import com.x.base.core.project.logger.LoggerFactory; +import com.x.query.core.entity.schema.Table; +import com.x.query.service.processing.Business; + +class ActionUpdateWithBundle extends BaseAction { + + private static final Logger LOGGER = LoggerFactory.getLogger(ActionUpdateWithBundle.class); + + ActionResult execute(EffectivePerson effectivePerson, String flag, String bundle, JsonElement jsonElement) + throws Exception { + + LOGGER.debug("execute:{}, flag:{}, bundle:{}.", effectivePerson::getDistinguishedName, () -> flag, + () -> bundle); + + ClassLoader classLoader = Business.getDynamicEntityClassLoader(); + Thread.currentThread().setContextClassLoader(classLoader); + ActionResult result = new ActionResult<>(); + + if (StringUtils.isEmpty(bundle)) { + throw new ExceptionBundleEmpty(); + } + + try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) { + LOGGER.debug("execute:{}, flag:{}.", effectivePerson::getDistinguishedName, () -> flag); + Table table = emc.flag(flag, Table.class); + if (null == table) { + throw new ExceptionEntityNotExist(flag, Table.class); + } + DynamicEntity dynamicEntity = new DynamicEntity(table.getName()); + @SuppressWarnings("unchecked") + Class cls = (Class) classLoader + .loadClass(dynamicEntity.className()); + JpaObject o = update(jsonElement, cls); + JpaObject obj = emc.find(bundle, cls); + emc.beginTransaction(cls); + if (null != obj) { + o.copyTo(obj, JpaObject.FieldsUnmodify); + emc.check(obj, CheckPersistType.all); + } else { + emc.persist(o); + emc.check(o, CheckPersistType.all); + } + emc.commit(); + Wo wo = new Wo(); + wo.setValue(true); + result.setData(wo); + } + return result; + } + + public static class Wo extends WrapBoolean { + + private static final long serialVersionUID = 7570720614789228246L; + + } + +} \ No newline at end of file diff --git a/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/table/BaseAction.java b/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/table/BaseAction.java index 9a94f8fc62ca9e2cdac46823f6e2aea821f55190..ef4d31b71f7459e49d6a99bf1da0c9869d8ceaab 100644 --- a/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/table/BaseAction.java +++ b/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/table/BaseAction.java @@ -1,7 +1,405 @@ package com.x.query.service.processing.jaxrs.table; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import javax.persistence.Column; + +import org.apache.commons.beanutils.PropertyUtilsBean; +import org.apache.commons.lang3.reflect.FieldUtils; +import org.apache.openjpa.persistence.jdbc.ElementColumn; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import com.x.base.core.entity.JpaObject; +import com.x.base.core.entity.tools.JpaObjectTools; import com.x.base.core.project.jaxrs.StandardJaxrsAction; +import com.x.base.core.project.logger.Logger; +import com.x.base.core.project.logger.LoggerFactory; +import com.x.base.core.project.tools.DateTools; +import com.x.base.core.project.tools.StringTools; abstract class BaseAction extends StandardJaxrsAction { + private static final Logger LOGGER = LoggerFactory.getLogger(BaseAction.class); + + private PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean(); + + protected T update(JsonElement jsonElement, Class cls) + throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException, + IllegalArgumentException, SecurityException { + List fields = FieldUtils.getFieldsListWithAnnotation(cls, Column.class); + T t = cls.getConstructor().newInstance(); + JsonObject jsonObject = null; + if ((jsonElement != null) && jsonElement.isJsonObject()) { + jsonObject = jsonElement.getAsJsonObject(); + } + for (Field field : fields) { + switch (JpaObjectTools.type(field)) { + case JpaObject.TYPE_STRING: + propertyUtilsBean.setProperty(t, field.getName(), extractStringOrDistinguishedName( + getJsonElement(jsonObject, field), getColumnDefinedStringLength(field))); + break; + case JpaObject.TYPE_INTEGER: + propertyUtilsBean.setProperty(t, field.getName(), extractInteger(getJsonElement(jsonObject, field))); + break; + case JpaObject.TYPE_LONG: + propertyUtilsBean.setProperty(t, field.getName(), extractLong(getJsonElement(jsonObject, field))); + break; + case JpaObject.TYPE_FLOAT: + propertyUtilsBean.setProperty(t, field.getName(), extractFloat(getJsonElement(jsonObject, field))); + break; + case JpaObject.TYPE_DOUBLE: + propertyUtilsBean.setProperty(t, field.getName(), extractDouble(getJsonElement(jsonObject, field))); + break; + case JpaObject.TYPE_DATETIME: + propertyUtilsBean.setProperty(t, field.getName(), extractDateTime(getJsonElement(jsonObject, field))); + break; + case JpaObject.TYPE_DATE: + propertyUtilsBean.setProperty(t, field.getName(), extractDate(getJsonElement(jsonObject, field))); + break; + case JpaObject.TYPE_BOOLEAN: + propertyUtilsBean.setProperty(t, field.getName(), extractBoolean(getJsonElement(jsonObject, field))); + break; + case JpaObject.TYPE_STRINGLIST: + propertyUtilsBean.setProperty(t, field.getName(), extractStringOrDistinguishedNameList( + getJsonElement(jsonObject, field), getElementColumnDefinedStringLength(field))); + break; + case JpaObject.TYPE_INTEGERLIST: + propertyUtilsBean.setProperty(t, field.getName(), + extractIntegerList(getJsonElement(jsonObject, field))); + break; + case JpaObject.TYPE_LONGLIST: + propertyUtilsBean.setProperty(t, field.getName(), extractLongList(getJsonElement(jsonObject, field))); + break; + case JpaObject.TYPE_FLOATLIST: + propertyUtilsBean.setProperty(t, field.getName(), extractFloatList(getJsonElement(jsonObject, field))); + break; + case JpaObject.TYPE_DOUBLELIST: + propertyUtilsBean.setProperty(t, field.getName(), extractDoubleList(getJsonElement(jsonObject, field))); + break; + case JpaObject.TYPE_DATETIMELIST: + propertyUtilsBean.setProperty(t, field.getName(), + extractDateTimeList(getJsonElement(jsonObject, field))); + break; + case JpaObject.TYPE_BOOLEANLIST: + propertyUtilsBean.setProperty(t, field.getName(), + extractBooleanList(getJsonElement(jsonObject, field))); + break; + default: + break; + } + } + return t; + } + + private JsonElement getJsonElement(JsonObject jsonObject, Field field) { + JsonElement element = null; + if ((null != jsonObject) && jsonObject.has(field.getName())) { + element = jsonObject.get(field.getName()); + } + return element; + } + + private int getColumnDefinedStringLength(Field field) { + Column column = field.getAnnotation(Column.class); + return (null != column) ? column.length() : JpaObject.length_255B; + } + + private int getElementColumnDefinedStringLength(Field field) { + ElementColumn elementColumn = field.getAnnotation(ElementColumn.class); + return (null != elementColumn) ? elementColumn.length() : JpaObject.length_255B; + } + + private String extractStringOrDistinguishedName(JsonElement jsonElement, int maxLength) { + String value = null; + if (null != jsonElement) { + if (jsonElement.isJsonPrimitive()) { + value = jsonElement.getAsString(); + } else if (jsonElement.isJsonObject()) { + JsonObject o = jsonElement.getAsJsonObject(); + if (o.has(JpaObject.DISTINGUISHEDNAME)) { + value = o.get(JpaObject.DISTINGUISHEDNAME).getAsString(); + } + } else if (jsonElement.isJsonArray()) { + JsonArray os = jsonElement.getAsJsonArray(); + if (os.size() > 0) { + value = extractStringOrDistinguishedName(os.get(0), maxLength); + } + } + } + return StringTools.utf8SubString(value, maxLength); + } + + private List extractStringOrDistinguishedNameList(JsonElement jsonElement, int maxLength) { + List values = new ArrayList<>(); + if (null != jsonElement) { + if (jsonElement.isJsonPrimitive()) { + values.add(StringTools.utf8SubString(jsonElement.getAsString(), maxLength)); + } else if (jsonElement.isJsonObject()) { + JsonObject o = jsonElement.getAsJsonObject(); + if (o.has(JpaObject.DISTINGUISHEDNAME)) { + values.add(StringTools.utf8SubString(o.get(JpaObject.DISTINGUISHEDNAME).getAsString(), maxLength)); + } + } else if (jsonElement.isJsonArray()) { + JsonArray os = jsonElement.getAsJsonArray(); + for (JsonElement o : os) { + values.add(extractStringOrDistinguishedName(o, maxLength)); + } + } + } + return values; + } + + private Integer extractInteger(JsonElement jsonElement) { + Integer value = null; + if (null != jsonElement) { + if (jsonElement.isJsonPrimitive()) { + JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); + if (jsonPrimitive.isNumber()) { + value = jsonPrimitive.getAsInt(); + } + } else if (jsonElement.isJsonArray()) { + JsonArray os = jsonElement.getAsJsonArray(); + if (os.size() > 0) { + value = extractInteger(os.get(0)); + } + } + } + return value; + } + + private List extractIntegerList(JsonElement jsonElement) { + List values = new ArrayList<>(); + if (null != jsonElement) { + if (jsonElement.isJsonPrimitive()) { + JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); + if (jsonPrimitive.isNumber()) { + values.add(jsonPrimitive.getAsInt()); + } + } else if (jsonElement.isJsonArray()) { + JsonArray os = jsonElement.getAsJsonArray(); + for (JsonElement o : os) { + values.add(extractInteger(o)); + } + } + } + return values; + } + + private Long extractLong(JsonElement jsonElement) { + Long value = null; + if (null != jsonElement) { + if (jsonElement.isJsonPrimitive()) { + JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); + if (jsonPrimitive.isNumber()) { + value = jsonPrimitive.getAsLong(); + } + } else if (jsonElement.isJsonArray()) { + JsonArray os = jsonElement.getAsJsonArray(); + if (os.size() > 0) { + value = extractLong(os.get(0)); + } + } + } + return value; + } + + private List extractLongList(JsonElement jsonElement) { + List values = new ArrayList<>(); + if (null != jsonElement) { + if (jsonElement.isJsonPrimitive()) { + JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); + if (jsonPrimitive.isNumber()) { + values.add(jsonPrimitive.getAsLong()); + } + } else if (jsonElement.isJsonArray()) { + JsonArray os = jsonElement.getAsJsonArray(); + for (JsonElement o : os) { + values.add(extractLong(o)); + } + } + } + return values; + } + + private Float extractFloat(JsonElement jsonElement) { + Float value = null; + if (null != jsonElement) { + if (jsonElement.isJsonPrimitive()) { + JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); + if (jsonPrimitive.isNumber()) { + value = jsonPrimitive.getAsFloat(); + } + } else if (jsonElement.isJsonArray()) { + JsonArray os = jsonElement.getAsJsonArray(); + if (os.size() > 0) { + value = extractFloat(os.get(0)); + } + } + } + return value; + } + + private List extractFloatList(JsonElement jsonElement) { + List values = new ArrayList<>(); + if (null != jsonElement) { + if (jsonElement.isJsonPrimitive()) { + JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); + if (jsonPrimitive.isNumber()) { + values.add(jsonPrimitive.getAsFloat()); + } + } else if (jsonElement.isJsonArray()) { + JsonArray os = jsonElement.getAsJsonArray(); + for (JsonElement o : os) { + values.add(extractFloat(o)); + } + } + } + return values; + } + + private Double extractDouble(JsonElement jsonElement) { + Double value = null; + if (null != jsonElement) { + if (jsonElement.isJsonPrimitive()) { + JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); + if (jsonPrimitive.isNumber()) { + value = jsonPrimitive.getAsDouble(); + } + } else if (jsonElement.isJsonArray()) { + JsonArray os = jsonElement.getAsJsonArray(); + if (os.size() > 0) { + value = extractDouble(os.get(0)); + } + } + } + return value; + } + + private List extractDoubleList(JsonElement jsonElement) { + List values = new ArrayList<>(); + if (null != jsonElement) { + if (jsonElement.isJsonPrimitive()) { + JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); + if (jsonPrimitive.isNumber()) { + values.add(jsonPrimitive.getAsDouble()); + } + } else if (jsonElement.isJsonArray()) { + JsonArray os = jsonElement.getAsJsonArray(); + for (JsonElement o : os) { + values.add(extractDouble(o)); + } + } + } + return values; + } + + private Date extractDateTime(JsonElement jsonElement) { + Date value = null; + if (null != jsonElement) { + if (jsonElement.isJsonPrimitive()) { + JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); + if (jsonPrimitive.isString()) { + try { + value = DateTools.parseDateTime(jsonPrimitive.getAsString()); + } catch (Exception e) { + LOGGER.error(e); + } + } + } else if (jsonElement.isJsonArray()) { + JsonArray os = jsonElement.getAsJsonArray(); + if (os.size() > 0) { + value = extractDateTime(os.get(0)); + } + } + } + return value; + } + + private List extractDateTimeList(JsonElement jsonElement) { + List values = new ArrayList<>(); + if (null != jsonElement) { + if (jsonElement.isJsonPrimitive()) { + JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); + if (jsonPrimitive.isString()) { + try { + values.add(DateTools.parseDateTime(jsonPrimitive.getAsString())); + } catch (Exception e) { + LOGGER.error(e); + } + } + } else if (jsonElement.isJsonArray()) { + JsonArray os = jsonElement.getAsJsonArray(); + for (JsonElement o : os) { + values.add(extractDateTime(o)); + } + } + } + return values; + } + + private Date extractDate(JsonElement jsonElement) { + Date value = null; + if (null != jsonElement) { + if (jsonElement.isJsonPrimitive()) { + JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); + if (jsonPrimitive.isString()) { + try { + value = DateTools.parseDate(jsonPrimitive.getAsString()); + } catch (Exception e) { + LOGGER.error(e); + } + } + } else if (jsonElement.isJsonArray()) { + JsonArray os = jsonElement.getAsJsonArray(); + if (os.size() > 0) { + value = extractDate(os.get(0)); + } + } + } + return value; + } + + private Boolean extractBoolean(JsonElement jsonElement) { + Boolean value = null; + if (null != jsonElement) { + if (jsonElement.isJsonPrimitive()) { + JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); + if (jsonPrimitive.isNumber()) { + value = jsonPrimitive.getAsBoolean(); + } + } else if (jsonElement.isJsonArray()) { + JsonArray os = jsonElement.getAsJsonArray(); + if (os.size() > 0) { + value = extractBoolean(os.get(0)); + } + } + } + return value; + } + + private List extractBooleanList(JsonElement jsonElement) { + List values = new ArrayList<>(); + if (null != jsonElement) { + if (jsonElement.isJsonPrimitive()) { + JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); + if (jsonPrimitive.isNumber()) { + values.add(jsonPrimitive.getAsBoolean()); + } + } else if (jsonElement.isJsonArray()) { + JsonArray os = jsonElement.getAsJsonArray(); + for (JsonElement o : os) { + values.add(extractBoolean(o)); + } + } + } + return values; + } + } diff --git a/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/table/TableAction.java b/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/table/TableAction.java index 30c66557420db38e49a62b95f7439f698f0d5f4e..9921e88d3d89faaf0140239ffb5e0f37bd86bac5 100644 --- a/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/table/TableAction.java +++ b/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/table/TableAction.java @@ -1,7 +1,5 @@ package com.x.query.service.processing.jaxrs.table; -import java.util.List; - import javax.servlet.http.HttpServletRequest; import javax.ws.rs.Consumes; import javax.ws.rs.GET; @@ -32,18 +30,36 @@ public class TableAction extends StandardJaxrsAction { private static final Logger LOGGER = LoggerFactory.getLogger(TableAction.class); - @JaxrsMethodDescribe(value = "更新指定表中的行.", action = ActionUpdate.class) + @JaxrsMethodDescribe(value = "更新指定表中的行.", action = ActionUpdateWithBundle.class) @POST @Path("{flag}/update/{bundle}") @Produces(HttpMediaType.APPLICATION_JSON_UTF_8) @Consumes(MediaType.APPLICATION_JSON) - public void listNext(@Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request, + public void updateWithBundle(@Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request, @JaxrsParameterDescribe("标识") @PathParam("flag") String flag, @JaxrsParameterDescribe("数据行标识") @PathParam("bundle") String bundle, JsonElement jsonElement) { - ActionResult result = new ActionResult<>(); + ActionResult result = new ActionResult<>(); + EffectivePerson effectivePerson = this.effectivePerson(request); + try { + result = new ActionUpdateWithBundle().execute(effectivePerson, flag, bundle, jsonElement); + } catch (Exception e) { + LOGGER.error(e, effectivePerson, request, jsonElement); + result.error(e); + } + asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result)); + } + + @JaxrsMethodDescribe(value = "指定表中插入行.", action = ActionInsert.class) + @POST + @Path("{flag}/insert") + @Produces(HttpMediaType.APPLICATION_JSON_UTF_8) + @Consumes(MediaType.APPLICATION_JSON) + public void insert(@Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request, + @JaxrsParameterDescribe("标识") @PathParam("flag") String flag, JsonElement jsonElement) { + ActionResult result = new ActionResult<>(); EffectivePerson effectivePerson = this.effectivePerson(request); try { - result = new ActionUpdate().execute(effectivePerson, flag, bundle, jsonElement); + result = new ActionInsert().execute(effectivePerson, flag, jsonElement); } catch (Exception e) { LOGGER.error(e, effectivePerson, request, jsonElement); result.error(e); diff --git a/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/touch/ActionTest.java b/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/touch/ActionTest.java deleted file mode 100644 index 1928cf10c07b99ac594f360ce4884c0dbce4eebc..0000000000000000000000000000000000000000 --- a/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/touch/ActionTest.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.x.query.service.processing.jaxrs.touch; - -import com.google.gson.JsonElement; -import com.x.base.core.project.http.ActionResult; -import com.x.base.core.project.http.EffectivePerson; -import com.x.base.core.project.jaxrs.WrapBoolean; -import com.x.base.core.project.logger.Logger; -import com.x.base.core.project.logger.LoggerFactory; - -class ActionTest extends BaseAction { - - private static Logger logger = LoggerFactory.getLogger(ActionTest.class); - - ActionResult execute(EffectivePerson effectivePerson, JsonElement jsonElement) throws Exception { - ActionResult result = new ActionResult<>(); - System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - System.out.println(jsonElement); - System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - Wo wo = new Wo(); - result.setData(wo); - return result; - - } - - public static class Wo extends WrapBoolean { - } - -} \ No newline at end of file diff --git a/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/touch/TouchAction.java b/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/touch/TouchAction.java index d4c1eb4bd554cd22c244395c441695b4ca27951d..ed50445f4b71333834dc045fc100947cf1ebfa37 100644 --- a/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/touch/TouchAction.java +++ b/o2server/x_query_service_processing/src/main/java/com/x/query/service/processing/jaxrs/touch/TouchAction.java @@ -79,23 +79,4 @@ public class TouchAction extends StandardJaxrsAction { asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result)); } - @JaxrsMethodDescribe(value = "文字转换.", action = ActionTest.class) - @POST - @Path("test") - @Produces(HttpMediaType.APPLICATION_JSON_UTF_8) - @Consumes(MediaType.APPLICATION_JSON) - public void extract(@Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request, - JsonElement jsonElement) { - ActionResult result = new ActionResult<>(); - EffectivePerson effectivePerson = this.effectivePerson(request); - try { - result = new ActionTest().execute(effectivePerson, jsonElement); - } catch (Exception e) { - LOGGER.error(e, effectivePerson, request, null); - result.error(e); - } - asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result)); - - } - } \ No newline at end of file