IEverestCmsDataContextExtensions.cs :  » Content-Management-Systems-CMS » Kooboo » Everest » CmsServices » Models » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Content Management Systems CMS » Kooboo 
Kooboo » Everest » CmsServices » Models » IEverestCmsDataContextExtensions.cs
/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.

This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

using Everest.Library.Data.Entity;
using Everest.Library.ExtensionMethod;

namespace Everest.CmsServices.Models{
    /// <summary>
    /// Cannot to cache the IQueryable Data.
    /// </summary>
    public static class IEverestCmsDataContextExtensions
    {
        #region QueryFolder
        /// <summary>
        /// Determines whether [is folder exists] [the specified data context].
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <param name="folderName">Name of the folder.</param>
        /// <param name="folderUUID">The folder UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Folder> IsFolderExists(this IEverestCmsDataContext dataContext, string application,
            string folderName, Guid folderUUID, int folderType)
        {
            return dataContext.QueryFolder(application, folderName).Where(f => f.UUID != folderUUID && f.FolderType == folderType);
        }
        /// <summary>
        /// Determines whether [is folder exists] [the specified data context].
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <param name="folderOriginaUUID">The folder origina UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Folder> IsFolderExists(this IEverestCmsDataContext dataContext, string application, Guid folderOriginaUUID)
        {
            return dataContext.QueryFolders(f => f.aspnet_Applications.ApplicationName == application && f.OriginalUUID == folderOriginaUUID);
        }
        /// <summary>
        /// Queries the localized folder.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <param name="folderOriginalUUID">The folder original UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Folder> QueryLocalizedFolder(this IEverestCmsDataContext dataContext, string application, Guid folderOriginalUUID)
        {
            //var baseApplications = CachedData.GetBaseApplications(application);

            return dataContext.QueryFolders(f => f.OriginalUUID == folderOriginalUUID).QueryLocalizes(application);
        }
        /// <summary>
        /// Queries the localizes.
        /// </summary>
        /// <param name="queryable">The queryable.</param>
        /// <param name="application">The application.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Folder> QueryLocalizes(this IQueryable<Cms_Folder> queryable, string application)
        {
            var apps = CachedData.GetBaseApplications(application);
            var expression = EfUtility.BuildContainsExpression<Cms_Folder, string>(c => c.aspnet_Applications.ApplicationName, apps);
            var q = queryable.Where(expression);
            return q.GroupBy(bc => bc.OriginalUUID)
               .Join(q, g => new { ApplicationLevel = g.Max(gbc => gbc.ApplicationLevel), OriginalUUID = g.Key }
               , bc => new { bc.ApplicationLevel, bc.OriginalUUID }, (gbc, bc) => bc)
               .OrderByDescending(bc => bc.FolderId);
        }
        /// <summary>
        /// Queries the folder.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <param name="folderName">Name of the folder.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Folder> QueryFolder(this IEverestCmsDataContext dataContext, string application, string folderName, params FolderType[] folderTypes)
        {
            Expression<Func<Cms_Folder, bool>> expression = f => f.aspnet_Applications.ApplicationName == application && f.FolderName == folderName;
            if (folderTypes != null && folderTypes.Length > 0)
            {
                List<int> intValues = new List<int>();
                foreach (var folderType in folderTypes)
                {
                    intValues.Add((int)folderType);
                }
                expression = expression.And(EfUtility.BuildContainsExpression<Cms_Folder, int>(f => f.FolderType, intValues));
            }
            return dataContext.QueryFolders(expression);
        }
        /// <summary>
        /// Queries the folder by base.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="baseUUID">The base UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Folder> QueryFoldersByBase(this IEverestCmsDataContext dataContext, Guid baseUUID)
        {
            Expression<Func<Cms_Folder, bool>> expression = f => f.Base.UUID == baseUUID;
            return dataContext.QueryFolders(expression);
        }
        /// <summary>
        /// Queries the folders.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="schemaId">The schema id.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Folder> QueryFoldersBySchema(this IEverestCmsDataContext dataContext, Guid schemaUUID)
        {
            Expression<Func<Cms_Folder, bool>> expression = f => f.Cms_Schema.UUID == schemaUUID;
            return dataContext.QueryFolders(expression);
        }

        /// <summary>
        /// Queries the folders by schema.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="applications">The applications.</param>
        /// <param name="schemaUUID">The schema UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Folder> QueryFoldersBySchema(this IEverestCmsDataContext dataContext, IEnumerable<string> applications, Guid schemaUUID)
        {
            return dataContext.QueryFolders(applications).Where(f => f.Cms_Schema.UUID == schemaUUID);
        }

        /// <summary>
        /// Queries the folders by schema.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <param name="schemaName">Name of the schema.</param>
        /// <param name="schemaType">Type of the schema.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Folder> QueryFoldersBySchema(this IEverestCmsDataContext dataContext, string application, string schemaName)
        {
            Expression<Func<Cms_Folder, bool>> expression = f => f.aspnet_Applications.ApplicationName == application
                && f.Cms_Schema.SchemaName == schemaName;
            return dataContext.QueryFolders(expression);

        }
        /// <summary>
        /// Queries the folders by workflow.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="workflowUUID">The workflow UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Folder> QueryFoldersByWorkflow(this IEverestCmsDataContext dataContext, Guid workflowUUID)
        {
            Expression<Func<Cms_Folder, bool>> expression = f => f.Cms_Workflow.UUID == workflowUUID;
            return dataContext.QueryFolders(expression);
        }
        /// <summary>
        /// Gets the folders.
        /// get the localized folder for the applications inherit link.
        /// 
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="applications">The applications.</param>
        /// <param name="originalUUID">The original UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Folder> QueryFolders(this IEverestCmsDataContext dataContext, IEnumerable<string> applications, Guid originalUUID)
        {
            Expression<Func<Cms_Folder, bool>> queryExpression = f => f.OriginalUUID == originalUUID;
            queryExpression = queryExpression.And(EfUtility.BuildContainsExpression<Cms_Folder, string>(c => c.aspnet_Applications.ApplicationName, applications));
            return dataContext.QueryFolders(queryExpression);
        }
        /// <summary>
        /// Gets the folders.
        /// get the inherit folders
        /// 
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="startFolderId">The start folder id.</param>
        /// <param name="originalUUID">The original UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Folder> QueryFolders(this IEverestCmsDataContext dataContext, int startFolderId, Guid originalUUID)
        {
            Expression<Func<Cms_Folder, bool>> queryExpression = f => f.FolderId > startFolderId && f.OriginalUUID == originalUUID;
            return dataContext.QueryFolders(queryExpression);
        }
        /// <summary>
        /// Gets the folders.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="folderId">The folder id.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Folder> QueryFolder(this IEverestCmsDataContext dataContext, int folderId)
        {
            Expression<Func<Cms_Folder, bool>> queryExpression = f => f.FolderId == folderId;
            return dataContext.QueryFolders(queryExpression);
        }
        /// <summary>
        /// Queries the folder.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="folderUUID">The folder UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Folder> QueryFolder(this IEverestCmsDataContext dataContext, Guid folderUUID)
        {
            Expression<Func<Cms_Folder, bool>> expression = f => f.UUID == folderUUID;
            return dataContext.QueryFolders(expression);
        }
        /// <summary>
        /// Queries the base folder.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="folderUUID">The folder UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Folder> QueryBaseFolder(this IEverestCmsDataContext dataContext, Guid folderUUID)
        {
            Expression<Func<Cms_Folder, bool>> expression = f => f.Derives.Any(d => d.UUID == folderUUID);
            return dataContext.QueryFolders(expression);
        }
        /// <summary>
        /// Queries the folder.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="folderPath">The folder path.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Folder> QueryFolderByPath(this IEverestCmsDataContext dataContext, string folderPath)
        {
            Expression<Func<Cms_Folder, bool>> expression = f => f.FolderPath == folderPath;
            return dataContext.QueryFolders(expression);
        }
        /// <summary>
        /// Gets the folders.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="folderType">Type of the folder.</param>
        /// <param name="application">The application.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Folder> QueryFolders(this IEverestCmsDataContext dataContext, string application, params FolderType[] folderTypes)
        {
            //StringBuilder cacheKey = new StringBuilder("__QueryFolders_Applciation_" + application);
            //if (folderTypes != null && folderTypes.Length > 0)
            //{
            //    foreach (var folderType in folderTypes)
            //    {
            //        cacheKey.AppendFormat("__FolderType_{0}", folderType);
            //    }
            //}
            //IQueryable<Cms_Folder> folderQuery = CacheManager.Get(CachedData.Folder, cacheKey.ToString()) as IQueryable<Cms_Folder>;
            //if (folderQuery == null)
            //{
            Expression<Func<Cms_Folder, bool>> queryExpression = f => f.aspnet_Applications.ApplicationName == application;
            Expression<Func<Cms_Folder, bool>> folderExpression = f => 1 != 1;
            foreach (var folderType in folderTypes)
            {
                int intFolderType = (int)folderType;
                folderExpression = folderExpression.Or(f => f.FolderType == intFolderType);
            }
            queryExpression = queryExpression.And(folderExpression);
            return dataContext.QueryFolders(queryExpression);

            //    CacheManager.Add(CachedData.Folder, cacheKey.ToString(), folderQuery, CacheItemPriority.Normal, null, CachedData.DefaultExpirations);
            //}

            //return folderQuery;
        }
        /// <summary>
        /// Gets the folders.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="applications">The applications.</param>
        /// <param name="folderTypes">The folder types.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Folder> QueryFolders(this IEverestCmsDataContext dataContext, IEnumerable<string> applications, params FolderType[] folderTypes)
        {
            Expression<Func<Cms_Folder, bool>> folderExpression = f => false;
            foreach (var folderType in folderTypes)
            {
                int intFolderType = (int)folderType;
                folderExpression = folderExpression.Or(f => f.FolderType == intFolderType);
            }
            return dataContext.QueryFolders(applications).Where(folderExpression);
        }
        /// <summary>
        /// Queries the folders.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="applications">The applications.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Folder> QueryFolders(this IEverestCmsDataContext dataContext, IEnumerable<string> applications)
        {
            Expression<Func<Cms_Folder, bool>> queryExpression = EfUtility.BuildContainsExpression<Cms_Folder, string>(c => c.aspnet_Applications.ApplicationName, applications);
            return dataContext.QueryFolders(queryExpression);
        }
        /// <summary>
        /// Queries the folder.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="queryExpression">The query expression.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Folder> QueryFolders(this IEverestCmsDataContext dataContext, Expression<Func<Cms_Folder, bool>> queryExpression)
        {
            return dataContext.Cms_Folder.Where(queryExpression);
        }
        #endregion

