EnVisageOnline/Beta/Source/EnVisage/Models/HolidayModel.cs

297 lines
12 KiB
C#

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 : IValidatableObject
{
#region Classes and enums
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 Properties
public Guid Id { get; set; }
/// <summary>
/// Gets or sets a name of the holiday.
/// </summary>
[Required]
[DataType(DataType.Text)]
[StringLength(50)]
[Display(Name = "Name")]
public string Name { get; set; }
/// <summary>
/// Gets or sets an occurrence of the holiday each year.
/// </summary>
[Required]
[Display(Name = "Occurrence")]
public HolidayOccurrence? OccurrenceType { get; set; }
/// <summary>
/// Gets or sets a week day of holiday.
/// </summary>
[Display(Name = "Occurrence day of week")]
public DayOfWeek? OccurrenceWeekDay { get; set; }
/// <summary>
/// Gets or sets a day of holiday.
/// </summary>
[Display(Name = "Occurrence day of month")]
[Range(1, 31)]
public short? OccurrenceMonthDay { get; set; }
/// <summary>
/// Gets or sets a month of holiday.
/// </summary>
[Display(Name = "of")]
[Range(1, 12)]
public short? OccurrenceMonth { get; set; }
/// <summary>
/// Gets or sets a value indicating whether a holiday is a working day or not.
/// </summary>
[Display(Name = "Working Day")]
public bool WorkingDay { get; set; }
/// <summary>
/// Gets or sets a value indicating whether a holiday results in non-working week or not.
/// </summary>
[Display(Name = "Non-Work Week")]
public bool NonWorkingWeek { 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
/// <summary>
/// Determines whether the specified object is valid.
/// </summary>
/// <param name="validationContext">The validation context.</param>
/// <returns>A collection that holds failed-validation information.</returns>
public IEnumerable<ValidationResult> 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;
}
}
/// <summary>
/// Casts a <see cref="Holiday"/> obect to the object of type <see cref="HolidayModel"/>.
/// </summary>
/// <param name="obj">A <see cref="Holiday"/> object.</param>
/// <returns>A <see cref="HolidayModel"/> object filled with data from db.</returns>
public static explicit operator HolidayModel(Holiday obj)
{
if (obj == null)
return null;
var model = new HolidayModel
{
Id = obj.Id,
Name = obj.Name,
NonWorkingWeek = obj.NonWorkingWeek,
OccurrenceType = (HolidayOccurrence)obj.OccurrenceType,
OccurrenceMonth = obj.OccurrenceMonth,
OccurrenceMonthDay = obj.OccurrenceMonthDay,
OccurrenceWeekDay = (DayOfWeek)obj.OccurrenceWeekDay,
WorkingDay = obj.WorkingDay
};
model.TrimStringProperties();
return model;
}
/// <summary>
/// Copies data from model to DAL object.
/// </summary>
/// <param name="holiday">A target DAL object.</param>
public void CopyTo(Holiday holiday)
{
if (holiday == null)
throw new ArgumentNullException();
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;
}
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;
}
#endregion
}
public class HolidayDetailsModel
{
#region Properties
public Guid Id { get; set; }
/// <summary>
/// Gets or sets a name of the holiday.
/// </summary>
[DataType(DataType.Text)]
[Display(Name = "Name")]
public string Name { get; set; }
/// <summary>
/// Gets or sets an occurrence of the holiday each year.
/// </summary>
[Display(Name = "Occurrence")]
public string Occurrence { get; set; }
/// <summary>
/// Gets or sets a value indicating whether a holiday is a working day or not.
/// </summary>
[Display(Name = "Working Day")]
public bool WorkingDay { get; set; }
/// <summary>
/// Gets or sets a value indicating whether a holiday results in non-working week or not.
/// </summary>
[Display(Name = "Non-Work Week")]
public bool NonWorkingWeek { get; set; }
#endregion
#region Methods
/// <summary>
/// Casts a <see cref="Holiday"/> obect to the object of type <see cref="HolidayDetailsModel"/>.
/// </summary>
/// <param name="obj">A <see cref="Holiday"/> object.</param>
/// <returns>A <see cref="HolidayDetailsModel"/> object filled with data from db.</returns>
public static explicit operator HolidayDetailsModel(Holiday obj)
{
if (obj == null)
return null;
var model = new HolidayDetailsModel
{
Id = obj.Id,
Name = obj.Name,
NonWorkingWeek = obj.NonWorkingWeek,
WorkingDay = obj.WorkingDay
};
var sb = new StringBuilder();
sb.Append(((HolidayModel.HolidayOccurrence) obj.OccurrenceType).ToDisplayValue() + " ");
switch ((HolidayModel.HolidayOccurrence) obj.OccurrenceType)
{
case HolidayModel.HolidayOccurrence.OnlyOnce:
case HolidayModel.HolidayOccurrence.SameDayEveryYear:
case HolidayModel.HolidayOccurrence.VariesEveryYear:
switch (obj.OccurrenceMonthDay % 10)
{
case 2:
sb.Append("2nd ");
break;
case 3:
sb.Append("3rd ");
break;
default:
sb.Append(obj.OccurrenceMonthDay + "th ");
break;
}
sb.Append("of " +
new DateTime(DateTime.Today.Year, obj.OccurrenceMonth, 1).ToString("MMMM") + " every year");
break;
default:
sb.Append(" " + ((DayOfWeek)obj.OccurrenceWeekDay).ToString() + " of " +
new DateTime(DateTime.Today.Year, obj.OccurrenceMonth, 1).ToString("MMMM") + " every year");
break;
}
model.Occurrence = sb.ToString();
model.TrimStringProperties();
return model;
}
#endregion
}
}