forked from MediaPortal/MediaPortal-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
1174 lines (1039 loc) · 42.7 KB
/
Program.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 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>Program</b>.
/// </summary>
[TableName("Program")]
public class Program : Persistent
{
#region Members
private bool isChanged;
[TableColumn("idProgram", NotNull = true), PrimaryKey(AutoGenerated = true)] private int idProgram;
[TableColumn("idChannel", NotNull = true), ForeignKey("Channel", "idChannel")] private int idChannel;
[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("state", NotNull = true)] private int state;
[TableColumn("originalAirDate", NotNull = true)] private DateTime originalAirDate;
[TableColumn("seriesNum", NotNull = true)] private string seriesNum;
[TableColumn("episodeNum", NotNull = true)] private string episodeNum;
[TableColumn("episodePart", NotNull = true)] private string episodePart;
[TableColumn("episodeName", NotNull = true)] private string episodeName;
[TableColumn("starRating", NotNull = true)] private int starRating;
[TableColumn("classification", NotNull = true)] private string classification;
[TableColumn("parentalRating", NotNull = true)] private int parentalRating;
#endregion
#region public enum
[Flags]
public enum ProgramState
{
None = 0,
Notify = 1,
RecordOnce = 2,
RecordSeries = 4,
RecordManual = 8,
Conflict = 16,
RecordOncePending = 32, // used to indicate recording icon on tvguide, even though it hasnt begun yet.
RecordSeriesPending = 64, // used to indicate recording icon on tvguide, even though it hasnt begun yet.
PartialRecordSeriesPending = 128
// used to indicate partial recording icon on tvguide, even though it hasnt begun yet.
}
#endregion
#region Constructors
/// <summary>
/// Create a new object by specifying all fields (except the auto-generated primary key field).
/// </summary>
public Program(int idChannel, DateTime startTime, DateTime endTime, string title, string description, string genre,
ProgramState state, DateTime originalAirDate, string seriesNum, string episodeNum, string episodeName,
string episodePart, int starRating,
string classification, int parentalRating)
{
isChanged = true;
this.idChannel = idChannel;
this.startTime = startTime;
this.endTime = endTime;
this.title = title;
this.description = description;
this.genre = genre;
this.state = (int)state;
this.originalAirDate = originalAirDate;
this.seriesNum = seriesNum;
this.episodeNum = episodeNum;
this.episodeName = episodeName;
this.episodePart = episodePart;
this.starRating = starRating;
this.classification = classification;
this.parentalRating = parentalRating;
}
/// <summary>
/// Create an object from an existing row of data. This will be used by Gentle to
/// construct objects from retrieved rows.
/// </summary>
public Program(int idProgram, int idChannel, DateTime startTime, DateTime endTime, string title, string description,
string genre, ProgramState state, DateTime originalAirDate, string seriesNum, string episodeNum,
string episodeName, string episodePart,
int starRating, string classification, int parentalRating)
{
this.idProgram = idProgram;
this.idChannel = idChannel;
this.startTime = startTime;
this.endTime = endTime;
this.title = title;
this.description = description;
this.genre = genre;
this.state = (int)state;
this.originalAirDate = originalAirDate;
this.seriesNum = seriesNum;
this.episodeNum = episodeNum;
this.episodeName = episodeName;
this.episodePart = episodePart;
this.starRating = starRating;
this.classification = classification;
this.parentalRating = parentalRating;
}
#endregion
#region Public Properties
/// <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 idProgram
/// </summary>
public int IdProgram
{
get { return idProgram; }
}
/// <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 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 notify
/// </summary>
public bool Notify
{
get { return ((state & (int)ProgramState.Notify) == (int)ProgramState.Notify); }
set
{
ProgramState newState = (ProgramState)state;
if (!value && (state & (int)ProgramState.Notify) == (int)ProgramState.Notify)
// remove the notify bit flag if present
{
newState ^= ProgramState.Notify;
}
if (value) //add the notify bit flag
{
newState |= ProgramState.Notify;
}
isChanged |= (int)state != (int)newState;
state = (int)newState;
}
}
/// <summary>
/// Property relating to database column conflict
/// </summary>
public bool HasConflict
{
get { return ((state & (int)ProgramState.Conflict) == (int)ProgramState.Conflict); }
set
{
ProgramState newState = (ProgramState)state;
if (!value && (state & (int)ProgramState.Conflict) == (int)ProgramState.Conflict)
// remove the Conflict bit flag if present
{
newState ^= ProgramState.Conflict;
}
if (value) //add the Conflict bit flag
{
newState |= ProgramState.Conflict;
}
isChanged |= (int)state != (int)newState;
state = (int)newState;
}
}
/// <summary>
/// Property relating to database column IsRecording
/// </summary>
public bool IsRecording
{ // This should reflect whether program is actually recording right now
get { return ( IsRecordingSeries || IsRecordingManual || IsRecordingOnce); }
}
/// <summary>
/// Property relating to database column IsRecordingOnce
/// </summary>
public bool IsRecordingOnce
{
get { return ((state & (int)ProgramState.RecordOnce) == (int)ProgramState.RecordOnce); }
set
{
ProgramState newState = (ProgramState)state;
if (!value && (state & (int)ProgramState.RecordOnce) == (int)ProgramState.RecordOnce)
// remove the Record bit flag if present
{
newState ^= ProgramState.RecordOnce;
}
if (value) //add the Record bit flag
{
newState |= ProgramState.RecordOnce;
}
isChanged |= (int)state != (int)newState;
state = (int)newState;
}
}
/// <summary>
/// Property relating to database column IsRecordingSeriesPending
/// </summary>
public bool IsRecordingSeriesPending
{
get { return ((state & (int)ProgramState.RecordSeriesPending) == (int)ProgramState.RecordSeriesPending); }
set
{
ProgramState newState = (ProgramState)state;
if (!value && (state & (int)ProgramState.RecordSeriesPending) == (int)ProgramState.RecordSeriesPending)
// remove the Record bit flag if present
{
newState ^= ProgramState.RecordSeriesPending;
}
if (value) //add the Record bit flag
{
newState |= ProgramState.RecordSeriesPending;
}
isChanged |= (int)state != (int)newState;
state = (int)newState;
}
}
/// <summary>
/// Property relating to database column IsRecordingSeriesPending
/// </summary>
public bool IsPartialRecordingSeriesPending
{
get { return ((state & (int)ProgramState.PartialRecordSeriesPending) == (int)ProgramState.PartialRecordSeriesPending); }
set
{
ProgramState newState = (ProgramState)state;
if (value) //add the Record bit flag
{
newState |= ProgramState.PartialRecordSeriesPending;
}
else
{
newState &= ~ProgramState.PartialRecordSeriesPending;
}
isChanged |= (int)state != (int)newState;
state = (int)newState;
}
}
/// <summary>
/// Property relating to database column IsRecordingOncePending
/// </summary>
public bool IsRecordingOncePending
{
get { return ((state & (int)ProgramState.RecordOncePending) == (int)ProgramState.RecordOncePending); }
set
{
ProgramState newState = (ProgramState)state;
if (!value && (state & (int)ProgramState.RecordOncePending) == (int)ProgramState.RecordOncePending)
// remove the Record bit flag if present
{
newState ^= ProgramState.RecordOncePending;
}
if (value) //add the Record bit flag
{
newState |= ProgramState.RecordOncePending;
}
isChanged |= (int)state != (int)newState;
state = (int)newState;
}
}
/// <summary>
/// Property relating to database column IsRecordingManual
/// </summary>
public bool IsRecordingManual
{
get { return ((state & (int)ProgramState.RecordManual) == (int)ProgramState.RecordManual); }
set
{
ProgramState newState = (ProgramState)state;
if (!value && (state & (int)ProgramState.RecordManual) == (int)ProgramState.RecordManual)
// remove the Record bit flag if present
{
newState ^= ProgramState.RecordManual;
}
if (value) //add the Record bit flag
{
newState |= ProgramState.RecordManual;
}
isChanged |= (int)state != (int)newState;
state = (int)newState;
}
}
/// <summary>
/// Property relating to database column isRecording
/// </summary>
public bool IsRecordingSeries
{
get { return ((state & (int)ProgramState.RecordSeries) == (int)ProgramState.RecordSeries); }
set
{
ProgramState newState = (ProgramState)state;
if (!value && (state & (int)ProgramState.RecordSeries) == (int)ProgramState.RecordSeries)
// remove the RecordSeries bit flag if present
{
newState ^= ProgramState.RecordSeries;
}
if (value) //add the RecordSeries bit flag
{
newState |= ProgramState.RecordSeries;
}
isChanged |= (int)state != (int)newState;
state = (int)newState;
}
}
/// <summary>
/// The original air date of the program
/// </summary>
public DateTime OriginalAirDate
{
get { return originalAirDate; }
set
{
isChanged |= originalAirDate != value;
originalAirDate = value;
}
}
/// <summary>
/// The programs series id
/// </summary>
public string SeriesNum
{
get { return seriesNum; }
set
{
isChanged |= seriesNum != value;
seriesNum = value;
}
}
/// <summary>
/// The programs episode number from the distributor
/// </summary>
public string EpisodeNum
{
get { return episodeNum; }
set
{
isChanged |= episodeNum != value;
episodeNum = value;
}
}
/// <summary>
/// The programs episode title from the distributor
/// </summary>
public string EpisodeName
{
get { return episodeName; }
set
{
isChanged |= episodeName != value;
episodeName = value;
}
}
/// <summary>
/// The programs episode part from the distributor
/// </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>
/// A rating for a movie (EPG standard is 0/5 stars = 10)
/// </summary>
public int StarRating
{
get { return starRating; }
set
{
isChanged |= starRating != value;
starRating = value;
}
}
/// <summary>
/// A rating for a program (e.g. USK, FSK, etc)
/// </summary>
public string Classification
{
get { return classification; }
set
{
isChanged |= classification != value;
classification = value;
}
}
/// <summary>
/// A parental guidance rating for a program (the nr gives the min. age suggested to watch this program)
/// </summary>
public int ParentalRating
{
get { return parentalRating; }
set
{
isChanged |= parentalRating != value;
parentalRating = value;
}
}
#endregion
#region Storage and Retrieval
#region SQL constraint helpers
private static void AddTimeRangeConstraint(SqlBuilder sb, string startField, string endField, string startParam,
string endParam, bool crossMidnight)
{
string provider = ProviderFactory.GetDefaultProvider().Name.ToLowerInvariant();
string baseConstraint;
switch (provider)
{
case "mysql":
baseConstraint =
string.Format("({0} < DATE_ADD(ADDDATE(DATE({0}), {4}{{0}}), INTERVAL TIME(?{3}) HOUR_SECOND)" +
" AND {1} > DATE_ADD(ADDDATE(DATE({0}), 0{{0}}), INTERVAL TIME(?{2}) HOUR_SECOND))",
startField, endField, startParam, endParam, crossMidnight ? 1 : 0);
break;
case "sqlserver":
baseConstraint = string.Format("({0} < DateAdd(Day, DateDiff(Day, @{2}, {0}){{0}}, @{3})" +
"AND {1} > DateAdd(Day, DateDiff(Day, @{2}, {0}){{0}}, @{2}))",
startField, endField, startParam, endParam);
break;
default:
return;
}
sb.AddConstraint("(" + string.Format(baseConstraint, "") +
" OR " + string.Format(baseConstraint, "+1") +
" OR " + string.Format(baseConstraint, "-1") + ")");
}
private static void AddWeekdayConstraint(SqlBuilder sb, string timeField, string days)
{
string provider = ProviderFactory.GetDefaultProvider().Name.ToLowerInvariant();
switch (provider)
{
case "mysql":
sb.AddConstraint(string.Format("DAYOFWEEK({0}) in ({1})", timeField, days));
break;
case "sqlserver":
// DATEPART(dw, {0})+@@DATEFIRST-7 makes sure result is the same regardless of regional settings
sb.AddConstraint(string.Format("(DATEPART(dw, {0})+@@DATEFIRST-7) in ({1})", timeField, days));
break;
}
}
private static void AddWeekendsConstraint(SqlBuilder sb, string timeField)
{
AddWeekdayConstraint(sb, timeField, WeekEndTool.SqlWeekendDays);
}
private static void AddWorkingDaysConstraint(SqlBuilder sb, string timeField)
{
AddWeekdayConstraint(sb, timeField, WeekEndTool.SqlWorkingDays);
}
#endregion
/// <summary>
/// Static method to retrieve all instances that are stored in the database in one call
/// </summary>
public static IList<Program> ListAll()
{
return Broker.RetrieveList<Program>();
}
/// <summary>
/// Retrieves an entity given it's id.
/// </summary>
public static Program 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 (Program), true, "idProgram", id);
return Broker.RetrieveInstance<Program>(key);
}
/// <summary>
/// Retreives the first found instance of a Program given its Title,Start and End Times
/// </summary>
/// <param name="title">Title we wanna look for</param>
/// <param name="startTime">StartTime</param>
/// <param name="endTime">EndTime</param>
/// <returns></returns>
public static Program RetrieveByTitleAndTimes(string title, DateTime startTime, DateTime endTime)
{
//select * from 'foreigntable'
SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof (Program));
// where foreigntable.foreignkey = ourprimarykey
sb.AddConstraint(Operator.Equals, "Title", title);
sb.AddConstraint(Operator.Equals, "startTime", startTime);
sb.AddConstraint(Operator.Equals, "endTime", endTime);
// passing true indicates that we'd like a list of elements, i.e. that no primary key
// constraints from the type being retrieved should be added to the statement
SqlStatement stmt = sb.GetStatement(true);
// execute the statement/query and create a collection of User instances from the result set
IList<Program> result = ObjectFactory.GetCollection<Program>(stmt.Execute());
if (result == null)
{
return null;
}
if (result.Count == 0)
{
return null;
}
return result[0];
// TODO In the end, a GentleList should be returned instead of an arraylist
//return new GentleList( typeof(ChannelMap), this );
}
public static Program RetrieveOnce(string title, DateTime startTime, DateTime endTime, int channelId)
{
return RetrieveByTitleTimesAndChannel(title, startTime, endTime, channelId);
}
public static IList<Program> RetrieveDaily(DateTime startTime, DateTime endTime, int channelId)
{
return RetrieveDaily(startTime, endTime, channelId, -1);
}
public static IList<Program> RetrieveDaily(DateTime startTime, DateTime endTime, int channelId, int maxDays)
{
SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof (Program));
// where foreigntable.foreignkey = ourprimarykey
sb.AddConstraint(Operator.GreaterThanOrEquals, "endTime", DateTime.Now /*startTime*/);
if (maxDays > 0)
{
sb.AddConstraint(Operator.LessThan, "startTime", DateTime.Now.AddDays(maxDays));
}
sb.AddConstraint(Operator.Equals, "idChannel", channelId);
sb.AddParameter("pStartTime", typeof (DateTime));
sb.AddParameter("pEndTime", typeof (DateTime));
AddTimeRangeConstraint(sb, "startTime", "endTime", "pStartTime", "pEndTime", (endTime.Day != startTime.Day));
// passing true indicates that we'd like a list of elements, i.e. that no primary key
// constraints from the type being retrieved should be added to the statement
SqlStatement stmt = sb.GetStatement(true);
stmt.SetParameter("pStartTime", startTime);
stmt.SetParameter("pEndTime", endTime);
// execute the statement/query and create a collection of User instances from the result set
return ObjectFactory.GetCollection<Program>(stmt.Execute());
}
public static IList<Program> RetrieveWeeklyEveryTimeOnThisChannel(DateTime startTime, DateTime endTime, string title,
int channelId)
{
//select * from 'foreigntable'
SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof (Program));
// where foreigntable.foreignkey = ourprimarykey
sb.AddConstraint(Operator.Equals, "Title", title);
sb.AddConstraint(Operator.Equals, "idChannel", channelId);
sb.AddConstraint(Operator.GreaterThanOrEquals, "startTime", DateTime.Now);
sb.AddConstraint(Operator.LessThanOrEquals, "endTime", DateTime.MaxValue);
AddWeekdayConstraint(sb, "startTime", ((int)startTime.DayOfWeek + 1).ToString());
// passing true indicates that we'd like a list of elements, i.e. that no primary key
// constraints from the type being retrieved should be added to the statement
SqlStatement stmt = sb.GetStatement(true);
// execute the statement/query and create a collection of User instances from the result set
return ObjectFactory.GetCollection<Program>(stmt.Execute());
}
public static IList<Program> RetrieveEveryTimeOnEveryChannel(string title)
{
return RetrieveByTitleAndTimesInterval(title, DateTime.Now, DateTime.MaxValue);
}
public static IList<Program> RetrieveEveryTimeOnEveryChannel(string title, int maxDays)
{
return RetrieveByTitleAndTimesInterval(title, DateTime.Now, DateTime.Now.AddDays(maxDays));
}
public static IList<Program> RetrieveEveryTimeOnThisChannel(string title, int channelId)
{
return RetrieveEveryTimeOnThisChannel(title, channelId, -1);
}
public static IList<Program> RetrieveEveryTimeOnThisChannel(string title, int channelId, int maxDays)
{
DateTime startTime = DateTime.Now;
DateTime endTime = maxDays > 0 ? startTime.AddDays(maxDays) : DateTime.MaxValue;
SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof (Program));
// where foreigntable.foreignkey = ourprimarykey
sb.AddConstraint(Operator.Equals, "Title", title);
sb.AddConstraint(Operator.GreaterThanOrEquals, "endTime", startTime);
sb.AddConstraint(Operator.LessThan, "endTime", endTime);
sb.AddConstraint(Operator.Equals, "idChannel", channelId);
// passing true indicates that we'd like a list of elements, i.e. that no primary key
// constraints from the type being retrieved should be added to the statement
SqlStatement stmt = sb.GetStatement(true);
// execute the statement/query and create a collection of User instances from the result set
return ObjectFactory.GetCollection<Program>(stmt.Execute());
}
public static IList<Program> RetrieveWeekends(DateTime startTime, DateTime endTime, int channelId)
{
return RetrieveWeekends(startTime, endTime, channelId, -1);
}
public static IList<Program> RetrieveWeekends(DateTime startTime, DateTime endTime, int channelId, int maxDays)
{
SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof (Program));
// where foreigntable.foreignkey = ourprimarykey
sb.AddConstraint(Operator.GreaterThanOrEquals, "endTime", DateTime.Now /*startTime*/);
if (maxDays > 0)
{
sb.AddConstraint(Operator.LessThan, "startTime", DateTime.Now.AddDays(maxDays));
}
sb.AddConstraint(Operator.Equals, "idChannel", channelId);
sb.AddParameter("pStartTime", typeof (DateTime));
sb.AddParameter("pEndTime", typeof (DateTime));
AddTimeRangeConstraint(sb, "startTime", "endTime", "pStartTime", "pEndTime", (endTime.Day != startTime.Day));
AddWeekendsConstraint(sb, "startTime");
// passing true indicates that we'd like a list of elements, i.e. that no primary key
// constraints from the type being retrieved should be added to the statement
SqlStatement stmt = sb.GetStatement(true);
stmt.SetParameter("pStartTime", startTime);
stmt.SetParameter("pEndTime", endTime);
// execute the statement/query and create a collection of User instances from the result set
return ObjectFactory.GetCollection<Program>(stmt.Execute());
}
public static IList<Program> RetrieveWeekly(DateTime startTime, DateTime endTime, int channelId)
{
return RetrieveWeekly(startTime, endTime, channelId, -1);
}
public static IList<Program> RetrieveWeekly(DateTime startTime, DateTime endTime, int channelId, int maxDays)
{
SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof (Program));
// where foreigntable.foreignkey = ourprimarykey
sb.AddConstraint(Operator.GreaterThanOrEquals, "endTime", DateTime.Now /*startTime*/);
if (maxDays > 0)
{
sb.AddConstraint(Operator.LessThan, "startTime", DateTime.Now.AddDays(maxDays));
}
sb.AddConstraint(Operator.Equals, "idChannel", channelId);
sb.AddParameter("pStartTime", typeof (DateTime));
sb.AddParameter("pEndTime", typeof (DateTime));
AddTimeRangeConstraint(sb, "startTime", "endTime", "pStartTime", "pEndTime", (endTime.Day != startTime.Day)); //
AddWeekdayConstraint(sb, "startTime", ((int)startTime.DayOfWeek + 1).ToString());
// passing true indicates that we'd like a list of elements, i.e. that no primary key
// constraints from the type being retrieved should be added to the statement
SqlStatement stmt = sb.GetStatement(true);
stmt.SetParameter("pStartTime", startTime);
stmt.SetParameter("pEndTime", endTime);
// execute the statement/query and create a collection of User instances from the result set
return ObjectFactory.GetCollection<Program>(stmt.Execute());
}
public static IList<Program> RetrieveWorkingDays(DateTime startTime, DateTime endTime, int channelId)
{
return RetrieveWorkingDays(startTime, endTime, channelId, -1);
}
public static IList<Program> RetrieveWorkingDays(DateTime startTime, DateTime endTime, int channelId, int maxDays)
{
SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof (Program));
// where foreigntable.foreignkey = ourprimarykey
sb.AddConstraint(Operator.GreaterThanOrEquals, "endTime", DateTime.Now /*startTime*/);
if (maxDays > 0)
{
sb.AddConstraint(Operator.LessThan, "startTime", DateTime.Now.AddDays(maxDays));
}
sb.AddConstraint(Operator.Equals, "idChannel", channelId);
sb.AddParameter("pStartTime", typeof (DateTime));
sb.AddParameter("pEndTime", typeof (DateTime));
AddTimeRangeConstraint(sb, "startTime", "endTime", "pStartTime", "pEndTime", (endTime.Day != startTime.Day));
AddWorkingDaysConstraint(sb, "startTime");
// passing true indicates that we'd like a list of elements, i.e. that no primary key
// constraints from the type being retrieved should be added to the statement
SqlStatement stmt = sb.GetStatement(true);
stmt.SetParameter("pStartTime", startTime);
stmt.SetParameter("pEndTime", endTime);
// execute the statement/query and create a collection of User instances from the result set
return ObjectFactory.GetCollection<Program>(stmt.Execute());
}
/// <summary>
/// Retreives the first found instance of a Program given its Title,Start and End Times and referenced channel
/// </summary>
/// <param name="title">Title we wanna look for</param>
/// <param name="startTime">StartTime</param>
/// <param name="endTime">EndTime</param>
/// <param name="channelId">Referenced Channel id</param>
/// <returns></returns>
public static Program RetrieveByTitleTimesAndChannel(string title, DateTime startTime, DateTime endTime,
int channelId)
{
//select * from 'foreigntable'
SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof (Program));
// where foreigntable.foreignkey = ourprimarykey
sb.AddConstraint(Operator.Equals, "Title", title);
sb.AddConstraint(Operator.Equals, "startTime", startTime);
sb.AddConstraint(Operator.Equals, "endTime", endTime);
sb.AddConstraint(Operator.Equals, "idChannel", channelId);
// passing true indicates that we'd like a list of elements, i.e. that no primary key
// constraints from the type being retrieved should be added to the statement
SqlStatement stmt = sb.GetStatement(true);
// execute the statement/query and create a collection of User instances from the result set
IList<Program> result = ObjectFactory.GetCollection<Program>(stmt.Execute());
if (result == null)
{
return null;
}
if (result.Count == 0)
{
return null;
}
return result[0];
// TODO In the end, a GentleList should be returned instead of an arraylist
//return new GentleList( typeof(ChannelMap), this );
}
/// <summary>
/// Retreives the first found instance of a Program given its Title,Start and End Times and referenced channel
/// </summary>
/// <param name="title">Title we wanna look for</param>
/// <param name="startTime">StartTime</param>
/// <param name="endTime">EndTime</param>
/// <param name="channelId">Referenced Channel id</param>
/// <returns></returns>
public static IList<Program> RetrieveListByTitleTimesAndChannel(string title, DateTime startTime, DateTime endTime,
int channelId)
{
//select * from 'foreigntable'
SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof (Program));
// where foreigntable.foreignkey = ourprimarykey
sb.AddConstraint(Operator.Equals, "Title", title);
sb.AddConstraint(Operator.Equals, "startTime", startTime);
sb.AddConstraint(Operator.Equals, "endTime", endTime);
sb.AddConstraint(Operator.Equals, "idChannel", channelId);
// passing true indicates that we'd like a list of elements, i.e. that no primary key
// constraints from the type being retrieved should be added to the statement
SqlStatement stmt = sb.GetStatement(true);
// execute the statement/query and create a collection of User instances from the result set
return ObjectFactory.GetCollection<Program>(stmt.Execute());
}
public static void ResetPendingState(int idProgram)
{
Program prg = Program.Retrieve(idProgram);
if (prg != null)
{
prg.IsRecordingOncePending = false;
prg.IsRecordingSeriesPending = false;
prg.IsPartialRecordingSeriesPending = false;
}
prg.Persist();
}
public static void SetSingleStateSeriesPending(DateTime startTime, int idChannel, string title)
{
//select * from 'foreigntable'
SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof (Program));
// where foreigntable.foreignkey = ourprimarykey
sb.AddConstraint(Operator.Equals, "Title", title);
sb.AddConstraint(Operator.Equals, "startTime", startTime);
sb.AddConstraint(Operator.Equals, "idChannel", idChannel);
// passing true indicates that we'd like a list of elements, i.e. that no primary key
// constraints from the type being retrieved should be added to the statement
SqlStatement stmt = sb.GetStatement(true);
IList<Program> result = ObjectFactory.GetCollection<Program>(stmt.Execute());
if (result == null || result.Count == 0)
{
return;
}
Program prg = result[0];
prg.IsRecordingOncePending = false;
prg.IsRecordingSeriesPending = true;
prg.Persist();
}
public static void ResetAllStates()
{
string sql = "Update Program set state=0 where state<>0;";
SqlStatement stmt = new SqlStatement(StatementType.Update, Broker.Provider.GetCommand(), sql);
stmt.Execute();
Gentle.Common.CacheManager.ClearQueryResultsByType(typeof(Program));
}
public void ClearRecordPendingState()
{
state &=
~(int)
(ProgramState.RecordOncePending | ProgramState.RecordSeriesPending | ProgramState.PartialRecordSeriesPending);
}
public static IList<Program> RetrieveAllNotifications()
{
SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof (Program));
//sb.AddConstraint("state % 2 <> 0"); //odd state numbers are notifications
string notify = Convert.ToString((int)ProgramState.Notify);
sb.AddConstraint("state & " + notify + " = " + notify);
SqlStatement stmt = sb.GetStatement(true);
// execute the statement/query and create a collection of User instances from the result set
return ObjectFactory.GetCollection<Program>(stmt.Execute());
}
/// <summary>
/// Retreives the first found instance of a Program given its Title
/// </summary>
/// <param name="title">Title we wanna look for</param>
/// <returns></returns>
public static IList<Program> RetrieveByTitle(string title)
{
//select * from 'foreigntable'
SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof (Program));
// where foreigntable.foreignkey = ourprimarykey
sb.AddConstraint(Operator.Equals, "Title", title);
// passing true indicates that we'd like a list of elements, i.e. that no primary key
// constraints from the type being retrieved should be added to the statement
SqlStatement stmt = sb.GetStatement(true);
// execute the statement/query and create a collection of User instances from the result set
//return (Program)ObjectFactory.GetCollection(typeof(Program), stmt.Execute())[0];
return ObjectFactory.GetCollection<Program>(stmt.Execute());
// TODO In the end, a GentleList should be returned instead of an arraylist
//return new GentleList( typeof(ChannelMap), this );
}
/// <summary>
/// Retreives any current running Program given its Title , using pre and post recording times
/// </summary>
/// <param name="title">Title we wanna look for</param>
/// <param name="preRec">Pre recording value</param>
/// <param name="postRec">Post recording value</param>
/// <returns></returns>
public static IList<Program> RetrieveCurrentRunningByTitle(string title, int preRec, int postRec)
{
//select * from 'foreigntable'