        #region QueryContent
        /// <summary>
        /// Determines whether [is content exists] [the specified data context].
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="userKey">The user key.</param>
        /// <param name="contentUUID">The content UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Content> IsContentExists(this IEverestCmsDataContext dataContext, string application, string userKey, Guid contentUUID)
        {
            //Expression<Func<Cms_Content, bool>> expression = c => c.UserKey == userKey && c.UUID != contentUUID;
            return dataContext.QueryContent(application, userKey).Where(c => c.UUID != contentUUID);
        }
        /// <summary>
        /// Queries the contents.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="schemaUUID">The schema UUID.</param>
        /// <param name="application">The application.</param>
        /// <param name="title">The title.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Content> QueryContents(this IEverestCmsDataContext dataContext, Guid schemaUUID, string application, string title)
        {
            Expression<Func<Cms_Content, bool>> expression = c => c.Cms_Schema.UUID == schemaUUID && c.aspnet_Applications.ApplicationName == application;
            if (!StringExtensions.IsNullOrEmptyTrim(title))
            {
                expression = expression.And(c => c.Title.Contains(title));
            }
            return dataContext.QueryContents(expression);
        }
        /// <summary>
        /// Gets the contents.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="contentId">The content id.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Content> QueryContent(this IEverestCmsDataContext dataContext, int contentId)
        {
            Expression<Func<Cms_Content, bool>> queryExpression = c => c.ContentId == contentId;
            //foreach (var id in contentId)
            //{
            //    queryExpression = queryExpression.Or(f => f.ContentId == id);
            //}
            return dataContext.QueryContents(queryExpression);
        }
        /// <summary>
        /// Queries the content.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="contentUUID">The content UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Content> QueryContent(this IEverestCmsDataContext dataContext, Guid contentUUID)
        {
            Expression<Func<Cms_Content, bool>> queryExpression = c => c.UUID == contentUUID;
            return dataContext.QueryContents(queryExpression);
        }
        /// <summary>
        /// Queries the content.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="userKey">The user key.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Content> QueryContent(this IEverestCmsDataContext dataContext, string application, string userKey)
        {
            Expression<Func<Cms_Content, bool>> expression = c => c.aspnet_Applications.ApplicationName == application && c.UserKey == userKey;
            return dataContext.QueryContents(expression);
        }
        /// <summary>
        /// Queries the referencing contents.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="uuid">The UUID. such like CategoryID</param>
        /// <returns></returns>
        public static IQueryable<Cms_Content> QueryReferencingContents(this IEverestCmsDataContext dataContext, Guid uuid)
        {
            Expression<Func<Cms_Content, bool>> expression = c => c.Cms_ContentReferencing.Any(cr => cr.Cms_ReferencedContent.UUID == uuid);
            return dataContext.QueryContents(expression);
        }
        /// <summary>
        /// Queries the referenced contents.
        /// Query the content categories.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="uuid">The UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Content> QueryReferencedContents(this IEverestCmsDataContext dataContext, Guid uuid)
        {
            Expression<Func<Cms_Content, bool>> expression = c => c.Cms_ContentReferenced.Any(cr => cr.Cms_Content.UUID == uuid);
            return dataContext.QueryContents(expression);
        }

        /// <summary>
        /// Queries the included by contents.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="uuid">The UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Content> QueryIncludedByContents(this IEverestCmsDataContext dataContext, Guid uuid)
        {
            Expression<Func<Cms_Content, bool>> expression = c => c.ContentFiles.Any(ct => ct.BinaryContent.UUID == uuid);
            return dataContext.QueryContents(expression);
        }
        /// <summary>
        /// Queries the content localizations.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="uuid">The UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Content> QueryContentLocalizations(this IEverestCmsDataContext dataContext, Guid uuid)
        {
            Expression<Func<Cms_Content, bool>> expression = c => c.Base.UUID == uuid && c.UUID != uuid;
            return dataContext.QueryContents(expression);
        }
        /// <summary>
        /// Queries the contents.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="folders">The folders.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Content> QueryPublishedContents(this IEverestCmsDataContext dataContext, Cms_Folder folder, IEnumerable<Cms_Folder> baseFolders)
        {
            var expression = EfUtility.BuildContainsExpression<Cms_Content, int>(c => c.Cms_Folder.FolderId, baseFolders.Select(f => f.FolderId).ToArray());
            expression = expression.And(c => c.ContentStatus == 1 && (c.Cms_Folder.UUID == folder.UUID || c.Cms_ContentInFolder.Any(ci => ci.Cms_Folder.UUID == folder.UUID)));
            var queryable = dataContext.QueryContents(expression);
            return queryable.QueryLocalizes();
        }
        private static IQueryable<Cms_Content> QueryLocalizes(this IQueryable<Cms_Content> queryable)
        {
            return queryable.GroupBy(bc => bc.OriginalUUID)
              .Join(queryable, g => new { ApplicationLevel = g.Max(gbc => gbc.ApplicationLevel), OriginalUUID = g.Key }
              , bc => new { bc.ApplicationLevel, bc.OriginalUUID }, (gbc, bc) => bc)
              .OrderByDescending(bc => bc.ContentId);
        }
        /// <summary>
        /// Queries the contents.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <param name="schemaUUID">The schema UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Content> QueryContents(this IEverestCmsDataContext dataContext, string application, Guid schemaUUID)
        {
            var applications = CachedData.GetBaseApplications(application);
            Expression<Func<Cms_Content, bool>> expression = c => c.Cms_Schema.UUID == schemaUUID;
            return dataContext.QueryContents(applications).Where(expression);
        }
        public static IQueryable<Cms_Content> QueryContents(this IEverestCmsDataContext dataContext, IEnumerable<string> applications)
        {
            var expression = (EfUtility.BuildContainsExpression<Cms_Content, string>(c => c.aspnet_Applications.ApplicationName, applications));
            var queryable = dataContext.QueryContents(expression);
            return queryable.QueryLocalizes();
        }
        public static IQueryable<Cms_Content> QueryContents(this IEverestCmsDataContext dataContext, Expression<Func<Cms_Content, bool>> expression)
        {
            return dataContext.Cms_Content.Where(expression);
        }

        #endregion

        #region QueryPage
        [Obsolete]
        public static IQueryable<Cms_Page> QueryPagesByWebForm(this IEverestCmsDataContext dataContext, string webFormName)
        {
            //ref to ComponentType
            Expression<Func<Cms_Page, bool>> expression = p => p.Cms_ContentTemplateInPageHolder.Any(ctp => ctp.ComponentType == 2 && ctp.ComponentValue == webFormName);

            return dataContext.QueryPages(expression);
        }
        public static IQueryable<Cms_Page> QueryPagesByModule(this IEverestCmsDataContext dataContext, string moduleName)
        {
            //ref to ComponentType
            Expression<Func<Cms_Page, bool>> expression = p => p.Cms_ContentTemplateInPageHolder.Any(ctp => ctp.ComponentType == 1 && ctp.ComponentValue == moduleName);

            return dataContext.QueryPages(expression);
        }
        /// <summary>
        /// Queries the page by plugin.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="pluginType">Type of the plugin.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Page> QueryPagesByPlugin(this IEverestCmsDataContext dataContext, string pluginType)
        {
            Expression<Func<Cms_Page, bool>> expression = p => p.Cms_Plugin.Any(cp => cp.PluginType == pluginType);

            return dataContext.QueryPages(expression);
        }
        /// <summary>
        /// Queries the page.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="pageId">The page id.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Page> QueryPage(this IEverestCmsDataContext dataContext, int pageId)
        {
            Expression<Func<Cms_Page, bool>> expression = p => p.PageId == pageId;
            return dataContext.QueryPages(expression);
        }
        /// <summary>
        /// Queries the page.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="pageUUID">The page UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Page> QueryPage(this IEverestCmsDataContext dataContext, Guid pageUUID)
        {
            Expression<Func<Cms_Page, bool>> expression = p => p.UUID == pageUUID;
            return dataContext.QueryPages(expression);
        }
        public static IQueryable<Cms_Page> QueryPageByNameNParent(this IEverestCmsDataContext dataContext, Guid? parentUUID, string pageName, string applicationName)
        {
            Expression<Func<Cms_Page, bool>> expression;
            if (parentUUID.HasValue)
            {
                expression = p => p.Cms_ParentPage.UUID == parentUUID.Value && p.PageName == pageName;
            }
            else
            {
                expression = p => p.Cms_ParentPage == null && p.PageName == pageName;
            }
            return dataContext.QueryPages(expression).QueryLocalizes(applicationName);
        }
        public static IQueryable<Cms_Page> QueryPagesByParent(this IEverestCmsDataContext dataContext, Guid parentUUID)
        {
            Expression<Func<Cms_Page, bool>> expression = p => p.Cms_ParentPage.UUID == parentUUID;
            return dataContext.QueryPages(expression);
        }
        /// <summary>
        /// Queries the pages by parent.
        /// ParentUUID=parentUUID
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <param name="parentUUID">The parent UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Page> QueryPagesByParent(this IEverestCmsDataContext dataContext, string application, Guid? parentUUID)
        {
            Expression<Func<Cms_Page, bool>> expression;
            if (parentUUID == null)
            {
                expression = p => p.Cms_ParentPage == null;
            }
            else
            {
                expression = p => p.Cms_ParentPage.UUID == parentUUID;
            }
            return dataContext.QueryPages(expression).QueryLocalizes(application);

        }

