SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
Locales.cs
Go to the documentation of this file.
1 ï»¿// SmartAPI - .Net programmatic access to RedDot servers
2 //
3 // Copyright (C) 2013 erminas GbR
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free Software Foundation,
7 // either version 3 of the License, or (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 // See the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program.
14 // If not, see <http://www.gnu.org/licenses/>.
15 
16 using System;
17 using System.Collections.Generic;
18 using System.Linq;
19 using System.Xml;
20 using erminas.SmartAPI.Utils;
21 using erminas.SmartAPI.Utils.CachedCollections;
22 
23 namespace erminas.SmartAPI.CMS
24 {
25  public interface ISystemLocale : ISessionObject
26  {
27  string Country { get; }
28 
32  IIndexedCachedList<int, IDateTimeFormat> DateTimeFormats { get; }
33 
34  bool Equals(ISystemLocale other);
35  bool IsStandardLanguage { get; }
36  int LCID { get; }
37  string Language { get; }
38  string LanguageAbbreviation { get; }
39  string RFCLanguageId { get; }
40  }
41 
42  public interface IDialogLocale : ISessionObject
43  {
44  string Country { get; }
45 
49  IIndexedCachedList<int, IDateTimeFormat> DateTimeFormats { get; }
50 
51  bool Equals(IDialogLocale other);
52 
53  bool IsStandardLanguage { get; }
54  int LCID { get; }
55  string Language { get; }
56  string LanguageAbbreviation { get; }
57  string RFCLanguageId { get; }
58  }
59 
60  internal class SystemLocale : Locale, ISystemLocale
61  {
62  internal SystemLocale(ISession session, XmlElement xmlElement) : base(session, xmlElement)
63  {
64  }
65 
66  public bool Equals(ISystemLocale other)
67  {
68  return other.LCID == LCID;
69  }
70  }
71 
72  internal class DialogLocale : Locale, IDialogLocale
73  {
74  internal DialogLocale(ISession session, XmlElement xmlElement) : base(session, xmlElement)
75  {
76  }
77 
78  public bool Equals(IDialogLocale other)
79  {
80  return other.LCID == LCID;
81  }
82  }
83 
84  internal abstract class Locale
85  {
86  private readonly ISession _session;
87 
88  protected Locale(ISession session, XmlElement xmlElement)
89  {
90  _session = session;
91  LanguageAbbreviation = xmlElement.GetAttributeValue("id");
92  Country = xmlElement.GetAttributeValue("country");
93  Language = xmlElement.GetAttributeValue("language");
94  IsStandardLanguage = xmlElement.GetBoolAttributeValue("standardlanguage").GetValueOrDefault();
95  LCID = xmlElement.GetIntAttributeValue("lcid").GetValueOrDefault();
96  RFCLanguageId = xmlElement.GetAttributeValue("rfclanguageid");
97  DateTimeFormats = new IndexedCachedList<int, IDateTimeFormat>(GetFormats, x => x.TypeId, Caching.Enabled);
98  }
99 
100  public string Country { get; private set; }
101 
105  public IIndexedCachedList<int, IDateTimeFormat> DateTimeFormats { get; private set; }
106 
107  public override bool Equals(object obj)
108  {
109  if (ReferenceEquals(null, obj))
110  {
111  return false;
112  }
113  if (ReferenceEquals(this, obj))
114  {
115  return true;
116  }
117  if (obj.GetType() != GetType())
118  {
119  return false;
120  }
121  return Equals((Locale) obj);
122  }
123 
124  public bool Equals(Locale other)
125  {
126  return Equals(LCID, other.LCID);
127  }
128 
129  public override int GetHashCode()
130  {
131  return LanguageAbbreviation.GetHashCode();
132  }
133 
134  public bool IsStandardLanguage { get; private set; }
135  public int LCID { get; private set; }
136  public string Language { get; private set; }
137  public string LanguageAbbreviation { get; private set; }
138  public string RFCLanguageId { get; private set; }
139 
140  public ISession Session
141  {
142  get { return _session; }
143  }
144 
145  public override string ToString()
146  {
147  return Country + " (" + Language + ")";
148  }
149 
150  private List<IDateTimeFormat> GetFormats()
151  {
152  List<IDateTimeFormat> dateEntries =
153  (from XmlElement curEntry in GetFormatsOfSingleType(DateTimeFormatTypes.Date)
154  select
155  (IDateTimeFormat)
156  new DateTimeFormat(DateTimeFormatTypes.Date | DateTimeFormatTypes.DateTime, curEntry)).ToList();
157 
158  IEnumerable<DateTimeFormat> timeEntries =
159  from XmlElement curEntry in GetFormatsOfSingleType(DateTimeFormatTypes.Time)
160  select new DateTimeFormat(DateTimeFormatTypes.Time, curEntry);
161 
162  IEnumerable<DateTimeFormat> dateTimeEntries =
163  from XmlElement curEntry in GetFormatsOfSingleType(DateTimeFormatTypes.DateTime)
164  let entry = new DateTimeFormat(DateTimeFormatTypes.DateTime, curEntry)
165  where dateEntries.All(x => x.TypeId != entry.TypeId)
166  select entry;
167 
168  return dateEntries.Union(timeEntries).Union(dateTimeEntries).ToList();
169  }
170 
171  private XmlNodeList GetFormatsOfSingleType(DateTimeFormatTypes types)
172  {
173  const string LOAD_TIME_FORMATS =
174  @"<TEMPLATE><ELEMENT action=""load"" ><{0}FORMATS action=""list"" lcid=""{1}""/></ELEMENT></TEMPLATE>";
175  string formatTypeString = types.ToString().ToUpper();
176  XmlDocument result = _session.ExecuteRQL(string.Format(LOAD_TIME_FORMATS, formatTypeString, LCID),
177  RQL.IODataFormat.SessionKeyAndLogonGuid);
178 
179  var timeformats = result.GetElementsByTagName(formatTypeString + "FORMATS")[0] as XmlElement;
180  if (timeformats == null)
181  {
182  var e = new Exception("could not load timeformats for lcid '" + LCID + "'");
183  e.Data.Add("result", result);
184  throw e;
185  }
186 
187  string answerElementsName = types == DateTimeFormatTypes.Time ? "TIMEFORMAT" : "DATEFORMAT";
188  return timeformats.GetElementsByTagName(answerElementsName);
189  }
190  }
191 }