/*
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.Text.RegularExpressions;
using Everest.Library.Providers.Caching;
using Everest.CmsServices.Models;
using Everest.Library.Data;
using Everest.Library.ExtensionMethod;
namespace Everest.CmsServices.Services{
/// <summary>
///
/// </summary>
public class TextResourceService
{
/// <summary>
/// Initializes a new instance of the <see cref="TextResourceService"/> class.
/// </summary>
public TextResourceService()
{
}
#region Parse Text
static Regex regexGetResource = new Regex(@"GetResource\(\s*@*("".*?[^""\\]""(?!""))\s*(?:\)|(?:,\s*@*("".*?[^""\\]""(?!"")||"""")\s*))?(?:\)|(?:,\s*@*("".*?[^""\\]""(?!"")||"""")\s*))?\)", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
/// <summary>
/// Parses the text.
/// </summary>
/// <param name="application">The application.</param>
/// <param name="s">The s.</param>
public void ParseText(aspnet_Applications application, string s)
{
var matches = regexGetResource.Matches(s);
if (matches.Count > 0)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
List<Cms_TextResource> insertedResource = new List<Cms_TextResource>();
foreach (Match match in matches)
{
//the group value is empty or more than 2 chars.("")
if (!IsValid(match.Groups[1].Value, match.Groups[2].Value, match.Groups[3].Value))
{
break;
}
string namespaceName, key, defaultValue;
//GetResource("namespace","key","defalutValue");
if (StringExtensions.IsNullOrEmptyTrim(match.Groups[3].Value))
{
key = match.Groups[1].Value.Substring(1, match.Groups[1].Value.Length - 2);
if (StringExtensions.IsNullOrEmptyTrim(match.Groups[2].Value))
{
//GetResource(string);
defaultValue = key;
}
else
{
//GetResource(string,string);
defaultValue = match.Groups[2].Value.Substring(1, match.Groups[2].Value.Length - 2);
}
}
else
{
//GetResource(string,string,string);
namespaceName = match.Groups[1].Value.Substring(1, match.Groups[1].Value.Length - 2);
key = match.Groups[2].Value.Substring(1, match.Groups[2].Value.Length - 2);
if (!StringExtensions.IsNullOrEmptyTrim(namespaceName))
{
key = namespaceName + "." + key;
}
defaultValue = match.Groups[3].Value.Substring(1, match.Groups[3].Value.Length - 2);
}
if (!StringExtensions.IsNullOrEmptyTrim(key))
{
//do not inserted this time.
if (insertedResource.Where(tr => tr.ResourceKey.Equals(key, StringComparison.InvariantCultureIgnoreCase)).Count() == 0)
{
var textResource = InsertTextResource(dataContext, application, key, defaultValue);
if (textResource != null)
{
insertedResource.Add(textResource);
}
}
}
}
dataContext.SaveChanges();
}
}
/// <summary>
/// Adds the text resource.
/// </summary>
/// <param name="application">The application.</param>
/// <param name="key">The key.</param>
/// <param name="defaultValue">The default value.</param>
public void AddTextResource(string application, string key, string defaultValue)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
var app = dataContext.QueryApplication(application).First();
InsertTextResource(dataContext, app, key, defaultValue);
dataContext.SaveChanges();
}
/// <summary>
/// Inserts the text resource.
/// </summary>
/// <param name="application">The application.</param>
/// <param name="key">The key.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns></returns>
private Cms_TextResource InsertTextResource(IEverestCmsDataContext dataContext, aspnet_Applications application, string key, string defaultValue)
{
var isExists = dataContext.QueryTextResources(application.ApplicationName, key).Exists();
if (!isExists)
{
Cms_TextResource textResource = new Cms_TextResource();
textResource.ResourceKey = key;
textResource.ResourceValue = defaultValue;
textResource.aspnet_Applications = application;
textResource.OriginalUUID = textResource.UUID;
dataContext.AddToCms_TextResource(textResource);
return textResource;
}
return null;
}
/// <summary>
/// Determines whether the specified ns is valid.
/// </summary>
/// <param name="ns">The ns.</param>
/// <param name="key">The key.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>
/// <c>true</c> if the specified ns is valid; otherwise, <c>false</c>.
/// </returns>
private bool IsValid(string ns, string key, string defaultValue)
{
if (StringExtensions.IsNullOrEmptyTrim(defaultValue))
{
key = ns;
}
if (StringExtensions.IsNullOrEmptyTrim(key) || key.Length < 2)
{
return false;
}
if ((!StringExtensions.IsNullOrEmptyTrim(ns) && ns.Length < 2) || (!StringExtensions.IsNullOrEmptyTrim(defaultValue) && defaultValue.Length < 2))
{
return false;
}
return true;
}
/// <summary>
/// Gets all the text resources in the application.
/// </summary>
/// <param name="application">The application.</param>
/// <returns></returns>
public IDictionary<string, string> GetTextResources(string application)
{
string cacheKey = string.Format("__TextResource_Application:{0}", application);
IDictionary<string, string> resources = CacheManager.Get(CachedData.Template, cacheKey) as IDictionary<string, string>;
if (resources == null)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
var textResourceQuery = dataContext.QueryTextResources(application);
resources = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
foreach (var item in textResourceQuery)
{
if (!resources.ContainsKey(item.ResourceKey))
{
resources.Add(item.ResourceKey, item.ResourceValue);
}
}
CacheManager.Add(CachedData.Template, cacheKey, resources, CacheItemPriority.Normal, null, CachedData.DefaultExpirations);
}
return resources;
}
#endregion
#region Get Namespaces
/// <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)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
var namespaces = dataContext.GroupTextResourceNamespaces(application, parentNamespace).Select(gtr => gtr.Key).ToList();
return NamespaceHelper.GetChildNamespaces(parentNamespace, namespaces);
}
#endregion
}
}
|