using System; using System.Collections.Generic; using System.Linq; using MongoDB.Bson.Serialization.Attributes; using Newtonsoft.Json; namespace bn.pds.ren.RENAPI.objects.ren { public class Override { public const string DefaultSetName = "Ren Generated"; public Guid Id { get; set; } [BsonDateTimeOptions(Kind = DateTimeKind.Local)] public DateTime StartDate { get; set; } [BsonDateTimeOptions(Kind = DateTimeKind.Local)] public DateTime EndDate { get; set; } [BsonDateTimeOptions(Kind = DateTimeKind.Local)] public DateTime LastUpdated { get; set; } public List Recs { get; set; } public string SetName { get; set; } public bool IsImported { get; set; } // flag that indicates when recommendations will be updated // from aggregation or are a fixed snapshot. public bool AllowAutoFill { get; set; } [BsonIgnore] public bool IsActive { get { DateTime now = DateTime.Now; if (StartDate <= now && now <= EndDate) return true; else return false; } } [BsonIgnore] public bool IsExpired { get { if (DateTime.Now > EndDate) return true; else return false; } } [BsonIgnore] public bool IsFuture { get { if (DateTime.Now < StartDate) return true; else return false; } } [BsonIgnore] public bool IsDefault { get { return SetName.Equals(DefaultSetName); } } public static Override CreateDefaultOverrideSet(List existingRecs) { return new Override(existingRecs); } // 02-March-2016: Marc Simkin // We need to set a default private constructor, so that JsonConvert.Deserialize can properly // deserialize the object. [JsonConstructor] private Override() { } protected Override(List existingRecs) { Id = Guid.NewGuid(); AllowAutoFill = true; StartDate = DateTime.MinValue; EndDate = DateTime.MaxValue; Recs = existingRecs; IsImported = false; SetName = DefaultSetName; } public Override(Guid id, DateTime start, DateTime end, bool allowAutoFill, List existingRecs, DateTime lastUpdated, string setName, bool isImported = false) { if (id == Guid.Empty) throw new ArgumentNullException("id"); if (start == DateTime.MinValue || start == DateTime.MaxValue) throw new ArgumentOutOfRangeException("start", "A valid date must be provided."); if (end == DateTime.MinValue || end == DateTime.MaxValue) throw new ArgumentOutOfRangeException("end", "A valid date must be provided."); if (lastUpdated == DateTime.MinValue || lastUpdated == DateTime.MaxValue) throw new ArgumentOutOfRangeException("lastUpdated", "A valid date must be provided."); if (existingRecs == null) throw new ArgumentNullException("existingRecs", "Either a null or empty existingRecs list was provided."); if (string.IsNullOrWhiteSpace(setName)) throw new ArgumentNullException("setName", "Either a null or empty string was provided."); Id = id; Recs = Clone(existingRecs); StartDate = start; EndDate = end; AllowAutoFill = allowAutoFill; LastUpdated = lastUpdated; SetName = setName; IsImported = isImported; } private List Clone(IEnumerable existingRecs) { return (from a in existingRecs select new Rec { Ean = a.Ean, Score = a.Score}).ToList(); } } }