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
package chat.user;
 
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
import chat.server.im.DataPool;
import frame.object.data.DataObject;
import frame.object.data.Entity;
import frame.object.data.ID;
import frame.persist.NamedSQL;
 
public class ClientStore {
 
    private static Map<String, Client> clientMap;
    
    static {
        init();
    }
    
    private static void init() {
        clientMap = new ConcurrentHashMap<String, Client>();
    }    
 
    public static void loadOne(Entity entity) {
        String id = entity.getString("id");
        
        Client client = new Client(id);
        client.load(entity);
        
        addOne(client);
    }
    
    public static void addOne(Client client) {
        if (client == null) {
            return;
        }
        
        String userId = client.getUserId();
        User user = UserStore.getById(userId);
        
        //client.setUser(user);
        
        clientMap.put(client.getId(), client);
    }
    
    public static Client getOrCreate(DataPool dataPool) throws Exception {
        String id = dataPool.getString("clientId");
        
        //1. get from cache
        Client clientResult = get(id);
        
        //2. get from database
        if (clientResult == null) {
            clientResult = loadOneClient(id);
        }
        
        //3. create new client
        if (clientResult == null) {
            clientResult = new Client(id);
            clientResult.load(dataPool);
            
            clientResult = createOneClient(clientResult);
        }
        
        //4. user has loaded
        User user = clientResult.getUser();
        
        if (user != null) {
            return clientResult;
        }
        
        //5. user not load
        String userId = clientResult.getUserId();
        
        if (userId != null) {
            user = UserStore.getById(userId);
            clientResult.setUser(user);
        }
        
        if (user != null) {
            return clientResult;
        }
        
        //userId = ID.newValue();
        //6. new user
        user = new User(null);
        user.load(dataPool);
        user = UserStore.getOrCreate(user);
        
        clientResult.setUser(user);
        updateOneClient(clientResult);
        
        return clientResult;
    }
    
    public static Client get(String clientId) {
        if (clientId == null) {
            return null;
        }
        
        return clientMap.get(clientId);
    }
    
    public static boolean contains(String clientId) {
        if (clientId == null) {
            return false;
        }
        
        return clientMap.containsKey(clientId);
    }
    
    private static Client loadOneClient(String clientId) throws Exception {
        NamedSQL namedSQL = NamedSQL.getInstance("getOneClient");
        namedSQL.setParam("id", clientId);
        Entity entity = namedSQL.getEntity();
        
        if (entity == null) {
            return null;
        }
        
        Client client = new Client(clientId);
        client.load(entity);
        
        return client;
    }
 
    private static Client createOneClient(Client client) throws Exception {
        DataObject dataObject = DataObject.getInstance("client");
        
        Entity entity = dataObject.newEntity();
        client.pushTo(entity);
            
        dataObject.insertToDataBase(entity);
        return client;
    }
    
    private static Client updateOneClient(Client client) throws Exception {
        DataObject dataObject = DataObject.getInstance("client");
        
        Entity entity = dataObject.newEntity();
        client.pushTo(entity);
            
        dataObject.updateToDataBase(entity);
        return client;
    }
 
}