package foundation.system; import foundation.capacity.Actor; import foundation.capacity.ActorBucket; import foundation.capacity.Capacity; import foundation.capacity.CapacityBucket; import foundation.capacity.role.Role; import foundation.capacity.role.RoleAgent; import foundation.capacity.role.RoleBucket; import foundation.capacity.sight.LineLimit; import foundation.capacity.sight.Sight; import foundation.capacity.sight.SightAgent; import foundation.capacity.sight.SightBucket; import foundation.capacity.sight.SightLineLimitManager; import foundation.dao.Filter; import foundation.dao.LineLimitManager; import foundation.dao.OrderBy; import foundation.data.entity.Entity; import foundation.data.entity.EntitySet; import foundation.data.entity.IDictionary; import foundation.data.object.DataObject; import foundation.dictionary.Dictionary; import foundation.dictionary.DictionaryBucket; import foundation.dictionary.DictionaryItem; import foundation.geography.City; import foundation.geography.CityBucket; import foundation.geography.Province; import foundation.geography.ProvinceBucket; import foundation.log.ILogWriter; import foundation.menu.Menu; import foundation.menu.MenuTree; import foundation.page.Button; import foundation.page.Page; import foundation.page.PageBucket; import foundation.page.Tab; import foundation.persist.NamedSQL; import foundation.persist.SQLRunner; import foundation.server.Initializer; import foundation.state.approve.StepsCreator; import foundation.token.IUserManager; import foundation.user.UserManager; import foundation.util.Util; public class SystemLoader extends Initializer { @Override public void startUp() throws Exception { //1. 注册系统组件 registerComponets(); //2. 加载资源 loadResources(); //3. 加载权限 loadCapacities(); } private void registerComponets() { //1. 注册日志 Handler LogHandler handler = new LogHandler(); ILogWriter.register(handler); //2. 注册用户管理器 UserManager userManager = UserManager.getInstance(); IUserManager.registerManager(userManager); //3. 注册数据权限管理器 SightLineLimitManager sightLineLimitManager = new SightLineLimitManager(); LineLimitManager.registLimitManager(sightLineLimitManager); //4. 注册销售架构审批步骤 Creator HierarchyStepsCreator stepsCreator = HierarchyStepsCreator.getInstance(); StepsCreator.registerSalesHierarchyStepsCreator(stepsCreator); } private void loadResources() throws Exception { LoadMonitor monitor = new LoadMonitor(); boolean success = monitor.lock(); if (!success) { return; } try { //1. page loadPage(monitor); //2. menu loadMenu(monitor); //3. dictionary loadDictionary(monitor); //4. geography loadGeography(monitor); //5. active monitor.activate(); } finally { monitor.release(); } } public void loadCapacities() throws Exception { LoadMonitor monitor = new LoadMonitor(); boolean success = monitor.lock(); if (!success) { return; } try { //1. actor loadActor(monitor); //2. role loadRole(monitor); //3. sight loadSight(monitor); //4. capacity loadCapacity(monitor); //5. actor-capacity loadActorCapacity(monitor); //6. active monitor.activate(); } finally { monitor.release(); } } protected void loadPage(LoadMonitor monitor) throws Exception { DataObject dataObject; EntitySet entitySet; //1. register or get bucket PageBucket pageBucket = monitor.register(PageBucket.class); //2. load page dataObject = DataObject.getInstance("sys_page"); entitySet = dataObject.getTableEntitySet(); for (Entity entity: entitySet) { Page page = new Page(); page.load(entity); pageBucket.loadOne(page.getId(), page); } //3. load tab dataObject = DataObject.getInstance("sys_page_tab"); entitySet = dataObject.getTableEntitySet(new OrderBy("page_id")); for (Entity entity: entitySet) { String pageId = entity.getString("page_id"); Tab tab = new Tab(); tab.load(entity); Page page = pageBucket.getCachedOne(pageId); page.loadOneTab(tab); } //4. load button dataObject = DataObject.getInstance("sys_page_button"); entitySet = dataObject.getTableEntitySet(new OrderBy("page_id, index_no")); for (Entity entity: entitySet) { String pageId = entity.getString("page_id"); Button button = new Button(); button.load(entity); Page page = pageBucket.getCachedOne(pageId); page.loadOneButton(button); } } protected void loadMenu(LoadMonitor monitor) throws Exception { //1. register or get bucket MenuTree menuTree = monitor.register(MenuTree.class); //2. load menu NamedSQL namedSQL = NamedSQL.getInstance("getMenu"); EntitySet entitySet = namedSQL.getEntitySet(); for (Entity entity: entitySet) { Menu menu = new Menu(); menu.load(entity); menuTree.loadOne(menu.getId(), menu); } menuTree.onAfterLoad(); } protected void loadDictionary(LoadMonitor monitor) throws Exception { DataObject dataObject; EntitySet entitySet; DictionaryBucket dictionaryBucket = DictionaryBucket.newInstance(); //1. 加载字典 dataObject = DataObject.getInstance("sys_dictionary"); entitySet = dataObject.getTableEntitySet(); for (Entity entity: entitySet) { Dictionary dictionary = new Dictionary(); dictionary.load(entity); dictionaryBucket.loadOne(dictionary); } //2. 加载字典项(保存在 sys_dictionary_detail 中 的字典项) dataObject = DataObject.getInstance("sys_dictionary_detail"); entitySet = dataObject.getTableEntitySet(new OrderBy("parent_id")); for (Entity entity: entitySet) { String parentId = entity.getString("parent_id"); Dictionary dictionary = dictionaryBucket.getCachedOne(parentId); if (dictionary == null) { continue; } DictionaryItem item = new DictionaryItem(); item.load(entity); dictionary.loadOne(item); } //3. 加载字典项(保存在其他表中的字典项) for (IDictionary iDictionary: dictionaryBucket) { Dictionary dictionary = (Dictionary)iDictionary; String dataName = dictionary.getDataName(); if (Util.isEmpty(dataName)) { continue; } DataObject itemObject = DataObject.getInstance(dataName); EntitySet itemSet = itemObject.getTableEntitySet(); for (Entity entity: itemSet) { DictionaryItem item = new DictionaryItem(); item.load(entity, dictionary.getFieldCode(), dictionary.getFieldName()); dictionary.loadOne(item); } } dictionaryBucket.activate(); } protected void loadGeography(LoadMonitor monitor) throws Exception { DataObject dataObject; EntitySet entitySet; //1. register or get bucket ProvinceBucket provinceBucket = monitor.register(ProvinceBucket.class); CityBucket cityBucket = monitor.register(CityBucket.class); //1. load dictionary bucket dataObject = DataObject.getInstance("md_geo_province"); entitySet = dataObject.getTableEntitySet(); for (Entity entity: entitySet) { Province province = new Province(); province.load(entity); provinceBucket.loadOne(province.getId(), province); } //2. load dictionary item dataObject = DataObject.getInstance("md_geo_city"); entitySet = dataObject.getTableEntitySet(new OrderBy("province_id")); for (Entity entity: entitySet) { String parentId = entity.getString("province_id"); Province province = provinceBucket.getCachedOne(parentId); if (province == null) { continue; } City city = new City(); city.load(entity); province.loadOneCity(city); cityBucket.loadOne(city.getId(), city); } } private void loadActor(LoadMonitor monitor) throws Exception { DataObject dataObject; EntitySet entitySet; ActorBucket actorBucket = monitor.register(ActorBucket.class); //1. 加载角色 dataObject = DataObject.getInstance("sys_right_actor"); entitySet = dataObject.getTableEntitySet(); Actor actor; for (Entity entity: entitySet) { actor = new Actor(); actor.load(entity); actorBucket.loadOne(actor.getId(), actor); } } public static void reloadRole(String roleId, String pageId) throws Exception { RoleBucket roleBucket = RoleBucket.getInstance(); RoleAgent roleAgent = roleBucket.get(roleId); if (roleAgent == null) { return; } if (!Util.isEmpty(pageId)) { PageBucket pageBucket = PageBucket.getInstance(); Page page = pageBucket.get(pageId); //0. load page tab loadPageTab(page); //0.1. load page button loadPageButton(page); } //1. 新建一个新的role Role result = roleAgent.newTarget(); //2. load role page loadRolePages(null, result, PageBucket.getInstance()); //3. 加载 role tabs loadRoleTabs(null, result); //4. 加载 role buttons loadRoleButtons(null, result); //5. 加载 role menus loadOneRoleMenus(result, MenuTree.getInstance()); //6. 替换原来在role roleAgent.set(result); roleAgent.onAfterLoadMenuTree(); } public static void reloadSight(String sightId) throws Exception { SightBucket sightBucket = SightBucket.getInstance(); SightAgent sightAgent = sightBucket.get(sightId); if (sightAgent == null) { return; } //1. 新建一个新的 sight Sight result = sightAgent.newTarget(); loadSightLineLimits(null, result); //2. 替换原来在 sight sightAgent.set(result); } protected void loadRole(LoadMonitor monitor) throws Exception { DataObject dataObject; EntitySet entitySet; //1. register or get bucket RoleBucket roleBucket = monitor.register(RoleBucket.class); PageBucket pageBucket = monitor.get(PageBucket.class); MenuTree menuTree = monitor.get(MenuTree.class); //2. load role dataObject = DataObject.getInstance("sys_right_role"); entitySet = dataObject.getTableEntitySet(); for (Entity entity: entitySet) { RoleAgent role = new RoleAgent(); role.load(entity); roleBucket.loadOne(role.getId(), role); } //3. load role page loadRolePages(roleBucket, null, pageBucket); //4. load role tabs loadRoleTabs(roleBucket, null); //5. load role buttons loadRoleButtons(roleBucket, null); //6. load role menus loadAllRoleMenus(roleBucket, menuTree); //7. initialize tree for (RoleAgent role: roleBucket) { role.onAfterLoadMenuTree(); } } private static void loadPageTab(Page page) throws Exception { DataObject dataObject = DataObject.getInstance("sys_page_tab"); String filter = (page == null) ? "1=1" : "page_id=" + Util.quotedStr(page.getId()); EntitySet entitySet = dataObject.getTableEntitySet(new Filter(filter), new OrderBy("page_id")); for (Entity entity: entitySet) { Tab tab = new Tab(); tab.load(entity); page.loadOneTab(tab); } } private static void loadPageButton(Page page) throws Exception { DataObject dataObject = DataObject.getInstance("sys_page_button"); String filter = (page == null) ? "1=1" : "page_id=" + Util.quotedStr(page.getId()); EntitySet entitySet = dataObject.getTableEntitySet(new Filter(filter), new OrderBy("page_id, index_no")); for (Entity entity: entitySet) { Button button = new Button(); button.load(entity); page.loadOneButton(button); } } private static void loadRolePages(RoleBucket roleBucket, Role role, PageBucket pageBucket) throws Exception { DataObject dataObject = DataObject.getInstance("sys_right_role_menu"); String filter = (role == null) ? "1=1" : "role_id=" + Util.quotedStr(role.getId()); EntitySet entitySet = dataObject.getTableEntitySet(new Filter(filter), new OrderBy("role_id")); String roleId, pageId, menuId; for (Entity entity: entitySet) { roleId = entity.getString("role_id"); pageId = entity.getString("page_id"); menuId = entity.getString("menu_id"); if (roleBucket != null) { RoleAgent agent = roleBucket.getCachedOne(roleId); if (agent == null) { continue; } role = agent.get(); } if (role == null) { logger.error("role not exists: {}", roleId); continue; } Page page = pageBucket.getCachedOne(pageId); role.loadOnePage(menuId, page); } } private static void loadRoleTabs(RoleBucket roleBucket, Role role) throws Exception { NamedSQL namedSQL = NamedSQL.getInstance("getRolePageTab"); String filter = (role == null) ? "1=1" : "role_id=" + Util.quotedStr(role.getId()); namedSQL.setFilter(filter); EntitySet entitySet = SQLRunner.getEntitySet(namedSQL); String roleId, pageId, tabId, menuId; for (Entity entity: entitySet) { roleId = entity.getString("role_code"); pageId = entity.getString("page_id"); tabId = entity.getString("tab_id"); menuId = entity.getString("menu_id"); if (roleBucket != null) { RoleAgent agent = roleBucket.getCachedOne(roleId); if (agent == null) { continue; } role = agent.get(); } if (role == null) { logger.error("role not exists: {}", roleId); continue; } role.loadOneTab(menuId, pageId, tabId); } } private static void loadRoleButtons(RoleBucket roleBucket, Role role) throws Exception { NamedSQL namedSQL = NamedSQL.getInstance("getRolePageButton"); String filter = (role == null) ? "1=1" : "role_id=" + Util.quotedStr(role.getId()); namedSQL.setFilter(filter); EntitySet entitySet = SQLRunner.getEntitySet(namedSQL); String roleId, pageId, button_id, menuId; for (Entity entity: entitySet) { roleId = entity.getString("role_code"); pageId = entity.getString("page_id"); button_id = entity.getString("button_id"); menuId = entity.getString("menu_id"); if (roleBucket != null) { RoleAgent agent = roleBucket.getCachedOne(roleId); if (agent == null) { continue; } role = agent.get(); } if (role == null) { logger.error("role not exists: {}", roleId); continue; } role.loadOneButton(menuId, pageId, button_id, entity); } } private static void loadAllRoleMenus(RoleBucket roleBucket, MenuTree menuTree) throws Exception { DataObject dataObject = DataObject.getInstance("sys_right_role_menu"); Filter filter = new Filter("is_active", "T"); EntitySet entitySet = dataObject.getTableEntitySet(filter); //1. load menus String roleId, menuId; Role role = null; for (Entity entity: entitySet) { roleId = entity.getString("role_id"); menuId = entity.getString("menu_id"); if (roleBucket != null) { RoleAgent agent = roleBucket.get(roleId); if (agent != null) { role = agent.get(); } else { logger.error("load role menu error, role agent not exists: {}", roleId); continue; } } Menu menu = menuTree.get(menuId); role.loadOneMenu(menu); } } private static void loadOneRoleMenus(Role role, MenuTree menuTree) throws Exception { DataObject dataObject = DataObject.getInstance("sys_right_role_menu"); Filter filter = new Filter("is_active", "T").add("role_id", role.getId()); EntitySet entitySet = dataObject.getTableEntitySet(filter); for (Entity entity: entitySet) { String menuId = entity.getString("menu_id"); Menu menu = menuTree.get(menuId); role.loadOneMenu(menu); } } protected void loadSight(LoadMonitor monitor) throws Exception { DataObject dataObject; EntitySet entitySet; //1. register or get bucket SightBucket sightBucket = monitor.register(SightBucket.class); //2. load free dataObject = DataObject.getInstance("sys_right_sight_free_table"); entitySet = dataObject.getTableEntitySet(); for (Entity entity: entitySet) { String tableName = entity.getString("table_name"); sightBucket.loadOneFreeTable(tableName); } //3. load sight dataObject = DataObject.getInstance("sys_right_sight"); entitySet = dataObject.getTableEntitySet(); for (Entity entity: entitySet) { SightAgent sight = new SightAgent(); sight.load(entity); sightBucket.loadOne(sight.getId(), sight); } //4. load line limit loadSightLineLimits(sightBucket, null); } private static void loadSightLineLimits(SightBucket sightBucket, Sight sight) throws Exception { DataObject dataObject = DataObject.getInstance("sys_right_sight_line"); String filter = (sight == null) ? "1=1" : "parent_id = " + Util.quotedStr(sight.getId()); EntitySet entitySet = dataObject.getTableEntitySet(new Filter(filter), new OrderBy("parent_id")); for (Entity entity: entitySet) { String sightId = entity.getString("parent_id"); if (sightBucket != null) { SightAgent agent = sightBucket.getCachedOne(sightId); if (agent == null) { continue; } sight = agent.get(); } if (sight == null) { logger.error("role not exists: {}", sightId); continue; } LineLimit lineLimit = new LineLimit(); lineLimit.load(entity); sight.loadOneLimit(lineLimit); } } protected void loadCapacity(LoadMonitor monitor) throws Exception { DataObject dataObject; EntitySet entitySet; //1. register or get bucket CapacityBucket capacityBucket = monitor.register(CapacityBucket.class); SightBucket sightBucket = monitor.get(SightBucket.class); RoleBucket roleBucket = monitor.get(RoleBucket.class); //2. load dataObject = DataObject.getInstance("sys_right_capacity"); entitySet = dataObject.getTableEntitySet(); for (Entity entity: entitySet) { //2.1 capacity Capacity capacity = new Capacity(); capacity.load(entity); capacityBucket.loadOne(capacity.getId(), capacity); //2.2 role String roleId = capacity.getRoleId(); RoleAgent role = roleBucket.get(roleId); if (role == null) { logger.error("load capacity role error, role not exists: {}", roleId); continue; } capacity.setRole(role); //2.3 sight String sightId = capacity.getSightId(); SightAgent sight = sightBucket.get(sightId); if (sight == null) { logger.error("load capacity sight error, sight not exists: {}", sightId); continue; } capacity.setSight(sight); } } private void loadActorCapacity(LoadMonitor monitor) throws Exception { CapacityBucket capacityBucket = monitor.get(CapacityBucket.class); ActorBucket actorBucket = monitor.get(ActorBucket.class); //1. 从数据库加载Capacity DataObject dataObject = DataObject.getInstance("sys_right_actor_capacity"); EntitySet entitySet = dataObject.getTableEntitySet(new OrderBy("actor_id")); for (Entity entity: entitySet) { String actorId = entity.getString("actor_id"); Actor actor = actorBucket.get(actorId); if (actor == null) { logger.error("load actor capacity error, actor not exists: {}", actorId); continue; } String capacityId = entity.getString("capacity_id"); Capacity capacity = capacityBucket.get(capacityId); if (capacity == null) { logger.error("load actor capacity error, capacity not exists: {}", capacityId); continue; } capacity.setActorId(actorId); actor.loadOneCapacity(capacity); } //2. 合并每个角色的Capacity(一个角色可能有多个Capacity) for (Actor actor: actorBucket) { actor.buildCapacityRuntime(); } } @Override public void shutDown() throws Exception { } }