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
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.persist.NamedSQL;
 
public class NotifyStore {
 
    private static Map<String, Notify> notifyMap;
    
    static {
        init();
    }
    
    private static void init() {
        notifyMap = new ConcurrentHashMap<String, Notify>();
    }    
 
    public static void loadOne(Entity entity) {
        String id = entity.getString("id");
        
        Notify notify = new Notify(id);
        notify.load(entity);
        
        addOne(notify);
    }
    
    public static void addOne(Notify notify) {
        if (notify == null) {
            return;
        }
        
        notifyMap.put(notify.getId(), notify);
    }
    
    public static Notify getOrCreate(DataPool dataPool) throws Exception {
        String id = dataPool.getString("id");
        
        //1. get from cache
        Notify notifyResult = get(id);
        
        //2. get from database
        if (notifyResult == null) {
            notifyResult = loadOneNotify(id);
        }
        
        //3. create new client
        if (notifyResult == null) {
            notifyResult = new Notify(id);
            notifyResult.load(dataPool);
            
            notifyResult = createOneNotify(notifyResult);
        }
        
        updateOneNotify(notifyResult);
        
        return notifyResult;
    }
    
    public static Notify get(String notifyId) {
        if (notifyId == null) {
            return null;
        }
        
        return notifyMap.get(notifyId);
    }
    
    public static boolean contains(String notifyId) {
        if (notifyId == null) {
            return false;
        }
        
        return notifyMap.containsKey(notifyId);
    }
    
    private static Notify loadOneNotify(String notifyId) throws Exception {
        NamedSQL namedSQL = NamedSQL.getInstance("getOneNotify");
        namedSQL.setParam("id", notifyId);
        Entity entity = namedSQL.getEntity();
        
        if (entity == null) {
            return null;
        }
        
        Notify notify = new Notify(notifyId);
        notify.load(entity);
        
        return notify;
    }
 
    private static Notify createOneNotify(Notify notify) throws Exception {
        DataObject dataObject = DataObject.getInstance("notify");
        
        Entity entity = dataObject.newEntity();
        notify.pushTo(entity);
            
        dataObject.insertToDataBase(entity);
        return notify;
    }
    
    private static Notify updateOneNotify(Notify notify) throws Exception {
        DataObject dataObject = DataObject.getInstance("notify");
        
        Entity entity = dataObject.newEntity();
        notify.pushTo(entity);
            
        dataObject.updateToDataBase(entity);
        return notify;
    }
 
}