        /// <summary>
        /// Queries the page.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <param name="loweredPageName">Name of the lowered page.</param>
        /// <param name="pageId">The page id.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Page> IsPageExists(this IEverestCmsDataContext dataContext, string application, string loweredPageName, Guid pageUUID, Guid? parentUUID)
        {
            Expression<Func<Cms_Page, bool>> expression = p => p.UUID != pageUUID;
            if (parentUUID.HasValue)
            {
                expression = expression.And(p => p.Cms_ParentPage.UUID == parentUUID.Value);
            }
            else
            {
                expression = expression.And(p => p.Cms_ParentPage == null);
            }
            return dataContext.QueryPage(application, loweredPageName).Where(expression);
        }

        /// <summary>
        /// Queries the pages by content template.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="contentTemplateUUID">The content template UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Page> QueryPagesByContentTemplate(this IEverestCmsDataContext dataContext, Guid contentTemplateUUID)
        {
            Expression<Func<Cms_Page, bool>> expression = p => p.Cms_ContentTemplateInPageHolder.Any(ch => ch.Cms_ContentTemplate.UUID == contentTemplateUUID);
            return dataContext.QueryPages(expression);
        }
        /// <summary>
        /// Queries the pages by page template.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="pageTemplateUUID">The page template UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Page> QueryPagesByPageTemplate(this IEverestCmsDataContext dataContext, Guid pageTemplateUUID)
        {
            Expression<Func<Cms_Page, bool>> expression = p => p.Cms_PageTemplate.UUID == pageTemplateUUID;
            return dataContext.QueryPages(expression);
        }
        /// <summary>
        /// Queries the pages by page template.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="pageTemplateUUID">The page template UUID.</param>
        /// <param name="application">The application.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Page> QueryPagesByPageTemplate(this IEverestCmsDataContext dataContext, Guid pageTemplateUUID, string application)
        {
            return dataContext.QueryPagesByPageTemplate(pageTemplateUUID).Where(p => p.aspnet_Applications.ApplicationName == application);
        }
        /// <summary>
        /// Queries the page localizations.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="pageUUID">The page UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Page> QueryPageLocalizations(this IEverestCmsDataContext dataContext, Guid pageUUID)
        {
            Expression<Func<Cms_Page, bool>> expression = p => p.Base.UUID == pageUUID && p.UUID != pageUUID;
            return dataContext.QueryPages(expression);
        }

        /// <summary>
        /// Queries the pages.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <param name="loweredPageName">Name of the lowered page.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Page> QueryPage(this IEverestCmsDataContext dataContext, string application, string loweredPageName)
        {
            var queryable = dataContext.QueryPages(application);
            return queryable.Where(p => p.LoweredPageName == loweredPageName);
        }
        /// <summary>
        /// Queries the pages.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Page> QueryPages(this IEverestCmsDataContext dataContext, string application)
        {
            return dataContext.Cms_Page.QueryLocalizes(application);
        }
        /// <summary>
        /// Queries the pages.
        /// </summary>
        /// <param name="queryable">The queryable.</param>
        /// <param name="application">The application.</param>
        /// <returns></returns>
        private static IQueryable<Cms_Page> QueryLocalizes(this IQueryable<Cms_Page> queryable, string application)
        {
            var apps = CachedData.GetBaseApplications(application);
            var expression = EfUtility.BuildContainsExpression<Cms_Page, string>(c => c.aspnet_Applications.ApplicationName, apps);
            var q = queryable.Where(expression);
            return q.GroupBy(bc => bc.OriginalUUID)
               .Join(q, g => new { ApplicationLevel = g.Max(gbc => gbc.ApplicationLevel), OriginalUUID = g.Key }
               , bc => new { bc.ApplicationLevel, bc.OriginalUUID }, (gbc, bc) => bc)
               .OrderByDescending(bc => bc.PageId);
        }

        /// <summary>
        /// Queries the pages.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Page> QueryPages(this IEverestCmsDataContext dataContext, Expression<Func<Cms_Page, bool>> expression)
        {
            return dataContext.Cms_Page.Where(expression);
        }
        #endregion

        #region ContentTempateInPage
        public static IQueryable<Cms_ContentTemplateInPageHolder> QueryContentTemplateInPageHolders(this IEverestCmsDataContext dataContext, int pageId
            , string positionId, Guid contentTemplateUUID)
        {
            return dataContext.QueryContentTemplateInPageHolders(pageId, positionId).Where(ctp => ctp.Cms_ContentTemplate.UUID == contentTemplateUUID);
        }
        /// <summary>
        /// Queries the content templates in page holder.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="pageId">The page id.</param>
        /// <param name="positionId">The position id.</param>
        /// <returns></returns>
        public static IQueryable<Cms_ContentTemplateInPageHolder> QueryContentTemplateInPageHolders(this IEverestCmsDataContext dataContext, int pageId
            , string positionId)
        {
            return dataContext.QueryContentTemplateInPages(pageId).Where(ctp => ctp.Position == positionId).OrderBy(ctp => ctp.Order);
        }
        /// <summary>
        /// Queries the content templates in page.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="pageId">The page id.</param>
        /// <returns></returns>
        public static IQueryable<Cms_ContentTemplateInPageHolder> QueryContentTemplateInPages(this IEverestCmsDataContext dataContext, int pageId)
        {
            return dataContext.Cms_ContentTemplateInPageHolder.Where(ctp => ctp.Cms_Page.PageId == pageId);
        }
        /// <summary>
        /// Queries the content templates in page.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="contentTemplateUUID">The content template UUID.</param>
        /// <param name="position">The position.</param>
        /// <returns></returns>
        public static IQueryable<Cms_ContentTemplateInPageHolder> QueryContentTemplateInPages(this IEverestCmsDataContext dataContext, Guid contentTemplateUUID
            , string position)
        {
            return dataContext.Cms_ContentTemplateInPageHolder.Where(ctp => ctp.Cms_ContentTemplate.UUID == contentTemplateUUID && ctp.Position == position);
        }
        #endregion

        #region QueryContentFile
        public static IQueryable<Cms_ContentFile> QueryContentFile(this IEverestCmsDataContext dataContext, Guid contentFileUUID)
        {
            Expression<Func<Cms_ContentFile, bool>> expression = cf => cf.ContentFileUUID == contentFileUUID;
            return dataContext.QueryContentFiles(expression);
        }
        public static IQueryable<Cms_ContentFile> QueryContentFile(this IEverestCmsDataContext dataContext, Guid textContentUUID, Guid binaryContentUUID)
        {
            Expression<Func<Cms_ContentFile, bool>> expression = cf => cf.TextContent.UUID == textContentUUID && cf.BinaryContent.UUID == binaryContentUUID;
            return dataContext.QueryContentFiles(expression);
        }
        public static IQueryable<Cms_ContentFile> QueryContentFileByTextContent(this IEverestCmsDataContext dataContext, Guid textContentUUID)
        {
            Expression<Func<Cms_ContentFile, bool>> expression = cf => cf.TextContent.UUID == textContentUUID && cf.BinaryContent == null;
            return dataContext.QueryContentFiles(expression);
        }
        /// <summary>
        /// Queries the content files.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="binaryContentId">The binary content id.</param>
        /// <returns></returns>
        public static IQueryable<Cms_ContentFile> QueryContentFiles(this IEverestCmsDataContext dataContext, int binaryContentId)
        {
            //the status of the textcontent must be published.
            Expression<Func<Cms_ContentFile, bool>> expression = cf => cf.BinaryContent.ContentId == binaryContentId && cf.TextContent.ContentStatus == 1;
            return dataContext.QueryContentFiles(expression);
        }
        /// <summary>
        /// Queries the content files.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        public static IQueryable<Cms_ContentFile> QueryContentFiles(this IEverestCmsDataContext dataContext, Expression<Func<Cms_ContentFile, bool>> expression)
        {
            return dataContext.Cms_ContentFile.Where(expression);
        }
        #endregion

        #region QueryDataRule
        public static IQueryable<Cms_DataRule> QueryDataRulesByFolder(this IEverestCmsDataContext dataContext, Guid folderUUID)
        {
            Expression<Func<Cms_DataRule, bool>> expression = dr => dr.Cms_Folder.UUID == folderUUID;

            return dataContext.QueryDataRules(expression);
        }

