package kr.co.icontrols.ilp300_700.rfreader;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import kr.co.icontrols.ilp300_700.MainActivity;
import kr.co.icontrols.ilp300_700.database.DBManager;
import kr.co.icontrols.ilp300_700.global.Configure.ConfigValue;
import kr.co.icontrols.ilp300_700.global.DataClasses.AccessKeyInfo;
import kr.co.icontrols.ilp300_700.global.DataClasses.AccessKeyInfoTable;
import kr.co.icontrols.ilp300_700.global.Declare;
import kr.co.icontrols.ilp300_700.global.Declare.AccessValues;

public final class UIDAccessKeyManager {

    static final String TAG = "UIDAccessKeyManager";

    static boolean bLOG = true;   // true, false
    static boolean bLOG_DEBUG = false;   // true, false
    static boolean bLOG_INFO = true;   // true, false

    private static void LOG(String log) {
        if (bLOG) Log.v(TAG, log);
    }

    private static void LOG_DEBUG(String log) {
        if (bLOG_DEBUG) Log.d(TAG, log);
    }

    private static void LOG_INFO(String log) {
        if (bLOG_INFO) Log.i(TAG, log);
    }

    public static boolean initRFKey(Context context) {
        try {
            MainActivity.RFKeyDBManager = new DBManager(context, Declare.DBInfo.DATABASE_FILENAME_RFKEY);

            if (checkDB_RFKey()) {
                // DB 파일이 존재하는 경우 (일반 부팅으로 간주)
                LOG_INFO("[initRFKey] RFKey DB OK!!");
                return true;
            } else {
                // DB 파일이 존재하지 않는 경우 (최초부팅으로 간주)
                LOG_INFO("[initRFKey] RFKey DB NOT OK!!");
                cleanDBStorage();
                return MainActivity.RFKeyDBManager.createTable(Declare.DBInfo.TABLE_NAME_RFKEY, Declare.DBInfo.VALUE_NAME_RFKEY);
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] initRFKey()");
            e.printStackTrace();
            return false;
        }
    }

