#region Header // ************************************************* // Project: RENAPI // File name: OverrideRec.cs // Developer: Marc Simkin // Creation Date: 07/02/2014 12:02 PM // Last Changed: 07/09/2014 2:54 PM // ************************************************* #endregion #region Using using System; using System.ComponentModel.DataAnnotations; using System.Collections.Generic; using System.Web.Script.Serialization; using MongoDB.Bson.Serialization.Attributes; #endregion namespace bn.pds.ren.RENAPI.objects.ren { public enum OverrideType { NoChange = 0, Exclude = 1, Replace = 2, Move = 3, Add = 4, GlobalExclude = 5, Include = 6, UndoAdd = 7, BatchAdd = 8, GlobalExcludeDelete = 98, SavePoint = 99, Audit = 100 } public class OverrideRec : IValidatableObject { public OverrideRec() { OverrideType = OverrideType.NoChange; } public OverrideRec(long ean, int position) : this() { Ean = ean; Position = position; } public long Ean { get; set; } public int Position { get; set; } public OverrideType OverrideType { get; set; } public string Reason { get; set; } public long Score { get; set; } public int OriginalPosition { get; set; } [BsonIgnore] public bool IsFixed { get; set; } public UserInfo ModifiedBy { get; set; } [BsonDateTimeOptions(Kind = DateTimeKind.Local)] public DateTime ModifiedOn { get; set; } public override string ToString() { return new JavaScriptSerializer().Serialize(this); } public static OverrideRec CreateSavePoint() { return new OverrideRec { Ean = 0, OriginalPosition = 0, OverrideType = OverrideType.SavePoint, Position = 0 }; } public static OverrideRec CreateAuditEntry(UserInfo userInfo, DateTime modifiedTime) { if (userInfo == null) throw new ArgumentNullException("userInfo", "The user info needs to be provided."); if (modifiedTime == DateTime.MinValue || modifiedTime == DateTime.MaxValue) throw new ArgumentNullException("modifiedTime", "A valid modification time must be supplied."); return new OverrideRec { Ean = 0, OriginalPosition = 0, OverrideType = OverrideType.Audit, Position = 0, ModifiedBy = userInfo, ModifiedOn = modifiedTime }; } #region IValidateObject Interface Methods public IEnumerable Validate(ValidationContext validationContext) { // position = rank and should never be 0;check that the required fields are present. if (Position <= 0) { ValidationResult error = new ValidationResult("The Position is required and needs to be greater than 0.", new[] { "Position" }); yield return error; } if (Position <= 0) { ValidationResult error = new ValidationResult("The OriginalPosition is required and needs to be greater than 0.", new[] { "OriginalPosition" }); yield return error; } if (!IsFixed) { if (OverrideType == OverrideType.Move && OriginalPosition <= 0) { ValidationResult error = new ValidationResult("The OriginalPosition needs to be greater than 0 when the operation type is move.", new[] { "OriginalPosition" }); yield return error; } if (OverrideType == OverrideType.Move && OriginalPosition == Position) { ValidationResult error = new ValidationResult("You can't place the recommendation on top of itself.", new[] { "Position" }); yield return error; } } } #endregion IValidateObject Interface Methods } }