        /// <summary>
        /// Queries the data rules by page.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="pageId">The page id.</param>
        /// <returns></returns>
        public static IQueryable<Cms_DataRule> QueryDataRulesByPage(this IEverestCmsDataContext dataContext, int pageId)
        {
            var queryable = dataContext.Cms_DataRule.Where(dr => dr.Cms_Page.PageId == pageId).OrderBy(dr => dr.Order);
            return queryable;
        }
        public static IQueryable<Cms_DataRule> QueryDataRulesByContentTemplate(this IEverestCmsDataContext dataContext, Guid contentTemplateUUID)
        {
            Expression<Func<Cms_DataRule, bool>> expression = dr => dr.Cms_ContentTemplate.UUID == contentTemplateUUID;
            return dataContext.QueryDataRules(expression).OrderBy(dr => dr.Order);
        }

        /// <summary>
        /// Queries the data rules.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="folderId">The folder id.</param>
        /// <param name="dataRuleValueType">Type of the data rule value.</param>
        /// <returns></returns>
        public static IQueryable<Cms_DataRule> QueryPagesByDataRule(this IEverestCmsDataContext dataContext, int folderId, DataRuleValueType dataRuleValueType, string application, int? dynamicLinkingOrder)
        {
            //Query all his base folder.
            Cms_Folder folder = dataContext.QueryFolder(folderId).First();
            int intDataRuleValueType = (int)dataRuleValueType;
            Expression<Func<Cms_DataRule, bool>> expression = dr => dr.ValueType == intDataRuleValueType;
            expression = expression.And(EfUtility.BuildContainsExpression<Cms_DataRule, int?>(dr => dr.Cms_Folder.FolderId, folder.GetBaseFoldersRecusive().Select(f => (int?)f.FolderId)));
            var dataRules = dataContext.QueryDataRules(expression);

            //Cms_Folder pageFolder = dataContext.QueryFolders(application, FolderType.Page).First();
            var apps = CachedData.GetBaseApplications(application);
            var pageQuery = EfUtility.BuildContainsExpression<Cms_DataRule, string>(dr => dr.Cms_Page.aspnet_Applications.ApplicationName, apps);

            dataRules = dataRules.Where(pageQuery).OrderBy(dr => dr.DynamicLinkingOrder);
            if (dynamicLinkingOrder != null)
            {
                dataRules.Skip(dynamicLinkingOrder.Value - 1);
            }
            return dataRules;
        }
        /// <summary>
        /// Queries the data rules.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        public static IQueryable<Cms_DataRule> QueryDataRules(this IEverestCmsDataContext dataContext, Expression<Func<Cms_DataRule, bool>> expression)
        {
            return dataContext.Cms_DataRule.Where(expression);
        }

        #endregion

        #region QueryAsp_Application
        public static IQueryable<aspnet_Applications> QueryApplicationsByUser(this IEverestCmsDataContext dataContext, string userName)
        {
            Expression<Func<aspnet_Applications, bool>> expression = a => a.CmsApplicationsInUser.Any(u => u.UserName == userName);
            return dataContext.QueryApplications(expression);
        }

        /// <summary>
        /// Queries the inheritable applications.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <returns></returns>
        public static IQueryable<aspnet_Applications> QueryInheritableApplications(this IEverestCmsDataContext dataContext, string application)
        {
            Expression<Func<aspnet_Applications, bool>> expression = app => app.ApplicationName != application;
            return dataContext.QueryApplications(expression);
        }
        /// <summary>
        /// Queries the applications.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <param name="applicationId">The application id.</param>
        /// <returns></returns>
        public static IQueryable<aspnet_Applications> IsApplicationExists(this IEverestCmsDataContext dataContext, string application, Guid applicationId)
        {
            return dataContext.QueryApplication(application).Where(app => app.ApplicationId != applicationId);
        }
        /// <summary>
        /// Determines whether [is host name exists] [the specified data context].
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="hostName">Name of the host.</param>
        /// <param name="virtualPath">The virtual path.</param>
        /// <param name="applicationId">The application id.</param>
        /// <returns></returns>
        public static IQueryable<aspnet_Applications> IsHostNameExists(this IEverestCmsDataContext dataContext, string hostName, string virtualPath, Guid applicationId)
        {
            var query = dataContext.aspnet_Applications.Where(app => app.HostName.Contains(hostName));
            if (string.IsNullOrEmpty(virtualPath))
            {
                query = query.Where(app => app.VirtualPath == null || app.VirtualPath == "");
            }
            else
            {
                query = query.Where(app => app.VirtualPath == virtualPath);
            }
            return query.Where(app => app.ApplicationId != applicationId);
        }
        /// <summary>
        /// Queries the applications by base.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="baseApplication">The base application.</param>
        /// <returns></returns>
        public static IQueryable<aspnet_Applications> QueryApplicationsByBase(this IEverestCmsDataContext dataContext, string baseApplication)
        {
            Expression<Func<aspnet_Applications, bool>> expression = app => app.BaseApplication == baseApplication;
            return dataContext.QueryApplications(expression);
        }
        /// <summary>
        /// Queries the application.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <returns></returns>
        public static IQueryable<aspnet_Applications> QueryApplication(this IEverestCmsDataContext dataContext, string application)
        {
            Expression<Func<aspnet_Applications, bool>> expression = app => app.ApplicationName == application;
            return dataContext.QueryApplications(expression);
        }
        /// <summary>
        /// Queries the applications.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="applications">The applications.</param>
        /// <returns></returns>
        public static IQueryable<aspnet_Applications> QueryApplications(this IEverestCmsDataContext dataContext, string[] applications)
        {
            Expression<Func<aspnet_Applications, bool>> expression = EfUtility.BuildContainsExpression<aspnet_Applications, string>(a => a.ApplicationName, applications);
            return dataContext.QueryApplications(expression);
        }
        /// <summary>
        /// Queries the application.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="applicationId">The application id.</param>
        /// <returns></returns>
        public static IQueryable<aspnet_Applications> QueryApplication(this IEverestCmsDataContext dataContext, Guid applicationId)
        {
            Expression<Func<aspnet_Applications, bool>> expression = app => app.ApplicationId == applicationId;
            return dataContext.QueryApplications(expression);
        }
        /// <summary>
        /// Queries the name of the application by host.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="hostName">Name of the host.</param>
        /// <returns></returns>
        public static IQueryable<aspnet_Applications> QueryApplicationByHostName(this IEverestCmsDataContext dataContext, string hostName)
        {
            Expression<Func<aspnet_Applications, bool>> expression = app => app.HostName.Contains(hostName) && (app.VirtualPath == null || app.VirtualPath == "");
            return dataContext.QueryApplications(expression);
        }
        /// <summary>
        /// Queries the application by host name N virtual path.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="hostName">Name of the host.</param>
        /// <param name="virtualPath">The virtual path.</param>
        /// <returns></returns>
        public static IQueryable<aspnet_Applications> QueryApplicationByHostNameNVirtualPath(this IEverestCmsDataContext dataContext, string hostName, string virtualPath)
        {
            Expression<Func<aspnet_Applications, bool>> expression = app => app.HostName.Contains(hostName) && app.VirtualPath == virtualPath;
            return dataContext.QueryApplications(expression);
        }
        /// <summary>
        /// Queries the applications.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        public static IQueryable<aspnet_Applications> QueryApplications(this IEverestCmsDataContext dataContext, Expression<Func<aspnet_Applications, bool>> expression)
        {
            return dataContext.aspnet_Applications.Where(expression);
        }
        #endregion

