public static String replaceDiCodeOdpsTablePrefix()

in client/migrationx/migrationx-transformer/src/main/java/com/aliyun/dataworks/migrationx/transformer/core/utils/DiCodeUtils.java [127:200]


    public static String replaceDiCodeOdpsTablePrefix(String code, String newPrefix) {
        if (StringUtils.isBlank(code)) {
            return code;
        }

        try {
            JsonObject codeJson = GsonUtils.fromJsonString(code, JsonObject.class);
            String srcType = codeJson.has("srcType") ? codeJson.get("srcType").getAsString() : null;
            String srcGuid = codeJson.has("srcGuid") ? codeJson.get("srcGuid").getAsString() : null;
            String dstType = codeJson.has("dstType") ? codeJson.get("dstType").getAsString() : null;
            String dstGuid = codeJson.has("dstGuid") ? codeJson.get("dstGuid").getAsString() : null;
            if (StringUtils.equalsIgnoreCase("odps", srcType)) {
                srcGuid = replaceGuidOdpsProjectPrefix(newPrefix, srcGuid);
                codeJson.addProperty("srcGuid", srcGuid);
            }
            if (StringUtils.equalsIgnoreCase("odps", dstType)) {
                dstGuid = replaceGuidOdpsProjectPrefix(newPrefix, dstGuid);
                codeJson.addProperty("dstGuid", dstGuid);
            }

            // 旧版的DI任务
            if (codeJson.has("config") && codeJson.get("config").isJsonObject()) {
                JsonObject config = codeJson.get("config").getAsJsonObject();
                if (StringUtils.equalsIgnoreCase("odps", srcType)) {
                    JsonObject reader = config.has("reader") ? config.get("reader").getAsJsonObject()
                        : new JsonObject();
                    String guid = reader.has("guid") ? reader.get("guid").getAsString() : null;
                    guid = replaceGuidOdpsProjectPrefix(newPrefix, guid);
                    reader.addProperty("guid", guid);
                    config.remove("reader");
                    config.add("reader", reader);
                    codeJson.add("config", config);
                }

                if (StringUtils.equalsIgnoreCase("odps", dstType)) {
                    JsonObject writer = config.has("writer") ? config.get("writer").getAsJsonObject()
                        : new JsonObject();
                    String guid = writer.has("guid") ? writer.get("guid").getAsString() : null;
                    guid = replaceGuidOdpsProjectPrefix(newPrefix, guid);
                    writer.addProperty("guid", guid);
                    config.remove("writer");
                    config.add("writer", writer);
                    codeJson.add("config", config);
                }
            }

            // 新版的DI任务
            if (codeJson.has("steps")) {
                JsonArray steps = codeJson.get("steps").getAsJsonArray();
                if (steps != null && steps.size() > 0) {
                    for (int i = 0; i < steps.size(); i++) {
                        JsonObject step = steps.get(i).getAsJsonObject();
                        if (!step.has("stepType")) {
                            continue;
                        }

                        String stepType = step.get("stepType").getAsString();
                        if (StringUtils.equalsIgnoreCase("odps", stepType)) {
                            if (step.has("parameter")) {
                                JsonObject parameter = step.get("parameter").getAsJsonObject();
                                String guid = parameter.has("guid") ? parameter.get("guid").getAsString() : null;
                                guid = replaceGuidOdpsProjectPrefix(newPrefix, guid);
                                parameter.addProperty("guid", guid);
                            }
                        }
                    }
                }
            }
            return GsonUtils.defaultGson.toJson(codeJson);
        } catch (Exception e) {
            LOGGER.error("replace di code error, code: {}", code, e);
        }
        return code;
    }