From 56138df075038376fa6519619a3f36f6f60405d3 Mon Sep 17 00:00:00 2001
From: xingjinshuang <xingjs@qq.com>
Date: 星期二, 31 十二月 2024 15:16:28 +0800
Subject: [PATCH] @xingjs@20241231@新增sem数据格式相关的类和代码,新增sqlite和sem的读取和使用

---
 src/main/java/com/se/simu/controller/SemFilesSimuController.java    |   26 +++
 src/main/java/com/se/simu/service/Impl/SemFilesSimuServiceImpl.java |  275 +++++++++++++++++++++++++++++++++++++++++++++
 src/main/java/com/se/simu/service/SemFilesSimuService.java          |    3 
 3 files changed, 300 insertions(+), 4 deletions(-)

diff --git a/src/main/java/com/se/simu/controller/SemFilesSimuController.java b/src/main/java/com/se/simu/controller/SemFilesSimuController.java
index ce9f838..04a8464 100644
--- a/src/main/java/com/se/simu/controller/SemFilesSimuController.java
+++ b/src/main/java/com/se/simu/controller/SemFilesSimuController.java
@@ -2,13 +2,11 @@
 
 import com.se.simu.service.SemFilesSimuService;
 import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
 import io.swagger.annotations.ApiOperation;
 import lombok.RequiredArgsConstructor;
 import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.CrossOrigin;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
 
@@ -71,4 +69,24 @@
     }
 
 
+    /**
+     * sem鏂囦欢鍒涘缓妯℃嫙
+     */
+    @ApiOperation("1-sem鏂囦欢鍒涘缓妯℃嫙")
+    @PostMapping("/create")
+    public ResponseEntity<Object> createSimuBySemFile() throws Exception {
+        return ResponseEntity.ok(semFilesSimuService.createSimuBySemFile());
+    }
+
+    /**
+     * sem鏂囦欢璇诲彇妯℃嫙
+     */
+    @ApiOperation("2-sem鏂囦欢璇诲彇妯℃嫙")
+    @ApiImplicitParam(name = "filePath", value = "鏂囦欢鍦板潃", required = true, dataType = "String", paramType = "query", example = "D:\\app\\simulation\\other\\1211SEM鏍蜂緥\\绠$偣.sem", dataTypeClass = String.class)
+    @PostMapping("/read")
+    public ResponseEntity<Object> readSemFile(@RequestParam("filePath") String filePath) throws Exception {
+        return ResponseEntity.ok(semFilesSimuService.readSemFile(filePath));
+    }
+
+
 }
diff --git a/src/main/java/com/se/simu/service/Impl/SemFilesSimuServiceImpl.java b/src/main/java/com/se/simu/service/Impl/SemFilesSimuServiceImpl.java
index 728777a..264693b 100644
--- a/src/main/java/com/se/simu/service/Impl/SemFilesSimuServiceImpl.java
+++ b/src/main/java/com/se/simu/service/Impl/SemFilesSimuServiceImpl.java
@@ -1,9 +1,19 @@
 package com.se.simu.service.Impl;
 
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
 import com.se.simu.service.SemFilesSimuService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
+import java.sql.*;
 import java.util.HashMap;
 
 @Slf4j
@@ -43,5 +53,270 @@
         return introduceMap;
     }
 
