SchedulerDB.cs
14.4 KB
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
/*
Enable-Migrations -ContextTypeName iSchedulerDB -MigrationsDirectory Migrations
Update-Database -TargetMigration $InitialDatabase
Add-Migration -name InitialDB -Force -Verbose
Update-Database -Verbose
Update-Database -TargetMigration InitialDB -Verbose -Force
Add-Migration -name UpdateDB_01 -Force -Verbose
Update-Database -TargetMigration UpdateDB_01 -Verbose -Force
Add-Migration -name UpdateDB_02 -Force -Verbose
*/
namespace Vrh.iScheduler
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using Vrh.XmlProcessing;
/// <summary>
/// Az ütemezésekhez szükséges adatmodell (DBContext).
/// </summary>
public class SchedulerDB : DbContext
{
#region Constructors
/// <summary>
/// A DBContext elõállítása az alapértelmezett kapcsolati sztringgel.
/// </summary>
public SchedulerDB() : this(ConnectionStringStore.GetSQL(SchConst.DEFAULT_SQLCONNECTIONSTRINGNAME)) { }
/// <summary>
/// A DBContext elõállítása <paramref name="connectionString"/>
/// paraméterben megadott kapcsolati sztringgel.
/// </summary>
/// <param name="connectionString">Szabványos és szabályos SQL kapcsolati sztring. (Nem megnevezés!)</param>
public SchedulerDB(string connectionString) : base(connectionString)
{
// migráció ellenõrzése
System.Data.Entity.Database.SetInitializer(new System.Data.Entity.MigrateDatabaseToLatestVersion<SchedulerDB, Vrh.iScheduler.Migrations.Configuration>(true));
}
#endregion Constructors
#region DbSets
/// <summary>
/// Az ütemezendõ ojektumokat tartalmazó táblázat.
/// </summary>
public virtual DbSet<ScheduleObject> ScheduleObjects { get; set; }
/// <summary>
/// Az ütemezendõ ojektumokon végezhetõ mûveleteket tartalmazó táblázat.
/// </summary>
public virtual DbSet<ScheduleOperation> ScheduleOperations { get; set; }
/// <summary>
/// Az ütemezéseket tartalmazó táblázat.
/// </summary>
public virtual DbSet<Schedule> Schedules { get; set; }
/// <summary>
/// Sorozat ütemezések azonosítói és típusai.
/// </summary>
public virtual DbSet<ScheduleSeriesOne> ScheduleSeries { get; set; }
#endregion DbSets
#region OnModelCreating
/// <summary>
/// A EF CodeFirst-ben nem elvégezhetõ vagy módosítandó dolgokat ebben
/// az eseményben kell elvégezni.
/// </summary>
/// <param name="modelBuilder"></param>
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//modelBuilder.HasDefaultSchema("GoodBill");
//WA20160124: Sajnos az EF a kötelezõ idegen kulcsnál alapértelmezésként cascadeDelete értékét true-ra állítja,
// ami sok esetben nem megfelelõ (nem is értem, miért true a default :(( )
// Ahol ez elõfordul, ott a ForeignKey-eket itt kell definiálni.
// HasRequired = many oldal virtual property
// WithMany = ha a one oldalon lett definiálva ICollection a many-ra, akkor azt kell ide írni, egyébként üres
// HasForeignKey = many oldalon a foreign key mezõ
modelBuilder.Entity<Schedule>().HasRequired(v => v.ScheduleObject)
.WithMany(one => one.Schedules)
.HasForeignKey(t => t.ScheduleObjectId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Schedule>().HasRequired(v => v.ScheduleOperation)
.WithMany(one => one.Schedules)
.HasForeignKey(t => t.ScheduleOperationId)
.WillCascadeOnDelete(false);
base.OnModelCreating(modelBuilder);
}
#endregion OnModelCreating
public class SelectLists
{
public static void CreateSystemViews(string sqlconnectionstring)
{
const string createview = @"CREATE VIEW [iScheduler].[{0}] AS {1}";
const string dropview = @"IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[iScheduler].[{0}]')) DROP VIEW [iScheduler].[{0}];";
var sl = new Dictionary<string, string>();
sl.Add("SelectList_Schedules", selecttxt_SelectList_Schedules);
sl.Add("SelectList_SchedulesActive", selecttxt_SelectList_SchedulesActive);
int numofall = 0, numofcreated = 0;
using (var dbc = new SchedulerDB(sqlconnectionstring))
{
foreach (var s in sl)
{
numofall++;
var dropviewtext = string.Format(dropview, s.Key);
var createviewtext = string.Format(createview, s.Key, s.Value);
dbc.Database.ExecuteSqlCommand(dropviewtext);
dbc.Database.ExecuteSqlCommand(createviewtext);
}
dbc.SaveChanges();
}
}
#region systemviewtexts
const string selecttxt_SelectList_Schedules = @"
SELECT TOP 100
[iScheduler].[Schedules].[Id] AS IID,
convert(varchar(25), [iScheduler].[Schedules].[Id]) + ' (' + convert(varchar(25), [OperationTime], 120) + ','
+ [iScheduler].[ScheduleObjects].[ObjectType] + ','
+ [iScheduler].[ScheduleObjects].[ObjectName] + ','
+ [iScheduler].[ScheduleOperations].[OperationName] + ','
+ (CASE WHEN [iScheduler].[Schedules].[State] = 0 THEN 'Active' ELSE 'Passive' END) + ')'
AS DDISPLAY
FROM [iScheduler].[Schedules]
INNER JOIN [iScheduler].[ScheduleObjects] ON [iScheduler].[Schedules].[ScheduleObjectId] = [iScheduler].[ScheduleObjects].[Id]
INNER JOIN [iScheduler].[ScheduleOperations] ON [iScheduler].[Schedules].[ScheduleOperationId] = [iScheduler].[ScheduleOperations].[Id]";
const string selecttxt_SelectList_SchedulesActive = selecttxt_SelectList_Schedules + @" WHERE [iScheduler].[Schedules].[State] = 0";
#endregion systemviewtexts
}
}
#region ScheduleObject class
/// <summary>
/// Ütemezhetõ objektumokat tartalmazó táblázat.
/// Egy típus alatt egyedinek kell lennie az azonosítónak.
/// </summary>
[Table("ScheduleObjects", Schema = "iScheduler")]
public partial class ScheduleObject
{
#region Properties
/// <summary>
/// Az ütemezett objektum belsõ egyedi azonosítója.
/// </summary>
[Key]
public int Id { get; set; }
/// <summary>
/// Ütemezett objektum típusa.
/// </summary>
[Required, MaxLength(100), Index("IX_ScheduleObject_ObjectTypeObjectId", 1, IsUnique = true)]
public string ObjectType { get; set; }
/// <summary>
/// Ütemezett objektum azonosítója.
/// </summary>
[Required, MaxLength(100), Index("IX_ScheduleObject_ObjectTypeObjectId", 2, IsUnique = true)]
public string ObjectId { get; set; }
/// <summary>
/// Az ütemezett objektum publikus megnevezése.
/// </summary>
[Required, MaxLength(200)]
public string ObjectName { get; set; }
/// <summary>
/// Ütemezett objektumcsoport azonosítója.
/// </summary>
[Required, MaxLength(100)]
public string ObjectGroupId { get; set; }
/// <summary>
/// Gyûjtemény, mely azokat az ütemezéseket tartalmazza,
/// amelyekben az objektum szerepel.
/// </summary>
public virtual ICollection<Schedule> Schedules { get; set; }
#endregion Properties
#region Constructor
/// <summary>
/// A ScheduleObject alapértelmezett konstruktora,
/// melyben inicializálódik a Schedules gyûjtemény.
/// </summary>
public ScheduleObject()
{
Schedules = new HashSet<Schedule>();
}
#endregion Constructor
}
#endregion ScheduleObject class
#region ScheduleOperation class
/// <summary>
/// Ütemezhetõ objektumokon elvégezhetõ mûveletek.
/// Egy típus alatt egyedinek kell lennie az azonosítónak.
/// </summary>
[Table("ScheduleOperations", Schema = "iScheduler")]
public partial class ScheduleOperation
{
#region Properties
/// <summary>
/// A mûvelet egyedi belsõ azonosítója.
/// </summary>
[Key]
public short Id { get; set; }
/// <summary>
/// Ütemezett objektum típusa.
/// </summary>
[Required, MaxLength(100), Index("IX_ScheduleOperation_ObjectTypeOperationId", 1, IsUnique = true)]
public string ObjectType { get; set; }
/// <summary>
/// Ütemezett objektumon végezhetõ mûvelet "távoli" azonosítója.
/// </summary>
[Required, MaxLength(100), Index("IX_ScheduleOperation_ObjectTypeOperationId", 2, IsUnique = true)]
public string OperationId { get; set; }
/// <summary>
/// Ütemezett objektumon végezhetõ mûvelet "távoli" megnevezése.
/// </summary>
[Required, MaxLength(200)]
public string OperationName { get; set; }
/// <summary>
/// Gyûjtemény, mely azokat az ütemezéseket tartalmazza,
/// amelyekben a mûvelet szerepel.
/// </summary>
public virtual ICollection<Schedule> Schedules { get; set; }
#endregion Properties
#region Constructor
/// <summary>
/// A ScheduleOperation alapértelmezett konstruktora,
/// melyben a Schedules gyûjtemény inicializálása történik.
/// </summary>
public ScheduleOperation()
{
Schedules = new HashSet<Schedule>();
}
#endregion Constructor
}
#endregion ScheduleOperation class
#region Schedule class
/// <summary>
/// Ütemezéseket tartalmazó táblázat.
/// </summary>
[Table("Schedules", Schema = "iScheduler")]
public partial class Schedule
{
#region Properties
/// <summary>
/// Az ütemezés egyedi belsõ azonosítója.
/// </summary>
[Key]
public int Id { get; set; }
/// <summary>
/// Az adott (OperationTime) idõpontban végrehajtandó mûvelet ezen objektumhoz kapcsolódik.
/// </summary>
public int ScheduleObjectId { get; set; }
/// <summary>
/// Az adott (OperationTime) idõpontban végrehajtandó mûvelet azonosítója.
/// </summary>
public short ScheduleOperationId { get; set; }
/// <summary>
/// Ütemezés idõpontja, amikor végre kell hajtani a jelölt mûveletet a jelölt objektumon.
/// </summary>
public DateTime OperationTime { get; set; }
/// <summary>
/// Ütemezés állapota. Alapértelmezett értéke: ScheduleStates.Active (0)
/// </summary>
[Index(IsUnique = false)]
public ScheduleStates State { get; set; }
/// <summary>
/// Annak a sorozat ütemezésnek az azonosítója, mely alapján az egyedi ütemezés létrejött.
/// Ha nem sorozat ütemezés alapján (wizard) jön létre, akkor az értéke null.
/// </summary>
public int? ScheduleSeriesId { get; set; }
/// <summary>
/// Mûvelet végrehajtásakor visszakapott ReturnInfoJSON ReturnValue tulajdonság értékének tárolására.
/// </summary>
/// <remarks>Csak akkor kerül bele érték, ha az iSchedulerMonitor service hajtja végre ScheduleExecute akciót.</remarks>
public int? ReturnValue { get; set; }
/// <summary>
/// Mûvelet végrehajtásakor visszakapott ReturnInfoJSON ReturnMessage tulajdonság értékének tárolására.
/// </summary>
/// <remarks>Csak akkor kerül bele érték, ha az iSchedulerMonitor service hajtja végre ScheduleExecute akciót.</remarks>
public string ReturnMessage { get; set; }
/// <summary>
/// ScheduleObjectId idegen kulcshoz tartozó virtuális tulajdonság.
/// </summary>
[ForeignKey("ScheduleObjectId")]
public virtual ScheduleObject ScheduleObject { get; set; }
/// <summary>
/// ScheduleOperationId idegen kulcshoz tartozó virtuális tulajdonság.
/// </summary>
[ForeignKey("ScheduleOperationId")]
public virtual ScheduleOperation ScheduleOperation { get; set; }
/// <summary>
/// ScheduleSeriesId idegen kulcshoz tartozó virtuális tulajdonság.
/// </summary>
[ForeignKey("ScheduleSeriesId")]
public virtual ScheduleSeriesOne ScheduleSeriesOne { get; set; }
#endregion Properties
#region Contructor
/// <summary>
/// A Schedule alapértelmezett konstruktora, melyben a State, és
/// ScheduleSeriesId mezõk inicializálása történik meg.
/// </summary>
public Schedule()
{
State = ScheduleStates.Active;
ScheduleSeriesId = null;
}
#endregion Contructor
}
#endregion Schedule class
#region ScheduleSeriesOne class
/// <summary>
/// Sorozat ütemezéseket tartalmazó táblázat.
/// </summary>
/// <remarks>
/// A név ütközött valami egyéb összetevõvel, ezért a típus kapott egy "One" suffix-et.
/// </remarks>
[Table("ScheduleSeries", Schema = "iScheduler")]
public partial class ScheduleSeriesOne
{
/// <summary>
/// Sorozat egyedi belsõ azonosítója.
/// </summary>
[Key]
public int Id { get; set; }
/// <summary>
/// Sorozat típusa.
/// </summary>
[Required, MaxLength(20)]
public string SeriesType { get; set; }
/// <summary>
/// Sorozat megnevezése, amelyet meg is mutatunk a manager-ben összefûzve a típussal
/// TT-nnnnnnnn (id).
/// </summary>
[Required, MaxLength(100)]
public string SeriesName { get; set; }
/// <summary>
/// Sorozat színjelölése.
/// </summary>
[MaxLength(30)]
public string SeriesColor { get; set; }
/// <summary>
///
/// </summary>
public virtual ICollection<Schedule> Schedules { get; set; }
}
#endregion ScheduleSeriesOne class
}