Cms_Content.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 » Cms_Content.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.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;
using System.IO;
using System.Xml.Serialization;

using Everest.CmsServices.Search;
using Everest.Library;
using Everest.Library.Data;
using Everest.Library.Data.Rule;
using Everest.Library.Data.Entity;
using Everest.Library.ExtensionMethod;
using Everest.Library.Versioning;
using Everest.CmsServices.Services;
using Everest.CmsServices.Providers;
using Everest.Library.Web;

namespace Everest.CmsServices.Models{
    /// <summary>
    /// sync Workflowsequence ContentStatus
    /// </summary>
    public enum ContentStatus
    {
        /// <summary>
        /// 
        /// </summary>
        Draft = 0,
        /// <summary>
        /// 
        /// </summary>
        Published = 1
    }
    public partial class Cms_Content : IVersionable, IXmlSerializable, IRuleEntity
    {

        #region Constructor
        public Cms_Content()
        {
            this.UUID = Guid.NewGuid();
            this.OriginalUUID = UUID;
            this.ContentStatus = 0;

            this.Title = string.Empty;
        }
        #endregion

        #region GetContentPath
        /// <summary>
        /// Gets the content path.
        /// </summary>
        /// <returns></returns>
        public string GetContentPath()
        {
            var parent = this;
            string path = this.Title;
            do
            {
                parent.Cms_FolderReference.Load(parent.Cms_Folder, parent.EntityState);
                if (parent.Cms_Folder == null)
                {
                    this.ParentContentReference.Load(this.ParentContent, this.EntityState);
                    parent = this.ParentContent;
                    path = Path.Combine(parent.Title, path);
                }

            } while (parent.Cms_Folder == null);
            parent.Cms_Folder.aspnet_ApplicationsReference.Load(parent.Cms_Folder.aspnet_Applications, parent.Cms_Folder.EntityState);
            path = Path.Combine(Path.Combine(parent.Cms_Folder.aspnet_Applications.ApplicationName, parent.Cms_Folder.FolderName), path);
            return path;
        }

        #endregion

        #region Property
        /// <summary>
        /// Gets or sets the content field values.
        /// </summary>
        /// <value>The content field values.</value>
        public IDictionary<string, object> ContentFieldValues { get; set; }


        /// <summary>
        /// Gets the schema id from Cms_SchemaReference
        /// </summary>
        /// <value>The schema id.</value>
        public Guid SchemaUUIDFromReference
        {
            get
            {
                return this.Cms_SchemaReference.EntityKey.GetKeyValue<Guid>("UUID");
            }
        }
        #endregion

        #region Clear Children
        /// <summary>
        /// Clears the content files.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        internal void ClearContentFiles(IEverestCmsDataContext dataContext)
        {
            if (this.EntityState != System.Data.EntityState.Added && this.EntityState != System.Data.EntityState.Detached)
            {
                this.ContentFiles.Load();
                Cms_ContentFile contentFile = null;
                do
                {
                    contentFile = this.ContentFiles.FirstOrDefault();
                    if (contentFile != null)
                    {
                        dataContext.DeleteObject(contentFile);
                    }

                } while (contentFile != null);
            }
        }
        internal void ClearReferencing(IEverestCmsDataContext dataContext)
        {
            if (this.EntityState != System.Data.EntityState.Added && this.EntityState != System.Data.EntityState.Detached)
            {
                this.Cms_ContentReferencing.Load();
                Cms_ContentReferencing refContent = null;
                do
                {
                    refContent = this.Cms_ContentReferencing.FirstOrDefault();
                    if (refContent != null)
                    {
                        dataContext.DeleteObject(refContent);
                    }

                } while (refContent != null);
                this.Cms_ContentReferencing.Clear();
            }
        }
        #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
            {
                this.Cms_SchemaReference.Load(this.Cms_Schema, this.EntityState);
                if ((SchemaType)this.Cms_Schema.SchemaType == SchemaType.Binary)
                {
                    return "BinaryContent";
                }
                else
                {
                    return this.Cms_Schema.UUID.ToString();
                }
            }
        }

