Cms_Schema.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_Schema.cs
/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.

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

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

You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data.SqlClient;
using System.Xml.Serialization;
using System.Runtime.Serialization;

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

using Everest.CmsServices.Services;
using Everest.CmsServices.Providers;
namespace Everest.CmsServices.Models{
    public partial class Cms_Schema : IVersionable, IXmlSerializable, IRuleEntity
    {
        public const string TitleColumnName = "Title";
        public Cms_Schema(string schemaName, SchemaType schemaType)
            : this()
        {
            this.SchemaName = schemaName;
            this.SchemaType = (int)schemaType;
        }

        /// <summary>
        /// Gets or sets the category schemas.
        /// refer to : ReferencingSchemas
        /// Separated by ',',e.g: uuid1,uuid2
        /// </summary>
        /// <value>The category schemas.</value>
        public string CategorySchemasUUID
        {
            get
            {
                return this.ReferencingSchemas;
            }
            set
            {
                this.ReferencingSchemas = value;
            }
        }

        /// <summary>
        /// Gets the dynamic table columns.
        /// </summary>
        /// <value>The dynamic table columns.</value>
        public IEnumerable<Cms_Column> DynamicTableColumns
        {
            get
            {
                if (this.Cms_Column.IsLoaded)
                {
                    this.Cms_Column.Load();
                }
                return this.Cms_Column.Where(c => !c.ColumnName.Equals(TitleColumnName, StringComparison.InvariantCultureIgnoreCase));
            }
        }

