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