hefeixia
2021-02-18 5b8c95c760840f09910730943b21391e47187315
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
package chat.module;
 
import java.sql.Connection;
import java.util.concurrent.Executors;
 
import org.apache.log4j.Logger;
 
import chat.message.MessageRunner;
import chat.message.MessagesPublisher;
import chat.module.entity.ChatSpaceType;
import chat.module.entity.FriendShip;
import chat.module.entity.GroupType;
import chat.module.entity.MessageContainer;
import chat.module.friendcircle.FriendCircle;
import chat.user.ClientStore;
import chat.user.NotifyStore;
import chat.user.User;
import chat.user.UserStore;
import chat.util.DBUtil;
import frame.object.data.Entity;
import frame.object.data.EntitySet;
import frame.persist.NamedSQL;
import frame.persist.SQLRunner;
 
public class ModuleLoader {
    
    private static Logger logger;
    private static PrivateFriendBucket privateFriendBucket;
    private static String cachedSpaceId;
    private static MessageContainer cachedContainer;
    private static String cachedUserId;
    private static User cachedUser;
    public static Connection connDB;
    private static MessageRunner imBusinessScheduler;
    private static MessagesPublisher messagePublisher;
    
    static {
        logger = Logger.getLogger(ModuleLoader.class);
        
        privateFriendBucket = PrivateFriendBucket.getInstance();
    }
 
    public static PrivateFriendBucket getPrivateFriendBucket() {
        return privateFriendBucket;
    }
 
    public static void setPrivateFriendBucket(PrivateFriendBucket privateFriendBucket) {
        ModuleLoader.privateFriendBucket = privateFriendBucket;
    }
 
    public static void load() throws Exception {
        //1.
        loadUser();
        loadClient();
        loadNotify();
        
        //2.群组
        loadChatSpace();
        
        //3.群组成员
        loadMember();
        
        //4.群组消息 
        loadMessage();
        
        //5. 群组消息关系
        loadMessageRelation();
        
        //6.加好友关系
        LoadFriendShip();
        
        //7.加朋友圈
        LoadFriendCircle();
        
        //8.加数据库连接
        connDB = LoadDBConn();
        
        //9.加载执行线程池
        int threadNum = Runtime.getRuntime().availableProcessors() * 2;
        imBusinessScheduler = new MessageRunner(Executors.newScheduledThreadPool(threadNum));    
        
        //10.实例化消息发送
        messagePublisher = new MessagesPublisher();
    }
    
    private static Connection LoadDBConn() throws Exception {
        DBUtil dbUtil = new DBUtil();
        Connection conn = dbUtil.getConnection();
        return conn;
    }
 
    private static void loadUser() throws Exception {
        NamedSQL namedSQL = NamedSQL.getInstance("getEntitySet");
        namedSQL.setTableName("usr");
        EntitySet entitySet = SQLRunner.getEntitySet(namedSQL);
        
        int cnt = 0;
        for (Entity entity: entitySet) {
             UserStore.loadOne(entity);
             cnt++;
        }
        
        if (logger.isDebugEnabled()) {
            logger.debug("load user: " + cnt);
        }
    }
 
    private static void loadClient() throws Exception {
        NamedSQL namedSQL = NamedSQL.getInstance("getEntitySet");
        namedSQL.setTableName("client");
        EntitySet entitySet = SQLRunner.getEntitySet(namedSQL);
        
        int cnt = 0;
        for (Entity entity: entitySet) {
             ClientStore.loadOne(entity);
             cnt++;
        }
        
        if (logger.isDebugEnabled()) {
            logger.debug("load client: " + cnt);
        }        
    }
    
    private static void loadNotify() throws Exception {
        NamedSQL namedSQL = NamedSQL.getInstance("getEntitySet");
        namedSQL.setTableName("notify");
        namedSQL.setFilter("isread = 'F' ");
        
        EntitySet entitySet = SQLRunner.getEntitySet(namedSQL);
        
        int cnt = 0;
        for (Entity entity: entitySet) {
             NotifyStore.loadOne(entity);
             cnt++;
        }
        
        if (logger.isDebugEnabled()) {
            logger.debug("load client: " + cnt);
        }
    }
 