        /// <summary>
        /// Called when [revert].
        /// </summary>
        /// <param name="workingItem">The working item.</param>
        /// <param name="toBeRevertedItem">The source revision to be reverted item.</param>
        public void OnRevert(IVersionItem workingItem, IVersionItem toBeRevertedItem)
        {
            IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
            var content = dataContext.QueryContent(this.UUID).First();

            content.Title = this.Title;
            content.ModifiedDate = DateTime.Now;
            content.ContentStatus = this.ContentStatus;
            content.PostDate = this.PostDate;
            content.UserName = workingItem.CheckinUser;
            content.UserKey = this.UserKey;

            if (this.Cms_Schema == null || (SchemaType)this.Cms_Schema.SchemaType == SchemaType.Text)
            {
                #region Text Content
                if (this.ContentFiles.Count > 0)
                {
                    content.ClearContentFiles(dataContext);
                    //resotre the content files
                    foreach (var contentFile in this.ContentFiles)
                    {
                        Cms_ContentFile newContentFile = new Cms_ContentFile();
                        //,Cms_BinaryContent,
                        if (contentFile.BinaryContent != null)
                        {
                            newContentFile.BinaryContent = dataContext.QueryContent(contentFile.BinaryContent.UUID).FirstOrDefault();
                        }
                        newContentFile.FilePath = contentFile.FilePath;

                        content.ContentFiles.Add(newContentFile);
                    }
                }
                if (this.Cms_ContentReferencing.Count > 0)
                {
                    content.ClearReferencing(dataContext);
                    foreach (var referencing in this.Cms_ContentReferencing)
                    {
                        var c = dataContext.QueryContent(referencing.Cms_ReferencedContent.UUID).FirstOrDefault();
                        if (c != null)
                        {
                            Cms_ContentReferencing contentRef = new Cms_ContentReferencing();
                            contentRef.Cms_Content = content;
                            contentRef.Cms_ReferencedContent = c;
                            contentRef.ReferencedContentOriginalUUID = c.OriginalUUID;
                            content.Cms_ContentReferencing.Add(contentRef);
                        }
                    }
                }
                dataContext.SaveChanges();
                //update the dynamic fields.
                IContentProvider contentProvider = UnityManager.Resolve<IContentProvider>();
                contentProvider.TextContentManager.UpdateContent(dataContext, content, this.ContentFieldValues, true);
                #endregion
            }
            else
            {
                #region Binary Content
                content.Cms_BinaryContentReference.Load(content.Cms_BinaryContent, content.EntityState);
                content.Cms_BinaryContent.FilePath = this.Cms_BinaryContent.FilePath;
                content.Cms_BinaryContent.FileSize = this.Cms_BinaryContent.FileSize;
                dataContext.SaveChanges();
                #endregion
            }

        }

        #endregion

        #region Serialize
        #region IXmlSerializable Members

