ContentTemplateService.cs :  » Content-Management-Systems-CMS » Kooboo » Everest » CmsServices » Services » 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 » Services » ContentTemplateService.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 Everest.CmsServices.Models;
using Everest.Library.Json;
using Everest.Library.Versioning;
using Everest.Library.Data;
using Everest.Library.Extjs.Tree;
using Everest.CmsServices.Extension.Module;
using Everest.Library.Extjs;
using Everest.Library.ExtensionMethod;
namespace Everest.CmsServices.Services{
    public class ContentTemplateService
    {
        IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();

        #region Add && Update
        /// <summary>
        /// Adds the content template.
        /// </summary>
        /// <param name="values">The values.</param>
        /// <param name="userName">Name of the user.</param>
        /// <returns></returns>
        public Cms_ContentTemplate AddContentTemplate(NameValueCollection values, string userName)
        {
            var template = new Cms_ContentTemplate();
            template.UUID = Guid.NewGuid();
            template.OriginalUUID = template.UUID;

            template.Name = values["Name"];
            template.Body = values["Body"];
            template.Description = values["Description"];
            template.UserName = userName;
            template.ModifiedDate = DateTime.Now;
            string application = values["Application"];
            template.aspnet_Applications = dataContext.QueryApplication(application).First();
            template.ApplicationLevel = template.aspnet_Applications.ApplicationLevel;
            dataContext.AddToCms_ContentTemplate(template);


            #region Parameters
            //AddParameters(values, template);

            #endregion

            #region Plugins
            AddPlugins(values, template);
            #endregion

            #region DataRule
            AddDataRules(dataContext, values, template);

            #endregion
            template.SaveBody();
            dataContext.SaveChanges();


            //save version history
            template.Checkin(userName, "");

            return template;
        }
        /// <summary>
        /// Updates the content template.
        /// </summary>
        /// <param name="contentTemplateUUID">The content template UUID.</param>
        /// <param name="values">The values.</param>
        /// <param name="userName">Name of the user.</param>
        /// <returns></returns>
        public Cms_ContentTemplate UpdateContentTemplate(Guid contentTemplateUUID, NameValueCollection values, string userName)
        {
            var template = dataContext.QueryContentTemplate(contentTemplateUUID).First();

            //why to SaveChanged???
            //dataContext.SaveChanges();
            string oldFileName = template.FileName;

            template.Name = values["Name"];
            template.Body = values["Body"];
            template.Description = values["Description"];
            template.UserName = userName;
            template.ModifiedDate = DateTime.Now;
            //clear parameters
            template.ClearParameters(dataContext);
            //clear plugins
            template.ClearPlugins(dataContext);
            template.ClearDataRule(dataContext);

            //#region Parameters
            //AddParameters(values, template);

            //#endregion

            #region Plugins
            AddPlugins(values, template);
            #endregion

            #region DataRule
            AddDataRules(dataContext, values, template);

            #endregion
            template.SaveBody();
            dataContext.SaveChanges();

            if (!oldFileName.Equals(template.FileName, StringComparison.InvariantCultureIgnoreCase))
            {
                template.DeleteBody(oldFileName);
            }
            //save version history
            template.Checkin(userName, "");

            return template;
        }
        /// <summary>
        /// Adds the data rules.
        /// </summary>
        /// <param name="dataContext">The data context.</param>
        /// <param name="values">The values.</param>
        /// <param name="template">The template.</param>
        private void AddDataRules(IEverestCmsDataContext dataContext, NameValueCollection values, Cms_ContentTemplate template)
        {
            IDictionary<string, string>[] newDataRules = values["DataRules"].DeserializeJSON<Dictionary<string, string>[]>();
            int itemCount = 0;
            for (int i = 0; i < newDataRules.Length; i++)
            {
                var dataRule = Cms_DataRule.CreateCms_DataRule(dataContext, newDataRules[i]);
                if (dataRule != null)
                {
                    itemCount++;
                    dataRule.Order = itemCount;
                    template.Cms_DataRule.Add(dataRule);
                }
            }
        }

        private static void AddPlugins(NameValueCollection values, Cms_ContentTemplate template)
        {
            Cms_Plugin[] plugins = values["Plugins"].DeserializeJSON<Cms_Plugin[]>();
            foreach (var plugin in plugins)
            {
                template.Cms_Plugin.Add(plugin);
            }
        }