        internal Cms_Column TitleColumn
        {
            get
            {
                return this.Cms_Column.Where(c => c.ColumnName.Equals(TitleColumnName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
            }
        }
        #region static field
        static List<string> requiredItemsControls = new List<string> { 
            "combobox", 
            "lovcombo",
            "radiogroup", 
            "checkboxgroup"
        };
        #endregion

        #region IVersionable Members
        /// <summary>
        /// Called when [revert].
        /// </summary>
        /// <param name="workingItem">The working item.</param>
        /// <param name="toRevertItem">To revert item.</param>
        public void OnRevert(IVersionItem workingItem, IVersionItem toRevertItem)
        {
            IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
            var currentSchema = dataContext.QuerySchema(this.UUID).First();

            var oldSchemaName = currentSchema.SchemaName;
            currentSchema.SchemaName = this.SchemaName;
            currentSchema.ModifiedDate = DateTime.Now;
            currentSchema.UserName = workingItem.CheckinUser;
            currentSchema.Extensions = this.Extensions;
            currentSchema.MaxSize = this.MaxSize;
            currentSchema.BinarySchemas = this.BinarySchemas;
            currentSchema.FileUploadable = this.FileUploadable;
            currentSchema.ChildSchemas = this.ChildSchemas;
            currentSchema.ReferencingSchemas = this.ReferencingSchemas;
            currentSchema.IncludeUserKey = this.IncludeUserKey;

            //if the text column ,revert the columns.
            if ((SchemaType)currentSchema.SchemaType == Everest.CmsServices.Models.SchemaType.Text)
            {
                var olderColumns = currentSchema.ClearColumns(dataContext);
                foreach (var column in this.Cms_Column)
                {
                    Cms_Column newColumn = new Cms_Column();
                    //set the UUID is used when update the schema,determine if is the column is added or deleted.
                    newColumn.UUID = Guid.NewGuid();
                    newColumn.OriginalUUID = column.OriginalUUID;
                    //newColumn.ColumnId = column.ColumnId;
                    newColumn.ColumnName = column.ColumnName;
                    newColumn.Label = column.Label;
                    newColumn.DataType = column.DataType;
                    newColumn.Length = column.Length;
                    newColumn.ControlType = column.ControlType;
                    newColumn.Items = column.Items;
                    newColumn.DefaultValue = column.DefaultValue;
                    newColumn.Queryable = column.Queryable;
                    newColumn.Indexable = column.Indexable;
                    newColumn.AllowNull = column.AllowNull;
                    newColumn.Modifiable = column.Modifiable;
                    newColumn.VisibleInList = column.VisibleInList;
                    newColumn.SortOrder = column.SortOrder;
                    newColumn.VType = column.VType;
                    newColumn.Tpl = column.Tpl;
                    newColumn.Tooltip = column.Tooltip;

                    if (column.Cms_ValidatorGroup != null)
                    {
                        newColumn.Cms_ValidatorGroup = dataContext.QueryValidatorGroup(column.Cms_ValidatorGroup.UUID).FirstOrDefault();
                    }


                    currentSchema.Cms_Column.Add(newColumn);
                }

                currentSchema.ClearFunctions(dataContext);
                foreach (var function in this.Cms_SchemaFunction)
                {
                    Cms_SchemaFunction newFunction = new Cms_SchemaFunction();
                    newFunction.Name = function.Name;
                    newFunction.Script = function.Script;
                    newFunction.FormType = function.FormType;
                    currentSchema.Cms_SchemaFunction.Add(newFunction);
                }

                IContentProvider contentProvider = UnityManager.Resolve<IContentProvider>();

                contentProvider.TextContentManager.UpdateSchema(dataContext, currentSchema, oldSchemaName, olderColumns);

            }
            dataContext.SaveChanges();
        }
        public string FormSchemaName
        {
            get
            {
                switch ((SchemaType)this.SchemaType)
                {
                    case Everest.CmsServices.Models.SchemaType.Binary:
                        return "BinarySchema";
                    case Everest.CmsServices.Models.SchemaType.Text:
                        return "TextSchema";
                    default:
                        return null;
                }
            }
        }

        #endregion

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the <see cref="Cms_Schema"/> class.
        /// </summary>
        public Cms_Schema()
        {
            this.UUID = Guid.NewGuid();
        }
        #endregion

        #region Serialize
        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            var dataContext = EverestCmsEntities.GetDataContext();
            XmlDocument xmlDom = new XmlDocument();
            xmlDom.Load(reader);

            XmlNode schemaNode = xmlDom.FirstChild.FirstChild;

            this.UUID = new Guid(schemaNode.Attributes["UUID"].Value);
            this.SchemaId = int.Parse(schemaNode.Attributes["SchemaId"].Value);
            this.SchemaName = schemaNode.Attributes["SchemaName"].Value;
            this.ModifiedDate = DateTime.Parse(schemaNode.Attributes["ModifiedDate"].Value);
            this.SchemaType = (int)Enum.Parse(typeof(SchemaType), schemaNode.Attributes["SchemaType"].Value);
            this.UserName = schemaNode.Attributes["UserName"].Value;
            this.Extensions = schemaNode.Attributes["Extensions"].Value;
            this.MaxSize = schemaNode.Attributes["MaxSize"].Value.GetNullableInt();
            this.BinarySchemas = schemaNode.Attributes["BinarySchemas"].Value;
            this.FileUploadable = bool.Parse(schemaNode.Attributes["FileUploadable"].Value);
            this.ChildSchemas = schemaNode.Attributes["ChildSchemas"].Value;
            this.ReferencingSchemas = schemaNode.Attributes["ReferencingSchemas"].Value;

            if (schemaNode.Attributes["IncludeUserKey"] != null)
            {
                this.IncludeUserKey = bool.Parse(schemaNode.Attributes["IncludeUserKey"].Value);
            }

            this.aspnet_Applications = aspnet_Applications.DeserializeFromNode(schemaNode);

            if ((SchemaType)this.SchemaType == Everest.CmsServices.Models.SchemaType.Text)
            {
                #region  Columns

                XmlNode columnsNode = schemaNode.SelectSingleNode("columns");
                foreach (XmlNode columnNode in columnsNode.ChildNodes)
                {
                    Cms_Column column = new Cms_Column();

                    column.UUID = new Guid(columnNode.Attributes["UUID"].Value);
                    if (columnNode.Attributes["ColumnId"] != null)
                    {
                        column.ColumnId = int.Parse(columnNode.Attributes["ColumnId"].Value);
                    }

                    column.ColumnName = columnNode.Attributes["ColumnName"].Value;
                    column.Label = columnNode.Attributes["Label"].Value;
                    column.DataType = columnNode.Attributes["DataType"].Value;
                    column.AllowNull = columnNode.Attributes["AllowNull"].Value.GetNullableBool();
                    column.Length = columnNode.Attributes["Length"].Value.GetNullableInt();
                    column.Order = columnNode.Attributes["Order"].Value.GetNullableInt();
                    column.Queryable = columnNode.Attributes["Queryable"].Value.GetNullableBool();
                    column.Modifiable = columnNode.Attributes["Modifiable"].Value.GetNullableBool();
                    column.ControlType = columnNode.Attributes["ControlType"].Value;
                    column.DefaultValue = columnNode.Attributes["DefaultValue"].Value;
                    column.Items = columnNode.Attributes["Items"].Value;
                    column.SortOrder = (int)Enum.Parse(typeof(SortOrder), columnNode.Attributes["SortOrder"].Value);
                    column.Indexable = columnNode.Attributes["Indexable"].Value.GetNullableBool();

                    column.VisibleInList = columnNode.Attributes["VisibleInList"].Value.GetNullableBool();
                    column.OriginalUUID = new Guid(columnNode.Attributes["OriginalUUID"].Value);
                    column.VType = columnNode.Attributes["VType"].Value;
                    if (columnNode.Attributes["Tpl"] != null)
                    {
                        column.Tpl = columnNode.Attributes["Tpl"].Value;
                    }
                    if (columnNode.Attributes["Tooltip"] != null)
                    {
                        column.Tooltip = columnNode.Attributes["Tooltip"].Value;
                    }
                    //when revert the version,must be required to query the validate group object by the ValidateGroupId.
                    if (!StringExtensions.IsNullOrEmptyTrim(columnNode.Attributes["ValidatorGroup"].Value))
                    {
                        Guid validatorGroupUUID = new Guid(columnNode.Attributes["ValidatorGroup"].Value);
                        var validatorGroup = dataContext.QueryValidatorGroup(validatorGroupUUID).FirstOrDefault();
                        if (validatorGroup != null)
                        {
                            dataContext.ObjectContext.Detach(validatorGroup);
                            column.Cms_ValidatorGroup = validatorGroup;
                        }

                    }
                    this.Cms_Column.Add(column);
                    //detach the object.
                    //dataContext.ObjectContext.Detach(column);
                }
                #endregion

                XmlNode functionsNode = schemaNode.SelectSingleNode("functions");
                foreach (XmlNode functionNode in functionsNode.ChildNodes)
                {
                    Cms_SchemaFunction function = new Cms_SchemaFunction();
                    this.Cms_SchemaFunction.Add(function);
                    function.Name = functionNode.Attributes["Name"].Value;
                    function.Script = functionNode.Attributes["Script"].Value;
                    function.FormType = int.Parse(functionNode.Attributes["FormType"].Value);
                }
            }
            //dataContext.ObjectContext.Detach(this);
            //dataContext.Dispose();
        }

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

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


            //add schema id attribute
            XmlAttribute schemaIdAtt = xmlDom.CreateAttribute("SchemaId");
            schemaIdAtt.Value = this.SchemaId.ToString();
            schemaNode.Attributes.Append(schemaIdAtt);

            //add schema name attribute
            XmlAttribute schemaNameAtt = xmlDom.CreateAttribute("SchemaName");
            schemaNameAtt.Value = this.SchemaName;
            schemaNode.Attributes.Append(schemaNameAtt);

            //add modified date
            XmlAttribute modifiedDataAtt = xmlDom.CreateAttribute("ModifiedDate");
            modifiedDataAtt.Value = this.ModifiedDate.ToString();
            schemaNode.Attributes.Append(modifiedDataAtt);

            //add schemaType
            XmlAttribute schemaTypeAtt = xmlDom.CreateAttribute("SchemaType");
            schemaTypeAtt.Value = ((SchemaType)this.SchemaType).ToString();
            schemaNode.Attributes.Append(schemaTypeAtt);

            //add user name
            XmlAttribute userNameAtt = xmlDom.CreateAttribute("UserName");
            userNameAtt.Value = this.UserName;
            schemaNode.Attributes.Append(userNameAtt);

            //add application

            this.aspnet_ApplicationsReference.Load(this.aspnet_Applications, this.EntityState);
            if (this.aspnet_Applications != null)
            {
                this.aspnet_Applications.SerializeAsNode(schemaNode);
            }

            //add extensions
            XmlAttribute extensionsAtt = xmlDom.CreateAttribute("Extensions");
            extensionsAtt.Value = this.Extensions;
            schemaNode.Attributes.Append(extensionsAtt);

            //add maxSize
            XmlAttribute maxSizeAtt = xmlDom.CreateAttribute("MaxSize");
            maxSizeAtt.Value = this.MaxSize.HasValue ? this.MaxSize.ToString() : "";
            schemaNode.Attributes.Append(maxSizeAtt);

            //add binary schemas
            XmlAttribute binarySchemasAtt = xmlDom.CreateAttribute("BinarySchemas");
            binarySchemasAtt.Value = this.BinarySchemas;
            schemaNode.Attributes.Append(binarySchemasAtt);

            //add file uploadable
            XmlAttribute fileUploadableAtt = xmlDom.CreateAttribute("FileUploadable");
            fileUploadableAtt.Value = this.FileUploadable.ToString();
            schemaNode.Attributes.Append(fileUploadableAtt);

            XmlAttribute childSchemasAtt = xmlDom.CreateAttribute("ChildSchemas");
            childSchemasAtt.Value = this.ChildSchemas;
            schemaNode.Attributes.Append(childSchemasAtt);

            XmlAttribute referencingAtt = xmlDom.CreateAttribute("ReferencingSchemas");
            referencingAtt.Value = this.ReferencingSchemas;
            schemaNode.Attributes.Append(referencingAtt);

            if (this.IncludeUserKey.HasValue)
            {
                XmlAttribute includeUserKeyAtt = xmlDom.CreateAttribute("IncludeUserKey");
                includeUserKeyAtt.Value = this.IncludeUserKey.ToString();
                schemaNode.Attributes.Append(includeUserKeyAtt);
            }


            if ((SchemaType)this.SchemaType == Everest.CmsServices.Models.SchemaType.Text)
            {
                #region AddColumns

                //add columns
                XmlNode columnsNode = xmlDom.CreateNode(XmlNodeType.Element, "columns", "");
                schemaNode.AppendChild(columnsNode);

                foreach (var column in this.Cms_Column)
                {
                    XmlNode columnNode = xmlDom.CreateNode(XmlNodeType.Element, "column", "");
                    columnsNode.AppendChild(columnNode);

                    //add column uuid
                    XmlAttribute columnUUIDAtt = xmlDom.CreateAttribute("UUID");
                    columnUUIDAtt.Value = column.UUID.ToString();
                    columnNode.Attributes.Append(columnUUIDAtt);

                    //add column id
                    XmlAttribute columnIdAtt = xmlDom.CreateAttribute("ColumnId");
                    columnIdAtt.Value = column.ColumnId.ToString();
                    columnNode.Attributes.Append(columnIdAtt);

                    //add column name
                    XmlAttribute columnNameAtt = xmlDom.CreateAttribute("ColumnName");
                    columnNameAtt.Value = column.ColumnName;
                    columnNode.Attributes.Append(columnNameAtt);

                    //add column label
                    XmlAttribute columnLabelAtt = xmlDom.CreateAttribute("Label");
                    columnLabelAtt.Value = column.Label;
                    columnNode.Attributes.Append(columnLabelAtt);

                    //add column data type
                    XmlAttribute dataTypeAtt = xmlDom.CreateAttribute("DataType");
                    dataTypeAtt.Value = column.DataType;
                    columnNode.Attributes.Append(dataTypeAtt);

                    //add allow null
                    XmlAttribute allNullAtt = xmlDom.CreateAttribute("AllowNull");
                    allNullAtt.Value = column.AllowNull.HasValue ? column.AllowNull.ToString() : "";
                    columnNode.Attributes.Append(allNullAtt);

                    //add length
                    XmlAttribute lengthAtt = xmlDom.CreateAttribute("Length");
                    lengthAtt.Value = column.Length.HasValue ? column.Length.ToString() : "";
                    columnNode.Attributes.Append(lengthAtt);

                    //add order
                    XmlAttribute orderAtt = xmlDom.CreateAttribute("Order");
                    orderAtt.Value = column.Order.HasValue ? column.Order.ToString() : "";
                    columnNode.Attributes.Append(orderAtt);

                    //add queryable
                    XmlAttribute queryableAtt = xmlDom.CreateAttribute("Queryable");
                    queryableAtt.Value = column.Queryable.HasValue ? column.Queryable.ToString() : "";
                    columnNode.Attributes.Append(queryableAtt);

                    //add modifiable
                    XmlAttribute modifiableAtt = xmlDom.CreateAttribute("Modifiable");
                    modifiableAtt.Value = column.Modifiable.HasValue ? column.Modifiable.ToString() : "";
                    columnNode.Attributes.Append(modifiableAtt);

                    //add control type
                    XmlAttribute controlTypeAtt = xmlDom.CreateAttribute("ControlType");
                    controlTypeAtt.Value = column.ControlType;
                    columnNode.Attributes.Append(controlTypeAtt);

                    //add default value
                    XmlAttribute defaultValueAtt = xmlDom.CreateAttribute("DefaultValue");
                    defaultValueAtt.Value = column.DefaultValue;
                    columnNode.Attributes.Append(defaultValueAtt);

                    //add items 
                    XmlAttribute itemsAtt = xmlDom.CreateAttribute("Items");
                    itemsAtt.Value = column.Items;
                    columnNode.Attributes.Append(itemsAtt);

                    //add sort order
                    XmlAttribute sortOrderAtt = xmlDom.CreateAttribute("SortOrder");
                    sortOrderAtt.Value = ((SortOrder)column.SortOrder).ToString();
                    columnNode.Attributes.Append(sortOrderAtt);

                    //add indexable
                    XmlAttribute indexableAtt = xmlDom.CreateAttribute("Indexable");
                    indexableAtt.Value = column.Indexable.HasValue ? column.Indexable.ToString() : "";
                    columnNode.Attributes.Append(indexableAtt);

                    //add VisibleInList
                    XmlAttribute visibleInListAtt = xmlDom.CreateAttribute("VisibleInList");
                    visibleInListAtt.Value = column.VisibleInList.HasValue ? column.VisibleInList.ToString() : "";
                    columnNode.Attributes.Append(visibleInListAtt);

                    //add OriginalUUID
                    XmlAttribute originalUUIDAtt = xmlDom.CreateAttribute("OriginalUUID");
                    originalUUIDAtt.Value = column.OriginalUUID.ToString();
                    columnNode.Attributes.Append(originalUUIDAtt);

                    //add VType
                    XmlAttribute vtypeUUIDAtt = xmlDom.CreateAttribute("VType");
                    vtypeUUIDAtt.Value = column.VType;
                    columnNode.Attributes.Append(vtypeUUIDAtt);

                    //add Tpl
                    XmlAttribute tplAtt = xmlDom.CreateAttribute("Tpl");
                    tplAtt.Value = column.Tpl;
                    columnNode.Attributes.Append(tplAtt);


                    XmlAttribute tooltipAtt = xmlDom.CreateAttribute("Tooltip");
                    tooltipAtt.Value = column.Tooltip;
                    columnNode.Attributes.Append(tooltipAtt);

                    //add validation group
                    column.Cms_ValidatorGroupReference.Load(column.Cms_ValidatorGroup, this.EntityState);

                    XmlAttribute validateGroupAtt = xmlDom.CreateAttribute("ValidatorGroup");
                    validateGroupAtt.Value = column.Cms_ValidatorGroup != null ? column.Cms_ValidatorGroup.UUID.ToString() : "";
                    columnNode.Attributes.Append(validateGroupAtt);

                    //add refrence schema and column have not be added yet..
                }
                #endregion

                #region Add Functions
                XmlNode functionsNode = xmlDom.CreateNode(XmlNodeType.Element, "functions", "");
                schemaNode.AppendChild(functionsNode);

                if (!this.Cms_SchemaFunction.IsLoaded && this.EntityState != System.Data.EntityState.Detached)
                {
                    this.Cms_SchemaFunction.Load();
                }
                foreach (var function in this.Cms_SchemaFunction)
                {
                    XmlNode functionNode = xmlDom.CreateNode(XmlNodeType.Element, "function", "");
                    functionsNode.AppendChild(functionNode);

                    XmlAttribute functionNameAtt = xmlDom.CreateAttribute("Name");
                    functionNameAtt.Value = function.Name;
                    functionNode.Attributes.Append(functionNameAtt);

                    XmlAttribute formTypeAtt = xmlDom.CreateAttribute("FormType");
                    formTypeAtt.Value = function.FormType.ToString();
                    functionNode.Attributes.Append(formTypeAtt);

                    XmlAttribute scriptAtt = xmlDom.CreateAttribute("Script");
                    scriptAtt.Value = function.Script;
                    functionNode.Attributes.Append(scriptAtt);
                }

                #endregion
            }


            xmlDom.WriteTo(writer);
        }

