Beispiel #1
0
 public CategoryItm(string CategoryId, string CategoryName, CategoryItm parent = null)
 {
     this.gl_catItms = new List<CategoryItm>();
     this.gl_parent = parent;
     this.gl_catId = CategoryId;
     this.gl_catName = CategoryName;
 }
 public bool AddCategoryLink(CategoryItm catItm)
 {
     try
     {
         if (!this.gl_catIdLst.ContainsKey(catItm.CategoryId))
         {
             this.gl_catIdLst.Add(catItm.CategoryId, catItm);
             return true;
         }
     }
     catch
     {
     }
     return false;
 }
        public bool LoadAllSourceNodes(TreeViewItem tItm, CategoryItm xContainer, string parentId = "")
        {
            try
            {
                var categoryQuery = from category in xContainer.Items
                                    select category;

                foreach (var category in categoryQuery)
                {
                    string CategoryId = category.CategoryId;
                    string CategoryName = category.CategoryName;
                    CategoryIdDevice catDev = new CategoryIdDevice(CategoryId, CategoryName, false, parentId);
                    catDev.CategorySelected += new CategoryUnOrSelectedEventHandler(catDev_CategorySelected);
                    TreeViewItem tItm2 = new TreeViewItem();
                    tItm2.Selected += new RoutedEventHandler(tItm_Selected);
                    tItm2.Header = catDev;
                    tItm.Items.Add(tItm2);
                    this.gl_catDevLst.Add(CategoryId, catDev);

                    if (category.Items.Count > 0)
                    {
                        LoadAllSourceNodes(tItm2, category, CategoryId);
                    }
                }
                return true;
            }
            catch
            {
            }
            return false;
        }
Beispiel #4
0
        private static void FindNextNodeC(CategoryItm catItm, string[] path, int cLevel, List<string> list, ref bool foundAllPaths)
        {
            for (int i = 0; i < catItm.Items.Count; i++)
            {
                if (catItm.Items[i].CategoryName == path[cLevel])
                {
                    list.Add(catItm.Items[i].CategoryId);
                    if (cLevel == (path.Length - 1))
                    {
                        foundAllPaths = true;
                        break;
                    }

                    FindNextNodeC(catItm.Items[i], path, (cLevel + 1), list, ref foundAllPaths);
                }
            }
        }
