import java.sql.*; import java.util.*; /** Installs separate API and file collection navigation while retaining existing grants. */ class ApiIngestionMenu { public static void main(String[] args) throws Exception { String schema = System.getenv("DATAGOV_DM_SCHEMA"); if (schema == null || !schema.matches("AIXUN_DATAGOV_[A-Z0-9_]+")) throw new IllegalArgumentException("Dedicated schema required"); Properties properties = new Properties(); properties.setProperty("user", System.getenv("DATAGOV_DM_USERNAME")); properties.setProperty("password", System.getenv("DATAGOV_DM_PASSWORD")); properties.setProperty("connectTimeout", "6000"); properties.setProperty("socketTimeout", "30000"); try (Connection connection = DriverManager.getConnection("jdbc:dm://" + System.getenv("DATAGOV_DM_HOST") + ":" + System.getenv("DATAGOV_DM_PORT"), properties)) { connection.setSchema(schema); connection.setAutoCommit(false); long api = 1761490000000002820L, file = 1761490000000000090L, offline = 1761490000000000080L; if (args.length == 1 && "--apply".equals(args[0])) { try (PreparedStatement check = connection.prepareStatement("SELECT path,perms FROM sys_menu WHERE menu_id=?")) { check.setLong(1, api); try (ResultSet rows = check.executeQuery()) { if (rows.next() && (!"/api-ingestion".equals(rows.getString(1)) || !"aixun:dataops:file-ingestion:view".equals(rows.getString(2)))) throw new IllegalStateException("API menu identity conflict"); } } try (PreparedStatement update = connection.prepareStatement("UPDATE sys_menu SET menu_name=CASE WHEN menu_id=? THEN '离线采集' ELSE '文件采集' END, order_num=CASE WHEN menu_id=? THEN order_num ELSE 7 END WHERE (menu_id=? AND path='/unified-ingestion') OR (menu_id=? AND path='/file-ingestion')")) { update.setLong(1, offline); update.setLong(2, offline); update.setLong(3, offline); update.setLong(4, file); if (update.executeUpdate() != 2) throw new IllegalStateException("Existing menu identity mismatch"); } try (PreparedStatement insert = connection.prepareStatement("INSERT INTO sys_menu(menu_id,menu_name,parent_id,order_num,path,component,is_frame,is_cache,menu_type,visible,status,perms,icon,create_time) SELECT ?, 'API采集', parent_id, 6, '/api-ingestion', 'datagov/dataops/api-ingestion/index', 'N','N','C','0','0',perms,'link',CURRENT_TIMESTAMP FROM sys_menu WHERE menu_id=? AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE menu_id=?)")) { insert.setLong(1, api); insert.setLong(2, file); insert.setLong(3, api); insert.executeUpdate(); } try (PreparedStatement grants = connection.prepareStatement("INSERT INTO sys_role_menu(role_id,menu_id) SELECT role_id,? FROM sys_role_menu old_menu WHERE menu_id=? AND NOT EXISTS (SELECT 1 FROM sys_role_menu new_menu WHERE new_menu.role_id=old_menu.role_id AND new_menu.menu_id=?)")) { grants.setLong(1, api); grants.setLong(2, file); grants.setLong(3, api); grants.executeUpdate(); } connection.commit(); } try (PreparedStatement query = connection.prepareStatement("SELECT menu_name,path FROM sys_menu WHERE menu_id IN (?,?,?) ORDER BY order_num")) { query.setLong(1, offline); query.setLong(2, api); query.setLong(3, file); try (ResultSet rows = query.executeQuery()) { int count = 0; while (rows.next()) { count++; System.out.println(rows.getString(1) + " " + rows.getString(2)); } if (count != 3) throw new IllegalStateException("Menu installation incomplete"); } } } } }