        /// <summary>
        /// This method is reserved and should not be used. When implementing the IXmlSerializable interface, you should return null (Nothing in Visual Basic) from this method, and instead, if specifying a custom schema is required, apply the <see cref="T:System.Xml.Serialization.XmlSchemaProviderAttribute"/> to the class.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Xml.Schema.XmlSchema"/> that describes the XML representation of the object that is produced by the <see cref="M:System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)"/> method and consumed by the <see cref="M:System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)"/> method.
        /// </returns>
        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        /// <summary>
        /// Generates an object from its XML representation.
        /// </summary>
        /// <param name="reader">The <see cref="T:System.Xml.XmlReader"/> stream from which the object is deserialized.</param>
        public void ReadXml(System.Xml.XmlReader reader)
        {
            //DataContext
            //DataContext.Save
            XmlDocument xmlDom = new XmlDocument();
            xmlDom.Load(reader);

            XmlNode contentNode = xmlDom.FirstChild.FirstChild;

            this.ContentId = int.Parse(contentNode.Attributes["ContentId"].Value);
            this.UUID = new Guid(contentNode.Attributes["UUID"].Value);

            this.Cms_Folder = Cms_Folder.DeserializeFromNode(contentNode);

            this.ParentContent = Cms_Content.DeserializeFromNode(contentNode, "parentContent");
            this.aspnet_Applications = aspnet_Applications.DeserializeFromNode(contentNode);

            this.UserName = contentNode.Attributes["UserName"].Value;
            this.Title = contentNode.Attributes["Title"].Value;
            if (!StringExtensions.IsNullOrEmptyTrim(contentNode.Attributes["ModifiedDate"].Value))
            {
                this.ModifiedDate = DateTime.Parse(contentNode.Attributes["ModifiedDate"].Value);
            }
            this.PostDate = DateTime.Parse(contentNode.Attributes["PostDate"].Value);

            if (contentNode.Attributes["ContentStatus"] != null)
            {
                this.ContentStatus = int.Parse(contentNode.Attributes["ContentStatus"].Value);
            }
            if (contentNode.Attributes["UserKey"] != null)
            {
                this.UserKey = contentNode.Attributes["UserKey"].Value;
            }
            if (contentNode.Attributes["ApplicationLevel"] != null)
            {
                this.ApplicationLevel = int.Parse(contentNode.Attributes["ApplicationLevel"].Value);
            }
            if (contentNode.Attributes["FolderLevel"] != null)
            {
                this.FolderLevel = int.Parse(contentNode.Attributes["FolderLevel"].Value);
            }

            this.Cms_Schema = Cms_Schema.DeserializeFromNode(contentNode);

            if (this.Cms_Schema == null || (SchemaType)this.Cms_Schema.SchemaType == SchemaType.Text)
            {
                #region TextContent
                XmlNode dynamicFields = contentNode.SelectSingleNode("dynamicFields");
                if (dynamicFields != null)
                {
                    this.ContentFieldValues = new Dictionary<string, object>();
                    foreach (XmlAttribute att in dynamicFields.Attributes)
                    {
                        ContentFieldValues.Add(att.Name, att.Value);
                    }
                }
                XmlNode contentFilesNode = contentNode.SelectSingleNode("contentFiles");
                if (contentFilesNode != null)
                {
                    foreach (XmlNode contentFileNode in contentFilesNode.ChildNodes)
                    {
                        Cms_ContentFile contentFile = new Cms_ContentFile();
                        this.ContentFiles.Add(contentFile);

                        //content fileCms_BinaryContentContentId
                        //
                        if (contentFileNode.Attributes["BinaryContentUUID"] != null &&
                            !StringExtensions.IsNullOrEmptyTrim(contentFileNode.Attributes["BinaryContentUUID"].Value))
                        {
                            contentFile.BinaryContent = new Cms_Content();
                            contentFile.BinaryContent.UUID = new Guid(contentFileNode.Attributes["BinaryContentUUID"].Value);
                        }

                        contentFile.FilePath = contentFileNode.Attributes["FilePath"].Value;
                    }
                }

                //XmlNode referencingsNode = contentNode.SelectSingleNode("referencings");
                //if (referencingsNode != null)
                //{
                //    foreach (XmlNode referencingContentNode in referencingsNode.ChildNodes)
                //    {
                //        Cms_ContentReferencing contentRef = new Cms_ContentReferencing();
                //        contentRef.Cms_Content = this;
                //        contentRef.Cms_ReferencedContent = new Cms_Content();
                //        contentRef.Cms_ReferencedContent.UUID = new Guid(referencingContentNode.Attributes["UUID"].Value);
                //        contentRef.ReferencedContentOriginalUUID = contentRef.Cms_ReferencedContent.OriginalUUID;
                //        this.Cms_ContentReferencing.Add(contentRef);
                //    }
                //}
                #endregion
            }
            else
            {
                #region BinaryContent

                XmlNode binaryContentNode = contentNode.SelectSingleNode("binaryContent");
                if (binaryContentNode != null)
                {
                    Cms_BinaryContent binaryContent = new Cms_BinaryContent();
                    binaryContent.FileSize = int.Parse(binaryContentNode.Attributes["FileSize"].Value);
                    binaryContent.FilePath = binaryContentNode.Attributes["FilePath"].Value;
                    this.Cms_BinaryContent = binaryContent;
                }
                #endregion
            }
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            XmlDocument xmlDom = new XmlDocument();
            XmlNode contentNode = xmlDom.CreateElement("content");
            xmlDom.AppendChild(contentNode);

            //add content id
            XmlAttribute contentIdAtt = xmlDom.CreateAttribute("ContentId");
            contentIdAtt.Value = this.ContentId.ToString();
            contentNode.Attributes.Append(contentIdAtt);

            //add uuid
            XmlAttribute uuidAtt = xmlDom.CreateAttribute("UUID");
            uuidAtt.Value = this.UUID.ToString();
            contentNode.Attributes.Append(uuidAtt);


            //add folder
            this.Cms_FolderReference.Load(this.Cms_Folder, this.EntityState);
            if (this.Cms_Folder != null)
            {
                this.Cms_Folder.SerializeAsNode(contentNode);
            }

            //add SchemaUUID
            this.Cms_SchemaReference.Load(this.Cms_Schema, this.EntityState);
            this.Cms_Schema.SerializeAsNode(contentNode);


            //add parentUUID
            this.ParentContentReference.Load(this.ParentContent, this.EntityState);
            if (this.ParentContent != null)
            {
                this.ParentContent.SerializeAsNode(contentNode, "parentContent");
            }

            //add application
            this.aspnet_ApplicationsReference.Load(this.aspnet_Applications, this.EntityState);
            this.aspnet_Applications.SerializeAsNode(contentNode);

            //add username
            XmlAttribute userNameAtt = xmlDom.CreateAttribute("UserName");
            userNameAtt.Value = this.UserName;
            contentNode.Attributes.Append(userNameAtt);

            //add subject
            XmlAttribute subjectAtt = xmlDom.CreateAttribute("Title");
            subjectAtt.Value = this.Title;
            contentNode.Attributes.Append(subjectAtt);

            //add modifiedDate
            XmlAttribute modifiedDateAtt = xmlDom.CreateAttribute("ModifiedDate");
            modifiedDateAtt.Value = this.ModifiedDate.ToString();
            contentNode.Attributes.Append(modifiedDateAtt);

            //add PostDate
            XmlAttribute postDateAtt = xmlDom.CreateAttribute("PostDate");
            postDateAtt.Value = this.PostDate.ToString();
            contentNode.Attributes.Append(postDateAtt);

            //add ContentStatus
            XmlAttribute contentStatusAtt = xmlDom.CreateAttribute("ContentStatus");
            contentStatusAtt.Value = this.ContentStatus.ToString();
            contentNode.Attributes.Append(contentStatusAtt);

            //add UserKey
            XmlAttribute userKeyAtt = xmlDom.CreateAttribute("UserKey");
            userKeyAtt.Value = this.UserKey;
            contentNode.Attributes.Append(userKeyAtt);

            //add ApplicationLevel
            XmlAttribute applicationLevelAtt = xmlDom.CreateAttribute("ApplicationLevel");
            applicationLevelAtt.Value = this.ApplicationLevel.ToString();
            contentNode.Attributes.Append(applicationLevelAtt);

            //add FolderLevel
            XmlAttribute folderLevelAtt = xmlDom.CreateAttribute("FolderLevel");
            folderLevelAtt.Value = this.FolderLevel.ToString();
            contentNode.Attributes.Append(folderLevelAtt);

            #region TextContent
            if ((SchemaType)this.Cms_Schema.SchemaType == SchemaType.Text)
            {
                XmlNode dynamicFields = xmlDom.CreateElement("dynamicFields");
                contentNode.AppendChild(dynamicFields);
                foreach (var item in this.ContentFieldValues)
                {
                    if (item.Value != null)
                    {
                        XmlAttribute fieldAtt = xmlDom.CreateAttribute(item.Key);
                        fieldAtt.Value = item.Value.ToString();
                        dynamicFields.Attributes.Append(fieldAtt);
                    }
                }

                XmlNode contentFilesNode = xmlDom.CreateElement("contentFiles");
                contentNode.AppendChild(contentFilesNode);
                this.ContentFiles.Load();
                foreach (var contentFile in this.ContentFiles)
                {
                    XmlNode contentFileNode = xmlDom.CreateElement("contentFile");
                    contentFilesNode.AppendChild(contentFileNode);

                    if (contentFile.BinaryContentReference.EntityKey != null)
                    {
                        XmlAttribute binaryContentIdAtt = xmlDom.CreateAttribute("BinaryContentUUID");
                        //the BinaryContentId Store in the reference ,get the value in there will save once query.
                        binaryContentIdAtt.Value = contentFile.BinaryContentReference.EntityKey
                            .GetKeyValue<Guid>("UUID").ToString();
                        contentFileNode.Attributes.Append(binaryContentIdAtt);
                    }

                    XmlAttribute filePathAtt = xmlDom.CreateAttribute("FilePath");
                    filePathAtt.Value = contentFile.FilePath;
                    contentFileNode.Attributes.Append(filePathAtt);
                }

                //XmlNode referencingsNode = xmlDom.CreateElement("referencings");
                //contentNode.AppendChild(referencingsNode);
                //var dataContext = EverestCmsEntities.GetDataContext();
                //var refrencedContent = dataContext.QueryReferencedContents(this.UUID);
                //var referencingContents = refrencedContent.Select(c => c.UUID).ToArray();
                //foreach (var referencingUUID in referencingContents)
                //{
                //    XmlNode referencingContentNode = xmlDom.CreateElement("referencingContent");
                //    referencingsNode.AppendChild(referencingContentNode);

                //    XmlAttribute contentUUIDAtt = xmlDom.CreateAttribute("UUID");
                //    contentUUIDAtt.Value = referencingUUID.ToString();
                //    referencingContentNode.Attributes.Append(contentUUIDAtt);
                //}
            }
            #endregion

            #region BinaryContent
            if ((SchemaType)this.Cms_Schema.SchemaType == SchemaType.Binary)
            {
                this.Cms_BinaryContentReference.Load(this.Cms_BinaryContent, this.EntityState);
                XmlNode binaryContentNode = xmlDom.CreateElement("binaryContent");
                contentNode.AppendChild(binaryContentNode);

                //add FileSize
                XmlAttribute fileSizeAtt = xmlDom.CreateAttribute("FileSize");
                fileSizeAtt.Value = this.Cms_BinaryContent.FileSize.ToString();
                binaryContentNode.Attributes.Append(fileSizeAtt);

                //add FilePath
                XmlAttribute filePathAtt = xmlDom.CreateAttribute("FilePath");
                filePathAtt.Value = this.Cms_BinaryContent.FilePath;
                binaryContentNode.Attributes.Append(filePathAtt);
            }
            #endregion

            xmlDom.WriteTo(writer);

        }

