/*
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 System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text.RegularExpressions;
using Everest.Library.ExtensionMethod;
using Everest.CmsServices.Services;
using Everest.Library.Versioning;
using Everest.Library;
using Everest.Library.Data;
using Everest.Library.Data.Entity;
using Everest.Library.Data.Rule;
using Everest.Library.Web;
namespace Everest.CmsServices.Models{
partial class Cms_ContentTemplate : IVersionable, IXmlSerializable, IRuleEntity
{
#region Body
/// <summary>
/// Gets or sets the body.
/// </summary>
/// <value>The body.</value>
public string Body
{
get;
set;
}
/// <summary>
/// Gets the name of the file.
/// </summary>
/// <value>The name of the file.</value>
public string FileName
{
get
{
var application = this.aspnet_Applications;
if (application == null)
application = CachedData.GetApplicationByContentTemplate(this.UUID);
return TemplateFileManager.GetAbsoluteContentTemplateFilePath(application.ApplicationName, this.Name);
}
}
/// <summary>
/// Saves the body to disk file.
/// </summary>
public void SaveBody()
{
if (!Body.Trim().StartsWith("<%@"))
{
Body = @"<%@ Control Language=""C#"" Inherits=""Everest.CmsServices.MvcHelper.CmsUserControl"" %>
" + Body;
}
FileExtensions.SaveFileBody(FileName, this.Body);
//parse textresource
if (this.aspnet_Applications == null)
{
this.aspnet_ApplicationsReference.Load();
}
UnityManager.Resolve<TextResourceService>().ParseText(this.aspnet_Applications, this.Body);
this.ParseParameters(this.Body);
}
/// <summary>
/// Gets the body from disk file.
/// </summary>
public string GetBody()
{
if (System.IO.File.Exists(this.FileName))
{
this.Body = FileExtensions.GetFileBody(this.FileName);
}
else
{
this.Body = string.Empty;
}
return this.Body;
}
public void DeleteBody()
{
DeleteBody(FileName);
}
/// <summary>
/// Deletes the body.
/// </summary>
public void DeleteBody(string fileName)
{
string absoluteFile = CmsGlobal.ToAbsolutePath(fileName);
if (File.Exists(absoluteFile))
{
File.Delete(absoluteFile);
}
}
#endregion
static Regex parametersRegex = new Regex(@"public\s+(?<Type>\w+)\s+(?<PropertyName>\w+)\s*\{[^}]+set[^}]+\}", RegexOptions.Compiled | RegexOptions.Singleline);
/// <summary>
/// Parses the parameters.
/// </summary>
/// <param name="body">The body.</param>
private void ParseParameters(string body)
{
MatchCollection matches = parametersRegex.Matches(body);
List<Cms_ContentTemplateParameters> contentTemplateParameters = new List<Cms_ContentTemplateParameters>();
foreach (Match match in matches)
{
Cms_ContentTemplateParameters p = new Cms_ContentTemplateParameters();
p.Name = match.Groups["PropertyName"].Value;
p.DataType = match.Groups["Type"].Value;
contentTemplateParameters.Add(p);
}
if (matches.Count > 0)
{
this.AddParameters(contentTemplateParameters);
}
}
/// <summary>
/// Adds the content template's parameters.
/// </summary>
/// <param name="parameters">The parameters.</param>
private void AddParameters(IEnumerable<Cms_ContentTemplateParameters> parameters)
{
foreach (var item in parameters)
{
this.Cms_ContentTemplateParameters.Add(item);
}
}
/// <summary>
/// Gets the content template virtual path.
/// </summary>
/// <returns></returns>
public string GetVirtualPath()
{
return UrlConvertor.AbsolutePathToRelativeUrl(this.FileName);
}
#region partial methods
partial void OnNameChanged()
{
int lastDotIndex = this.Name.LastIndexOf('.');
if (lastDotIndex > 0)
{
Namespace = Name.Substring(0, lastDotIndex);
}
else
{
Namespace = string.Empty;
}
}
#endregion
#region Clear Children
/// <summary>
/// Clears the parameters.
/// </summary>
/// <param name="dataContext">The data context.</param>
/// <returns></returns>
internal void ClearParameters(IEverestCmsDataContext dataContext)
{
this.Cms_ContentTemplateParameters.Load();
Cms_ContentTemplateParameters parameter = null;
do
{
parameter = this.Cms_ContentTemplateParameters.FirstOrDefault();
if (parameter != null)
{
dataContext.DeleteObject(parameter);
}
} while (parameter != null);
this.Cms_ContentTemplateParameters.Clear();
}
/// <summary>
/// Clears the plugins.
/// </summary>
/// <param name="dataContext">The data context.</param>
internal void ClearPlugins(IEverestCmsDataContext dataContext)
{
this.Cms_Plugin.Load();
Cms_Plugin plugin = null;
do
{
plugin = this.Cms_Plugin.FirstOrDefault();
if (plugin != null)
{
dataContext.DeleteObject(plugin);
}
} while (plugin != null);
}
/// <summary>
/// Clears the data rule.
/// </summary>
/// <param name="dataContext">The data context.</param>
internal void ClearDataRule(IEverestCmsDataContext dataContext)
{
if (this.EntityState == System.Data.EntityState.Added)
{
return;
}
if (!this.Cms_DataRule.IsLoaded)
{
this.Cms_DataRule.Load();
}
Cms_DataRule dataRule = null;
do
{
dataRule = Cms_DataRule.FirstOrDefault();
if (dataRule != null)
{
dataContext.DeleteObject(dataRule);
}
} while (dataRule != null);
}
#endregion
#region IVersionable Members
/// <summary>
/// Gets the name of the snapshot form.
/// </summary>
/// <value>The name of the snapshot form.</value>
public string FormSchemaName
{
get { return "ContentTemplate"; }
}
public void OnRevert(IVersionItem workingItem, IVersionItem toBeRevertedItem)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
var currentContentTemplate = dataContext.QueryContentTemplate(this.UUID).First();
currentContentTemplate.Name = this.Name;
currentContentTemplate.Body = this.Body;
currentContentTemplate.ModifiedDate = DateTime.Now;
currentContentTemplate.UserName = workingItem.CheckinUser;
currentContentTemplate.Description = this.Description;
currentContentTemplate.Namespace = this.Namespace;
//clear parmeters
currentContentTemplate.ClearParameters(dataContext);
foreach (var parameter in this.Cms_ContentTemplateParameters)
{
Cms_ContentTemplateParameters newParameter = new Cms_ContentTemplateParameters();
newParameter.Name = parameter.Name;
newParameter.DataType = parameter.DataType;
currentContentTemplate.Cms_ContentTemplateParameters.Add(newParameter);
}
//the Data Rule is deserilized from the history
//can not use the DataRule.Copy
currentContentTemplate.ClearDataRule(dataContext);
foreach (var dataRule in this.Cms_DataRule)
{
Cms_DataRule newDataRule = new Cms_DataRule();
newDataRule.DataName = dataRule.DataName;
if (dataRule.Cms_Folder != null)
{
newDataRule.Cms_Folder = dataContext.QueryFolder(dataRule.Cms_Folder.UUID).First();
}
newDataRule.ValueRule = dataRule.ValueRule;
newDataRule.ValueType = dataRule.ValueType;
newDataRule.Order = dataRule.Order;
newDataRule.OrderBy = dataRule.OrderBy;
newDataRule.TopCount = dataRule.TopCount;
newDataRule.IncludeChildren = dataRule.IncludeChildren;
newDataRule.DynamicLinkingOrder = dataRule.DynamicLinkingOrder;
newDataRule.ReferencingContentId = dataRule.ReferencingContentId;
newDataRule.PageIndex = dataRule.PageIndex;
newDataRule.PageSize = dataRule.PageSize;
newDataRule.Caching = dataRule.Caching;
currentContentTemplate.Cms_DataRule.Add(newDataRule);
}
currentContentTemplate.ClearPlugins(dataContext);
foreach (var plugin in this.Cms_Plugin)
{
Cms_Plugin newPlugin = new Cms_Plugin();
newPlugin.PluginName = plugin.PluginName;
newPlugin.PluginType = plugin.PluginType;
currentContentTemplate.Cms_Plugin.Add(newPlugin);
}
dataContext.SaveChanges();
currentContentTemplate.SaveBody();
}
#endregion
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
XmlDocument xmlDom = new XmlDocument();
xmlDom.Load(reader);
XmlNode contentTemplateNode = xmlDom.FirstChild.FirstChild;
this.UUID = new Guid(contentTemplateNode.Attributes["UUID"].Value);
this.ContentTemplateId = int.Parse(contentTemplateNode.Attributes["Id"].Value);
this.Name = contentTemplateNode.Attributes["Name"].Value;
this.Body = contentTemplateNode.Attributes["Body"].Value;
this.ModifiedDate = DateTime.Parse(contentTemplateNode.Attributes["ModifiedDate"].Value);
if (contentTemplateNode.Attributes["Description"] != null)
{
this.Description = contentTemplateNode.Attributes["Description"].Value;
}
if (contentTemplateNode.Attributes["ApplicationLevel"] != null)
{
this.ApplicationLevel = int.Parse(contentTemplateNode.Attributes["ApplicationLevel"].Value);
}
if (contentTemplateNode.Attributes["Namespace"] != null)
{
this.Namespace = contentTemplateNode.Attributes["Namespace"].Value;
}
this.aspnet_Applications = aspnet_Applications.DeserializeFromNode(contentTemplateNode);
//deserialize the parameters node
XmlNode parametersNode = contentTemplateNode.SelectSingleNode("parameters");
foreach (XmlNode parameterNode in parametersNode.ChildNodes)
{
Cms_ContentTemplateParameters contentTemplateParameter = new Cms_ContentTemplateParameters();
this.Cms_ContentTemplateParameters.Add(contentTemplateParameter);
contentTemplateParameter.ParameterId = int.Parse(parameterNode.Attributes["ParameterId"].Value);
contentTemplateParameter.Name = parameterNode.Attributes["Name"].Value;
contentTemplateParameter.DataType = parameterNode.Attributes["DataType"].Value;
}
//deserialize the plugins node
XmlNode pluginsNode = contentTemplateNode.SelectSingleNode("plugins");
foreach (XmlNode pluginNode in pluginsNode.ChildNodes)
{
Cms_Plugin plugin = new Cms_Plugin();
plugin.PluginId = int.Parse(pluginNode.Attributes["PluginId"].Value);
plugin.PluginName = pluginNode.Attributes["PluginName"].Value;
plugin.PluginType = pluginNode.Attributes["PluginType"].Value;
this.Cms_Plugin.Add(plugin);
}
XmlNode dataRulesNode = contentTemplateNode.SelectSingleNode("dataRules");
if (dataRulesNode != null)
{
foreach (XmlNode dataRuleNode in dataRulesNode.ChildNodes)
{
Cms_DataRule dataRule = new Cms_DataRule();
this.Cms_DataRule.Add(dataRule);
dataRule.DataName = dataRuleNode.Attributes["DataName"].Value;
int valueType;
if (int.TryParse(dataRuleNode.Attributes["ValueType"].Value, out valueType))
{
dataRule.ValueType = valueType;
}
if (!StringExtensions.IsNullOrEmptyTrim(dataRuleNode.Attributes["FolderUUID"].Value))
{
dataRule.Cms_Folder = CachedData.GetFolder(new Guid(dataRuleNode.Attributes["FolderUUID"].Value));
}
dataRule.ValueRule = dataRuleNode.Attributes["ValueRule"].Value;
dataRule.OrderBy = dataRuleNode.Attributes["OrderBy"].Value;
dataRule.TopCount = dataRuleNode.Attributes["TopCount"].Value;
dataRule.IncludeChildren = dataRuleNode.Attributes["IncludeChildren"].Value.GetNullableBool();
dataRule.DynamicLinkingOrder = int.Parse(dataRuleNode.Attributes["DynamicLinkingOrder"].Value);
dataRule.Order = int.Parse(dataRuleNode.Attributes["Order"].Value);
if (dataRuleNode.Attributes["ReferencingContentId"] != null)
{
dataRule.ReferencingContentId = dataRuleNode.Attributes["ReferencingContentId"].Value;
}
if (dataRuleNode.Attributes["PageIndex"] != null)
{
dataRule.PageIndex = dataRuleNode.Attributes["PageIndex"].Value;
}
if (dataRuleNode.Attributes["PageSize"] != null)
{
dataRule.PageSize = dataRuleNode.Attributes["PageSize"].Value;
}
if (dataRuleNode.Attributes["Caching"] != null)
{
dataRule.Caching = dataRuleNode.Attributes["Caching"].Value;
}
}
}
}
public void WriteXml(System.Xml.XmlWriter writer)
{
XmlDocument xmlDom = new XmlDocument();
XmlNode contentTemplateNode = xmlDom.CreateElement("contentTemplate");
xmlDom.AppendChild(contentTemplateNode);
//add uuid
XmlAttribute uuidAtt = xmlDom.CreateAttribute("UUID");
uuidAtt.Value = this.UUID.ToString();
contentTemplateNode.Attributes.Append(uuidAtt);
//add content template id
XmlAttribute contentTemplateIdAtt = xmlDom.CreateAttribute("Id");
contentTemplateIdAtt.Value = this.ContentTemplateId.ToString();
contentTemplateNode.Attributes.Append(contentTemplateIdAtt);
//add content template name
XmlAttribute contentTemplateNameAtt = xmlDom.CreateAttribute("Name");
contentTemplateNameAtt.Value = this.Name;
contentTemplateNode.Attributes.Append(contentTemplateNameAtt);
//add content template body
XmlAttribute bodyAtt = xmlDom.CreateAttribute("Body");
bodyAtt.Value = this.Body;
contentTemplateNode.Attributes.Append(bodyAtt);
//add ModifiedDate
XmlAttribute modifiedDate = xmlDom.CreateAttribute("ModifiedDate");
modifiedDate.Value = this.ModifiedDate.ToString();
contentTemplateNode.Attributes.Append(modifiedDate);
XmlAttribute descriptionAtt = xmlDom.CreateAttribute("Description");
descriptionAtt.Value = this.Description;
contentTemplateNode.Attributes.Append(descriptionAtt);
//Application Level
XmlAttribute applicationLevelAtt = xmlDom.CreateAttribute("ApplicationLevel");
applicationLevelAtt.Value = this.ApplicationLevel.ToString();
contentTemplateNode.Attributes.Append(applicationLevelAtt);
//Namespace
XmlAttribute namespaceAtt = xmlDom.CreateAttribute("Namespace");
namespaceAtt.Value = this.Namespace;
contentTemplateNode.Attributes.Append(namespaceAtt);
this.aspnet_ApplicationsReference.Load(this.aspnet_Applications, this.EntityState);
this.aspnet_Applications.SerializeAsNode(contentTemplateNode);
//add the parameters
XmlNode parametersNode = xmlDom.CreateElement("parameters");
contentTemplateNode.AppendChild(parametersNode);
foreach (var p in this.Cms_ContentTemplateParameters)
{
XmlNode parameterNode = xmlDom.CreateElement("parameter");
parametersNode.AppendChild(parameterNode);
XmlAttribute parameterIdAtt = xmlDom.CreateAttribute("ParameterId");
parameterIdAtt.Value = p.ParameterId.ToString();
parameterNode.Attributes.Append(parameterIdAtt);
XmlAttribute parameterNameAtt = xmlDom.CreateAttribute("Name");
parameterNameAtt.Value = p.Name;
parameterNode.Attributes.Append(parameterNameAtt);
XmlAttribute parameterDataTypeAtt = xmlDom.CreateAttribute("DataType");
parameterDataTypeAtt.Value = p.DataType;
parameterNode.Attributes.Append(parameterDataTypeAtt);
}
//add the plugin
XmlNode pluginsNode = xmlDom.CreateElement("plugins");
contentTemplateNode.AppendChild(pluginsNode);
foreach (var p in this.Cms_Plugin)
{
XmlNode pluginNode = xmlDom.CreateElement("plugin");
pluginsNode.AppendChild(pluginNode);
XmlAttribute pluginIdAtt = xmlDom.CreateAttribute("PluginId");
pluginIdAtt.Value = p.PluginId.ToString();
pluginNode.Attributes.Append(pluginIdAtt);
XmlAttribute pluginNameAtt = xmlDom.CreateAttribute("PluginName");
pluginNameAtt.Value = p.PluginName;
pluginNode.Attributes.Append(pluginNameAtt);
XmlAttribute pluginTypeAtt = xmlDom.CreateAttribute("PluginType");
pluginTypeAtt.Value = p.PluginType;
pluginNode.Attributes.Append(pluginTypeAtt);
}
#region add data rules node
//add data rule
XmlNode dataRulesNode = xmlDom.CreateElement("dataRules");
contentTemplateNode.AppendChild(dataRulesNode);
if (!this.Cms_DataRule.IsLoaded)
{
this.Cms_DataRule.Load();
}
foreach (var dataRule in this.Cms_DataRule)
{
XmlNode dataRuleNode = xmlDom.CreateElement("dataRule");
dataRulesNode.AppendChild(dataRuleNode);
//add dataName
XmlAttribute dataNameAtt = xmlDom.CreateAttribute("DataName");
dataNameAtt.Value = dataRule.DataName;
dataRuleNode.Attributes.Append(dataNameAtt);
//add valueType
XmlAttribute valueTypeAtt = xmlDom.CreateAttribute("ValueType");
valueTypeAtt.Value = dataRule.ValueType.ToString();
dataRuleNode.Attributes.Append(valueTypeAtt);
//add folder
XmlAttribute folderIdAtt = xmlDom.CreateAttribute("FolderUUID");
if (dataRule.FolderUUIDFromReference != null)
{
folderIdAtt.Value = dataRule.FolderUUIDFromReference.ToString();
}
dataRuleNode.Attributes.Append(folderIdAtt);
//add value rule
XmlAttribute valueRuleAtt = xmlDom.CreateAttribute("ValueRule");
valueRuleAtt.Value = dataRule.ValueRule;
dataRuleNode.Attributes.Append(valueRuleAtt);
//add order by
XmlAttribute orderByAtt = xmlDom.CreateAttribute("OrderBy");
orderByAtt.Value = dataRule.OrderBy;
dataRuleNode.Attributes.Append(orderByAtt);
//add order
XmlAttribute orderAtt = xmlDom.CreateAttribute("Order");
orderAtt.Value = dataRule.Order.ToString();
dataRuleNode.Attributes.Append(orderAtt);
XmlAttribute topCountAtt = xmlDom.CreateAttribute("TopCount");
topCountAtt.Value = dataRule.TopCount;
dataRuleNode.Attributes.Append(topCountAtt);
XmlAttribute includeChildrenAtt = xmlDom.CreateAttribute("IncludeChildren");
includeChildrenAtt.Value = dataRule.IncludeChildren.ToString();
dataRuleNode.Attributes.Append(includeChildrenAtt);
XmlAttribute dynamicLinkingOrderAtt = xmlDom.CreateAttribute("DynamicLinkingOrder");
dynamicLinkingOrderAtt.Value = dataRule.DynamicLinkingOrder.ToString();
dataRuleNode.Attributes.Append(dynamicLinkingOrderAtt);
XmlAttribute referencingContentIdAtt = xmlDom.CreateAttribute("ReferencingContentId");
referencingContentIdAtt.Value = dataRule.ReferencingContentId;
dataRuleNode.Attributes.Append(referencingContentIdAtt);
XmlAttribute pageIndexAtt = xmlDom.CreateAttribute("PageIndex");
pageIndexAtt.Value = dataRule.PageIndex;
dataRuleNode.Attributes.Append(pageIndexAtt);
XmlAttribute pageSizeAtt = xmlDom.CreateAttribute("PageSize");
pageSizeAtt.Value = dataRule.PageSize;
dataRuleNode.Attributes.Append(pageSizeAtt);
XmlAttribute cachingAtt = xmlDom.CreateAttribute("Caching");
cachingAtt.Value = dataRule.Caching;
dataRuleNode.Attributes.Append(cachingAtt);
}
#endregion
xmlDom.WriteTo(writer);
}
#endregion
#region copy new
/// <summary>
/// Copies the specified content template.
/// </summary>
/// <returns></returns>
public Cms_ContentTemplate CopyNew()
{
Cms_ContentTemplate newContentTemplate = new Cms_ContentTemplate();
this.GetBody();
newContentTemplate.Name = this.Name;
newContentTemplate.Body = this.Body;
newContentTemplate.aspnet_Applications = this.aspnet_Applications;
newContentTemplate.ModifiedDate = DateTime.Now;
newContentTemplate.UUID = Guid.NewGuid();
newContentTemplate.Description = this.Description;
newContentTemplate.Namespace = this.Namespace;
if (!this.Cms_ContentTemplateParameters.IsLoaded)
{
this.Cms_ContentTemplateParameters.Load();
}
foreach (var parameter in this.Cms_ContentTemplateParameters)
{
Cms_ContentTemplateParameters newParameter = new Cms_ContentTemplateParameters();
newParameter.Name = parameter.Name;
newParameter.DataType = parameter.DataType;
newContentTemplate.Cms_ContentTemplateParameters.Add(newParameter);
}
//copy the data rule
if (this.Cms_DataRule.IsLoaded == false)
{
this.Cms_DataRule.Load();
}
foreach (var dataRule in this.Cms_DataRule)
{
Cms_DataRule newDataRule = dataRule.Copy();
newContentTemplate.Cms_DataRule.Add(newDataRule);
}
if (!this.Cms_Plugin.IsLoaded)
{
this.Cms_Plugin.Load();
}
foreach (var plugin in this.Cms_Plugin)
{
Cms_Plugin newPlugin = new Cms_Plugin();
newPlugin.PluginName = plugin.PluginName;
newPlugin.PluginType = plugin.PluginType;
newContentTemplate.Cms_Plugin.Add(newPlugin);
}
return newContentTemplate;
}
#endregion
#region IRuleEntity Members
public IEnumerable<RuleViolation> GetRuleViolations()
{
var dataContext = EverestCmsEntities.GetDataContext();
List<RuleViolation> list = new List<RuleViolation>();
this.aspnet_ApplicationsReference.Load(this.aspnet_Applications, this.EntityState);
if (dataContext.IsContentTemplateExists(this.Name, this.aspnet_Applications.ApplicationName, this.ContentTemplateId).Exists())
{
if (this.Base == null)
{
list.Add(new RuleViolation("Name", this.Name, Resources.ContentTempalteAlreadyExists));
}
}
return list;
}
#endregion
}
}
|