Text.cs :  » Content-Management-Systems-CMS » umbraco » umbraco » cms » businesslogic » language » 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 » umbraco 
umbraco » umbraco » cms » businesslogic » language » Text.cs
using System;
using System.Collections;
using System.Data;

using umbraco.DataLayer;
using umbraco.BusinessLogic;

namespace umbraco.cms.businesslogic.language{
    /// <summary>
    /// Item class contains method for accessing language translated text, its a generic component which
    /// can be used for storing language translated content, all items are associated to an unique identifier (Guid)
    /// 
    /// The data is cached and are usable in the public website.
    /// 
    /// Primarily used by the built-in dictionary
    /// 
    /// </summary>
    public class Item
    {
        private static Hashtable _items = Hashtable.Synchronized(new Hashtable());
        private static bool isInitialized = false;
        private static string _Connstring = GlobalSettings.DbDSN;

        /// <summary>
        /// Gets the SQL helper.
        /// </summary>
        /// <value>The SQL helper.</value>
        protected static ISqlHelper SqlHelper
        {
            get { return Application.SqlHelper; }
        }

        private static void ensureData()
        {
            if (!isInitialized)
            {
                // load all data
                IRecordsReader dr = SqlHelper.ExecuteReader("Select LanguageId, UniqueId,[value] from cmsLanguageText order by UniqueId");

                while (dr.Read())
                {
                    int LanguageId = dr.GetInt("LanguageId");
                    Guid UniqueId = dr.GetGuid("UniqueId");
                    string text = dr.GetString("value");
                    updateCache(LanguageId, UniqueId, text);
                }
                isInitialized = true;
                dr.Close();
            }
        }

        private static void updateCache(int LanguageId, Guid key, string text)
        {
            // test if item already exist in items and update internal data or insert new internal data
            if (_items.ContainsKey(key))
            {
                System.Collections.Hashtable languagevalues = (System.Collections.Hashtable)_items[key];
                // check if the current language key is used
                if (languagevalues.ContainsKey(LanguageId))
                {
                    languagevalues[LanguageId] = text;
                }
                else
                {
                    languagevalues.Add(LanguageId, text);
                }
            }
            else
            {
                // insert 
                Hashtable languagevalues = Hashtable.Synchronized(new Hashtable());
                languagevalues.Add(LanguageId, text);
                _items.Add(key, languagevalues);
            }
        }


        /// <summary>
        /// Retrieves the value of a languagetranslated item given the key
        /// </summary>
        /// <param name="key">Unique identifier</param>
        /// <param name="LanguageId">Umbraco languageid</param>
        /// <returns>The language translated text</returns>
        public static string Text(Guid key, int LanguageId)
        {
            if (hasText(key, LanguageId))
                return ((System.Collections.Hashtable)_items[key])[LanguageId].ToString();
            else
                throw new ArgumentException("Key being requested does not exist");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key">Unique identifier</param>
        /// <param name="LanguageId">Umbraco language id</param>
        /// <returns>returns True if there is a value associated to the unique identifier with the specified language</returns>
        public static bool hasText(Guid key, int LanguageId)
        {
            ensureData();
            if (_items.ContainsKey(key))
            {
                System.Collections.Hashtable tmp = (System.Collections.Hashtable)_items[key];
                return tmp.ContainsKey(LanguageId);
            }
            return false;
        }
        /// <summary>
        /// Updates the value of the language translated item, throws an exeption if the
        /// key does not exist
        /// </summary>
        /// <param name="LanguageId">Umbraco language id</param>
        /// <param name="key">Unique identifier</param>
        /// <param name="value">The new dictionaryvalue</param>

        public static void setText(int LanguageId, Guid key, string value)
        {
            ensureData();
            if (!hasText(key, LanguageId)) throw new ArgumentException("Key does not exist");

            updateCache(LanguageId, key, value);
            SqlHelper.ExecuteNonQuery("Update cmsLanguageText set [value] = @value where LanguageId = @languageId And UniqueId = @key",
                SqlHelper.CreateParameter("@value", value),
                SqlHelper.CreateParameter("@languageId", LanguageId),
                SqlHelper.CreateParameter("@key", key));
        }

        /// <summary>
        /// Adds a new languagetranslated item to the collection
        /// 
        /// </summary>
        /// <param name="LanguageId">Umbraco languageid</param>
        /// <param name="key">Unique identifier</param>
        /// <param name="value"></param>
        public static void addText(int LanguageId, Guid key, string value)
        {
            ensureData();
            if (hasText(key, LanguageId)) throw new ArgumentException("Key being add'ed already exists");
            updateCache(LanguageId, key, value);
            SqlHelper.ExecuteNonQuery("Insert Into cmsLanguageText (languageId,UniqueId,[value]) values (@languageId, @key, @value)",
                SqlHelper.CreateParameter("@languageId", LanguageId),
                SqlHelper.CreateParameter("@key", key),
                SqlHelper.CreateParameter("@value", value));
        }
        /// <summary>
        /// Removes all languagetranslated texts associated to the unique identifier.
        /// </summary>
        /// <param name="key">Unique identifier</param>
        public static void removeText(Guid key)
        {
            // remove from cache
            _items.Remove(key);
            // remove from database
            SqlHelper.ExecuteNonQuery("Delete from cmsLanguageText where UniqueId =  @key",
                SqlHelper.CreateParameter("@key", key));
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.