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;
|
}
|
|
}
|