internal static LinkItemCollection GetChildren(ProductMappingLinkItem parent)
        {
            LinkItemCollection children = new LinkItemCollection();


            Action <IDataReader> readAction = (rs) =>
            {
                while (rs.Read())
                {
                    ProductMappingLinkItem child = new ProductMappingLinkItem();
                    child.Name           = XmlCommon.GetLocaleEntry(rs.Field("Name"), Customer.Current.LocaleSetting, true);
                    child.Parent         = parent;
                    child.Type           = rs.Field("Type");
                    child.SEName         = rs.Field("SEName");
                    child.ParentEntityID = rs.FieldInt("ParentEntityID");
                    child.DisplayOrder   = rs.FieldInt("DisplayOrder");
                    child.ID             = rs.FieldInt("ID");
                    children.Add(child);
                }
            };

            string query = string.Empty;

            switch (parent.Type.ToLowerInvariant())
            {
            case "section":
                query = string.Format("SELECT A.SectionID ID, Name, SEName, 'Section' [Type], ParentSectionID ParentEntityID, DisplayOrder FROM Section A WITH (NOLOCK) INNER JOIN (SELECT DISTINCT " +
                                      "SectionID EntityID FROM Section A WITH (NOLOCK) LEFT JOIN EntityStore B WITH (NOLOCK) ON A.SectionID = B.EntityID AND EntityType = 'Section' WHERE ({0} = 0 or StoreID = {1})) B " +
                                      "ON A.SectionID = B.EntityID WHERE ParentSectionID = {2} AND Published=1 AND Deleted=0 ORDER BY DisplayOrder, Name ASC", CommonLogic.IIF(AppLogic.GlobalConfigBool("AllowEntityFiltering") == true, 1, 0), AppLogic.StoreID(), parent.ID);
                break;

            case "category":
                query = string.Format("SELECT A.CategoryID ID, Name, SEName, 'Category' [Type], ParentCategoryID ParentEntityID, DisplayOrder FROM Category A WITH (NOLOCK) INNER JOIN (SELECT DISTINCT " +
                                      "CategoryID EntityID FROM Category A WITH (NOLOCK) LEFT JOIN EntityStore B WITH (NOLOCK) ON A.CategoryID = B.EntityID AND EntityType = 'Category' WHERE ({0} = 0 or StoreID = {1})) B " +
                                      "ON A.CategoryID = B.EntityID WHERE ParentCategoryID = {2} AND Published=1 AND Deleted=0 ORDER BY DisplayOrder, Name ASC", CommonLogic.IIF(AppLogic.GlobalConfigBool("AllowEntityFiltering") == true, 1, 0), AppLogic.StoreID(), parent.ID);
                break;

            case "manufacturer":
                query = string.Format("SELECT A.ManufacturerID ID, Name, SEName, 'Manufacturer' [Type], ParentManufacturerID ParentEntityID, DisplayOrder FROM Manufacturer A WITH (NOLOCK) INNER JOIN (SELECT " +
                                      "DISTINCT ManufacturerID EntityID FROM Manufacturer A WITH (NOLOCK) LEFT JOIN EntityStore B WITH (NOLOCK) ON A.ManufacturerID = B.EntityID AND EntityType = 'Manufacturer' WHERE ({0} = 0 or " +
                                      "StoreID = {1})) B ON A.ManufacturerID = B.EntityID WHERE ParentManufacturerID = {2} AND Published=1 AND Deleted=0 ORDER BY DisplayOrder, Name ASC", CommonLogic.IIF(AppLogic.GlobalConfigBool("AllowEntityFiltering") == true, 1, 0), AppLogic.StoreID(), parent.ID);
                break;

            case "vector":
                query = string.Format("SELECT A.VectorID ID, Name, SEName, 'Vector' [Type], ParentVectorID ParentEntityID, DisplayOrder FROM Vector A WITH (NOLOCK) INNER JOIN (SELECT DISTINCT " +
                                      "VectorID EntityID FROM Vector A WITH (NOLOCK) LEFT JOIN EntityStore B WITH (NOLOCK) ON A.VectorID = B.EntityID AND EntityType = 'Vector' WHERE ({0} = 0 or StoreID = {1})) B " +
                                      "ON A.VectorID = B.EntityID WHERE ParentVectorID = {2} AND Published=1 AND Deleted=0 ORDER BY DisplayOrder, Name ASC", CommonLogic.IIF(AppLogic.GlobalConfigBool("AllowEntityFiltering") == true, 1, 0), AppLogic.StoreID(), parent.ID);
                break;

            case "genre":
                query = string.Format("SELECT A.GenreID ID, Name, SEName, 'Genre' [Type], ParentGenreID ParentEntityID, DisplayOrder FROM Genre A WITH (NOLOCK) INNER JOIN (SELECT DISTINCT " +
                                      "GenreID EntityID FROM Genre A WITH (NOLOCK) LEFT JOIN EntityStore B WITH (NOLOCK) ON A.GenreID = B.EntityID AND EntityType = 'GenreID' WHERE ({0} = 0 or StoreID = {1})) B " +
                                      "ON A.GenreID = B.EntityID WHERE ParentGenreID = {2} AND Published=1 AND Deleted=0 ORDER BY DisplayOrder, Name ASC", CommonLogic.IIF(AppLogic.GlobalConfigBool("AllowEntityFiltering") == true, 1, 0), AppLogic.StoreID(), parent.ID);
                break;

            case "distributor":
                query = string.Format("SELECT A.DistributorID ID, Name, SEName, 'Distributor' [Type], ParentDistributorID ParentEntityID, DisplayOrder FROM Distributor A WITH (NOLOCK) INNER JOIN (SELECT " +
                                      "DISTINCT DistributorID EntityID FROM Distributor A WITH (NOLOCK) LEFT JOIN EntityStore B WITH (NOLOCK) ON A.DistributorID = B.EntityID AND EntityType = 'Distributor' WHERE ({0} = 0 or " +
                                      "StoreID = {1})) B ON A.DistributorID = B.EntityID WHERE ParentDistributorID = {2} AND Published=1 AND Deleted=0 ORDER BY DisplayOrder, Name ASC", CommonLogic.IIF(AppLogic.GlobalConfigBool("AllowEntityFiltering") == true, 1, 0), AppLogic.StoreID(), parent.ID);
                break;
            }

            if (query.Length > 0)
            {
                DB.UseDataReader(query, readAction);
            }

            return(children);
        }
        /// <summary>
        /// Gets all first level entities.
        /// </summary>
        /// <param name="type">The entity type.</param>
        /// <param name="locale">The customer locale.</param>
        /// <returns></returns>
        public static LinkItemCollection GetAllFirstLevel(string type, int top, string locale)
        {
            LinkItemCollection all = new LinkItemCollection();

            Action <IDataReader> readAction = (rs) =>
            {
                while (rs.Read())
                {
                    ProductMappingLinkItem entity = new ProductMappingLinkItem();
                    entity.Name           = XmlCommon.GetLocaleEntry(rs.Field("Name"), Customer.Current.LocaleSetting, true);
                    entity.SEName         = rs.Field("SEName");
                    entity.Type           = rs.Field("Type");
                    entity.ParentEntityID = rs.FieldInt("ParentEntityID");
                    entity.DisplayOrder   = rs.FieldInt("DisplayOrder");
                    entity.ID             = rs.FieldInt("ID");
                    all.Add(entity);
                }
            };

            string topQuery = string.Empty;

            if (top != ReturnAllRecords)
            {
                topQuery = "TOP {0}".FormatWith(top);
            }

            string query = string.Empty;

            switch (type.ToLowerInvariant())
            {
            case "section":
                query = string.Format("SELECT {2} A.SectionID ID, Name, SEName, 'Section' [Type], ParentSectionID ParentEntityID, DisplayOrder FROM Section A WITH (NOLOCK) INNER JOIN (SELECT DISTINCT SectionID " +
                                      "EntityID FROM Section A WITH (NOLOCK) LEFT JOIN EntityStore B WITH (NOLOCK) ON A.SectionID = B.EntityID AND EntityType = 'Section' WHERE ({0} = 0 or StoreID = {1})) B ON A.SectionID = B.EntityID " +
                                      "WHERE ParentSectionID = 0" + CommonLogic.IIF(AppLogic.IsAdminSite, String.Empty, "and Published=1 and Deleted=0") + " order by DisplayOrder, Name asc", CommonLogic.IIF(AppLogic.GlobalConfigBool("AllowEntityFiltering") == true, 1, 0), AppLogic.StoreID(), topQuery);
                break;

            case "category":
                query = string.Format("SELECT {2} A.CategoryID ID, Name, SEName, 'Category' [Type], ParentCategoryID ParentEntityID, DisplayOrder FROM Category A WITH (NOLOCK) INNER JOIN " +
                                      "(SELECT DISTINCT CategoryID EntityID FROM Category A WITH (NOLOCK) LEFT JOIN EntityStore B WITH (NOLOCK) ON A.CategoryID = B.EntityID AND EntityType = 'Category' " +
                                      "WHERE ({0} = 0 or StoreID = {1})) B ON A.CategoryID = B.EntityID WHERE ParentCategoryID = 0 " + CommonLogic.IIF(AppLogic.IsAdminSite, String.Empty, "and Published=1 and Deleted=0") + " order by DisplayOrder, Name asc", CommonLogic.IIF(AppLogic.GlobalConfigBool("AllowEntityFiltering") == true, 1, 0), AppLogic.StoreID(), topQuery);
                break;

            case "manufacturer":
                query = string.Format("SELECT {2} A.ManufacturerID ID, Name, SEName, 'Manufacturer' [Type], ParentManufacturerID ParentEntityID, DisplayOrder FROM Manufacturer A WITH (NOLOCK) " +
                                      "INNER JOIN (SELECT DISTINCT ManufacturerID EntityID FROM Manufacturer A WITH (NOLOCK) LEFT JOIN EntityStore B WITH (NOLOCK) ON A.ManufacturerID = B.EntityID AND EntityType = 'Manufacturer' " +
                                      "WHERE ({0} = 0 or StoreID = {1})) B ON A.ManufacturerID = B.EntityID WHERE ParentManufacturerID = 0 " + CommonLogic.IIF(AppLogic.IsAdminSite, String.Empty, "and Published=1 and Deleted=0") + " order by DisplayOrder, Name asc", CommonLogic.IIF(AppLogic.GlobalConfigBool("AllowEntityFiltering") == true, 1, 0), AppLogic.StoreID(), topQuery);
                break;

            case "vector":
                query = string.Format("SELECT {2} A.VectorID ID, Name, SEName, 'Vector' [Type], ParentVectorID ParentEntityID, DisplayOrder FROM Vector A WITH (NOLOCK) INNER JOIN (SELECT DISTINCT VectorID " +
                                      "EntityID FROM Vector A WITH (NOLOCK) LEFT JOIN EntityStore B WITH (NOLOCK) ON A.VectorID = B.EntityID AND EntityType = 'Vector' WHERE ({0} = 0 or StoreID = {1})) B ON A.VectorID = B.EntityID " +
                                      "WHERE ParentVectorID = 0" + CommonLogic.IIF(AppLogic.IsAdminSite, String.Empty, "and Published=1 and Deleted=0") + " order by DisplayOrder, Name asc", CommonLogic.IIF(AppLogic.GlobalConfigBool("AllowEntityFiltering") == true, 1, 0), AppLogic.StoreID(), topQuery);
                break;

            case "genre":
                query = string.Format("SELECT {2} A.GenreID ID, Name, SEName, 'Genre' [Type], ParentGenreID ParentEntityID, DisplayOrder FROM Genre A WITH (NOLOCK) INNER JOIN " +
                                      "(SELECT DISTINCT GenreID EntityID FROM Genre A WITH (NOLOCK) LEFT JOIN EntityStore B WITH (NOLOCK) ON A.GenreID = B.EntityID AND EntityType = 'Genre' " +
                                      "WHERE ({0} = 0 or StoreID = {1})) B ON A.GenreID = B.EntityID WHERE ParentGenreID = 0 " + CommonLogic.IIF(AppLogic.IsAdminSite, String.Empty, "and Published=1 and Deleted=0") + " order by DisplayOrder, Name asc", CommonLogic.IIF(AppLogic.GlobalConfigBool("AllowEntityFiltering") == true, 1, 0), AppLogic.StoreID(), topQuery);
                break;

            case "distributor":
                query = string.Format("SELECT {2} A.DistributorID ID, Name, SEName, 'Distributor' [Type], ParentDistributorID ParentEntityID, DisplayOrder FROM Distributor A WITH (NOLOCK) " +
                                      "INNER JOIN (SELECT DISTINCT DistributorID EntityID FROM Distributor A WITH (NOLOCK) LEFT JOIN EntityStore B WITH (NOLOCK) ON A.DistributorID = B.EntityID AND EntityType = 'Distributor' " +
                                      "WHERE ({0} = 0 or StoreID = {1})) B ON A.DistributorID = B.EntityID WHERE ParentDistributorID = 0 " + CommonLogic.IIF(AppLogic.IsAdminSite, String.Empty, "and Published=1 and Deleted=0") + " order by DisplayOrder, Name asc", CommonLogic.IIF(AppLogic.GlobalConfigBool("AllowEntityFiltering") == true, 1, 0), AppLogic.StoreID(), topQuery);
                break;
            }

            if (query.Length > 0)
            {
                DB.UseDataReader(query, readAction);
            }

            return(all);
        }