+    @Override
+    public Object createSimuBySemFile() {
+        return null;
+    }
+
+    @Override
+    public Object readSemFile(String filePath) {
+        // 鏂囦欢鍦板潃
+        log.info("filePath:{}", filePath);
+        // 澶勭悊鏂囦欢
+        JSONObject result = new JSONObject();
+        try {
+            // 娴嬭瘯杩炴帴SQLite鏁版嵁搴�
+            Connection connection = connectToSQLiteWithCopy(filePath);
+            System.out.println("SQLite鏁版嵁搴撹繛鎺ユ垚鍔燂紒");
+            Statement stmt = connection.createStatement();
+
+
+            // 鏌ヨENTITY琛ㄧ殑鏁版嵁骞惰繑鍥濲SON鏍煎紡鐨勭粨鏋�
+//            result = queryEntityTable(stmt);
+            result = queryDynamizersTable(stmt);
+
+            // 鍏抽棴杩炴帴
+            connection.close();
+            return result;
+        } catch (SQLException | IOException e) {
+            System.err.println("鎿嶄綔澶辫触: " + e.getMessage());
+            return e.getMessage();
+        }
+    }
+
+
+    /**
+     * 鏍规嵁浼犲叆鐨勬枃浠惰矾寰勶紝娣诲姞鍚庣紑 `.db` 骞惰繛鎺� SQLite 鏁版嵁搴撱��
+     *
+     * @param filePath 鏂囦欢璺緞
+     * @return SQLite鏁版嵁搴撶殑杩炴帴
+     * @throws SQLException 濡傛灉SQLite杩炴帴澶辫触
+     */
+    public static Connection connectToSQLite(String filePath) throws SQLException {
+        // 妫�鏌ユ枃浠惰矾寰勬槸鍚︿负绌�
+        if (filePath == null || filePath.trim().isEmpty()) {
+            throw new IllegalArgumentException("鏂囦欢璺緞涓嶈兘涓虹┖");
+        }
+
+        // 妫�鏌ユ枃浠舵槸鍚﹀凡缁忔湁.db鍚庣紑锛屾病鏈夊垯娣诲姞
+        if (!filePath.endsWith(".db")) {
+            filePath = filePath + ".db";
+        }
+        log.info("Connecting to SQLite database: " + filePath);
+
+        // 鍒涘缓鏂囦欢瀵硅薄
+        File dbFile = new File(filePath);
+
+        // 濡傛灉鏂囦欢涓嶅瓨鍦紝鍒欏垱寤轰竴涓柊鏂囦欢
+        if (!dbFile.exists()) {
+            log.info("鏂囦欢涓嶅瓨鍦紝璇锋鏌ユ枃浠朵綅缃槸鍚︽纭�...");
+            return null;
+            // try {
+            //     // 閫氳繃璋冪敤 createNewFile() 鏂规硶鍒涘缓鏂版枃浠�
+            //     boolean created = dbFile.createNewFile();
+            //     if (!created) {
+            //         throw new SQLException("鏃犳硶鍒涘缓鏁版嵁搴撴枃浠�");
+            //     }
+            // } catch (Exception e) {
+            //     throw new SQLException("鍒涘缓鏁版嵁搴撴枃浠舵椂鍑洪敊: " + e.getMessage(), e);
+            // }
+        }
+
+        // 浣跨敤 SQLite JDBC 杩炴帴瀛楃涓茶繛鎺ユ暟鎹簱
+        String url = "jdbc:sqlite:" + dbFile.getAbsolutePath();
+        log.info("杩炴帴鍒癝QLite鏁版嵁搴�: {}", url);
+
+        // 鍒涘缓骞惰繑鍥炴暟鎹簱杩炴帴
+        return DriverManager.getConnection(url);
+    }
+
+    /**
+     * 鏍规嵁浼犲叆鐨勬枃浠惰矾寰勶紝澶嶅埗鏂囦欢骞剁粰澶嶅埗鐨勬枃浠舵坊鍔犲悗缂� `.db`锛岀劧鍚庤繛鎺� SQLite 鏁版嵁搴撱��
+     *
+     * @param filePath 鍘熷鏂囦欢璺緞
+     * @return SQLite鏁版嵁搴撶殑杩炴帴
+     * @throws SQLException 濡傛灉SQLite杩炴帴澶辫触
+     * @throws IOException  濡傛灉鏂囦欢澶嶅埗澶辫触
+     */
+    public static Connection connectToSQLiteWithCopy(String filePath) throws SQLException, IOException {
+        // 妫�鏌ユ枃浠惰矾寰勬槸鍚︿负绌�
+        if (filePath == null || filePath.trim().isEmpty()) {
+            throw new IllegalArgumentException("鏂囦欢璺緞涓嶈兘涓虹┖");
+        }
+        // 鍒涘缓鍘熷鏂囦欢瀵硅薄
+        File originalFile = new File(filePath);
+        // 妫�鏌ュ師鏂囦欢鏄惁瀛樺湪
+        if (!originalFile.exists()) {
+            throw new FileNotFoundException("鍘熷鏂囦欢涓嶅瓨鍦細" + filePath);
+        }
+        // 鑾峰彇褰撳墠鏃堕棿鎴充綔涓烘枃浠跺悕鐨勪竴閮ㄥ垎
+        String timestamp = String.valueOf(System.currentTimeMillis());
+        // 鍒涘缓涓�涓柊鐨勬枃浠跺悕锛屾坊鍔犱竴涓殢鏈哄悗缂�浠ラ伩鍏嶆枃浠跺悕鍐茬獊
+        String newFilePath = filePath + "." + timestamp + ".db";
+        // 澶嶅埗鏂囦欢鍒版柊鐨勮矾寰�
+        copyFile(originalFile, new File(newFilePath));
+        // 浣跨敤 SQLite JDBC 杩炴帴瀛楃涓茶繛鎺ユ柊鐨勬暟鎹簱鏂囦欢
+        String url = "jdbc:sqlite:" + newFilePath;
+        log.info("杩炴帴鍒癝QLite鏁版嵁搴�: {}", url);
+        // 杩斿洖SQLite鏁版嵁搴撹繛鎺�
+        return DriverManager.getConnection(url);
+    }
+
+
+    /**
+     * 澶嶅埗鏂囦欢鍒版柊鐨勪綅缃�
+     *
+     * @param sourceFile 鍘熷鏂囦欢
+     * @param destFile   鐩爣鏂囦欢
+     * @throws IOException 濡傛灉澶嶅埗杩囩▼涓彂鐢熼敊璇�
+     */
+    private static void copyFile(File sourceFile, File destFile) throws IOException {
+        // 浣跨敤NIO鐨凢iles.copy鏂规硶杩涜楂樻晥鐨勬枃浠跺鍒�
+        Path sourcePath = sourceFile.toPath();
+        Path destinationPath = destFile.toPath();
+
+        // 澶嶅埗鏂囦欢骞惰鐩栫洰鏍囨枃浠�
+        Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
+        log.info("鏂囦欢宸插鍒跺埌锛�" + destinationPath.toString());
+    }
+
+
+    /**
+     * 鏌ヨENTITY琛ㄧ殑鏁版嵁骞惰繑鍥濲SON鏍煎紡鐨勬暟鎹�
+     *
+     * @param stmt SQLite鏁版嵁搴撹繛鎺�
+     * @return JSON鏍煎紡鐨勬煡璇㈢粨鏋�
+     * @throws SQLException 濡傛灉鏌ヨ杩囩▼涓彂鐢熼敊璇�
+     */
+    public static JSONObject queryEntityTable(Statement stmt) throws SQLException {
+        // 鏋勫缓SQL鏌ヨ璇彞
+        String querySql = "SELECT ID,DATA,SCHEMA,UUID FROM ENTITY";
+        // 鎵ц鏌ヨ骞惰繑鍥炵粨鏋�
+        try (ResultSet rs = stmt.executeQuery(querySql)) {
+            // 鍒涘缓涓�涓� JSON 鏁扮粍鐢ㄤ簬瀛樺偍澶氭潯璁板綍
+            JSONArray jsonArray = new JSONArray();
+            // 灏嗘煡璇㈢粨鏋滆浆鍖栦负JSON鏁扮粍
+            JSONObject resJsonObject = new JSONObject();
+            // 閬嶅巻鏌ヨ缁撴灉
+            while (rs.next()) {
+                JSONObject jsonObject = new JSONObject();
+                jsonObject.put("ID", rs.getInt("ID"));
+                String data = rs.getString("DATA");
+                // 灏咲ATA瀛楁杩涜鍙嶅簭鍒楀寲
+                jsonObject.put("DATA", JSON.parse(data));
+                jsonObject.put("SCHEMA", rs.getInt("SCHEMA"));
+                jsonObject.put("UUID", rs.getString("UUID"));
+                // 灏嗘瘡涓�琛岀殑缁撴灉娣诲姞鍒癑SON鏁扮粍
+                jsonArray.add(jsonObject);
+            }
+            resJsonObject.put("entity", jsonArray);
+            return resJsonObject;
+        }
+    }
+
+    /**
+     * 鏌ヨDYNAMIZERS琛ㄧ殑鏁版嵁骞惰繑鍥濲SON鏍煎紡鐨勬暟鎹�
+     * 瀵逛簬BLOB瀛楁锛岃浆鎹负Base64瀛楃涓�
+     *
+     * @param stmt SQL璇彞鎵ц瀵硅薄
+     * @return JSON鏍煎紡鐨勬煡璇㈢粨鏋�
+     * @throws SQLException 濡傛灉鏌ヨ杩囩▼涓彂鐢熼敊璇�
+     */
+    public static JSONObject queryDynamizersTable(Statement stmt) throws SQLException {
+        // 鏋勫缓SQL鏌ヨ璇彞
+        String querySql = "SELECT URL, GMLID, DATA FROM DYNAMIZERS";
+        // 鍒涘缓涓�涓� JSON 瀵硅薄鐢ㄤ簬瀛樺偍缁撴灉
+        JSONObject resultJson = new JSONObject();
+        // 鍒涘缓涓�涓� JSON 鏁扮粍鐢ㄤ簬瀛樺偍澶氭潯璁板綍
+        JSONArray jsonArray = new JSONArray();
+        // 鎵ц鏌ヨ骞惰繑鍥炵粨鏋�
+        try (ResultSet rs = stmt.executeQuery(querySql)) {
+            // 閬嶅巻鏌ヨ缁撴灉
+            while (rs.next()) {
+                JSONObject jsonObject = new JSONObject();
+                jsonObject.put("URL", rs.getString("URL"));
+                jsonObject.put("GMLID", rs.getString("GMLID"));
+                // 鑾峰彇 BLOB 鏁版嵁骞跺皢鍏惰浆鎹负 Base64 缂栫爜瀛楃涓�
+                String blobData = rs.getString("DATA");
+//                System.out.println("blobData = " + blobData);
+               /* if (blobData != null) {
+                    // 灏� BLOB 鏁版嵁杞崲涓哄瓧鑺傛暟缁�
+                    byte[] blobBytes = blobData.getBytes(1, (int) blobData.length());
+                    // 灏嗗瓧鑺傛暟缁勮浆鎹负 Base64 瀛楃涓�
+                    String base64Data = Base64.getEncoder().encodeToString(blobBytes);
+                    // 灏� Base64 瀛楃涓叉斁鍏� JSON 瀵硅薄
+                    jsonObject.put("DATA", base64Data);
+                }*/
+                // 灏嗘瘡涓�琛岀殑缁撴灉娣诲姞鍒� JSON 鏁扮粍涓�
+                jsonArray.add(jsonObject);
+            }
+            // 灏嗘煡璇㈢粨鏋滄斁鍏ユ渶缁堢殑 JSON 瀵硅薄涓�
+            resultJson.put("dynamizers", jsonArray);
+        }
+        return resultJson;
+    }
+
+    /**
+     * 浣跨敤鍒嗛〉 Query Entity Table
+     * // 鍋囪姣忛〉鏌ヨ1000鏉℃暟鎹紝鏌ヨ绗�2椤电殑鏁版嵁
+     * // JSONArray result = queryEntityTableWithPagination(stmt, 1000, 2);
+     *
+     * @param stmt       STMT
+     * @param pageSize
+     * @param pageNumber 椤电爜
+     * @return {@link JSONArray}
+     * @throws SQLException sql寮傚父
+     */
+    public static JSONArray queryEntityTableWithPagination(Statement stmt, int pageSize, int pageNumber) throws SQLException {
+        // 璁$畻鏌ヨ鐨勫亸绉婚噺
+        int offset = (pageNumber - 1) * pageSize;
+
+        // 鏋勫缓SQL鏌ヨ璇彞锛屼娇鐢↙IMIT鍜孫FFSET杩涜鍒嗛〉
+        String querySql = "SELECT * FROM ENTITY LIMIT " + pageSize + " OFFSET " + offset;
+
+        try (ResultSet rs = stmt.executeQuery(querySql)) {
+            JSONArray jsonArray = new JSONArray();
+
+            // 閬嶅巻鏌ヨ缁撴灉
+            while (rs.next()) {
+                JSONObject jsonObject = new JSONObject();
+                jsonObject.put("ID", rs.getInt("ID"));
+                jsonObject.put("DATA", rs.getString("DATA"));
+                jsonObject.put("SCHEMA", rs.getString("SCHEMA"));
+                jsonObject.put("UUID", rs.getString("UUID"));
+
+                jsonArray.add(jsonObject);
+            }
+
+            return jsonArray;
+        }
+    }
+
+
+//    public static void main(String[] args) {
+//        try {
+//            // 娴嬭瘯杩炴帴SQLite鏁版嵁搴�
+//            Connection connection = connectToSQLiteWithCopy("D:\\0a_project\\simulation\\other\\1211SEM鏍蜂緥\\绠$偣.sem");
+//            System.out.println("SQLite鏁版嵁搴撹繛鎺ユ垚鍔燂紒");
+//            // 鍏抽棴杩炴帴
+//            connection.close();
+//        } catch (SQLException | IOException e) {
+//            System.err.println("鎿嶄綔澶辫触: " + e.getMessage());
+//        }
+//    }
+
+    /**
+     * 鍒涘缓鏁版嵁搴撹繛鎺�
+     */
+    private static Connection getConnection(String sqliteDbPath) throws SQLException {
+        // 鍒涘缓SQLite鏁版嵁搴撹繛鎺�
+        Connection conn = DriverManager.getConnection("jdbc:sqlite:" + sqliteDbPath);
+        Statement stmt = conn.createStatement();
+
+        // 鍏抽棴鏁版嵁搴撹繛鎺�
+        //conn.close();
+        return conn;
+    }
+
 
 }
diff --git a/src/main/java/com/se/simu/service/SemFilesSimuService.java b/src/main/java/com/se/simu/service/SemFilesSimuService.java
index 9b743c6..62b461a 100644
--- a/src/main/java/com/se/simu/service/SemFilesSimuService.java
+++ b/src/main/java/com/se/simu/service/SemFilesSimuService.java
@@ -11,4 +11,7 @@
     Object getIntroduce();
 
 
+    Object createSimuBySemFile();
+
+    Object readSemFile(String filePath);
 }

--
Gitblit v1.9.3