Beispiel #5
0
        public static List<string> BuildZenCartPathIfNotExist(CategoryContainer catContainer, string path, string Host, string Db, string User,
            string Pwd, bool appendToFirstFound = true)
        {
            MySqlConnection mCon1 = null;

            try
            {
                string conStr = string.Format("server={0};database={1};user={2};password={3}", Host, Db, User, Pwd);
                mCon1 = new MySqlConnection(conStr);
                mCon1.Open();

                string[] wPath = path.Split(new Char[] { '/' });

                if (wPath.Length < 2)
                {
#if DEBUG
                    System.Diagnostics.Debug.WriteLine("The path does not contain a path long enough to travel down.");
#endif
                    return null;
                }

                List<string> tmpList = CategoryMgr.GetCategoryIdList(catContainer, path);
                if (tmpList == null)
                {
                    if (appendToFirstFound == true)
                    {
                        // The path does not exist. Attempt to build it.
                        CategoryItm lItm = null;
                        bool c = false;
                        string cPath = wPath[0];
                        //for (int i = 1; i < wPath.Length; i++)
                        //{
                        cPath += "/" + wPath[1];
                        List<string> tmpL = CategoryMgr.GetCategoryIdList(catContainer, cPath);
                        List<string> cTmpL = null;
                        int offset = 1;
                        while (tmpL != null)
                        {
                            cTmpL = tmpL;
                            offset++;
                            cPath += "/" + wPath[offset];
                            tmpL = CategoryMgr.GetCategoryIdList(catContainer, cPath);
                        }

                        if (cTmpL != null)
                        {
                            string Id = (cTmpL[cTmpL.Count - 1]).ToString();
                            CategoryItm catItm = catContainer.IdLinkList[Id];

                            // Ok, so we know the most bottom level.
                            CategoryItm catItm2 = null;
                            for (int i2 = offset; i2 < wPath.Length; i2++)
                            {
                                if (catItm2 == null)
                                {
                                    MySqlCommand mCmd1 = new MySqlCommand(string.Format("INSERT INTO categories(parent_id, categories_status) VALUES('{0}', '1');", catItm.CategoryId), mCon1);
                                    mCmd1.ExecuteNonQuery();
                                    MySqlCommand mCmd2 = new MySqlCommand(string.Format("INSERT INTO categories_description(categories_id, categories_name) VALUES('{0}', '{1}');",
                                        mCmd1.LastInsertedId.ToString(), wPath[i2]), mCon1);
                                    mCmd2.ExecuteNonQuery();

                                    CategoryItm catItm3 = new CategoryItm(mCmd1.LastInsertedId.ToString(), wPath[i2], catItm);
                                    if (!catContainer.IdLinkList.ContainsKey(catItm3.CategoryId))
                                    {
                                        catContainer.IdLinkList.Add(catItm3.CategoryId, catItm3);
                                    }
                                    catItm.Items.Add(catItm3);
                                    catItm2 = catItm3;
                                    mCmd1.Dispose();
                                    mCmd1 = null;
                                    mCmd2.Dispose();
                                    mCmd2 = null;
                                }
                                else
                                {
                                    MySqlCommand mCmd1 = new MySqlCommand(string.Format("INSERT INTO categories(parent_id, categories_status) VALUES('{0}', '1');", catItm2.CategoryId), mCon1);
                                    mCmd1.ExecuteNonQuery();
                                    MySqlCommand mCmd2 = new MySqlCommand(string.Format("INSERT INTO categories_description(categories_id, categories_name) VALUES('{0}', '{1}');",
                                        mCmd1.LastInsertedId.ToString(), wPath[i2]), mCon1);
                                    mCmd2.ExecuteNonQuery();

                                    CategoryItm catItm3 = new CategoryItm(mCmd1.LastInsertedId.ToString(), wPath[i2], catItm2);
                                    if (!catContainer.IdLinkList.ContainsKey(catItm3.CategoryId))
                                    {
                                        catContainer.IdLinkList.Add(catItm3.CategoryId, catItm3);
                                    }
                                    catItm2.Items.Add(catItm3);
                                    catItm2 = catItm3;
                                }
                            }
                            tmpList = CategoryMgr.GetCategoryIdList(catContainer, path);
                        }
                        else
                        {
                            // Ok, so we know that we can't find a partial list to go by,
                            // lets start by creating a whole new one.

                            bool foundRootCat = false;
                            string rootCatId = "";
                            // try one last attempt to see if there is a root category available.
                            var rootQuery = from CategoryItm catItm in catContainer.Items
                                            where catItm.CategoryName == wPath[0]
                                            select catItm;
                            foreach (var category in rootQuery)
                            {
                                foundRootCat = true;
                                rootCatId = category.CategoryId;
                                break;
                            }

                            MySqlCommand mCmd1 = null;
                            if (foundRootCat == false)
                            {
                                mCmd1 = new MySqlCommand("INSERT INTO categories(categories_status) VALUES('1');", mCon1);
                                mCmd1.ExecuteNonQuery();
                                MySqlCommand mCmd2 = new MySqlCommand(string.Format("INSERT INTO categories_description(categories_id, categories_name) VALUES('{0}', '{1}');",
                                    mCmd1.LastInsertedId.ToString(), wPath[0]), mCon1);
                                mCmd2.ExecuteNonQuery();

                                rootCatId = mCmd1.LastInsertedId.ToString();

                                mCmd1.Dispose();
                                mCmd1 = null;
                                mCmd2.Dispose();
                                mCmd2 = null;
                            }
                            else
                            {
                                //
                            }

                            CategoryItm pCatItm = new CategoryItm(rootCatId, wPath[0]);
                            catContainer.Items.Add(pCatItm);
                            if (!catContainer.IdLinkList.ContainsKey(pCatItm.CategoryId))
                            {
                                catContainer.IdLinkList.Add(pCatItm.CategoryId, pCatItm);
                            }
                            for (int i2 = 1; i2 < wPath.Length; i2++)
                            {
                                MySqlCommand mCmd3 = new MySqlCommand(string.Format("INSERT INTO categories(parent_id, categories_status) VALUES('{0}', '1');", pCatItm.CategoryId), mCon1);
                                mCmd3.ExecuteNonQuery();
                                MySqlCommand mCmd4 = new MySqlCommand(string.Format("INSERT INTO categories_description(categories_id, categories_name) VALUES('{0}', '{1}');",
                                    mCmd3.LastInsertedId.ToString(), wPath[i2]), mCon1);
                                mCmd4.ExecuteNonQuery();

                                CategoryItm catItm = new CategoryItm(mCmd3.LastInsertedId.ToString(), wPath[i2], pCatItm);
                                pCatItm.Items.Add(catItm);
                                if (!catContainer.IdLinkList.ContainsKey(catItm.CategoryId))
                                {
                                    catContainer.IdLinkList.Add(catItm.CategoryId, catItm);
                                }
                                mCmd3.Dispose();
                                mCmd3 = null;
                                mCmd4.Dispose();
                                mCmd4 = null;
                                pCatItm = catItm;
                            }

                            if (mCon1 != null)
                            {
                                mCon1.Close();
                                mCon1.Dispose();
                                mCon1 = null;
                            }
                            return (tmpList = CategoryMgr.GetCategoryIdList(catContainer, path));
                        }

                        if (mCon1 != null)
                        {
                            mCon1.Close();
                            mCon1.Dispose();
                            mCon1 = null;
                        }
                        return tmpList;
                    }
                    else
                    {
                        MySqlCommand mCmd1 = new MySqlCommand("INSERT INTO categories(categories_status) VALUES('1');", mCon1);
                        mCmd1.ExecuteNonQuery();
                        MySqlCommand mCmd2 = new MySqlCommand(string.Format("INSERT INTO categories_description(categories_id, categories_name) VALUES('{0}', '{1}');",
                            mCmd1.LastInsertedId.ToString(), wPath[0]), mCon1);
                        mCmd2.ExecuteNonQuery();

                        CategoryItm pCatItm = new CategoryItm(mCmd1.LastInsertedId.ToString(), wPath[0]);
                        catContainer.Items.Add(pCatItm);
                        catContainer.IdLinkList.Add(pCatItm.CategoryId, pCatItm);
                        mCmd1.Dispose();
                        mCmd1 = null;
                        mCmd2.Dispose();
                        mCmd2 = null;
                        for (int i = 1; i < wPath.Length; i++)
                        {
                            MySqlCommand mCmd3 = new MySqlCommand(string.Format("INSERT INTO categories(parent_id, categories_status) VALUES('{0}', '1');", pCatItm.CategoryId), mCon1);
                            mCmd3.ExecuteNonQuery();
                            MySqlCommand mCmd4 = new MySqlCommand(string.Format("INSERT INTO categories_description(categories_id, categories_name) VALUES('{0}', '{1}');",
                                mCmd3.LastInsertedId.ToString(), wPath[i]), mCon1);
                            mCmd4.ExecuteNonQuery();

                            CategoryItm catItm = new CategoryItm(mCmd3.LastInsertedId.ToString(), wPath[i], pCatItm);
                            pCatItm.Items.Add(catItm);
                            if (!catContainer.IdLinkList.ContainsKey(catItm.CategoryId))
                            {
                                catContainer.IdLinkList.Add(catItm.CategoryId, catItm);
                            }
                            mCmd3.Dispose();
                            mCmd3 = null;
                            mCmd4.Dispose();
                            mCmd4 = null;
                            pCatItm = catItm;
                        }

                        if (mCon1 != null)
                        {
                            mCon1.Close();
                            mCon1.Dispose();
                            mCon1 = null;
                        }
                        return (tmpList = CategoryMgr.GetCategoryIdList(catContainer, path));
                    }
                    
                    //catContainer.Items.Add
                }
                else
                {
                    return tmpList;
                }
            }
#if DEBUG
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Error while trying to build the path. Error: {0}", e.ToString());
            }
