Files
win8phoneApp/CampusAppWP8/CampusAppWP8/Model/TimeTable/AppointmentModel.cs
2013-11-11 17:46:20 +01:00

420 lines
14 KiB
C#

//-----------------------------------------------------------------------------
// <copyright file="AppointmentModel.cs" company="BTU/IIT">
// Company copyright tag.
// </copyright>
// <author>fiedlchr</author>
// <sience>26.08.2013</sience>
//-----------------------------------------------------------------------------
namespace CampusAppWP8.Model.TimeTable
{
using System;
using System.Collections.Generic;
using CampusAppWP8.Utility;
using CampusAppWP8.Utility.ICSProperties;
/// <summary> Model for appointments. </summary>
/// <remarks> Fiedler, 06.11.2013. </remarks>
public class AppointmentModel
{
/// <summary> The ical object. </summary>
private ICalObject icalObj = null;
/// <summary> Executes the appointment click action. </summary>
/// <remarks> Fiedler, 06.11.2013. </remarks>
/// <param name="sender"> The sender. </param>
public delegate void OnAppointmentClick(AppointmentModel sender);
/// <summary> Occurs when On Click. </summary>
public event OnAppointmentClick OnClick = null;
/// <summary> Initializes a new instance of the <see cref="AppointmentModel" /> class. </summary>
/// <remarks> Fiedler, 06.11.2013. </remarks>
public AppointmentModel()
{
this.icalObj = new ICalObject();
CampusAppWP8.Utility.ICSProperties.Begin newBegin = new CampusAppWP8.Utility.ICSProperties.Begin();
newBegin.Value = ICSTag.VCALENDAR;
CampusAppWP8.Utility.ICSProperties.Version newVersion = new CampusAppWP8.Utility.ICSProperties.Version();
newVersion.Value = 2.0f;
CampusAppWP8.Utility.ICSProperties.ProductID newProdID = new CampusAppWP8.Utility.ICSProperties.ProductID();
newProdID.Value = CampusAppWP8.Resources.AppResources.ApplicationTitle;
this.icalObj.Header = newBegin;
this.icalObj.AddProperty(newVersion);
this.icalObj.AddProperty(newProdID);
}
/// <summary> Initializes a new instance of the AppointmentModel class. </summary>
/// <remarks> Fiedler, 06.11.2013. </remarks>
/// <param name="icsData"> Information describing the ics. </param>
public AppointmentModel(string icsData) : this()
{
this.icalObj = ICSManager.ImportFromICS(icsData);
}
/// <summary> Gets or sets the calendar object. </summary>
/// <value> The calendar object. </value>
public ICalObject CalendarObj
{
get
{
return this.icalObj;
}
set
{
this.icalObj = value;
}
}
/// <summary> Is date. </summary>
/// <remarks> Fiedler, 06.11.2013. </remarks>
/// <param name="date"> The date Date/Time. </param>
/// <param name="daySpan"> (Optional) the day span. </param>
/// <returns> An int. </returns>
public int IsDate(DateTime date, int daySpan = 0)
{
int retValue = -1;
DateTime startTime = this.Start;
DateTime endTime = this.End;
if (startTime != null)
{
int toDaySpan = 0;
while ((toDaySpan <= daySpan) && (retValue == -1))
{
DateTime tempDT = date.AddDays(toDaySpan);
if(startTime.Date <= tempDT.Date && tempDT.Date <= endTime.Date)
//if (tempDT.Date.Equals(startTime.Date) == true)
{
retValue = toDaySpan;
}
toDaySpan++;
}
}
return retValue;
}
/// <summary> Query if 'date' is in range. </summary>
/// <remarks> Fiedler, 11.11.2013. </remarks>
/// <param name="date"> The date Date/Time. </param>
/// <param name="span"> The span. </param>
/// <returns> true if in range, false if not. </returns>
public bool IsInRange(DateTime date, TimeSpan span)
{
bool retValue = false;
if ((this.Start.Subtract(date) <= span) || (this.End.Subtract(date) <= span))
{
retValue = true;
}
return retValue;
}
/// <summary> Intersects the given model. </summary>
/// <remarks> Fiedler, 06.11.2013. </remarks>
/// <param name="model"> The model. </param>
/// <returns> true if it succeeds, false if it fails. </returns>
public bool Intersect(AppointmentModel model)
{
bool retValue = false;
DateTime modelStart = model.Start;
DateTime modelEnd = model.End;
DateTime thisStart = this.Start;
DateTime thisEnd = this.End;
if ((modelStart <= thisStart && thisStart <= modelEnd)
|| (modelStart <= thisEnd && thisEnd <= modelEnd)
|| (thisStart <= modelStart && modelStart <= thisEnd)
|| (thisStart <= modelEnd && modelEnd <= thisEnd))
{
retValue = true;
}
return retValue;
}
/// <summary> Intersects the given model. </summary>
/// <remarks> Fiedler, 06.11.2013. </remarks>
/// <param name="modelList"> List of models. </param>
/// <returns> true if it succeeds, false if it fails. </returns>
public int Intersect(AppointmentModel[] modelList)
{
int retValue = -1;
for (int i = 0; i < modelList.Length; i++)
{
if (this.Equals(modelList[i]) == false && this.Intersect(modelList[i]) == true)
{
retValue = i;
}
}
return retValue;
}
/// <summary> Intersect array. </summary>
/// <remarks> Fiedler, 06.11.2013. </remarks>
/// <param name="modelList"> List of models. </param>
/// <returns> An int[]. </returns>
public int[] IntersectArray(AppointmentModel[] modelList)
{
List<int> retValue = new List<int>();
for (int i = 0; i < modelList.Length; i++)
{
if (this.Equals(modelList[i]) == false && this.Intersect(modelList[i]) == true)
{
retValue.Add(i);
}
}
return retValue.ToArray();
}
/// <summary> Sets a value. </summary>
/// <remarks> Fiedler, 06.11.2013. </remarks>
/// <param name="value"> The value. </param>
public void SetValue(object value)
{
ICalObject vevent = this.GetVEventObj(true);
vevent.AddProperty(value);
}
/// <summary> Gets a value. </summary>
/// <remarks> Fiedler, 06.11.2013. </remarks>
/// <param name="propertyName"> Name of the property. </param>
/// <returns> The value. </returns>
public object GetValue(string propertyName)
{
object retValue = this.GetVEventObj().GetProperty(propertyName);
return retValue;
}
/// <summary> Gets total hours. </summary>
/// <remarks> Fiedler, 06.11.2013. </remarks>
/// <returns> The total hours. </returns>
public double GetTotalHours()
{
return this.End.Subtract(this.Start).TotalHours;
}
/// <summary> Gets total hours. </summary>
/// <remarks> Fiedler, 06.11.2013. </remarks>
/// <param name="date"> The date Date/Time. </param>
/// <returns> The total hours. </returns>
public double GetTotalHours(DateTime date)
{
double retValue = 0;
if (date.Date.Equals(this.Start.Date))
{
if (this.End.Date.Equals(this.Start.Date))
{
retValue = this.GetTotalHours();
}
else
{
retValue = date.Date.AddDays(1).Subtract(this.Start).TotalHours;
}
}
else if (date.Date.Equals(this.End.Date))
{
retValue = this.End.Subtract(date.Date).TotalHours;
}
else
{
if (this.Start.Date <= date && date <= this.End.Date)
{
retValue = 24;
}
else
{
retValue = 0;
}
}
return retValue;
}
/// <summary> Gets v event object. </summary>
/// <remarks> Fiedler, 06.11.2013. </remarks>
/// <exception cref="NullReferenceException">
/// Thrown when a value was unexpectedly null.
/// </exception>
/// <param name="create"> (Optional) the create. </param>
/// <returns> The v event object. </returns>
private ICalObject GetVEventObj(bool create = false)
{
ICalObject retValue = this.icalObj.GetProperty(ICSTag.VEVENT) as ICalObject;
if (retValue == null)
{
if (create == false)
{
throw new NullReferenceException();
}
else
{
retValue = new ICalObject();
Begin newHeader = new Begin();
newHeader.Value = ICSTag.VEVENT;
retValue.Header = newHeader;
this.icalObj.PropertieList.Add(retValue);
}
}
return retValue;
}
/// <summary> Raises the canvas click event. </summary>
/// <remarks> Fiedler, 06.11.2013. </remarks>
/// <param name="sender"> Source of the event. </param>
/// <param name="e"> Event information to send to registered event handlers. </param>
public void OnCanvasClick(object sender, EventArgs e)
{
if (this.OnClick != null)
{
this.OnClick(this);
}
}
// ------------------------------------------------------
/// <summary> Gets the title. </summary>
/// <value> The title. </value>
public string Title
{
get
{
ICalObject eventICal = this.GetVEventObj();
Summary tempSum = eventICal.GetProperty(ICSTag.SUMMARY) as Summary;
return tempSum.Value;
}
}
/// <summary> Gets the date. </summary>
/// <value> The date. </value>
public string Date
{
get
{
string retValue = string.Empty;
ICalObject eventICal = this.GetVEventObj();
DTStart startDT = eventICal.GetProperty(ICSTag.DT_START) as DTStart;
DTEnd endDT = eventICal.GetProperty(ICSTag.DT_END) as DTEnd;
if (startDT != null && endDT != null)
{
if (startDT.Value.Year.Equals(endDT.Value.Year) && startDT.Value.Month.Equals(endDT.Value.Month) && startDT.Value.Day.Equals(endDT.Value.Day))
{
retValue = string.Format("{0:d}, {0:HH:ss} - {1:HH:ss}", startDT.Value, endDT.Value);
}
else
{
retValue = string.Format("{0:d} {0:HH:ss} - {1:d} {1:HH:ss}", startDT.Value, endDT.Value);
}
}
return retValue;
}
}
/// <summary> Gets the location. </summary>
/// <value> The location. </value>
public string Location
{
get
{
string retValue = string.Empty;
ICalObject eventICal = this.GetVEventObj();
Location loc = eventICal.GetProperty(ICSTag.LOCATION) as Location;
if (loc != null)
{
retValue = loc.Value;
}
return retValue;
}
}
/// <summary> Gets the description. </summary>
/// <value> The description. </value>
public string Description
{
get
{
string retValue = string.Empty;
ICalObject eventICal = this.GetVEventObj();
Description desc = eventICal.GetProperty(ICSTag.DESCRIPTION) as Description;
if (desc != null)
{
retValue = desc.Value;
}
return retValue;
}
}
/// <summary> Gets the Date/Time of the start. </summary>
/// <value> The start. </value>
public DateTime Start
{
get
{
ICalObject eventICal = this.GetVEventObj();
DTStart retVal = eventICal.GetProperty(ICSTag.DT_START) as DTStart;
if (retVal != null)
{
return retVal.Value;
}
return DateTime.MinValue;
}
}
/// <summary> Gets the Date/Time of the end. </summary>
/// <value> The end. </value>
public DateTime End
{
get
{
ICalObject eventICal = this.GetVEventObj();
DTEnd end = eventICal.GetProperty(ICSTag.DT_END) as DTEnd;
CampusAppWP8.Utility.ICSProperties.Duration dur = eventICal.GetProperty(ICSTag.DURATION) as CampusAppWP8.Utility.ICSProperties.Duration;
if (end == null && dur == null)
{
throw new KeyNotFoundException();
}
else if (end != null)
{
return end.Value;
}
else
{
return this.Start.Add(dur.Value);
}
}
}
}
}