Skip to content

Commit ad743b5

Browse files
committed
请求操作符新增支持监听事件 ON: { "name": { UPDATE:{ "Comment": {"userName@": "User/name" } } } },方便同步修改其它表字段记录值等
1 parent d0130bc commit ad743b5

File tree

3 files changed

+60
-7
lines changed

3 files changed

+60
-7
lines changed

APIJSONORM/src/main/java/apijson/StringUtil.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -780,20 +780,28 @@ public static String getPrice(double price, int formatType) {
780780
}
781781
}
782782

783+
public static String join(String[] arr) {
784+
return join(arr);
785+
}
783786
/** 数组以指定分隔s拼接
784787
* @param arr
785788
* @param s
786789
* @return
787790
*/
788791
public static String join(String[] arr, String s) {
789-
StringBuilder stringBuilder = new StringBuilder();
792+
if (s == null) {
793+
s = ",";
794+
}
795+
796+
StringBuilder sb = new StringBuilder();
790797
for (int i = 0; i < arr.length; i++) {
791-
stringBuilder.append(arr[i]);
792-
if(i<arr.length-1){
793-
stringBuilder.append(s);
798+
sb.append(arr[i]);
799+
if (i < arr.length-1) {
800+
sb.append(s);
794801
}
795802
}
796-
return stringBuilder.toString();
803+
804+
return sb.toString();
797805
}
798806
/**分割路径
799807
* @param path

APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static apijson.orm.Operation.UNIQUE;
2424
import static apijson.orm.Operation.UPDATE;
2525
import static apijson.orm.Operation.VERIFY;
26+
import static apijson.orm.Operation.ON;
2627

2728
import java.net.URL;
2829
import java.time.LocalDate;
@@ -151,6 +152,7 @@ public abstract class AbstractVerifier<T extends Object> implements Verifier<T>,
151152
OPERATION_KEY_LIST.add(REMOVE.name());
152153
OPERATION_KEY_LIST.add(MUST.name());
153154
OPERATION_KEY_LIST.add(REFUSE.name());
155+
OPERATION_KEY_LIST.add(ON.name());
154156
OPERATION_KEY_LIST.add(ALLOW_PARTIAL_UPDATE_FAIL.name());
155157

156158

@@ -912,6 +914,7 @@ public static <T extends Object> JSONObject parse(@NotNull final RequestMethod m
912914
String remove = StringUtil.getString(target.getString(REMOVE.name()));
913915
String must = StringUtil.getString(target.getString(MUST.name()));
914916
String refuse = StringUtil.getString(target.getString(REFUSE.name()));
917+
JSONObject on = target.getJSONObject(ON.name());
915918
String allowPartialUpdateFail = StringUtil.getString(target.getString(ALLOW_PARTIAL_UPDATE_FAIL.name()));
916919

917920

@@ -1051,6 +1054,7 @@ public static <T extends Object> JSONObject parse(@NotNull final RequestMethod m
10511054

10521055
// 解析不允许的字段>>>>>>>>>>>>>>>>>>>
10531056

1057+
Set<String> onKeys = new LinkedHashSet<>();
10541058

10551059
// 判断不允许传的key<<<<<<<<<<<<<<<<<<<<<<<<<
10561060
for (String rk : rkset) {
@@ -1084,6 +1088,11 @@ public static <T extends Object> JSONObject parse(@NotNull final RequestMethod m
10841088
+ rk + ":[] 等未定义的 Table[]:[{}] 批量操作键值对!");
10851089
}
10861090
}
1091+
1092+
// 先让其它操作符完成
1093+
// if (rv != null) { // || nulls.contains(rk)) {
1094+
// onKeys.add(rk);
1095+
// }
10871096
}
10881097
// 判断不允许传的key>>>>>>>>>>>>>>>>>>>>>>>>>
10891098

@@ -1167,6 +1176,34 @@ public static <T extends Object> JSONObject parse(@NotNull final RequestMethod m
11671176
// 校验并配置允许部分批量增删改失败>>>>>>>>>>>>>>>>>>>
11681177

11691178

1179+
String[] nks = on == null ? null : StringUtil.split(real.getString(JSONRequest.KEY_NULL));
1180+
Collection<?> nkl = nks == null || nks.length <= 0 ? new HashSet<>() : Arrays.asList(nks);
1181+
1182+
Set<Map.Entry<String, Object>> onSet = on == null ? null : on.entrySet();
1183+
if (onSet != null) {
1184+
// 没必要限制,都是后端配置的,安全可控,而且可能确实有特殊需求,需要 id, @column 等
1185+
// List<String> condKeys = new ArrayList<>(Arrays.asList(apijson.JSONRequest.KEY_ID, apijson.JSONRequest.KEY_ID_IN
1186+
// , apijson.JSONRequest.KEY_USER_ID, apijson.JSONRequest.KEY_USER_ID_IN));
1187+
// condKeys.addAll(JSONRequest.TABLE_KEY_LIST);
1188+
1189+
for (Map.Entry<String, Object> entry : onSet) {
1190+
String k = entry == null ? null : entry.getKey();
1191+
// if (condKeys.contains(k)) {
1192+
// throw new IllegalArgumentException("Request 表 structure 配置的 " + ON.name()
1193+
// + ":{ " + k + ":value } 中 key 不合法,不允许传 [" + StringUtil.join(condKeys.toArray(new String[]{})) + "] 中的任何一个 !");
1194+
// }
1195+
1196+
Object v = k == null ? null : entry.getValue();
1197+
if (v instanceof JSONObject == false) {
1198+
throw new IllegalArgumentException("Request 表 structure 配置的 " + ON.name()
1199+
+ ":{ " + k + ":value } 中 value 不合法,必须是 JSONObject {} !");
1200+
}
1201+
1202+
if (nkl.contains(k) || real.get(k) != null) {
1203+
real = parse(method, name, (JSONObject) v, real, database, schema, datasource, idCallback, creator, callback);
1204+
}
1205+
}
1206+
}
11701207
Log.i(TAG, "parse return real = " + JSON.toJSONString(real));
11711208
return real;
11721209
}

APIJSONORM/src/main/java/apijson/orm/Operation.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,19 @@ public enum Operation {
114114
*/
115115
REMOVE,
116116

117+
/**
118+
* 监听事件,用于同步到其它表,结构是
119+
* "key0": {}
120+
* 例如 "name": { "UPDATE": { "Comment": { "userName@": "/name" } } }
121+
* 当 User.name 被修改时,同步修改 Comment.userName
122+
*/
123+
ON,
124+
117125
/**
118126
* 允许批量增删改部分失败,结构是
119127
* "Table[],key[],key:alias[]"
120-
* 自动 ALLOW_PARTIAL_UPDATE_FAILED_TABLE_MAP.put
128+
* 自动 ALLOW_PARTIAL_UPDATE_FAILED_TABLE_MAP.put
121129
*/
122-
ALLOW_PARTIAL_UPDATE_FAIL;
130+
ALLOW_PARTIAL_UPDATE_FAIL;
123131

124132
}

0 commit comments

Comments
 (0)