/*
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.Text;
using System.Data.Objects.DataClasses;
using Everest.CmsServices.Models;
using Everest.Library.Providers.Caching;
using Everest.Library.ExtensionMethod;
namespace Everest.CmsServices.Models{
/// <summary>
///
/// </summary>
public static class CachedData
{
public readonly static ICacheItemExpiration[] DefaultExpirations;
static CachedData()
{
DefaultExpirations = new ICacheItemExpiration[1];
DefaultExpirations[0] = new SlidingTime("00:30:00");
}
#region GroupName
/// <summary>
/// Use in Application,Folder,
/// </summary>
public const string Application = "__Cache__Application";
/// <summary>
/// Use in TextSchema,BinarySchema,Validator,ValidatorGroup.
/// </summary>
public const string Schema = "__Cache__Schema";
/// <summary>
/// Use in ContentTemplate,PageTemplate,TextResource
/// </summary>
public const string Template = "__Cache__Template";
/// <summary>
/// Use in Page,ContentTemplateInPage
/// </summary>
public const string Page = "__Cache__Page";
/// <summary>
/// Page,ContentTemplate
/// </summary>
public const string DataRule = "__Cache__DataRule";
/// <summary>
/// Folder
/// </summary>
public const string Folder = "__Cache_Folder";
/// <summary>
/// ModulePermission
/// </summary>
public const string Permission = "_Cache_Permission";
/// <summary>
/// ModuleSetting
/// </summary>
public const string ModuleSetting = "_CacheGroup_ModuleSetting";
public const string WebForm = "_CacheGroup_WebForm";
public const string Content = "__BinaryContent";
/// <summary>
/// SearchSetting
/// </summary>
public const string SearchSetting = "_CacheGroup_SearchSetting";
#endregion
#region GetCacheGroupName
public static IEnumerable<string> GetCacheGroupNames(Type entityType)
{
switch (entityType.FullName)
{
case "Everest.CmsServices.Models.Cms_ContentTemplate":
case "Everest.CmsServices.Models.Cms_PageTemplate":
case "Everest.CmsServices.Models.Cms_TextResource":
return new string[] { CachedData.Template, CachedData.DataRule };
case "Everest.CmsServices.Models.Cms_Schema":
case "Everest.CmsServices.Models.Cms_Validator":
case "Everest.CmsServices.Models.Cms_ValidatorGroup":
case "Everest.CmsServices.Models.Cms_SchemaFunction":
return new string[] { CachedData.Schema, CachedData.Folder };
case "Everest.CmsServices.Models.aspnet_Applications":
case "Everest.CmsServices.Models.CmsApplicationsInUser":
case "Everest.CmsServices.Models.aspnet_Membership":
return new string[] { CachedData.Application, CachedData.Permission };
case "Everest.CmsServices.Models.Cms_Page":
case "Everest.CmsServices.Models.Cms_ContentTemplateInPageHolder":
return new string[] { CachedData.Page, CachedData.DataRule };
case "Everest.CmsServices.Models.Cms_Folder":
return new string[] { CachedData.Folder };
case "Everest.CmsServices.Models.Cms_ModulePermission":
case "Everest.CmsServices.Models.Cms_Permission":
case "Everest.CmsServices.Models.aspnet_Users":
case "Everest.CmsServices.Models.aspnet_Roles":
return new string[] { CachedData.Permission };
case "Everest.CmsServices.Models.Cms_ModuleSetting":
return new string[] { CachedData.ModuleSetting };
case "Everest.CmsServices.Models.Cms_CustomerForm":
return new string[] { CachedData.WebForm };
case "Everest.CmsServices.Models.Cms_SearchSetting":
return new string[] { CachedData.SearchSetting };
case "Everest.CmsServices.Models.Cms_BinaryContent":
case "Everest.CmsServices.Models.Cms_Content":
return new string[] { CachedData.Content };
default:
return null;
}
}
#endregion
#region Get Application
/// <summary>
/// Gets the cached applicaiton.
/// </summary>
/// <param name="applicationName">Name of the application.</param>
/// <returns></returns>
public static aspnet_Applications GetApplication(string applicationName)
{
string cacheKey = "__Common_GetApplication__Name_" + applicationName;
aspnet_Applications application = CacheManager.Get(CachedData.Application, cacheKey) as aspnet_Applications;
if (application == null && CacheManager.Get(CachedData.Application, cacheKey) == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
application = dataContext.QueryApplication(applicationName).FirstOrDefault();
//detach entity object
if (application != null)
{
dataContext.ObjectContext.Detach(application);
CacheManager.Add(CachedData.Application, cacheKey, application, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
else
{
CacheManager.Add(CachedData.Application, cacheKey, cacheKey, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
}
return application;
}
/// <summary>
/// Gets the cached application.
/// </summary>
/// <param name="applicationId">The application id.</param>
/// <returns></returns>
public static aspnet_Applications GetApplication(Guid applicationId)
{
string cacheKey = string.Format("__Common_GetApplication__applicationId:{0}", applicationId);
aspnet_Applications application = CacheManager.Get(CachedData.Application, cacheKey) as aspnet_Applications;
if (application == null && CacheManager.Get(CachedData.Application, cacheKey) == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
application = dataContext.QueryApplication(applicationId).FirstOrDefault();
//detach entity object
if (application != null)
{
dataContext.ObjectContext.Detach(application);
CacheManager.Add(CachedData.Application, cacheKey, application, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
else
{
CacheManager.Add(CachedData.Application, cacheKey, cacheKey, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
}
return application;
}
/// <summary>
/// Gets the name of the cached application by host.
/// </summary>
/// <param name="hostName">Name of the host.</param>
/// <returns></returns>
public static aspnet_Applications GetApplicationByHostName(string hostName)
{
string cacheKey = "__Common_GetApplicationByHostName__HostName_" + hostName;
aspnet_Applications application = CacheManager.Get(CachedData.Application, cacheKey) as aspnet_Applications;
if (application == null && CacheManager.Get(CachedData.Application, cacheKey) == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
application = dataContext.QueryApplicationByHostName(hostName).FirstOrDefault();
//detach entity object
if (application != null)
{
dataContext.ObjectContext.Detach(application);
CacheManager.Add(CachedData.Application, cacheKey, application, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
else
{
CacheManager.Add(CachedData.Application, cacheKey, cacheKey, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
}
return application;
}
public static aspnet_Applications GetApplicationByHostNameNVirtualPath(string hostName, string virtualPath)
{
string cacheKey = string.Format("__Common_GetApplicationByHostNameNVirtualPath__HostName_{0}_VirtualPath_{1}", hostName, virtualPath);
aspnet_Applications application = CacheManager.Get(CachedData.Application, cacheKey) as aspnet_Applications;
if (application == null && CacheManager.Get(CachedData.Application, cacheKey) == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
application = dataContext.QueryApplicationByHostNameNVirtualPath(hostName, virtualPath).FirstOrDefault();
//detach entity object
if (application != null)
{
dataContext.ObjectContext.Detach(application);
CacheManager.Add(CachedData.Application, cacheKey, application, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
else
{
CacheManager.Add(CachedData.Application, cacheKey, cacheKey, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
}
return application;
}
/// <summary>
/// Gets the application by page template.
/// </summary>
/// <param name="pageTemplateUUID">The page template UUID.</param>
/// <returns></returns>
public static aspnet_Applications GetApplicationByPageTemplate(Guid pageTemplateUUID)
{
string cacheKey = string.Format("__Common_GetApplicationByPageTemplate__PageTemplate:{0}", pageTemplateUUID);
aspnet_Applications application = CacheManager.Get(CachedData.Application, cacheKey) as aspnet_Applications;
if (application == null && CacheManager.Get(CachedData.Application, cacheKey) == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
application = dataContext.QueryPageTemplate(pageTemplateUUID).Select(pt => pt.aspnet_Applications).FirstOrDefault();
//detach entity object
if (application != null)
{
dataContext.ObjectContext.Detach(application);
CacheManager.Add(CachedData.Application, cacheKey, application, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
else
{
CacheManager.Add(CachedData.Application, cacheKey, cacheKey, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
}
return application;
}
/// <summary>
/// Gets the application by schema.
/// </summary>
/// <param name="schemaUUID">The schema UUID.</param>
/// <returns></returns>
public static aspnet_Applications GetApplicationBySchema(Guid schemaUUID)
{
string cacheKey = string.Format("__Common_GetApplicationBySchema__SchemaUUID:{0}", schemaUUID);
aspnet_Applications application = CacheManager.Get(CachedData.Application, cacheKey) as aspnet_Applications;
if (application == null && CacheManager.Get(CachedData.Application, cacheKey) == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
application = dataContext.QuerySchema(schemaUUID).Select(pt => pt.aspnet_Applications).FirstOrDefault();
//detach entity object
if (application != null)
{
dataContext.ObjectContext.Detach(application);
CacheManager.Add(CachedData.Application, cacheKey, application, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
else
{
CacheManager.Add(CachedData.Application, cacheKey, cacheKey, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
}
return application;
}
/// <summary>
/// Gets the application by content template.
/// </summary>
/// <param name="contentTemplateUUID">The content template UUID.</param>
/// <returns></returns>
public static aspnet_Applications GetApplicationByContentTemplate(Guid contentTemplateUUID)
{
string cacheKey = string.Format("__Common_GetApplicationByContentTemplate__ContentTemplate:{0}", contentTemplateUUID);
aspnet_Applications application = CacheManager.Get(CachedData.Application, cacheKey) as aspnet_Applications;
if (application == null && CacheManager.Get(CachedData.Application, cacheKey) == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
application = dataContext.QueryContentTemplate(contentTemplateUUID).Select(pt => pt.aspnet_Applications).FirstOrDefault();
//detach entity object
if (application != null)
{
dataContext.ObjectContext.Detach(application);
CacheManager.Add(CachedData.Application, cacheKey, application, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
else
{
CacheManager.Add(CachedData.Application, cacheKey, cacheKey, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
}
return application;
}
/// <summary>
/// Gets the base applications.
/// </summary>
/// <param name="applicationName">Name of the application.</param>
/// <returns></returns>
public static IEnumerable<string> GetBaseApplications(string applicationName)
{
string cacheKey = string.Format("__GetBaseApplications__ApplicationName:{0}", applicationName);
List<string> apps = CacheManager.Get(CachedData.Application, cacheKey) as List<string>;
if (apps == null)
{
apps = new List<string>();
var application = CachedData.GetApplication(applicationName);
do
{
apps.Add(application.ApplicationName);
if (!StringExtensions.IsNullOrEmptyTrim(application.BaseApplication))
{
application = CachedData.GetApplication(application.BaseApplication);
}
else
{
application = null;
}
} while (application != null);
CacheManager.Add(CachedData.Application, cacheKey, apps, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return apps;
}
/// <summary>
/// Gets the applications by user.
/// </summary>
/// <param name="userName">Name of the user.</param>
/// <returns></returns>
public static IEnumerable<string> GetApplicationsByUser(string userName)
{
string cacheKey = string.Format("__GetApplicationsByUser__UserName:{0}", userName);
string[] apps = CacheManager.Get(CachedData.Application, cacheKey) as string[];
if (apps == null)
{
var dataContext = EverestCmsEntities.GetDataContext();
apps = dataContext.QueryApplicationsByUser(userName).Select(a => a.ApplicationName).ToArray();
CacheManager.Add(CachedData.Application, cacheKey, apps, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return apps;
}
/// <summary>
/// Gets the custom errors.
/// </summary>
/// <param name="application">The application.</param>
/// <returns></returns>
public static IEnumerable<CustomError> GetCustomErrors(string application)
{
string cacheKey = string.Format("__GetCustomErrors_ApplicationName:{0}", application);
CustomError[] customErrors = CacheManager.Get(CachedData.Application, cacheKey) as CustomError[];
if (customErrors == null)
{
var dataContext = EverestCmsEntities.GetDataContext();
customErrors = dataContext.Cms_CustomError.Where(c => c.aspnet_Applications.ApplicationName == application)
.Select(c => new CustomError() { StatusCodeValue = c.StatusCode, Redirect = c.Redirect }).ToArray();
CacheManager.Add(CachedData.Application, cacheKey, customErrors, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return customErrors;
}
#endregion
#region Get Schema
/// <summary>
/// Gets the cached schema.
/// </summary>
/// <param name="dataContext">The data context.</param>
/// <param name="schemaId">The schema id.</param>
/// <returns></returns>
public static Cms_Schema GetSchema(int schemaId)
{
string cacheKey = string.Format("_GetSchema_SchemaId:{0}", schemaId);
Cms_Schema schema = CacheManager.Get(CachedData.Schema, cacheKey) as Cms_Schema;
if (schema == null && CacheManager.Get(CachedData.Schema, cacheKey) == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
schema = dataContext.QuerySchema(schemaId).FirstOrDefault();
//detach entity object
if (schema != null)
{
dataContext.ObjectContext.Detach(schema);
CacheManager.Add(CachedData.Schema, cacheKey, schema, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
else
{
CacheManager.Add(CachedData.Schema, cacheKey, cacheKey, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
}
return schema;
}
/// <summary>
/// Gets the cached schema.
/// </summary>
/// <param name="schemaUUID">The schema UUID.</param>
/// <returns></returns>
public static Cms_Schema GetSchema(Guid schemaUUID)
{
string cacheKey = string.Format("_GetSchema_SchemaUUID:{0}", schemaUUID);
Cms_Schema schema = CacheManager.Get(CachedData.Schema, cacheKey) as Cms_Schema;
if (schema == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
schema = dataContext.QuerySchema(schemaUUID).First();
//detach entity object
if (schema != null)
{
dataContext.ObjectContext.Detach(schema);
}
CacheManager.Add(CachedData.Schema, cacheKey, schema, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return schema;
}
/// <summary>
/// Gets the schema.
/// </summary>
/// <param name="application">The application.</param>
/// <param name="FormSchemaName">Name of the schema.</param>
/// <returns></returns>
public static Cms_Schema GetSchema(string application, string FormSchemaName)
{
string cacheKey = string.Format("_GetSchema_application:{0}_FormSchemaName:{1}", application, FormSchemaName);
Cms_Schema schema = CacheManager.Get(CachedData.Schema, cacheKey) as Cms_Schema;
if (schema == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
schema = dataContext.QuerySchema(application, FormSchemaName).First();
//detach entity object
dataContext.ObjectContext.Detach(schema);
CacheManager.Add(CachedData.Schema, cacheKey, schema, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return schema;
}
/// <summary>
/// Gets the schema by folder.
/// </summary>
/// <param name="folderUUID">The folder UUID.</param>
/// <returns></returns>
public static Cms_Schema GetSchemaByFolder(Guid folderUUID)
{
string cacheKey = string.Format("_GetSchemaByFolder_FolderUUID:{0}", folderUUID);
Cms_Schema schema = CacheManager.Get(CachedData.Schema, cacheKey) as Cms_Schema;
if (schema == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
schema = dataContext.QueryFolder(folderUUID).Select(f => f.Cms_Schema).First();
//detach entity object
dataContext.ObjectContext.Detach(schema);
CacheManager.Add(CachedData.Schema, cacheKey, schema, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return schema;
}
/// <summary>
/// Gets the columns by schema.
/// </summary>
/// <param name="schemaUUID">The schema UUID.</param>
/// <returns></returns>
public static IEnumerable<Cms_Column> GetColumnsBySchema(Guid schemaUUID)
{
string cacheKey = string.Format("_GetColumnsBySchema_SchemaUUID:{0}", schemaUUID);
IEnumerable<Cms_Column> columns = CacheManager.Get(CachedData.Schema, cacheKey) as IEnumerable<Cms_Column>;
if (columns == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
columns = dataContext.QueryColumns(c => c.Cms_Schema.UUID == schemaUUID && c.ColumnName != Cms_Schema.TitleColumnName).ToArray();
//detach entity object
foreach (var column in columns)
{
dataContext.ObjectContext.Detach(column);
}
CacheManager.Add(CachedData.Schema, cacheKey, columns, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return columns;
}
/// <summary>
/// Gets the function by schema.
/// </summary>
/// <param name="schemaUUID">The schema UUID.</param>
/// <param name="functionType">Type of the function.</param>
/// <returns></returns>
public static IEnumerable<QueriedFunction> GetFunctionBySchema(Guid schemaUUID, FunctionType functionType)
{
string cacheKey = string.Format("_GetFunctionBySchema_SchemaUUID:{0}_FunctionType:{1}", schemaUUID, functionType);
IEnumerable<QueriedFunction> functions = CacheManager.Get(CachedData.Schema, cacheKey) as IEnumerable<QueriedFunction>;
if (functions == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
functions = dataContext.QuerySchemaFunctions(schemaUUID, functionType).Select(f => new QueriedFunction() { FunctionId = f.FunctionId, Name = f.Name, Script = f.Script }).ToArray();
CacheManager.Add(CachedData.Schema, cacheKey, functions, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return functions;
}
#endregion
#region Get Cached Page
/// <summary>
/// Gets the cached page.
/// </summary>
/// <param name="applicationName">Name of the application.</param>
/// <param name="loweredPageName">Name of the lowered page.</param>
/// <returns></returns>
public static Cms_Page GetPage(string applicationName, string loweredPageName)
{
string cacheKey = string.Format("__GetPage_Application:{0}__loweredPage:{1}", applicationName, loweredPageName);
Cms_Page page = CacheManager.Get(CachedData.Page, cacheKey) as Cms_Page;
if (page == null && CacheManager.Get(CachedData.Page, cacheKey) == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
page = dataContext.QueryPage(applicationName, loweredPageName).FirstOrDefault();
//detach entity object
if (page != null)
{
dataContext.ObjectContext.Detach(page);
CacheManager.Add(CachedData.Page, cacheKey, page, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
else
{
CacheManager.Add(CachedData.Page, cacheKey, cacheKey, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
}
return page;
}
public static string GetPageVirtualPath(Guid pageUUID)
{
string cacheKey = string.Format("__GetPageVirtualPath_PageUUID:{0}", pageUUID);
string virtualPath = CacheManager.Get(CachedData.Page, cacheKey) as string;
if (virtualPath == null && CacheManager.Get(CachedData.Page, cacheKey) == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
var page = dataContext.QueryPage(pageUUID).FirstOrDefault();
//detach entity object
if (page != null)
{
virtualPath = page.PageName;
page.Cms_ParentPageReference.Load();
if (page.Cms_ParentPage != null)
{
var parentVirutalPath = GetPageVirtualPath(page.Cms_ParentPage.UUID);
virtualPath = parentVirutalPath + "/" + virtualPath;
}
CacheManager.Add(CachedData.Page, cacheKey, virtualPath, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
else
{
CacheManager.Add(CachedData.Page, cacheKey, "", CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
}
return virtualPath;
}
/// <summary>
/// Gets the page by URL.
/// </summary>
/// <param name="requestUrl">The request URL.</param>
/// <param name="application">The application.</param>
/// <param name="pageVirtualPath">The page virtual path.</param>
/// <returns></returns>
internal static Cms_Page GetPageByUrl(string requestUrl, string application, out string pageVirtualPath)
{
pageVirtualPath = "";
if (string.IsNullOrEmpty(requestUrl))
{
return null;
}
string[] pathNames = requestUrl.Split('/');
Cms_Page parentPage = null;
Cms_Page currentPage = null;
pageVirtualPath = "";
foreach (var pageName in pathNames)
{
if (string.IsNullOrEmpty(pageName))
{
break;
}
parentPage = currentPage;
var parentUUID = parentPage == null ? new Nullable<Guid>() : parentPage.UUID;
currentPage = GetPageByNameNParent(parentUUID, pageName, application);
if (currentPage != null)
{
parentPage = currentPage;
if (string.IsNullOrEmpty(pageVirtualPath))
{
pageVirtualPath = pageName;
}
else
{
pageVirtualPath = pageVirtualPath + "/" + pageName;
}
}
else
{
break;
}
}
return parentPage;
}
/// <summary>
/// Gets the page by virtual path.
/// </summary>
/// <param name="application">The application.</param>
/// <param name="virtualPath">The virtual path.</param>
/// <returns></returns>
internal static Cms_Page GetPageByVirtualPath(string application, string virtualPath)
{
string validVirtualPath;
Cms_Page cms_page = CachedData.GetPageByUrl(virtualPath, application, out validVirtualPath);
if (cms_page == null)
{
cms_page = CachedData.GetPage(application, virtualPath.ToLower());
}
return cms_page;
}
private static Cms_Page GetPageByNameNParent(Guid? parentUUID, string pageName, string applicationName)
{
string cacheKey = string.Format("__Common_GetPageByNameNParent_ParentPage:{0}__PageName:{1}_ApplicationName:{2}", parentUUID, pageName, applicationName);
Cms_Page page = CacheManager.Get(CachedData.Page, cacheKey) as Cms_Page;
if (page == null && CacheManager.Get(CachedData.Page, cacheKey) == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
page = dataContext.QueryPageByNameNParent(parentUUID, pageName, applicationName).FirstOrDefault();
//detach entity object
if (page != null)
{
dataContext.ObjectContext.Detach(page);
CacheManager.Add(CachedData.Page, cacheKey, page, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
else
{
CacheManager.Add(CachedData.Page, cacheKey, cacheKey, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
}
return page;
}
/// <summary>
/// Gets the plugins by page.
/// </summary>
/// <param name="pageUUID">The page UUID.</param>
/// <returns></returns>
public static IEnumerable<Cms_Plugin> GetPluginsByPage(Guid pageUUID)
{
string cacheKey = string.Format("__PluginsByPage__PageId:{0}", pageUUID);
IEnumerable<Cms_Plugin> plugins = CacheManager.Get(CachedData.Page, cacheKey) as IEnumerable<Cms_Plugin>;
if (plugins == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
plugins = dataContext.Cms_Plugin.Where(cp => cp.Cms_Page.UUID == pageUUID).ToArray();
//detach entity object
//if (plugins != null)
//{
foreach (var plugin in plugins)
{
dataContext.ObjectContext.Detach(plugin);
}
//}
CacheManager.Add(CachedData.Page, cacheKey, plugins, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return plugins;
}
/// <summary>
/// Gets the page template by page.
/// </summary>
/// <param name="pageUUID">The page UUID.</param>
/// <returns></returns>
public static Cms_PageTemplate GetPageTemplateByPage(Guid pageUUID)
{
string cacheKey = string.Format("__GetPageTemplateByPage__PageId:{0}", pageUUID);
Cms_PageTemplate pageTemplate = CacheManager.Get(CachedData.Page, cacheKey) as Cms_PageTemplate;
if (pageTemplate == null && CacheManager.Get(CachedData.Page, cacheKey) == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
pageTemplate = dataContext.QueryPage(pageUUID).Select(p => p.Cms_PageTemplate).FirstOrDefault();
//detach entity object
if (pageTemplate != null)
{
dataContext.ObjectContext.Detach(pageTemplate);
CacheManager.Add(CachedData.Page, cacheKey, pageTemplate, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
else
{
CacheManager.Add(CachedData.Page, cacheKey, cacheKey, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
}
return pageTemplate;
}
/// <summary>
/// Gets the cached page template.
/// </summary>
/// <param name="applicationName">Name of the application.</param>
/// <param name="originalId">The original id.</param>
/// <returns></returns>
public static Cms_PageTemplate GetPageTemplate(string applicationName, Guid originalUUID)
{
string cacheKey = string.Format("__Common__Folder_Application:{0}_PageTemplateOriginalId:{1}", applicationName, originalUUID);
Cms_PageTemplate pageTemplate = CacheManager.Get(CachedData.Template, cacheKey) as Cms_PageTemplate;
if (pageTemplate == null && CacheManager.Get(CachedData.Template, cacheKey) == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
pageTemplate = dataContext.QueryPageTemplate(applicationName, originalUUID).OrderByDescending(pt => pt.PageTemplateId).FirstOrDefault();
//detach entity object
if (pageTemplate != null)
{
dataContext.ObjectContext.Detach(pageTemplate);
CacheManager.Add(CachedData.Template, cacheKey, pageTemplate, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
else
{
CacheManager.Add(CachedData.Template, cacheKey, cacheKey, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
}
return pageTemplate;
}
#endregion
#region Get Folder
/// <summary>
/// Gets the cached folder.
/// </summary>
/// <param name="applicationName">Name of the application.</param>
/// <param name="folderType">Type of the folder.</param>
/// <returns></returns>
public static Cms_Folder GetFolder(string applicationName, FolderType folderType)
{
string cacheKey = string.Format("__Common__Folder_Application:{0}_FolderType:{1}", applicationName, folderType.ToString());
Cms_Folder folder = CacheManager.Get(CachedData.Folder, cacheKey) as Cms_Folder;
if (folder == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.CreateDataContext(false);
folder = dataContext.QueryFolders(applicationName, folderType).First();
//folder.GetBaseFoldersRecusive();
//folder.GetBaseApplicationsRecusive();
//detach
dataContext.ObjectContext.Detach(folder);
CacheManager.Add(CachedData.Folder, cacheKey, folder, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return folder;
}
/// <summary>
/// Gets the cached folder.
/// </summary>
/// <param name="folderId">The folder id.</param>
/// <returns></returns>
public static Cms_Folder GetFolder(int folderId)
{
string cacheKey = string.Format("__Common_GetFolder__FolderId:{0}", folderId);
Cms_Folder folder = CacheManager.Get(CachedData.Folder, cacheKey) as Cms_Folder;
if (folder == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
folder = dataContext.QueryFolder(folderId).First();
dataContext.ObjectContext.Detach(folder);
CacheManager.Add(CachedData.Folder, cacheKey, folder, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return folder;
}
/// <summary>
/// Gets the cached folder.
/// </summary>
/// <param name="folderUUID">The folder UUID.</param>
/// <returns></returns>
public static Cms_Folder GetFolder(Guid folderUUID)
{
string cacheKey = string.Format("__Common_GetFolder__FolderUUID:{0}", folderUUID);
Cms_Folder folder = CacheManager.Get(CachedData.Folder, cacheKey) as Cms_Folder;
if (folder == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.CreateDataContext(false);
folder = dataContext.QueryFolder(folderUUID).First();
//get and cached the base folder
//folder.GetBaseFoldersRecusive();
//folder.GetBaseApplicationsRecusive();
dataContext.ObjectContext.Detach(folder);
CacheManager.Add(CachedData.Folder, cacheKey, folder, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return folder;
}
/// <summary>
/// Gets the folder.
/// </summary>
/// <param name="application">The application.</param>
/// <param name="folderName">Name of the folder.</param>
/// <returns></returns>
public static Cms_Folder GetFolder(string application, string folderName)
{
string cacheKey = string.Format("__GetFolder__Application:{0}_folderName:{1}", application, folderName);
Cms_Folder folder = CacheManager.Get(CachedData.Folder, cacheKey) as Cms_Folder;
if (folder == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.CreateDataContext(false);
folder = dataContext.QueryFolder(application, folderName, FolderType.TextContent, FolderType.BinaryContent).FirstOrDefault();
if (folder == null)
{
//
var baseApp = CachedData.GetApplication(application).BaseApplication;
if (!StringExtensions.IsNullOrEmptyTrim(baseApp))
{
return GetFolder(baseApp, folderName);
}
else
{
return null;
}
}
//folder.GetBaseFoldersRecusive();
//folder.GetBaseApplicationsRecusive();
dataContext.ObjectContext.Detach(folder);
CacheManager.Add(CachedData.Folder, cacheKey, folder, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return folder;
}
/// <summary>
/// Gets the localized folder.
/// </summary>
/// <param name="application">The application.</param>
/// <param name="folderUUID">The folder UUID.</param>
/// <returns></returns>
public static Cms_Folder GetLocalizedFolder(string application, Guid folderUUID)
{
string cacheKey = string.Format("__GetLocalizedFolder__Application:{0}_folderUUID:{1}", application, folderUUID);
Cms_Folder folder = CacheManager.Get(CachedData.Folder, cacheKey) as Cms_Folder;
if (folder == null)
{
var originalUUID = CachedData.GetFolder(folderUUID).OriginalUUID;
IEverestCmsDataContext dataContext = EverestCmsEntities.CreateDataContext(false);
folder = dataContext.QueryLocalizedFolder(application, originalUUID).First();
//folder.GetBaseFoldersRecusive();
//folder.GetBaseApplicationsRecusive();
dataContext.ObjectContext.Detach(folder);
CacheManager.Add(CachedData.Folder, cacheKey, folder, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return folder;
}
/// <summary>
/// Gets the folders.
/// </summary>
/// <param name="application">The application.</param>
/// <param name="FormSchemaName">Name of the schema.</param>
/// <returns></returns>
public static IEnumerable<Cms_Folder> GetFolders(string application, string FormSchemaName)
{
string cacheKey = string.Format("__GetFolders__Application:{0}_FormSchemaName:{1}", application, FormSchemaName);
IEnumerable<Cms_Folder> folders = CacheManager.Get(CachedData.Folder, cacheKey) as IEnumerable<Cms_Folder>;
if (folders == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.CreateDataContext(false);
folders = dataContext.QueryFoldersBySchema(application, FormSchemaName).ToArray();
foreach (var folder in folders)
{
dataContext.ObjectContext.Detach(folder);
}
CacheManager.Add(CachedData.Folder, cacheKey, folders, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return folders;
}
/// <summary>
/// Gets all derives.
/// </summary>
/// <param name="folderUUID">The folder UUID.</param>
/// <returns></returns>
public static IEnumerable<Cms_Folder> GetAllDerives(Guid folderUUID)
{
string cacheKey = string.Format("__GetAllDerives__FolderUUID:{0}", folderUUID);
IEnumerable<Cms_Folder> folders = CacheManager.Get(CachedData.Folder, cacheKey) as IEnumerable<Cms_Folder>;
if (folders == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.CreateDataContext(false);
List<Cms_Folder> derives = new List<Cms_Folder>();
var folder = dataContext.QueryFolder(folderUUID).First();
folder.Derives.Load();
foreach (var f in folder.Derives.ToArray())
{
dataContext.ObjectContext.Detach(f);
derives.Add(f);
derives.AddRange(GetAllDerives(f.UUID));
}
folders = derives;
CacheManager.Add(CachedData.Folder, cacheKey, folders, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return folders;
}
/// <summary>
/// Gets all base.
/// </summary>
/// <param name="folderUUID">The folder UUID.</param>
/// <returns></returns>
public static IEnumerable<Cms_Folder> GetAllBase(Guid folderUUID)
{
string cacheKey = string.Format("__GetAllBase__FolderUUID:{0}", folderUUID);
IEnumerable<Cms_Folder> folders = CacheManager.Get(CachedData.Folder, cacheKey) as IEnumerable<Cms_Folder>;
if (folders == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
var folder = dataContext.QueryFolder(folderUUID).First();
List<Cms_Folder> baseFolders = new List<Cms_Folder>();
Cms_Folder baseFolder = folder;
while (baseFolder != null && !baseFolders.Contains(baseFolder))
{
baseFolders.Add(baseFolder);
baseFolder.BaseReference.Load();
baseFolder = baseFolder.Base;
}
folders = baseFolders;
CacheManager.Add(CachedData.Folder, cacheKey, folders, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return folders;
}
#endregion
#region Content Template
public static Cms_ContentTemplate GetContentTemplate(Guid contentTemplateUUID)
{
string cacheKey = string.Format("__Common_GetContentTemplateContentTemplateUUID:{0}", contentTemplateUUID);
Cms_ContentTemplate contentTemplate = CacheManager.Get(CachedData.Template, cacheKey) as Cms_ContentTemplate;
if (contentTemplate == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
contentTemplate = dataContext.QueryContentTemplate(contentTemplateUUID).FirstOrDefault();
if (contentTemplate != null)
{
//detach
dataContext.ObjectContext.Detach(contentTemplate);
}
CacheManager.Add(CachedData.Template, cacheKey, contentTemplate, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return contentTemplate;
}
/// <summary>
/// Gets the cached content template.
/// </summary>
/// <param name="applicationName">Name of the application.</param>
/// <param name="originalId">The original id.</param>
/// <returns></returns>
public static Cms_ContentTemplate GetContentTemplate(string applicationName, Guid originalId)
{
string cacheKey = string.Format("__Common_GetContentTemplate_Application:{0}_ContentTemplateOriginalId:{1}", applicationName, originalId);
Cms_ContentTemplate contentTemplate = CacheManager.Get(CachedData.Template, cacheKey) as Cms_ContentTemplate;
if (contentTemplate == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
contentTemplate = dataContext.QueryContentTemplate(applicationName, originalId).OrderByDescending(ct => ct.ContentTemplateId).FirstOrDefault();
if (contentTemplate != null)
{
//detach
dataContext.ObjectContext.Detach(contentTemplate);
}
CacheManager.Add(CachedData.Template, cacheKey, contentTemplate, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return contentTemplate;
}
/// <summary>
/// Gets the cached content template.
/// </summary>
/// <param name="applicationName">Name of the application.</param>
/// <param name="contentTemplateName">Name of the content template.</param>
/// <returns></returns>
public static Cms_ContentTemplate GetContentTemplate(string applicationName, string contentTemplateName)
{
string cacheKey = string.Format("__Common_GetContentTemplate_Application:{0}_ContentTemplateName:{1}", applicationName, contentTemplateName);
Cms_ContentTemplate contentTemplate = CacheManager.Get(CachedData.Template, cacheKey) as Cms_ContentTemplate;
if (contentTemplate == null && CacheManager.Get(CachedData.Template, cacheKey) == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
contentTemplate = dataContext.QueryContentTemplate(applicationName, contentTemplateName).OrderByDescending(ct => ct.ContentTemplateId).FirstOrDefault();
//detach
if (contentTemplate != null)
{
dataContext.ObjectContext.Detach(contentTemplate);
CacheManager.Add(CachedData.Template, cacheKey, contentTemplate, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
else
{
CacheManager.Add(CachedData.Template, cacheKey, cacheKey, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
}
return contentTemplate;
}
/// <summary>
/// Gets the cached content template in page.
/// </summary>
/// <param name="pageId">The page id.</param>
/// <param name="positionId">The position id.</param>
/// <returns></returns>
public static IEnumerable<QueryedComponent> GetContentTemplateInPage(int pageId, string positionId)
{
string cacheKey = string.Format("__Common_ContentTemplateInPage_PageId:{0}_PositionId:{1}", pageId, positionId);
IEnumerable<QueryedComponent> contentTemplatesInPageHolder = CacheManager.Get(CachedData.Page, cacheKey) as IEnumerable<QueryedComponent>;
if (contentTemplatesInPageHolder == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
contentTemplatesInPageHolder = dataContext.QueryContentTemplateInPageHolders(pageId, positionId).OrderBy(p => p.Order)
.Select(p => new QueryedComponent()
{
ComponentPositionId = p.ContentTemplateInHolderId,
ContentTemplateUUID = p.Cms_ContentTemplate == null ? default(Guid) : p.Cms_ContentTemplate.UUID,
ParameterValues = p.Cms_ParameterValues.Select(pv => new QueriedParameterValues()
{
Name = pv.Name,
Value = pv.Value,
DataType = pv.DataType
}),
ComponentValue = p.ComponentValue,
IntComponentType = p.ComponentType,
ControlId = p.ControlId
}).ToArray();
CacheManager.Add(CachedData.Page, cacheKey, contentTemplatesInPageHolder, CacheItemPriority.Normal, null, CachedData.DefaultExpirations);
}
return contentTemplatesInPageHolder;
}
/// <summary>
/// Gets the cached data rule.
/// </summary>
/// <param name="pageId">The page id.</param>
/// <returns></returns>
public static IEnumerable<Cms_DataRule> GetDataRuleByPage(int pageId)
{
string cacheKey = string.Format("__Common__DataRule_PageId:{0}", pageId);
IEnumerable<Cms_DataRule> dataRules = CacheManager.Get(CachedData.DataRule, cacheKey) as IEnumerable<Cms_DataRule>;
if (dataRules == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
dataRules = dataContext.QueryDataRulesByPage(pageId).ToArray();
//detach
foreach (var dataRule in dataRules)
{
dataContext.ObjectContext.Detach(dataRule);
}
CacheManager.Add(CachedData.DataRule, cacheKey, dataRules, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return dataRules;
}
/// <summary>
/// Gets the cached data rule by content template.
/// </summary>
/// <param name="contentTemplateUUID">The content template UUID.</param>
/// <returns></returns>
public static IEnumerable<Cms_DataRule> GetDataRulesByContentTemplate(Guid contentTemplateUUID)
{
string cacheKey = string.Format("__Common__DataRule_ContentTemplateUUID:{0}", contentTemplateUUID);
IEnumerable<Cms_DataRule> dataRules = CacheManager.Get(CachedData.DataRule, cacheKey) as IEnumerable<Cms_DataRule>;
if (dataRules == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
dataRules = dataContext.QueryDataRulesByContentTemplate(contentTemplateUUID).ToArray();
//detach
foreach (var dataRule in dataRules)
{
dataContext.ObjectContext.Detach(dataRule);
}
CacheManager.Add(CachedData.DataRule, cacheKey, dataRules, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return dataRules;
}
#endregion
#region GetPages
/// <summary>
/// Gets the cached navigations.
/// </summary>
/// <param name="application">The application.</param>
/// <param name="parentUUID">The parent UUID.</param>
/// <returns></returns>
public static IEnumerable<Cms_Page> GetSubPages(string application, Guid? parentUUID)
{
string cacheKey = string.Format("__Common__GetSubPages_Application:{0}_ParentUUID:{1}", application, parentUUID == null ? "" : parentUUID.Value.ToString());
IEnumerable<Cms_Page> pages = CacheManager.Get(CachedData.Page, cacheKey) as IEnumerable<Cms_Page>;
if (pages == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
pages = dataContext.QueryPagesByParent(application, parentUUID)
.Where(p => p.ShowInNavigation == true)
.OrderBy(p => p.PageId)
.OrderBy(p => p.NavigationOrder).ToArray();
//detach
foreach (var page in pages)
{
dataContext.ObjectContext.Detach(page);
}
CacheManager.Add(CachedData.Page, cacheKey, pages, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return pages;
}
/// <summary>
/// Gets the parent page.
/// </summary>
/// <param name="pageUUID">The page UUID.</param>
/// <returns></returns>
public static Cms_Page GetParentPage(Guid pageUUID)
{
string cacheKey = string.Format("__Common__GetParentPage_PageUUID:{0}", pageUUID);
Cms_Page page = CacheManager.Get(CachedData.Page, cacheKey) as Cms_Page;
if (page == null && CacheManager.Get(CachedData.Page, cacheKey) == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
page = dataContext.QueryPage(pageUUID).Select(p => p.Cms_ParentPage).FirstOrDefault();
//detach
if (page != null)
{
dataContext.ObjectContext.Detach(page);
CacheManager.Add(CachedData.Page, cacheKey, page, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
else
{
CacheManager.Add(CachedData.Page, cacheKey, cacheKey, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
}
return page;
}
#endregion
#region GetWebForm
public static Cms_CustomerForm GetWebForm(Guid formUUID)
{
string cacheKey = string.Format("__Common__GetWebForm_formUUID:{0}", formUUID);
var webForm = CacheManager.Get(CachedData.WebForm, cacheKey) as Cms_CustomerForm;
//cacheKey
if (webForm == null && CacheManager.Get(CachedData.Page, cacheKey) == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
webForm = dataContext.QueryWebForm(formUUID).FirstOrDefault();
//detach
if (webForm != null)
{
dataContext.ObjectContext.Detach(webForm);
CacheManager.Add(CachedData.WebForm, cacheKey, webForm, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
else
{
CacheManager.Add(CachedData.WebForm, cacheKey, cacheKey, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
}
return webForm;
}
#endregion
#region SearchSettings
public static IEnumerable<SearchSetting> GetSearchSettings(string application)
{
string cacheKey = string.Format("___GetSearchSettings_Application:{0}", application);
var searchSettings = CacheManager.Get(CachedData.SearchSetting, cacheKey) as SearchSetting[];
if (searchSettings == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
searchSettings = dataContext.QuerySearchSettings(application)
.Select(s => new SearchSetting()
{
Application = s.aspnet_Applications.ApplicationName,
OriginalFolderUUID = s.Cms_Folder.OriginalUUID,
Url = s.Url
})
.ToArray();
CacheManager.Add(CachedData.SearchSetting, cacheKey, searchSettings, CacheItemPriority.Normal,
null, CachedData.DefaultExpirations);
}
return searchSettings;
}
#endregion
}
}
|