using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Web; using EnVisage.Code; using EnVisage.Code.BLL; namespace EnVisage.Models { public class HolidayModel : IBaseModel { #region Enums /// /// Status of the Calendar Settings record /// public enum HolidayStatus { /// /// NoValue = 0. /// [DisplayValue("No Value")] NoValue = 0, /// /// Inactive = 0. /// [DisplayValue("Inactive")] Inactive = 1, /// /// Active = 1. /// [DisplayValue("Active")] Active = 2, /// /// Inactive = 2. /// [DisplayValue("Future")] Future = 3, } public enum HolidayOccurrence { [DisplayValue("Each")] SameDayEveryYear = 0, [DisplayValue("First")] FirstDayOfWeek = 1, [DisplayValue("Second")] SecondDayOfWeek = 2, [DisplayValue("Third")] ThirdDayOfWeek = 3, [DisplayValue("Fourth")] FourthDayOfWeek = 4, [DisplayValue("Last")] LastDayOfWeek = 5, [DisplayValue("Varies every year")] VariesEveryYear = 6, [DisplayValue("Only once")] OnlyOnce = 7, } #endregion #region Constructors public HolidayModel() { Id = Guid.Empty; HolidayId = Guid.Empty; Status = HolidayStatus.NoValue; CreatedAt = DateTime.UtcNow; } #endregion #region Properties public Guid Id { get; set; } public Guid HolidayId { get; set; } /// /// Gets or sets a name of the holiday. /// [Required] [DataType(DataType.Text)] [StringLength(50)] [Display(Name = "Name")] public string Name { get; set; } /// /// Gets or sets an occurrence of the holiday each year. /// [Required] [Display(Name = "Occurrence")] public HolidayOccurrence? OccurrenceType { get; set; } /// /// Gets or sets a week day of holiday. /// [Display(Name = "Occurrence day of week")] public DayOfWeek? OccurrenceWeekDay { get; set; } /// /// Gets or sets a day of holiday. /// [Display(Name = "Occurrence day of month")] [Range(1, 31)] public short? OccurrenceMonthDay { get; set; } /// /// Gets or sets a month of holiday. /// [Display(Name = "of")] [Range(1, 12)] public short? OccurrenceMonth { get; set; } /// /// Gets or sets a value indicating whether a holiday is a working day or not. /// [Display(Name = "Working Day")] public bool WorkingDay { get; set; } /// /// Gets or sets a value indicating whether a holiday results in non-working week or not. /// [Display(Name = "Non-Work Week")] public bool NonWorkingWeek { get; set; } [Required] [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:MM/dd/yy}", ApplyFormatInEditMode = true)] [Display(Name = "Effective Date of Change")] public DateTime? EffectiveChangeDate { get; set; } [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:MM/dd/yy}", ApplyFormatInEditMode = true)] [Display(Name = "Valid To")] public DateTime? ValidTo { get; set; } public DateTime CreatedAt { get; set; } [Display(Name = "Status")] public HolidayStatus Status { get; set; } public bool ShowWeekDayDdl { get { return OccurrenceType.HasValue && OccurrenceType.Value == HolidayOccurrence.FirstDayOfWeek && OccurrenceType.Value == HolidayOccurrence.SecondDayOfWeek && OccurrenceType.Value == HolidayOccurrence.ThirdDayOfWeek && OccurrenceType.Value == HolidayOccurrence.FourthDayOfWeek && OccurrenceType.Value == HolidayOccurrence.LastDayOfWeek && OccurrenceType.Value == HolidayOccurrence.VariesEveryYear && OccurrenceType.Value == HolidayOccurrence.OnlyOnce; } } #endregion #region Methods /// /// Determines whether the specified object is valid. /// /// The validation context. /// A collection that holds failed-validation information. public IEnumerable Validate(ValidationContext validationContext) { if (OccurrenceType != null) switch (OccurrenceType) { case HolidayOccurrence.SameDayEveryYear: case HolidayOccurrence.OnlyOnce: case HolidayOccurrence.VariesEveryYear: switch (OccurrenceMonth) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (OccurrenceMonthDay > 31) yield return new ValidationResult(string.Format(Constants.ERROR_RANGE_MAX_VALUE_TEMPLATE, "Occurrence day of month", 31), new[] { "OccurrenceMonthDay" }); break; case 2: if (OccurrenceMonthDay > 28) yield return new ValidationResult(string.Format(Constants.ERROR_RANGE_MAX_VALUE_TEMPLATE, "Occurrence day of month", 28), new[] { "OccurrenceMonthDay" }); break; default: if (OccurrenceMonthDay > 30) yield return new ValidationResult(string.Format(Constants.ERROR_RANGE_MAX_VALUE_TEMPLATE, "Occurrence day of month", 30), new[] { "OccurrenceMonthDay" }); break; } yield break; default: break; } } /// /// Casts a obect to the object of type . /// /// A object. /// A object filled with data from db. public static explicit operator HolidayModel(Holiday obj) { if (obj == null) return null; var model = new HolidayModel { Id = obj.Id, HolidayId = obj.HolidayId, Name = obj.Name, NonWorkingWeek = obj.NonWorkingWeek, OccurrenceType = (HolidayOccurrence)obj.OccurrenceType, OccurrenceMonth = obj.OccurrenceMonth, OccurrenceMonthDay = obj.OccurrenceMonthDay, OccurrenceWeekDay = (DayOfWeek)obj.OccurrenceWeekDay, WorkingDay = obj.WorkingDay, EffectiveChangeDate = obj.EffectiveChangeDate, CreatedAt = obj.CreatedAt }; model.TrimStringProperties(); return model; } /// /// Copies data from model to DAL object. /// /// A target DAL object. public void CopyTo(Holiday holiday) { if (holiday == null) throw new ArgumentNullException("holiday"); if (!EffectiveChangeDate.HasValue) throw new Exception("Effective Date of Change must be set"); holiday.Id = Id; holiday.HolidayId = HolidayId; holiday.Name = Name; holiday.OccurrenceMonth = OccurrenceMonth ?? 1; holiday.OccurrenceMonthDay = OccurrenceMonthDay ?? 1; holiday.OccurrenceType = (short)(OccurrenceType ?? HolidayOccurrence.SameDayEveryYear); holiday.OccurrenceWeekDay = (short)(OccurrenceWeekDay ?? DayOfWeek.Monday); holiday.WorkingDay = WorkingDay; holiday.NonWorkingWeek = NonWorkingWeek; holiday.EffectiveChangeDate = EffectiveChangeDate.Value; holiday.CreatedAt = CreatedAt; } public static DateTime GetHolidayDate(HolidayOccurrence occurrenceType, short month, short day, short weekDay) { DateTime date = DateTime.Today; switch (occurrenceType) { case HolidayOccurrence.OnlyOnce: case HolidayOccurrence.VariesEveryYear: case HolidayOccurrence.SameDayEveryYear: return new DateTime(DateTime.Today.Year, month, day); case HolidayOccurrence.FirstDayOfWeek: date = Utils.GetNextDateByWeekDate (new DateTime(DateTime.Today.Year, month, 1), weekDay, 1); break; case HolidayOccurrence.SecondDayOfWeek: date = Utils.GetNextDateByWeekDate(new DateTime(DateTime.Today.Year, month, 1), weekDay, 2); break; case HolidayOccurrence.ThirdDayOfWeek: date = Utils.GetNextDateByWeekDate(new DateTime(DateTime.Today.Year, month, 1), weekDay, 3); break; case HolidayOccurrence.FourthDayOfWeek: date = Utils.GetNextDateByWeekDate(new DateTime(DateTime.Today.Year, month, 1), weekDay, 4); break; case HolidayOccurrence.LastDayOfWeek: date = Utils.GetLastDateByWeekDate(new DateTime(DateTime.Today.Year, month, 1), weekDay); break; } return date; } public static string GetOccurenceText(HolidayModel item) { if (item == null) return String.Empty; var sb = new StringBuilder(); sb.Append((item.OccurrenceType.Value).ToDisplayValue() + " "); switch (item.OccurrenceType.Value) { case HolidayModel.HolidayOccurrence.OnlyOnce: case HolidayModel.HolidayOccurrence.SameDayEveryYear: case HolidayModel.HolidayOccurrence.VariesEveryYear: switch (item.OccurrenceMonthDay % 10) { case 2: sb.Append("2nd "); break; case 3: sb.Append("3rd "); break; default: sb.Append(item.OccurrenceMonthDay.Value + "th "); break; } sb.Append("of " + new DateTime(DateTime.Today.Year, item.OccurrenceMonth.Value, 1).ToString("MMMM") + " every year"); break; default: sb.Append(" " + (item.OccurrenceWeekDay.Value).ToString() + " of " + new DateTime(DateTime.Today.Year, item.OccurrenceMonth.Value, 1).ToString("MMMM") + " every year"); break; } string result = sb.ToString(); return result; } #endregion } public class HolidayDisplayModel { #region Properties public Guid Id { get; set; } public Guid HolidayId { get; set; } /// /// Gets or sets a name of the holiday. /// [DataType(DataType.Text)] [Display(Name = "Name")] public string Name { get; set; } public DateTime Date { get; set; } [Display(Name = "Date")] public string DateText { get; set; } /// /// Gets or sets a value indicating whether a holiday is a working day or not. /// [Display(Name = "Working Day")] public bool WorkingDay { get; set; } /// /// Gets or sets a value indicating whether a holiday results in non-working week or not. /// [Display(Name = "Non-Work Week")] public bool NonWorkingWeek { get; set; } public DateTime EffectiveChangeDate { get; set; } [Display(Name = "Effective Date of Change")] public string EffectiveChangeDateAsText { get; set; } public DateTime? ValidTo { get; set; } [Display(Name = "Valid To")] public string ValidToAsText { get; set; } #endregion } public class HolidayListModel { public HolidayListModel() { Items = new List(); } public List Items { get; set; } } }