#else
            catch
            {
            }
#endif
            return null;
        }
Beispiel #6
0
        public static List<string> BuildPathIfNotExist(CategoryContainer catContainer, string path, bool appendToFirstFound = true)
        {
            try
            {
                string[] wPath = path.Split(new Char[] { '/' });

                if (wPath.Length < 2)
                {
#if DEBUG
                    System.Diagnostics.Debug.WriteLine("The path does not contain a path long enough to travel down.");
#endif
                    return null;
                }

                List<string> tmpList = CategoryMgr.GetCategoryIdList(catContainer, path);
                if (tmpList == null)
                {
                    if (appendToFirstFound == true)
                    {
                        // The path does not exist. Attempt to build it.
                        CategoryItm lItm = null;
                        string cPath = wPath[0];
                        //for (int i = 1; i < wPath.Length; i++)
                        //{
                            cPath += "/" + wPath[1];
                            List<string> tmpL = CategoryMgr.GetCategoryIdList(catContainer, cPath);
                            List<string> cTmpL = null;
                            int offset = 1;
                            while (tmpL != null)
                            {
                                cTmpL = tmpL;
                                offset++;
                                cPath += "/" + wPath[offset];
                                tmpL = CategoryMgr.GetCategoryIdList(catContainer, cPath);
                            }

                            if (cTmpL != null)
                            {
                                string Id = (cTmpL[cTmpL.Count - 1]).ToString();
                                CategoryItm catItm = catContainer.IdLinkList[Id];

                                // Ok, so we know the most bottom level.
                                CategoryItm catItm2 = null;
                                for (int i2 = offset; i2 < wPath.Length; i2++)
                                {
                                    if (catItm2 == null)
                                    {
                                        CategoryItm catItm3 = new CategoryItm("{" + Guid.NewGuid().ToString() + "}", wPath[i2], catItm);
                                        if (!catContainer.IdLinkList.ContainsKey(catItm3.CategoryId))
                                        {
                                            catContainer.IdLinkList.Add(catItm3.CategoryId, catItm3);
                                        }
                                        catItm.Items.Add(catItm3);
                                        catItm2 = catItm3;
                                    }
                                    else
                                    {
                                        CategoryItm catItm3 = new CategoryItm("{" + Guid.NewGuid().ToString() + "}", wPath[i2], catItm2);
                                        if (!catContainer.IdLinkList.ContainsKey(catItm3.CategoryId))
                                        {
                                            catContainer.IdLinkList.Add(catItm3.CategoryId, catItm3);
                                        }
                                        catItm2.Items.Add(catItm3);
                                        catItm2 = catItm3;
                                    }
                                }
                                tmpList = CategoryMgr.GetCategoryIdList(catContainer, path);
                            }
                            else
                            {
                                // Ok, so we know that we can't find a partial list to go by,
                                // lets start by creating a whole new one.

                                CategoryItm pCatItm = null;
                                bool foundRootCat = false;
                                string rootCatId = "";
                                // try one last attempt to see if there is a root category available.
                                var rootQuery = from CategoryItm catItm in catContainer.Items
                                                where catItm.CategoryName == wPath[0]
                                                select catItm;
                                foreach (var category in rootQuery)
                                {
                                    foundRootCat = true;
                                    rootCatId = category.CategoryId;
                                    break;
                                }

                                if (foundRootCat == false)
                                {
                                    pCatItm = new CategoryItm("{" + Guid.NewGuid().ToString() + "}", wPath[0]);
                                    catContainer.Items.Add(pCatItm);
                                }
                                else
                                {
                                    pCatItm = catContainer.IdLinkList[rootCatId];
                                }

                                catContainer.Items.Add(pCatItm);
                                if (!catContainer.IdLinkList.ContainsKey(pCatItm.CategoryId))
                                {
                                    catContainer.IdLinkList.Add(pCatItm.CategoryId, pCatItm);
                                }
                                for (int i2 = 1; i2 < wPath.Length; i2++)
                                {
                                    CategoryItm catItm = new CategoryItm(Guid.NewGuid().ToString(), wPath[i2], pCatItm);
                                    pCatItm.Items.Add(catItm);
                                    if (!catContainer.IdLinkList.ContainsKey(catItm.CategoryId))
                                    {
                                        catContainer.IdLinkList.Add(catItm.CategoryId, catItm);
                                    }
                                    pCatItm = catItm;
                                }

                                return (tmpList = CategoryMgr.GetCategoryIdList(catContainer, path));
                            }
                        return tmpList;
                    }
                    else
                    {
                        CategoryItm pCatItm = new CategoryItm("{" + Guid.NewGuid().ToString() + "}", wPath[0]);
                        catContainer.Items.Add(pCatItm);
                        catContainer.IdLinkList.Add(pCatItm.CategoryId, pCatItm);
                        for (int i = 1; i < wPath.Length; i++)
                        {
                            CategoryItm catItm = new CategoryItm("{" + Guid.NewGuid().ToString() + "}", wPath[i], pCatItm);
                            pCatItm.Items.Add(catItm);
                            if (!catContainer.IdLinkList.ContainsKey(catItm.CategoryId))
                            {
                                catContainer.IdLinkList.Add(catItm.CategoryId, catItm);
                            }
                            pCatItm = catItm;
                        }
                        return (tmpList = CategoryMgr.GetCategoryIdList(catContainer, path));
                    }
                    
                    //catContainer.Items.Add
                }
                else
                {
                    return tmpList;
                }
            }
#if DEBUG
            catch(Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Error while trying to build the path. Error: {0}", e.ToString());
            }
#else
            catch
            {
            }
#endif
            return null;
        }
