365 lines
11 KiB
Java
365 lines
11 KiB
Java
package Manager;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
|
|
import Utility.User.UserPermission;
|
|
import Utility.chat.Chat;
|
|
import Utility.chat.ChatCategory;
|
|
import Utility.jsonUtils;
|
|
import org.json.simple.JSONArray;
|
|
import org.json.simple.JSONObject;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
public class ChatManager {
|
|
|
|
private static Logger log = LoggerFactory.getLogger(ChatManager.class);
|
|
|
|
private static final String CHATS_FILENAME = "chats.json";
|
|
|
|
/**
|
|
* Load All chats to look ak
|
|
* @return list of Chats
|
|
*/
|
|
public static Set<Long> loadWatch(){
|
|
log.debug("Load WhatchChats");
|
|
|
|
Set<Long> ret = new HashSet<>();
|
|
for (Object o : jsonUtils.loadJson("WatchChats.json")){
|
|
JSONObject x = (JSONObject) o;
|
|
|
|
ret.add(Long.parseLong(x.get("ID").toString()));
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* Safe list of chats to look at
|
|
* @param list list of chats to save
|
|
*/
|
|
public static void saveWatch(Set<Long> list){
|
|
log.debug("Save WhatchChats");
|
|
|
|
JSONArray save = new JSONArray();
|
|
|
|
for (long x : list){
|
|
JSONObject tmp = new JSONObject();
|
|
tmp.put("ID", Long.toString(x));
|
|
save.add(tmp);
|
|
}
|
|
|
|
jsonUtils.saveJson(save, "WatchChats.json");
|
|
}
|
|
|
|
/**
|
|
* Add a new Chat Category
|
|
* @param category new Category
|
|
*/
|
|
public static void addCategory(ChatCategory category) {
|
|
log.info("Adding Utility.chat category " + category.getName());
|
|
|
|
JSONArray categories = jsonUtils.loadJson(CHATS_FILENAME);
|
|
|
|
//Check if the uid is actually unique
|
|
if (checkCategoryUID(category.getUid())) {
|
|
log.debug("no unique Chat ID");
|
|
return;
|
|
}
|
|
|
|
JSONObject newCategory = new JSONObject();
|
|
newCategory.put("uid", category.getUid());
|
|
newCategory.put("name", category.getName());
|
|
newCategory.put("chats", new JSONArray());
|
|
|
|
categories.add(newCategory);
|
|
|
|
jsonUtils.saveJson(categories, CHATS_FILENAME);
|
|
}
|
|
|
|
/**
|
|
* Load a List with all Chat Categories
|
|
* @return List of ChatCategories
|
|
*/
|
|
public static List<ChatCategory> loadCategories() {
|
|
log.debug("Loading chat categories from file");
|
|
|
|
JSONArray categoriesJson = jsonUtils.loadJson(CHATS_FILENAME);
|
|
List<ChatCategory> categories = new ArrayList<>();
|
|
|
|
for (Object o : categoriesJson) {
|
|
JSONObject category = (JSONObject) o;
|
|
long uid = (long) category.get("uid");
|
|
|
|
categories.add(loadCategory(uid));
|
|
}
|
|
|
|
return categories;
|
|
}
|
|
|
|
/**
|
|
* Load singe Chat Category by ID
|
|
* @param uid id of Categoriy
|
|
* @return ChatCategory
|
|
*/
|
|
public static ChatCategory loadCategory(long uid) {
|
|
log.debug("Loading chat category by uid " + uid);
|
|
JSONArray categories = jsonUtils.loadJson(CHATS_FILENAME);
|
|
|
|
for (Object o : categories) {
|
|
JSONObject category = (JSONObject) o;
|
|
long searchUid = (long) category.get("uid");
|
|
|
|
if (searchUid == uid) {
|
|
String name = (String) category.get("name");
|
|
JSONArray chatsJson = (JSONArray) category.get("chats");
|
|
List<Chat> chats = new ArrayList<>();
|
|
|
|
for (Object c : chatsJson) {
|
|
JSONObject chatJson = (JSONObject) c;
|
|
long chatUid = (long) chatJson.get("uid");
|
|
String chatName = (String) chatJson.get("name");
|
|
String chatUrl = (String) chatJson.get("url");
|
|
String permissionJson = (String) chatJson.get("permissionNeeded");
|
|
UserPermission permissionNeeded = UserPermission.valueOf(permissionJson);
|
|
|
|
chats.add(new Chat(chatUid, chatName, chatUrl, permissionNeeded));
|
|
}
|
|
|
|
ChatCategory found = new ChatCategory(uid, name);
|
|
found.setChatList(chats);
|
|
|
|
return found;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Delete existing Category
|
|
* @param uid id of Category to delete
|
|
*/
|
|
public static void deleteCategory(long uid) {
|
|
log.info("delete Category: " + uid);
|
|
|
|
JSONArray categories = jsonUtils.loadJson(CHATS_FILENAME);
|
|
|
|
for (Object o : categories) {
|
|
JSONObject category = (JSONObject)o;
|
|
long searchUid = (Long) category.get("uid");
|
|
|
|
if (searchUid == uid) {
|
|
categories.remove(o);
|
|
break;
|
|
}
|
|
}
|
|
|
|
jsonUtils.saveJson(categories, CHATS_FILENAME);
|
|
}
|
|
|
|
/**
|
|
* Check if a ChatCategory uid already exists
|
|
* @param uid ID to check
|
|
* @return boolean true if exists
|
|
*/
|
|
public static boolean checkCategoryUID(long uid) {
|
|
log.debug("Checking if Utility.chat category " + uid + " already exists");
|
|
JSONArray categories = jsonUtils.loadJson(CHATS_FILENAME);
|
|
|
|
for (Object o : categories) {
|
|
JSONObject category = (JSONObject) o;
|
|
long searchUid = (long) category.get("uid");
|
|
|
|
if (uid == searchUid) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Check if a Chat uid already exists
|
|
* @param uid ID to check
|
|
* @return boolean true if exists
|
|
*/
|
|
public static boolean checkChatUID(long uid) {
|
|
JSONArray categories = jsonUtils.loadJson(CHATS_FILENAME);
|
|
|
|
for (Object o : categories) {
|
|
JSONObject category = (JSONObject)o;
|
|
JSONArray chats = (JSONArray)category.get("chats");
|
|
|
|
for (Object c : chats) {
|
|
JSONObject chat = (JSONObject) c;
|
|
long chatUid = (long) chat.get("uid");
|
|
|
|
if (uid == chatUid)
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Get biggest chatCategory Uid
|
|
* @return Greatest UID
|
|
*/
|
|
public static long getBiggestChatCategoryUid() {
|
|
log.debug("Get biggest Utility.chat category uid");
|
|
JSONArray categories = jsonUtils.loadJson(CHATS_FILENAME);
|
|
long biggestUid = 0;
|
|
|
|
for (Object o : categories) {
|
|
JSONObject category = (JSONObject) o;
|
|
long uid = (long) category.get("uid");
|
|
if (uid > biggestUid) {
|
|
biggestUid = uid;
|
|
}
|
|
}
|
|
|
|
return biggestUid;
|
|
}
|
|
|
|
/**
|
|
* Save Chat into Category
|
|
* @param chat Chat to save
|
|
* @param categoryUid Category to save chat into
|
|
*/
|
|
public static void saveChat(Chat chat, long categoryUid)
|
|
{
|
|
JSONArray categories = jsonUtils.loadJson(CHATS_FILENAME);
|
|
|
|
//search for Category
|
|
for (Object o : categories) {
|
|
JSONObject category = (JSONObject)o;
|
|
long searchCategoryUid = (Long) category.get("uid");
|
|
|
|
//If found
|
|
if (searchCategoryUid == categoryUid) {
|
|
|
|
JSONArray chats = (JSONArray)category.get("chats");
|
|
|
|
//search if chat already exist
|
|
for (Object c : chats) {
|
|
JSONObject searchChat = (JSONObject)c;
|
|
long searchChatUid = (Long) searchChat.get("uid");
|
|
|
|
//if chat already exists, delete it
|
|
if (searchChatUid == chat.getUid()) {
|
|
chats.remove(c);
|
|
break;
|
|
}
|
|
}
|
|
|
|
//save chat
|
|
JSONObject chatJson = new JSONObject();
|
|
chatJson.put("uid", chat.getUid());
|
|
chatJson.put("name", chat.getName());
|
|
chatJson.put("url", chat.getUrl());
|
|
chatJson.put("permissionNeeded", chat.getPermissionNeeded().toString());
|
|
chats.add(chatJson);
|
|
}
|
|
}
|
|
jsonUtils.saveJson(categories, CHATS_FILENAME);
|
|
}
|
|
|
|
/**
|
|
* Loads specific chat out of specific category
|
|
* @param chatUid chat id to load
|
|
* @param categoryUid category id where chat is located
|
|
* @return Chat
|
|
*/
|
|
public static Chat loadChatFromCategory(long chatUid, long categoryUid)
|
|
{
|
|
JSONArray categories = jsonUtils.loadJson(CHATS_FILENAME);
|
|
|
|
for (Object o : categories) {
|
|
JSONObject category = (JSONObject)o;
|
|
long searchCategoryUid = (Long) category.get("uid");
|
|
|
|
if (searchCategoryUid == categoryUid) {
|
|
JSONArray chats = (JSONArray)category.get("chats");
|
|
|
|
for (Object c : chats) {
|
|
JSONObject chat = (JSONObject)c;
|
|
long searchChatUid = (Long) chat.get("uid");
|
|
|
|
if (searchChatUid == chatUid) {
|
|
String name = (String)chat.get("name");
|
|
String url = (String)chat.get("url");
|
|
String permissionString = (String)chat.get("permissionNeeded");
|
|
UserPermission permissionNeeded = UserPermission.valueOf(permissionString);
|
|
|
|
return new Chat(chatUid, name, url, permissionNeeded);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Delete Chat
|
|
* @param chatUid ID of chat to delete
|
|
*/
|
|
public static void deleteChat(long chatUid)
|
|
{
|
|
JSONArray categories = jsonUtils.loadJson(CHATS_FILENAME);
|
|
|
|
for (Object o : categories) {
|
|
JSONObject category = (JSONObject)o;
|
|
JSONArray chats = (JSONArray)category.get("chats");
|
|
|
|
for (Object c : chats) {
|
|
JSONObject searchChat = (JSONObject)c;
|
|
long searchChatUid = (Long) searchChat.get("uid");
|
|
|
|
if (searchChatUid == chatUid) {
|
|
chats.remove(c);
|
|
break;
|
|
}
|
|
}
|
|
category.put("chats", chats);
|
|
}
|
|
jsonUtils.saveJson(categories, CHATS_FILENAME);
|
|
}
|
|
|
|
/**
|
|
* Change UID of a Chat
|
|
* @param chat new Chat
|
|
* @param categoryUid Category ID where chat is located
|
|
* @param oldUid ID of Chat to change
|
|
*/
|
|
public static void changeUid(Chat chat, long categoryUid, long oldUid)
|
|
{
|
|
JSONArray categories = jsonUtils.loadJson(CHATS_FILENAME);
|
|
|
|
for (Object o : categories) {
|
|
|
|
JSONObject category = (JSONObject)o;
|
|
|
|
long searchCategoryUid = (Long) category.get("uid");
|
|
|
|
if (searchCategoryUid == categoryUid) {
|
|
JSONArray chats = (JSONArray)category.get("chats");
|
|
|
|
for (Object c : chats) {
|
|
|
|
JSONObject searchChat = (JSONObject)c;
|
|
long searchChatUid = (Long) searchChat.get("uid");
|
|
|
|
if (searchChatUid == oldUid) {
|
|
searchChat.put("uid", chat.getUid());
|
|
searchChat.put("name", chat.getName());
|
|
searchChat.put("url", chat.getUrl());
|
|
searchChat.put("permissionNeeded", chat.getPermissionNeeded().toString());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
jsonUtils.saveJson(categories, CHATS_FILENAME);
|
|
}
|
|
}
|