Add business blueprint generation templates
This commit is contained in:
@@ -11,6 +11,7 @@ import com.ruoyi.generator.domain.SysProjectStructure;
|
||||
import com.ruoyi.generator.domain.SysProjectTemplate;
|
||||
import com.ruoyi.generator.domain.Template;
|
||||
import com.ruoyi.generator.domain.TemplateFile;
|
||||
import com.ruoyi.generator.domain.front.dto.BusinessActionDesign;
|
||||
import com.ruoyi.generator.mapper.GenProjectMapper;
|
||||
import com.ruoyi.generator.mapper.GenTableMapper;
|
||||
import com.ruoyi.generator.util.VelocityInitializer;
|
||||
@@ -25,6 +26,7 @@ import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -392,12 +394,32 @@ public class GenProjectServiceImpl implements IGenProjectService {
|
||||
setSubTable(table);
|
||||
setPkColumn(table);
|
||||
VelocityContext context = VelocityUtils.prepareContext(table);
|
||||
putTableBusinessActions(context, project, table);
|
||||
putSnippetVariables(context, table, modules, templateFile);
|
||||
return context;
|
||||
}
|
||||
return VelocityUtils.prepareContextProject(project);
|
||||
}
|
||||
|
||||
private void putTableBusinessActions(VelocityContext context, GenProject project, GenTable table) {
|
||||
List<BusinessActionDesign> tableBusinessActions = filterTableBusinessActions(project, table);
|
||||
context.put("tableBusinessActions", tableBusinessActions);
|
||||
context.put("hasTableBusinessActions", StringUtils.isNotEmpty(tableBusinessActions));
|
||||
}
|
||||
|
||||
private List<BusinessActionDesign> filterTableBusinessActions(GenProject project, GenTable table) {
|
||||
if (project == null || table == null || StringUtils.isEmpty(project.getBusinessActions())) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
List<BusinessActionDesign> tableBusinessActions = new ArrayList<BusinessActionDesign>();
|
||||
for (BusinessActionDesign action : project.getBusinessActions()) {
|
||||
if (action != null && StringUtils.equals(action.getOwnerTable(), table.getTableName())) {
|
||||
tableBusinessActions.add(action);
|
||||
}
|
||||
}
|
||||
return tableBusinessActions;
|
||||
}
|
||||
|
||||
private GenTable getRequiredTable(GenProject project, Long tableId) {
|
||||
if (project != null && StringUtils.isNotEmpty(project.getTables())) {
|
||||
for (GenTable projectTable : project.getTables()) {
|
||||
|
||||
@@ -127,12 +127,16 @@ public class VelocityUtils {
|
||||
velocityContext.put("packageName", genProject.getPackageName());
|
||||
velocityContext.put("projectName", genProject.getProjectName());
|
||||
velocityContext.put("projectFileName", genProject.getProjectFileName());
|
||||
List<GenTable> tables = genProject.getTables() == null
|
||||
? Collections.<GenTable>emptyList() : genProject.getTables();
|
||||
velocityContext.put("tables", tables);
|
||||
velocityContext.put("hasTables", StringUtils.isNotEmpty(tables));
|
||||
List<?> businessActions = genProject.getBusinessActions() == null
|
||||
? Collections.emptyList() : genProject.getBusinessActions();
|
||||
velocityContext.put("businessActions", businessActions);
|
||||
velocityContext.put("hasBusinessActions", StringUtils.isNotEmpty(businessActions));
|
||||
StringBuffer createTableSql = new StringBuffer();
|
||||
for (GenTable table : genProject.getTables()) {
|
||||
for (GenTable table : tables) {
|
||||
createTableSql.append("\n");
|
||||
createTableSql.append(table.getCreateTableSql());
|
||||
createTableSql.append("\n");
|
||||
|
||||
@@ -10,6 +10,7 @@ import com.ruoyi.generator.domain.SysProjectStructure;
|
||||
import com.ruoyi.generator.domain.SysProjectTemplate;
|
||||
import com.ruoyi.generator.domain.Template;
|
||||
import com.ruoyi.generator.domain.TemplateFile;
|
||||
import com.ruoyi.generator.domain.front.dto.BusinessActionDesign;
|
||||
import com.ruoyi.generator.mapper.GenProjectMapper;
|
||||
import com.ruoyi.generator.mapper.GenTableMapper;
|
||||
import org.junit.Before;
|
||||
@@ -142,6 +143,32 @@ public class GenProjectServiceImplTest {
|
||||
assertFalse(content.contains("public void wrong() {}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void previewCodeExposesBusinessActionsForCurrentTableOnly() {
|
||||
GenProject project = project();
|
||||
project.setBusinessActions(Arrays.asList(
|
||||
businessAction("borrow_book", "sys_user"),
|
||||
businessAction("return_book", "sys_role")
|
||||
));
|
||||
GenTable table = table(11L, "sys_user", "User");
|
||||
|
||||
when(genProjectMapper.selectGenProjectById(1L)).thenReturn(project);
|
||||
when(genTableMapper.selectGenTablesByProjectId(1L)).thenReturn(Arrays.asList(table));
|
||||
when(genTableMapper.selectGenTableById(11L)).thenReturn(table);
|
||||
when(sysProjectModuleService.selectSysProjectModuleList(any(SysProjectModule.class))).thenReturn(Arrays.asList(projectModule(1L)));
|
||||
when(codeSnippetService.selectSysCodeSnippetList(any(SysCodeSnippet.class))).thenReturn(Arrays.asList());
|
||||
when(sysProjectTemplateService.selectSysProjectTemplateByProjectAndType(1L, "backend")).thenReturn(projectTemplate(100L));
|
||||
when(templateFileService.selectTemplateFilesByTemplateId(100L)).thenReturn(Arrays.asList(
|
||||
templateFile("controller.java.vm", "#if($hasTableBusinessActions)#foreach($action in $tableBusinessActions)${action.code} #end#end")
|
||||
));
|
||||
|
||||
Map<String, String> preview = service.previewCode(1L, 11L, "controller.java.vm", "backend");
|
||||
|
||||
String content = preview.get("controller.java.vm");
|
||||
assertTrue(content.contains("borrow_book"));
|
||||
assertFalse(content.contains("return_book"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getProjectStructureForDraftProjectUsesFirstActiveTemplateWithoutLoadingProject() {
|
||||
GenProject project = project();
|
||||
@@ -326,4 +353,14 @@ public class GenProjectServiceImplTest {
|
||||
snippet.setDelFlag("0");
|
||||
return snippet;
|
||||
}
|
||||
|
||||
private BusinessActionDesign businessAction(String code, String ownerTable) {
|
||||
BusinessActionDesign action = new BusinessActionDesign();
|
||||
action.setCode(code);
|
||||
action.setName(code);
|
||||
action.setOwnerTable(ownerTable);
|
||||
action.setMethod("POST");
|
||||
action.setPath("/" + code);
|
||||
return action;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.ruoyi.generator.service.front;
|
||||
|
||||
import com.ruoyi.generator.domain.GenProject;
|
||||
import com.ruoyi.generator.domain.GenTable;
|
||||
import com.ruoyi.generator.domain.front.FrontProject;
|
||||
import com.ruoyi.generator.domain.front.FrontProjectColumn;
|
||||
import com.ruoyi.generator.domain.front.FrontProjectTable;
|
||||
@@ -99,6 +100,24 @@ public class FrontProjectPreviewServiceImplTest
|
||||
assertFalse((Boolean) context.get("hasBusinessActions"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void projectVelocityContextExposesTablesForProjectLevelTemplates()
|
||||
{
|
||||
GenProject genProject = new GenProject();
|
||||
genProject.setProjectName("Demo");
|
||||
genProject.setProjectFileName("demo");
|
||||
genProject.setPackageName("com.example.demo");
|
||||
GenTable table = new GenTable();
|
||||
table.setTableName("book_info");
|
||||
table.setClassName("BookInfo");
|
||||
table.setBusinessName("bookInfo");
|
||||
genProject.setTables(Arrays.asList(table));
|
||||
|
||||
VelocityContext context = VelocityUtils.prepareContextProject(genProject);
|
||||
|
||||
assertEquals(genProject.getTables(), context.get("tables"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void downloadAllPackagesBackendFrontendAndAdminFrontend() throws Exception
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user