        #endregion

        public static Cms_Schema DeserializeFromNode(XmlNode parentNode)
        {
            var schema = new Cms_Schema();
            XmlNode schemaNode = parentNode.SelectSingleNode("schema");
            if (schemaNode != null)
            {
                schema.UUID = new Guid(schemaNode.Attributes["UUID"].Value);
                schema.SchemaName = schemaNode.Attributes["SchemaName"].Value;
                schema.SchemaType = int.Parse(schemaNode.Attributes["SchemaType"].Value);
                return schema;
            }
            return null;
        }

        public void SerializeAsNode(XmlNode parentNode)
        {
            var xmlDom = parentNode.OwnerDocument;
            //add schema
            XmlNode schemaNode = parentNode.OwnerDocument.CreateElement("schema");
            parentNode.AppendChild(schemaNode);
            XmlAttribute schemaUUIDAtt = xmlDom.CreateAttribute("UUID");
            schemaUUIDAtt.Value = this.UUID.ToString();
            schemaNode.Attributes.Append(schemaUUIDAtt);

            XmlAttribute schemaNameAtt = xmlDom.CreateAttribute("SchemaName");
            schemaNameAtt.Value = this.SchemaName;
            schemaNode.Attributes.Append(schemaNameAtt);

            XmlAttribute schemaTypeAtt = xmlDom.CreateAttribute("SchemaType");
            schemaTypeAtt.Value = this.SchemaType.ToString();
            schemaNode.Attributes.Append(schemaTypeAtt);
        }
        #endregion

