feat(metadata): wire legacy process selector
Some checks failed
ci / build-test-parity (push) Has been cancelled
Some checks failed
ci / build-test-parity (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,176 @@
|
||||
package com.eip4.platform.metadata.web;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 旧 {@code ProcessSelectDialog} 的流程选择数据源。
|
||||
*
|
||||
* <p>老窗口从 Activiti {@code ModelQuery} 读取模型名,并把模型名本身作为提交值。
|
||||
* 本端点保持这个值口径:{@code value == label == 流程名称}。本地 dev 库可能没有
|
||||
* {@code act_re_model/act_re_procdef} 数据,因此按模型、部署定义、历史流程三段降级,
|
||||
* 让迁移页可被真实页面验收。
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/process-select")
|
||||
public class ProcessSelectController {
|
||||
|
||||
private static final int DEFAULT_LIMIT = 1000;
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
public ProcessSelectController(JdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
@GetMapping("/options")
|
||||
public Map<String, Object> options(@RequestParam(required = false) String q,
|
||||
@RequestParam(defaultValue = "" + DEFAULT_LIMIT) int limit) {
|
||||
return okOptions(listProcesses(q, safeLimit(limit)));
|
||||
}
|
||||
|
||||
@GetMapping("/search")
|
||||
public Map<String, Object> search(@RequestParam(name = "term", required = false) String term,
|
||||
@RequestParam(name = "q", required = false) String q,
|
||||
@RequestParam(defaultValue = "200") int limit) {
|
||||
return okOptions(listProcesses(firstNonBlank(term, q), safeLimit(limit)));
|
||||
}
|
||||
|
||||
private List<Map<String, Object>> listProcesses(String keyword, int limit) {
|
||||
LinkedHashMap<String, Map<String, Object>> rows = new LinkedHashMap<>();
|
||||
addModelRows(rows, keyword, limit);
|
||||
if (rows.isEmpty()) {
|
||||
addProcDefRows(rows, keyword, limit);
|
||||
}
|
||||
if (rows.isEmpty()) {
|
||||
addHistoryRows(rows, keyword, limit);
|
||||
}
|
||||
return new ArrayList<>(rows.values());
|
||||
}
|
||||
|
||||
private void addModelRows(Map<String, Map<String, Object>> rows, String keyword, int limit) {
|
||||
StringBuilder sql = new StringBuilder("SELECT trim(name_) AS label, trim(id_) AS id, "
|
||||
+ "trim(key_) AS key, trim(category_) AS category FROM act_re_model "
|
||||
+ "WHERE name_ IS NOT NULL AND trim(name_) <> '' ");
|
||||
List<Object> args = new ArrayList<>();
|
||||
appendKeyword(sql, args, keyword, "name_", "key_", "category_");
|
||||
sql.append("ORDER BY lower(name_) LIMIT ?");
|
||||
args.add(limit);
|
||||
queryOptions(sql.toString(), args, rows, "model");
|
||||
}
|
||||
|
||||
private void addProcDefRows(Map<String, Map<String, Object>> rows, String keyword, int limit) {
|
||||
StringBuilder sql = new StringBuilder("SELECT COALESCE(NULLIF(trim(name_), ''), trim(key_)) AS label, "
|
||||
+ "trim(id_) AS id, trim(key_) AS key, trim(category_) AS category FROM act_re_procdef "
|
||||
+ "WHERE COALESCE(NULLIF(trim(name_), ''), trim(key_)) <> '' ");
|
||||
List<Object> args = new ArrayList<>();
|
||||
appendKeyword(sql, args, keyword, "name_", "key_", "category_");
|
||||
sql.append("ORDER BY lower(COALESCE(NULLIF(trim(name_), ''), trim(key_))), version_ DESC LIMIT ?");
|
||||
args.add(limit);
|
||||
queryOptions(sql.toString(), args, rows, "processDefinition");
|
||||
}
|
||||
|
||||
private void addHistoryRows(Map<String, Map<String, Object>> rows, String keyword, int limit) {
|
||||
StringBuilder sql = new StringBuilder("SELECT label, NULL AS id, label AS key, NULL AS category "
|
||||
+ "FROM (SELECT DISTINCT trim(processname) AS label FROM view_act_process "
|
||||
+ "WHERE processname IS NOT NULL AND trim(processname) <> '' ");
|
||||
List<Object> args = new ArrayList<>();
|
||||
appendKeyword(sql, args, keyword, "processname");
|
||||
sql.append(") p ORDER BY lower(label) LIMIT ?");
|
||||
args.add(limit);
|
||||
queryOptions(sql.toString(), args, rows, "history");
|
||||
}
|
||||
|
||||
private void queryOptions(String sql, List<Object> args, Map<String, Map<String, Object>> rows, String source) {
|
||||
try {
|
||||
jdbcTemplate.query(sql, rs -> {
|
||||
String label = trim(rs.getString("label"));
|
||||
if (label == null || rows.containsKey(label)) {
|
||||
return;
|
||||
}
|
||||
Map<String, Object> item = new LinkedHashMap<>();
|
||||
item.put("label", label);
|
||||
item.put("value", label);
|
||||
item.put("type", "process");
|
||||
item.put("source", source);
|
||||
item.put("id", trim(rs.getString("id")));
|
||||
item.put("key", trim(rs.getString("key")));
|
||||
item.put("category", trim(rs.getString("category")));
|
||||
rows.put(label, item);
|
||||
}, args.toArray());
|
||||
} catch (DataAccessException ignored) {
|
||||
// 某些环境未迁移 Flowable/历史表时,选择框降级为空而不是让页面 500。
|
||||
}
|
||||
}
|
||||
|
||||
private static void appendKeyword(StringBuilder sql, List<Object> args, String q, String... columns) {
|
||||
String keyword = normalizeKeyword(q);
|
||||
if (keyword == null) {
|
||||
return;
|
||||
}
|
||||
sql.append("AND (");
|
||||
for (int i = 0; i < columns.length; i++) {
|
||||
if (i > 0) {
|
||||
sql.append(" OR ");
|
||||
}
|
||||
sql.append("lower(").append(columns[i]).append(") LIKE ?");
|
||||
args.add(keyword);
|
||||
}
|
||||
sql.append(") ");
|
||||
}
|
||||
|
||||
private static Map<String, Object> okOptions(List<Map<String, Object>> options) {
|
||||
Map<String, Object> data = new LinkedHashMap<>();
|
||||
data.put("options", options);
|
||||
data.put("items", options);
|
||||
data.put("total", options.size());
|
||||
Map<String, Object> resp = new LinkedHashMap<>();
|
||||
resp.put("status", 0);
|
||||
resp.put("msg", "");
|
||||
resp.put("data", data);
|
||||
return resp;
|
||||
}
|
||||
|
||||
private static int safeLimit(int limit) {
|
||||
if (limit <= 0) {
|
||||
return DEFAULT_LIMIT;
|
||||
}
|
||||
return Math.min(limit, 5000);
|
||||
}
|
||||
|
||||
private static String normalizeKeyword(String q) {
|
||||
if (q == null || q.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
String s = q.trim().toLowerCase(Locale.ROOT)
|
||||
.replace("\\", "\\\\")
|
||||
.replace("%", "\\%")
|
||||
.replace("_", "\\_");
|
||||
return "%" + s + "%";
|
||||
}
|
||||
|
||||
private static String firstNonBlank(String a, String b) {
|
||||
if (a != null && !a.isBlank()) {
|
||||
return a;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
private static String trim(String value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
String s = value.trim();
|
||||
return s.isEmpty() ? null : s;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user