PageRenderTime 21ms CodeModel.GetById 6ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/PigeonCms.Core/DAL/CulturesManager.cs

http://pigeoncms.googlecode.com/
C# | 247 lines | 217 code | 24 blank | 6 comment | 14 complexity | 8cd7c9ae7aa74ed2391a33f11772ad08 MD5 | raw file
  1using System;
  2using System.Data;
  3using System.Configuration;
  4using System.Web;
  5using System.Web.Security;
  6using System.Web.UI;
  7using System.Web.UI.WebControls;
  8using System.Web.UI.WebControls.WebParts;
  9using System.Web.UI.HtmlControls;
 10using System.ComponentModel;
 11using System.Collections.Generic;
 12using System.IO;
 13using System.Data.Common;
 14using PigeonCms;
 15using System.Diagnostics;
 16
 17namespace PigeonCms
 18{
 19    /// <summary>
 20    /// DAL for Culture obj (in table Cultures)
 21    /// </summary>
 22    public class CulturesManager: TableManagerWithOrdering<Culture, CulturesFilter, string>
 23    {
 24        [DebuggerStepThrough()]
 25        public CulturesManager()
 26        {
 27            this.TableName = "#__cultures";
 28            this.KeyFieldName = "CultureCode";
 29        }
 30
 31        public override Dictionary<string, string> GetList()
 32        {
 33            Dictionary<string, string> res = new Dictionary<string, string>();
 34            CulturesFilter filter = new CulturesFilter();
 35            List<Culture> list = GetByFilter(filter, "");
 36            foreach (Culture item in list)
 37            {
 38                res.Add(item.CultureCode.ToString(), item.DisplayName);
 39            }
 40            return res;
 41        }
 42
 43        public override List<Culture> GetByFilter(CulturesFilter filter, string sort)
 44        {
 45            DbProviderFactory myProv = Database.ProviderFactory;
 46            DbConnection myConn = myProv.CreateConnection();
 47            DbDataReader myRd = null;
 48            DbCommand myCmd = myConn.CreateCommand();
 49            string sSql;
 50            List<Culture> result = new List<Culture>();
 51
 52            try
 53            {
 54                myConn.ConnectionString = Database.ConnString;
 55                myConn.Open();
 56                myCmd.Connection = myConn;
 57
 58                sSql = "SELECT t.cultureCode "
 59                    + " FROM [#__cultures] t "
 60                    + " WHERE 1=1 ";
 61                if (!string.IsNullOrEmpty(filter.CultureCode))
 62                {
 63                    sSql += " AND t.CultureCode = @CultureCode ";
 64                    myCmd.Parameters.Add(Database.Parameter(myProv, "CultureCode", filter.CultureCode));
 65                }
 66                if (filter.Enabled != Utility.TristateBool.NotSet)
 67                {
 68                    sSql += " AND t.Enabled = @Enabled ";
 69                    myCmd.Parameters.Add(Database.Parameter(myProv, "Enabled", filter.Enabled));
 70                }
 71
 72                if (!string.IsNullOrEmpty(sort))
 73                {
 74                    sSql += " ORDER BY " + sort;
 75                }
 76                else
 77                {
 78                    sSql += " ORDER BY t.Ordering ";
 79                }
 80
 81                myCmd.CommandText = Database.ParseSql(sSql);
 82                myRd = myCmd.ExecuteReader();
 83                while (myRd.Read())
 84                {
 85                    Culture item = GetByKey(myRd["CultureCode"].ToString());
 86                    result.Add(item);
 87                }
 88                myRd.Close();
 89            }
 90            finally
 91            {
 92                myConn.Dispose();
 93            }
 94            return result;
 95        }
 96
 97        public override Culture GetByKey(string cultureCode)
 98        {
 99            DbProviderFactory myProv = Database.ProviderFactory;
100            DbConnection myConn = myProv.CreateConnection();
101            DbDataReader myRd = null;
102            DbCommand myCmd = myConn.CreateCommand();
103            string sSql;
104            Culture result = new Culture();
105
106            try
107            {
108                myConn.ConnectionString = Database.ConnString;
109                myConn.Open();
110                myCmd.Connection = myConn;
111
112                sSql = "SELECT CultureCode, DisplayName, Enabled, Ordering "
113                + " FROM [#__cultures] "
114                + " WHERE CultureCode = @CultureCode ";
115                myCmd.CommandText = Database.ParseSql(sSql);
116                myCmd.Parameters.Add(Database.Parameter(myProv, "CultureCode", cultureCode));
117                myRd = myCmd.ExecuteReader();
118                if (myRd.Read())
119                {
120                    if (!Convert.IsDBNull(myRd["CultureCode"]))
121                        result.CultureCode = myRd["CultureCode"].ToString();
122                    if (!Convert.IsDBNull(myRd["DisplayName"]))
123                        result.DisplayName = myRd["DisplayName"].ToString();
124                    if (!Convert.IsDBNull(myRd["Enabled"]))
125                        result.Enabled = (bool)myRd["Enabled"];
126                    if (!Convert.IsDBNull(myRd["Ordering"]))
127                        result.Ordering = (int)myRd["Ordering"];
128                }
129                myRd.Close();
130            }
131            finally
132            {
133                myConn.Dispose();
134            }
135            return result;
136        }
137
138        public override int Update(Culture theObj)
139        {
140            DbProviderFactory myProv = Database.ProviderFactory;
141            DbConnection myConn = myProv.CreateConnection();
142            DbCommand myCmd = myConn.CreateCommand();
143            string sSql;
144            int result = 0;
145
146            try
147            {
148                myConn.ConnectionString = Database.ConnString;
149                myConn.Open();
150                myCmd.Connection = myConn;
151
152                if (theObj.Ordering == 0)
153                {
154                    theObj.Ordering = this.GetNextOrdering();
155                }
156
157                sSql = "UPDATE [#__cultures] "
158                + " SET DisplayName=@DisplayName, Enabled=@Enabled, Ordering=@Ordering "
159                + " WHERE CultureCode = @CultureCode";
160                myCmd.CommandText = Database.ParseSql(sSql);
161                myCmd.Parameters.Add(Database.Parameter(myProv, "CultureCode", theObj.CultureCode));
162                myCmd.Parameters.Add(Database.Parameter(myProv, "DisplayName", theObj.DisplayName));
163                myCmd.Parameters.Add(Database.Parameter(myProv, "Enabled", theObj.Enabled));
164                myCmd.Parameters.Add(Database.Parameter(myProv, "Ordering", theObj.Ordering));
165
166                result = myCmd.ExecuteNonQuery();
167            }
168            catch (Exception e)
169            {
170                throw e;
171            }
172            finally
173            {
174                myConn.Dispose();
175            }
176            return result;
177        }
178
179        public override Culture Insert(Culture newObj)
180        {
181            DbProviderFactory myProv = Database.ProviderFactory;
182            DbConnection myConn = myProv.CreateConnection();
183            DbCommand myCmd = myConn.CreateCommand();
184            string sSql;
185            Culture result = new Culture();
186
187            try
188            {
189                myConn.ConnectionString = Database.ConnString;
190                myConn.Open();
191                myCmd.Connection = myConn;
192
193                result = newObj;
194                result.Ordering = base.GetNextOrdering();
195
196                sSql = "INSERT INTO [#__cultures](CultureCode, DisplayName, Enabled, Ordering) "
197                + " VALUES(@CultureCode, @DisplayName, @Enabled, @Ordering) ";
198                myCmd.CommandText = Database.ParseSql(sSql);
199                myCmd.Parameters.Add(Database.Parameter(myProv, "CultureCode", result.CultureCode));
200                myCmd.Parameters.Add(Database.Parameter(myProv, "DisplayName", result.DisplayName));
201                myCmd.Parameters.Add(Database.Parameter(myProv, "Enabled", result.Enabled));
202                myCmd.Parameters.Add(Database.Parameter(myProv, "Ordering", result.Ordering));
203
204                myCmd.ExecuteNonQuery();
205            }
206            catch (Exception e)
207            {
208                throw e;
209            }
210            finally
211            {
212                myConn.Dispose();
213            }
214            return result;
215        }
216
217        public void RefreshCultureList()
218        {
219            try
220            {
221                Dictionary<String, String> cultureList = new Dictionary<string, string>();
222
223                CulturesFilter filter = new CulturesFilter();
224                filter.Enabled = Utility.TristateBool.True;
225                List<PigeonCms.Culture> list = new CulturesManager().GetByFilter(filter, "");
226
227                foreach (PigeonCms.Culture item in list)
228                {
229                    cultureList.Add(item.CultureCode, item.DisplayName);
230                }
231                if (list.Count == 0)
232                {
233                    cultureList.Add("it-IT", "Italiano");
234                    //cultureList.Add("en-US", "English");
235                    //cultureList.Add("de-DE", "Deutsch");
236                    //cultureList.Add("es-ES", "Espa?ol");
237                }
238
239                HttpContext.Current.Application["CultureList"] = cultureList;
240            }
241            catch (Exception ex)
242            {
243                throw ex;
244            }
245        }
246    }
247}