diff --git a/build.gradle b/build.gradle index 06e32a013c66ecb35031cac2836c63cca34e76e4..331223c25bcda7f6c07fb702f6d08da611bd49ae 100644 --- a/build.gradle +++ b/build.gradle @@ -57,7 +57,7 @@ dependencies { api 'org.apache.logging.log4j:log4j-core:2.17.2' api 'org.apache.logging.log4j:log4j-api:2.17.2' implementation 'mysql:mysql-connector-java:8.0.27' - implementation 'org.bdware.doip:doip-audit-tool:1.5.9' + implementation 'org.bdware.doip:doip-audit-tool:1.6.1' implementation 'org.bdware.bdcontract:sdk-java:1.0.2' implementation 'org.bdware.sc:cp:1.9.91' testImplementation 'org.bdware.bdcontract:sdk-java:1.0.2' diff --git a/src/main/java/org/bdware/sc/repo/Controller.java b/src/main/java/org/bdware/sc/repo/Controller.java index 4c127f2b8ce68a37983731e4a7ba5757922ff7d1..08d2ed7efd8d13006882e1f96d20813efcd8131f 100644 --- a/src/main/java/org/bdware/sc/repo/Controller.java +++ b/src/main/java/org/bdware/sc/repo/Controller.java @@ -15,6 +15,7 @@ import org.bdware.doip.codec.JsonDoipMessage; import org.bdware.doip.codec.doipMessage.DoipMessage; import org.bdware.doip.codec.doipMessage.DoipMessageFactory; import org.bdware.doip.codec.doipMessage.DoipResponseCode; +import org.bdware.doip.codec.operations.BasicOperations; import org.bdware.doip.encrypt.SM2Signer; import org.bdware.doip.endpoint.event.EventMessageFactory; import org.bdware.doip.endpoint.event.PublishMessageType; @@ -35,31 +36,146 @@ import java.sql.*; import java.util.*; public class Controller implements RepositoryHandler, TopicHandler { - private final AuditIrpClient auditIrpClient; + public static final String TransferTypeTemplate = "{\"transferType\":\"%s\"}"; static Logger LOGGER = LogManager.getLogger(Controller.class); - private final String topicId; - private final String repoId; + // 是否需要同步标识解析系统 + public static Boolean syncIrs = false; + public static EndpointConfig config; + private final AuditIrpClient auditIrpClient; + private String topicId = ""; + private String repoId = ""; RepoEventHandler pool; - public static final String TransferTypeTemplate = "{\"transferType\":\"%s\"}"; public Controller(JsonObject jo) { TempConfigStorage storage = new TempConfigStorage(jo.toString()); - EndpointConfig config = storage.loadAsEndpointConfig(); + + JsonObject startConfig = storage.load(); + + Controller.config = storage.loadAsEndpointConfig(); auditIrpClient = new AuditIrpClient(config); - int i = 0; - for (EndpointInfo info = auditIrpClient.getEndpointInfo(); info == null && i < 20; i++) { + try { + int i = 0; + for (EndpointInfo info = auditIrpClient.getEndpointInfo(); info == null + && i < 20; i++) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + + String doId; + if (startConfig.has("doId")) { + doId = startConfig.get("doId").getAsString(); + } else { + doId = auditIrpClient.getEndpointInfo().getDoId(); + } + + Controller.syncIrs = + startConfig.has("syncIrs") && startConfig.get("syncIrs").getAsBoolean(); + + this.topicId = doId + "/updateDoMeta"; + this.repoId = doId; + + AuditDoaClient doaClient = new AuditDoaClient(doId, config, + new SM2Signer(SM2KeyPair.fromJson(storage.load().toString()))); + + + pool = new RepoEventHandler("./brokerDB", doaClient, topicId); + } catch (Exception e) { + e.printStackTrace(); + } + + if (Controller.syncIrs) { + registerTopicId(topicId); + } + } + + public static Controller newInstance(ScriptObjectMirror arg) { + JsonObject jo = JSONTool.convertMirrorToJson(arg).getAsJsonObject(); + return new Controller(jo); + } + + public static DoipMessage wrapperJOToDoipMessage(DoipMessage request, JsonObject jo) { + DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder(); + builder.createResponse( + jo.has("code") && jo.get("code").getAsInt() == 0 ? DoipResponseCode.Success + : DoipResponseCode.UnKnownError, + request); + builder.setBody(jo.get("msg").getAsString().getBytes(StandardCharsets.UTF_8)); + return builder.create(); + } + + public static String getSuffix(String id) { + int offset = id.lastIndexOf("/"); + int length = id.length(); + String suffix = id.substring(offset + 1, length); + return suffix; + } + + public static void connSQL(String url, String user, String passwd, String dbname, + String tablename, String suffix) { + // System.out.println(url+user+passwd+dbname+tablename); + Connection conn = null; + Statement stmt = null; + try { + // 注册 JDBC 驱动 + Class.forName("com.mysql.cj.jdbc.Driver"); + Properties prop = new Properties(); + prop.put("user", user); + prop.put("password", passwd); + // 打开链接 + conn = DriverManager.getConnection(url, prop); + stmt = conn.createStatement(); + String sql; + // // 数据库规模 + // sql = "SELECT COUNT(*) as itemCount FROM " + tableName; + // ResultSet rs = stmt.executeQuery(sql); + // while (rs.next()) { + // ctx.put("count", rs.getInt("itemCount")); + // } + // rs.close(); + // 数据库数据示例 + List dataList = new ArrayList<>(); + sql = "SELECT * FROM " + tablename + " LIMIT 10 OFFSET 0"; + // sql = "SELECT * FROM " + tablename;// + " LIMIT 10 OFFSET 0"; + ResultSet rs = stmt.executeQuery(sql); + ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData(); + // {"data|3":["no":'@string',"subject":'@string',"core|60-100":1}],"status":"success"} + int size = rsmd.getColumnCount(); + + while (rs.next()) { + Map rowData = new HashMap<>(); + for (int i = 0; i < size; i++) { + rowData.put(rsmd.getColumnName(i + 1), rs.getObject(i + 1)); + } + dataList.add(rowData); + } + // ctx.put("data", JsonUtil.toPrettyJson(dataList.subList(0,3))); + // System.out.println("zz"+ JsonUtil.toJson(dataList)); + Storage.doTable.put(suffix, JsonUtil.toJson(dataList)); + Storage.doType.put(suffix, String.format(TransferTypeTemplate, "doip:full")); + rs.close(); + stmt.close(); + conn.close(); + } catch (Exception se) { + // 处理 JDBC 错误 + se.printStackTrace(); + } // 处理 Class.forName 错误 + finally { + // 关闭资源 + try { + if (stmt != null) + stmt.close(); + } catch (SQLException ignored) { + } // 什么都不做 try { - Thread.sleep(100); - } catch (InterruptedException e) { - throw new RuntimeException(e); + if (conn != null) + conn.close(); + } catch (SQLException se) { + se.printStackTrace(); } } - AuditDoaClient doaClient = new AuditDoaClient(auditIrpClient.getEndpointInfo().getDoId(), - config, new SM2Signer(SM2KeyPair.fromJson(storage.load().toString()))); - this.repoId = auditIrpClient.getEndpointInfo().getDoId(); - topicId = auditIrpClient.getEndpointInfo().getDoId() + "/updateDoMeta"; - pool = new RepoEventHandler("./brokerDB", doaClient, topicId); - registerTopicId(topicId); } private void registerTopicId(String topicId) { @@ -81,22 +197,18 @@ public class Controller implements RepositoryHandler, TopicHandler { } } - public static Controller newInstance(ScriptObjectMirror arg) { - JsonObject jo = JSONTool.convertMirrorToJson(arg).getAsJsonObject(); - return new Controller(jo); - } - - public void insertDoMeta(String doId, JsonObject doMeta) { + public void upsertDoMeta(String handleType, String doId, JsonObject doMeta) { + Storage.doMeta.put(doId, doMeta.toString()); JsonObject content = new JsonObject(); - content.addProperty("type", "create"); + content.addProperty("type", handleType); content.addProperty("topicId", topicId); - content.addProperty("fromRepo", auditIrpClient.getEndpointInfo().getDoId()); + content.addProperty("fromRepo", this.repoId); content.addProperty("doId", doId); content.add("meta", doMeta); DoipMessage message = EventMessageFactory.publish("unknownID", repoId, topicId, PublishMessageType.Data, content.toString().getBytes()); pool.publish(message); - Storage.doEventTable.put("create", + Storage.doEventTable.put(handleType, JsonUtil.toJson(JsonDoipMessage.fromDoipMessage(message))); } @@ -104,7 +216,7 @@ public class Controller implements RepositoryHandler, TopicHandler { JsonObject content = new JsonObject(); content.addProperty("type", "delete"); content.addProperty("topicId", topicId); - content.addProperty("fromRepo", auditIrpClient.getEndpointInfo().getDoId()); + content.addProperty("fromRepo", this.repoId); content.addProperty("doId", doId); DoipMessage message = EventMessageFactory.publish("unknownID", repoId, topicId, PublishMessageType.Data, content.toString().getBytes()); @@ -117,72 +229,57 @@ public class Controller implements RepositoryHandler, TopicHandler { public DoipMessage handleCreate(DoipMessage doipMessage) { DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder(); DoipMessage ret = builder.createResponse(DoipResponseCode.Invalid, doipMessage).create(); - String doid = null; - try { - StateInfoBase base = new StateInfoBase(); - base.identifier = - auditIrpClient.getEndpointInfo().getDoId() + "/" + UUID.randomUUID().toString(); - base.handleValues = new JsonObject(); - base.handleValues.addProperty("repoId", auditIrpClient.getEndpointInfo().getDoId()); - doid = auditIrpClient.register(base); - if (doid == null || doid.length() == 0) { - ret.body.encodedData = - "register failed! check IRS!".getBytes(StandardCharsets.UTF_8); - return ret; - } - } catch (Exception e) { - ret.body.encodedData = - "register failed! meets exception".getBytes(StandardCharsets.UTF_8); - } - try { - JsonObject jo = doipMessage.header.parameters.attributes; - if (!jo.has("type")) { - ret.body.encodedData = "missing arguments: type".getBytes(StandardCharsets.UTF_8); - return ret; - } - String type = jo.get("type").getAsString(); - if (doid == null || doid.length() == 0) { + JsonObject jo = doipMessage.header.parameters.attributes; + + String doid = jo.has("doid") ? jo.get("doid").getAsString() + : this.repoId + "/" + UUID.randomUUID().toString(); + if (Controller.syncIrs) { + try { + StateInfoBase base = new StateInfoBase(); + base.identifier = doid; + base.handleValues = new JsonObject(); + base.handleValues.addProperty("repoId", this.repoId); + String res = auditIrpClient.register(base); + if (res == null || res.isEmpty()) { + ret.body.encodedData = + "register failed! check IRS!".getBytes(StandardCharsets.UTF_8); + return ret; + } + } catch (Exception e) { ret.body.encodedData = - "register failed! check irpserver".getBytes(StandardCharsets.UTF_8); + "register failed! meets exception".getBytes(StandardCharsets.UTF_8); return ret; } - if (type.equals("SQL")) { - return wrapperJOToDoipMessage(doipMessage, createSQL(jo, doid)); - } else if (type.equals("CSV")) { - return wrapperJOToDoipMessage(doipMessage, createCSV(jo, doid)); - } else if (type.equals("FTP")) { - return wrapperJOToDoipMessage(doipMessage, createFTP(jo, doid)); - } else if (type.equals("HTTP")) { - return wrapperJOToDoipMessage(doipMessage, createHTTP(jo, doid)); - } - return wrapperJOToDoipMessage(doipMessage, - createByte(jo, doid, doipMessage.body.getDataAsJsonString())); - } catch (Exception e) { - ByteArrayOutputStream bo = new ByteArrayOutputStream(); - e.printStackTrace(new PrintStream(bo)); - JsonObject result = new JsonObject(); - result.addProperty("code", 1); - result.addProperty("msg", bo.toString()); - return wrapperJOToDoipMessage(doipMessage, result); } + + return upsertDo("create", doipMessage, doid, null); } - private JsonObject createFTP(JsonObject jo, String fullDoid) { + private JsonObject upsertFTP(String handleType, JsonObject jo, String fullDoid) { String suffix = getSuffix(fullDoid); JsonObject result = new JsonObject(); try { String url = jo.get("url").getAsString(); + JsonObject metaJO = new JsonObject(); metaJO.addProperty("doId", fullDoid); - metaJO.addProperty("createTime", System.currentTimeMillis()); + metaJO.add("createTime", jo.get("createTime")); + // 当handleType为create时,修改时间与创建时间一致 + if (handleType.equals("create")) { + metaJO.add("updateTime", jo.get("createTime")); + } else { + metaJO.addProperty("updateTime", System.currentTimeMillis()); + } metaJO.addProperty("owner", jo.get("owner").getAsString()); metaJO.addProperty("type", "FTP"); metaJO.addProperty("contentType", "plain/txt"); metaJO.addProperty("transferType", "ftp:full"); metaJO.addProperty("description", jo.get("description").getAsString()); + Storage.doTable.put(suffix, url); Storage.doType.put(suffix, String.format(TransferTypeTemplate, "ftp:full")); - insertDoMeta(fullDoid, metaJO); + upsertDoMeta(handleType, fullDoid, metaJO); + result.addProperty("code", 0); result.addProperty("msg", fullDoid); } catch (Exception e) { @@ -194,21 +291,30 @@ public class Controller implements RepositoryHandler, TopicHandler { return result; } - private JsonObject createHTTP(JsonObject jo, String fullDoid) { + private JsonObject upsertHTTP(String handleType, JsonObject jo, String fullDoid) { String suffix = getSuffix(fullDoid); JsonObject result = new JsonObject(); try { String url = jo.get("url").getAsString(); + JsonObject metaJO = new JsonObject(); metaJO.addProperty("doId", fullDoid); - metaJO.addProperty("createTime", System.currentTimeMillis()); + metaJO.add("createTime", jo.get("createTime")); + // 当handleType为create时,修改时间与创建时间一致 + if (handleType.equals("create")) { + metaJO.add("updateTime", jo.get("createTime")); + } else { + metaJO.addProperty("updateTime", System.currentTimeMillis()); + } metaJO.addProperty("owner", jo.get("owner").getAsString()); metaJO.addProperty("type", "HTTP"); metaJO.addProperty("transferType", "http:full"); metaJO.addProperty("description", jo.get("description").getAsString()); + Storage.doTable.put(suffix, url); Storage.doType.put(suffix, String.format(TransferTypeTemplate, "http:full")); - insertDoMeta(fullDoid, metaJO); + upsertDoMeta(handleType, fullDoid, metaJO); + result.addProperty("code", 0); result.addProperty("msg", fullDoid); } catch (Exception e) { @@ -222,17 +328,124 @@ public class Controller implements RepositoryHandler, TopicHandler { @Override public DoipMessage handleHello(DoipMessage doipMessage) { - return null; + if (!doipMessage.header.parameters.id.equals(repoId)) { + DoipMessageFactory.DoipMessageBuilder builder = + new DoipMessageFactory.DoipMessageBuilder(); + builder.createResponse(DoipResponseCode.Declined, doipMessage); + builder.setBody("repoId not matched!".getBytes()); + return builder.create(); + } + DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder(); + builder.createResponse(DoipResponseCode.Success, doipMessage); + builder.addAttributes("doId", repoId); + JsonObject repoInfo = new JsonObject(); + repoInfo.addProperty("doId", repoId); + repoInfo.addProperty("name", Controller.config.repoName); + repoInfo.addProperty("pubKey", Controller.config.publicKey); + repoInfo.addProperty("repoType", "system/bdware/repository"); + builder.addAttributes("repoInfo", repoInfo); + builder.addAttributes("repoName", Controller.config.repoName); + builder.addAttributes("rrt", "10ms"); + return builder.create(); } @Override public DoipMessage handleListOps(DoipMessage doipMessage) { - return null; + if (!doipMessage.header.parameters.id.equals(repoId)) { + DoipMessageFactory.DoipMessageBuilder builder = + new DoipMessageFactory.DoipMessageBuilder(); + builder.createResponse(DoipResponseCode.Declined, doipMessage); + builder.setBody("repoId not matched!".getBytes()); + return builder.create(); + } + DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder(); + builder.createResponse(DoipResponseCode.Success, doipMessage); + builder.addAttributes("doId", repoId); + + JsonArray ops = new JsonArray(); + ops.add(BasicOperations.Hello.getName()); + ops.add(BasicOperations.ListOps.getName()); + ops.add(BasicOperations.Create.getName()); + ops.add(BasicOperations.Update.getName()); + ops.add(BasicOperations.Delete.getName()); + ops.add(BasicOperations.Retrieve.getName()); + ops.add(BasicOperations.Subscribe.getName()); + builder.setBody(ops.toString().getBytes()); + return builder.create(); } @Override public DoipMessage handleUpdate(DoipMessage doipMessage) { - return null; + String doid = doipMessage.header.parameters.id; + DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder(); + DoipMessage ret = builder.createResponse(DoipResponseCode.Invalid, doipMessage).create(); + + // 由于在标识解析系统中注册doId时之填写了doId、repoId这两项,并且这两项不支持修改,所以修改时不需要同步修改标识解析系统,只修改数据库即可 + if (doid == null || doid.isEmpty()) { + builder.setBody("DoId cannot be empty!".getBytes()); + return ret; + } + + String meta = Storage.doMeta.get(doid); + if (meta == null) { + builder.setBody("DoId is not exist!".getBytes()); + return ret; + } + JsonObject jo = JsonParser.parseString(meta).getAsJsonObject(); + if (jo.has("createTime")) { + doipMessage.header.parameters.attributes.add("createTime", jo.get("createTime")); + } else { + doipMessage.header.parameters.attributes.addProperty("createTime", + System.currentTimeMillis()); + } + return upsertDo("update", doipMessage, doid, jo); + } + + private DoipMessage upsertDo(String handleType, DoipMessage doipMessage, String doid, + JsonObject data) { + try { + JsonObject jo = doipMessage.header.parameters.attributes; + DoipMessageFactory.DoipMessageBuilder builder = + new DoipMessageFactory.DoipMessageBuilder(); + DoipMessage ret = + builder.createResponse(DoipResponseCode.Invalid, doipMessage).create(); + if (!jo.has("type")) { + ret.body.encodedData = "missing arguments: type".getBytes(StandardCharsets.UTF_8); + return ret; + } + String type = jo.get("type").getAsString(); + if (doid == null || doid.length() == 0) { + ret.body.encodedData = + "register failed! check irpserver".getBytes(StandardCharsets.UTF_8); + return ret; + } + + // type 为update时createTime字段从data中获取,为create时获取当前时间 + if (handleType.equals("update")) { + jo.add("createTime", data.get("createTime")); + } else { + jo.addProperty("createTime", System.currentTimeMillis()); + } + + if (type.equals("SQL")) { + return wrapperJOToDoipMessage(doipMessage, upsertSQL(handleType, jo, doid)); + } else if (type.equals("CSV")) { + return wrapperJOToDoipMessage(doipMessage, upsertCSV(handleType, jo, doid)); + } else if (type.equals("FTP")) { + return wrapperJOToDoipMessage(doipMessage, upsertFTP(handleType, jo, doid)); + } else if (type.equals("HTTP")) { + return wrapperJOToDoipMessage(doipMessage, upsertHTTP(handleType, jo, doid)); + } + return wrapperJOToDoipMessage(doipMessage, + upsertByte(handleType, jo, doid, doipMessage.body.getDataAsJsonString())); + } catch (Exception e) { + ByteArrayOutputStream bo = new ByteArrayOutputStream(); + e.printStackTrace(new PrintStream(bo)); + JsonObject result = new JsonObject(); + result.addProperty("code", 1); + result.addProperty("msg", bo.toString()); + return wrapperJOToDoipMessage(doipMessage, result); + } } @Override @@ -242,19 +455,23 @@ public class Controller implements RepositoryHandler, TopicHandler { DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder(); DoipMessage ret = builder.createResponse(DoipResponseCode.Invalid, doipMessage).create(); String result = null; - try { - StateInfoBase base = new StateInfoBase(); - base.handleValues = new JsonObject(); - base.handleValues.addProperty("repoId", auditIrpClient.getEndpointInfo().getDoId()); - result = auditIrpClient.unRegister(fullDoid); - if (result == null || result.length() == 0) { + + if (Controller.syncIrs) { + try { + StateInfoBase base = new StateInfoBase(); + base.handleValues = new JsonObject(); + base.handleValues.addProperty("repoId", this.repoId); + + result = auditIrpClient.unRegister(fullDoid); + if (result == null || result.length() == 0) { + ret.body.encodedData = + "unRegister failed! check IRS!".getBytes(StandardCharsets.UTF_8); + return ret; + } + } catch (Exception e) { ret.body.encodedData = - "unRegister failed! check IRS!".getBytes(StandardCharsets.UTF_8); - return ret; + "unRegister failed! meets exception".getBytes(StandardCharsets.UTF_8); } - } catch (Exception e) { - ret.body.encodedData = - "unRegister failed! meets exception".getBytes(StandardCharsets.UTF_8); } String suffix = getSuffix(fullDoid); @@ -267,7 +484,6 @@ public class Controller implements RepositoryHandler, TopicHandler { return builder2.create(); } - @Override public DoipMessage handleRetrieve(DoipMessage doipMessage) { DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder(); @@ -355,24 +571,7 @@ public class Controller implements RepositoryHandler, TopicHandler { LOGGER.info("test"); } - public static DoipMessage wrapperJOToDoipMessage(DoipMessage request, JsonObject jo) { - DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder(); - builder.createResponse( - jo.has("code") && jo.get("code").getAsInt() == 0 ? DoipResponseCode.Success - : DoipResponseCode.UnKnownError, - request); - builder.setBody(jo.get("msg").getAsString().getBytes(StandardCharsets.UTF_8)); - return builder.create(); - } - - public static String getSuffix(String id) { - int offset = id.lastIndexOf("/"); - int length = id.length(); - String suffix = id.substring(offset + 1, length); - return suffix; - } - - public JsonObject createSQL(JsonObject jo, String fullDoid) { + public JsonObject upsertSQL(String handleType, JsonObject jo, String fullDoid) { JsonObject ret = new JsonObject(); try { String suffix = getSuffix(fullDoid); @@ -386,8 +585,15 @@ public class Controller implements RepositoryHandler, TopicHandler { String tablename = jo.get("tablename").getAsString(); connSQL("jdbc:mysql://" + url + "/" + dbname, user, password, dbname, tablename, suffix); + JsonObject metaJO = new JsonObject(); - metaJO.addProperty("createTime", System.currentTimeMillis()); + metaJO.add("createTime", jo.get("createTime")); + // 当handleType为create时,修改时间与创建时间一致 + if (handleType.equals("create")) { + metaJO.add("updateTime", jo.get("createTime")); + } else { + metaJO.addProperty("updateTime", System.currentTimeMillis()); + } metaJO.addProperty("doId", fullDoid); if (jo.get("description") != null) metaJO.addProperty("description", jo.get("description").getAsString()); @@ -396,7 +602,8 @@ public class Controller implements RepositoryHandler, TopicHandler { metaJO.addProperty("type", "SQL"); metaJO.addProperty("contentType", "plain/txt"); metaJO.addProperty("transferType", "doip:full"); - insertDoMeta(fullDoid, metaJO); + upsertDoMeta(handleType, fullDoid, metaJO); + ret.addProperty("code", 0); ret.addProperty("msg", fullDoid); } catch (Exception e) { @@ -408,72 +615,7 @@ public class Controller implements RepositoryHandler, TopicHandler { return ret; } - public static void connSQL(String url, String user, String passwd, String dbname, - String tablename, String suffix) { - // System.out.println(url+user+passwd+dbname+tablename); - Connection conn = null; - Statement stmt = null; - try { - // 注册 JDBC 驱动 - Class.forName("com.mysql.cj.jdbc.Driver"); - Properties prop = new Properties(); - prop.put("user", user); - prop.put("password", passwd); - // 打开链接 - conn = DriverManager.getConnection(url, prop); - stmt = conn.createStatement(); - String sql; - // // 数据库规模 - // sql = "SELECT COUNT(*) as itemCount FROM " + tableName; - // ResultSet rs = stmt.executeQuery(sql); - // while (rs.next()) { - // ctx.put("count", rs.getInt("itemCount")); - // } - // rs.close(); - // 数据库数据示例 - List dataList = new ArrayList<>(); - sql = "SELECT * FROM " + tablename + " LIMIT 10 OFFSET 0"; - // sql = "SELECT * FROM " + tablename;// + " LIMIT 10 OFFSET 0"; - ResultSet rs = stmt.executeQuery(sql); - ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData(); - // {"data|3":["no":'@string',"subject":'@string',"core|60-100":1}],"status":"success"} - int size = rsmd.getColumnCount(); - - while (rs.next()) { - Map rowData = new HashMap<>(); - for (int i = 0; i < size; i++) { - rowData.put(rsmd.getColumnName(i + 1), rs.getObject(i + 1)); - } - dataList.add(rowData); - } - // ctx.put("data", JsonUtil.toPrettyJson(dataList.subList(0,3))); - // System.out.println("zz"+ JsonUtil.toJson(dataList)); - Storage.doTable.put(suffix, JsonUtil.toJson(dataList)); - Storage.doType.put(suffix, String.format(TransferTypeTemplate, "doip:full")); - rs.close(); - stmt.close(); - conn.close(); - } catch (Exception se) { - // 处理 JDBC 错误 - se.printStackTrace(); - } // 处理 Class.forName 错误 - finally { - // 关闭资源 - try { - if (stmt != null) - stmt.close(); - } catch (SQLException ignored) { - } // 什么都不做 - try { - if (conn != null) - conn.close(); - } catch (SQLException se) { - se.printStackTrace(); - } - } - } - - public JsonObject createCSV(JsonObject jo, String fullDoid) { + public JsonObject upsertCSV(String handleType, JsonObject jo, String fullDoid) { String suffix = getSuffix(fullDoid); JsonObject result = new JsonObject(); try { @@ -494,7 +636,13 @@ public class Controller implements RepositoryHandler, TopicHandler { // } JsonObject metaJO = new JsonObject(); metaJO.addProperty("doId", fullDoid); - metaJO.addProperty("createTime", System.currentTimeMillis()); + metaJO.add("createTime", jo.get("createTime")); + // 当handleType为create时,修改时间与创建时间一致 + if (handleType.equals("create")) { + metaJO.add("updateTime", jo.get("createTime")); + } else { + metaJO.addProperty("updateTime", System.currentTimeMillis()); + } metaJO.addProperty("owner", jo.get("owner").getAsString()); metaJO.addProperty("type", "CSV"); metaJO.addProperty("contentType", "application/csv"); @@ -502,7 +650,8 @@ public class Controller implements RepositoryHandler, TopicHandler { metaJO.addProperty("description", jo.get("description").getAsString()); Storage.doTable.put(suffix, content); Storage.doType.put(suffix, String.format(TransferTypeTemplate, "doip:full")); - insertDoMeta(fullDoid, metaJO); + upsertDoMeta(handleType, fullDoid, metaJO); + result.addProperty("code", 0); result.addProperty("msg", fullDoid); @@ -515,13 +664,20 @@ public class Controller implements RepositoryHandler, TopicHandler { return result; } - private JsonObject createByte(JsonObject jo, String fullDoid, String content) { + private JsonObject upsertByte(String handleType, JsonObject jo, String fullDoid, + String content) { String suffix = getSuffix(fullDoid); JsonObject result = new JsonObject(); try { JsonObject metaJO = new JsonObject(); metaJO.addProperty("doId", fullDoid); - metaJO.addProperty("createTime", System.currentTimeMillis()); + metaJO.add("createTime", jo.get("createTime")); + // 当handleType为create时,修改时间与创建时间一致 + if (handleType.equals("create")) { + metaJO.add("updateTime", jo.get("createTime")); + } else { + metaJO.addProperty("updateTime", System.currentTimeMillis()); + } metaJO.addProperty("owner", jo.get("owner").getAsString()); metaJO.addProperty("type", "ByteArray"); metaJO.addProperty("contentType", "application/octet-stream"); @@ -529,7 +685,7 @@ public class Controller implements RepositoryHandler, TopicHandler { metaJO.addProperty("description", jo.get("description").getAsString()); Storage.doTable.put(suffix, content); Storage.doType.put(suffix, String.format(TransferTypeTemplate, "doip:full")); - insertDoMeta(fullDoid, metaJO); + upsertDoMeta(handleType, fullDoid, metaJO); result.addProperty("code", 0); result.addProperty("msg", fullDoid); } catch (Exception e) { diff --git a/src/main/java/org/bdware/sc/repo/Storage.java b/src/main/java/org/bdware/sc/repo/Storage.java index 422ad20d4d9a385af1681f8c660f486e3545bcda..f4fd00c3ee0a3e4759529f218b12d48e5973f17f 100644 --- a/src/main/java/org/bdware/sc/repo/Storage.java +++ b/src/main/java/org/bdware/sc/repo/Storage.java @@ -6,6 +6,7 @@ import org.bdware.sc.boundry.utils.RocksDBUtil; public class Storage { public static RocksDBUtil doTable = RocksDBUtil.loadDB("./doTable", false); public static RocksDBUtil doType = RocksDBUtil.loadDB("./doType", false); + public static RocksDBUtil doMeta = RocksDBUtil.loadDB("./doMeta", false); // TODO Remove DoEventTable public static MultiTagIndexDBUtil doEventTable = MultiTagIndexDBUtil.loadDB("./doEvent", "events"); diff --git a/yjs/Repo.yjs b/yjs/Repo.yjs index 69ca82f2d8ac7748de9479d8c47871158e8af763..a4554ddb69f3817df7477712e30515f9f675b684 100644 --- a/yjs/Repo.yjs +++ b/yjs/Repo.yjs @@ -19,6 +19,15 @@ doipmodule RepositoryExample { return Global; } + @DOOP({ + "operationType" : "Hello" + }) + @Description("示例参数:{\"header\":{\"identifier\":\"bdtest/Repository\"}}") + export function hello(arg) { + var doipMessage = Global.controller.handleHello(arg.rawDoipMsg); + return doipMessage; + } + @DOOP({ "operationType" : "Create" }) @@ -28,6 +37,15 @@ doipmodule RepositoryExample { return doipMessage; } + @DOOP({ + "operationType" : "Update" + }) + @Description("示例参数:{\"header\":{\"identifier\":\"bdtest/Repository/test\",\"attributes\":{\"type\":\"ByteArray\",\"owner\":\"zzz\",\"description\":\"这是一段说明\"}},\"body\":\"content\"}") + export function upateDO(arg) { + var doipMessage = Global.controller.handleUpdate(arg.rawDoipMsg); + return doipMessage; + } + @DOOP({ "operationType" : "Delete" }) @@ -46,6 +64,15 @@ doipmodule RepositoryExample { return doipMessage; } + @DOOP({ + "operationType" : "ListOperations" + }) + @Description("获取do:{\"header\":{\"identifier\":\"bdtes/Repository\"}}") + export function listOps(arg) { + var doipMessage = Global.controller.handleListOps(arg.rawDoipMsg); + return doipMessage; + } + export function test(arg) { return Global.controller.test(arg); }