    private static void loadChatSpace() throws Exception {
        int cnt = 0; 
        ChatSpaceType spaceType;
        
        NamedSQL namedSQL = NamedSQL.getInstance("getEntitySet");
        namedSQL.setTableName("chatSpace");
        EntitySet entitySet = SQLRunner.getEntitySet(namedSQL);
        
        for (Entity entity: entitySet) {
            spaceType = ChatSpaceType.pase(entity.getString("typeCode"));
            
            if (ChatSpaceType.Private == spaceType) {//私聊
                privateFriendBucket.load(entity);                
            }
            else if (ChatSpaceType.GroupDoctor == spaceType) {//医生群
                GroupBucket.load(GroupType.Doctor, entity);
            }
            else if (ChatSpaceType.GroupPatient == spaceType) {//患者群
                GroupBucket.load(GroupType.Patient, entity);
            }
            else if (ChatSpaceType.GroupFriend == spaceType) {//朋友群
                GroupBucket.load(GroupType.Friend, entity);
            }
            else if (ChatSpaceType.Assistant == spaceType) {//医助
                
            }
            else if (ChatSpaceType.Channel == spaceType) {//频道
                
            }
            else if (ChatSpaceType.ChatRoom == spaceType) {//聊天室
                
            }
            else if (ChatSpaceType.FriendCircle == spaceType) {//朋友圈
                
            }
 
            cnt++;
        }
        
        if (logger.isDebugEnabled()) {
            logger.debug("load chat space: " + cnt);
        }    
    }
    
    private static void loadMember() throws Exception {
        //1.私聊
        doLoadMember(ChatSpaceType.Private, privateFriendBucket);
        //2.医生群组
        doLoadMember(ChatSpaceType.GroupDoctor, GroupBucket.getInstance(GroupType.Doctor));
        //3.患者群组
        doLoadMember(ChatSpaceType.GroupPatient, GroupBucket.getInstance(GroupType.Patient));
        //4.朋友群组
        doLoadMember(ChatSpaceType.GroupFriend, GroupBucket.getInstance(GroupType.Friend));
    }
    
    private static void doLoadMember(ChatSpaceType spaceType, Bucket<?> spaceBucket) throws Exception {
        String spaceId; 
        MessageContainer messageContainer;
        
        NamedSQL namedSQL = NamedSQL.getInstance("getEntitySet");
        namedSQL.setTableName("member").setOrderBy("spaceid");
        namedSQL.setFilter("spacecode = '" + spaceType + "' ");
        EntitySet entitySet = SQLRunner.getEntitySet(namedSQL);
        
        for (Entity entity: entitySet) {
             spaceId = entity.getString("spaceid");
             messageContainer = getMessageContainer(spaceBucket, spaceId);
             messageContainer.loadOneMember(entity);
        }
    }
 
    private static void loadMessage() throws Exception {
        doLoadMessage(ChatSpaceType.Private, privateFriendBucket);
        doLoadMessage(ChatSpaceType.GroupDoctor, GroupBucket.getInstance(GroupType.Doctor));
        doLoadMessage(ChatSpaceType.GroupPatient, GroupBucket.getInstance(GroupType.Patient));
        doLoadMessage(ChatSpaceType.GroupFriend, GroupBucket.getInstance(GroupType.Friend));
    }
    
    private static void doLoadMessage(ChatSpaceType spaceType, Bucket<?> spaceBucket) throws Exception {
        String spaceId; 
        MessageContainer messageContainer;
        
        NamedSQL namedSQL = NamedSQL.getInstance("getEntitySet");
        namedSQL.setTableName("message").setOrderBy("spaceid, createtime asc");
        namedSQL.setFilter("TO_DAYS(NOW()) - TO_DAYS(createtime) >= 0 and spacecode = '" + spaceType + "' ");
        EntitySet entitySet = SQLRunner.getEntitySet(namedSQL);
        
        for (Entity entity: entitySet) {
             spaceId = entity.getString("spaceid");
             messageContainer = getMessageContainer(spaceBucket, spaceId);
             messageContainer.loadOneMessage(entity);
        }
    }
 