    private static boolean checkDB_RFKey() {
        try {
            if (!MainActivity.RFKeyDBManager.isDBFileExist()) return false;
            if (!MainActivity.RFKeyDBManager.initDatabase()) return false;
            if (!MainActivity.RFKeyDBManager.isTableExist(Declare.DBInfo.TABLE_NAME_RFKEY))
                return false;
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] checkDB_RFKey()");
            e.printStackTrace();
            return false;
        }
    }

    private static void cleanDBStorage() {
        try {
            LOG_INFO("[cleanDBStorage] ===== START =====");
            if (MainActivity.RFKeyDBManager.isDBFileExist()) {
                LOG_INFO("[cleanDBStorage] DBFile exist!!!");
                MainActivity.RFKeyDBManager.deleteDBFile();
            }
            MainActivity.RFKeyDBManager.initDatabase();
        } catch (Exception e) {
            Log.e(TAG, "[Exception] cleanDBStorage()");
            e.printStackTrace();
        }
    }

    public static boolean setDBKey(String strKey, AccessKeyInfo KeyInfo) {
        try {
            return MainActivity.RFKeyDBManager.setData(Declare.DBInfo.TABLE_NAME_RFKEY, strKey, Declare.DBInfo.VALUE_NAME_RFKEY, KeyInfo.getValueArray());
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setDBKey()");
            e.printStackTrace();
            return false;
        }
    }

    public static AccessKeyInfo getDBKey(String strKey) {
        try {
//            LOG("[getDBKey] strKey = " + strKey);
            String[] aDBResult = MainActivity.RFKeyDBManager.getDataAll(Declare.DBInfo.TABLE_NAME_RFKEY, strKey);
//            for (int i = 0; i < aDBResult.length; i++) {
//                LOG_DEBUG("[getDBKey] aDBResult[" + i + "] = " + aDBResult[i]);
//            }
            AccessKeyInfo KeyInfo = new AccessKeyInfo();
            if (aDBResult.length != Declare.DBInfo.VALUE_NAME_RFKEY.length + 1) {
                KeyInfo.setValue(aDBResult[0], aDBResult[0], aDBResult[0], aDBResult[0], aDBResult[0], aDBResult[0], aDBResult[0], aDBResult[0], aDBResult[0], aDBResult[0], aDBResult[0], aDBResult[0], aDBResult[0], aDBResult[0], aDBResult[0]);
                return KeyInfo;
            }
            KeyInfo.setValue(aDBResult[6], aDBResult[7], aDBResult[8], aDBResult[2], aDBResult[3], aDBResult[4], aDBResult[5], aDBResult[9], aDBResult[10], aDBResult[11], aDBResult[12], aDBResult[13], aDBResult[14], aDBResult[15], aDBResult[16]);
            return KeyInfo;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getDBKey(String strKey)");
            e.printStackTrace();
            return null;
        }
    }

    public static int deleteDBKey(String strTable, String strKey) {
        try {
            LOG("[deleteDBKey] strTable [" + strTable + "], strKey [" + strKey + "]");
            int nResult = MainActivity.RFKeyDBManager.deleteDBKey(strTable, strKey);
            LOG("[deleteDBKey] nResult = " + nResult);
            return nResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] deleteDBKey(strTable, strKey)");
            e.printStackTrace();
            return -99;
        }
    }

    public static int deleteDBRec(String strTable, String[] strNames, String[] strValues) {
        try {
            int nResult = MainActivity.RFKeyDBManager.deleteDBRec(strTable, strNames, strValues);
            LOG("[deleteDBRec] nResult = " + nResult);
            return nResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] deleteDBRec(strTable, strNames, strValues)");
            e.printStackTrace();
            return -99;
        }
    }

    public static int isKeyExist(String strKey) {
        try {
            int nResult = MainActivity.RFKeyDBManager.isKeyExist(Declare.DBInfo.TABLE_NAME_RFKEY, strKey);
            LOG("[isKeyExist] nResult = " + nResult);
            return nResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] isKeyExist(strKey)");
            e.printStackTrace();
            return -99;
        }
    }

    public static boolean initAccessKeyTest() {
        try {
            LOG("[initAccessKeyTest] ===== START =====");
            long start;
            long end;
            long difference;
            start = System.currentTimeMillis();
            AccessKeyInfo AccessKeyTest = new AccessKeyInfo();
//            String TestUID = "FDE1F1C28000007";
//            AccessKeyTest.setValue("12345678", "102", "103", AccessValues.hkey, TestUID, TestUID, TestUID, "1", "1", "2015.03.01", "2015.3.30", "2015.03.15", "", "", "");
//            MainActivity.RFKeyDBManager.setData(Declare.DBInfo.TABLE_NAME_RFKEY, AccessKeyTest.getUID(), Declare.DBInfo.VALUE_NAME_RFKEY, AccessKeyTest.getValueArray());
            for (int i = 0; i < 100; i++) {
                String TestUID = "FDE1F1C280" + String.format("%05X", i);
//                LOG("[initAccessKeyTest] TestUID! [" + TestUID + "]");
                if (TestUID.equalsIgnoreCase("FDE1F1C28000007")) {
                    AccessKeyTest.setValue("12345678", "102", "103", AccessValues.hkey, TestUID, TestUID, TestUID, "1", "1", "2015.03.01", "2015.3.30", "2015.03.15", "", "", "");
                    AccessKeyTest.setValue("12345678", "202", "202", AccessValues.hkey, TestUID, TestUID, TestUID, "1", "1", "2015.03.01", "2015.3.30", "2015.03.15", "", "", "");
                } else {
                    AccessKeyTest.setValue("12345678", "102", "102", AccessValues.hkey, TestUID, TestUID, TestUID, "1", "1", "2015.03.01", "2015.3.30", "2015.03.15", "", "", "");
                }
                MainActivity.RFKeyDBManager.setData(Declare.DBInfo.TABLE_NAME_RFKEY, AccessKeyTest.getUID(), Declare.DBInfo.VALUE_NAME_RFKEY, AccessKeyTest.getValueArray());
            }
            end = System.currentTimeMillis();
            difference = end - start;
            LOG("[initAccessKeyTest] start [" + start + "], end [" + end + "], difference [" + difference + "]");
            LOG("[initAccessKeyTest] ===== END =====");
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] AccessKeyTest()");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean onSetAccessKeyInfo(String strAction, String strKeyType, String strDong, String strHo, AccessKeyInfoTable KeyInfoTable) {
        try {
            LOG("[onSetAccessKeyInfo] strAction = " + strAction + ", strKeyType = " + strKeyType + ", strDong = " + strDong + ", strHo = " + strHo);
//            KeyInfoTable.printValue();
            if (strAction.equalsIgnoreCase("save")) return saveAccessKey(KeyInfoTable);
            else if (strAction.equalsIgnoreCase("delete")) return deleteAccessKey(KeyInfoTable);
            else if (strAction.equalsIgnoreCase("renew"))
                return renewAccessKey(strKeyType, strDong, strHo, KeyInfoTable);
            else return false;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] onSetAccessKeyInfo(strAction, KeyInfoTable)");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean saveAccessKey(AccessKeyInfoTable KeyInfoTable) {
        try {
            LOG("[saveAccessKey] ===== START =====");
            int nCnt = KeyInfoTable.getCnt();
            for (int i = 0; i < nCnt; i++) {
                if (isKeyExist(KeyInfoTable.getKeyInfo(i).getUID()) > 0) {
                    LOG("[saveAccessKey] Duplicated Key [" + KeyInfoTable.getKeyInfo(i).getUID() + "]");
                    return false;
                }
            }
            AccessKeyInfo[] KeyInfo;
            KeyInfo = changeDataClass(KeyInfoTable);
            for (int i = 0; i < nCnt; i++) {
                if (!setDBKey(KeyInfo[i].getUID(), KeyInfo[i])) return false;
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] saveAccessKey(KeyInfoTable)");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean deleteAccessKey(AccessKeyInfoTable KeyInfoTable) {
        try {
            int nCnt = KeyInfoTable.getCnt();
            int nResult = 0;
            for (int i = 0; i < nCnt; i++) {
                nResult = deleteDBKey(Declare.DBInfo.TABLE_NAME_RFKEY, KeyInfoTable.getKeyInfo(i).getUID());
//                LOG("[deleteAccessKey] nResult = " + nResult);
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] deleteAccessKey(KeyInfoTable)");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean renewAccessKey(String strKeyType, String strDong, String strHo, AccessKeyInfoTable KeyInfoTable) {
        try {
            LOG("[renewAccessKey] ===== START =====");
            for (int i = 0; i < KeyInfoTable.getCnt(); i++) {
                String strKey = KeyInfoTable.getKeyInfo(i).getUID();
                AccessKeyInfo Key = getDBKey(strKey);
//                Key.printValue();
//                LOG("[renewAccessKey] Key.getUID = " + Key.getUID());
                if (Key.getSitecode().equalsIgnoreCase("DUPLICATED")) {
                    Key.printValue();
                    return false;
                } else if (Key.getSitecode().equalsIgnoreCase(ConfigValue.strSitecode)) {
                    if (!(Key.getKeyType().equalsIgnoreCase(strKeyType) && Key.getDong().equalsIgnoreCase(strDong) && Key.getHo().equalsIgnoreCase(strHo))) {
                        LOG_INFO("[renewAccessKey] Duplicated with other resi!!");
                        Key.printValue();
                        return false;
                    }
                }
            }

            if (strKeyType.equalsIgnoreCase(AccessValues.hkey)) {
                String[] strNames = {"KEYTYPE", "DONG", "HO"};
                String[] strValues = {strKeyType, strDong, strHo};
                deleteDBRec(Declare.DBInfo.TABLE_NAME_RFKEY, strNames, strValues);
            } else if (strKeyType.equalsIgnoreCase(AccessValues.dkey)) {
                String[] strNames = {"KEYTYPE", "DONG"};
                String[] strValues = {strKeyType, strDong};
                deleteDBRec(Declare.DBInfo.TABLE_NAME_RFKEY, strNames, strValues);
            } else if (strKeyType.equalsIgnoreCase(AccessValues.mkey)) {
                String[] strNames = {"KEYTYPE"};
                String[] strValues = {strKeyType};
                deleteDBRec(Declare.DBInfo.TABLE_NAME_RFKEY, strNames, strValues);
            } else return false;

            AccessKeyInfo[] KeyInfo = changeDataClass(KeyInfoTable);
            if (KeyInfo == null) return false;
            for (int i = 0; i < KeyInfo.length; i++) {
                if (!setDBKey(KeyInfo[i].getUID(), KeyInfo[i])) return false;
            }
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] renewAccessKey(strKeyType, strDong, strHo, KeyInfoTable)");
            e.printStackTrace();
            return false;
        }
    }

    public static String[] getKeyByKeySN(String strKeySN) {
        try {
            String strDBQuiry = "select KEY from " + Declare.DBInfo.TABLE_NAME_RFKEY + " where SN=\"" + strKeySN + "\";";
            Cursor mCursor = MainActivity.RFKeyDBManager.accessDBDirectly(strDBQuiry);
            int nRecCnt = mCursor.getCount();
            String[] strResult = new String[nRecCnt];
            int nCnt = 0;

            while (mCursor.moveToNext()) {
                strResult[nCnt] = mCursor.getString(0);
                nCnt++;
            }
            return strResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyByKeySN(strKeySN)");
            e.printStackTrace();
            return null;
        }
    }

    public static String[] getKeyByKeyType(String strKeyType) {
        try {
            String strDBQuiry = "select KEY from " + Declare.DBInfo.TABLE_NAME_RFKEY + " where KEYTYPE=\"" + strKeyType + "\";";
            Cursor mCursor = MainActivity.RFKeyDBManager.accessDBDirectly(strDBQuiry);
            int nRecCnt = mCursor.getCount();
            String[] strResult = new String[nRecCnt];
            int nCnt = 0;

            while (mCursor.moveToNext()) {
                strResult[nCnt] = mCursor.getString(0);
                nCnt++;
            }
            return strResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyByKeyType(strKeyType)");
            e.printStackTrace();
            return null;
        }
    }

    public static String[] getKeyByKeyInfo(String strKeyType, String strDong, String strHo) {
        try {
            String strDBQuiry = "select KEY from " + Declare.DBInfo.TABLE_NAME_RFKEY + " where KEYTYPE=\"" + strKeyType + "\" and DONG=\"" + strDong + "\" and HO=\"" + strHo + "\";";
            Cursor mCursor = MainActivity.RFKeyDBManager.accessDBDirectly(strDBQuiry);
            int nRecCnt = mCursor.getCount();
            String[] strResult = new String[nRecCnt];
            int nCnt = 0;
            if (mCursor.moveToNext()) {
                strResult[nCnt] = mCursor.getString(0);
                nCnt++;
            }
            return strResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyByKeyInfo(strDong, strHo)");
            e.printStackTrace();
            return null;
        }
    }

    public static String[] getKeyByHKeyInfo(String strDong, String strHo) {
        try {
            String strDBQuiry = "select KEY from " + Declare.DBInfo.TABLE_NAME_RFKEY + " where KEYTYPE=\"" + AccessValues.hkey + "\" and DONG=\"" + strDong + "\" and HO=\"" + strHo + "\";";
            Cursor mCursor = MainActivity.RFKeyDBManager.accessDBDirectly(strDBQuiry);
            int nRecCnt = mCursor.getCount();
            String[] strResult = new String[nRecCnt];
            int nCnt = 0;
            while (mCursor.moveToNext()) {
                strResult[nCnt] = mCursor.getString(0);
                nCnt++;
            }
            return strResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyByHKeyInfo(strDong, strHo)");
            e.printStackTrace();
            return null;
        }
    }

    public static String[] getKeyByHKeyInfo(String strDong, String strHo, String strNo) {
        try {
            String strDBQuiry = "select KEY from " + Declare.DBInfo.TABLE_NAME_RFKEY + " where KEYTYPE=\"" + AccessValues.hkey + "\" and DONG=\"" + strDong + "\" and HO=\"" + strHo + "\" and KEYNO=\"" + strNo + "\";";
            Cursor mCursor = MainActivity.RFKeyDBManager.accessDBDirectly(strDBQuiry);
            int nRecCnt = mCursor.getCount();
            String[] strResult = new String[nRecCnt];
            int nCnt = 0;
            while (mCursor.moveToNext()) {
                strResult[nCnt] = mCursor.getString(0);
                nCnt++;
            }
            return strResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyByHKeyInfo(String strDong, String strHo, String strNo)");
            e.printStackTrace();
            return null;
        }
    }

    public static String[] getKeyByDKeyInfo(String strDong) {
        try {
            String strDBQuiry = "select KEY from " + Declare.DBInfo.TABLE_NAME_RFKEY + " where KEYTYPE=\"" + AccessValues.dkey + "\" and DONG=\"" + strDong + "\";";
            Cursor mCursor = MainActivity.RFKeyDBManager.accessDBDirectly(strDBQuiry);
            int nRecCnt = mCursor.getCount();
            String[] strResult = new String[nRecCnt];
            int nCnt = 0;
            while (mCursor.moveToNext()) {
                strResult[nCnt] = mCursor.getString(0);
                nCnt++;
            }
            return strResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyByDKeyInfo(strDong)");
            e.printStackTrace();
            return null;
        }
    }

    public static String[] getKeyByDKeyInfo(String strDong, String strNo) {
        try {
            String strDBQuiry = "select KEY from " + Declare.DBInfo.TABLE_NAME_RFKEY + " where KEYTYPE=\"" + AccessValues.dkey + "\" and DONG=\"" + strDong + "\" and KEYNO=\"" + strNo + "\";";
            Cursor mCursor = MainActivity.RFKeyDBManager.accessDBDirectly(strDBQuiry);
            int nRecCnt = mCursor.getCount();
            String[] strResult = new String[nRecCnt];
            int nCnt = 0;
            while (mCursor.moveToNext()) {
                strResult[nCnt] = mCursor.getString(0);
                nCnt++;
            }
            return strResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyByDKeyInfo(String strDong, String strNo)");
            e.printStackTrace();
            return null;
        }
    }

    public static String[] getKeyByMKeyInfo() {
        try {
            String strDBQuiry = "select KEY from " + Declare.DBInfo.TABLE_NAME_RFKEY + " where KEYTYPE=\"" + AccessValues.mkey + "\";";
            Cursor mCursor = MainActivity.RFKeyDBManager.accessDBDirectly(strDBQuiry);
            int nRecCnt = mCursor.getCount();
            String[] strResult = new String[nRecCnt];
            int nCnt = 0;
            while (mCursor.moveToNext()) {
                strResult[nCnt] = mCursor.getString(0);
                nCnt++;
            }
            return strResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyByMKeyInfo()");
            e.printStackTrace();
            return null;
        }
    }

    public static String[] getKeyByMKeyInfo(String strNo) {
        try {
            String strDBQuiry = "select KEY from " + Declare.DBInfo.TABLE_NAME_RFKEY + " where KEYTYPE=\"" + AccessValues.mkey + "\" and KEYNO=\"" + strNo + "\";";
            Cursor mCursor = MainActivity.RFKeyDBManager.accessDBDirectly(strDBQuiry);
            int nRecCnt = mCursor.getCount();
            String[] strResult = new String[nRecCnt];
            int nCnt = 0;
            while (mCursor.moveToNext()) {
                strResult[nCnt] = mCursor.getString(0);
                nCnt++;
            }
            return strResult;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyByMKeyInfo()");
            e.printStackTrace();
            return null;
        }
    }

    public static AccessKeyInfo changeDataClass(AccessKeyInfoTable.KeyInfo KeyInfoTable) {
        try {
            AccessKeyInfo KeyInfo = new AccessKeyInfo();
            KeyInfo.setValue(KeyInfoTable.getSitecode(), KeyInfoTable.getDong(), KeyInfoTable.getHo(), KeyInfoTable.getType(),
                    KeyInfoTable.getUID(), KeyInfoTable.getUID(), KeyInfoTable.getSN(), KeyInfoTable.getKeyNo(),
                    KeyInfoTable.getIssueCnt(), KeyInfoTable.getIssuedDate(), "null", KeyInfoTable.getLastUseDate(), "null",
                    KeyInfoTable.getMessage(), "null");
            return KeyInfo;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] changeDataClass(KeyInfoTable)");
            e.printStackTrace();
            return null;
        }
    }

    public static AccessKeyInfo[] changeDataClass(AccessKeyInfoTable KeyInfoTable) {
        try {
            LOG("[changeDataClass] KeyInfoTable.getCnt() = " + KeyInfoTable.getCnt());
//            KeyInfoTable.printValue();
            int nCnt = KeyInfoTable.getCnt();
            AccessKeyInfo[] KeyInfo = new AccessKeyInfo[nCnt];
            for (int i = 0; i < nCnt; i++) {
                KeyInfo[i] = new AccessKeyInfo();
                KeyInfo[i].setValue(KeyInfoTable.getKeyInfo(i).getSitecode(), KeyInfoTable.getKeyInfo(i).getDong(), KeyInfoTable.getKeyInfo(i).getHo(), KeyInfoTable.getKeyInfo(i).getType(),
                        KeyInfoTable.getKeyInfo(i).getUID(), KeyInfoTable.getKeyInfo(i).getUID(), KeyInfoTable.getKeyInfo(i).getSN(), KeyInfoTable.getKeyInfo(i).getKeyNo(),
                        KeyInfoTable.getKeyInfo(i).getIssueCnt(), KeyInfoTable.getKeyInfo(i).getIssuedDate(), "null", KeyInfoTable.getKeyInfo(i).getLastUseDate(), "null",
                        KeyInfoTable.getKeyInfo(i).getMessage(), "null");
            }
            return KeyInfo;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] changeDataClass(AccessKeyInfoTable KeyInfoTable)");
            e.printStackTrace();
            return null;
        }
    }

    public static AccessKeyInfoTable changeDataClass(AccessKeyInfo KeyInfo) {
        try {
            AccessKeyInfoTable KeyInfoTable = new AccessKeyInfoTable();

            if (KeyInfo == null) return null;

            KeyInfoTable.addKeyInfo("1", KeyInfo.getSN(), KeyInfo.getUID(), KeyInfo.getKeyType(), KeyInfo.getSitecode(), KeyInfo.getDong(), KeyInfo.getHo(),
                    KeyInfo.getKeyNo(), KeyInfo.getIssueCnt(), KeyInfo.getIssuedDate(), KeyInfo.getLastUseDate(), KeyInfo.getMessage());

            return KeyInfoTable;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] changeDataClass(AccessKeyInfo KeyInfo)");
            e.printStackTrace();
            return null;
        }
    }

    public static AccessKeyInfoTable changeDataClass(AccessKeyInfo[] KeyInfo) {
        try {
            AccessKeyInfoTable KeyInfoTable = new AccessKeyInfoTable();

            if (KeyInfo == null) return null;

            if (KeyInfo.length == 0) {
                KeyInfoTable.addKeyInfo("NONE", "NONE", "NONE", "NONE", "NONE", "NONE", "NONE", "NONE", "NONE", "NONE", "NONE", "NONE");
            } else {
                int nIndex = 0;
                for (int i = 0; i < KeyInfo.length; i++) {
                    nIndex = i + 1;
                    KeyInfoTable.addKeyInfo(Integer.toString(nIndex), KeyInfo[i].getSN(), KeyInfo[i].getUID(), KeyInfo[i].getKeyType(), KeyInfo[i].getSitecode(), KeyInfo[i].getDong(), KeyInfo[i].getHo(),
                            KeyInfo[i].getKeyNo(), KeyInfo[i].getIssueCnt(), KeyInfo[i].getIssuedDate(), KeyInfo[i].getLastUseDate(), KeyInfo[i].getMessage());
                }
            }
            return KeyInfoTable;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] changeDataClass(AccessKeyInfo[] KeyInfo)");
            e.printStackTrace();
            return null;
        }
    }

    public static AccessKeyInfoTable getKeyInfoByUID(String strUID) {
        try {
            AccessKeyInfo KeyInfo = getDBKey(strUID);
            return changeDataClass(KeyInfo);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyInfoByUID(String strUID)");
            e.printStackTrace();
            return null;
        }
    }

    public static AccessKeyInfoTable getKeyInfoBySN(String strSN) {
        try {
            String[] astrResult = getKeyByKeySN(strSN);
            AccessKeyInfoTable KeyInfoTable = new AccessKeyInfoTable();

            if (astrResult.length == 0) {
                KeyInfoTable.addKeyInfo("NONE", "NONE", "NONE", "NONE", "NONE", "NONE", "NONE", "NONE", "NONE", "NONE", "NONE", "NONE");
                return KeyInfoTable;
            }

            AccessKeyInfo[] aKeyInfo = new AccessKeyInfo[astrResult.length];
            if (astrResult.length != 0) {
                for (int i = 0; i < astrResult.length; i++) {
//                    LOG("[getKeyInfoBySN] astrResult[" + i + "] = " + astrResult[i]);
                    aKeyInfo[i] = getDBKey(astrResult[i]);
                }
                KeyInfoTable = changeDataClass(aKeyInfo);
            }

            return KeyInfoTable;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyInfoBySN(String strSN)");
            e.printStackTrace();
            return null;
        }
    }

    public static AccessKeyInfoTable getKeyInfoByUIDALL(String strUID) {
        try {
            AccessKeyInfo KeyInfo = getDBKey(strUID);

            if (KeyInfo.getUID().equals("NONE")) {
                return changeDataClass(KeyInfo);
            }

//            KeyInfo.printValue();
            if (KeyInfo.getKeyType().equals("hkey")) {
                String astrResult[] = getKeyByHKeyInfo(KeyInfo.getDong(), KeyInfo.getHo());
                AccessKeyInfo[] aKeyInfo = new AccessKeyInfo[astrResult.length];
                for (int i = 0; i < astrResult.length; i++) {
                    aKeyInfo[i] = getDBKey(astrResult[i]);
                }
                return changeDataClass(aKeyInfo);
            } else if (KeyInfo.getKeyType().equals("dkey")) {
                String astrResult[] = getKeyByDKeyInfo(KeyInfo.getDong());
                AccessKeyInfo[] aKeyInfo = new AccessKeyInfo[astrResult.length];
                for (int i = 0; i < astrResult.length; i++) {
                    aKeyInfo[i] = getDBKey(astrResult[i]);
                }
                return changeDataClass(aKeyInfo);
            } else if (KeyInfo.getKeyType().equals("mkey")) {
                String astrResult[] = getKeyByMKeyInfo();
                AccessKeyInfo[] aKeyInfo = new AccessKeyInfo[astrResult.length];
                for (int i = 0; i < astrResult.length; i++) {
                    aKeyInfo[i] = getDBKey(astrResult[i]);
                }
                return changeDataClass(aKeyInfo);
            } else {
                Log.w(TAG, "[getKeyInfoByUIDALL] KeyType is not correct!!");
                return null;
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyInfoByUIDALL(String strUID)");
            e.printStackTrace();
            return null;
        }
    }

    public static AccessKeyInfoTable getKeyInfoBySNALL(String strSN) {
        try {
            String[] astrResult = getKeyByKeySN(strSN);
            if (astrResult.length == 0) {
                // SN이 검색되지 않는 경우
                AccessKeyInfoTable KeyInfoTable = new AccessKeyInfoTable();
                KeyInfoTable.addKeyInfo("NONE", "NONE", "NONE", "NONE", "NONE", "NONE", "NONE", "NONE", "NONE", "NONE", "NONE", "NONE");
                return KeyInfoTable;
            }

            AccessKeyInfo KeyInfo = getDBKey(astrResult[0]);

            if (KeyInfo.getUID().equals("NONE")) {
                // UID가 검색되지 않는 경우
                return changeDataClass(KeyInfo);
            } else if (KeyInfo.getUID().equals("DUPLICATED")) {
                // UID가 중복되는 경우
                return changeDataClass(KeyInfo);
            }

            KeyInfo.printValue();
            if (KeyInfo.getKeyType().equals("hkey")) {
                astrResult = getKeyByHKeyInfo(KeyInfo.getDong(), KeyInfo.getHo());
                AccessKeyInfo[] aKeyInfo = new AccessKeyInfo[astrResult.length];
                for (int i = 0; i < astrResult.length; i++) {
                    aKeyInfo[i] = getDBKey(astrResult[i]);
                }
                return changeDataClass(aKeyInfo);
            } else if (KeyInfo.getKeyType().equals("dkey")) {
                astrResult = getKeyByDKeyInfo(KeyInfo.getDong());
                AccessKeyInfo[] aKeyInfo = new AccessKeyInfo[astrResult.length];
                for (int i = 0; i < astrResult.length; i++) {
                    aKeyInfo[i] = getDBKey(astrResult[i]);
                }
                return changeDataClass(aKeyInfo);
            } else if (KeyInfo.getKeyType().equals("mkey")) {
                astrResult = getKeyByMKeyInfo();
                AccessKeyInfo[] aKeyInfo = new AccessKeyInfo[astrResult.length];
                for (int i = 0; i < astrResult.length; i++) {
                    aKeyInfo[i] = getDBKey(astrResult[i]);
                }
                return changeDataClass(aKeyInfo);
            } else {
                Log.w(TAG, "[getKeyInfoBySNALL] KeyType is not correct!!");
                return null;
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyInfoBySNALL(String strSN)");
            e.printStackTrace();
            return null;
        }
    }

    public static AccessKeyInfoTable getKeyInfoByKEYTYPE(String strKeyType) {
        try {
            String[] astrResult = getKeyByKeyType(strKeyType);

            AccessKeyInfo[] aKeyInfo = new AccessKeyInfo[astrResult.length];
            AccessKeyInfoTable KeyInfoTable = new AccessKeyInfoTable();

            if (astrResult.length != 0) {
                for (int i = 0; i < astrResult.length; i++) {
//                    LOG("[getKeyInfoByKEYTYPE] astrResult[" + i + "] = " + astrResult[i]);
                    aKeyInfo[i] = getDBKey(astrResult[i]);
                }
                KeyInfoTable = changeDataClass(aKeyInfo);
            }
            return KeyInfoTable;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyInfoByKeyType(String strKeyType)");
            e.printStackTrace();
            return null;
        }
    }

    public static AccessKeyInfoTable getKeyInfoByHKEY(String strDong, String strHo, String strNo) {
        try {
            if (strNo.equals("all")) {
                String astrResult[] = getKeyByHKeyInfo(strDong, strHo);
                AccessKeyInfo[] aKeyInfo = new AccessKeyInfo[astrResult.length];
                for (int i = 0; i < astrResult.length; i++) {
                    aKeyInfo[i] = getDBKey(astrResult[i]);
                }
                return changeDataClass(aKeyInfo);
            } else {
                int nNo = Integer.parseInt(strNo);
                String astrResult[] = getKeyByHKeyInfo(strDong, strHo, Integer.toString(nNo));
                AccessKeyInfo[] aKeyInfo = new AccessKeyInfo[astrResult.length];
                for (int i = 0; i < astrResult.length; i++) {
                    aKeyInfo[i] = getDBKey(astrResult[i]);
                }
                return changeDataClass(aKeyInfo);
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyInfoByHKEY(String strDong, String strHo, String strNo)");
            e.printStackTrace();
            return null;
        }
    }

    public static AccessKeyInfoTable getKeyInfoByDKEY(String strDong, String strNo) {
        try {
            if (strNo.equals("all")) {
                String astrResult[] = getKeyByDKeyInfo(strDong);
                AccessKeyInfo[] aKeyInfo = new AccessKeyInfo[astrResult.length];
                for (int i = 0; i < astrResult.length; i++) {
                    aKeyInfo[i] = getDBKey(astrResult[i]);
                }
                return changeDataClass(aKeyInfo);
            } else {
                int nNo = Integer.parseInt(strNo);
                String astrResult[] = getKeyByDKeyInfo(strDong, Integer.toString(nNo));
                AccessKeyInfo[] aKeyInfo = new AccessKeyInfo[astrResult.length];
                for (int i = 0; i < astrResult.length; i++) {
                    aKeyInfo[i] = getDBKey(astrResult[i]);
                }
                return changeDataClass(aKeyInfo);
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyInfoByDKEY(String strDong, String strNo)");
            e.printStackTrace();
            return null;
        }
    }

    public static AccessKeyInfoTable getKeyInfoByMKEY(String strNo) {
        try {
            if (strNo.equals("all")) {
                String astrResult[] = getKeyByMKeyInfo();
                AccessKeyInfo[] aKeyInfo = new AccessKeyInfo[astrResult.length];
                for (int i = 0; i < astrResult.length; i++) {
                    aKeyInfo[i] = getDBKey(astrResult[i]);
                }
                return changeDataClass(aKeyInfo);
            } else {
                int nNo = Integer.parseInt(strNo);
                String astrResult[] = getKeyByMKeyInfo(Integer.toString(nNo));
                AccessKeyInfo[] aKeyInfo = new AccessKeyInfo[astrResult.length];
                for (int i = 0; i < astrResult.length; i++) {
                    aKeyInfo[i] = getDBKey(astrResult[i]);
                }
                return changeDataClass(aKeyInfo);
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getKeyInfoByMKEY(String strNo)");
            e.printStackTrace();
            return null;
        }
    }


    // 사용하지 않는 코드를 정리함 (주석처리가 묶이지 않아서 함수로 포장함, JEFF, 2015.03.31)
    private void unusedcodes() {
/*


     // UID Key를 저장하는 TreeMap 선언
    public static TreeMap<String, AccessKeyInfo> AccessKeyMap = new TreeMap<String, AccessKeyInfo>();

    public static boolean initRFKey(Context context) {
        try {
            if (!clearAccessKeyData()) return false;
            MainActivity.RFKeyDBManager = new DBManager(context, Declare.DBInfo.DATABASE_FILENAME_RFKEY);

            if (MainActivity.RFKeyDBManager.isDBFileExist()) {
                // DB 파일이 존재하는 경우 (일반 부팅으로 간주)
                LOG("[initRFKey] The file exist!! [" + Declare.DBInfo.DATABASE_FILENAME_RFKEY + "]");
                MainActivity.RFKeyDBManager.initDatabase();
                return true;
//                return getDBRFKeyAll();
            }
            else {
                // DB 파일이 존재하지 않는 경우 (최초부팅으로 간주)
                LOG("[initRFKeySTD] The file does not exist!! [" + Declare.DBInfo.DATABASE_FILENAME_RFKEY + "]");
                MainActivity.RFKeyDBManager.initDatabase();
                MainActivity.RFKeyDBManager.createTable(Declare.DBInfo.TABLE_NAME_RFKEY, Declare.DBInfo.VALUE_NAME_RFKEY);
                initAccessKeyTest();
                setDBAccessKey();
                return true;
            }
        } catch (Exception e) {
            Log.e(TAG, "[Exception] initRFKey()");
            e.printStackTrace();
            return false;
        }
    }

    // AccessKey 정보 초기화
    private static boolean clearAccessKeyData() {
        try {
            AccessKeyMap.clear();
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] initAccessKeyData()");
            e.printStackTrace();
            return false;
        }
    }

    // Key 존재 유무 확인
    private static boolean isExistAccessKey(String strKey) {
        try {
            if (AccessKeyMap.containsKey(strKey)) return true;
            else return false;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] isExistAccessKey(strKey)");
            e.printStackTrace();
            return false;
        }
    }

    // AccessKey 추가
    private static boolean addAccessKey(AccessKeyInfo KeyInfo) {
        try {
            if (AccessKeyMap.containsKey(KeyInfo.getUID())) {
                LOG("[addAccessKeyData] Duplicated Key [" + KeyInfo.getUID() + "]");
                return false;
            }
            AccessKeyMap.put(KeyInfo.getUID(), KeyInfo);
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] addAccessKey(KeyInfo)");
            e.printStackTrace();
            return false;
        }
    }

    // AccessKey 추가 (배열입력)
    private static boolean addAccessKey(AccessKeyInfo[] aKeyInfo) {
        try {
            for (int i = 0; i < aKeyInfo.length; i++) {
                if (AccessKeyMap.containsKey(aKeyInfo[i].getUID())) {
                    LOG("[addAccessKey] Duplicated Key [" + aKeyInfo[i].getUID() + "]");
                    return false;
                }
            }

            for (int i = 0; i < aKeyInfo.length; i++) {
                if (!addAccessKey(aKeyInfo[i])) {
                    return false;
                }
            }
            return false;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] addAccessKey(aKeyInfo)");
            e.printStackTrace();
            return false;
        }
    }

    // AccessKey 업데이트
    private static boolean updateAccessKey(String strKey, AccessKeyInfo KeyInfo) {
        try {
            if (!AccessKeyMap.containsKey(strKey)) {
                LOG("[updateAccessKeyData] No Key [" + strKey + "]");
                return false;
            }
            AccessKeyMap.put(strKey, KeyInfo);
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] updateAccessKey(strKey, KeyInfo)");
            e.printStackTrace();
            return false;
        }
    }

    // AccessKey 삭제
    private static boolean deleteAccessKey(String strKey) {
        try {
            if (!AccessKeyMap.containsKey(strKey)) {
                LOG("[deleteAccessKeyData] No Key [" + strKey + "]");
                return false;
            }
            AccessKeyMap.remove(strKey);
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] deleteAccessKey(strKey)");
            e.printStackTrace();
            return false;
        }
    }

    // AccessKeyMap에 저장된 Key 정보를 DB에 저장한다.
    public static boolean setDBAccessKey() {
        try {
            LOG("[setDBAccessKey] ===== START =====");
            long start; long end; long difference;
            start = System.currentTimeMillis();
            Iterator<String> mIterator = AccessKeyMap.keySet().iterator();
            AccessKeyInfo KeyInfo;
            String strKey;
            while (mIterator.hasNext()) {
                strKey = (String)mIterator.next();
                KeyInfo = AccessKeyMap.get(strKey);
                if (!MainActivity.RFKeyDBManager.setData(Declare.DBInfo.TABLE_NAME_RFKEY, strKey, Declare.DBInfo.VALUE_NAME_RFKEY, KeyInfo.getValueArray())) {
                    return false;
                }
            }
            end = System.currentTimeMillis();
            difference = end - start;
            LOG("[setDBAccessKey] start [" + start + "], end [" + end + "], difference [" + difference + "]");
            LOG("[setDBAccessKey] ===== END =====");
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] setDBAccessKey()");
            e.printStackTrace();
            return false;
        }
    }

    // DB에 저장된 Key 정보를 가져온다.
    public static boolean getDBAccessKey() {
        try {
            return false;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getDBAccessKey()");
            e.printStackTrace();
            return false;
        }
    }

    // DB에 저장된 Key 정보를 삭제한다.
    public static boolean deleteDBAccessKey(String strKey) {
        try {
            return false;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] deleteDBAccessKey()");
            e.printStackTrace();
            return false;
        }
    }

    // AccessKey 가져오기 by Key (중복없음)
    private static AccessKeyInfo getAccessKeyByKey(String strKey) {
        try {
            if (!AccessKeyMap.containsKey(strKey)) {
                LOG("[deleteAccessKeyData] No Key [" + strKey + "]");
                return null;
            }
            return AccessKeyMap.get(strKey);
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getAccessKeyByKey(strKey)");
            e.printStackTrace();
            return null;
        }
    }

    // AccessKey 가져오기 by SN (중복가능, 중복시 첫번째 검색되는 정보반환)
    private static AccessKeyInfo getAccessKeyBySN(String strSN) {
        try {
            Iterator<String> mIterator = AccessKeyMap.keySet().iterator();
            AccessKeyInfo KeyInfo;
            String strKey;
            while (mIterator.hasNext()) {
                strKey = (String)mIterator.next();
                KeyInfo = AccessKeyMap.get(strKey);
                if (KeyInfo.getSN().equalsIgnoreCase(strSN)) {
                    return KeyInfo;
                }
            }
            LOG("[getAccessKeyBySN] No SN [" + strSN + "]");
            return null;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getAccessKeyBySN(strSN)");
            e.printStackTrace();
            return null;
        }
    }

    // AccessKeyInfoTable 가져오기 by Key (중복없음)
    private static AccessKeyInfoTable getAccessKeyTableByKey(String strKey) {
        try {
            if (!AccessKeyMap.containsKey(strKey)) {
                LOG("[getAccessKeyTableByKey] No Key [" + strKey + "]");
                return null;
            }
            AccessKeyInfo KeyInfo = AccessKeyMap.get(strKey);
            AccessKeyInfoTable Result = new AccessKeyInfoTable();
            Result.addAccessKeyInfo("1", KeyInfo.getSN(), KeyInfo.getUID(), KeyInfo.getKeyType(), KeyInfo.getSitecode(), KeyInfo.getDong(), KeyInfo.getHo(),
                                                 KeyInfo.getKeyNo(), KeyInfo.getIssueCnt(), KeyInfo.getLastUseDate(), KeyInfo.getLastUseDate(), KeyInfo.getMessage());
            return Result;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getAccessKeyTableByKey(strKey)");
            e.printStackTrace();
            return null;
        }
    }

    // AccessKeyInfoTable 가져오기 by Key
    // Key값이 소속된 그룹(동일세대, 동관리자, 단지관리자)의 Key정보를 모두 가져온다.
    private static AccessKeyInfoTable getAccessKeyTableByKeyAll(String strKey) {
        try {
            if (!AccessKeyMap.containsKey(strKey)) {
                LOG("[getAccessKeyTableByKeyAll] No Key [" + strKey + "]");
                return null;
            }
            AccessKeyInfo KeyInfo = AccessKeyMap.get(strKey);
            LOG("[getAccessKeyTableByKeyAll] KeyType [" + KeyInfo.getKeyType() + "]");
            if (KeyInfo.getKeyType().equalsIgnoreCase(AccessValues.hkey)) {
                // 세대키
                String strDong = KeyInfo.getDong();
                String strHo = KeyInfo.getHo();
                Iterator<String> mIterator = AccessKeyMap.keySet().iterator();
                AccessKeyInfoTable Result = new AccessKeyInfoTable();
                AccessKeyInfo ResultKeyInfo;
                String strResultKey;
                int nIndex = 0;
                while (mIterator.hasNext()) {
                    strResultKey = (String)mIterator.next();
                    ResultKeyInfo = AccessKeyMap.get(strResultKey);
                    if (ResultKeyInfo.getKeyType().equalsIgnoreCase(AccessValues.hkey) && ResultKeyInfo.getDong().equalsIgnoreCase(strDong) && ResultKeyInfo.getHo().equalsIgnoreCase(strHo)) {
                        nIndex++;
                        Result.addAccessKeyInfo(Integer.toString(nIndex), KeyInfo.getSN(), KeyInfo.getUID(), KeyInfo.getKeyType(), KeyInfo.getSitecode(), KeyInfo.getDong(), KeyInfo.getHo(),
                                                             KeyInfo.getKeyNo(), KeyInfo.getIssueCnt(), KeyInfo.getLastUseDate(), KeyInfo.getLastUseDate(), KeyInfo.getMessage());
                    }
                }
                LOG("[getAccessKeyTableByKeyAll (hkey)] Result.getCnt [" + Result.getCnt() + "], nIndex [" + nIndex + "]");
                if (Result.getCnt() == nIndex) return Result;
                else return null;
            }
            else if (KeyInfo.getKeyType().equalsIgnoreCase(AccessValues.dkey)) {
                // 동관리자키
                String strDong = KeyInfo.getDong();
                String strHo = "0";
                Iterator<String> mIterator = AccessKeyMap.keySet().iterator();
                AccessKeyInfoTable Result = new AccessKeyInfoTable();
                AccessKeyInfo ResultKeyInfo;
                String strResultKey;
                int nIndex = 0;
                while (mIterator.hasNext()) {
                    strResultKey = (String)mIterator.next();
                    ResultKeyInfo = AccessKeyMap.get(strResultKey);
                    if (ResultKeyInfo.getKeyType().equalsIgnoreCase(AccessValues.dkey) && ResultKeyInfo.getDong().equalsIgnoreCase(strDong) && ResultKeyInfo.getHo().equalsIgnoreCase(strHo)) {
                        nIndex++;
                        Result.addAccessKeyInfo(Integer.toString(nIndex), KeyInfo.getSN(), KeyInfo.getUID(), KeyInfo.getKeyType(), KeyInfo.getSitecode(), KeyInfo.getDong(), KeyInfo.getHo(),
                                                             KeyInfo.getKeyNo(), KeyInfo.getIssueCnt(), KeyInfo.getLastUseDate(), KeyInfo.getLastUseDate(), KeyInfo.getMessage());
                    }
                }
                LOG("[getAccessKeyTableByKeyAll (dkey)] Result.getCnt [" + Result.getCnt() + "], nIndex [" + nIndex + "]");
                if (Result.getCnt() == nIndex) return Result;
                else return null;
            }
            else if (KeyInfo.getKeyType().equalsIgnoreCase(AccessValues.mkey)) {
                // 전체관리자키
                String strDong = "0";
                String strHo = "0";
                Iterator<String> mIterator = AccessKeyMap.keySet().iterator();
                AccessKeyInfoTable Result = new AccessKeyInfoTable();
                AccessKeyInfo ResultKeyInfo;
                String strResultKey;
                int nIndex = 0;
                while (mIterator.hasNext()) {
                    strResultKey = (String)mIterator.next();
                    ResultKeyInfo = AccessKeyMap.get(strResultKey);
                    if (ResultKeyInfo.getKeyType().equalsIgnoreCase(AccessValues.mkey) && ResultKeyInfo.getDong().equalsIgnoreCase(strDong) && ResultKeyInfo.getHo().equalsIgnoreCase(strHo)) {
                        nIndex++;
                        Result.addAccessKeyInfo(Integer.toString(nIndex), KeyInfo.getSN(), KeyInfo.getUID(), KeyInfo.getKeyType(), KeyInfo.getSitecode(), KeyInfo.getDong(), KeyInfo.getHo(),
                                                             KeyInfo.getKeyNo(), KeyInfo.getIssueCnt(), KeyInfo.getLastUseDate(), KeyInfo.getLastUseDate(), KeyInfo.getMessage());
                    }
                }
                LOG("[getAccessKeyTableByKeyAll (mkey)] Result.getCnt [" + Result.getCnt() + "], nIndex [" + nIndex + "]");
                if (Result.getCnt() == nIndex) return Result;
                else return null;
            }
            return null;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getAccessKeyTableByKeyAll(strKey)");
            e.printStackTrace();
            return null;
        }
    }

    // AccessKeyInfoTable 가져오기 by SN (중복가능, 중복시 검색되는 모든 정보반환)
    private static AccessKeyInfoTable getAccessKeyTableBySN(String strSN) {
        try {
            Iterator<String> mIterator = AccessKeyMap.keySet().iterator();
            AccessKeyInfoTable Result = new AccessKeyInfoTable();
            AccessKeyInfo KeyInfo;
            String strKey;
            int nIndex = 0;
            while (mIterator.hasNext()) {
                strKey = (String)mIterator.next();
                KeyInfo = AccessKeyMap.get(strKey);
                if (KeyInfo.getSN().equalsIgnoreCase(strSN)) {
                    nIndex++;
                    Result.addAccessKeyInfo(Integer.toString(nIndex), KeyInfo.getSN(), KeyInfo.getUID(), KeyInfo.getKeyType(), KeyInfo.getSitecode(), KeyInfo.getDong(), KeyInfo.getHo(),
                                                         KeyInfo.getKeyNo(), KeyInfo.getIssueCnt(), KeyInfo.getLastUseDate(), KeyInfo.getLastUseDate(), KeyInfo.getMessage());
                }
            }
            LOG("[getAccessKeyTableBySN] Result.getCnt [" + Result.getCnt() + "], nIndex [" + nIndex + "]");
            if (Result.getCnt() == nIndex) return Result;
            else return null;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getAccessKeyTableBySN(strSN)");
            e.printStackTrace();
            return null;
        }
    }

    // AccessKeyInfoTable 가져오기 by SN (중복가능, 중복시 첫번째 검색되는 SN을 기반으로 그룹정보 반환)
    // SN값이 소속된 그룹(동일세대, 동관리자, 단지관리자)의 Key정보를 모두 가져온다.
    private static AccessKeyInfoTable getAccessKeyTableBySNAll(String strSN) {
        try {
            AccessKeyInfo KeyInfo = getAccessKeyBySN(strSN);
            LOG("[getAccessKeyTableBySNAll] KeyType [" + KeyInfo.getKeyType() + "]");
            if (KeyInfo.getKeyType().equalsIgnoreCase(AccessValues.hkey)) {
                String strDong = KeyInfo.getDong();
                String strHo = KeyInfo.getHo();
                Iterator<String> mIterator = AccessKeyMap.keySet().iterator();
                AccessKeyInfoTable Result = new AccessKeyInfoTable();
                AccessKeyInfo ResultKeyInfo;
                String strResultKey;
                int nIndex = 0;
                while (mIterator.hasNext()) {
                    strResultKey = (String)mIterator.next();
                    ResultKeyInfo = AccessKeyMap.get(strResultKey);
                    if (ResultKeyInfo.getKeyType().equalsIgnoreCase(AccessValues.hkey) && ResultKeyInfo.getDong().equalsIgnoreCase(strDong) && ResultKeyInfo.getHo().equalsIgnoreCase(strHo)) {
                        nIndex++;
                        Result.addAccessKeyInfo(Integer.toString(nIndex), KeyInfo.getSN(), KeyInfo.getUID(), KeyInfo.getKeyType(), KeyInfo.getSitecode(), KeyInfo.getDong(), KeyInfo.getHo(),
                                                             KeyInfo.getKeyNo(), KeyInfo.getIssueCnt(), KeyInfo.getLastUseDate(), KeyInfo.getLastUseDate(), KeyInfo.getMessage());
                    }
                }
                LOG("[getAccessKeyTableBySNAll (hkey)] Result.getCnt [" + Result.getCnt() + "], nIndex [" + nIndex + "]");
                if (Result.getCnt() == nIndex) return Result;
                else return null;
            }
            else if (KeyInfo.getKeyType().equalsIgnoreCase(AccessValues.dkey)) {
                String strDong = KeyInfo.getDong();
                String strHo = "0";
                Iterator<String> mIterator = AccessKeyMap.keySet().iterator();
                AccessKeyInfoTable Result = new AccessKeyInfoTable();
                AccessKeyInfo ResultKeyInfo;
                String strResultKey;
                int nIndex = 0;
                while (mIterator.hasNext()) {
                    strResultKey = (String)mIterator.next();
                    ResultKeyInfo = AccessKeyMap.get(strResultKey);
                    if (ResultKeyInfo.getKeyType().equalsIgnoreCase(AccessValues.dkey) && ResultKeyInfo.getDong().equalsIgnoreCase(strDong) && ResultKeyInfo.getHo().equalsIgnoreCase(strHo)) {
                        nIndex++;
                        Result.addAccessKeyInfo(Integer.toString(nIndex), KeyInfo.getSN(), KeyInfo.getUID(), KeyInfo.getKeyType(), KeyInfo.getSitecode(), KeyInfo.getDong(), KeyInfo.getHo(),
                                                             KeyInfo.getKeyNo(), KeyInfo.getIssueCnt(), KeyInfo.getLastUseDate(), KeyInfo.getLastUseDate(), KeyInfo.getMessage());
                    }
                }
                LOG("[getAccessKeyTableBySNAll (dkey)] Result.getCnt [" + Result.getCnt() + "], nIndex [" + nIndex + "]");
                if (Result.getCnt() == nIndex) return Result;
                else return null;
            }
            else if (KeyInfo.getKeyType().equalsIgnoreCase(AccessValues.mkey)) {
                String strDong = "0";
                String strHo = "0";
                Iterator<String> mIterator = AccessKeyMap.keySet().iterator();
                AccessKeyInfoTable Result = new AccessKeyInfoTable();
                AccessKeyInfo ResultKeyInfo;
                String strResultKey;
                int nIndex = 0;
                while (mIterator.hasNext()) {
                    strResultKey = (String)mIterator.next();
                    ResultKeyInfo = AccessKeyMap.get(strResultKey);
                    if (ResultKeyInfo.getKeyType().equalsIgnoreCase(AccessValues.mkey) && ResultKeyInfo.getDong().equalsIgnoreCase(strDong) && ResultKeyInfo.getHo().equalsIgnoreCase(strHo)) {
                        nIndex++;
                        Result.addAccessKeyInfo(Integer.toString(nIndex), KeyInfo.getSN(), KeyInfo.getUID(), KeyInfo.getKeyType(), KeyInfo.getSitecode(), KeyInfo.getDong(), KeyInfo.getHo(),
                                                             KeyInfo.getKeyNo(), KeyInfo.getIssueCnt(), KeyInfo.getLastUseDate(), KeyInfo.getLastUseDate(), KeyInfo.getMessage());
                    }
                }
                LOG("[getAccessKeyTableBySNAll (mkey)] Result.getCnt [" + Result.getCnt() + "], nIndex [" + nIndex + "]");
                if (Result.getCnt() == nIndex) return Result;
                else return null;
            }
            return null;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getAccessKeyTableBySNAll(strSN)");
            e.printStackTrace();
            return null;
        }
    }

    // 세대용 AccessKeyInfoTable 가져오기 by Dong & Ho
    // 세대에 속한 모든 키정보를 가져온다.
    private static AccessKeyInfoTable getAccessKeyTableByResi(String strDong, String strHo) {
        try {
            Iterator<String> mIterator = AccessKeyMap.keySet().iterator();
            AccessKeyInfoTable Result = new AccessKeyInfoTable();
            AccessKeyInfo KeyInfo;
            String strKey;
            int nIndex = 0;
            while (mIterator.hasNext()) {
                strKey = (String)mIterator.next();
                KeyInfo = AccessKeyMap.get(strKey);
                if (KeyInfo.getKeyType().equalsIgnoreCase(AccessValues.hkey) && KeyInfo.getDong().equalsIgnoreCase(strDong) && KeyInfo.getHo().equalsIgnoreCase(strHo)) {
                    nIndex++;
                    Result.addAccessKeyInfo(Integer.toString(nIndex), KeyInfo.getSN(), KeyInfo.getUID(), KeyInfo.getKeyType(), KeyInfo.getSitecode(), KeyInfo.getDong(), KeyInfo.getHo(),
                                                         KeyInfo.getKeyNo(), KeyInfo.getIssueCnt(), KeyInfo.getLastUseDate(), KeyInfo.getLastUseDate(), KeyInfo.getMessage());
                }
            }
            LOG("[getAccessKeyTableByResi] Result.getCnt [" + Result.getCnt() + "], nIndex [" + nIndex + "]");
            if (Result.getCnt() == nIndex) return Result;
            else return null;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getAccessKeyTableByResi(strDong, strHo)");
            e.printStackTrace();
            return null;
        }
    }

    private static boolean initAccessKeyTest() {
        try {
            LOG("[initAccessKeyTest] ===== START =====");
            long start; long end; long difference;
            start = System.currentTimeMillis();
            AccessKeyInfo AccessKeyTest = new AccessKeyInfo();
            for (int i = 0; i < 10000; i++) {
                String TestUID = "FDE1F1C280" + String.format("%05X", i);
//                LOG("[initAccessKeyTest] TestUID! [" + TestUID + "]");
                if (TestUID.equalsIgnoreCase("FDE1F1C28000079")) {
                    AccessKeyTest.setValue("12345678", "102", "103", AccessValues.hkey, TestUID, TestUID, TestUID, "1", "1", "2015.03.01", "2015.3.30", "2015.03.15", "", "", "");
                }
                else {
                    AccessKeyTest.setValue("12345678", "102", "102", AccessValues.hkey, TestUID, TestUID, TestUID, "1", "1", "2015.03.01", "2015.3.30", "2015.03.15", "", "", "");
                }
                if (!addAccessKey(AccessKeyTest)) return false;
            }
            end = System.currentTimeMillis();
            difference = end - start;
            LOG("[initAccessKeyTest] start [" + start + "], end [" + end + "], difference [" + difference + "]");
            LOG("[initAccessKeyTest] ===== END =====");
            return true;
        } catch (Exception e) {
            Log.e(TAG, "[Exception] AccessKeyTest()");
            e.printStackTrace();
            return false;
        }
    }

    public static void getAccessKeyTestFromMemory() {
        try {
            LOG("[getAccessKeyTestFromMemory] ===== START =====");
            long start; long end; long difference;
            start = System.currentTimeMillis();
            MainActivity.RFKeyDBManager.getKeyByValue(Declare.DBInfo.TABLE_NAME_RFKEY, "HO", "103");
            AccessKeyInfo result = AccessKeyMap.get("FDE1F1C2800000000");
            LOG("[getAccessKeyTestFromMemory] Sitecode [" + result.getSitecode() + "], Dong [" + result.getDong() + "], Ho [" + result.getHo() + "], KeyType [" + result.getKeyType() + "], " +
            		                                                "UID [" + result.getUID() + "], CID [" + result.getCID() + "], SN [" + result.getSN() + "], KeyNo [" + result.getKeyNo() + "], IssueCnt [" + result.getIssueCnt() + "], " +
                                                                    "IssuedDate [" + result.getIssuedDate() + "], ExpireDate [" + result.getExpireDate() + "], LastUseDate [" + result.getLastUseDate() + "], Name [" + result.getName() + "], " +
                                                                    "Message [" + result.getMessage() + "], ETC [" + result.getETC() + "]");
            end = System.currentTimeMillis();
            difference = end - start;
            LOG("[getAccessKeyTestFromMemory] start [" + start + "], end [" + end + "], difference [" + difference + "]");
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getAccessKeyTestFromMemory()");
            e.printStackTrace();
        }
    }

    public static void getAccessKeyTestFromDB() {
        try {
            LOG("[getAccessKeyTestFromDB] ===== START =====");
            long start; long end; long difference;
            start = System.currentTimeMillis();
            AccessKeyInfo result = AccessKeyMap.get("FDE1F1C2800000000");
            LOG("[getAccessKeyTestFromDB] Sitecode [" + result.getSitecode() + "], Dong [" + result.getDong() + "], Ho [" + result.getHo() + "], KeyType [" + result.getKeyType() + "], " +
                                                           "UID [" + result.getUID() + "], CID [" + result.getCID() + "], SN [" + result.getSN() + "], KeyNo [" + result.getKeyNo() + "], IssueCnt [" + result.getIssueCnt() + "], " +
                                                           "IssuedDate [" + result.getIssuedDate() + "], ExpireDate [" + result.getExpireDate() + "], LastUseDate [" + result.getLastUseDate() + "], Name [" + result.getName() + "], " +
                                                           "Message [" + result.getMessage() + "], ETC [" + result.getETC() + "]");
            end = System.currentTimeMillis();
            difference = end - start;
            LOG("[getAccessKeyTestFromDB] start [" + start + "], end [" + end + "], difference [" + difference + "]");
        } catch (Exception e) {
            Log.e(TAG, "[Exception] getAccessKeyTestFromDB()");
            e.printStackTrace();
        }
    }

    */
    }
    // 사용하지 않는 코드를 정리함 (주석처리가 묶이지 않아서 함수로 포장함, JEFF, 2015.03.31)


}