Beispiel #7
0
        public bool AddCategory(string categoryName, string categoryId = "", string parentId = "{ROOT}")
        {
            try
            {
                string catId = null;

                if (categoryId == "")
                {
                    catId = "{" + Guid.NewGuid().ToString() + "}";
                }
                else
                {
                    catId = categoryId;
                }

                if (parentId != "{ROOT}")
                {
                    CategoryItm catItm = null;

#if DEBUG
                    System.Diagnostics.Debug.WriteLine("Parent id is set. Attempting to locate the CategoryItm object. Parent Id: {0}", parentId);
#endif
                    if (this.gl_container.IdLinkList.TryGetValue(parentId, out catItm) == true)
                    {
#if DEBUG
                        System.Diagnostics.Debug.WriteLine("Found the CategoryItm parent id.");
#endif
                        CategoryItm catItm2 = new CategoryItm(catId, categoryName, catItm);
                        catItm.Items.Add(catItm2);
                        this.gl_container.IdLinkList.Add(catItm2.CategoryId, catItm2);
                        return true;
                    }
#if DEBUG
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("Did not find the parent id.");
                    }
#endif
                }
                else
                {
                    CategoryItm catItm2 = new CategoryItm(catId, categoryName);
                    this.gl_container.Items.Add(catItm2);
                    this.gl_container.IdLinkList.Add(catItm2.CategoryId, catItm2);
                    return true;
                }
            }
            catch
            {
            }
            return false;
        }
