P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
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 {
        
    }    
 
}