Files
win8phoneApp/CampusAppWP8/CampusAppWP8/Model/MainModel.cs
2013-10-15 11:28:01 +02:00

212 lines
7.1 KiB
C#

//-----------------------------------------------------------------------
// <copyright file="MainModel.cs" company="BTU/IIT">
// The MIT License (MIT). Copyright (c) 2013 BTU/IIT.
// </copyright>
// <author>fiedlchr</author>
// <date>15.10.2013</date>
// <summary>Implements the main model class</summary>
//-----------------------------------------------------------------------
namespace CampusAppWP8.Model
{
using System;
using System.IO;
using System.Net;
using CampusAppWP8.Utility;
using CampusAppWPortalLib8.Model;
/// <summary> Base model io handling class. </summary>
/// <remarks> fiedlchr, 15.10.2013. </remarks>
/// <seealso cref="T:CampusAppWPortalLib8.Model.AbstractMainModel{T}"/>
public abstract class MainModel<T> : AbstractMainModel<T>
{
#region Constructor
/// <summary> Initializes a new instance of the <see cref="MainModel{T}" /> class. </summary>
/// <remarks> fiedlchr, 15.10.2013. </remarks>
/// <param name="modelType"> Model IO type. </param>
/// <param name="fileName"> name of the file. </param>
/// <param name="url"> url of the feed. </param>
public MainModel(ModelType modelType, string fileName, string url)
: base(modelType, fileName, url)
{
}
/// <summary> Initializes a new instance of the <see cref="MainModel{T}" /> class. </summary>
/// <remarks> fiedlchr, 15.10.2013. </remarks>
/// <param name="modelType"> Model IO type. </param>
/// <param name="sourceName"> name of the file or the url of the feed. </param>
public MainModel(ModelType modelType, string sourceName)
: base(modelType, sourceName)
{
}
#endregion
#region Events
/// <summary> Delegate of the IsFileUpToDate callback function. </summary>
/// <remarks> fiedlchr, 15.10.2013. </remarks>
/// <param name="model"> data model. </param>
/// <param name="fileInfo"> info of the file. </param>
/// <returns> true, is file is up to date. </returns>
public delegate bool IsFileUpToDate(T model, FileInfo fileInfo);
/// <summary> Callback pointer, for checking if file is up to date at loading. </summary>
public event IsFileUpToDate IsFileUpToDateOnLoad = null;
/// <summary> Callback pointer, for checking if file is up to date at saving. </summary>
public event IsFileUpToDate IsFileUpToDateOnSave = null;
#endregion
#region property
/// <summary> Gets the file. </summary>
/// <value> The file. </value>
public new CampusAppWP8.Utility.File File
{
get
{
return (CampusAppWP8.Utility.File)base.File;
}
protected set
{
base.File = value;
}
}
/// <summary> Gets the api. </summary>
/// <value> The API. </value>
public new HttpRequest Api
{
get
{
return (HttpRequest)base.Api;
}
protected set
{
base.Api = value;
}
}
#endregion
#region Method
#region protected
/// <summary> Method overrides the base CheckLoadFileIsNotUpToDate Method. </summary>
/// <remarks> fiedlchr, 15.10.2013. </remarks>
/// <returns> true if it is not up-to-date, otherwise false. </returns>
protected override bool CheckLoadFileIsNotUpToDate()
{
return this.CheckIsNotUpToDate(this.IsFileUpToDateOnLoad);
}
/// <summary> Method overrides the base CheckSaveFileIsNotUpToDate Method. </summary>
/// <remarks> fiedlchr, 15.10.2013. </remarks>
/// <returns> true if it is not up-to-date, otherwise false. </returns>
protected override bool CheckSaveFileIsNotUpToDate()
{
return this.CheckIsNotUpToDate(this.IsFileUpToDateOnSave);
}
/// <summary> Method overrides the base SendHttpGet Method. </summary>
/// <remarks> fiedlchr, 15.10.2013. </remarks>
/// <param name="url"> the url. </param>
protected override void SendHttpGet(Uri url)
{
((HttpRequest)this.Api).HttpGet(url, this.OnLoadDataComplete);
}
/// <summary> Initializes the file object. </summary>
/// <remarks> fiedlchr, 15.10.2013. </remarks>
protected override void InitFile()
{
if ((this.IsFile() == true)
&& (this.File == null))
{
this.File = new CampusAppWP8.Utility.File(this.FileName);
}
}
/// <summary> Initializes the web object. </summary>
/// <remarks> fiedlchr, 15.10.2013. </remarks>
protected override void InitHttpApi()
{
if ((this.IsHttpApi() == true)
&& (this.Api == null))
{
this.Api = new HttpRequest(this.HttpApiUri);
}
}
/// <summary> Check if model or file is not up to date. </summary>
/// <remarks> fiedlchr, 15.10.2013. </remarks>
/// <param name="checkFunc"> The check function. </param>
/// <returns> true if model or file is not up to date, false if it is. </returns>
protected override bool CheckIsNotUpToDate(object checkFunc)
{
bool retValue = false;
// if there is no check function, the model or file is not up to date
if (checkFunc == null)
{
retValue = true;
}
else
{
Type funcType = checkFunc.GetType();
if (funcType.Equals(typeof(IsFileUpToDate)))
{
retValue = !(checkFunc as IsFileUpToDate)(this.Model, this.File.GetFileInfo());
}
else if (funcType.Equals(typeof(IsModelUpToDate)))
{
retValue = !(checkFunc as IsModelUpToDate)(this.Model);
}
}
return retValue;
}
#endregion
#region private
/// <summary> Is called after the loading from web is complete. </summary>
/// <remarks> fiedlchr, 15.10.2013. </remarks>
/// <param name="sender"> sending object. </param>
/// <param name="e"> event args. </param>
private void OnLoadDataComplete(object sender, OpenReadCompletedEventArgs e)
{
Exception downloadError = e.Error;
if (downloadError != null)
{
this.FireLoadFailEvents();
}
else
{
byte[] data;
using (MemoryStream ms = new MemoryStream())
{
e.Result.CopyTo(ms);
data = ms.ToArray();
}
if (data != null && data.Length > 0)
{
this.DeserializeModel(data);
}
this.FireLoadCompletedEvents();
}
}
#endregion
#endregion
}
}