TextResourceController.cs :  » Content-Management-Systems-CMS » Kooboo » Everest » CmsServices » Controllers » 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 » Controllers » TextResourceController.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.IO;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Text;
using Everest.CmsServices.Models;
using Everest.CmsServices.Services;
using Everest.Forms;
using Everest.Library;
using Everest.Library.Data.Entity;
using Everest.Library.Extjs;
using Everest.Library.Mvc;
using Everest.Library.Json;
using Everest.Library.Data;
using Everest.Library.Data.Rule;

using Everest.CmsServices.Search;
using Everest.Library.Versioning;
using Everest.Library.Extjs.Tree;
using Everest.Library.ExtensionMethod;

namespace Everest.CmsServices.Controllers{
    /// <summary>
    /// 
    /// </summary>
    [ValidateInput(false)]
    [PermissionFilter(Permission = FolderType.TextResource)]
    public class TextResourceController : CmsExtController
    {
        IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();

        #region Standardactions

        /// <summary>
        /// Gets the list.
        /// </summary>
        /// <returns></returns>
        public System.Web.Mvc.ActionResult GetList()
        {
            int start, limit;

            EnsurePaging(out start, out limit);

            string application = Request.Form["application"];
            string strNamespace = Request.Form["fullNamespace"];
            IQueryable<Cms_TextResource> query = dataContext.QueryTextResourcesByNamespace(application, strNamespace);
            string resourceKey = Request.Form["ResourceKey"];
            if (!StringExtensions.IsNullOrEmptyTrim(resourceKey))
            {
                query = query.Where(tr => tr.ResourceKey.Contains(resourceKey));
            }
            query = query.OrderByDescending(tr => tr.ResourceId);
            query = OrderByRequest(query);

            var queryResult = query.Select<Cms_TextResource, object>(s => new
            {
                s.UUID,
                s.ResourceId,
                s.ResourceKey,
                s.ResourceValue,
                s.Namespace,
                Application = s.aspnet_Applications.ApplicationName,
                IsLocalized = s.aspnet_Applications.ApplicationName == application,
                BaseUUID = s.Base == null ? null : (Guid?)s.Base.UUID
            });
            return Json(new ExtJsonReaderObject(queryResult.Skip(start).Take(limit).ToArray(), queryResult.Count()));
        }

        /// <summary>
        /// Gets the details.
        /// </summary>
        /// <returns></returns>
        [ValidateInput(false)]
        public System.Web.Mvc.ActionResult GetDetails()
        {
            int resourceId = int.Parse(Request.Form["ResourceId"]);

            object jsonData = GetTextResouceDetails(resourceId);

            return Json(new JsonResultData() { success = true, data = jsonData });
        }
        /// <summary>
        /// Gets the text resouce details.
        /// </summary>
        /// <param name="resourceId">The resource id.</param>
        /// <returns></returns>
        private object GetTextResouceDetails(int resourceId)
        {
            var jsonData = dataContext.QueryTextResources(resourceId).Select(tr => new
                            {
                                tr.UUID,
                                Application = tr.aspnet_Applications.ApplicationName,
                                tr.ResourceId,
                                tr.ResourceKey,
                                FormTitle = tr.ResourceKey,
                                tr.ResourceValue,
                                tr.Namespace
                            }).First();

            return jsonData;
        }
        /// <summary>
        /// Submits the specified add.
        /// </summary>
        /// <param name="add">if set to <c>true</c> [add].</param>
        /// <param name="closeForm"></param>
        /// <returns></returns>
        [ValidateInput(false)]
        public System.Web.Mvc.ActionResult Submit(bool add, bool? closeForm)
        {
            JsonResultData resultData = new JsonResultData();
            try
            {
                Cms_TextResource textResource;
                if (add)
                {
                    textResource = new Cms_TextResource();
                    string application = Request.Form["application"];
                    textResource.aspnet_Applications = dataContext.QueryApplication(application).First();
                    textResource.ApplicationLevel = textResource.aspnet_Applications.ApplicationLevel;
                    dataContext.AddToCms_TextResource(textResource);
                }
                else
                {
                    int textResourceId = int.Parse(Request.Form["oldData.ResourceId"]);
                    textResource = dataContext.QueryTextResources(textResourceId).First();
                }

                textResource.ResourceKey = Request.Form["ResourceKey"];
                textResource.ResourceValue = Request.Form["ResourceValue"];

                dataContext.SaveChanges();
                textResource.Checkin(User.Identity.Name, "");
                if (closeForm.Value == false && resultData.success)
                {
                    resultData.closeForm = false;
                    resultData.data = GetTextResouceDetails(textResource.ResourceId);
                }
            }
            catch (RuleViolationException ruleException)
            {
                ruleException.Issues.UpdateResultDataWithViolations(resultData);
            }
            return Json(resultData);
        }

        /// <summary>
        /// Deletes this instance.
        /// </summary>
        /// <returns></returns>
        public System.Web.Mvc.ActionResult Delete(Guid[] uuid)
        {
            JsonResultData resultData = new JsonResultData() { success = true };
            foreach (var guid in uuid)
            {
                var textResource = dataContext.QueryTextResource(guid).First();
                if (BeReferenced(guid))
                {
                    resultData.message = string.Format(Resources.ItemCouldNotBeDeleted, textResource.ResourceKey);
                }
                else
                {
                    dataContext.DeleteObject(textResource);
                    textResource.ClearVersions();
                    dataContext.SaveChanges();
                }
            }
            return Json(resultData);
        }