        //private static void AddParameters(NameValueCollection values, Cms_ContentTemplate template)
        //{
        //    Cms_ContentTemplateParameters[] parameters = values["Parameters"].DeserializeJSON<Cms_ContentTemplateParameters[]>();
        //    template.AddParameters(parameters);
        //}
        #endregion

        #region Delete
        /// <summary>
        /// Deletes the specified UUID.
        /// </summary>
        /// <param name="uuid">The UUID.</param>
        public void Delete(Guid uuid)
        {
            var template = dataContext.QueryContentTemplate(uuid).First();
            if (BeReferenced(uuid))
            {
                throw new Exception(string.Format(Resources.ItemCouldNotBeDeleted, template.Name));
            }
            else
            {
                template.DeleteBody();
                dataContext.DeleteObject(template);
                template.ClearVersions();
                dataContext.SaveChanges();
            }
        }
        private bool BeReferenced(Guid uuid)
        {
            if (dataContext.QueryPagesByContentTemplate(uuid).Exists())
            {
                return true;
            }
            if (dataContext.QueryTemplateLocalizations(uuid).Exists())
            {
                return true;
            }
            return false;
        }
        #endregion

        #region Localize
        /// <summary>
        /// Localizes the specified content template UUID.
        /// </summary>
        /// <param name="contentTemplateUUID">The content template UUID.</param>
        /// <param name="application">The application.</param>
        /// <param name="userName">Name of the user.</param>
        /// <returns></returns>
        public Cms_ContentTemplate Localize(Guid contentTemplateUUID, string application, string userName)
        {
            var aspnet_Application = dataContext.QueryApplication(application).First();

            var originalContentTemplate = dataContext.QueryContentTemplate(contentTemplateUUID).First();

            if (originalContentTemplate != null)
            {
                var localizedContentTemplate = originalContentTemplate.CopyNew();
                localizedContentTemplate.aspnet_Applications = aspnet_Application;
                localizedContentTemplate.ApplicationLevel = localizedContentTemplate.aspnet_Applications.ApplicationLevel;
                localizedContentTemplate.UserName = userName;
                localizedContentTemplate.Base = originalContentTemplate;
                localizedContentTemplate.OriginalUUID = originalContentTemplate.OriginalUUID;

                dataContext.AddToCms_ContentTemplate(localizedContentTemplate);

                dataContext.SaveChanges();
                localizedContentTemplate.SaveBody();

                localizedContentTemplate.Checkin(userName, string.Format(Resources.CheckinByLocalization, originalContentTemplate.ContentTemplateId));

                return localizedContentTemplate;
            }
            return null;
        }
        #endregion