        #region QueryPageTemplate
        /// <summary>
        /// Queries the page template.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="name">The name.</param>
        /// <param name="application">The application.</param>
        /// <param name="pageTemplateId">The page template id.</param>
        /// <returns></returns>
        public static IQueryable<Cms_PageTemplate> IsPageTemplateExists(this IEverestCmsDataContext dataContext, string name,
            string application, int pageTemplateId)
        {
            return dataContext.QueryPageTemplate(application, name).Where(pt => pt.PageTemplateId != pageTemplateId);
        }
        /// <summary>
        /// Queries the page template.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <param name="pageTemplateName">Name of the page template.</param>
        /// <returns></returns>
        public static IQueryable<Cms_PageTemplate> QueryPageTemplate(this IEverestCmsDataContext dataContext, string application,
            string pageTemplateName)
        {
            return dataContext.QueryPageTemplates(application).Where(pt => pt.Name == pageTemplateName);
        }
        /// <summary>
        /// Queries the page template.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="pageTemplateId">The page template id.</param>
        /// <returns></returns>
        public static IQueryable<Cms_PageTemplate> QueryPageTemplate(this IEverestCmsDataContext dataContext, int pageTemplateId)
        {
            Expression<Func<Cms_PageTemplate, bool>> expression = cp => cp.PageTemplateId == pageTemplateId;
            return dataContext.QueryPageTemplates(expression);
        }
        /// <summary>
        /// Queries the page template.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="pageTemplateUUID">The page template UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_PageTemplate> QueryPageTemplate(this IEverestCmsDataContext dataContext, Guid pageTemplateUUID)
        {
            Expression<Func<Cms_PageTemplate, bool>> expression = cp => cp.UUID == pageTemplateUUID;
            return dataContext.QueryPageTemplates(expression);
        }
        /// <summary>
        /// Queries the page template.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <param name="originalId">The original id.</param>
        /// <returns></returns>
        public static IQueryable<Cms_PageTemplate> QueryPageTemplate(this IEverestCmsDataContext dataContext, string application, Guid originalId)
        {
            return dataContext.QueryPageTemplates(application).Where(pt => pt.OriginalUUID == originalId);
        }
        /// <summary>
        /// Queries the page template localizations.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="pageTemplateUUID">The page template UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_PageTemplate> QueryPageTemplateLocalizations(this IEverestCmsDataContext dataContext, Guid pageTemplateUUID)
        {
            Expression<Func<Cms_PageTemplate, bool>> expression = pt => pt.Base.UUID == pageTemplateUUID && pt.UUID != pageTemplateUUID;
            return dataContext.QueryPageTemplates(expression);
        }
        /// <summary>
        /// Queries the page templates.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <returns></returns>
        public static IQueryable<Cms_PageTemplate> QueryPageTemplates(this IEverestCmsDataContext dataContext, string application)
        {
            var baseApps = CachedData.GetBaseApplications(application);
            var expression = (EfUtility.BuildContainsExpression<Cms_PageTemplate, string>(c => c.aspnet_Applications.ApplicationName, baseApps));
            var queryable = dataContext.QueryPageTemplates(expression);
            return queryable.GroupBy(pt => pt.OriginalUUID)
              .Join(dataContext.QueryPageTemplates(expression), g => new { ApplicationLevel = g.Max(gpt => gpt.ApplicationLevel), OriginalUUID = g.Key },
              pt => new { pt.ApplicationLevel, OriginalUUID = pt.OriginalUUID }, (gpt, pt) => pt);
        }
        /// <summary>
        /// Queries the page templates.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        public static IQueryable<Cms_PageTemplate> QueryPageTemplates(this IEverestCmsDataContext dataContext, Expression<Func<Cms_PageTemplate, bool>> expression)
        {
            return dataContext.Cms_PageTemplate.Where(expression);
        }
        #endregion

        #region QueryContentTemplate

        /// <summary>
        /// Queries the content template by plugin.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="pluginType">Type of the plugin.</param>
        /// <returns></returns>
        public static IQueryable<Cms_ContentTemplate> QueryContentTemplateByPlugin(this IEverestCmsDataContext dataContext, string pluginType)
        {
            Expression<Func<Cms_ContentTemplate, bool>> expression = p => p.Cms_Plugin.Any(cp => cp.PluginType == pluginType);

            return dataContext.QueryContentTemplates(expression);
        }
        /// <summary>
        /// Groups the content template namespaces.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <param name="prefixNamespace">The prefix namespace.</param>
        /// <returns></returns>
        public static IQueryable<IGrouping<string, Cms_ContentTemplate>> GroupContentTemplateNamespaces(this IEverestCmsDataContext dataContext, string application, string prefixNamespace)
        {
            Expression<Func<Cms_ContentTemplate, bool>> expression = tr => tr.Namespace != null && tr.Namespace != "";
            if (!StringExtensions.IsNullOrEmptyTrim(prefixNamespace))
            {
                expression = expression.And(tr => tr.Namespace.Contains(prefixNamespace));
            }
            var group = dataContext.QueryContentTemplates(application)
                  .Where(expression)
                  .GroupBy(tr => tr.Namespace);
            return group;
        }
        /// <summary>
        /// Queries the content template.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="name">The name.</param>
        /// <param name="application">The application.</param>
        /// <param name="pageTemplateId">The page template id.</param>
        /// <returns></returns>
        public static IQueryable<Cms_ContentTemplate> IsContentTemplateExists(this IEverestCmsDataContext dataContext, string name,
            string application, int pageTemplateId)
        {
            return dataContext.QueryContentTemplates(application).Where(ct => ct.Name == name && ct.ContentTemplateId != pageTemplateId);
        }
        /// <summary>
        /// Queries the content template.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="contentTemplateId">The content template id.</param>
        /// <returns></returns>
        public static IQueryable<Cms_ContentTemplate> QueryContentTemplate(this IEverestCmsDataContext dataContext, int contentTemplateId)
        {
            Expression<Func<Cms_ContentTemplate, bool>> expression = ct => ct.ContentTemplateId == contentTemplateId;
            return dataContext.QueryContentTemplates(expression);
        }
        /// <summary>
        /// Queries the content template.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="contentTemplateUUID">The content template UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_ContentTemplate> QueryContentTemplate(this IEverestCmsDataContext dataContext, Guid contentTemplateUUID)
        {
            Expression<Func<Cms_ContentTemplate, bool>> expression = ct => ct.UUID == contentTemplateUUID;
            return dataContext.QueryContentTemplates(expression);
        }

        /// <summary>
        /// Queries the content template.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <param name="contentTemplateName">Name of the content template.</param>
        /// <returns></returns>
        public static IQueryable<Cms_ContentTemplate> QueryContentTemplate(this IEverestCmsDataContext dataContext, string application, string contentTemplateName)
        {
            return dataContext.QueryContentTemplates(application).Where(ct => ct.Name == contentTemplateName);
        }
        /// <summary>
        /// Queries the content templates.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <param name="originalUUID">The original UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_ContentTemplate> QueryContentTemplate(this IEverestCmsDataContext dataContext, string application, Guid originalUUID)
        {
            return dataContext.QueryContentTemplates(application).Where(ct => ct.OriginalUUID == originalUUID);
        }
        /// <summary>
        /// Queries the template localizations.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="contentTemplateUUID">The content template UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_ContentTemplate> QueryTemplateLocalizations(this IEverestCmsDataContext dataContext, Guid contentTemplateUUID)
        {
            Expression<Func<Cms_ContentTemplate, bool>> expression = ct => ct.UUID != contentTemplateUUID && ct.OriginalUUID == contentTemplateUUID;
            return dataContext.QueryContentTemplates(expression);
        }
        /// <summary>
        /// Queries the content templates.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <param name="strNamespace">The STR namespace.</param>
        /// <returns></returns>
        public static IQueryable<Cms_ContentTemplate> QueryContentTemplates(this IEverestCmsDataContext dataContext, string application, string strNamespace)
        {
            Expression<Func<Cms_ContentTemplate, bool>> expression = tr => tr.Namespace == strNamespace;
            if (StringExtensions.IsNullOrEmptyTrim(strNamespace))
            {
                expression = tr => tr.Namespace == null || tr.Namespace == "";
                //expression = tr => true; //by crl
            }
            return dataContext.QueryContentTemplates(application).Where(expression);
        }
        /// <summary>
        /// Queries the content templates.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <returns></returns>
        public static IQueryable<Cms_ContentTemplate> QueryContentTemplates(this IEverestCmsDataContext dataContext, string application)
        {
            var baseApps = CachedData.GetBaseApplications(application);
            var expression = (EfUtility.BuildContainsExpression<Cms_ContentTemplate, string>(c => c.aspnet_Applications.ApplicationName, baseApps));
            var queryable = dataContext.QueryContentTemplates(expression);
            return queryable.GroupBy(pt => pt.OriginalUUID)
              .Join(dataContext.QueryContentTemplates(expression), g => new { ApplicationLevel = g.Max(gpt => gpt.ApplicationLevel), OriginalUUID = g.Key },
              pt => new { ApplicationLevel = pt.ApplicationLevel, OriginalUUID = pt.OriginalUUID }, (gpt, pt) => pt);
        }
        /// <summary>
        /// Queries the content templates.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        public static IQueryable<Cms_ContentTemplate> QueryContentTemplates(this IEverestCmsDataContext dataContext, Expression<Func<Cms_ContentTemplate, bool>> expression)
        {
            return dataContext.Cms_ContentTemplate.Where(expression);
        }
        #endregion

