public static LinkItemCollection BuildFrom(int id, string type)
        {
            ProductMappingLinkItem node = ProductMappingLinkItem.Find(id, type);

            if (node != null)
            {
                return(BuildFrom(node, type));
            }

            // return empty collection
            return(new LinkItemCollection());
        }
        public static LinkItemCollection BuildFrom(ProductMappingLinkItem node, string type)
        {
            node.Selected = true;
            BuildTree(node);

            // find the rootest
            while (node.Parent != null)
            {
                node = node.Parent as ProductMappingLinkItem;
            }

            // and start the reference to it
            return(node.ChildItems);
        }
        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>
        /// Builds the entity tree.
        /// </summary>
        /// <param name="current">The current node.</param>
        private static void BuildTree(ProductMappingLinkItem current)
        {
            ProductMappingLinkItem parent = null;
            string queryTree = string.Format("aspdnsf_GetEntityTree @entity = {0}, @entityid = {1}, @storeid = {2}, @filterentity = {3}", DB.SQuote(current.Type), current.ID, AppLogic.StoreID(), AppLogic.GlobalConfigBool("AllowEntityFiltering"));

            using (SqlConnection con = DB.dbConn())
            {
                con.Open();
                using (IDataReader rs = DB.GetRS(queryTree, con))
                {
                    // parent
                    if (rs.Read())
                    {
                        parent      = new ProductMappingLinkItem();
                        parent.Type = current.Type;
                        parent.ID   = DB.RSFieldInt(rs, "ID");
                        parent.Name = DB.RSFieldByLocale(rs, "Name", Customer.Current.LocaleSetting);
                    }
                    else
                    {
                        ProductMappingLinkItem root = new ProductMappingLinkItem();
                        root.ID   = ProductMappingLinkItem.ROOT_ID;
                        root.Name = "Root";
                        root.Type = current.Type;

                        parent = root;
                    }

                    // sibling
                    if (rs.NextResult())
                    {
                        while (rs.Read())
                        {
                            int id = DB.RSFieldInt(rs, "ID");
                            if (current.ID == id)
                            {
                                current.Parent  = parent;
                                parent.Selected = true;
                                parent.ChildItems.Add(current);
                            }
                            else
                            {
                                ProductMappingLinkItem node = new ProductMappingLinkItem();

                                node.Type = current.Type;
                                node.ID   = id;
                                node.Name = DB.RSFieldByLocale(rs, "Name", Customer.Current.LocaleSetting);
                                if (string.IsNullOrEmpty(node.Name))
                                {
                                    node.Name = DB.RSField(rs, "Name");
                                }
                                node.Parent = parent;

                                parent.ChildItems.Add(node);
                            }
                        }
                    }
                }
            }

            if (parent != null && parent.ID != ProductMappingLinkItem.ROOT_ID)
            {
                BuildTree(parent);
            }
        }
        /// <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);
        }