-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathStarter.cs
351 lines (285 loc) · 16.2 KB
/
Starter.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
using System;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using Signum.Engine;
using Signum.Engine.Authorization;
using Signum.Engine.Basics;
using Signum.Engine.Chart;
using Signum.Engine.Dashboard;
using Signum.Engine.Disconnected;
using Signum.Engine.DynamicQuery;
using Signum.Engine.Mailing;
using Signum.Engine.Maps;
using Signum.Engine.Operations;
using Signum.Engine.UserQueries;
using Signum.Entities;
using Signum.Entities.Authorization;
using Signum.Entities.Basics;
using Signum.Entities.Chart;
using Signum.Entities.Dashboard;
using Signum.Entities.Disconnected;
using Signum.Entities.Mailing;
using Signum.Entities.UserQueries;
using Signum.Utilities;
using Signum.Utilities.ExpressionTrees;
using Southwind.Entities;
using Signum.Engine.Processes;
using Signum.Entities.Processes;
using Signum.Engine.Alerts;
using Signum.Engine.Notes;
using Signum.Engine.Cache;
using Signum.Engine.Profiler;
using Signum.Engine.Translation;
using Signum.Engine.Files;
using Signum.Entities.Alerts;
using Signum.Entities.Notes;
using Signum.Entities.UserAssets;
using Signum.Engine.UserAssets;
using Signum.Engine.Scheduler;
using Signum.Entities.Scheduler;
using Signum.Engine.SMS;
using Signum.Engine.ViewLog;
using System.Collections.Generic;
using Signum.Entities.ViewLog;
using Signum.Engine.Help;
using Signum.Engine.Word;
using Signum.Entities.Word;
using Signum.Engine.Migrations;
using Signum.Entities.DynamicQuery;
using System.Net.Mail;
using Signum.Engine.DiffLog;
using Signum.Entities.DiffLog;
using Signum.Engine.Map;
using Signum.Engine.Excel;
using Signum.Engine.Dynamic;
using Signum.Entities.Dynamic;
using Signum.Engine.Workflow;
using Signum.Engine.Toolbar;
using Signum.Engine.MachineLearning;
using Signum.Entities.MachineLearning;
using Signum.Entities.Files;
using Signum.Engine.MachineLearning.CNTK;
using Signum.Entities.Rest;
using Signum.Engine.Rest;
using Microsoft.Exchange.WebServices.Data;
namespace Southwind.Logic
{
//Starts-up the engine for Southwind Entities, used by Web and Load Application
public static partial class Starter
{
public static ResetLazy<ApplicationConfigurationEntity> Configuration = null!;
public static void Start(string connectionString, bool includeDynamic = true)
{
using (HeavyProfiler.Log("Start"))
using (var initial = HeavyProfiler.Log("Initial"))
{
StartParameters.IgnoredDatabaseMismatches = new List<Exception>();
StartParameters.IgnoredCodeErrors = new List<Exception>();
string? logDatabase = Connector.TryExtractDatabaseNameWithPostfix(ref connectionString, "_Log");
SchemaBuilder sb = new CustomSchemaBuilder { LogDatabaseName = logDatabase, Tracer = initial };
sb.Schema.Version = typeof(Starter).Assembly.GetName().Version!;
sb.Schema.ForceCultureInfo = CultureInfo.GetCultureInfo("en-US");
MixinDeclarations.Register<OperationLogEntity, DiffLogMixin>();
MixinDeclarations.Register<UserEntity, UserEmployeeMixin>();
OverrideAttributes(sb);
if (connectionString.Contains("Data Source"))
{
var sqlVersion = SqlServerVersionDetector.Detect(connectionString);
Connector.Default = new SqlConnector(connectionString, sb.Schema, sqlVersion!.Value);
}
else
{
var postgreeVersion = PostgresVersionDetector.Detect(connectionString);
Connector.Default = new PostgreSqlConnector(connectionString, sb.Schema, postgreeVersion);
}
CacheLogic.Start(sb);
DynamicLogicStarter.Start(sb);
if (includeDynamic)
{
DynamicLogic.CompileDynamicCode();
DynamicLogic.RegisterMixins();
DynamicLogic.BeforeSchema(sb);
}
// Framework modules
TypeLogic.Start(sb);
OperationLogic.Start(sb);
ExceptionLogic.Start(sb);
QueryLogic.Start(sb);
// Extensions modules
MigrationLogic.Start(sb);
CultureInfoLogic.Start(sb);
FilePathEmbeddedLogic.Start(sb);
EmailLogic.Start(sb, () => Configuration.Value.Email, (et, target) => Configuration.Value.EmailSender);
AuthLogic.Start(sb, "System", null);
AuthLogic.StartAllModules(sb);
ResetPasswordRequestLogic.Start(sb);
UserTicketLogic.Start(sb);
SessionLogLogic.Start(sb);
ProcessLogic.Start(sb);
PackageLogic.Start(sb, packages: true, packageOperations: true);
SchedulerLogic.Start(sb);
UserQueryLogic.Start(sb);
UserQueryLogic.RegisterUserTypeCondition(sb, SouthwindGroup.UserEntities);
UserQueryLogic.RegisterRoleTypeCondition(sb, SouthwindGroup.RoleEntities);
ChartLogic.Start(sb);
UserChartLogic.RegisterUserTypeCondition(sb, SouthwindGroup.UserEntities);
UserChartLogic.RegisterRoleTypeCondition(sb, SouthwindGroup.RoleEntities);
DashboardLogic.Start(sb);
DashboardLogic.RegisterUserTypeCondition(sb, SouthwindGroup.UserEntities);
DashboardLogic.RegisterRoleTypeCondition(sb, SouthwindGroup.RoleEntities);
ViewLogLogic.Start(sb, new HashSet<Type> { typeof(UserQueryEntity), typeof(UserChartEntity), typeof(DashboardEntity) });
DiffLogLogic.Start(sb, registerAll: true);
ExcelLogic.Start(sb, excelReport: true);
ToolbarLogic.Start(sb);
SMSLogic.Start(sb, null, () => Configuration.Value.Sms);
NoteLogic.Start(sb, typeof(UserEntity), /*Note*/typeof(OrderEntity));
AlertLogic.Start(sb, typeof(UserEntity), /*Alert*/typeof(OrderEntity));
FileLogic.Start(sb);
TranslationLogic.Start(sb, countLocalizationHits: false);
TranslatedInstanceLogic.Start(sb, () => CultureInfo.GetCultureInfo("en"));
HelpLogic.Start(sb);
WordTemplateLogic.Start(sb);
MapLogic.Start(sb);
PredictorLogic.Start(sb, () => new FileTypeAlgorithm(f => new PrefixPair(Starter.Configuration.Value.Folders.PredictorModelFolder)));
PredictorLogic.RegisterAlgorithm(CNTKPredictorAlgorithm.NeuralNetwork, new CNTKNeuralNetworkPredictorAlgorithm());
PredictorLogic.RegisterPublication(ProductPredictorPublication.MonthlySales, new PublicationSettings(typeof(OrderEntity)));
RestLogLogic.Start(sb);
RestApiKeyLogic.Start(sb);
WorkflowLogicStarter.Start(sb, () => Starter.Configuration.Value.Workflow);
ProfilerLogic.Start(sb,
timeTracker: true,
heavyProfiler: true,
overrideSessionTimeout: true);
// Southwind modules
EmployeeLogic.Start(sb);
ProductLogic.Start(sb);
CustomerLogic.Start(sb);
OrderLogic.Start(sb);
ShipperLogic.Start(sb);
StartSouthwindConfiguration(sb);
TypeConditionLogic.Register<OrderEntity>(SouthwindGroup.UserEntities, o => o.Employee == EmployeeEntity.Current);
TypeConditionLogic.Register<EmployeeEntity>(SouthwindGroup.UserEntities, e => EmployeeEntity.Current.Is(e));
TypeConditionLogic.Register<OrderEntity>(SouthwindGroup.CurrentCustomer, o => o.Customer == CustomerEntity.Current);
TypeConditionLogic.Register<PersonEntity>(SouthwindGroup.CurrentCustomer, o => o == CustomerEntity.Current);
TypeConditionLogic.Register<CompanyEntity>(SouthwindGroup.CurrentCustomer, o => o == CustomerEntity.Current);
if (includeDynamic)
{
DynamicLogic.StartDynamicModules(sb);
DynamicLogic.RegisterExceptionIfAny();
}
SetupCache(sb);
Schema.Current.OnSchemaCompleted();
}
}
public class CustomSchemaBuilder : SchemaBuilder
{
public CustomSchemaBuilder() : base(true)
{
}
public string? LogDatabaseName;
public override ObjectName GenerateTableName(Type type, TableNameAttribute? tn)
{
return base.GenerateTableName(type, tn).OnSchema(GetSchemaName(type));
}
public override ObjectName GenerateTableNameCollection(Table table, NameSequence name, TableNameAttribute? tn)
{
return base.GenerateTableNameCollection(table, name, tn).OnSchema(GetSchemaName(table.Type));
}
SchemaName GetSchemaName(Type type)
{
var isPostgres = this.Schema.Settings.IsPostgres;
return new SchemaName(this.GetDatabaseName(type), GetSchemaNameName(type) ?? SchemaName.Default(isPostgres).Name, isPostgres);
}
public Type[] InLogDatabase = new Type[]
{
typeof(OperationLogEntity),
typeof(ExceptionEntity),
};
DatabaseName? GetDatabaseName(Type type)
{
if (this.LogDatabaseName == null)
return null;
if (InLogDatabase.Contains(type))
return new DatabaseName(null, this.LogDatabaseName, this.Schema.Settings.IsPostgres);
return null;
}
static string? GetSchemaNameName(Type type)
{
type = EnumEntity.Extract(type) ?? type;
if (type == typeof(ColumnOptionsMode) || type == typeof(FilterOperation) || type == typeof(PaginationMode) || type == typeof(OrderType))
type = typeof(UserQueryEntity);
if (type == typeof(SmtpDeliveryFormat) || type == typeof(SmtpDeliveryMethod) || type == typeof(ExchangeVersion))
type = typeof(EmailMessageEntity);
if (type == typeof(DayOfWeek))
type = typeof(ScheduledTaskEntity);
if (type.Assembly == typeof(ApplicationConfigurationEntity).Assembly)
return null;
if (type.Namespace == DynamicCode.CodeGenEntitiesNamespace)
return "codegen";
if (type.Assembly == typeof(DashboardEntity).Assembly)
{
var name = type.Namespace!.Replace("Signum.Entities.", "");
name = (name.TryBefore('.') ?? name);
if (name == "SMS")
return "sms";
if (name == "Authorization")
return "auth";
return name.FirstLower();
}
if (type.Assembly == typeof(Entity).Assembly)
return "framework";
throw new InvalidOperationException("Impossible to determine SchemaName for {0}".FormatWith(type.FullName));
}
}
private static void OverrideAttributes(SchemaBuilder sb)
{
PredictorLogic.IgnorePinned(sb);
sb.Schema.Settings.TypeAttributes<OrderEntity>().Add(new SystemVersionedAttribute());
sb.Schema.Settings.FieldAttributes((RestLogEntity a) => a.User).Replace(new ImplementedByAttribute(typeof(UserEntity)));
sb.Schema.Settings.FieldAttributes((ExceptionEntity ua) => ua.User).Replace(new ImplementedByAttribute(typeof(UserEntity)));
sb.Schema.Settings.FieldAttributes((OperationLogEntity ua) => ua.User).Replace(new ImplementedByAttribute(typeof(UserEntity)));
sb.Schema.Settings.FieldAttributes((UserQueryEntity uq) => uq.Owner).Replace(new ImplementedByAttribute(typeof(UserEntity), typeof(RoleEntity)));
sb.Schema.Settings.FieldAttributes((UserChartEntity uc) => uc.Owner).Replace(new ImplementedByAttribute(typeof(UserEntity), typeof(RoleEntity)));
sb.Schema.Settings.FieldAttributes((DashboardEntity cp) => cp.Owner).Replace(new ImplementedByAttribute(typeof(UserEntity), typeof(RoleEntity)));
sb.Schema.Settings.FieldAttributes((ViewLogEntity cp) => cp.User).Replace(new ImplementedByAttribute(typeof(UserEntity)));
sb.Schema.Settings.FieldAttributes((NoteEntity n) => n.CreatedBy).Replace(new ImplementedByAttribute(typeof(UserEntity)));
sb.Schema.Settings.FieldAttributes((AlertEntity a) => a.CreatedBy).Replace(new ImplementedByAttribute(typeof(UserEntity)));
sb.Schema.Settings.FieldAttributes((AlertEntity a) => a.Recipient).Replace(new ImplementedByAttribute(typeof(UserEntity)));
sb.Schema.Settings.FieldAttributes((AlertEntity a) => a.AttendedBy).Replace(new ImplementedByAttribute(typeof(UserEntity)));
sb.Schema.Settings.FieldAttributes((PackageLineEntity cp) => cp.Package).Replace(new ImplementedByAttribute(typeof(PackageEntity), typeof(PackageOperationEntity)));
sb.Schema.Settings.FieldAttributes((ProcessExceptionLineEntity cp) => cp.Line).Replace(new ImplementedByAttribute(typeof(PackageLineEntity)));
sb.Schema.Settings.FieldAttributes((ProcessEntity cp) => cp.Data).Replace(new ImplementedByAttribute(typeof(PackageEntity), typeof(PackageOperationEntity), typeof(EmailPackageEntity), typeof(AutoconfigureNeuralNetworkEntity), typeof(PredictorEntity)));
sb.Schema.Settings.FieldAttributes((ProcessEntity s) => s.User).Replace(new ImplementedByAttribute(typeof(UserEntity)));
sb.Schema.Settings.FieldAttributes((EmailMessageEntity em) => em.From.EmailOwner).Replace(new ImplementedByAttribute(typeof(UserEntity)));
sb.Schema.Settings.FieldAttributes((EmailMessageEntity em) => em.Recipients.First().EmailOwner).Replace(new ImplementedByAttribute(typeof(UserEntity)));
sb.Schema.Settings.FieldAttributes((EmailSenderConfigurationEntity em) => em.DefaultFrom!.EmailOwner).Replace(new ImplementedByAttribute(typeof(UserEntity)));
sb.Schema.Settings.FieldAttributes((EmailSenderConfigurationEntity em) => em.AdditionalRecipients.First().EmailOwner).Replace(new ImplementedByAttribute(typeof(UserEntity)));
sb.Schema.Settings.FieldAttributes((ScheduledTaskEntity a) => a.User).Replace(new ImplementedByAttribute(typeof(UserEntity)));
sb.Schema.Settings.FieldAttributes((ScheduledTaskLogEntity a) => a.User).Replace(new ImplementedByAttribute(typeof(UserEntity)));
sb.Schema.Settings.FieldAttributes((DashboardEntity a) => a.Parts[0].Content).Replace(new ImplementedByAttribute(typeof(UserChartPartEntity), typeof(UserQueryPartEntity), typeof(ValueUserQueryListPartEntity), typeof(LinkListPartEntity)));
}
private static void StartSouthwindConfiguration(SchemaBuilder sb)
{
sb.Include<ApplicationConfigurationEntity>()
.WithSave(ApplicationConfigurationOperation.Save)
.WithQuery(() => s => new
{
Entity = s,
s.Id,
s.Environment,
s.Email.SendEmails,
s.Email.OverrideEmailAddress,
s.Email.DefaultCulture,
s.Email.UrlLeft
});
Configuration = sb.GlobalLazy<ApplicationConfigurationEntity>(
() => Database.Query<ApplicationConfigurationEntity>().Single(a => a.DatabaseName == Connector.Current.DatabaseName()),
new InvalidateWith(typeof(ApplicationConfigurationEntity)));
}
private static void SetupCache(SchemaBuilder sb)
{
CacheLogic.CacheTable<ShipperEntity>(sb);
}
}
}