        #endregion

        public static Cms_Content DeserializeFromNode(XmlNode parentNode, string nodeName)
        {
            var content = new Cms_Content();
            XmlNode contentNode = parentNode.SelectSingleNode(nodeName);
            if (contentNode != null)
            {
                content.UUID = new Guid(contentNode.Attributes["UUID"].Value);
                content.Title = contentNode.Attributes["Title"].Value;
                return content;
            }
            return null;
        }

        public void SerializeAsNode(XmlNode parentNode, string nodeName)
        {
            var xmlDom = parentNode.OwnerDocument;
            //add content node
            XmlNode contentNode = parentNode.OwnerDocument.CreateElement(nodeName);
            parentNode.AppendChild(contentNode);
            XmlAttribute contentUUIDAtt = xmlDom.CreateAttribute("UUID");
            contentUUIDAtt.Value = this.UUID.ToString();
            contentNode.Attributes.Append(contentUUIDAtt);

            XmlAttribute titleAtt = xmlDom.CreateAttribute("Title");
            titleAtt.Value = this.Title;
            contentNode.Attributes.Append(titleAtt);
        }
        #endregion

        #region Copy
        /// <summary>
        /// Copies the new content of the binary.
        /// </summary>
        /// <returns></returns>
        public Cms_Content CopyNewBinaryContent()
        {
            Cms_Content newBinaryContent = new Cms_Content();

            newBinaryContent.aspnet_Applications = this.aspnet_Applications;
            newBinaryContent.PostDate = DateTime.Now;
            newBinaryContent.Base = this;
            newBinaryContent.OriginalUUID = this.OriginalUUID;
            if (!this.Cms_SchemaReference.IsLoaded)
            {
                this.Cms_SchemaReference.Load();
            }
            newBinaryContent.Cms_Schema = this.Cms_Schema;
            newBinaryContent.Title = this.Title;

            if (!this.Cms_BinaryContentReference.IsLoaded)
            {
                this.Cms_BinaryContentReference.Load();
            }
            newBinaryContent.Cms_BinaryContent = new Cms_BinaryContent();
            newBinaryContent.Cms_BinaryContent.FilePath = this.Cms_BinaryContent.FilePath;
            newBinaryContent.Cms_BinaryContent.FileSize = this.Cms_BinaryContent.FileSize;

            return newBinaryContent;
        }
        #endregion

