From 948d138061492e78edf24a0391032d79ab5ab536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?java=E4=B8=AD=E5=9B=BD?= Date: Sun, 17 Feb 2019 12:36:14 +0800 Subject: [PATCH] Gen the module listener --- .../io/jpress/codegen/ModuleGenerator.java | 64 +++++++++++++++++++ .../templates/module_listener_template.jf | 63 ++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 codegen/src/main/java/io/jpress/codegen/templates/module_listener_template.jf diff --git a/codegen/src/main/java/io/jpress/codegen/ModuleGenerator.java b/codegen/src/main/java/io/jpress/codegen/ModuleGenerator.java index 27e1ea208..7c87a4ae3 100644 --- a/codegen/src/main/java/io/jpress/codegen/ModuleGenerator.java +++ b/codegen/src/main/java/io/jpress/codegen/ModuleGenerator.java @@ -65,6 +65,7 @@ public class ModuleGenerator { genModule(); genPomXml(); genCode(); + genModuleListener(); } @@ -171,5 +172,68 @@ public class ModuleGenerator { new ServiceApiGenerator(servicePackage, modelPackage, apiPath).generate(tableMetaList); new ServiceProviderGenerator(servicePackage, modelPackage, providerPath).generate(tableMetaList); + } + private void genModuleListener() + { + List tableMetaList = CodeGenHelpler.createMetaBuilder().build(); + if (StrUtil.isNotBlank(dbTables)) { + List newTableMetaList = new ArrayList(); + Set excludeTableSet = StrUtil.splitToSet(dbTables, ","); + for (TableMeta tableMeta : tableMetaList) { + if (excludeTableSet.contains(tableMeta.name.toLowerCase())) { + newTableMetaList.add(tableMeta); + } + } + tableMetaList.clear(); + tableMetaList.addAll(newTableMetaList); + } + + + System.out.println("Generate module listener ..."); + System.out.println("Module listener Output Dir: "); + + Engine engine = Engine.create("forModuleListener"); + engine.setSourceFactory(new ClassPathSourceFactory()); + engine.addSharedMethod(new StrKit()); + + + Kv data = Kv.by("forModuleListener", moduleName); + + String upcasedModuleName=(new StringBuilder()).append(Character.toUpperCase(moduleName.charAt(0))).append(moduleName.substring(1)).toString(); + data.set("moduleName", moduleName); + data.set("upcasedModuleName", upcasedModuleName); + data.set("modelPackage", modelPackage); + String template = "/io/jpress/codegen/templates/module_listener_template.jf"; + String moduleListenerContent = engine.getTemplate(template).renderToString(data); + + + String webPath = basePath + "/module-" + moduleName + "-web"; + String moduleListenerPakcage=modelPackage.substring(0, modelPackage.lastIndexOf(".")); + String path = webPath + "/src/main/java/" + moduleListenerPakcage.replace(".", "/"); + + + System.out.println("Generate module listener for "+path); + + File dir = new File(path); + if (!dir.exists()) { + dir.mkdirs(); + } + + String target = path + File.separator + upcasedModuleName + "ModuleListener" + ".java"; + + File targetFile = new File(target); + if (targetFile.exists()) { + return; + } + try { + FileWriter fw = new FileWriter(target); + try { + fw.write(moduleListenerContent); + } finally { + fw.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } } } diff --git a/codegen/src/main/java/io/jpress/codegen/templates/module_listener_template.jf b/codegen/src/main/java/io/jpress/codegen/templates/module_listener_template.jf new file mode 100644 index 000000000..297abfa63 --- /dev/null +++ b/codegen/src/main/java/io/jpress/codegen/templates/module_listener_template.jf @@ -0,0 +1,63 @@ +/** + * Copyright (c) 2016-2019, Michael Yang 杨福海 (fuhai999@gmail.com). + *

+ * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl-3.0.txt + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.jpress.module.#(moduleName); + +import com.jfinal.core.Controller; +import io.jboot.Jboot; +import io.jboot.core.listener.JbootAppListenerBase; +import io.jboot.db.model.Columns; +import io.jpress.core.menu.MenuGroup; +import io.jpress.core.module.ModuleListener; +import java.util.List; + +/** + * @author Gavin lau 刘洪义 (logicbiz@qq.com) + * @version V1.0 + * @Title: Module 监听器 + * @Description: 每个 module 都应该有这样的一个监听器,用来配置自身Module的信息,比如后台菜单等 + * @Package io.jpress.module.#(moduleName) + */ +public class #(upcasedModuleName)ModuleListener extends JbootAppListenerBase implements ModuleListener { + + + @Override + public String onRenderDashboardBox(Controller controller) { + return null; + } + + @Override + public String onRenderToolsBox(Controller controller) { + return null; + } + + @Override + public void onConfigAdminMenu(List adminMenus) { + MenuGroup menuGroup = new MenuGroup(); + menuGroup.setId("#(moduleName)"); + menuGroup.setText("#(moduleName)"); + menuGroup.setIcon(""); + menuGroup.setOrder(1); + adminMenus.add(menuGroup); + } + + @Override + public void onConfigUcenterMenu(List ucenterMenus) { + + + } + + +} -- Gitee