Beispiel #8
0
        public bool LoadAllNodes(CategoryItm tItm, XContainer xContainer, string parentId = "")
        {
            try
            {
                var categoryQuery = from category in xContainer.Elements("Category")
                                    select category;

                foreach (var category in categoryQuery)
                {
                    string CategoryId = category.Attribute("Id").Value.ToString();
                    string CategoryName = category.Attribute("CategoryName").Value.ToString();
                    //CategoryIdDevice catDev = new CategoryIdDevice(CategoryId, CategoryName, false, parentId);
                    //catDev.CategorySelected += new CategoryUnOrSelectedEventHandler(catDev_CategorySelected);
                    CategoryItm tItm2 = new CategoryItm(CategoryId, CategoryName, tItm);
                    this.gl_container.AddCategoryLink(tItm2);
                    //tItm2.Selected += new RoutedEventHandler(tItm_Selected);
                    //tItm2.Header = catDev;
                    tItm.Items.Add(tItm2);
                    //this.gl_catDevLst.Add(CategoryId, catDev);

                    if (category.HasElements == true)
                    {
                        LoadAllNodes(tItm2, category, CategoryId);
                    }
                }
                return true;
            }
            catch
            {
            }
            return false;
        }
Beispiel #9
0
        public bool LoadInitCategories()
        {
            try
            {
                var categoryQuery = from category in this.gl_xDoc.Root.Element("CategoryRoot").Elements("Category")
                                    select category;
                foreach (var category in categoryQuery)
                {
                    string CategoryId = category.Attribute("Id").Value.ToString();
                    string CategoryName = category.Attribute("CategoryName").Value.ToString();
                    //CategoryIdDevice catDev = new CategoryIdDevice(CategoryId, CategoryName, false, "");
                    CategoryItm tItm = new CategoryItm(CategoryId, CategoryName, null);
                    //tItm.Header = catDev;
                    //(this.xCatTreeLst.Items[0] as TreeViewItem).Items.Add(tItm);
                    this.gl_container.Items.Add(tItm);
                    this.gl_container.AddCategoryLink(tItm);
                    //this.gl_catDevLst.Add(CategoryId, catDev);

                    if (category.HasElements == true)
                    {
                        LoadAllNodes(tItm, category, CategoryId);
                    }
                }
                return true;
            }
            catch
            {
            }
            return false;
        }
 private static void LoadZenNode(int parentId, CategoryItm catItm, DataSet dset_CatM, DataSet dset_CatMDesc, CategoryContainer container)
 {
     var categoryQuery = from DataRow category in dset_CatM.Tables["categories"].Rows
                         where category.Field<int>("parent_id") == parentId
                         select category;
     foreach (var category in categoryQuery)
     {
         string catName = "";
         var catDesc = from DataRow category2 in dset_CatMDesc.Tables["categories_description"].Rows
                       where category2.Field<int>("categories_id") == category.Field<int>("categories_id")
                       select category2;
         foreach (var category2 in catDesc)
         {
             catName = category2.Field<string>("categories_name").ToString();
             break;
         }
         string cId = category.Field<int>("categories_id").ToString();
         CategoryItm catItm2 = new CategoryItm(cId, catName, catItm);
         if (!container.IdLinkList.ContainsKey(cId))
         {
             container.IdLinkList.Add(cId, catItm2);
         }
         catItm.Items.Add(catItm2);
         LoadZenNode(int.Parse(catItm2.CategoryId), catItm2, dset_CatM, dset_CatMDesc, container);
     }
 }
 private static void LoadeBayStoreNode(string parentId, CategoryItm catItm, Dictionary<string, CategoryDef> catIdLst, CategoryContainer container)
 {
     var categoryQuery = from category in catIdLst
                         where category.Value.ParentId == parentId
                         select category;
     foreach (var category in categoryQuery)
     {
         string catName = category.Value.CategoryName;
         string cId = category.Value.CategoryId;
         CategoryItm catItm2 = new CategoryItm(cId, catName, catItm);
         if (!container.IdLinkList.ContainsKey(cId))
         {
             container.IdLinkList.Add(cId, catItm2);
         }
         catItm.Items.Add(catItm2);
         LoadeBayStoreNode(catItm2.CategoryId, catItm2, catIdLst, container);
     }
 }
        private static void GeteBayChildCategoriesRoot(StoreCustomCategoryType cat, CategoryItm catItm, int pId, Dictionary<string, CategoryDef> catDefIdLst, CategoryContainer container)
        {
            //continue the recursion for each of the child categories
            foreach (StoreCustomCategoryType childcat in cat.ChildCategory)
            {
                //get the category name, ID and whether it is a leaf
                int parentId = pId;
                long id = cat.CategoryID;
                string name = cat.Name;
                bool leaf = (cat.ChildCategory.Count == 0);
                Console.WriteLine("id = " + id + " name = " + name + " leaf= " + leaf);
                CategoryDef catDef = new CategoryDef(childcat.Name, childcat.CategoryID.ToString(), cat.CategoryID.ToString());
                try
                {
                    catDefIdLst.Add(childcat.CategoryID.ToString(), catDef);
                }
                catch
                {
                }

                string catName = name;
                // LoadNode(parent category id, container) -> find all cats call -> LoadNode(parent category id, container)
                //string pId = parentCategory.Key;
                CategoryItm catItm2 = new CategoryItm(childcat.CategoryID.ToString(), childcat.Name, catItm);
                if (!container.IdLinkList.ContainsKey(childcat.CategoryID.ToString()))
                {
                    container.IdLinkList.Add(childcat.CategoryID.ToString(), catItm2);
                }
                catItm.Items.Add(catItm2);

                GeteBayChildCategoriesRoot(childcat, catItm2, (int)id, catDefIdLst, container);
            }
        }
        public static CategoryContainer BuildCategoryFromeBay(bool storeCatBuild = false)
        {
            if (storeCatBuild == false)
            {
                CategoryContainer container = new CategoryContainer();

                apiContext = AppSettingHelper.GetApiContext();
                apiContext.ApiLogManager = new ApiLogManager();
                apiContext.ApiLogManager.ApiLoggerList.Add(new FileLogger("log.txt", true, true, true));
                apiContext.ApiLogManager.EnableLogging = true;
                apiContext.Site = SiteCodeType.US;

                GetCategoriesCall catCall = new GetCategoriesCall(apiContext)
                {
                    EnableCompression = true,
                    ViewAllNodes = true
                };

                catCall.DetailLevelList.Add(DetailLevelCodeType.ReturnAll);
                catCall.GetCategories();

                Dictionary<string, CategoryDef> catIdCMLst = new Dictionary<string, CategoryDef>();

                foreach (CategoryType category in catCall.CategoryList)
                {
                    int categoryId = Int32.Parse(category.CategoryID);
                    int? parentId = Int32.Parse(category.CategoryParentID[0]);
                    if (parentId == categoryId) { parentId = null; }

                    if (parentId == null) { catIdCMLst.Add(category.CategoryID, new CategoryDef(category.CategoryName, category.CategoryID)); }
                    else { catIdCMLst.Add(category.CategoryID, new CategoryDef(category.CategoryName, category.CategoryID, parentId.ToString())); }
                }

                // Lets get all parent categories so we can build down the tree.
                var parentQuery = from parentCategory in catIdCMLst
                                  where parentCategory.Value.ParentId == ""
                                  select parentCategory;
                foreach (var parentCategory in parentQuery)
                {
                    string catName = parentCategory.Value.CategoryName;
                    // LoadNode(parent category id, container) -> find all cats call -> LoadNode(parent category id, container)
                    string pId = parentCategory.Key;
                    CategoryItm catItm = new CategoryItm(pId, catName);
                    container.IdLinkList.Add(pId, catItm);
                    container.Items.Add(catItm);
                    LoadeBayNode(pId, catItm, catIdCMLst, container);
                }

                return container;
            }
            else
            {
                CategoryContainer container = new CategoryContainer();
                apiContext = AppSettingHelper.GetApiContext();
                apiContext.ApiLogManager = new ApiLogManager();
                apiContext.ApiLogManager.ApiLoggerList.Add(new FileLogger("log.txt", true, true, true));
                apiContext.ApiLogManager.EnableLogging = true;
                apiContext.Site = SiteCodeType.US;

                GetStoreCall getStoreCall = new GetStoreCall(apiContext) { EnableCompression = true };
                getStoreCall.DetailLevelList.Add(DetailLevelCodeType.ReturnAll);
                getStoreCall.CategoryStructureOnly = true;
                getStoreCall.Execute();

                StoreCustomCategoryType cType = new StoreCustomCategoryType();
                Dictionary<string, CategoryDef> catIdCMLst = new Dictionary<string, CategoryDef>();

                foreach (StoreCustomCategoryType category in getStoreCall.Store.CustomCategories)
                {
                    CategoryItm catItm = new CategoryItm(category.CategoryID.ToString(), category.Name);
                    container.IdLinkList.Add(category.CategoryID.ToString(), catItm);
                    container.Items.Add(catItm);
                    GeteBayChildCategoriesRoot(category, catItm, (int)category.CategoryID, catIdCMLst, container);
                    //container.Items.Add(catItm);
                }

                // Lets get all parent categories so we can build down the tree.
                /*var parentQuery = from parentCategory in catIdCMLst
                                  select parentCategory;
                foreach (var parentCategory in parentQuery)
                {
                    string catName = parentCategory.Value.CategoryName;
                    // LoadNode(parent category id, container) -> find all cats call -> LoadNode(parent category id, container)
                    string pId = parentCategory.Key;
                    CategoryItm catItm = new CategoryItm(pId, catName);
                    container.IdLinkList.Add(pId, catItm);
                    container.Items.Add(catItm);
                    LoadeBayNode(pId, catItm, catIdCMLst, container);
                }*/

                return container;
            }
            //}
            //catch
            //{
            //   return null;
            //}
        }
        public static CategoryContainer BuildCategoryFromZenCart(string Host, string Db, string User, string Password)
        {
            CategoryContainer container = new CategoryContainer();
            string conStr = string.Format("server={0};database={1};user={2};password={3}", Host, Db, User, Password);
            MySqlConnection mCon = null;

            //try
            //{
                mCon = new MySqlConnection(conStr);
                mCon.Open();

                MySqlCommand pCmd1 = new MySqlCommand("SELECT * FROM categories;", mCon);
                MySqlCommand pCmd2 = new MySqlCommand("SELECT * FROM categories_description;", mCon);
                MySqlDataAdapter mysqlAdapter_cat = null;
                MySqlDataAdapter mysqlAdapter_catDesc = null;

                DataSet dsetCatM = new DataSet();
                DataSet dsetCatDescM = new DataSet();

                mysqlAdapter_cat = new MySqlDataAdapter(pCmd1);
                MySqlCommandBuilder cmdBuilder1 = new MySqlCommandBuilder(mysqlAdapter_cat);
                mysqlAdapter_cat.Fill(dsetCatM, "categories");

                mysqlAdapter_catDesc = new MySqlDataAdapter(pCmd2);
                MySqlCommandBuilder cmdBuilder2 = new MySqlCommandBuilder(mysqlAdapter_catDesc);
                mysqlAdapter_catDesc.Fill(dsetCatDescM, "categories_description");

                // Lets get all parent categories so we can build down the tree.
                var parentQuery = from DataRow parentCategory in dsetCatM.Tables["categories"].Rows
                                  where parentCategory.Field<int>("parent_id") == 0
                                  select parentCategory;
                foreach (var parentCategory in parentQuery)
                {
                    string catName = "";
                    // LoadNode(parent category id, container) -> find all cats call -> LoadNode(parent category id, container)
                    var catDesc = from DataRow category in dsetCatDescM.Tables["categories_description"].Rows
                                  where category.Field<int>("categories_id") == parentCategory.Field<int>("categories_id")
                                  select category;
                    foreach (var category in catDesc)
                    {
                        catName = category.Field<string>("categories_name").ToString();
                        break;
                    }
                    string pId = parentCategory.Field<int>("categories_id").ToString();
                    CategoryItm catItm = new CategoryItm(pId, catName);
                    container.IdLinkList.Add(pId, catItm);
                    container.Items.Add(catItm);
                    LoadZenNode(parentCategory.Field<int>("categories_id"), catItm, dsetCatM, dsetCatDescM, container);
                }

                pCmd1.Dispose();
                pCmd1 = null;
                pCmd2.Dispose();
                pCmd2 = null;
                mysqlAdapter_cat.Dispose();
                mysqlAdapter_cat = null;
                mysqlAdapter_catDesc.Dispose();
                mysqlAdapter_catDesc = null;
                dsetCatM.Dispose();
                dsetCatM = null;
                dsetCatDescM.Dispose();
                dsetCatDescM = null;
                mCon.Close();
                mCon.Dispose();
                mCon = null;
                return container;
            //}
            //catch
            //{
             //   return null;
            //}
        }
 private void LoadNodes(CategoryItm catItm, List<CategoryItm> catLst)
 {
     catLst.Add(catItm);
     if (catItm.Parent != null && catItm.Parent is CategoryItm)
     {
         LoadNodes(catItm.Parent, catLst);
     }
 }
        private bool LoadAllSourceNodes(XElement tItm, CategoryItm xContainer, string parentId = "")
        {
            try
            {
                var categoryQuery = from category in xContainer.Items
                                    select category;

                foreach (var category in categoryQuery)
                {
                    string CategoryId = category.CategoryId;
                    string CategoryName = category.CategoryName;
                    //CategoryIdDevice catDev = new CategoryIdDevice(CategoryId, CategoryName, false, parentId);
                    //catDev.CategorySelected += new CategoryUnOrSelectedEventHandler(catDev_CategorySelected);
                    XElement tItm2 = new XElement("Category", new XAttribute("Id", CategoryId), new XAttribute("CategoryName", CategoryName));
                    //tItm2.Selected += new RoutedEventHandler(tItm_Selected);
                    //tItm2.Header = catDev;
                    tItm.Add(tItm2);
                    //this.gl_catDevLst.Add(CategoryId, catDev);

                    if (category.Items.Count > 0)
                    {
                        LoadAllSourceNodes(tItm2, category, CategoryId);
                    }
                }
                return true;
            }
            catch
            {
            }
            return false;
        }