-
Notifications
You must be signed in to change notification settings - Fork 1
/
Definitions.cs
1140 lines (971 loc) · 44.1 KB
/
Definitions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#region Related components
using System;
using System.Data;
using System.Linq;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Reflection;
using System.Dynamic;
using System.Diagnostics;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using MongoDB.Bson.Serialization.Attributes;
using net.vieapps.Components.Utility;
using net.vieapps.Components.Caching;
#endregion
namespace net.vieapps.Components.Repository
{
/// <summary>
/// Presents a respository definition (means a module definition)
/// </summary>
[DebuggerDisplay("Name = {Type?.FullName}")]
public class RepositoryDefinition
{
public RepositoryDefinition()
: this(null) { }
public RepositoryDefinition(Type type)
=> this.Type = type;
#region Properties
/// <summary>
/// Gets the type of the class that responsibility to process data of the repository
/// </summary>
public Type Type { get; }
/// <summary>
/// Gets the name of the primary data source
/// </summary>
public string PrimaryDataSourceName { get; private set; }
/// <summary>
/// Gets the primary data-source
/// </summary>
public DataSource PrimaryDataSource => RepositoryMediator.GetDataSource(this.PrimaryDataSourceName);
/// <summary>
/// Gets the name of the secondary data source
/// </summary>
public string SecondaryDataSourceName { get; private set; }
/// <summary>
/// Gets the secondary data-source
/// </summary>
public DataSource SecondaryDataSource => RepositoryMediator.GetDataSource(this.SecondaryDataSourceName);
/// <summary>
/// Gets the names of the all data-sources that available for sync
/// </summary>
public string SyncDataSourceNames { get; private set; }
/// <summary>
/// Gets the secondary data-source
/// </summary>
public List<DataSource> SyncDataSources
=> string.IsNullOrWhiteSpace(this.SyncDataSourceNames)
? new List<DataSource>()
: this.SyncDataSourceNames.ToList()
.Distinct(StringComparer.OrdinalIgnoreCase)
.Select(name => RepositoryMediator.GetDataSource(name))
.Where(dataSource => dataSource != null)
.ToList();
/// <summary>
/// Gets the name of the data source for storing information of versioning contents
/// </summary>
public string VersionDataSourceName { get; private set; }
/// <summary>
/// Gets the data-source that use to store versioning contents
/// </summary>
public DataSource VersionDataSource => RepositoryMediator.GetDataSource(this.VersionDataSourceName);
/// <summary>
/// Gets the name of the data source for storing information of trash contents
/// </summary>
public string TrashDataSourceName { get; private set; }
/// <summary>
/// Gets the data-source that use to store trash contents
/// </summary>
public DataSource TrashDataSource => RepositoryMediator.GetDataSource(this.TrashDataSourceName);
/// <summary>
/// Gets that state that specified this repository is an alias of other repository
/// </summary>
public bool IsAlias { get; private set; } = false;
/// <summary>
/// Gets that state that specified data of this repository is sync automatically between data sources
/// </summary>
public bool AutoSync { get; private set; } = false;
/// <summary>
/// Gets the extra information of the repository definition
/// </summary>
public Dictionary<string, object> Extras { get; private set; } = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Gets the definitions of all entities
/// </summary>
public List<EntityDefinition> EntityDefinitions => RepositoryMediator.EntityDefinitions.Where(kvp => kvp.Value.RepositoryDefinitionType.Equals(this.Type)).Select(kvp => kvp.Value).ToList();
#endregion
#region Properties [Module Definition]
/// <summary>
/// Gets the name of the service that associates with the repository (when this object is defined as a module definition)
/// </summary>
public string ServiceName => this.Type?.GetCustomAttribute<RepositoryAttribute>(false)?.ServiceName;
/// <summary>
/// Gets the identity (when this object is defined as a module definition)
/// </summary>
public string ID => this.Type?.GetCustomAttribute<RepositoryAttribute>(false)?.ID;
/// <summary>
/// Gets the title (when this object is defined as a module definition)
/// </summary>
public string Title => this.Type?.GetCustomAttribute<RepositoryAttribute>(false)?.Title;
/// <summary>
/// Gets the description (when this object is defined as a module definition)
/// </summary>
public string Description => this.Type?.GetCustomAttribute<RepositoryAttribute>(false)?.Description;
/// <summary>
/// Gets or Sets the name of the icon for working with user interfaces (when this object is defined as a module definition)
/// </summary>
public string Icon => this.Type?.GetCustomAttribute<RepositoryAttribute>(false)?.Icon;
/// <summary>
/// Gets or Sets the name of the directory that contains all files for working with user interfaces (when this object is defined as a module definition - will be placed in directory named '/themes/modules/', the value of 'ServiceName' will be used if no value was provided)
/// </summary>
public string Directory => this.Type?.GetCustomAttribute<RepositoryAttribute>(false)?.Directory;
/// <summary>
/// Gets the name of the SQL table for storing extended properties, default is 'T_Data_Extended_Properties' (when this object is defined as a module definition)
/// </summary>
public string ExtendedPropertiesTableName
{
get
{
var tableName = this.Type?.GetCustomAttribute<RepositoryAttribute>(false)?.ExtendedPropertiesTableName;
return string.IsNullOrWhiteSpace(tableName) ? "T_Data_Extended_Properties" : tableName.Trim();
}
}
/// <summary>
/// Gets the collection of business repositories (means business modules at run-time)
/// </summary>
public ConcurrentDictionary<string, IBusinessRepository> BusinessRepositories { get; } = new ConcurrentDictionary<string, IBusinessRepository>(StringComparer.OrdinalIgnoreCase);
#endregion
#region Register & Update settings
internal static void Register(Type type, Action<string, Exception> tracker = null)
{
// check
if (type == null || RepositoryMediator.RepositoryDefinitions.ContainsKey(type) || type.GetCustomAttribute<RepositoryAttribute>(false) == null)
return;
// initialize & register
var definition = new RepositoryDefinition(type);
if (RepositoryMediator.RepositoryDefinitions.TryAdd(type, definition))
{
tracker?.Invoke($"The repository definition was registered [{definition.Type.GetTypeName()}{(string.IsNullOrWhiteSpace(definition.Title) ? "" : $" => {definition.Title}")}]", null);
if (tracker == null && RepositoryMediator.IsDebugEnabled)
RepositoryMediator.WriteLogs($"The repository definition was registered [{definition.Type.GetTypeName()}{(string.IsNullOrWhiteSpace(definition.Title) ? "" : $" => {definition.Title}")}]");
}
}
internal static void Update(JObject settings, Action<string, Exception> tracker = null)
{
// check
if (settings == null)
throw new ArgumentNullException("settings");
else if (settings["type"] == null)
throw new ArgumentNullException("type", "[type] attribute of settings");
// prepare
Type aliasOf = null;
var isAlias = settings["isAlias"] != null && "true".IsEquals((settings["isAlias"] as JValue).Value as string);
if (isAlias)
{
var targetType = settings["target"] != null
? (settings["target"] as JValue).Value as string
: null;
if (string.IsNullOrWhiteSpace(targetType))
isAlias = false;
else
{
aliasOf = Enyim.Caching.AssemblyLoader.GetType(targetType);
if (aliasOf == null)
isAlias = false;
}
}
var typeStr = (settings["type"] as JValue).Value as string;
var type = Enyim.Caching.AssemblyLoader.GetType(typeStr);
if (type == null)
{
tracker?.Invoke($"The type of repository definition is not found [{typeStr}]", null);
if (tracker == null && RepositoryMediator.IsDebugEnabled)
RepositoryMediator.WriteLogs($"The type of repository definition is not found [{typeStr}]");
return;
}
// clone definition for new alias
if (isAlias)
{
if (!RepositoryMediator.RepositoryDefinitions.TryGetValue(aliasOf, out var targetDef))
throw new InformationInvalidException($"The target type named '{aliasOf.GetTypeName()}' is not available");
RepositoryMediator.RepositoryDefinitions.Add(type, RepositoryMediator.RepositoryDefinitions[aliasOf].Clone(cloneDef =>
{
cloneDef.IsAlias = true;
cloneDef.Extras = targetDef.Extras.Clone();
}));
}
// get definition
var definition = RepositoryMediator.GetRepositoryDefinition(type);
if (definition == null)
{
tracker?.Invoke($"The repository definition was not registered [{type}]", null);
if (tracker == null && RepositoryMediator.IsDebugEnabled)
RepositoryMediator.WriteLogs($"The repository definition was not registered [{type}]");
return;
}
// update
var data = settings["primaryDataSource"] != null
? (settings["primaryDataSource"] as JValue).Value as string
: null;
if (string.IsNullOrEmpty(data))
throw new ArgumentNullException("primaryDataSource", "[primaryDataSource] attribute of settings");
else if (!RepositoryMediator.DataSources.ContainsKey(data))
throw new InformationInvalidException($"The data source named '{data}' is not available");
definition.PrimaryDataSourceName = data;
data = settings["secondaryDataSource"] != null
? (settings["secondaryDataSource"] as JValue).Value as string
: null;
definition.SecondaryDataSourceName = !string.IsNullOrEmpty(data) && RepositoryMediator.DataSources.ContainsKey(data)
? data
: null;
definition.SyncDataSourceNames = settings["syncDataSources"] != null
? (settings["syncDataSources"] as JValue).Value as string
: null;
data = settings["versionDataSource"] != null
? (settings["versionDataSource"] as JValue).Value as string
: null;
definition.VersionDataSourceName = !string.IsNullOrEmpty(data) && RepositoryMediator.DataSources.ContainsKey(data)
? data
: null;
data = settings["trashDataSource"] != null
? (settings["trashDataSource"] as JValue).Value as string
: null;
definition.TrashDataSourceName = !string.IsNullOrEmpty(data) && RepositoryMediator.DataSources.ContainsKey(data)
? data
: null;
definition.AutoSync = "true".IsEquals(settings["autoSync"] != null ? (settings["autoSync"] as JValue).Value as string : "false");
tracker?.Invoke(
$"Settings of a repository was updated [{definition.Type.GetTypeName()}{(string.IsNullOrWhiteSpace(definition.Title) ? "" : $" => {definition.Title}")}]" + "\r\n" +
$"- Primary data source: {(definition.PrimaryDataSource != null ? $"{definition.PrimaryDataSource.Name} ({definition.PrimaryDataSource.Mode})" : "None")}" + "\r\n" +
$"- Secondary data source: {(definition.SecondaryDataSource != null ? $"{definition.SecondaryDataSource.Name} ({definition.SecondaryDataSource.Mode})" : "None")}" + "\r\n" +
$"- Sync data sources: {(definition.SyncDataSources.Count > 0 ? definition.SyncDataSources.Select(dataSource => $"{dataSource.Name} ({dataSource.Mode})").ToString(", ") : "None")}" + "\r\n" +
$"- Version data source: {definition.VersionDataSource?.Name ?? "(None)"}" + "\r\n" +
$"- Trash data source: {definition.TrashDataSource?.Name ?? "(None)"}" + "\r\n" +
$"- Auto sync: {definition.AutoSync}"
, null);
}
#endregion
#region Register/Unregister business repositories
/// <summary>
/// Registers a business repository (means a business module at run-time)
/// </summary>
/// <param name="businessRepository"></param>
public bool Register(IBusinessRepository businessRepository)
{
if (businessRepository != null && !string.IsNullOrWhiteSpace(businessRepository.ID))
{
var existed = this.BusinessRepositories.ContainsKey(businessRepository.ID);
this.BusinessRepositories[businessRepository.ID] = businessRepository;
if (!existed && RepositoryMediator.IsTraceEnabled)
RepositoryMediator.WriteLogs($"A business repository (a run-time business module) was registered\r\n{businessRepository.ToJson()}");
return true;
}
return false;
}
/// <summary>
/// Unregisters a business repository (means a business module at run-time)
/// </summary>
/// <param name="businessRepositoryID"></param>
public bool Unregister(string businessRepositoryID)
=> !string.IsNullOrWhiteSpace(businessRepositoryID) && this.BusinessRepositories.Remove(businessRepositoryID);
/// <summary>
/// Unregisters a business repository (means a business module at run-time)
/// </summary>
/// <param name="businessRepository"></param>
public bool Unregister(IBusinessRepository businessRepository)
{
var success = this.Unregister(businessRepository?.ID);
if (success && RepositoryMediator.IsTraceEnabled)
RepositoryMediator.WriteLogs($"A business repository (a run-time business module) was uregistered\r\n{businessRepository?.ToJson()}");
return success;
}
#endregion
}
// --------------------------------------------------------------------------------------------
/// <summary>
/// Presents a repository entity definition (means a content-type definition)
/// </summary>
[DebuggerDisplay("Name = {Type?.FullName}")]
public class EntityDefinition
{
public EntityDefinition()
: this(null) { }
public EntityDefinition(Type type)
=> this.Type = type;
#region Properties
/// <summary>
/// Gets the type of the class that responsibility to process data of the repository entity
/// </summary>
public Type Type { get; }
/// <summary>
/// Gets the name of the primary data source
/// </summary>
public string PrimaryDataSourceName { get; private set; }
/// <summary>
/// Gets the primary data-source
/// </summary>
public DataSource PrimaryDataSource => RepositoryMediator.GetDataSource(this.PrimaryDataSourceName);
/// <summary>
/// Gets the name of the secondary data source
/// </summary>
public string SecondaryDataSourceName { get; private set; }
/// <summary>
/// Gets the secondary data-source
/// </summary>
public DataSource SecondaryDataSource => RepositoryMediator.GetDataSource(this.SecondaryDataSourceName);
/// <summary>
/// Gets the names of the all data-sources that available for sync
/// </summary>
public string SyncDataSourceNames { get; private set; }
/// <summary>
/// Gets the other data sources that are available for synchronizing
/// </summary>
public List<DataSource> SyncDataSources => !string.IsNullOrWhiteSpace(this.SyncDataSourceNames)
? this.SyncDataSourceNames.ToList()
.Distinct(StringComparer.OrdinalIgnoreCase)
.Select(name => RepositoryMediator.GetDataSource(name))
.Where(dataSource => dataSource != null)
.ToList()
: null;
/// <summary>
/// Gets the name of the data source for storing information of versioning contents
/// </summary>
public string VersionDataSourceName { get; private set; }
/// <summary>
/// Gets the data-source that use to store versioning contents
/// </summary>
public DataSource VersionDataSource => RepositoryMediator.GetDataSource(this.VersionDataSourceName);
/// <summary>
/// Gets the name of the data source for storing information of trash contents
/// </summary>
public string TrashDataSourceName { get; private set; }
/// <summary>
/// Gets the data-source that use to store trash contents
/// </summary>
public DataSource TrashDataSource => RepositoryMediator.GetDataSource(this.TrashDataSourceName);
/// <summary>
/// Gets or Sets the name of the table in SQL database
/// </summary>
public string TableName => this.Type?.GetCustomAttribute<EntityAttribute>(false)?.TableName;
/// <summary>
/// Gets or Sets the name of the collection in NoSQL database
/// </summary>
public string CollectionName => this.Type?.GetCustomAttribute<EntityAttribute>(false)?.CollectionName;
/// <summary>
/// Gets or Sets the state that specifies this entity is able to search using full-text method
/// </summary>
public bool Searchable
{
get
{
var info = this.Type?.GetCustomAttribute<EntityAttribute>(false);
return info != null && info.Searchable;
}
}
/// <summary>
/// Gets the state to create new version when a repository entity object is updated
/// </summary>
public bool CreateNewVersionWhenUpdated
{
get
{
var info = this.Type?.GetCustomAttribute<EntityAttribute>(false);
return info != null && info.CreateNewVersionWhenUpdated;
}
}
/// <summary>
/// Gets that state that specified data of this repository entity is sync automatically between data sources
/// </summary>
public bool AutoSync { get; private set; } = false;
/// <summary>
/// Gets the extra information of the repository entity definition
/// </summary>
public Dictionary<string, object> Extras { get; private set; } = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Gets the collection of all available attributes (properties and fields)
/// </summary>
public List<AttributeInfo> Attributes { get; } = new List<AttributeInfo>();
/// <summary>
/// Gets the collection of all available attributes (properties and fields) for working with forms
/// </summary>
public List<AttributeInfo> FormAttributes { get; } = new List<AttributeInfo>();
/// <summary>
/// Gets the name of primary key property
/// </summary>
internal string PrimaryKey { get; set; }
/// <summary>
/// Gets the information of the primary key
/// </summary>
public AttributeInfo PrimaryKeyInfo => this.Attributes.FirstOrDefault(attribute => attribute.Name.Equals(this.PrimaryKey));
/// <summary>
/// Gets the collection of sortable properties
/// </summary>
public List<string> SortableAttributes { get; } = new List<string> { "ID" };
/// <summary>
/// Gets the caching object for processing with caching data of this entity
/// </summary>
public ICache Cache { get; private set; }
/// <summary>
/// Gets the type of the type that presents the repository definition of this repository entity definition
/// </summary>
public Type RepositoryDefinitionType { get; private set; }
/// <summary>
/// Gets the repository definition of this repository entity definition
/// </summary>
public RepositoryDefinition RepositoryDefinition => this.RepositoryDefinitionType?.GetRepositoryDefinition();
#endregion
#region Properties [Content-Type Definition]
/// <summary>
/// Gets the name of the service's object that associates with the entity (when this object is defined as a content-type definition)
/// </summary>
public string ObjectName => this.Type?.GetCustomAttribute<EntityAttribute>(false)?.ObjectName ?? this.Type?.GetTypeName(true);
/// <summary>
/// Gets the name prefix of the service's object that associates with the entity (when this object is defined as a content-type definition)
/// </summary>
public string ObjectNamePrefix => this.Type?.GetCustomAttribute<EntityAttribute>(false)?.ObjectNamePrefix;
/// <summary>
/// Gets the name suffix of the service's object that associates with the entity (when this object is defined as a content-type definition)
/// </summary>
public string ObjectNameSuffix => this.Type?.GetCustomAttribute<EntityAttribute>(false)?.ObjectNameSuffix;
/// <summary>
/// Gets the identity (when this object is defined as a content-type definition)
/// </summary>
public string ID => this.Type?.GetCustomAttribute<EntityAttribute>(false)?.ID;
/// <summary>
/// Gets the title (when this object is defined as a content-type definition)
/// </summary>
public string Title => this.Type?.GetCustomAttribute<EntityAttribute>(false)?.Title;
/// <summary>
/// Gets the description (when this object is defined as a content-type definition)
/// </summary>
public string Description => this.Type?.GetCustomAttribute<EntityAttribute>(false)?.Description;
/// <summary>
/// Gets or Sets the name of the icon for working with user interfaces (when this object is defined as a content-type definition)
/// </summary>
public string Icon => this.Type?.GetCustomAttribute<EntityAttribute>(false)?.Icon;
/// <summary>
/// Gets the state that allow to use multiple instances, default is false (when this object is defined as a content-type definition)
/// </summary>
public bool MultipleIntances
{
get
{
var info = this.Type?.GetCustomAttribute<EntityAttribute>(false);
return info != null && info.MultipleIntances;
}
}
/// <summary>
/// Gets or Sets the state that specifies this entity is able to index with global search module, default is true (when this object is defined as a content-type definition)
/// </summary>
public bool Indexable
{
get
{
var info = this.Type?.GetCustomAttribute<EntityAttribute>(false);
return info != null && info.Indexable;
}
}
/// <summary>
/// Gets the state that allow to extend this entity by extended properties, default is false (when this object is defined as a content-type definition)
/// </summary>
public bool Extendable
{
get
{
var info = this.Type?.GetCustomAttribute<EntityAttribute>(false);
return info != null && info.Extendable;
}
}
/// <summary>
/// Gets the state to specify that entity got some instances of portlet
/// </summary>
public bool Portlets
{
get
{
var info = this.Type?.GetCustomAttribute<EntityAttribute>(false);
return info != null &&!string.IsNullOrWhiteSpace(info.ID) && info.Portlets;
}
}
/// <summary>
/// Gets the type of parent entity definition (when this object is defined as a content-type definition)
/// </summary>
public Type ParentType => this.GetParentMappingAttribute()?.GetCustomAttribute<ParentMappingAttribute>()?.Type;
/// <summary>
/// Gets the collection of business repository entities (means business conten-types at run-time)
/// </summary>
public ConcurrentDictionary<string, IBusinessRepositoryEntity> BusinessRepositoryEntities { get; } = new ConcurrentDictionary<string, IBusinessRepositoryEntity>(StringComparer.OrdinalIgnoreCase);
#endregion
#region Register & Update settings
internal static void Register(Type type, Action<string, Exception> tracker = null)
{
// check
if (type == null || RepositoryMediator.EntityDefinitions.ContainsKey(type) || type.GetCustomAttribute<EntityAttribute>(false) == null)
return;
// initialize & verify the name of table/collection
var definition = new EntityDefinition(type);
if (string.IsNullOrWhiteSpace(definition.TableName) && string.IsNullOrWhiteSpace(definition.CollectionName))
throw new InformationRequiredException($"The type [{type}] must have name of SQL table or NoSQL collection");
// verify the mappings
var properties = type.GetPublicAttributes();
foreach (var attribute in properties.Where(attribute => attribute.IsMappings()))
{
if (!attribute.IsGenericListOrHashSet() || !attribute.GetGenericTypeArguments().First().Equals(typeof(string)))
throw new InformationInvalidException($"The attribute [{attribute.Name}] must be list or hash-set of string for using as mapping");
}
var parentMappings = properties.Where(attribute => attribute.IsParentMapping()).ToList();
if (parentMappings.Count > 0)
{
if (parentMappings.Count > 1)
throw new InformationInvalidException($"The type [{type}] got multiple mappings with parent entity definition");
else if (parentMappings.First().Type == null)
throw new InformationRequiredException($"The attribute [{parentMappings.First().Name}] must have the type of parent entity definition");
}
else if (properties.Any(attribute => attribute.IsMultipleParentMappings()))
throw new InformationInvalidException($"The type [{type}] got multiple parent mappings but got no information of the parent entity definition");
// verify the alias
var aliasProperties = properties.Where(attribute => attribute.IsAlias()).ToList();
if (aliasProperties.Count > 0)
{
if (aliasProperties.Count > 1)
throw new InformationInvalidException($"The type [{type}] got multiple alias properties");
else if (!typeof(IAliasEntity).IsAssignableFrom(type))
throw new InformationRequiredException($"The type [{type}] got alias property but not implement the 'IAliasEntity' interface");
var aliasInfo = aliasProperties.First().GetCustomAttribute<AliasAttribute>();
if (!string.IsNullOrWhiteSpace(aliasInfo.Properties))
{
var aliasProps = aliasInfo.Properties.ToHashSet(",", true);
var missingProps = aliasProps.Except(properties.Where(attribute => aliasProps.Contains(attribute.Name)).Select(attribute => attribute.Name), StringComparer.OrdinalIgnoreCase).ToList();
if (missingProps.Count > 0)
throw new InformationInvalidException($"The properties to make the alias combination of the type [{type}] are invalid [missing: {missingProps.Join(", ")}]");
}
}
// public properties
var numberOfPrimaryKeys = 0;
foreach (var property in properties)
{
// by-pass if ignore and not defined as a form control
if (property.IsIgnored() && !property.IsFormControl())
continue;
// create
var attribute = new AttributeInfo(property);
// primary key
if (!property.IsIgnored() && attribute.GetCustomAttribute<PrimaryKeyAttribute>() is PrimaryKeyAttribute primaryKeyInfo)
{
attribute.Column = primaryKeyInfo.Column;
attribute.NotNull = true;
attribute.NotEmpty = true;
if (primaryKeyInfo.MaxLength > 0)
attribute.MaxLength = primaryKeyInfo.MaxLength;
definition.PrimaryKey = attribute.Name;
numberOfPrimaryKeys += 1;
}
// property
if (attribute.GetCustomAttribute<PropertyAttribute>() is PropertyAttribute propertyInfo)
{
attribute.Column = propertyInfo.Column;
attribute.NotNull = propertyInfo.NotNull;
if (attribute.IsStringType())
{
if (propertyInfo.NotEmpty)
attribute.NotEmpty = true;
if (propertyInfo.IsCLOB)
attribute.IsCLOB = true;
if (propertyInfo.MinLength > 0)
attribute.MinLength = propertyInfo.MinLength;
if (!propertyInfo.IsCLOB)
attribute.MaxLength = propertyInfo.MaxLength > 0 && propertyInfo.MaxLength < 4000
? propertyInfo.MaxLength
: attribute.IsParentMapping() || attribute.Name.EndsWith("ID") ? 32 : 4000;
}
else
{
if (!string.IsNullOrWhiteSpace(propertyInfo.MinValue))
attribute.MinValue = propertyInfo.MinValue;
if (!string.IsNullOrWhiteSpace(propertyInfo.MaxValue))
attribute.MaxValue = propertyInfo.MaxValue;
}
}
// update definitions
definition.FormAttributes.Add(attribute);
if (!property.IsIgnored())
{
definition.Attributes.Add(attribute);
if (attribute.IsSortable())
definition.SortableAttributes.Add(attribute.Name);
}
};
// check primary key
if (numberOfPrimaryKeys < 1)
throw new InformationRequiredException($"The type [{type}] got no primary-key");
else if (numberOfPrimaryKeys > 1)
throw new InformationInvalidException($"The type [{type}] got multiple primary-keys");
// private attributes
type.GetPrivateAttributes().Where(field => !field.IsIgnored()).ForEach(field =>
{
// create
var attribute = new AttributeInfo(field);
// update info
if (attribute.GetCustomAttribute<FieldAttribute>() is FieldAttribute fieldInfo)
{
attribute.Column = fieldInfo.Column;
attribute.NotNull = fieldInfo.NotNull;
if (attribute.IsStringType())
{
if (fieldInfo.NotEmpty)
attribute.NotEmpty = true;
if (fieldInfo.IsCLOB)
attribute.IsCLOB = true;
if (fieldInfo.MinLength > 0)
attribute.MinLength = fieldInfo.MinLength;
if (!fieldInfo.IsCLOB)
attribute.MaxLength = fieldInfo.MaxLength > 0 && fieldInfo.MaxLength < 4000
? fieldInfo.MaxLength
: attribute.IsParentMapping() || attribute.Name.EndsWith("ID") ? 32 : 4000;
}
else
{
if (!string.IsNullOrWhiteSpace(fieldInfo.MinValue))
attribute.MinValue = fieldInfo.MinValue;
if (!string.IsNullOrWhiteSpace(fieldInfo.MaxValue))
attribute.MaxValue = fieldInfo.MaxValue;
}
// update definitions
definition.Attributes.Add(attribute);
}
});
// cache
var info = type.GetCustomAttribute<EntityAttribute>(false);
if (info.CacheClass != null && !string.IsNullOrWhiteSpace(info.CacheName))
{
var cache = info.CacheClass.GetStaticObject(info.CacheName);
definition.Cache = cache != null && cache is ICache ? cache as ICache : null;
}
// type of repository definition
var rootType = typeof(object);
var baseType = typeof(RepositoryBase);
var parentType = type.BaseType;
var grandParentType = parentType.BaseType;
while (grandParentType.BaseType != null && grandParentType.BaseType != rootType && grandParentType.BaseType != baseType)
{
parentType = parentType.BaseType;
grandParentType = parentType.BaseType;
}
var repositoryDefinitionTypeName = parentType.GetTypeName();
definition.RepositoryDefinitionType = Type.GetType(repositoryDefinitionTypeName.Left(repositoryDefinitionTypeName.IndexOf("[")) + repositoryDefinitionTypeName.Substring(repositoryDefinitionTypeName.IndexOf("]") + 2));
// update into collection
if (RepositoryMediator.EntityDefinitions.TryAdd(type, definition))
{
var log = $"The repository entity definition was registered [{definition.Type.GetTypeName()}{(string.IsNullOrWhiteSpace(definition.Title) ? "" : $" => {definition.Title}")}]" + "\r\n" +
$"- Attributes: {definition.Attributes.Select(attribute => $"{attribute.Name} ({attribute.Type.GetTypeName(true)})").Join(", ")}\r\n" +
$"- Parent: {definition.ParentType?.GetTypeName() ?? "None"}";
tracker?.Invoke(log, null);
if (tracker == null && RepositoryMediator.IsDebugEnabled)
RepositoryMediator.WriteLogs(log);
}
}
internal static void Update(JObject settings, Action<string, Exception> tracker = null)
{
// check
if (settings == null)
throw new ArgumentNullException("settings");
else if (settings["type"] == null)
throw new ArgumentNullException("type", "[type] attribute of settings");
// prepare
var definition = RepositoryMediator.GetEntityDefinition(Type.GetType((settings["type"] as JValue).Value as string));
if (definition == null)
return;
// update
var data = settings["primaryDataSource"] != null
? (settings["primaryDataSource"] as JValue).Value as string
: null;
definition.PrimaryDataSourceName = !string.IsNullOrEmpty(data) && RepositoryMediator.DataSources.ContainsKey(data)
? data
: null;
data = settings["secondaryDataSource"] != null
? (settings["secondaryDataSource"] as JValue).Value as string
: null;
definition.SecondaryDataSourceName = !string.IsNullOrEmpty(data) && RepositoryMediator.DataSources.ContainsKey(data)
? data
: null;
definition.SyncDataSourceNames = settings["syncDataSources"] != null
? (settings["syncDataSources"] as JValue).Value as string
: null;
data = settings["versionDataSource"] != null
? (settings["versionDataSource"] as JValue).Value as string
: null;
definition.VersionDataSourceName = !string.IsNullOrEmpty(data) && RepositoryMediator.DataSources.ContainsKey(data)
? data
: null;
data = settings["trashDataSource"] != null
? (settings["trashDataSource"] as JValue).Value as string
: null;
definition.TrashDataSourceName = !string.IsNullOrEmpty(data) && RepositoryMediator.DataSources.ContainsKey(data)
? data
: null;
// individual caching storage
if (settings["cacheRegion"] != null)
{
var cacheRegion = (settings["cacheRegion"] as JValue).Value as string;
var cacheExpirationTime = 30;
if (settings["cacheExpirationTime"] != null)
try
{
cacheExpirationTime = Convert.ToInt32((settings["cacheExpirationTime"] as JValue).Value);
if (cacheExpirationTime < 0)
cacheExpirationTime = 30;
}
catch { }
var cacheActiveSynchronize = settings["cacheActiveSynchronize"] != null && ((settings["cacheActiveSynchronize"] as JValue).Value as string).IsEquals("true");
var cacheProvider = settings["cacheProvider"] != null
? (settings["cacheProvider"] as JValue).Value as string
: null;
definition.Cache = new Cache(cacheRegion, cacheExpirationTime, cacheActiveSynchronize, cacheProvider);
}
definition.AutoSync = settings["autoSync"] != null
? "true".IsEquals((settings["autoSync"] as JValue).Value as string)
: definition.RepositoryDefinition.AutoSync;
tracker?.Invoke(
$"Settings of a repository entity was updated [{definition.Type.GetTypeName()}{(string.IsNullOrWhiteSpace(definition.Title) ? "" : $" => {definition.Title}")}]" + "\r\n" +
$"- Primary data source: {(definition.PrimaryDataSource != null ? $"{definition.PrimaryDataSource.Name} ({definition.PrimaryDataSource.Mode})" : "None")}" + "\r\n" +
$"- Secondary data source: {(definition.SecondaryDataSource != null ? $"{definition.SecondaryDataSource.Name} ({definition.SecondaryDataSource.Mode})" : "None")}" + "\r\n" +
$"- Sync data sources: {(definition.SyncDataSources != null && definition.SyncDataSources.Count > 0 ? definition.SyncDataSources.Select(dataSource => $"{dataSource.Name} ({dataSource.Mode})").ToString(", ") : "None")}" + "\r\n" +
$"- Version data source: {definition.VersionDataSource?.Name ?? "(None)"}" + "\r\n" +
$"- Trash data source: {definition.TrashDataSource?.Name ?? "(None)"}" + "\r\n" +
$"- Auto sync: {definition.AutoSync}"
, null);
}
/// <summary>
/// Sets the cache storage of a repository entity definition
/// </summary>
/// <param name="type">The type that presents information of a repository entity definition</param>
/// <param name="cache">The cache storage</param>
public void SetCacheStorage(Type type, Cache cache)
{
if (type != null && RepositoryMediator.EntityDefinitions.ContainsKey(type))
RepositoryMediator.EntityDefinitions[type].Cache = cache;
}
#endregion
#region Register/Unregister business repository entities
/// <summary>
/// Registers a business repository entity (means a business content-type at run-time)
/// </summary>
/// <param name="businessRepositoryEntity"></param>
public bool Register(IBusinessRepositoryEntity businessRepositoryEntity)
{
if (businessRepositoryEntity != null && !string.IsNullOrWhiteSpace(businessRepositoryEntity.ID))
{
var existed = this.BusinessRepositoryEntities.ContainsKey(businessRepositoryEntity.ID);
this.BusinessRepositoryEntities[businessRepositoryEntity.ID] = businessRepositoryEntity;
if (!existed && RepositoryMediator.IsTraceEnabled)
RepositoryMediator.WriteLogs($"A business repository entity (a run-time business content-type) was registered\r\n{businessRepositoryEntity.ToJson()}");
return true;
}
return false;
}
/// <summary>
/// Unregisters a business repository entity (means a business content-type at run-time)
/// </summary>
/// <param name="businessRepositoryEntityID"></param>
public bool Unregister(string businessRepositoryEntityID)
=> !string.IsNullOrWhiteSpace(businessRepositoryEntityID) && this.BusinessRepositoryEntities.Remove(businessRepositoryEntityID);
/// <summary>
/// Unregisters a business repository entity (means a business content-type at run-time)
/// </summary>
/// <param name="businessRepositoryEntity"></param>
public bool Unregister(IBusinessRepositoryEntity businessRepositoryEntity)
{
var success = this.Unregister(businessRepositoryEntity?.ID);
if (success && RepositoryMediator.IsTraceEnabled)
RepositoryMediator.WriteLogs($"A business repository entity (a run-time business content-type) was uregistered\r\n{businessRepositoryEntity?.ToJson()}");
return success;
}
#endregion
}
// --------------------------------------------------------------------------------------------
/// <summary>
/// Presents information of an attribute of a repository entity
/// </summary>
[DebuggerDisplay("Name = {Name}")]
public class AttributeInfo : ObjectService.AttributeInfo
{
public AttributeInfo() : this(null) { }
public AttributeInfo(ObjectService.AttributeInfo derived) : base(derived?.Info) { }
public string Column { get; internal set; }
public bool NotNull { get; internal set; } = false;
public bool? NotEmpty { get; internal set; }
public bool? IsCLOB { get; internal set; }
public int? MinLength { get; internal set; }
public int? MaxLength { get; internal set; }
public string MinValue { get; internal set; }
public string MaxValue { get; internal set; }
}
// --------------------------------------------------------------------------------------------
/// <summary>
/// Presents a definition of an extended property of an entity in a respository
/// </summary>
[DebuggerDisplay("Name = {Name}, Mode = {Mode}")]
public sealed class ExtendedPropertyDefinition
{
public ExtendedPropertyDefinition() { }
public ExtendedPropertyDefinition(JObject data = null)
=> this.CopyFrom(data ?? new JObject());
public ExtendedPropertyDefinition(ExpandoObject data = null)
=> this.CopyFrom(data ?? new ExpandoObject());
#region Properties
/// <summary>
/// Gets or Sets the name
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or Sets the mode
/// </summary>
[JsonConverter(typeof(StringEnumConverter)), BsonRepresentation(MongoDB.Bson.BsonType.String)]
public ExtendedPropertyMode Mode { get; set; } = ExtendedPropertyMode.SmallText;
/// <summary>
/// Gets or Sets the name of column for storing data (when repository mode is SQL)
/// </summary>
public string Column { get; set; }
/// <summary>
/// Gets or Sets the default value
/// </summary>
public string DefaultValue { get; set; }
/// <summary>
/// Gets or Sets the formula for computing default value
/// </summary>
public string DefaultValueFormula { get; set; }
#endregion
#region Properties [Helper]
/// <summary>
/// Gets the runtime-type of this property
/// </summary>
[JsonIgnore, BsonIgnore, XmlIgnore]
public Type Type
{
get
{
switch (this.Mode)
{
case ExtendedPropertyMode.YesNo:
return typeof(bool);
case ExtendedPropertyMode.IntegralNumber:
return typeof(long);
case ExtendedPropertyMode.FloatingPointNumber:
return typeof(decimal);