        #region QueryTextResource
        public static IQueryable<Cms_TextResource> IsTextResourcesExists(this IEverestCmsDataContext dataContext, string application, string strNamesapce,
            string key, int resourceId)
        {
            return dataContext.QueryTextResourcesByNamespace(application, strNamesapce).Where(tr => tr.ResourceKey == key && tr.ResourceId != resourceId);
        }
        /// <summary>
        /// Groups the text resources.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <param name="prefixNamespace">The prefix namespace.</param>
        /// <returns></returns>
        public static IQueryable<IGrouping<string, Cms_TextResource>> GroupTextResourceNamespaces(this IEverestCmsDataContext dataContext, string application, string prefixNamespace)
        {
            Expression<Func<Cms_TextResource, bool>> expression = tr => tr.Namespace != null && tr.Namespace != "";
            if (!StringExtensions.IsNullOrEmptyTrim(prefixNamespace))
            {
                expression = expression.And(tr => tr.Namespace.Contains(prefixNamespace + "."));
            }
            var group = dataContext.QueryTextResources(application)
                  .Where(expression)
                  .GroupBy(tr => tr.Namespace);
            return group;
        }
        /// <summary>
        /// Queries the text resources.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="textResourcesId">The text resources id.</param>
        /// <returns></returns>
        public static IQueryable<Cms_TextResource> QueryTextResources(this IEverestCmsDataContext dataContext, params int[] textResourcesId)
        {
            Expression<Func<Cms_TextResource, bool>> expression = f => false;
            foreach (var id in textResourcesId)
            {
                expression = expression.Or(tr => tr.ResourceId == id);
            }
            return dataContext.QueryTextResources(expression);

        }
        /// <summary>
        /// Queries the text resource.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="uuid">The UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_TextResource> QueryTextResource(this IEverestCmsDataContext dataContext, Guid uuid)
        {
            Expression<Func<Cms_TextResource, bool>> expression = tr => tr.UUID == uuid;
            return dataContext.QueryTextResources(expression);
        }
        /// <summary>
        /// Queries the text resource localizations.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="uuid">The UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_TextResource> QueryTextResourceLocalizations(this IEverestCmsDataContext dataContext, Guid uuid)
        {
            Expression<Func<Cms_TextResource, bool>> expression = f => f.Base.UUID == uuid && f.UUID != uuid;
            return dataContext.QueryTextResources(expression);
        }
        /// <summary>
        /// Queries the text resources.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="textResourceFolder">The text resource folder.</param>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public static IQueryable<Cms_TextResource> QueryTextResources(this IEverestCmsDataContext dataContext, string application, string key)
        {
            Expression<Func<Cms_TextResource, bool>> expression = tr => tr.ResourceKey == key;
            return dataContext.QueryTextResources(application).Where(expression);
        }
        /// <summary>
        /// Queries the text resources.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <param name="strNamespace">The STR namespace.</param>
        /// <returns></returns>
        public static IQueryable<Cms_TextResource> QueryTextResourcesByNamespace(this IEverestCmsDataContext dataContext, string application, string strNamespace)
        {
            Expression<Func<Cms_TextResource, bool>> expression = tr => tr.Namespace == strNamespace;
            if (StringExtensions.IsNullOrEmptyTrim(strNamespace))
            {
                //expression = tr => true;
                expression = tr => tr.Namespace == null || tr.Namespace == "";
            }
            return dataContext.QueryTextResources(application).Where(expression);
        }
        /// <summary>
        /// Queries the text resources.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <returns></returns>
        public static IQueryable<Cms_TextResource> QueryTextResources(this IEverestCmsDataContext dataContext, string application)
        {
            var baseApps = CachedData.GetBaseApplications(application);
            var expression = EfUtility.BuildContainsExpression<Cms_TextResource, string>(tr => tr.aspnet_Applications.ApplicationName, baseApps);
            return dataContext.QueryTextResources(expression).GroupBy(tr => tr.OriginalUUID)
              .Join(dataContext.QueryTextResources(expression), g => new { ApplicationLevel = g.Max(gtr => gtr.ApplicationLevel), OriginalUUID = g.Key }
              , tr => new { tr.ApplicationLevel, tr.OriginalUUID }, (gtr, tr) => tr);
        }
        /// <summary>
        /// Queries the text resources.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        public static IQueryable<Cms_TextResource> QueryTextResources(this IEverestCmsDataContext dataContext, Expression<Func<Cms_TextResource, bool>> expression)
        {
            return dataContext.Cms_TextResource.Where(expression);
        }
        #endregion

        #region QuerySchemaFunctions
        /// <summary>
        /// Queries the schema function.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="functionId">The function id.</param>
        /// <returns></returns>
        public static IQueryable<Cms_SchemaFunction> QuerySchemaFunction(this IEverestCmsDataContext dataContext, int functionId)
        {
            Expression<Func<Cms_SchemaFunction, bool>> expression = f => f.FunctionId == functionId;
            return dataContext.QuerySchemaFunctions(expression);
        }
        /// <summary>
        /// Queries the schema functions.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="schemaId">The schema id.</param>
        /// <returns></returns>
        public static IQueryable<Cms_SchemaFunction> QuerySchemaFunctions(this IEverestCmsDataContext dataContext, Guid schemaUUID)
        {
            Expression<Func<Cms_SchemaFunction, bool>> expression = sf => sf.Cms_Schema.UUID == schemaUUID;
            return dataContext.QuerySchemaFunctions(expression);
        }

        /// <summary>
        /// Queries the schema functions.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="schemaId">The schema id.</param>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public static IQueryable<Cms_SchemaFunction> QuerySchemaFunctions(this IEverestCmsDataContext dataContext, Guid schemaUUID, FunctionType type)
        {
            int typeValue = (int)type;
            return dataContext.QuerySchemaFunctions(schemaUUID).Where(f => f.FormType == typeValue);
        }
        /// <summary>
        /// Queries the schema functions.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        public static IQueryable<Cms_SchemaFunction> QuerySchemaFunctions(this IEverestCmsDataContext dataContext, Expression<Func<Cms_SchemaFunction, bool>> expression)
        {
            return dataContext.Cms_SchemaFunction.Where(expression);
        }
        #endregion

        #region QuerySchema