        #region Clear
        /// <summary>
        /// Clears the columns.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <returns></returns>
        internal List<Cms_Column> ClearColumns(IEverestCmsDataContext dataContext)
        {
            var olderColumns = new List<Cms_Column>();
            var columns = dataContext.Cms_Column.Where(c => c.Cms_Schema.UUID == this.UUID);
            foreach (var col in columns)
            {
                olderColumns.Add(col);
                dataContext.DeleteObject(col);
            }
            //this.Cms_Column.Load();
            //Cms_Column column = null;
            //do
            //{
            //    column = this.Cms_Column.FirstOrDefault();
            //    if (column != null)
            //    {
            //        olderColumns.Add(column);
            //        dataContext.DeleteObject(column);
            //    }

            //} while (column != null);
            //this.Cms_Column.Clear();
            return olderColumns;
        }
        /// <summary>
        /// Clears the functions.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        internal void ClearFunctions(IEverestCmsDataContext dataContext)
        {
            this.Cms_SchemaFunction.Load();
            Cms_SchemaFunction function = null;
            do
            {
                function = this.Cms_SchemaFunction.FirstOrDefault();
                if (function != null)
                {
                    dataContext.DeleteObject(function);
                }

            } while (function != null);
        }
        #endregion

        #region IRuleEntity Members
        static IList<string> invalidColumns = new List<string>()
        {
            "UUID",
            "ContentId",
            "ApplicationId",
            "UserName",           
            "ModifiedDate",
            "OriginalUUID",
            "PostDate",
            "BaseUUID",
            "ParentUUID",
            "SchemaUUID",
            "FolderUUID",
            "ContentStatus",
            "UserKey",
            "ApplicationLevel",
            "FolderLevel"
        };
        public IEnumerable<RuleViolation> GetRuleViolations()
        {
            IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
            List<RuleViolation> violations = new List<RuleViolation>();

            this.aspnet_ApplicationsReference.Load(this.aspnet_Applications, this.EntityState);
            if (StringExtensions.IsNullOrEmptyTrim(this.SchemaName))
            {
                violations.Add(new RuleViolation("SchemaName", this.SchemaName, string.Format(Resources.FieldIsRequired, "SchemaName")));
            }
            if (dataContext.IsSchemaExists(this.SchemaName, this.aspnet_Applications.ApplicationName, this.SchemaId).Exists())
            {
                violations.Add(new RuleViolation("SchemaName", this.SchemaName, Resources.SchemaIsAlreadyExists));
            }

            int i = 0;
            foreach (var column in this.Cms_Column)
            {
                i = i + 1;


                if (StringExtensions.IsNullOrEmptyTrim(column.ColumnName))
                {
                    violations.Add(new RuleViolation("SchemaName", column.ColumnName, string.Format(Resources.ColumnNameIsRequired, i)));
                }
                else
                {
                    if (invalidColumns.Contains(column.ColumnName, StringComparer.InvariantCultureIgnoreCase))
                    {
                        violations.Add(new RuleViolation("SchemaName", column.ColumnName, string.Format(Resources.ColumnNameIsInvalid, i)));
                    }
                }
                if (StringExtensions.IsNullOrEmptyTrim(column.DataType))
                {
                    violations.Add(new RuleViolation("SchemaName", column.DataType, string.Format(Resources.ColumnDataTypeIsRequired, i)));
                }
                if (StringExtensions.IsNullOrEmptyTrim(column.ControlType))
                {
                    violations.Add(new RuleViolation("SchemaName", column.ControlType, string.Format(Resources.ColumnControlTypeIsRequired, i)));
                }

                if (requiredItemsControls.Contains(column.ControlType.ToLower()) && StringExtensions.IsNullOrEmptyTrim(column.Items))
                {
                    violations.Add(new RuleViolation("SchemaName", column.Items, string.Format(Resources.ColumnSelectionItemsIsRequired, i)));
                }
            }
            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.