#region Fields and Properties
private DateTime _startDate;
private DateTime _endDate;
public DateTime StartDate
{
get { return _startDate; }
}
public DateTime EndDate
{
get { return _endDate; }
}
#endregion
#region Constructors
public ReportContext(HttpContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
bool hasStartDate = GetStartTime(request.QueryString["s"], out _startDate);
_endDate = GetEndTime(request.QueryString["e"], hasStartDate);
}
#endregion
#region Singleton
//this is a pseudo singleton, but since it's per request, there should never be a problem
public static ReportContext Current
{
get
{
HttpContext context = HttpContext.Current;
if (context == null)
{
throw new InvalidOperationException("ReportContext can only be used from within a http context");
}
ReportContext reportContext = (ReportContext)HttpContext.Current.Items["ReportContext"];
if (reportContext == null)
{
reportContext = new ReportContext(context);
HttpContext.Current.Items["ReportContext"] = reportContext;
}
return reportContext;
}
}
#endregion
#region Private Methods
private bool GetStartTime(string startDateString, out DateTime startDate)
{
if (string.IsNullOrEmpty(startDateString) || !DateTime.TryParseExact(startDateString, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out startDate))
{
startDate = DateTime.Now.AddDays(-5);
return false;
}
return true;
}
private DateTime? GetEndTime(string endDateString, bool hasStartDate)
{
DateTime endDate;
if (string.IsNullOrEmpty(endDateString) || !DateTime.TryParseExact(endDateString, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out endDate))
{
if (hasStartDate)
{
return null;
}
return DateTime.Now;
}
return endDate;
}
#endregion
}