|
|
@@ -0,0 +1,166 @@
|
|
|
+package com.java110.code.back;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.java110.code.web.GeneratorStart;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+public class GeneratorApiListener extends BaseGenerator {
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成代码
|
|
|
+ *
|
|
|
+ * @param data
|
|
|
+ */
|
|
|
+ public void generator(Data data) {
|
|
|
+ genneratorListListener(data);
|
|
|
+ genneratorVo(data);
|
|
|
+ genneratorDataVo(data);
|
|
|
+ genneratorIBmo(data);
|
|
|
+ genneratorBmoImpl(data);
|
|
|
+ genneratorSaveListener(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成API 侦听处理类
|
|
|
+ *
|
|
|
+ * @param data
|
|
|
+ */
|
|
|
+ private void genneratorListListener(Data data) {
|
|
|
+ StringBuffer sb = readFile(GeneratorStart.class.getResource("/template/ListListener.java").getFile());
|
|
|
+ String fileContext = sb.toString();
|
|
|
+
|
|
|
+ fileContext = super.replaceTemplateContext(fileContext, data);
|
|
|
+
|
|
|
+ String writePath = this.getClass().getResource("/").getPath()
|
|
|
+ + "out/api/listener/" + data.getName() + "/List" + toUpperCaseFirstOne(data.getName()) + "sListener.java";
|
|
|
+ System.out.printf("writePath: " + writePath);
|
|
|
+ writeFile(writePath,
|
|
|
+ fileContext);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void genneratorVo(Data data) {
|
|
|
+ StringBuffer sb = readFile(GeneratorStart.class.getResource("/template/ApiVo.java").getFile());
|
|
|
+ String fileContext = sb.toString();
|
|
|
+
|
|
|
+ fileContext = super.replaceTemplateContext(fileContext, data);
|
|
|
+
|
|
|
+ String writePath = this.getClass().getResource("/").getPath()
|
|
|
+ + "out/api/vo/" + data.getName() + "/Api" + toUpperCaseFirstOne(data.getName()) + "Vo.java";
|
|
|
+ System.out.printf("writePath: " + writePath);
|
|
|
+ writeFile(writePath,
|
|
|
+ fileContext);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void genneratorDataVo(Data data) {
|
|
|
+ StringBuffer sb = readFile(GeneratorStart.class.getResource("/template/ApiDataVo.java").getFile());
|
|
|
+ String fileContext = sb.toString();
|
|
|
+
|
|
|
+ fileContext = super.replaceTemplateContext(fileContext, data);
|
|
|
+ Map columns = data.getParams();
|
|
|
+ String variable = "";
|
|
|
+ String variableGetSet = "";
|
|
|
+
|
|
|
+// variable += "private String " + data.getId() + ";\n";
|
|
|
+//
|
|
|
+// variableGetSet += "public String get" + toUpperCaseFirstOne(data.getId()) + "() {\n"
|
|
|
+// + " return " + data.getId() + ";\n"
|
|
|
+// + " }\n";
|
|
|
+// variableGetSet += "public void set" + toUpperCaseFirstOne(data.getId()) + "(String " + data.getId() + ") {\n"
|
|
|
+// + " this." + data.getId() + " = " + data.getId() + ";\n"
|
|
|
+// + " }\n";
|
|
|
+
|
|
|
+ for (Object key : columns.keySet()) {
|
|
|
+ //JSONObject column = columns.get(columnIndex);
|
|
|
+ //String key = column.getString("code");
|
|
|
+ variable += "private String " + key.toString() + ";\n";
|
|
|
+
|
|
|
+ variableGetSet += "public String get" + toUpperCaseFirstOne(key.toString()) + "() {\n"
|
|
|
+ + " return " + key + ";\n"
|
|
|
+ + " }\n";
|
|
|
+ variableGetSet += "public void set" + toUpperCaseFirstOne(key.toString()) + "(String " + key + ") {\n"
|
|
|
+ + " this." + key + " = " + key + ";\n"
|
|
|
+ + " }\n";
|
|
|
+ }
|
|
|
+
|
|
|
+ fileContext = fileContext.replace("@@templateColumns@@", variable + variableGetSet);
|
|
|
+
|
|
|
+ String writePath = this.getClass().getResource("/").getPath()
|
|
|
+ + "out/api/vo/" +data.getName() + "/Api" + toUpperCaseFirstOne(data.getName()) + "DataVo.java";
|
|
|
+ System.out.printf("writePath: " + writePath);
|
|
|
+ writeFile(writePath,
|
|
|
+ fileContext);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成API 侦听处理类
|
|
|
+ *
|
|
|
+ * @param data
|
|
|
+ */
|
|
|
+ private void genneratorIBmo(Data data) {
|
|
|
+ StringBuffer sb = readFile(GeneratorStart.class.getResource("/template/IBMO.java").getFile());
|
|
|
+ String fileContext = sb.toString();
|
|
|
+
|
|
|
+ fileContext = super.replaceTemplateContext(fileContext, data);
|
|
|
+
|
|
|
+ String writePath = this.getClass().getResource("/").getPath()
|
|
|
+ + "out/api/bmo/" + data.getName() + "/I" + toUpperCaseFirstOne(data.getName()) + "BMO.java";
|
|
|
+ System.out.printf("writePath: " + writePath);
|
|
|
+ writeFile(writePath,
|
|
|
+ fileContext);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成API 侦听处理类
|
|
|
+ *
|
|
|
+ * @param data
|
|
|
+ */
|
|
|
+ private void genneratorBmoImpl(Data data) {
|
|
|
+ StringBuffer sb = readFile(GeneratorStart.class.getResource("/template/BMOImpl.java").getFile());
|
|
|
+ String fileContext = sb.toString();
|
|
|
+
|
|
|
+ fileContext = super.replaceTemplateContext(fileContext, data);
|
|
|
+
|
|
|
+ String writePath = this.getClass().getResource("/").getPath()
|
|
|
+ + "out/api/bmo/" + data.getName() + "/impl/I" + toUpperCaseFirstOne(data.getName()) + "BMO.java";
|
|
|
+ System.out.printf("writePath: " + writePath);
|
|
|
+ writeFile(writePath,
|
|
|
+ fileContext);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成API 侦听处理类
|
|
|
+ *
|
|
|
+ * @param data
|
|
|
+ */
|
|
|
+ private void genneratorSaveListener(Data data) {
|
|
|
+ StringBuffer sb = readFile(GeneratorStart.class.getResource("/template/SaveListener.java").getFile());
|
|
|
+ String fileContext = sb.toString();
|
|
|
+
|
|
|
+ fileContext = super.replaceTemplateContext(fileContext, data);
|
|
|
+
|
|
|
+ //替换校验部分代码 @@validateTemplateColumns@@
|
|
|
+ Map columns = data.getParams();
|
|
|
+ StringBuffer validateStr = new StringBuffer();
|
|
|
+ for (String key : data.getRequiredParam()) {
|
|
|
+ //JSONObject column = columns.getJSONObject(columnIndex);
|
|
|
+ validateStr.append("Assert.hasKeyAndValue(reqJson, \""+key+"\", \"请求报文中未包含"+key+"\");\n");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ fileContext = fileContext.replace("@@validateTemplateColumns@@", validateStr.toString());
|
|
|
+
|
|
|
+
|
|
|
+ String writePath = this.getClass().getResource("/").getPath()
|
|
|
+ + "out/api/listener/" + data.getName() + "/Save" + toUpperCaseFirstOne(data.getName()) + "Listener.java";
|
|
|
+ System.out.printf("writePath: " + writePath);
|
|
|
+ writeFile(writePath,
|
|
|
+ fileContext);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|