        private bool BeReferenced(Guid uuid)
        {
            if (dataContext.QueryTextResourceLocalizations(uuid).Exists())
            {
                return true;
            }
            return false;
        }
        #endregion

        #region Get Tree Nodes

        public ActionResult GetNamespaceNodes(string application, string fullNamespace, string id, string schema,
            string dataUrl, string folderUUID)
        {
            var namespaces = UnityManager.Resolve<TextResourceService>().GetChildNamespaces(application, fullNamespace);
            List<Dictionary<string, object>> treeNodes = new List<Dictionary<string, object>>();
            foreach (var item in namespaces)
            {
                TreeNode treeNode = new TreeNode();
                treeNode.Text = item.Name;
                treeNode.ParentNode = id;
                treeNode.AddAttribute("schema", schema);
                //treeNode.AddAttribute("formType", item.FormType);
                treeNode.AddAttribute("folderType", FolderType.TextResourceNamespace.ToString());
                treeNode.AddAttribute("application", application);
                treeNode.AddAttribute("dataUrl", dataUrl);
                treeNode.AddAttribute("fullNamespace", item.FullName);
                treeNode.AddAttribute("folderUUID", folderUUID);
                treeNode.IconCls = "Namespace";
                treeNodes.Add(treeNode.Attributes);
            }
            return Json(treeNodes);
        }
        #endregion

        #region localize
        /// <summary>
        /// Localizes the specified text resource.
        /// </summary>
        /// <param name="resourceId">The resource id.</param>
        /// <param name="application">The application.</param>
        /// <returns></returns>
        [ValidateInput(false)]
        public ActionResult Localize(int resourceId, string application)
        {
            JsonResultData resultData = new JsonResultData();

            var aspnet_Application = dataContext.QueryApplication(application).First();

            var originalTextResource = dataContext.QueryTextResources(resourceId).FirstOrDefault();

            if (originalTextResource != null)
            {
                var localizedTextResource = originalTextResource.CopyNew();
                localizedTextResource.aspnet_Applications = aspnet_Application;
                localizedTextResource.ApplicationLevel = localizedTextResource.aspnet_Applications.ApplicationLevel;
                //localizedTextResource.UserName = User.Identity.Name;
                localizedTextResource.Base = originalTextResource;
                localizedTextResource.OriginalUUID = originalTextResource.OriginalUUID;

                dataContext.AddToCms_TextResource(localizedTextResource);

                dataContext.SaveChanges();

                localizedTextResource.Checkin(User.Identity.Name, string.Format(Resources.CheckinByLocalization, originalTextResource.ResourceId));

                //
                resultData.data = localizedTextResource;
            }
            return Json(resultData);
        }
        #endregion

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

            #region Localizations
            var localizationsNodes = new TreeNode();
            localizationsNodes.Text = Resources.LocalizedBy;
            localizationsNodes.IconCls = FolderType.TextResource.ToString();
            localizationsNodes.Expanded = true;
            treeNodes.Add(localizationsNodes.Attributes);

            var localizations = dataContext.QueryTextResourceLocalizations(uuid)
                .Select(tr => new
                {
                    tr.aspnet_Applications.ApplicationName,
                    tr.ResourceKey,
                    tr.UUID
                });
            localizationsNodes.children = new List<IDictionary<string, object>>();
            foreach (var textResource in localizations)
            {
                var textResourceNode = new TreeNode();
                localizationsNodes.AddChild(textResourceNode);
                textResourceNode.Text = new UniqueName(textResource.ApplicationName, textResource.ResourceKey).ToString();
                textResourceNode.IconCls = FolderType.TextResource.ToString();
                textResourceNode.AddAttribute("uuid", textResource.UUID);
            }
            #endregion

            #region Localize From
            var localizeFromNodes = new TreeNode();
            localizeFromNodes.Text = Resources.LocalizeFrom;
            localizeFromNodes.IconCls = FolderType.TextResource.ToString();
            localizeFromNodes.Expanded = true;
            treeNodes.Add(localizeFromNodes.Attributes);

            var localizeFrom = dataContext.QueryTextResource(uuid).Where(tr => tr.Base != null)
                .Select(tr => new
                {
                    tr.Base.aspnet_Applications.ApplicationName,
                    tr.Base.ResourceKey,
                    tr.Base.UUID
                });
            localizeFromNodes.children = new List<IDictionary<string, object>>();
            foreach (var textResource in localizeFrom)
            {
                var textResourceNode = new TreeNode();
                localizeFromNodes.AddChild(textResourceNode);
                textResourceNode.Text = new UniqueName(textResource.ApplicationName, textResource.ResourceKey).ToString();
                textResourceNode.IconCls = FolderType.TextResource.ToString();
                textResourceNode.AddAttribute("uuid", textResource.UUID);
            }
            #endregion

            return Json(treeNodes);
        }
        #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.