    //消息关系  Private, GroupDoctor, GroupPatient, GroupFriend, Assistant, Channel, ChatRoom, FriendCircle, Unknown;
    private static void loadMessageRelation() throws Exception {
        //1.私聊
        doLoadMessageRelation(ChatSpaceType.Private, privateFriendBucket);
        //2.医生群组
        doLoadMessageRelation(ChatSpaceType.GroupDoctor, GroupBucket.getInstance(GroupType.Doctor));
        //3.患者群组
        doLoadMessageRelation(ChatSpaceType.GroupPatient, GroupBucket.getInstance(GroupType.Patient));
        //4.朋友群组
        doLoadMessageRelation(ChatSpaceType.GroupFriend, GroupBucket.getInstance(GroupType.Friend));        
    }
    
    private static void doLoadMessageRelation(ChatSpaceType spaceType, Bucket<?> spaceBucket) throws Exception {
        String spaceId;
        MessageContainer messageContainer;
        
        NamedSQL namedSQL = NamedSQL.getInstance("getEntitySet");
        namedSQL.setTableName("messagerelation").setOrderBy("spaceid, receivetime asc");
        namedSQL.setFilter("TO_DAYS(NOW()) - TO_DAYS(receivetime) >=0 and spacecode = '" + spaceType + "' ");
        EntitySet entitySet = SQLRunner.getEntitySet(namedSQL);
        
        for (Entity entity : entitySet) {
             spaceId = entity.getString("spaceid");
             
            if (spaceId == null) {
                continue;
            }
                
            messageContainer = getMessageContainer(spaceBucket, spaceId);
            messageContainer.loadOneMessageRelation(entity);
        }        
    }
    
    private static void LoadFriendShip() throws Exception {
        NamedSQL namedSQL = NamedSQL.getInstance("getActiveFriendship");
        EntitySet entitySet = SQLRunner.getEntitySet(namedSQL);
        
        User user, friend; 
        FriendShip friendShip;
        
        for (Entity entity : entitySet) {
            //1. 
            user = getUser(entity.getString("userId"));
            //friend = UserStore.getById(entity.getString("friendId"));
            friend = getUser(entity.getString("friendId"));
 
            if (user == null || friend == null) {
                continue;
            }
            
            //如果,朋友关系还处于审请状态,那么,将信息加入到朋友推送list中
            if ("apply".equals(entity.getString("statuscode"))) {
                friendShip = new FriendShip();
                friendShip.load(entity);
                user.addOnePushFriendShip(user.getId(), friendShip);
                continue;
            }
 
            friendShip = new FriendShip();
            friendShip.load(entity);
            //user.addOneFrieldShip(friend.getId(), friendShip);
            user.addOneFrieldShip(user.getId(), friendShip);
            
            friendShip = friendShip.getReverseInstance();
            friend.addOneFrieldShip(friendShip.getUserid(), friendShip);
        }    
    }
    
    private static void LoadFriendCircle() throws Exception {
        NamedSQL namedSQL = NamedSQL.getInstance("getEntitySet");
        namedSQL.setTableName("friendcircle").setOrderBy("senderid, createtime asc");
        EntitySet entitySet = SQLRunner.getEntitySet(namedSQL);
        
        String userId; User user; 
        FriendCircle friendCircle;
        
        for (Entity entity : entitySet) {
            userId = entity.getString("senderid");
            user = UserStore.getCachedById(userId);
            
            if (user == null) {
                continue;
            }
            
            friendCircle = user.getFriendCircle();
            friendCircle.loadOneCricleObject(entity);
        }                
    }    
    
    private static MessageContainer getMessageContainer(Bucket<?> spaceBucket, String spaceId) {
        if (spaceId.equals(cachedSpaceId)) {
            return cachedContainer;
        }
 
        cachedSpaceId = spaceId;
        cachedContainer = spaceBucket.getOne(cachedSpaceId);
        return cachedContainer;
    }
    
    private static User getUser(String userId) {
        if (userId.equals(cachedUserId)) {
            return cachedUser;
        }
 
        cachedUserId = userId;
        cachedUser = UserStore.getById(cachedUserId);
        return cachedUser;
    }
 
    public static MessageRunner getImBusinessScheduler() {
        return imBusinessScheduler;
    }
 
    public static void setImBusinessScheduler(MessageRunner imBusinessScheduler) {
        ModuleLoader.imBusinessScheduler = imBusinessScheduler;
    }
 
    public static MessagesPublisher getMessagePublisher() {
        return messagePublisher;
    }
 
    public static void setMessagePublisher(MessagesPublisher messagePublisher) {
        ModuleLoader.messagePublisher = messagePublisher;
    }
 
}