        #region Content Files
        /// <summary>
        /// Adds the content file.
        /// </summary>
        /// <param name="files">The files.</param>
        /// <param name="values">The values.</param>
        public void AddContentFiles(HttpFileCollectionBase files, NameValueCollection values)
        {
            HttpFileCollectionRemovable filesEx = new HttpFileCollectionRemovable(files);

            this.Cms_SchemaReference.Load(this.Cms_Schema, this.EntityState);
            if (!this.Cms_Schema.Cms_Column.IsLoaded)
                this.Cms_Schema.Cms_Column.Load();

            var fileColumns = this.Cms_Schema.Cms_Column.Where(c => c.ControlType.ToLower() == "file");
            foreach (var column in fileColumns)
            {
                var key = column.ColumnName;
                if (filesEx[key] != null && filesEx[key].ContentLength > 0 && !StringExtensions.IsNullOrEmptyTrim(filesEx[key].FileName))
                {
                    var filePath = SaveFile(filesEx[key]);
                    values[key] = filePath;

                    filesEx.Remove(key);
                }
            }
            if (this.Cms_Schema.FileUploadable)
            {
                foreach (var key in filesEx.AllKeys)
                {
                    if (filesEx[key].ContentLength > 0 && !StringExtensions.IsNullOrEmptyTrim(filesEx[key].FileName))
                    {
                        var filePath = SaveFile(filesEx[key]);

                        Cms_ContentFile contentFile = new Cms_ContentFile();
                        contentFile.FilePath = filePath;
                        this.ContentFiles.Add(contentFile);
                    }
                }
            }

        }
        /// <summary>
        /// Saves the file.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <returns></returns>
        private string SaveFile(HttpPostedFileBase file)
        {
            string absoluteContentFilePath = GetContentFileDirectory();
            string fileName = file.GetFileName();
            fileName = Path.GetFileNameWithoutExtension(fileName).ReplaceToValidUrl() + Path.GetExtension(fileName);
            string filePath = Path.Combine(absoluteContentFilePath, fileName);
            string savedFilePath = file.InputStream.SaveAs(filePath, false);

            return UrlConvertor.AbsolutePathToRelativeUrl(savedFilePath);
        }
        public string GetContentFileDirectory()
        {
            var content = this;
            var isContinue = false;
            var path = content.UUID.ToString();
            do
            {
                isContinue = false;
                content.ParentContentReference.Load(content.ParentContent, content.EntityState);
                if (content.ParentContent != null)
                {
                    isContinue = true;
                    content = content.ParentContent;
                    path = Path.Combine(content.UUID.ToString(), path);
                }
            }
            while (isContinue);

            if (content.Cms_Folder == null)
            {
                content.Cms_FolderReference.Load(content.Cms_Folder, content.EntityState);
            }

            return content.Cms_Folder.GetAbsoluteFolderContentFilePath(path);

            //this.ParentContentReference.Load(this.ParentContent, this.EntityState);
            //if (this.ParentContent == null)
            //{
            //    this.Cms_FolderReference.Load(this.Cms_Folder, this.EntityState);
            //    return this.Cms_Folder.GetAbsoluteFolderContentFilePath(this.UUID.ToString());
            //}
            //else
            //{
            //    this.ParentContent.Cms_FolderReference.Load(this.ParentContent.Cms_Folder, this.ParentContent.EntityState);
            //    return this.ParentContent.Cms_Folder.GetAbsoluteFolderContentFilePath(Path.Combine(this.ParentContent.UUID.ToString(), this.UUID.ToString()));
            //}
        }
        #endregion

