package kr.co.i4way.genesys.controller;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.genesyslab.platform.applicationblocks.com.objects.CfgAgentLogin;
import com.genesyslab.platform.applicationblocks.com.objects.CfgAgentLoginInfo;
import com.genesyslab.platform.applicationblocks.com.objects.CfgFolder;
import com.genesyslab.platform.applicationblocks.com.objects.CfgObjectID;
import com.genesyslab.platform.applicationblocks.com.objects.CfgPerson;
import com.genesyslab.platform.applicationblocks.com.objects.CfgSkillLevel;
import com.genesyslab.platform.commons.collections.KeyValueCollection;
import com.genesyslab.platform.commons.collections.KeyValuePair;
import com.genesyslab.platform.commons.protocol.ChannelState;
import com.genesyslab.platform.configuration.protocol.types.CfgFlag;

import kr.co.i4way.genesys.cfgserver.ConfigAgentLogin;
import kr.co.i4way.genesys.cfgserver.ConfigFolder;
import kr.co.i4way.genesys.cfgserver.ConfigPerson;
import kr.co.i4way.genesys.cfgserver.initConfigService;
import kr.co.i4way.genesys.model.GenesysInfoVo;
import kr.co.i4way.genesys.model.QueryVo;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;

@RestController
@CrossOrigin(origins = "*")
public class PersonController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
	@Autowired
	private GenesysInfoVo genesysinfovo;
	initConfigService initconfigservice = initConfigService.getInstance();
	ConfigPerson cfgperson = null;
	ConfigFolder cfgfolder = null;
	ConfigAgentLogin cfgagentlogin = null;

	@PostMapping("/person/getPersonInfo")
	public JSONObject getPersonInfo(@RequestBody QueryVo vo) throws Exception {
		CfgPerson person = null;
		JSONObject rtnobj = new JSONObject();
		Map<String, Object> map = new HashMap<>();
		String rtnStr = "";

		try {
			if (initconfigservice.openConfigService(genesysinfovo) == ChannelState.Opened) {
				cfgperson = new ConfigPerson();
				person = cfgperson.getPersonInfo(initconfigservice.service, genesysinfovo.getCfg_tenant_dbid(),
						vo.getQry_type(), vo.getQry_str());
				rtnobj = convertPersonInfo_jsonObj(person);
				// logger.info(map.toString());
			}
			rtnobj.put("command", "getPersonInfo");
		} catch (Exception e) {
			logger.error("Exception", e);
		} finally {
		}
		return rtnobj;
	}

	private JSONObject getPersonInfo(int dbid) throws Exception {
		CfgPerson person = null;
		JSONObject rtnobj = new JSONObject();

		try {
			if (initconfigservice.openConfigService(genesysinfovo) == ChannelState.Opened) {
				cfgperson = new ConfigPerson();
				person = cfgperson.getPersonInfo_single(initconfigservice.service, genesysinfovo.getCfg_tenant_dbid(),
						dbid);
				rtnobj = convertPersonInfo_jsonObj(person);
				// logger.info(map.toString());
			}
			rtnobj.put("command", "getPersonInfo");
		} catch (Exception e) {
			logger.error("Exception", e);
		} finally {
		}
		return rtnobj;
	}

	private JSONObject getFolderInfo(int owner_type, int dbid) throws Exception {
		CfgFolder folder = null;
		JSONObject rtnobj = new JSONObject();

		try {
			if (initconfigservice.openConfigService(genesysinfovo) == ChannelState.Opened) {
				cfgfolder = new ConfigFolder();
				folder = cfgfolder.getFolderInfo(initconfigservice.service, owner_type, dbid);
				rtnobj = convertFolderInfo_jsonObj(folder);
				// logger.info(map.toString());
			}
			rtnobj.put("command", "getFolderInfo");
		} catch (Exception e) {
			logger.error("Exception", e);
		} finally {

		}
		return rtnobj;
	}

	/*
	 * private CfgFolder getFolderInfo(int owner_type, int dbid) throws Exception {
	 * CfgFolder folder = null;
	 * 
	 * try { if (initconfigservice.openConfigService(genesysinfovo) ==
	 * ChannelState.Opened) { cfgfolder = new ConfigFolder(); folder =
	 * cfgfolder.getFolderInfo(initconfigservice.service, owner_type, dbid); } }
	 * catch (Exception e) { logger.error("Exception", e); } finally { } return
	 * folder; }
	 */
	@RequestMapping("/person/getAllPersonInfo")
	public JSONObject getAllPersonInfo() throws Exception {
		CfgPerson person = null;
		JSONObject rtnobj = new JSONObject();
		Map<String, Object> map = new HashMap<>();
		String rtnStr = "";

		try {
			if (initconfigservice.openConfigService(genesysinfovo) == ChannelState.Opened) {
				cfgperson = new ConfigPerson();
				Collection<CfgPerson> persons = cfgperson.getPersonInfo(initconfigservice.service,
						genesysinfovo.getCfg_tenant_dbid());
				rtnobj = convertPersonInfo_jsonObj(persons);

				// map = JsonUtil.getMapFromJsonObject(rtnobj);
				// logger.info(map.toString());
			}
			rtnobj.put("command", "getAllPersonInfo");
		} catch (Exception e) {
			logger.error("Exception", e);
		} finally {
		}
		return rtnobj;
	}

	@RequestMapping("/person/getFolderInfo")
	public JSONObject getFolderInfo(@RequestBody QueryVo vo) throws Exception {
		CfgFolder folder = null;
		JSONObject rtnobj = new JSONObject();
		Map<String, Object> map = new HashMap<>();
		String rtnStr = "";

		try {
			if (initconfigservice.openConfigService(genesysinfovo) == ChannelState.Opened) {
				cfgfolder = new ConfigFolder();
				folder = cfgfolder.getFolderInfo(initconfigservice.service, vo.getFolder_type(), vo.getFolder_dbid());
				rtnobj = convertFolderInfo_jsonObj(folder);

				// map = JsonUtil.getMapFromJsonObject(rtnobj);
				// logger.info(map.toString());
			}
			rtnobj.put("command", "getAllPersonInfo");
		} catch (Exception e) {
			logger.error("Exception", e);
		} finally {
		}
		return rtnobj;
	}

	@RequestMapping("/person/checkPerson")
	public JSONObject checkPerson(HttpServletRequest request) throws Exception {
		CfgPerson person = null;
		JSONObject tmpobj = new JSONObject();
		JSONObject rtnobj = new JSONObject();
		Map<String, Object> map = new HashMap<>();
		String rtnStr = "";

		try {
			String qry_type = request.getParameter("qry_type") != null ? request.getParameter("qry_type") : "";
			String qry_str = request.getParameter("qry_str") != null ? request.getParameter("qry_str") : "";
			if (initconfigservice.openConfigService(genesysinfovo) == ChannelState.Opened) {
				cfgperson = new ConfigPerson();
				person = cfgperson.getPersonInfo(initconfigservice.service, genesysinfovo.getCfg_tenant_dbid(),
						qry_type, qry_str);
				if (person == null) {
					rtnobj.put("result", false);
				} else {
					rtnobj.put("result", true);
				}
			}
			rtnobj.put("qry_type", qry_type);
			rtnobj.put("qry_str", qry_str);
			rtnobj.put("command", "checkPerson");
		} catch (Exception e) {
		} finally {
			// initconfigservice.closeConfigService();
		}
		return rtnobj;
	}

	@RequestMapping("/person/createPerson")
	public JSONObject createPerson(HttpServletRequest request) throws Exception {
		CfgAgentLogin agentlogininfo = null;
		JSONObject rtnobj = new JSONObject();
		CfgPerson person = null;

		try {
			String employee_id = request.getParameter("employee_id") != null ? request.getParameter("employee_id") : "";
			String user_name = request.getParameter("user_name") != null ? request.getParameter("user_name") : "";
			String first_name = request.getParameter("first_name") != null ? request.getParameter("first_name") : "";
			String last_name = request.getParameter("last_name") != null ? request.getParameter("last_name") : "";
			String folder_dbid = request.getParameter("folder_dbid") != null ? request.getParameter("folder_dbid")
					: "0";
			cfgperson = new ConfigPerson();

			if (initconfigservice.openConfigService(genesysinfovo) == ChannelState.Opened) {
				person = cfgperson.getPersonInfo(initconfigservice.service, genesysinfovo.getCfg_tenant_dbid(), "emp",
						employee_id);

				if (person != null) { // 수정모드
					person = cfgperson.modifyPerson(initconfigservice.service, person, first_name, last_name,
							employee_id, user_name);
					logger.info("Modify Person~");
				} else {
					logger.info("Create Person~");
					person = cfgperson.createPerson(initconfigservice.service, genesysinfovo.getCfg_tenant_dbid(),
							genesysinfovo.getCfg_switch_dbid(), first_name, last_name, employee_id, user_name,
							Integer.parseInt(folder_dbid));
				}
				rtnobj = convertPersonInfo_jsonObj(person);
			}
			rtnobj.put("command", "createPerson");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// initconfigservice.closeConfigService();
		}
		return rtnobj;
	}

	@RequestMapping("/person/modifyPerson")
	public JSONObject modifyPerson(HttpServletRequest request) throws Exception {
		CfgAgentLogin agentlogininfo = null;
		JSONObject rtnobj = new JSONObject();
		CfgPerson person = null;

		try {
			String employee_id = request.getParameter("employee_id") != null ? request.getParameter("employee_id") : "";
			String user_name = request.getParameter("user_name") != null ? request.getParameter("user_name") : "";
			String first_name = request.getParameter("first_name") != null ? request.getParameter("first_name") : "";
			String last_name = request.getParameter("last_name") != null ? request.getParameter("last_name") : "";
			cfgperson = new ConfigPerson();

			if (initconfigservice.openConfigService(genesysinfovo) == ChannelState.Opened) {
				person = cfgperson.getPersonInfo(initconfigservice.service, genesysinfovo.getCfg_tenant_dbid(), "emp",
						employee_id);

				if (person != null) { // 수정모드
					person = cfgperson.modifyPerson(initconfigservice.service, person, first_name, last_name,
							employee_id, user_name);
					logger.info("Modify Person~");
				}
				rtnobj = convertPersonInfo_jsonObj(person);
			}
			rtnobj.put("command", "modifyPerson");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// initconfigservice.closeConfigService();
		}
		return rtnobj;
	}

	@RequestMapping("/person/deletePerson")
	public JSONObject deletePerson(HttpServletRequest request) throws Exception {
		JSONObject rtnobj = new JSONObject();
		boolean del_login_result = false;
		boolean del_person_result = false;
		CfgPerson person = null;

		try {
			String employee_id = request.getParameter("employee_id") != null ? request.getParameter("employee_id") : "";
			cfgperson = new ConfigPerson();
			cfgagentlogin = new ConfigAgentLogin();
			if (initconfigservice.openConfigService(genesysinfovo) == ChannelState.Opened) {
				person = cfgperson.getPersonInfo(initconfigservice.service, genesysinfovo.getCfg_tenant_dbid(), "emp",
						employee_id);

				if (person != null) { // 삭제
					if (person.getAgentInfo().getAgentLogins() != null) {
						Collection<CfgAgentLoginInfo> aglo = person.getAgentInfo().getAgentLogins();
						for (CfgAgentLoginInfo al : aglo) {
							cfgagentlogin.deleteAgentLogins(initconfigservice.service,
									genesysinfovo.getCfg_tenant_dbid(), genesysinfovo.getCfg_switch_dbid(),
									al.getAgentLogin().getLoginCode());
						}
					}
					logger.info("Delete Person~");
					del_person_result = cfgperson.deletePerson(initconfigservice.service, person);
				}
			}
			rtnobj.put("command", "deletePerson");
			rtnobj.put("result_delete_agentLogin", del_login_result);
			rtnobj.put("result_delete_person", del_login_result);
			rtnobj.put("employee_id", employee_id);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// initconfigservice.closeConfigService();
		}
		return rtnobj;
	}

	@RequestMapping("/person/setPersonState")
	public JSONObject setPersonState(HttpServletRequest request) throws Exception {
		JSONObject rtnobj = new JSONObject();
		CfgPerson person = null;

		try {
			String employee_id = request.getParameter("employee_id") != null ? request.getParameter("employee_id") : "";
			String state = request.getParameter("state") != null ? request.getParameter("state") : "";
			cfgperson = new ConfigPerson();
			if (initconfigservice.openConfigService(genesysinfovo) == ChannelState.Opened) {
				person = cfgperson.getPersonInfo(initconfigservice.service, genesysinfovo.getCfg_tenant_dbid(), "emp",
						employee_id);

				if (person != null) { // 삭제
					logger.info("set Person State~");
					if (state.equals("true")) {
						person = cfgperson.setPersonState(initconfigservice.service, person, true);
					} else if (state.equals("false")) {
						person = cfgperson.setPersonState(initconfigservice.service, person, false);
					} else {
					}
				}
			}
			rtnobj.put("command", "setPersonState");
			rtnobj.put("state", person.getState().name());
			rtnobj.put("employee_id", person.getEmployeeID());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// initconfigservice.closeConfigService();
		}
		return rtnobj;
	}

	@RequestMapping("/person/setAgentloginToPerson")
	public JSONObject setAgentloginToPerson(HttpServletRequest request) throws Exception {
		JSONObject rtnobj = new JSONObject();
		CfgPerson person = null;
		boolean assign_result = false;
		try {
			String employee_id = request.getParameter("employee_id") != null ? request.getParameter("employee_id") : "";
			String agent_login_id = request.getParameter("agent_login_id") != null
					? request.getParameter("agent_login_id")
					: "";
			cfgperson = new ConfigPerson();
			if (initconfigservice.openConfigService(genesysinfovo) == ChannelState.Opened) {
				person = cfgperson.getPersonInfo(initconfigservice.service, genesysinfovo.getCfg_tenant_dbid(), "emp",
						employee_id);

				if (person != null) { // person이 널이 아니면
					logger.info("set Agent Login To Person~");
					if (cfgperson.setAgentLoginToPerson(initconfigservice.service, person,
							genesysinfovo.getCfg_tenant_dbid(), genesysinfovo.getCfg_switch_dbid(), agent_login_id)) {
						assign_result = true;
					}
				}
			}
			rtnobj.put("command", "setAgentloginToPerson");
			rtnobj.put("result", assign_result);
			rtnobj.put("agent_login_id", agent_login_id);
			rtnobj.put("employee_id", person.getEmployeeID());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// initconfigservice.closeConfigService();
		}
		return rtnobj;
	}

	@RequestMapping("/person/removeAgentloginToPerson")
	public JSONObject removeAgentloginToPerson(HttpServletRequest request) throws Exception {
		JSONObject rtnobj = new JSONObject();
		CfgPerson person = null;
		boolean remove_result = false;

		try {
			String employee_id = request.getParameter("employee_id") != null ? request.getParameter("employee_id") : "";
			String agent_login_id = request.getParameter("agent_login_id") != null
					? request.getParameter("agent_login_id")
					: "";
			cfgperson = new ConfigPerson();
			if (initconfigservice.openConfigService(genesysinfovo) == ChannelState.Opened) {
				person = cfgperson.getPersonInfo(initconfigservice.service, genesysinfovo.getCfg_tenant_dbid(), "emp",
						employee_id);

				if (person != null) { // person이 널이 아니면
					logger.info("remove Agent Login To Person~");
					if (cfgperson.removeAgentLoginToPerson(initconfigservice.service, person,
							genesysinfovo.getCfg_tenant_dbid(), genesysinfovo.getCfg_switch_dbid(), agent_login_id)) {
						remove_result = true;
					}
				}
			}
			rtnobj.put("command", "removeAgentloginToPerson");
			rtnobj.put("result", remove_result);
			rtnobj.put("employee_id", person.getEmployeeID());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// initconfigservice.closeConfigService();
		}
		return rtnobj;
	}

	@RequestMapping("/person/setSkillsToPerson")
	public JSONObject setSkillToPerson(HttpServletRequest request) throws Exception {
		JSONObject rtnobj = new JSONObject();
		CfgPerson person = null;
		boolean assign_result = false;
		try {
			String employee_id = request.getParameter("employee_id") != null ? request.getParameter("employee_id") : "";
			String skill_dbids = request.getParameter("skill_dbids") != null ? request.getParameter("skill_dbids") : "";
			String skill_levels = request.getParameter("skill_levels") != null ? request.getParameter("skill_levels")
					: "";
			String[] skillArry = skill_dbids.split(",");
			String[] levelArry = skill_levels.split(",");
			cfgperson = new ConfigPerson();
			if (initconfigservice.openConfigService(genesysinfovo) == ChannelState.Opened) {
				person = cfgperson.getPersonInfo(initconfigservice.service, genesysinfovo.getCfg_tenant_dbid(), "emp",
						employee_id);

				if (person != null) { // person이 널이 아니면
					logger.info("set Skill To Person~");
					if (cfgperson.setSkillsToPerson(initconfigservice.service, person, skillArry, levelArry)) {
						assign_result = true;
					}
				}
			}
			rtnobj.put("command", "setSkillsToPerson");
			rtnobj.put("resault", assign_result);
			rtnobj.put("skill_dbids", skill_dbids);
			rtnobj.put("skill_levels", skill_levels);
			rtnobj.put("employee_id", person.getEmployeeID());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// initconfigservice.closeConfigService();
		}
		return rtnobj;
	}

	@RequestMapping("/person/removeSkillsToPerson")
	public JSONObject removeSkillsToPerson(HttpServletRequest request) throws Exception {
		JSONObject rtnobj = new JSONObject();
		CfgPerson person = null;
		boolean remove_result = false;

		try {
			String employee_id = request.getParameter("employee_id") != null ? request.getParameter("employee_id") : "";
			String skill_dbids = request.getParameter("skill_dbids") != null ? request.getParameter("skill_dbids") : "";
			String skill_levels = request.getParameter("skill_levels") != null ? request.getParameter("skill_levels")
					: "";
			String[] skillArry = skill_dbids.split(",");
			String[] levelArry = skill_levels.split(",");
			cfgperson = new ConfigPerson();
			if (initconfigservice.openConfigService(genesysinfovo) == ChannelState.Opened) {
				person = cfgperson.getPersonInfo(initconfigservice.service, genesysinfovo.getCfg_tenant_dbid(), "emp",
						employee_id);

				if (person != null) { // person이 널이 아니면
					logger.info("set Skill To Person~");
					if (cfgperson.removeSkillsToPerson(initconfigservice.service, person, skillArry, levelArry)) {
						remove_result = true;
					}
				}
			}
			rtnobj.put("command", "removeSkillsToPerson");
			rtnobj.put("resault", remove_result);
			rtnobj.put("skill_dbids", skill_dbids);
			rtnobj.put("skill_levels", skill_levels);
			rtnobj.put("employee_id", person.getEmployeeID());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// initconfigservice.closeConfigService();
		}
		return rtnobj;
	}

	private JSONObject convertPersonInfo_jsonObj(CfgPerson person) {
		JSONObject jsonObj = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		JSONObject finalJsonObj = new JSONObject();

		if (person != null) {
			if (person.getIsAgent() == CfgFlag.CFGTrue) {
				finalJsonObj.put("skills", getCfgSkillLevelCollectionInfo(person.getAgentInfo().getSkillLevels()));
				finalJsonObj.put("agentLogins", getCfgAgentInfoCollectionInfo(person.getAgentInfo().getAgentLogins()));
			}

			finalJsonObj.put("dbid", person.getDBID());
			finalJsonObj.put("firstName", person.getFirstName());
			finalJsonObj.put("lastName", person.getLastName());
			finalJsonObj.put("userName", person.getUserName());
			finalJsonObj.put("employeeId", person.getEmployeeID());
			finalJsonObj.put("isAgent", person.getIsAgent().ordinal());
			finalJsonObj.put("state", person.getState().ordinal());
			finalJsonObj.put("annex", getAnnexInfo(person.getUserProperties()));
			finalJsonObj.put("obj", "CfgPerson");
		}
		return finalJsonObj;
	}

	private JSONObject convertPersonInfo_jsonObj(Collection<CfgPerson> persons) {
		JSONObject jsonObj = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		JSONObject finalJsonObj = new JSONObject();

		if (persons != null) {
			for (CfgPerson person : persons) {
				jsonObj = new JSONObject();
				if (person.getIsAgent() == CfgFlag.CFGTrue) {
					jsonObj.put("skills", getCfgSkillLevelCollectionInfo(person.getAgentInfo().getSkillLevels()));
					jsonObj.put("agentLogins", getCfgAgentInfoCollectionInfo(person.getAgentInfo().getAgentLogins()));
				}
				jsonObj.put("dbid", person.getDBID());
				jsonObj.put("firstName", person.getFirstName());
				jsonObj.put("lastName", person.getLastName());
				jsonObj.put("userName", person.getUserName());
				jsonObj.put("employeeId", person.getEmployeeID());
				jsonObj.put("isAgent", person.getIsAgent().ordinal());
				jsonObj.put("state", person.getState().ordinal());
				jsonObj.put("annex", getAnnexInfo(person.getUserProperties()));
				jsonObj.put("obj", "CfgPerson");
				jsonArray.add(jsonObj);
			}
			finalJsonObj.put("persons", jsonArray);
		}
		return finalJsonObj;
	}

	private JSONObject getAnnexInfo(KeyValueCollection kv) {
		JSONObject jsonObj = new JSONObject();
		JSONObject jsonObj2 = new JSONObject();
		KeyValuePair tmppair;

		for (Object selectionObj : kv) {
			jsonObj = new JSONObject();
			KeyValuePair sectionKvp = (KeyValuePair) selectionObj;

			for (Object recordObj : sectionKvp.getTKVValue()) {
				tmppair = (KeyValuePair) recordObj;
				jsonObj.put(tmppair.getStringKey(), tmppair.getStringValue());
			}
			jsonObj2.put(sectionKvp.getStringKey(), jsonObj);
		}
		return jsonObj2;
	}

	private JSONArray getCfgAgentInfoCollectionInfo(Collection<CfgAgentLoginInfo> coll) {
		JSONArray rtnArray = new JSONArray();
		JSONObject jsonObj;
		for (CfgAgentLoginInfo cfgagentinfo : coll) {
			jsonObj = new JSONObject();
			jsonObj.put("dbid", cfgagentinfo.getAgentLoginDBID());
			jsonObj.put("loginCode", cfgagentinfo.getAgentLogin().getLoginCode());
			jsonObj.put("warpupTime", cfgagentinfo.getWrapupTime());
			jsonObj.put("obj", "CfgAgentLoginInfo");
			rtnArray.add(jsonObj);
		}
		return rtnArray;
	}

	private JSONArray getCfgSkillLevelCollectionInfo(Collection<CfgSkillLevel> coll) {
		JSONArray rtnArray = new JSONArray();
		JSONObject jsonObj;
		for (CfgSkillLevel cfgskilllevel : coll) {
			jsonObj = new JSONObject();
			jsonObj.put("dbid", cfgskilllevel.getSkillDBID());
			jsonObj.put("skillName", cfgskilllevel.getSkill().getName());
			jsonObj.put("level", cfgskilllevel.getLevel());
			jsonObj.put("obj", "CfgSkillLevel");
			rtnArray.add(jsonObj);
		}
		return rtnArray;
	}

	private JSONArray getCfgObjectIDCollectionInfo(Collection<CfgObjectID> coll) {
		JSONArray rtnArray = new JSONArray();
		JSONObject jsonObj;
		CfgFolder folder = null;
		try {
			for (CfgObjectID cfgobjectid : coll) {
				jsonObj = new JSONObject();
				if (cfgobjectid.getType().name().equals("CFGPerson")) {
					jsonObj = getPersonInfo(cfgobjectid.getDBID());
				} else if (cfgobjectid.getType().name().equals("CFGFolder")) {
					// folder = getFolderInfo(cfgobjectid.getType().ordinal(),
					// cfgobjectid.getDBID());
					// jsonObj = new JSONObject();
					// jsonObj.put("dbid", cfgobjectid.getDBID());
					// jsonObj.put("type", cfgobjectid.getType().ordinal());
					// jsonObj.put("typeName", cfgobjectid.getType().name());
					// jsonObj.put("name", folder.getName());
					jsonObj = getFolderInfo(cfgobjectid.getType().ordinal(), cfgobjectid.getDBID());
					jsonObj.put("obj", "CfgFolder");
				}
				rtnArray.add(jsonObj);
			}
		} catch (Exception ex) {
		}
		return rtnArray;
	}

	private JSONObject convertFolderInfo_jsonObj(CfgFolder folder) {
		JSONObject finalJsonObj = new JSONObject();

		if (folder != null) {

			finalJsonObj.put("dbid", folder.getDBID());
			finalJsonObj.put("name", folder.getName());
			finalJsonObj.put("objectType", folder.getObjectType().ordinal());
			finalJsonObj.put("folderId", folder.getFolderId());
			finalJsonObj.put("objectDbid", folder.getObjectDbid());
			finalJsonObj.put("objectIds", getCfgObjectIDCollectionInfo(folder.getObjectIDs()));
			finalJsonObj.put("ownerDbid", folder.getOwnerID().getDBID());
			finalJsonObj.put("ownerType", folder.getOwnerID().getType().ordinal());
			finalJsonObj.put("type", folder.getType().ordinal());
			finalJsonObj.put("state", folder.getState().ordinal());
			finalJsonObj.put("annex", getAnnexInfo(folder.getUserProperties()));
			finalJsonObj.put("obj", "CfgFolder");

		}
		return finalJsonObj;
	}
}
