Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Language.cs
1using System;
3using System.Threading.Tasks;
7
9{
13 [CollectionName("Languages")]
14 [Index("Code")]
15 [TypeName(TypeNameSerialization.None)]
16 public class Language
17 {
18 private readonly SortedDictionary<string, Namespace> namespacesByName = new SortedDictionary<string, Namespace>(StringComparer.CurrentCultureIgnoreCase);
19 private readonly object synchObject = new object();
20 private Guid objectId = Guid.Empty;
21 private string code = string.Empty;
22 private string name = string.Empty;
23 private byte[] flag = null;
24 private int flagWidth = 0;
25 private int flagHeight = 0;
26 private bool namespacesLoaded = false;
27
31 public Language()
32 {
33 }
34
38 [ObjectId]
39 public Guid ObjectId
40 {
41 get => this.objectId;
42 set => this.objectId = value;
43 }
44
48 public string Code
49 {
50 get => this.code;
51 set => this.code = value;
52 }
53
57 [DefaultValueStringEmpty]
58 public string Name
59 {
60 get => this.name;
61 set => this.name = value;
62 }
63
67 [DefaultValueNull]
68 public byte[] Flag
69 {
70 get => this.flag;
71 set => this.flag = value;
72 }
73
77 [DefaultValue(0)]
78 public int FlagWidth
79 {
80 get => this.flagWidth;
81 set => this.flagWidth = value;
82 }
83
87 [DefaultValue(0)]
88 public int FlagHeight
89 {
90 get => this.flagHeight;
91 set => this.flagHeight = value;
92 }
93
99 public async Task<Namespace> GetNamespaceAsync(string Name)
100 {
101 lock (this.synchObject)
102 {
103 if (this.namespacesByName.TryGetValue(Name, out Namespace Result))
104 return Result;
105 }
106
107 Namespace Namespace = await Database.FindFirstIgnoreRest<Namespace>(new FilterAnd(
108 new FilterFieldEqualTo("LanguageId", this.objectId), new FilterFieldEqualTo("Name", Name)));
109
110 if (!(Namespace is null))
111 {
112 lock (this.synchObject)
113 {
114 if (this.namespacesByName.TryGetValue(Name, out Namespace Result))
115 return Result;
116 else
117 this.namespacesByName[Namespace.Name] = Namespace;
118 }
119 }
120
121 return Namespace;
122 }
123
130 public async Task<Namespace> GetNamespaceAsync(string Name, bool CreateIfNotFound)
131 {
132 Namespace ns = await this.GetNamespaceAsync(Name);
133 if (ns is null && CreateIfNotFound)
134 ns = await this.CreateNamespaceAsync(Name);
135
136 return ns;
137 }
138
143 public async Task<Namespace[]> GetNamespacesAsync()
144 {
145 if (!this.namespacesLoaded)
146 {
147 IEnumerable<Namespace> Namespaces = await Database.Find<Namespace>(
148 new FilterFieldEqualTo("LanguageId", this.objectId));
149
150 lock (this.synchObject)
151 {
152 foreach (Namespace Namespace in Namespaces)
153 {
154 if (!this.namespacesByName.ContainsKey(Namespace.Name))
155 this.namespacesByName[Namespace.Name] = Namespace;
156 }
157 }
158
159 this.namespacesLoaded = true;
160 }
161
162 lock (this.synchObject)
163 {
164 Namespace[] Result = new Namespace[this.namespacesByName.Count];
165 this.namespacesByName.Values.CopyTo(Result, 0);
166 return Result;
167 }
168 }
169
175 public async Task<Namespace> CreateNamespaceAsync(string Name)
176 {
177 Namespace Result = await this.GetNamespaceAsync(Name);
178 if (!(Result is null))
179 return Result;
180 else
181 {
182 lock (this.synchObject)
183 {
184 if (this.namespacesByName.TryGetValue(Name, out Result))
185 return Result;
186
187 Result = new Namespace()
188 {
189 LanguageId = this.objectId,
190 Name = Name
191 };
192
193 this.namespacesByName[Name] = Result;
194 }
195
196 await Database.Insert(Result);
197
198 return Result;
199 }
200 }
201
209 public Task<string> GetStringAsync(Type Type, int Id, string Default)
210 {
211 return this.GetStringAsync(Type, Id.ToString(), Default);
212 }
213
221 public async Task<string> GetStringAsync(Type Type, string Id, string Default)
222 {
223 Namespace Namespace = await this.GetNamespaceAsync(Type.Namespace)
224 ?? await this.CreateNamespaceAsync(Type.Namespace);
225
226 return await Namespace.GetStringAsync(Id, Default);
227 }
228
230 public override string ToString()
231 {
232 if (string.IsNullOrEmpty(this.name))
233 return this.code;
234 else
235 return this.name + " (" + this.code + ")";
236 }
237 }
238}
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
Definition: Database.cs:238
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Definition: Database.cs:97
This filter selects objects that conform to all child-filters provided.
Definition: FilterAnd.cs:10
This filter selects objects that have a named field equal to a given value.
Contains information about a language.
Definition: Language.cs:17
Language()
Contains information about a language.
Definition: Language.cs:31
async Task< string > GetStringAsync(Type Type, string Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Definition: Language.cs:221
int FlagWidth
Width of flag.
Definition: Language.cs:79
int FlagHeight
Height of flag.
Definition: Language.cs:89
override string ToString()
Definition: Language.cs:230
async Task< Namespace > GetNamespaceAsync(string Name, bool CreateIfNotFound)
Gets the namespace object, given its name, if available.
Definition: Language.cs:130
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Definition: Language.cs:209
async Task< Namespace > GetNamespaceAsync(string Name)
Gets the namespace object, given its name, if available.
Definition: Language.cs:99
string Code
Language code.
Definition: Language.cs:49
async Task< Namespace > CreateNamespaceAsync(string Name)
Creates a new language namespace, or updates an existing language namespace, if one exist with the sa...
Definition: Language.cs:175
string Name
Language name.
Definition: Language.cs:59
byte[] Flag
Language flag.
Definition: Language.cs:69
async Task< Namespace[]> GetNamespacesAsync()
Gets available namespaces.
Definition: Language.cs:143
Contains information about a namespace in a language.
Definition: Namespace.cs:17
Task< LanguageString > GetStringAsync(int Id)
Gets the string object, given its ID, if available.
Definition: Namespace.cs:65
TypeNameSerialization
How the type name should be serialized.