        /// <summary>
        /// Queries the schemas.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="schemaId">The schema id.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Schema> QuerySchema(this IEverestCmsDataContext dataContext, int schemaId)
        {
            Expression<Func<Cms_Schema, bool>> expression = f => f.SchemaId == schemaId;
            //foreach (var id in schemaId)
            //{
            //    expression = expression.Or(f => f.SchemaId == id);
            //}
            return dataContext.QuerySchemas(expression);
        }
        /// <summary>
        /// Queries the schema.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="uuid">The UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Schema> QuerySchema(this IEverestCmsDataContext dataContext, Guid uuid)
        {
            Expression<Func<Cms_Schema, bool>> expression = f => f.UUID == uuid;
            return dataContext.QuerySchemas(expression);
        }
        /// <summary>
        /// Queries the schema.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="schemaName">Name of the schema.</param>
        /// <param name="application">The application.</param>
        /// <param name="schemaUUID">The schema UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Schema> IsSchemaExists(this IEverestCmsDataContext dataContext, string schemaName,
            string application, int schemaId)
        {
            return dataContext.QuerySchema(application, schemaName).Where(s => s.SchemaId != schemaId);
        }
        /// <summary>
        /// Queries the schema.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <param name="schemaName">Name of the schema.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Schema> QuerySchema(this IEverestCmsDataContext dataContext, string application, string schemaName)
        {
            Expression<Func<Cms_Schema, bool>> expression = s => s.SchemaName == schemaName;

            return dataContext.QuerySchemas(application).Where(expression);
        }
        /// <summary>
        /// Queries the schemas.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <param name="folderType">Type of the folder.</param>
        /// <param name="schemaType">Type of the schema.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Schema> QuerySchemas(this IEverestCmsDataContext dataContext, string application)
        {
            IEnumerable<string> applications = CachedData.GetBaseApplications(application);
            return dataContext.QuerySchemas(applications);
        }
        /// <summary>
        /// Queries the schemas.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="folderId">The folder id.</param>
        /// <param name="schemaType">Type of the schema.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Schema> QuerySchemas(this IEverestCmsDataContext dataContext, string application, SchemaType schemaType)
        {
            int intSchemaType = (int)schemaType;
            Expression<Func<Cms_Schema, bool>> expression = s => s.SchemaType == intSchemaType;
            var apps = CachedData.GetBaseApplications(application);
            return dataContext.QuerySchemas(apps).Where(expression);
        }

        /// <summary>
        /// Queries the schemas.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="applications">The applications.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Schema> QuerySchemas(this IEverestCmsDataContext dataContext, IEnumerable<string> applications)
        {
            Expression<Func<Cms_Schema, bool>> expression = EfUtility.BuildContainsExpression<Cms_Schema, string>(c => c.aspnet_Applications.ApplicationName, applications);
            return dataContext.QuerySchemas(expression);
        }
        /// <summary>
        /// Queries the schemas.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Schema> QuerySchemas(this IEverestCmsDataContext dataContext, Expression<Func<Cms_Schema, bool>> expression)
        {
            return dataContext.Cms_Schema.Where(expression);
        }
        #endregion

        #region QueryUsers
        /// <summary>
        /// Queries the user.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="userName">Name of the user.</param>
        /// <returns></returns>
        public static IQueryable<aspnet_Users> QueryUser(this IEverestCmsDataContext dataContext, string userName)
        {
            Expression<Func<aspnet_Users, bool>> expression = u => u.UserName == userName;
            return dataContext.QueryUsers(expression);
        }
        /// <summary>
        /// Queries the users.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <returns></returns>
        public static IQueryable<aspnet_Users> QueryCmsUsers(this IEverestCmsDataContext dataContext, string application)
        {
            Expression<Func<aspnet_Users, bool>> expression = u => u.UsersInCmsApplication.Any(app => app.ApplicationName == application);
            return dataContext.QueryUsers(expression);
        }
        /// <summary>
        /// Queries the users.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        public static IQueryable<aspnet_Users> QueryUsers(this IEverestCmsDataContext dataContext, Expression<Func<aspnet_Users, bool>> expression)
        {
            return dataContext.aspnet_Users.Where(expression);
        }
        #endregion

        #region QueryRole
        public static IQueryable<aspnet_Roles> QueryRole(this IEverestCmsDataContext dataContext, Guid roleId)
        {
            Expression<Func<aspnet_Roles, bool>> expression = r => r.RoleId == roleId;
            return dataContext.QueryRoles(expression);
        }
        public static IQueryable<aspnet_Roles> IsRoleExists(this IEverestCmsDataContext dataContext, string roleName, string application, Guid roleId)
        {
            var queryable = dataContext.QueryRoles(application);
            return queryable.Where(r => r.RoleName == roleName && r.RoleId != roleId);
        }
        public static IQueryable<aspnet_Roles> QueryRoles(this IEverestCmsDataContext dataContext, string application)
        {
            Expression<Func<aspnet_Roles, bool>> expression = r => r.aspnet_Applications.ApplicationName == application;
            return dataContext.QueryRoles(expression);
        }
        public static IQueryable<aspnet_Roles> QueryRoles(this IEverestCmsDataContext dataContext, Expression<Func<aspnet_Roles, bool>> expression)
        {
            return dataContext.aspnet_Roles.Where(expression);
        }
        #endregion

        #region QueryMembership
        /// <summary>
        /// Queries the memberships.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="applicationName">Name of the application.</param>
        /// <returns></returns>
        public static IQueryable<aspnet_Membership> QueryMemberships(this IEverestCmsDataContext dataContext, string applicationName)
        {
            Expression<Func<aspnet_Membership, bool>> expression = m => m.aspnet_Applications.ApplicationName == applicationName;
            return dataContext.QueryMemberships(expression);
        }
        /// <summary>
        /// Queries the memberships.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        public static IQueryable<aspnet_Membership> QueryMemberships(this IEverestCmsDataContext dataContext, Expression<Func<aspnet_Membership, bool>> expression)
        {
            return dataContext.aspnet_Membership.Where(expression);
        }
        #endregion

        #region QueryValidatorGroup
        /// <summary>
        /// Queries the validator group.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="name">The name.</param>
        /// <param name="application">The application.</param>
        /// <returns></returns>
        public static IQueryable<Cms_ValidatorGroup> IsValidatorGroupExists(this IEverestCmsDataContext dataContext,
            string name, string application, int groupId)
        {
            return dataContext.QueryValidatorGroups(application).Where(vg => vg.ValidateGroupName == name && vg.ValidateGroupId != groupId);
        }
        /// <summary>
        /// Queries the validator group.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="validatorGroupId">The validator group id.</param>
        /// <returns></returns>
        public static IQueryable<Cms_ValidatorGroup> QueryValidatorGroup(this IEverestCmsDataContext dataContext,
            int validatorGroupId)
        {
            Expression<Func<Cms_ValidatorGroup, bool>> expression = vg => vg.ValidateGroupId == validatorGroupId;
            return dataContext.QueryValidatorGroups(expression);
        }
        public static IQueryable<Cms_ValidatorGroup> QueryValidatorGroup(this IEverestCmsDataContext dataContext,
            Guid validatorGroupUUID)
        {
            Expression<Func<Cms_ValidatorGroup, bool>> expression = vg => vg.UUID == validatorGroupUUID;
            return dataContext.QueryValidatorGroups(expression);
        }
        public static IQueryable<Cms_ValidatorGroup> QueryValidatorGroupsByValidator(this IEverestCmsDataContext dataContext, Guid validatorUUID)
        {
            Expression<Func<Cms_ValidatorGroup, bool>> expression = vg => vg.Cms_Validator.Any(v => v.UUID == validatorUUID);
            return dataContext.QueryValidatorGroups(expression);
        }
        public static IQueryable<Cms_ValidatorGroup> QueryValidatorGroup(this IEverestCmsDataContext dataContext, string application,
            string validatorGroupName)
        {
            return dataContext.QueryValidatorGroups(application).Where(vg => vg.ValidateGroupName == validatorGroupName);
        }
        //public static IQueryable<Cms_ValidatorGroup> QueryValidatorGroups(this IEverestCmsDataContext dataContext,
        //    string application)
        //{
        //    Cms_Folder folder = dataContext.QueryFolders(application, FolderType.ValidatorGroup).First();
        //    return dataContext.QueryValidatorGroups(folder);
        //}
        ///// <summary>
        ///// Queries the validator groups.
        ///// </summary>
        ///// <param name="dataContext">The data context.</param>
        ///// <param name="folderId">The folder id.</param>
        ///// <returns></returns>
        //public static IQueryable<Cms_ValidatorGroup> QueryValidatorGroups(this IEverestCmsDataContext dataContext,
        //    Guid folderUUID)
        //{
        //    Cms_Folder folder = dataContext.QueryFolder(folderUUID).First();
        //    return dataContext.QueryValidatorGroups(folder);
        //}
        /// <summary>
        /// Queries the validator groups.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="folder">The folder.</param>
        /// <returns></returns>
        public static IQueryable<Cms_ValidatorGroup> QueryValidatorGroups(this IEverestCmsDataContext dataContext,
            string application)
        {
            var apps = CachedData.GetBaseApplications(application);
            var expression = EfUtility.BuildContainsExpression<Cms_ValidatorGroup, string>(c => c.aspnet_Applications.ApplicationName, apps);
            return dataContext.QueryValidatorGroups(expression);
        }
        public static IQueryable<Cms_ValidatorGroup> QueryValidatorGroups(this IEverestCmsDataContext dataContext,
            Expression<Func<Cms_ValidatorGroup, bool>> expression)
        {
            return dataContext.Cms_ValidatorGroup.Where(expression);
        }
        #endregion

        #region QueryValidator
        /// <summary>
        /// Queries the validator.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="validatorName">Name of the validator.</param>
        /// <param name="application">The application.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Validator> IsValidatorExists(this IEverestCmsDataContext dataContext, string validatorName, string application
            , int validatorId)
        {
            return dataContext.QueryValidators(application).Where(v => v.ValidatorName == validatorName && v.ValidatorId != validatorId);
        }
        /// <summary>
        /// Queries the validator.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="validatorId">The validator id.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Validator> QueryValidator(this IEverestCmsDataContext dataContext, int validatorId)
        {
            Expression<Func<Cms_Validator, bool>> expression = cv => cv.ValidatorId == validatorId;
            return dataContext.QueryValidators(expression);
        }
        public static IQueryable<Cms_Validator> QueryValidator(this IEverestCmsDataContext dataContext, Guid validatorUUID)
        {
            Expression<Func<Cms_Validator, bool>> expression = cv => cv.UUID == validatorUUID;
            return dataContext.QueryValidators(expression);
        }

        public static IQueryable<Cms_Validator> QueryValidators(this IEverestCmsDataContext dataContext, params Guid[] validatorsId)
        {
            Expression<Func<Cms_Validator, bool>> expression = f => false;
            foreach (var id in validatorsId)
            {
                //ef bug. All of the parameters will be set the same value use the same variable.
                var uuid = id;
                expression = expression.Or(f => f.UUID == uuid);
            }
            return dataContext.QueryValidators(expression);
        }

        /// <summary>
        /// Queries the validators.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="folder">The folder.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Validator> QueryValidators(this IEverestCmsDataContext dataContext, string application)
        {
            var apps = CachedData.GetBaseApplications(application);
            var expression = EfUtility.BuildContainsExpression<Cms_Validator, string>(c => c.aspnet_Applications.ApplicationName, apps);
            return dataContext.QueryValidators(expression);

        }
        /// <summary>
        /// Queries the validators.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Validator> QueryValidators(this IEverestCmsDataContext dataContext, Expression<Func<Cms_Validator, bool>> expression)
        {
            return dataContext.Cms_Validator.Where(expression);
        }
        #endregion

        #region QueryProperty
        /// <summary>
        /// Queries the property.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="folderId">The folder id.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Properties> QueryProperty(this IEverestCmsDataContext dataContext, Guid folderUUID)
        {
            Expression<Func<Cms_Properties, bool>> expression = p => p.Cms_Folder.UUID == folderUUID;
            return dataContext.QueryProperties(expression);
        }
        /// <summary>
        /// Queries the properties.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Properties> QueryProperties(this IEverestCmsDataContext dataContext, Expression<Func<Cms_Properties, bool>> expression)
        {
            return dataContext.Cms_Properties.Where(expression);
        }
        #endregion

        #region QueryColumns
        /// <summary>
        /// Queries the columns by validator group.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="validatorGroupUUID">The validator group UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Column> QueryColumnsByValidatorGroup(this IEverestCmsDataContext dataContext, Guid validatorGroupUUID)
        {
            Expression<Func<Cms_Column, bool>> expression = c => c.Cms_ValidatorGroup.UUID == validatorGroupUUID;
            return dataContext.QueryColumns(expression);
        }
        public static IQueryable<Cms_Column> QueryColumns(this IEverestCmsDataContext dataContext, Expression<Func<Cms_Column, bool>> expression)
        {
            return dataContext.Cms_Column.Where(expression);
        }
        #endregion

        #region QueryWorkflow
        /// <summary>
        /// Determines whether [is workflow exists] [the specified data context].
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <param name="workflowName">Name of the workflow.</param>
        /// <param name="workflowId">The workflow id.</param>
        /// <returns>
        ///   <c>true</c> if [is workflow exists] [the specified data context]; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsWorkflowExists(this IEverestCmsDataContext dataContext, string application, string workflowName, int workflowId)
        {
            return dataContext.QueryWorkflows(application).Any(wf => wf.WorkflowName == workflowName && wf.WorkflowId != workflowId);
        }
        public static IQueryable<Cms_Workflow> QueryWorkflow(this IEverestCmsDataContext dataContext, int workflowId)
        {
            Expression<Func<Cms_Workflow, bool>> expression = wf => wf.WorkflowId == workflowId;
            return dataContext.QueryWorkflows(expression);
        }
        public static IQueryable<Cms_Workflow> QueryWorkflow(this IEverestCmsDataContext dataContext, Guid workflowUUID)
        {
            Expression<Func<Cms_Workflow, bool>> expression = wf => wf.UUID == workflowUUID;
            return dataContext.QueryWorkflows(expression);
        }
        public static IQueryable<Cms_Workflow> QueryWorkflowByFolder(this IEverestCmsDataContext dataContext, Guid folderUUID)
        {
            Expression<Func<Cms_Workflow, bool>> expression = wf => wf.Cms_Folder.Any(f => f.UUID == folderUUID);
            return dataContext.QueryWorkflows(expression);
        }
        public static IQueryable<Cms_Workflow> QueryWorkflows(this IEverestCmsDataContext dataContext, string application, string workflowName)
        {
            var queryable = dataContext.QueryWorkflows(application);
            if (!StringExtensions.IsNullOrEmptyTrim(workflowName))
            {
                return queryable.Where(wf => wf.WorkflowName.Contains(workflowName)).OrderBy(wf => wf.WorkflowId);
            }
            else
            {
                return queryable;
            }

        }
        public static IQueryable<Cms_Workflow> QueryWorkflows(this IEverestCmsDataContext dataContext, string application)
        {
            var apps = CachedData.GetBaseApplications(application);
            var expression = EfUtility.BuildContainsExpression<Cms_Workflow, string>(c => c.aspnet_Applications.ApplicationName, apps);
            return dataContext.QueryWorkflows(expression).OrderBy(wf => wf.WorkflowId);
        }
        public static IQueryable<Cms_Workflow> QueryWorkflows(this IEverestCmsDataContext dataContext, Expression<Func<Cms_Workflow, bool>> expression)
        {
            return dataContext.Cms_Workflow.Where(expression);
        }
        #endregion

        #region QueryWorkflowSequence
        public static IQueryable<Cms_WorkflowSequence> QueryPreviousWorkflowSequence(this IEverestCmsDataContext dataContext, Cms_Workflow workflow, int currentStep)
        {
            Expression<Func<Cms_WorkflowSequence, bool>> expression = wfs => wfs.Cms_Workflow.UUID == workflow.UUID && wfs.SequenceOrder == currentStep - 1;
            return dataContext.QueryWorkflowSequences(expression);
        }
        public static IQueryable<Cms_WorkflowSequence> QueryNextWorkflowSequence(this IEverestCmsDataContext dataContext, Cms_Workflow workflow, int currentStep)
        {
            Expression<Func<Cms_WorkflowSequence, bool>> expression = wfs => wfs.Cms_Workflow.UUID == workflow.UUID && wfs.SequenceOrder == currentStep + 1;
            return dataContext.QueryWorkflowSequences(expression);
        }
        public static IQueryable<Cms_WorkflowSequence> QueryWorkflowSequenceByRoles(this IEverestCmsDataContext dataContext, Guid workflowUUID, IEnumerable<string> roles)
        {
            Expression<Func<Cms_WorkflowSequence, bool>> expression = wfs => wfs.Cms_Workflow.UUID == workflowUUID;
            expression = expression.And(EfUtility.BuildContainsExpression<Cms_WorkflowSequence, string>(wfs => wfs.RoleName, roles));
            return dataContext.QueryWorkflowSequences(expression).OrderByDescending(wfs => wfs.SequenceOrder);

        }
        public static IQueryable<Cms_WorkflowSequence> QueryWorkflowSequences(this IEverestCmsDataContext dataContext, Expression<Func<Cms_WorkflowSequence, bool>> expression)
        {
            return dataContext.Cms_WorkflowSequence.Where(expression);
        }
        #endregion

        #region QueryWorkflowHistory
        public static IQueryable<Cms_Workflow> QueryWorkflowByByContent(this IEverestCmsDataContext dataContext, Guid contentUUID)
        {
            Expression<Func<Cms_WorkflowHistory, bool>> expression = wfh => wfh.Cms_Content.UUID == contentUUID;
            return dataContext.QueryWorkflowHistories(expression).Select(wfh => wfh.Cms_Workflow);
        }
        public static IQueryable<Cms_WorkflowHistory> QueryWorkflowHistories(this IEverestCmsDataContext dataContext, Guid contentUUID)
        {
            Expression<Func<Cms_WorkflowHistory, bool>> expression = wfh => wfh.Cms_Content.UUID == contentUUID;
            return dataContext.QueryWorkflowHistories(expression);
        }
        public static IQueryable<Cms_WorkflowHistory> QueryWorkflowHistory(this IEverestCmsDataContext dataContext, Guid historyUUID)
        {
            Expression<Func<Cms_WorkflowHistory, bool>> expression = wfh => wfh.UUID == historyUUID;
            return dataContext.QueryWorkflowHistories(expression);
        }
        public static IQueryable<Cms_WorkflowHistory> QueryWorkflowHistories(this IEverestCmsDataContext dataContext, Expression<Func<Cms_WorkflowHistory, bool>> expression)
        {
            return dataContext.Cms_WorkflowHistory.Where(expression);
        }
        #endregion

        #region QueryContentInFolder
        public static IQueryable<Cms_ContentInFolder> QueryContentInclusion(this IEverestCmsDataContext dataContext, Guid contentUUID)
        {
            Expression<Func<Cms_ContentInFolder, bool>> expression = cf => cf.Cms_Content.UUID == contentUUID;
            return dataContext.QueryContentInFolders(expression);
        }
        public static IQueryable<Cms_ContentInFolder> QueryContentInFolder(this IEverestCmsDataContext dataContext, Guid contentUUID, Guid folderUUID)
        {
            Expression<Func<Cms_ContentInFolder, bool>> expression = cf => cf.Cms_Content.UUID == contentUUID && cf.Cms_Folder.UUID == folderUUID;
            return dataContext.QueryContentInFolders(expression);
        }
        public static IQueryable<Cms_ContentInFolder> QueryContentInFolders(this IEverestCmsDataContext dataContext, Expression<Func<Cms_ContentInFolder, bool>> expression)
        {
            return dataContext.Cms_ContentInFolder.Where(expression);
        }
        #endregion

        #region QuerySchedule
        /// <summary>
        /// Queries the schedules.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Schedule> QuerySchedules(this IEverestCmsDataContext dataContext, int[] id)
        {
            Expression<Func<Cms_Schedule, bool>> expression = EfUtility.BuildContainsExpression<Cms_Schedule, int>(s => s.ScheduleId, id);
            return dataContext.QuerySchedules(expression);
        }
        /// <summary>
        /// Queries the schedules.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="expression">The expression.</param>
        /// <returns></returns>
        public static IQueryable<Cms_Schedule> QuerySchedules(this IEverestCmsDataContext dataContext, Expression<Func<Cms_Schedule, bool>> expression)
        {
            return dataContext.Cms_Schedule.Where(expression);
        }
        #endregion

        #region QueryModulePermission
        /// <summary>
        /// Queries the module permissions.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <param name="moduleName">Name of the module.</param>
        /// <returns></returns>
        public static IQueryable<Cms_ModulePermission> QueryModulePermissions(this IEverestCmsDataContext dataContext, string application, string moduleName)
        {
            Expression<Func<Cms_ModulePermission, bool>> expression = mp => mp.ModuleName == moduleName;
            if (!StringExtensions.IsNullOrEmptyTrim(application))
            {
                expression = expression.And(mp => mp.aspnet_Applications.ApplicationName == application);
            }
            return dataContext.Cms_ModulePermission.Where(expression);
        }
        #endregion

        #region QueryModuleSetting
        public static IQueryable<Cms_ModuleSetting> QueryModuleSettings(this IEverestCmsDataContext dataContxt, string application, string moduleName)
        {
            return dataContxt.Cms_ModuleSetting.Where(mp => mp.aspnet_Applications.ApplicationName == application && mp.ModuleName == moduleName);
        }
        #endregion

        #region QueryModuleApplication
        public static IQueryable<Cms_ModuleApplication> QueryModuleApplications(this IEverestCmsDataContext dataContext, string moduleName)
        {
            return dataContext.Cms_ModuleApplication.Where(ma => ma.ModuleName == moduleName);
        }
        #endregion

        #region QueryWebForm
        /// <summary>
        /// Queries the web forms.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <returns></returns>
        public static IQueryable<Cms_CustomerForm> QueryWebForms(this IEverestCmsDataContext dataContext, string application)
        {
            Expression<Func<Cms_CustomerForm, bool>> condition = s => s.aspnet_Applications.ApplicationName == application;
            IQueryable<Cms_CustomerForm> query = dataContext.Cms_CustomerForm.Where(condition).OrderByDescending(frm => frm.FormId);
            return query;
        }
        /// <summary>
        /// Queries the web form.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="webFormUUID">The web form UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_CustomerForm> QueryWebForm(this IEverestCmsDataContext dataContext, Guid webFormUUID)
        {
            IQueryable<Cms_CustomerForm> query = dataContext.Cms_CustomerForm.Where(wf => wf.UUID == webFormUUID);
            return query;
        }
        #endregion

        #region QuerySearchSetting
        /// <summary>
        /// Queries the search settings.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="application">The application.</param>
        /// <returns></returns>
        public static IQueryable<Cms_SearchSetting> QuerySearchSettings(this IEverestCmsDataContext dataContext, string application)
        {
            return dataContext.Cms_SearchSetting.Where(s => s.aspnet_Applications.ApplicationName == application);
        }

        /// <summary>
        /// Queries the search settings.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="folderUUID">The folder UUID.</param>
        /// <returns></returns>
        public static IQueryable<Cms_SearchSetting> QuerySearchSettings(this IEverestCmsDataContext dataContext, Guid folderUUID)
        {
            return dataContext.Cms_SearchSetting.Where(s => s.Cms_Folder.UUID == folderUUID);
        }
        #endregion

    }
}

www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.