import { TreeNode } from "@farris/ui-treetable"; import { NewDataType, NewEntitySet, NewEntityType, NewNavigationType, NewSchema } from "../common/NewSchema"; import { EntitySet, EntityType, NavigationType, Schema } from "../common/Schema"; import { VarEntity, InjectedType } from "../common/CommonEntity"; import { CheckContext } from "./CheckExpress"; export class Table { tableName: string; properties: Property[] = [] } class Property { code: string; type: string; } /** * 构造变量(上下文和会话) * @description 校验上下文构造上下文变量和会话变量 * @param checkContext 校验上下文 * @param contexts 上下文变量 * @param sessions 会话变量 */ export function buildVariable(checkContext: CheckContext, contexts: VarEntity[], sessions: VarEntity[]) { //存储上下文和会话 contexts.forEach(c => { checkContext.contextVariables.push(c.key); }) sessions.forEach(s => { checkContext.sessionVariables.push(s.key); }) } /** * 构造数据模型(NewSchema) * @param entityStructs * @param newSchemaMap * @param injectedEntities */ export function buildNewSchemaEntityStruct(entityStructs: Table[], newSchemaMap: any, injectedEntities: any) { let entities = SchemaToStructUtils.onInitNewSchema(newSchemaMap, injectedEntities, ""); entities.forEach(entity => { getEntityStruct(entity, entityStructs); }) } /** * 构造数据模型(Schema) * @param entityStructs * @param schemaMap * @param injectedEntities */ export function buildSchemaEntityStruct(entityStructs: Table[], schemaMap: any, injectedEntities: any) { let entities = SchemaToStructUtils.OnInitSchema(schemaMap, injectedEntities, ""); entities.forEach(entity => { getEntityStruct(entity, entityStructs); }) } function getEntityStruct(testData: TreeNode, tables: Table[]) { if (testData.children.length != 0) { //有属性,建立表 let table = new Table(); if (testData.data.type.startsWith("EntityType")) table.tableName = testData.data.name; else if (testData.data.type.startsWith("EntitySet")) { table.properties.push({ code: "length", type: "Integer" }) //子表类型,额外存储length属性 table.tableName = testData.data.name; } //存储属性 testData.children.forEach(pro => { if (pro.data.type.startsWith("EntityType") || pro.data.type.startsWith("EntitySet")) { getEntityStruct(pro, tables); } let ppp: Property = { code: pro.data.name, type: pro.data.type } if (pro.data.type.startsWith("EntitySet")) ppp.type = "Set"; else if (pro.data.type.startsWith("EntityType")) ppp.type = "Type"; table.properties.push(ppp); }) tables.push(table); } } export class SchemaToStructUtils { private static oldNumber = 99999; private static newNumber = 999; //#============NewSchema=================# /** * @description 初始化NewSchema */ public static onInitNewSchema(newSchemaMap: any, injectedEntities: any, setName: string, i18nTypes: { String: string, Byte: string, Boolean: string, Date: string, DateTime: string, Decimal: string, Integer: string } = null): TreeNode[] { this.newNumber = 999; //注入实体存在且有值 var entities: TreeNode[] = []; if (injectedEntities && injectedEntities.length > 0) { //初始化:entities(页面显示内容);rootNode(跟节点);navigatorMap1(禁忌列表) let rootNode: TreeNode = { data: { "id": this.newNumber++, "name": "", "description": "", "type": "", "schema": "", "nodeType": "" }, children: [] }; let navigatorMap1 = new Map(); //根据注入实体的schemaName找到对应的rootSchema injectedEntities.forEach(injectEntity => { let rootSchema = newSchemaMap.get(injectEntity.schemaName); var currentNewSchemaName = injectEntity.schemaName; //根据注入实体类型--->执行不同方法 switch (injectEntity.injectedType) { case InjectedType.EntitySet: { let entitySet = rootSchema.entitySets.find(set => set.code == injectEntity.injectedName); rootNode = this.convertNewEntitySet(entitySet, rootSchema, navigatorMap1, currentNewSchemaName, setName, i18nTypes); }; break; case InjectedType.EntityType: { let entityType = rootSchema.entityTypes.find(type => type.code == injectEntity.injectedName); rootNode = this.convertNewEntityType(entityType, rootSchema, navigatorMap1, currentNewSchemaName, setName, i18nTypes); }; break; } if (rootNode != null) { rootNode.data.name = injectEntity.name; entities.push(rootNode); } }); } return entities; } /** * @description 转换实体类型(EntityType) * @param entityType * @param schema * @param navigatorMap1 * @returns */ public static convertNewEntityType(entityType: NewEntityType, schema: NewSchema, navigatorMap1: any, currentNewSchemaName: any, setName: string, i18nTypes: { String: string, Byte: string, Boolean: string, Date: string, DateTime: string, Decimal: string, Integer: string }): TreeNode { if (entityType) { //初始化:NewEntityType的节点 let treeNode: TreeNode = { data: { "id": this.newNumber++, "name": entityType.code, "description": entityType.name, "type": "EntityType", "schema": currentNewSchemaName, "nodeType": "Complex" }, children: [] }; //pro navigatorMap1.set(entityType.name + entityType.code, InjectedType.EntityType); if (entityType.properties != null && entityType.properties.length > 0) { entityType.properties.forEach(pro => { let proNode: TreeNode = { data: { "id": this.newNumber++, "name": pro.code, "description": pro.name, "type": this.getDataType(pro.dataType, i18nTypes), "schema": currentNewSchemaName, "nodeType": "Simple" }, children: [] }; proNode.parent = treeNode; treeNode.children.push(proNode); }); } //naPro if (entityType.navigationProperties != null && entityType.navigationProperties.length > 0) { entityType.navigationProperties.forEach(naPro => { //判断是否导航属性对应的名称在禁忌表中 if (!(navigatorMap1.get(naPro.navigationName + naPro.code))) { if (naPro.navigationType == NewNavigationType.EntityType) { let entityType = schema.entityTypes.find(type1 => type1.code == naPro.navigationName); let a = this.convertNewEntityType(entityType, schema, navigatorMap1, currentNewSchemaName, setName, i18nTypes); a.parent = treeNode; treeNode.children.push(a); } else if (naPro.navigationType == NewNavigationType.EntitySet) { let entitySet = schema.entitySets.find(set1 => set1.code == naPro.navigationName); let a = this.convertNewEntitySet(entitySet, schema, navigatorMap1, currentNewSchemaName, setName, i18nTypes); a.parent = treeNode; treeNode.children.push(a); } } else { //若导航属性在禁忌表中 if (naPro.navigationType == NewNavigationType.EntityType) { let entityType = schema.entityTypes.find(type1 => type1.code == naPro.navigationName); let a = this.convertNewEntityType(entityType, schema, navigatorMap1, currentNewSchemaName, setName, i18nTypes); a.parent = treeNode; treeNode.children.push(a); } else if (naPro.navigationType == NewNavigationType.EntitySet) { let entitySet = schema.entitySets.find(set1 => set1.code == naPro.navigationName); let a = this.convertNewEntitySet(entitySet, schema, navigatorMap1, currentNewSchemaName, setName, i18nTypes); a.parent = treeNode; treeNode.children.push(a); } } }); } return treeNode; } return null; } /** * @description 转换实体集(EntitySet) * @param set * @param schema * @param navigatorMap1 * @returns */ public static convertNewEntitySet(set: NewEntitySet, schema: NewSchema, navigatorMap1: any, currentNewSchemaName: any, setName: string, i18nTypes: { String: string, Byte: string, Boolean: string, Date: string, DateTime: string, Decimal: string, Integer: string }): TreeNode { if (set) { //将entityset转化为TreeNode结构用于展示 let treeNode: TreeNode = { data: { "id": this.newNumber++, "name": set.code, "description": set.name, "type": (setName != "" ? setName : "EntitySet") + "[" + set.code + "]", "schema": currentNewSchemaName, "nodeType": "Complex" }, children: [] }; //将转化完成的实体集添加进禁忌表,防止循环转化 navigatorMap1.set(set.name, InjectedType.EntitySet); //找到实体集对应的实体类型 let entityType = schema.entityTypes.find(entityType => entityType.code == set.entityType); //将entitytype转化为TreeNode结构用于展示 this.convertNewEntityType(entityType, schema, navigatorMap1, currentNewSchemaName, setName, i18nTypes).children.forEach(child => { treeNode.children.push(child); }); return treeNode; } //实体集为空或未定义则返回空 else return null; } //数据类型翻译 public static getDataType(type: NewDataType, i18nTypes: { String: string, Byte: string, Boolean: string, Date: string, DateTime: string, Decimal: string, Integer: string }) { if (i18nTypes != null) { switch (type) { case NewDataType.Boolean: return i18nTypes.Boolean; case NewDataType.Byte: return i18nTypes.Byte; case NewDataType.Date: return i18nTypes.Date; case NewDataType.DateTime: return i18nTypes.DateTime; case NewDataType.Decimal: return i18nTypes.Decimal; case NewDataType.Integer: return i18nTypes.Integer; case NewDataType.String: return i18nTypes.String; default: return type; } } return type; } //#=============Schema=====================# //Schema转换为TreeNode[] public static OnInitSchema(schemaMap: any, injectedEntities: any, setName: string): TreeNode[] { this.oldNumber = 99999; //注入实体存在且有值 var entities: TreeNode[] = []; if (injectedEntities && injectedEntities.length > 0) { let entityTree: TreeNode = { data: { "id": this.oldNumber++, "name": "", "type": "", "description": "", "schema": "", "nodeType": "" }, children: [] }; let navigatorMap = new Map(); injectedEntities.forEach(injectEntity => { let currentSchema = schemaMap.get(injectEntity.schemaName); let currentSchemaName = injectEntity.schemaName; switch (injectEntity.injectedType) { case InjectedType.EntitySet: { let entitySet = currentSchema.entitySets.find(entitySet => entitySet.name == injectEntity.injectedName); entityTree = this.ConvertEntitySet(entitySet, currentSchema, navigatorMap, currentSchemaName, setName); }; break; case InjectedType.EntityType: { let entityType = currentSchema.entityTypes.find(entityType => entityType.name == injectEntity.injectedName); entityTree = this.ConvertEntityType(entityType, currentSchema, navigatorMap, currentSchemaName, setName); }; break; } if (entityTree != null) { entityTree.data.name = injectEntity.name; entities.push(entityTree); } }); } return entities; } //实体类型转化 public static ConvertEntityType(type: EntityType, schema: Schema, navigatorMap1: any, currentSchemaName: any, setName: string): TreeNode { if (type) { let treeNode: TreeNode = { data: { "id": this.oldNumber++, "name": type.name, "type": "EntityType", "description": type.description, "schema": currentSchemaName, "nodeType": "Complex" }, children: [] }; //将转化的entitytype添加进禁忌表 navigatorMap1.set(type.name + type.id, InjectedType.EntityType); //将每个属性转化为每个TreeNode if (type.properties != null && type.properties.length > 0) { type.properties.forEach(property => { let propertyTree: TreeNode = { data: { "id": this.oldNumber++, "name": "", "type": property.type, "description": property.description, "schema": currentSchemaName, "nodeType": "Simple" }, children: [] }; //根据条件判断该属性是否为主键 if (property.name == type.key) { propertyTree.data.name = property.name; } else { propertyTree.data.name = property.name; } propertyTree.parent = treeNode; treeNode.children.push(propertyTree); }); } if (type.navigationProperties != null && type.navigationProperties.length > 0) { type.navigationProperties.forEach(navigationProperty => { //判断是否导航属性对应的名称不在禁忌表中 if (!(navigatorMap1.get(navigationProperty.name + navigationProperty.navigationId))) { if (navigationProperty.type == NavigationType.EntitySet) { //若导航属性类型为实体集 let entitySet = schema.entitySets.find(entitySet => entitySet.name == navigationProperty.name); let a = this.ConvertEntitySet(entitySet, schema, navigatorMap1, currentSchemaName, setName); a.parent = treeNode; treeNode.children.push(a); }//若为实体类型 else if (navigationProperty.type == NavigationType.EntityType) { let entityType = schema.entityTypes.find(entitytype => entitytype.id == navigationProperty.navigationId && entitytype.name == navigationProperty.name); let a = this.ConvertEntityType(entityType, schema, navigatorMap1, currentSchemaName, setName); a.parent = treeNode; treeNode.children.push(a); } } else { //若导航属性在禁忌表中 if (navigationProperty.type == NavigationType.EntitySet) { //若导航属性类型为实体集 let entitySet = schema.entitySets.find(entitySet => entitySet.name == navigationProperty.name); let a = this.ConvertEntitySet(entitySet, schema, navigatorMap1, currentSchemaName, setName); a.parent = treeNode; treeNode.children.push(a); }//若为实体类型 else if (navigationProperty.type == NavigationType.EntityType) { let entityType = schema.entityTypes.find(entitytype => entitytype.id == navigationProperty.navigationId && entitytype.name == navigationProperty.name); let a = this.ConvertEntityType(entityType, schema, navigatorMap1, currentSchemaName, setName); a.parent = treeNode; treeNode.children.push(a); } } }); } return treeNode; } //若entitytype为空或未定义 else return null; } //实体集转化 public static ConvertEntitySet(set: EntitySet, schema: Schema, navigatorMap1: any, currentSchemaName: any, setName: string): TreeNode { if (set) { //将entityset转化为TreeNode结构用于展示 let treeNode: TreeNode = { data: { "id": this.oldNumber++, "name": set.name, "type": (setName != "" ? setName : "EntitySet") + "[" + set.entityType + "]", "description": set.description, "schema": currentSchemaName, "nodeType": "Complex" }, children: [] }; //将转化完成的实体集添加进禁忌表,防止循环转化 navigatorMap1.set(set.name, InjectedType.EntitySet); //找到实体集对应的实体类型 let entityType = schema.entityTypes.find(entityType => entityType.name == set.entityType); //将entitytype转化为TreeNode结构用于展示 this.ConvertEntityType(entityType, schema, navigatorMap1, currentSchemaName, setName).children.forEach(child => { treeNode.children.push(child); }); return treeNode; } //实体集为空或未定义则返回空 else return null; } }