        #region Relation
        public IList<Dictionary<string, object>> UsedBy(Guid uuid, string dataUrl)
        {
            IList<Dictionary<string, object>> treeNodes = new List<Dictionary<string, object>>();

            #region UsedBy Pages

            var pagesNode = new TreeNode();
            treeNodes.Add(pagesNode.Attributes);
            pagesNode.Text = Resources.TreeNode_Page;
            pagesNode.IconCls = FolderType.Page.ToString();
            pagesNode.Expanded = true;

            var pages = dataContext.QueryPagesByContentTemplate(uuid)
                .Select(p => new
                {
                    p.UUID,
                    p.aspnet_Applications.ApplicationName,
                    p.PageName
                });
            pagesNode.children = new List<IDictionary<string, object>>();
            foreach (var page in pages)
            {
                var pageNode = new TreeNode();
                pagesNode.children.Add(pageNode.Attributes);
                pageNode.Text = new UniqueName(CmsGlobal.GetApplicationName(page.ApplicationName), page.PageName).ToString();
                pageNode.Attributes.Add("uuid", page.UUID.ToString());
                pageNode.Attributes.Add("dataUrl", "Kooboo_Page/UsedBy");
                pageNode.IconCls = FolderType.PageNode.ToString();
            }

            #endregion

            #region LocalizedBy

            var localizationsNodes = new TreeNode();
            treeNodes.Add(localizationsNodes.Attributes);
            localizationsNodes.Text = Resources.LocalizedBy;
            localizationsNodes.Expanded= true;
            localizationsNodes.IconCls = FolderType.ContentTemplate.ToString();
            var localizations = dataContext.QueryTemplateLocalizations(uuid).Select(ct => new
            {
                ct.UUID,
                ct.aspnet_Applications.ApplicationName,
                ct.Name
            });
            localizationsNodes.children = new List<IDictionary<string, object>>();
            foreach (var contentTemplate in localizations)
            {
                var localizationNode = new TreeNode();
                localizationsNodes.children.Add(localizationNode.Attributes);
                localizationNode.Text = new UniqueName(CmsGlobal.GetApplicationName(contentTemplate.ApplicationName), contentTemplate.Name).ToString();
                localizationNode.IconCls = FolderType.ContentTemplate.ToString();
                localizationNode.Attributes.Add("uuid", contentTemplate.UUID);
                localizationNode.AddAttribute("dataUrl", dataUrl);
            }
            #endregion

            #region LocalizeFrom
            var localizeFromNodes = new TreeNode();
            treeNodes.Add(localizeFromNodes.Attributes);
            localizeFromNodes.Text = Resources.LocalizeFrom;
            localizeFromNodes.Expanded = true;
            localizeFromNodes.IconCls = FolderType.ContentTemplate.ToString();
            var localizeFrom = dataContext.QueryContentTemplate(uuid).Where(ct => ct.Base != null)
                .Select(ct => new
                {
                    ct.Base.UUID,
                    ct.Base.aspnet_Applications.ApplicationName,
                    ct.Base.Name
                });
            localizeFromNodes.children = new List<IDictionary<string, object>>();
            foreach (var contentTemplate in localizeFrom)
            {
                var localizeFromNode = new TreeNode();
                localizeFromNodes.children.Add(localizeFromNode.Attributes);
                localizeFromNode.Text = new UniqueName(CmsGlobal.GetApplicationName(contentTemplate.ApplicationName), contentTemplate.Name).ToString();
                localizeFromNode.Attributes.Add("uuid", contentTemplate.UUID);
                localizeFromNode.AddAttribute("dataUrl", dataUrl);
                localizeFromNode.IconCls = FolderType.ContentTemplate.ToString();
            }
            #endregion
            return treeNodes;
        }
        #endregion

        /// <summary>
        /// Gets the child namespaces.
        /// </summary>
        /// <param name="application">The application.</param>
        /// <param name="parentNamespace">The parent namespace.</param>
        /// <returns></returns>
        public IEnumerable<NamespaceObject> GetChildNamespaces(string application, string parentNamespace)
        {
            var namespaces = dataContext.GroupContentTemplateNamespaces(application, parentNamespace).Select(gtr => gtr.Key).ToList();
            return NamespaceHelper.GetChildNamespaces(parentNamespace, namespaces);
        }
        /// <summary>
        /// Gets the components for page.
        /// </summary>
        /// <param name="application">The application.</param>
        /// <returns></returns>
        public IEnumerable<Component> GetComponentsForPage(string application)
        {
            List<Component> components = new List<Component>();
            var contentTemplateQuery = dataContext.QueryContentTemplates(application).OrderBy(ct => ct.Name);

            foreach (var contentTemplate in contentTemplateQuery)
            {
                components.Add(new Component()
                {
                    ComponentName = contentTemplate.Name,
                    ComponentType = ComponentType.ContentTemplate,
                    ComponentValue = contentTemplate.ContentTemplateId.ToString()
                });
            }

            //get all modules
            var modules = ModuleManager.GetModules(application);
            foreach (var module in modules)
            {
                if (module.Configuration.Installed)
                {
                    var moduleName = module.ModuleName;
                    components.Add(new Component()
                    {
                        ComponentName = moduleName,
                        ComponentType = ComponentType.Module,
                        ComponentValue = moduleName
                    });
                }

            }

            //get all webforms
            var webForms = dataContext.QueryWebForms(application);
            foreach (var webform in webForms)
            {
                components.Add(new Component()
                {
                    ComponentName = webform.FormName,
                    ComponentType = ComponentType.WebForm,
                    ComponentValue = webform.UUID.ToString()
                });
            }

            return components;
        }
    }
}

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