/
Recording.cs
624 lines (553 loc) · 17.1 KB
/
Recording.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
#region Copyright (C) 2005-2011 Team MediaPortal
// Copyright (C) 2005-2011 Team MediaPortal
// http://www.team-mediaportal.com
//
// MediaPortal is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// MediaPortal is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with MediaPortal. If not, see <http://www.gnu.org/licenses/>.
#endregion
using System;
using System.Collections.Generic;
using Gentle.Framework;
using TvLibrary.Log;
namespace TvDatabase
{
/// <summary>
/// Instances of this class represent the properties and methods of a row in the table <b>Recording</b>.
/// </summary>
[TableName("Recording")]
public class Recording : Persistent
{
#region Members
private bool isChanged;
[TableColumn("idRecording", NotNull = true), PrimaryKey(AutoGenerated = true)] private int idRecording;
[TableColumn("idChannel", NotNull = true), ForeignKey("Channel", "idChannel")] private int idChannel;
[TableColumn("idSchedule", NotNull = true), ForeignKey("Schedule", "idSchedule")] private int idSchedule;
[TableColumn("isRecording", NotNull = false)] private bool isRecording;
[TableColumn("startTime", NotNull = true)] private DateTime startTime;
[TableColumn("endTime", NotNull = true)] private DateTime endTime;
[TableColumn("title", NotNull = true)] private string title;
[TableColumn("description", NotNull = true)] private string description;
[TableColumn("genre", NotNull = true)] private string genre;
[TableColumn("fileName", NotNull = true)] private string fileName;
[TableColumn("keepUntil", NotNull = true)] private int keepUntil;
[TableColumn("keepUntilDate", NotNull = true)] private DateTime keepUntilDate;
[TableColumn("timesWatched", NotNull = true)] private int timesWatched;
[TableColumn("idServer", NotNull = true), ForeignKey("Server", "idServer")] private int idServer;
// stores the last position - to make resume possible
[TableColumn("stopTime", NotNull = true)] private int stopTime;
[TableColumn("episodeName", NotNull = true)] private string episodeName;
[TableColumn("seriesNum", NotNull = true)] private string seriesNum;
[TableColumn("episodeNum", NotNull = true)] private string episodeNum;
[TableColumn("episodePart", NotNull = true)] private string episodePart;
#endregion
#region Constructors
/// <summary>
/// Create a new object by specifying all fields (except the auto-generated primary key field).
/// </summary>
public Recording(int idChannel, int idSchedule, bool isRecording, DateTime startTime, DateTime endTime, string title,
string description, string genre,
string fileName, int keepUntil, DateTime keepUntilDate, int timesWatched, int idServer,
string episodeName,
string seriesNum, string episodeNum, string episodePart)
{
isChanged = true;
this.idChannel = idChannel;
this.isRecording = isRecording;
this.idSchedule = idSchedule;
this.startTime = startTime;
this.endTime = endTime;
this.title = title;
this.description = description;
this.genre = genre;
this.fileName = fileName;
this.keepUntil = keepUntil;
this.keepUntilDate = keepUntilDate;
this.timesWatched = timesWatched;
this.idServer = idServer;
stopTime = 0;
this.episodeName = episodeName;
this.seriesNum = seriesNum;
this.episodeNum = episodeNum;
this.episodePart = episodePart;
}
/// <summary>
/// Create an object from an existing row of data. This will be used by Gentle to
/// construct objects from retrieved rows.
/// </summary>
public Recording(int idRecording, bool isRecording, int idSchedule, int idChannel, DateTime startTime,
DateTime endTime, string title,
string description, string genre, string fileName, int keepUntil, DateTime keepUntilDate,
int timesWatched, int idServer, int stopTime, string episodeName,
string seriesNum, string episodeNum, string episodePart)
{
this.idSchedule = idSchedule;
this.idRecording = idRecording;
this.isRecording = isRecording;
this.idChannel = idChannel;
this.startTime = startTime;
this.endTime = endTime;
this.title = title;
this.description = description;
this.genre = genre;
this.fileName = fileName;
this.keepUntil = keepUntil;
this.keepUntilDate = keepUntilDate;
this.timesWatched = timesWatched;
this.idServer = idServer;
this.stopTime = stopTime;
this.episodeName = episodeName;
this.seriesNum = seriesNum;
this.episodeNum = episodeNum;
this.episodePart = episodePart;
}
#endregion
#region Public Properties
/// <summary>
/// Is recording a manual one
/// </summary>
public bool IsManual
{
get { return (Title.Equals("manual")); }
}
/// <summary>
/// Indicates whether the entity is changed and requires saving or not.
/// </summary>
public bool IsChanged
{
get { return isChanged; }
}
/// <summary>
/// Property relating to database column idRecording
/// </summary>
public int IdRecording
{
get { return idRecording; }
}
/// <summary>
/// Property relating to database column idChannel
/// </summary>
public int IdChannel
{
get { return idChannel; }
set
{
isChanged |= idChannel != value;
idChannel = value;
}
}
/// <summary>
/// Property relating to database column idSchedule
/// </summary>
public int Idschedule
{
get { return idSchedule; }
set
{
isChanged |= idSchedule != value;
idSchedule = value;
}
}
/// <summary>
/// Property relating to database column startTime
/// </summary>
public DateTime StartTime
{
get { return startTime; }
set
{
isChanged |= startTime != value;
startTime = value;
}
}
/// <summary>
/// Property relating to database column endTime
/// </summary>
public DateTime EndTime
{
get { return endTime; }
set
{
isChanged |= endTime != value;
endTime = value;
}
}
/// <summary>
/// Property relating to database column title
/// </summary>
public string Title
{
get { return title; }
set
{
isChanged |= title != value;
title = value;
}
}
/// <summary>
/// Property relating to database column description
/// </summary>
public string Description
{
get { return description; }
set
{
isChanged |= description != value;
description = value;
}
}
/// <summary>
/// Property relating to database column genre
/// </summary>
public string Genre
{
get { return genre; }
set
{
isChanged |= genre != value;
genre = value;
}
}
/// <summary>
/// Property relating to database column fileName
/// </summary>
public string FileName
{
get { return fileName; }
set
{
isChanged |= fileName != value;
fileName = value;
}
}
/// <summary>
/// Property relating to database column keepUntil
/// </summary>
public int KeepUntil
{
get { return keepUntil; }
set
{
isChanged |= keepUntil != value;
keepUntil = value;
}
}
/// <summary>
/// Property relating to database column keepUntilDate
/// </summary>
public DateTime KeepUntilDate
{
get { return keepUntilDate; }
set
{
isChanged |= keepUntilDate != value;
keepUntilDate = value;
}
}
/// <summary>
/// Property relating to database column timesWatched
/// </summary>
public int TimesWatched
{
get { return timesWatched; }
set
{
isChanged |= timesWatched != value;
timesWatched = value;
}
}
/// <summary>
/// Property relating to database column idServer
/// </summary>
public int IdServer
{
get { return idServer; }
set
{
isChanged |= idServer != value;
idServer = value;
}
}
/// <summary>
/// Property relating to database column stopTime
/// </summary>
public int StopTime
{
get { return stopTime; }
set
{
isChanged |= stopTime != value;
stopTime = value;
}
}
/// <summary>
/// Property relating to database column episodeName
/// </summary>
public string EpisodeName
{
get { return episodeName; }
set
{
isChanged |= episodeName != value;
episodeName = value;
}
}
/// <summary>
/// Property relating to database column serieNum
/// </summary>
public string SeriesNum
{
get { return seriesNum; }
set
{
isChanged |= seriesNum != value;
seriesNum = value;
}
}
/// <summary>
/// Property relating to database column episodeNum
/// </summary>
public string EpisodeNum
{
get { return episodeNum; }
set
{
isChanged |= episodeNum != value;
episodeNum = value;
}
}
/// <summary>
/// Property relating to database column episodePart
/// </summary>
public string EpisodePart
{
get { return episodePart; }
set
{
isChanged |= episodePart != value;
episodePart = value;
}
}
/// <summary>
/// Property relating to database column seriesNum, episodeNum and episodePart (readonly)
/// </summary>
public string EpisodeNumber
{
get { return TvDatabase.EpisodeFormatter.GetEpisodeNumber(seriesNum, episodeNum, episodePart); }
}
/// <summary>
/// Property relating to database column isRecording
/// </summary>
public bool IsRecording
{
get
{
this.Refresh();
return isRecording;
}
set
{
isChanged |= isRecording != value;
isRecording = value;
}
}
#endregion
#region Storage and Retrieval
/// <summary>
/// Static method to retrieve all instances that are stored in the database in one call
/// </summary>
public static IList<Recording> ListAll()
{
return (List<Recording>)(Broker.RetrieveList<Recording>());
}
/// <summary>
/// Static method to retrieve all instances that are stored in the database in one call
/// </summary>
public static void ResetActiveRecordings()
{
/*SqlBuilder sb = new SqlBuilder(StatementType.Update, typeof(Recording));
sb.AddConstraint(Operator.Equals, "isRecording", false);
SqlStatement stmt = sb.GetStatement(true);
// execute the statement/query and create a collection of User instances from the result set
return ObjectFactory.GetCollection<Recording>(stmt.Execute());
*/
IList<Recording> activeRecordings = Recording.ListAllActive();
foreach (Recording rec in activeRecordings)
{
rec.IsRecording = false;
rec.Persist();
}
}
public static Recording ActiveRecording(int idSchedule)
{
SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof (Recording));
sb.AddConstraint(Operator.Equals, "isRecording", true);
sb.AddConstraint(Operator.Equals, "idSchedule", idSchedule);
SqlStatement stmt = sb.GetStatement(true);
// execute the statement/query and create a collection of User instances from the result set
IList<Recording> getList = ObjectFactory.GetCollection<Recording>(stmt.Execute());
if (getList.Count != 0)
{
return getList[0];
}
return null;
}
public static Recording ActiveRecording(string title, int idChannel)
{
SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof (Recording));
sb.AddConstraint(Operator.Equals, "isRecording", true);
sb.AddConstraint(Operator.Equals, "title", title);
sb.AddConstraint(Operator.Equals, "idChannel", idChannel);
SqlStatement stmt = sb.GetStatement(true);
// execute the statement/query and create a collection of User instances from the result set
IList<Recording> getList = ObjectFactory.GetCollection<Recording>(stmt.Execute());
if (getList.Count != 0)
{
return getList[0];
}
return null;
}
/// <summary>
/// Static method to retrieve all instances that are stored in the database in one call
/// </summary>
public static IList<Recording> ListAllActive()
{
SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof (Recording));
sb.AddConstraint(Operator.Equals, "isRecording", true);
SqlStatement stmt = sb.GetStatement(true);
// execute the statement/query and create a collection of User instances from the result set
return ObjectFactory.GetCollection<Recording>(stmt.Execute());
}
/// <summary>
/// Refresh the recording
/// </summary>
public static Recording Refresh(int id)
{
// Return null if id is smaller than seed and/or increment for autokey
if (id < 1)
{
return null;
}
Key key = new Key(typeof (Recording), true, "idRecording", id);
Recording rec = Broker.TryRetrieveInstance<Recording>(key);
if (rec != null)
{
Broker.Refresh(rec);
}
return rec;
}
/// <summary>
/// Retrieves an entity given it's id.
/// </summary>
public static Recording Retrieve(int id)
{
// Return null if id is smaller than seed and/or increment for autokey
if (id < 1)
{
return null;
}
Key key = new Key(typeof (Recording), true, "idRecording", id);
return Broker.RetrieveInstance<Recording>(key);
}
/// <summary>
/// Retrieves an entity given it's filename.
/// </summary>
public static Recording Retrieve(string fileName)
{
// Return null if id is smaller than seed and/or increment for autokey
if (string.IsNullOrEmpty(fileName))
{
return null;
}
SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof (Recording));
sb.AddConstraint(Operator.Equals, "fileName", fileName);
SqlStatement stmt = sb.GetStatement(true);
// execute the statement/query and create a collection of User instances from the result set
IList<Recording> getList = ObjectFactory.GetCollection<Recording>(stmt.Execute());
if (getList.Count != 0)
{
return getList[0];
}
return null;
}
/// <summary>
/// Retrieves an entity given it's id, using Gentle.Framework.Key class.
/// This allows retrieval based on multi-column keys.
/// </summary>
public static Recording Retrieve(Key key)
{
return Broker.RetrieveInstance<Recording>(key);
}
/// <summary>
/// Persists the entity if it was never persisted or was changed.
/// </summary>
public override void Persist()
{
if (IsChanged || !IsPersisted)
{
try
{
base.Persist();
}
catch (Exception ex)
{
Log.Error("Exception in Recording.Persist() with Message {0}", ex.Message);
return;
}
isChanged = false;
}
}
#endregion
#region Relations
/// <summary>
///
/// </summary>
public Channel ReferencedChannel()
{
return Channel.Retrieve(IdChannel);
}
/// <summary>
///
/// </summary>
public Schedule ReferencedSchedule()
{
return Schedule.Retrieve(Idschedule);
}
/// <summary>
///
/// </summary>
public Server ReferencedServer()
{
return Server.Retrieve(IdServer);
}
#endregion
public bool ShouldBeDeleted
{
get
{
if (KeepUntil != (int)KeepMethodType.TillDate)
{
return false;
}
if (KeepUntilDate.Date > DateTime.Now.Date)
{
return false;
}
return true;
}
}
public void Delete()
{
Remove();
}
}
}