        #region IRuleEntity Members

        /// <summary>
        /// Gets the rule violations.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<RuleViolation> GetRuleViolations()
        {
            //set FolderLevel
            if (Cms_Folder != null)
            {
                this.FolderLevel = Cms_Folder.FolderLevel;
            }
            if (aspnet_Applications != null)
            {
                this.ApplicationLevel = aspnet_Applications.ApplicationLevel;
            }

            List<RuleViolation> violations = new List<RuleViolation>();
            var dataContext = EverestCmsEntities.GetDataContext();
            //if (StringExtensions.IsNullOrEmptyTrim(this.Title))
            //{
            //    violations.Add(new RuleViolation("Title", this.Title, string.Format(Resources.FieldIsRequired, "Title")));
            //}
            this.aspnet_ApplicationsReference.Load(this.aspnet_Applications, this.EntityState);


            if (StringExtensions.IsNullOrEmptyTrim(this.UserKey))
            {
                this.UserKey = this.Title;
            }
            if (!StringExtensions.IsNullOrEmptyTrim(this.UserKey))
            {
                this.UserKey = this.UserKey.Trim().ReplaceToValidUrl();
                this.UserKey = this.UserKey.Chinese2PinYin();
                int tries = 0;
                var userKey = this.UserKey;
                while (dataContext.IsContentExists(this.aspnet_Applications.ApplicationName, userKey, this.UUID).Exists())
                {
                    tries++;
                    userKey = this.UserKey + "-" + tries.ToString();
                }
                this.UserKey = userKey;
            }

            return violations;
        }

        #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.