add abstractMainModel
This commit is contained in:
@@ -50,7 +50,7 @@ namespace CampusAppWP8.File.Exams
|
||||
{
|
||||
if (this.storageFile == null)
|
||||
{
|
||||
this.storageFile = await ((CampusAppWP8.Utility.File)(this.file)).AsStorageFile();
|
||||
this.storageFile = await ((CampusAppWP8.Utility.File)(this.File)).AsStorageFile();
|
||||
}
|
||||
|
||||
if (this.storageFile != null)
|
||||
@@ -66,7 +66,7 @@ namespace CampusAppWP8.File.Exams
|
||||
/// <remarks>Stubbfel, 03.09.2013.</remarks>
|
||||
public void SaveAndLaunchFile()
|
||||
{
|
||||
if (this.file.Exist())
|
||||
if (this.File.Exist())
|
||||
{
|
||||
this.LaunchFile();
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ namespace CampusAppWP8.Model
|
||||
/// <remarks>Stubbfel, 03.09.2013.</remarks>
|
||||
/// <param name="modelData">Information describing the model.</param>
|
||||
/// <returns>true if it succeeds, false if it fails.</returns>
|
||||
public override bool DeserializeModel(byte[] modelData)
|
||||
protected override bool DeserializeModel(byte[] modelData)
|
||||
{
|
||||
bool retValue = true;
|
||||
|
||||
@@ -61,7 +61,7 @@ namespace CampusAppWP8.Model
|
||||
/// <summary>Gets the serialize model.</summary>
|
||||
/// <remarks>Stubbfel, 03.09.2013.</remarks>
|
||||
/// <returns>an byte Array.</returns>
|
||||
public override byte[] SerializeModel()
|
||||
protected override byte[] SerializeModel()
|
||||
{
|
||||
return this.Model;
|
||||
}
|
||||
|
||||
@@ -5,62 +5,20 @@
|
||||
// <author>fiedlchr</author>
|
||||
// <sience>05.07.2013</sience>
|
||||
//-----------------------------------------------------------------------------
|
||||
namespace CampusAppWP8
|
||||
namespace CampusAppWP8.Model
|
||||
{
|
||||
using CampusAppWP8.Utility;
|
||||
using CampusAppWPortalLib8.Model;
|
||||
using CampusAppWPortalLib8.Model.Utility;
|
||||
using CampusAppWPortalLib8.Utility;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
|
||||
using CampusAppWP8.Utility;
|
||||
using CampusAppWPortalLib8.Model;
|
||||
|
||||
/// <summary>
|
||||
/// Base model io handling class.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">model type</typeparam>
|
||||
public abstract class MainModel<T> : IMainModel<T>
|
||||
public abstract class MainModel<T> : AbstractMainModel<T>
|
||||
{
|
||||
#region Member
|
||||
|
||||
/// <summary>
|
||||
/// File object.
|
||||
/// </summary>
|
||||
protected CampusAppWP8.Utility.File file = null;
|
||||
|
||||
/// <summary>
|
||||
/// Model io type.
|
||||
/// </summary>
|
||||
private ModelType modelType;
|
||||
|
||||
/// <summary>
|
||||
/// Model object.
|
||||
/// </summary>
|
||||
private T model = default(T);
|
||||
|
||||
/// <summary>
|
||||
/// Web object.
|
||||
/// </summary>
|
||||
private HttpRequest api = null;
|
||||
|
||||
/// <summary>
|
||||
/// Filename of saved data.
|
||||
/// </summary>
|
||||
private string fileName = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Url of the feed data.
|
||||
/// </summary>
|
||||
private Uri httpApiUri = null;
|
||||
|
||||
/// <summary>
|
||||
/// Parameterized uri of the feed.
|
||||
/// </summary>
|
||||
private Uri paramizedUri = null;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>
|
||||
@@ -70,8 +28,8 @@ namespace CampusAppWP8
|
||||
/// <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)
|
||||
{
|
||||
this.Init(modelType, fileName, url);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -80,42 +38,14 @@ namespace CampusAppWP8
|
||||
/// <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)
|
||||
{
|
||||
if (modelType == ModelType.File)
|
||||
{
|
||||
this.Init(modelType, sourceName, string.Empty);
|
||||
}
|
||||
else if (modelType == ModelType.Feed)
|
||||
{
|
||||
this.Init(modelType, string.Empty, sourceName);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotSupportedException("Wrong constructor was called for Feed and File support.");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
/// <summary>
|
||||
/// Delegate of the OnIO callback function.
|
||||
/// </summary>
|
||||
public delegate void OnIO();
|
||||
|
||||
/// <summary>
|
||||
/// Delegate of the OnFailed(File/Web) callback function.
|
||||
/// </summary>
|
||||
public delegate void OnFailed();
|
||||
|
||||
/// <summary>
|
||||
/// Delegate of the IsModelUpToDate callback function.
|
||||
/// </summary>
|
||||
/// <param name="model">data model</param>
|
||||
/// <returns>true, model is up to date</returns>
|
||||
public delegate bool IsModelUpToDate(T model);
|
||||
|
||||
/// <summary>
|
||||
/// Delegate of the IsFileUpToDate callback function.
|
||||
/// </summary>
|
||||
@@ -124,47 +54,6 @@ namespace CampusAppWP8
|
||||
/// <returns>true, is file is up to date</returns>
|
||||
public delegate bool IsFileUpToDate(T model, FileInfo fileInfo);
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, called before loading.
|
||||
/// </summary>
|
||||
public event OnIO OnLoading = null;
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, called after loading.
|
||||
/// </summary>
|
||||
public event OnIO OnLoaded = null;
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, called before saving.
|
||||
/// </summary>
|
||||
public event OnIO OnSaving = null;
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, called after saving.
|
||||
/// </summary>
|
||||
public event OnIO OnSaved = null;
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, called after failed file loading.
|
||||
/// </summary>
|
||||
public event OnFailed OnFailedFile = null;
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, called after failed web loading.
|
||||
/// </summary>
|
||||
public event OnFailed OnFailedWeb = null;
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, called after failed file or web loading, if there
|
||||
/// is no specialized onFailed callback set.
|
||||
/// </summary>
|
||||
public event OnFailed OnFailedLoad = null;
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, called after failed saving data to file.
|
||||
/// </summary>
|
||||
public event OnFailed OnFailedSave = null;
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, for checking if file is up to date at loading.
|
||||
/// </summary>
|
||||
@@ -175,272 +64,123 @@ namespace CampusAppWP8
|
||||
/// </summary>
|
||||
public event IsFileUpToDate IsFileUpToDateOnSave = null;
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, for checking if model is up to date at loading.
|
||||
/// </summary>
|
||||
public event IsModelUpToDate IsModelUpToDateOnLoad = null;
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, for checking if model is up to date at saving.
|
||||
/// (currently unused)
|
||||
/// </summary>
|
||||
#pragma warning disable 0067
|
||||
public event IsModelUpToDate IsModelUpToDateOnSave = null;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Property
|
||||
#region property
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Model.
|
||||
/// Gets or sets the file
|
||||
/// </summary>
|
||||
public T Model
|
||||
public new CampusAppWP8.Utility.File File
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.model;
|
||||
return (CampusAppWP8.Utility.File)base.File;
|
||||
}
|
||||
|
||||
set
|
||||
protected set
|
||||
{
|
||||
this.model = value;
|
||||
base.File = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the api
|
||||
/// </summary>
|
||||
public new HttpRequest Api
|
||||
{
|
||||
get
|
||||
{
|
||||
return (HttpRequest)base.Api;
|
||||
}
|
||||
|
||||
protected set
|
||||
{
|
||||
base.Api = value;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Method
|
||||
|
||||
#region public
|
||||
|
||||
/// <summary>
|
||||
/// Forces a update from web.
|
||||
/// </summary>
|
||||
public void ForceWebUpdate()
|
||||
{
|
||||
this.LoadData(ForceType.FORCE_WEB);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Forces a update from file.
|
||||
/// </summary>
|
||||
public void ForceReadFile()
|
||||
{
|
||||
this.LoadData(ForceType.FORCE_FILE);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load the data if necessary, from web or from file, regarding if
|
||||
/// the file data is up to date.
|
||||
/// </summary>
|
||||
/// <param name="force">if set/not invalid/not default, force to load from web or file</param>
|
||||
public void LoadData(ForceType force = ForceType.INVALID)
|
||||
{
|
||||
this.RunOnIOCallback(this.OnLoading);
|
||||
|
||||
// check which source is used for loading the data
|
||||
if (force == ForceType.INVALID)
|
||||
{
|
||||
// if the model is not up to date
|
||||
if (this.CheckIsNotUpToDate(this.IsModelUpToDateOnLoad) == true)
|
||||
{
|
||||
force = ForceType.FORCE_FILE;
|
||||
|
||||
if (this.file != null)
|
||||
{
|
||||
// if the file does not exist or is size of 0 or is not
|
||||
// up to date, then load from web
|
||||
if ((this.file.Exist() == false)
|
||||
|| (this.file.GetFileInfo().Length == 0)
|
||||
|| (this.CheckIsNotUpToDate(this.IsFileUpToDateOnLoad) == true))
|
||||
{
|
||||
force = ForceType.FORCE_WEB;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// if the file object does not exist, load from web
|
||||
force = ForceType.FORCE_WEB;
|
||||
}
|
||||
|
||||
// if the web object does not exist, load from file
|
||||
if (this.api == null)
|
||||
{
|
||||
force = ForceType.FORCE_FILE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// if it is up to date, nothing has to be loaded
|
||||
this.RunOnIOCallback(this.OnLoaded);
|
||||
}
|
||||
}
|
||||
|
||||
// load from web
|
||||
if (force == ForceType.FORCE_WEB)
|
||||
{
|
||||
if (this.api != null)
|
||||
{
|
||||
if (this.paramizedUri != null)
|
||||
{
|
||||
this.api.HttpGet(this.paramizedUri, this.OnLoadDataComplete);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.api.HttpGet(this.httpApiUri, this.OnLoadDataComplete);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// if web object does not exist, call OnFailed callbacks
|
||||
this.RunOnFailedCallback(this.OnFailedWeb, this.OnFailedLoad);
|
||||
}
|
||||
}
|
||||
|
||||
// load from file
|
||||
if (force == ForceType.FORCE_FILE)
|
||||
{
|
||||
if (this.file != null)
|
||||
{
|
||||
byte[] data = this.file.ReadFile();
|
||||
|
||||
if (data == null)
|
||||
{
|
||||
this.RunOnFailedCallback(this.OnFailedFile, this.OnFailedLoad);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (data.Length > 0)
|
||||
{
|
||||
this.DeserializeModel(data);
|
||||
}
|
||||
|
||||
this.RunOnIOCallback(this.OnLoaded);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// if file object does not exist, call OnFailed callbacks
|
||||
this.RunOnFailedCallback(this.OnFailedFile, this.OnFailedLoad);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Save the model data if necessary.
|
||||
/// </summary>
|
||||
/// <param name="force">force saving. DEFAULT: false</param>
|
||||
public void SaveData(bool force = false)
|
||||
{
|
||||
if ((this.file != null)
|
||||
&& ((this.CheckIsNotUpToDate(this.IsFileUpToDateOnSave) == true) || (force == true)))
|
||||
{
|
||||
this.RunOnIOCallback(this.OnSaving);
|
||||
|
||||
byte[] data = this.SerializeModel();
|
||||
|
||||
if ((this.OnSaved != null) && (this.OnFailedSave != null))
|
||||
{
|
||||
this.file.WriteFile(data, delegate { this.OnSaved(); }, delegate { this.OnFailedSave(); });
|
||||
}
|
||||
else if (this.OnSaved != null)
|
||||
{
|
||||
this.file.WriteFile(data, delegate { this.OnSaved(); }, null);
|
||||
}
|
||||
else if (this.OnFailedSave != null)
|
||||
{
|
||||
this.file.WriteFile(data, null, delegate { this.OnFailedSave(); });
|
||||
}
|
||||
else
|
||||
{
|
||||
this.file.WriteFile(data, null, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the model io type.
|
||||
/// </summary>
|
||||
/// <returns>model io type</returns>
|
||||
public ModelType GetModelType()
|
||||
{
|
||||
return this.modelType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the model.
|
||||
/// </summary>
|
||||
/// <returns>model object</returns>
|
||||
public T GetModel()
|
||||
{
|
||||
return this.model;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create the parameterized uri.
|
||||
/// </summary>
|
||||
/// <param name="parameters">uri parameter list</param>
|
||||
public void SetUriParams(List<UrlParamModel> parameters)
|
||||
{
|
||||
if (this.api != null)
|
||||
{
|
||||
this.paramizedUri = this.api.CreateGetUrl(parameters);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clear the parameterized uri.
|
||||
/// </summary>
|
||||
public void ClearUriParams()
|
||||
{
|
||||
this.paramizedUri = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Abstract declaration of the model deserialize function.
|
||||
/// </summary>
|
||||
/// <param name="modelData">model data as byte array</param>
|
||||
/// <returns>true, is succeeded</returns>
|
||||
public abstract bool DeserializeModel(byte[] modelData);
|
||||
|
||||
/// <summary>
|
||||
/// Abstract declaration of the model serialize function.
|
||||
/// </summary>
|
||||
/// <returns>model data as byte array</returns>
|
||||
public abstract byte[] SerializeModel();
|
||||
|
||||
#endregion
|
||||
|
||||
#region protected
|
||||
|
||||
/// <summary>
|
||||
/// Check if the model io type is file.
|
||||
/// Method overrides the base CheckLoadFileIsNotUpToDate Method
|
||||
/// </summary>
|
||||
/// <returns>true, if the model io type has file.</returns>
|
||||
protected bool IsFile()
|
||||
/// <returns>true if it is not up-to-date, otherwise false</returns>
|
||||
protected override bool CheckLoadFileIsNotUpToDate()
|
||||
{
|
||||
bool retValue = false;
|
||||
|
||||
if ((this.modelType & ModelType.File) != 0)
|
||||
{
|
||||
retValue = true;
|
||||
}
|
||||
|
||||
return retValue;
|
||||
return this.CheckIsNotUpToDate(this.IsFileUpToDateOnLoad);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if the model io type is feed.
|
||||
/// Method overrides the base CheckSaveFileIsNotUpToDate Method
|
||||
/// </summary>
|
||||
/// <returns>true if the model io type has feed.</returns>
|
||||
protected bool IsHttpApi()
|
||||
/// <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>
|
||||
/// <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>
|
||||
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>
|
||||
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>
|
||||
/// <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 ((this.modelType & ModelType.Feed) != 0)
|
||||
// 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;
|
||||
}
|
||||
@@ -449,60 +189,6 @@ namespace CampusAppWP8
|
||||
|
||||
#region private
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the class. Is called by the constructors.
|
||||
/// </summary>
|
||||
/// <param name="modelType">model IO type</param>
|
||||
/// <param name="fileName">name of the data file</param>
|
||||
/// <param name="url">url of the feed data</param>
|
||||
private void Init(ModelType modelType, string fileName, string url)
|
||||
{
|
||||
this.modelType = modelType;
|
||||
|
||||
if ((url != null) && (url.Equals(string.Empty) == false))
|
||||
{
|
||||
this.httpApiUri = new Uri(url, UriKind.Absolute);
|
||||
}
|
||||
|
||||
this.fileName = fileName;
|
||||
|
||||
if ((this.IsFile() == true)
|
||||
&& (fileName.Equals(string.Empty) == false))
|
||||
{
|
||||
this.InitFile();
|
||||
}
|
||||
|
||||
if ((this.IsHttpApi() == true)
|
||||
&& (url.Equals(string.Empty) == false))
|
||||
{
|
||||
this.InitHttpApi();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the file object.
|
||||
/// </summary>
|
||||
private void InitFile()
|
||||
{
|
||||
if ((this.IsFile() == true)
|
||||
&& (this.file == null))
|
||||
{
|
||||
this.file = new CampusAppWP8.Utility.File(this.fileName);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the web object.
|
||||
/// </summary>
|
||||
private void InitHttpApi()
|
||||
{
|
||||
if ((this.IsHttpApi() == true)
|
||||
&& (this.api == null))
|
||||
{
|
||||
this.api = new HttpRequest(this.httpApiUri);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Is called after the loading from web is complete.
|
||||
/// </summary>
|
||||
@@ -513,7 +199,7 @@ namespace CampusAppWP8
|
||||
Exception downloadError = e.Error;
|
||||
if (downloadError != null)
|
||||
{
|
||||
this.RunOnFailedCallback(this.OnFailedWeb, this.OnFailedLoad);
|
||||
this.FireLoadFailEvents();
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -529,66 +215,10 @@ namespace CampusAppWP8
|
||||
this.DeserializeModel(data);
|
||||
}
|
||||
|
||||
this.RunOnIOCallback(this.OnLoaded);
|
||||
this.FireLoadCompletedEvents();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the on i/o callback operation.
|
||||
/// </summary>
|
||||
/// <param name="callbackFunc">The callback function.</param>
|
||||
private void RunOnIOCallback(OnIO callbackFunc)
|
||||
{
|
||||
if (callbackFunc != null)
|
||||
{
|
||||
callbackFunc();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Executes the on failed callback operation.</summary>
|
||||
/// <param name="specialFunc">The special function.</param>
|
||||
/// <param name="defaultFunc">The default function.</param>
|
||||
private void RunOnFailedCallback(OnFailed specialFunc, OnFailed defaultFunc)
|
||||
{
|
||||
if (specialFunc != null)
|
||||
{
|
||||
specialFunc();
|
||||
}
|
||||
else if (defaultFunc != null)
|
||||
{
|
||||
defaultFunc();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Check if model or file is not up to date.</summary>
|
||||
/// <param name="checkFunc">The check function.</param>
|
||||
/// <returns>true if model or file is not up to date, false if it is.</returns>
|
||||
private 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
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -61,7 +61,7 @@ namespace CampusAppWP8.Model
|
||||
/// </summary>
|
||||
/// <param name="modelData">model data</param>
|
||||
/// <returns>true, if succeeded</returns>
|
||||
public override bool DeserializeModel(byte[] modelData)
|
||||
protected override bool DeserializeModel(byte[] modelData)
|
||||
{
|
||||
bool retValue = true;
|
||||
|
||||
@@ -84,7 +84,7 @@ namespace CampusAppWP8.Model
|
||||
/// Serializes the model to a byte array.
|
||||
/// </summary>
|
||||
/// <returns>model data</returns>
|
||||
public override byte[] SerializeModel()
|
||||
protected override byte[] SerializeModel()
|
||||
{
|
||||
byte[] retValue = null;
|
||||
|
||||
|
||||
@@ -70,7 +70,7 @@ namespace CampusAppWP8.Pages.Departments
|
||||
|
||||
if (this.isNewInstance)
|
||||
{
|
||||
if ((DepartmentIndexPage.FavoriteFile == null) || (DepartmentIndexPage.FavoriteFile.GetModel() == null))
|
||||
if ((DepartmentIndexPage.FavoriteFile == null) || (DepartmentIndexPage.FavoriteFile.Model == null))
|
||||
{
|
||||
DepartmentModel tempModel = null;
|
||||
|
||||
@@ -91,7 +91,7 @@ namespace CampusAppWP8.Pages.Departments
|
||||
|
||||
if (this.isSourceSet == false)
|
||||
{
|
||||
this.ContentPanel.ItemsSource = DepartmentIndexPage.GetFavoriteFile().GetModel().Faculties[0].Chairs;
|
||||
this.ContentPanel.ItemsSource = DepartmentIndexPage.GetFavoriteFile().Model.Faculties[0].Chairs;
|
||||
this.isSourceSet = true;
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ namespace CampusAppWP8.Pages.Departments
|
||||
|
||||
if (e.NavigationMode != System.Windows.Navigation.NavigationMode.Back)
|
||||
{
|
||||
App.SaveToIsolatedStorage<DepartmentModel>(Constants.IsolatedStorage_DepartmentFavoriteModel, DepartmentIndexPage.FavoriteFile.GetModel());
|
||||
App.SaveToIsolatedStorage<DepartmentModel>(Constants.IsolatedStorage_DepartmentFavoriteModel, DepartmentIndexPage.FavoriteFile.Model);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -159,7 +159,7 @@ namespace CampusAppWP8.Pages.Departments
|
||||
Button btn = this.lastClickedBtn as Button;
|
||||
TextBlock btnText = btn.Content as TextBlock;
|
||||
|
||||
if (DepartmentIndexPage.GetFavoriteFile().GetModel().Faculties[0].RemoveChair(btnText.Text) == true)
|
||||
if (DepartmentIndexPage.GetFavoriteFile().Model.Faculties[0].RemoveChair(btnText.Text) == true)
|
||||
{
|
||||
MessageBox.Show(AppResources.DeleteSucceeded);
|
||||
}
|
||||
|
||||
@@ -184,7 +184,7 @@ namespace CampusAppWP8.Pages.Departments
|
||||
/// </summary>
|
||||
private void SetupFacultyList()
|
||||
{
|
||||
this.FacultyList.ItemsSource = DepartmentIndexPage.feed.GetModel().Faculties;
|
||||
this.FacultyList.ItemsSource = DepartmentIndexPage.feed.Model.Faculties;
|
||||
this.progressBar.Visibility = Visibility.Collapsed;
|
||||
}
|
||||
|
||||
@@ -193,12 +193,12 @@ namespace CampusAppWP8.Pages.Departments
|
||||
/// </summary>
|
||||
private void CheckFavoriteFeed()
|
||||
{
|
||||
if (DepartmentIndexPage.favorite.GetModel() == null)
|
||||
if (DepartmentIndexPage.favorite.Model == null)
|
||||
{
|
||||
DepartmentIndexPage.favorite.Model = new Model.Departments.DepartmentModel();
|
||||
}
|
||||
|
||||
if (DepartmentIndexPage.favorite.GetModel().Faculties.Count == 0)
|
||||
if (DepartmentIndexPage.favorite.Model.Faculties.Count == 0)
|
||||
{
|
||||
DepartmentIndexPage.favorite.Model.Faculties.Add(new Model.Departments.FacultyModel("favorites"));
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ namespace CampusAppWP8.Pages.Departments
|
||||
|
||||
if (this.isNewInstance)
|
||||
{
|
||||
if ((DepartmentIndexPage.Feed == null) || (DepartmentIndexPage.Feed.GetModel() == null))
|
||||
if ((DepartmentIndexPage.Feed == null) || (DepartmentIndexPage.Feed.Model == null))
|
||||
{
|
||||
DepartmentModel tempModel = null;
|
||||
|
||||
@@ -91,7 +91,7 @@ namespace CampusAppWP8.Pages.Departments
|
||||
|
||||
if (this.isSourceSet == false)
|
||||
{
|
||||
this.DepartmentPivot.ItemsSource = DepartmentIndexPage.GetFeed().GetModel().Faculties;
|
||||
this.DepartmentPivot.ItemsSource = DepartmentIndexPage.GetFeed().Model.Faculties;
|
||||
this.isSourceSet = true;
|
||||
}
|
||||
|
||||
@@ -103,7 +103,7 @@ namespace CampusAppWP8.Pages.Departments
|
||||
int pivotIndexInt = int.Parse(pivotIndex) - 1;
|
||||
|
||||
// if the index is in the range of the array
|
||||
if ((pivotIndexInt >= 0) && (pivotIndexInt < DepartmentIndexPage.GetFeed().GetModel().Faculties.Count()))
|
||||
if ((pivotIndexInt >= 0) && (pivotIndexInt < DepartmentIndexPage.GetFeed().Model.Faculties.Count()))
|
||||
{
|
||||
DepartmentPivot.SelectedIndex = pivotIndexInt;
|
||||
}
|
||||
@@ -126,7 +126,7 @@ namespace CampusAppWP8.Pages.Departments
|
||||
|
||||
if (e.NavigationMode != System.Windows.Navigation.NavigationMode.Back)
|
||||
{
|
||||
App.SaveToIsolatedStorage<DepartmentModel>(Constants.IsolatedStorage_DepartmentModel, DepartmentIndexPage.Feed.GetModel());
|
||||
App.SaveToIsolatedStorage<DepartmentModel>(Constants.IsolatedStorage_DepartmentModel, DepartmentIndexPage.Feed.Model);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,11 +186,11 @@ namespace CampusAppWP8.Pages.Departments
|
||||
Button btn = this.lastClickedBtn as Button;
|
||||
TextBlock btnText = btn.Content as TextBlock;
|
||||
|
||||
Model.Departments.ChairModel tempModel = DepartmentIndexPage.GetFeed().GetModel().Faculties[this.DepartmentPivot.SelectedIndex].GetChairModel(btnText.Text);
|
||||
Model.Departments.ChairModel tempModel = DepartmentIndexPage.GetFeed().Model.Faculties[this.DepartmentPivot.SelectedIndex].GetChairModel(btnText.Text);
|
||||
|
||||
if (tempModel != null)
|
||||
{
|
||||
DepartmentIndexPage.GetFavoriteFile().GetModel().Faculties[0].AddChair(tempModel);
|
||||
DepartmentIndexPage.GetFavoriteFile().Model.Faculties[0].AddChair(tempModel);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ namespace CampusAppWP8.Pages.Events
|
||||
|
||||
if (this.isNewInstance)
|
||||
{
|
||||
if ((EventIndexPage.GetEventFeed() == null) || (EventIndexPage.GetEventFeed().GetModel() == null))
|
||||
if ((EventIndexPage.GetEventFeed() == null) || (EventIndexPage.GetEventFeed().Model == null))
|
||||
{
|
||||
RSSViewModel tempModel = null;
|
||||
|
||||
@@ -160,7 +160,7 @@ namespace CampusAppWP8.Pages.Events
|
||||
|
||||
if (e.NavigationMode != System.Windows.Navigation.NavigationMode.Back)
|
||||
{
|
||||
App.SaveToIsolatedStorage<RSSViewModel>(Constants.IsolatedStorage_EventRSSModel, EventIndexPage.GetEventFeed().GetModel());
|
||||
App.SaveToIsolatedStorage<RSSViewModel>(Constants.IsolatedStorage_EventRSSModel, EventIndexPage.GetEventFeed().Model);
|
||||
}
|
||||
|
||||
if (this.synth != null)
|
||||
@@ -222,7 +222,7 @@ namespace CampusAppWP8.Pages.Events
|
||||
{
|
||||
string ssmlPrompt = "<speak version=\"1.0\" ";
|
||||
ssmlPrompt += "xmlns=\"http://www.w3.org/2001/10/synthesis\" xml:lang=\"de-DE\">";
|
||||
ssmlPrompt += EventIndexPage.GetEventFeed().GetModel().Channel[0].Item[this.EventPivot.SelectedIndex].Text;
|
||||
ssmlPrompt += EventIndexPage.GetEventFeed().Model.Channel[0].Item[this.EventPivot.SelectedIndex].Text;
|
||||
ssmlPrompt += "</speak>";
|
||||
|
||||
this.isInSpeech = true;
|
||||
|
||||
@@ -84,7 +84,7 @@ namespace CampusAppWP8.Pages.News
|
||||
|
||||
if (this.isNewInstance)
|
||||
{
|
||||
if ((NewsIndexPage.Feed == null) || (NewsIndexPage.Feed.GetModel() == null))
|
||||
if ((NewsIndexPage.Feed == null) || (NewsIndexPage.Feed.Model == null))
|
||||
{
|
||||
RSSViewModel tempModel = null;
|
||||
|
||||
@@ -141,7 +141,7 @@ namespace CampusAppWP8.Pages.News
|
||||
|
||||
if (e.NavigationMode != System.Windows.Navigation.NavigationMode.Back)
|
||||
{
|
||||
App.SaveToIsolatedStorage<RSSViewModel>(Constants.IsolatedStorage_NewsRSSModel, NewsIndexPage.GetNewsFeed().GetModel());
|
||||
App.SaveToIsolatedStorage<RSSViewModel>(Constants.IsolatedStorage_NewsRSSModel, NewsIndexPage.GetNewsFeed().Model);
|
||||
}
|
||||
|
||||
if (this.synth != null)
|
||||
@@ -199,7 +199,7 @@ namespace CampusAppWP8.Pages.News
|
||||
{
|
||||
string ssmlPrompt = "<speak version=\"1.0\" ";
|
||||
ssmlPrompt += "xmlns=\"http://www.w3.org/2001/10/synthesis\" xml:lang=\"de-DE\">";
|
||||
ssmlPrompt += NewsIndexPage.GetNewsFeed().GetModel().Channel[0].Item[this.NewsPivot.SelectedIndex].Text;
|
||||
ssmlPrompt += NewsIndexPage.GetNewsFeed().Model.Channel[0].Item[this.NewsPivot.SelectedIndex].Text;
|
||||
ssmlPrompt += "</speak>";
|
||||
|
||||
this.isInSpeech = true;
|
||||
|
||||
@@ -79,7 +79,7 @@ namespace CampusAppWP8.Pages.Openinghours
|
||||
*/
|
||||
if (this.isNewInstance)
|
||||
{
|
||||
if ((this.feed == null) || (this.feed.GetModel() == null))
|
||||
if ((this.feed == null) || (this.feed.Model == null))
|
||||
{
|
||||
OpeninghoursModel tempModel = null;
|
||||
|
||||
@@ -104,7 +104,7 @@ namespace CampusAppWP8.Pages.Openinghours
|
||||
|
||||
if (e.NavigationMode != System.Windows.Navigation.NavigationMode.Back)
|
||||
{
|
||||
App.SaveToIsolatedStorage<OpeninghoursModel>(Constants.IsolatedStorage_OpeninghoursModel, this.feed.GetModel());
|
||||
App.SaveToIsolatedStorage<OpeninghoursModel>(Constants.IsolatedStorage_OpeninghoursModel, this.feed.Model);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -91,7 +91,15 @@ namespace CampusAppWP8.Utility
|
||||
/// <returns>true, if file exists, otherwise false</returns>
|
||||
public override bool Exist()
|
||||
{
|
||||
return this.GetFileInfo().Exists;
|
||||
FileInfo info = this.GetFileInfo();
|
||||
if (info.Exists && info.Length > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Converts this object to a storage file.</summary>
|
||||
|
||||
@@ -12,20 +12,13 @@ namespace CampusAppWP8.Utility
|
||||
using System.Net;
|
||||
using CampusAppWP8.Model.Utility;
|
||||
using CampusAppWPortalLib8.Model.Utility;
|
||||
using CampusAppWPortalLib8.Utility;
|
||||
|
||||
/// <summary>
|
||||
/// Class realize the access of restful HttpRequest
|
||||
/// </summary>
|
||||
public class HttpRequest
|
||||
public class HttpRequest : AbstractHttpRequest
|
||||
{
|
||||
#region Member
|
||||
|
||||
/// <summary>
|
||||
/// BaseAddress of the webClient
|
||||
/// </summary>
|
||||
private string baseAddress;
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>
|
||||
@@ -41,7 +34,7 @@ namespace CampusAppWP8.Utility
|
||||
/// <param name="apiBaseAddress">the url of the HttpRequest base address</param>
|
||||
public HttpRequest(Uri apiBaseAddress)
|
||||
{
|
||||
this.baseAddress = apiBaseAddress.AbsoluteUri;
|
||||
this.BaseAddress = apiBaseAddress.AbsoluteUri;
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -72,29 +65,6 @@ namespace CampusAppWP8.Utility
|
||||
client.OpenReadAsync(url);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Method create the Url for the http-get-method
|
||||
/// </summary>
|
||||
/// <param name="parameters"> list of parameters</param>
|
||||
/// <returns>absolute API-Url include GetParameter</returns>
|
||||
public Uri CreateGetUrl(List<UrlParamModel> parameters)
|
||||
{
|
||||
string paramterStr = string.Empty;
|
||||
string seperator = string.Empty;
|
||||
foreach (UrlParamModel parameter in parameters)
|
||||
{
|
||||
if (string.Empty.Equals(seperator))
|
||||
{
|
||||
seperator = parameter.ParamToken;
|
||||
}
|
||||
|
||||
paramterStr += parameter.ToString();
|
||||
}
|
||||
|
||||
string getUrlStr = this.baseAddress + seperator + paramterStr;
|
||||
return new Uri(getUrlStr, UriKind.Absolute);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Method realize the http-delete-method
|
||||
/// </summary>
|
||||
|
||||
@@ -33,7 +33,6 @@
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Model\IMainModel.cs" />
|
||||
<Compile Include="Model\ForcesTypes.cs" />
|
||||
<Compile Include="Model\Mensa\MealModel.cs" />
|
||||
<Compile Include="Model\Mensa\MenuModel.cs" />
|
||||
@@ -43,6 +42,7 @@
|
||||
<Compile Include="Model\RSS\RSSModel.cs" />
|
||||
<Compile Include="Model\RSS\RSSViewModel.cs" />
|
||||
<Compile Include="Model\Utility\CleanUrlParamModel.cs" />
|
||||
<Compile Include="Model\AbstractMainModel.cs" />
|
||||
<Compile Include="Model\Utility\URLParamModel.cs" />
|
||||
<Compile Include="Model\IXmlModel.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
||||
577
CampusAppWP8/CampusAppWPortalLib8/Model/AbstractMainModel.cs
Normal file
577
CampusAppWP8/CampusAppWPortalLib8/Model/AbstractMainModel.cs
Normal file
@@ -0,0 +1,577 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// <copyright file="AbstractMainModel.cs" company="BTU/IIT">
|
||||
// Company copyright tag.
|
||||
// </copyright>
|
||||
// <author>fiedlchr</author>
|
||||
// <sience>05.07.2013</sience>
|
||||
//-----------------------------------------------------------------------------
|
||||
namespace CampusAppWPortalLib8.Model
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using CampusAppWPortalLib8.Model.Utility;
|
||||
using CampusAppWPortalLib8.Utility;
|
||||
|
||||
/// <summary>
|
||||
/// abstract Base model io handling class.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">model type</typeparam>
|
||||
public abstract class AbstractMainModel<T>
|
||||
{
|
||||
#region Member
|
||||
|
||||
/// <summary>
|
||||
/// File object.
|
||||
/// </summary>
|
||||
private AbstractFile file = null;
|
||||
|
||||
/// <summary>
|
||||
/// Model io type.
|
||||
/// </summary>
|
||||
private ModelType modelType;
|
||||
|
||||
/// <summary>
|
||||
/// Model object.
|
||||
/// </summary>
|
||||
private T model = default(T);
|
||||
|
||||
/// <summary>
|
||||
/// Web object.
|
||||
/// </summary>
|
||||
private AbstractHttpRequest api = null;
|
||||
|
||||
/// <summary>
|
||||
/// Filename of saved data.
|
||||
/// </summary>
|
||||
private string fileName = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Url of the feed data.
|
||||
/// </summary>
|
||||
private Uri httpApiUri = null;
|
||||
|
||||
/// <summary>
|
||||
/// Parameterized uri of the feed.
|
||||
/// </summary>
|
||||
private Uri paramizedUri = null;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AbstractMainModel{T}" /> class.
|
||||
/// </summary>
|
||||
/// <param name="modelType">Model IO type</param>
|
||||
/// <param name="fileName">name of the file</param>
|
||||
/// <param name="url">url of the feed</param>
|
||||
public AbstractMainModel(ModelType modelType, string fileName, string url)
|
||||
{
|
||||
this.Init(modelType, fileName, url);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AbstractMainModel{T}" /> class.
|
||||
/// </summary>
|
||||
/// <param name="modelType">Model IO type</param>
|
||||
/// <param name="sourceName">name of the file or the url of the feed</param>
|
||||
public AbstractMainModel(ModelType modelType, string sourceName)
|
||||
{
|
||||
if (modelType == ModelType.File)
|
||||
{
|
||||
this.Init(modelType, sourceName, string.Empty);
|
||||
}
|
||||
else if (modelType == ModelType.Feed)
|
||||
{
|
||||
this.Init(modelType, string.Empty, sourceName);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotSupportedException("Wrong constructor was called for Feed and File support.");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
/// <summary>
|
||||
/// Delegate of the OnIO callback function.
|
||||
/// </summary>
|
||||
public delegate void OnIO();
|
||||
|
||||
/// <summary>
|
||||
/// Delegate of the OnFailed(File/Web) callback function.
|
||||
/// </summary>
|
||||
public delegate void OnFailed();
|
||||
|
||||
/// <summary>
|
||||
/// Delegate of the IsModelUpToDate callback function.
|
||||
/// </summary>
|
||||
/// <param name="model">data model</param>
|
||||
/// <returns>true, model is up to date</returns>
|
||||
public delegate bool IsModelUpToDate(T model);
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, called before loading.
|
||||
/// </summary>
|
||||
public event OnIO OnLoading = null;
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, called after loading.
|
||||
/// </summary>
|
||||
public event OnIO OnLoaded = null;
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, called before saving.
|
||||
/// </summary>
|
||||
public event OnIO OnSaving = null;
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, called after saving.
|
||||
/// </summary>
|
||||
public event OnIO OnSaved = null;
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, called after failed file loading.
|
||||
/// </summary>
|
||||
public event OnFailed OnFailedFile = null;
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, called after failed web loading.
|
||||
/// </summary>
|
||||
public event OnFailed OnFailedWeb = null;
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, called after failed file or web loading, if there
|
||||
/// is no specialized onFailed callback set.
|
||||
/// </summary>
|
||||
public event OnFailed OnFailedLoad = null;
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, called after failed saving data to file.
|
||||
/// </summary>
|
||||
public event OnFailed OnFailedSave = null;
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, for checking if model is up to date at loading.
|
||||
/// </summary>
|
||||
public event IsModelUpToDate IsModelUpToDateOnLoad = null;
|
||||
|
||||
/// <summary>
|
||||
/// Callback pointer, for checking if model is up to date at saving.
|
||||
/// (currently unused)
|
||||
/// </summary>
|
||||
#pragma warning disable 0067
|
||||
public event IsModelUpToDate IsModelUpToDateOnSave = null;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Property
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Model.
|
||||
/// </summary>
|
||||
public T Model
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.model;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
this.model = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the File.
|
||||
/// </summary>
|
||||
public AbstractFile File
|
||||
{
|
||||
get { return this.file; }
|
||||
protected set { this.file = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the ModelType.
|
||||
/// </summary>
|
||||
public ModelType ModelType
|
||||
{
|
||||
get { return this.modelType; }
|
||||
protected set { this.modelType = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Api.
|
||||
/// </summary>
|
||||
public AbstractHttpRequest Api
|
||||
{
|
||||
get { return this.api; }
|
||||
protected set { this.api = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the ApiUrl.
|
||||
/// </summary>
|
||||
public Uri HttpApiUri
|
||||
{
|
||||
get { return this.httpApiUri; }
|
||||
protected set { this.httpApiUri = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the FileName.
|
||||
/// </summary>
|
||||
public string FileName
|
||||
{
|
||||
get { return this.fileName; }
|
||||
protected set { this.fileName = value; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Method
|
||||
|
||||
#region public
|
||||
|
||||
/// <summary>
|
||||
/// Method Fire all Events for fail load
|
||||
/// </summary>
|
||||
public void FireLoadFailEvents()
|
||||
{
|
||||
this.RunOnFailedCallback(this.OnFailedWeb, this.OnFailedLoad);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Method Fire all Events for load is complete
|
||||
/// </summary>
|
||||
public void FireLoadCompletedEvents()
|
||||
{
|
||||
this.RunOnIOCallback(this.OnLoaded);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Forces a update from web.
|
||||
/// </summary>
|
||||
public void ForceWebUpdate()
|
||||
{
|
||||
this.LoadData(ForceType.FORCE_WEB);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Forces a update from file.
|
||||
/// </summary>
|
||||
public void ForceReadFile()
|
||||
{
|
||||
this.LoadData(ForceType.FORCE_FILE);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load the data if necessary, from web or from file, regarding if
|
||||
/// the file data is up to date.
|
||||
/// </summary>
|
||||
/// <param name="force">if set/not invalid/not default, force to load from web or file</param>
|
||||
public void LoadData(ForceType force = ForceType.INVALID)
|
||||
{
|
||||
this.RunOnIOCallback(this.OnLoading);
|
||||
|
||||
// check which source is used for loading the data
|
||||
if (force == ForceType.INVALID)
|
||||
{
|
||||
// if the model is not up to date
|
||||
if (this.CheckIsNotUpToDate(this.IsModelUpToDateOnLoad) == true)
|
||||
{
|
||||
force = ForceType.FORCE_FILE;
|
||||
|
||||
if (this.file != null)
|
||||
{
|
||||
// if the file does not exist or is size of 0 or is not
|
||||
// up to date, then load from web
|
||||
if ((this.file.Exist() == false)
|
||||
|| (this.CheckLoadFileIsNotUpToDate() == true))
|
||||
{
|
||||
force = ForceType.FORCE_WEB;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// if the file object does not exist, load from web
|
||||
force = ForceType.FORCE_WEB;
|
||||
}
|
||||
|
||||
// if the web object does not exist, load from file
|
||||
if (this.api == null)
|
||||
{
|
||||
force = ForceType.FORCE_FILE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// if it is up to date, nothing has to be loaded
|
||||
this.RunOnIOCallback(this.OnLoaded);
|
||||
}
|
||||
}
|
||||
|
||||
// load from web
|
||||
if (force == ForceType.FORCE_WEB)
|
||||
{
|
||||
if (this.api != null)
|
||||
{
|
||||
if (this.paramizedUri != null)
|
||||
{
|
||||
this.SendHttpGet(this.paramizedUri);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.SendHttpGet(this.httpApiUri);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// if web object does not exist, call OnFailed callbacks
|
||||
this.RunOnFailedCallback(this.OnFailedWeb, this.OnFailedLoad);
|
||||
}
|
||||
}
|
||||
|
||||
// load from file
|
||||
if (force == ForceType.FORCE_FILE)
|
||||
{
|
||||
if (this.file != null)
|
||||
{
|
||||
byte[] data = this.file.ReadFile();
|
||||
|
||||
if (data == null)
|
||||
{
|
||||
this.RunOnFailedCallback(this.OnFailedFile, this.OnFailedLoad);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (data.Length > 0)
|
||||
{
|
||||
this.DeserializeModel(data);
|
||||
}
|
||||
|
||||
this.RunOnIOCallback(this.OnLoaded);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// if file object does not exist, call OnFailed callbacks
|
||||
this.RunOnFailedCallback(this.OnFailedFile, this.OnFailedLoad);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Save the model data if necessary.
|
||||
/// </summary>
|
||||
/// <param name="force">force saving. DEFAULT: false</param>
|
||||
public void SaveData(bool force = false)
|
||||
{
|
||||
if ((this.file != null)
|
||||
&& ((this.CheckSaveFileIsNotUpToDate() == true) || (force == true)))
|
||||
{
|
||||
this.RunOnIOCallback(this.OnSaving);
|
||||
|
||||
byte[] data = this.SerializeModel();
|
||||
|
||||
if ((this.OnSaved != null) && (this.OnFailedSave != null))
|
||||
{
|
||||
this.file.WriteFile(data, delegate { this.OnSaved(); }, delegate { this.OnFailedSave(); });
|
||||
}
|
||||
else if (this.OnSaved != null)
|
||||
{
|
||||
this.file.WriteFile(data, delegate { this.OnSaved(); }, null);
|
||||
}
|
||||
else if (this.OnFailedSave != null)
|
||||
{
|
||||
this.file.WriteFile(data, null, delegate { this.OnFailedSave(); });
|
||||
}
|
||||
else
|
||||
{
|
||||
this.file.WriteFile(data, null, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the model io type.
|
||||
/// </summary>
|
||||
/// <returns>model io type</returns>
|
||||
public ModelType GetModelType()
|
||||
{
|
||||
return this.modelType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create the parameterized uri.
|
||||
/// </summary>
|
||||
/// <param name="parameters">uri parameter list</param>
|
||||
public void SetUriParams(List<UrlParamModel> parameters)
|
||||
{
|
||||
if (this.api != null)
|
||||
{
|
||||
this.paramizedUri = this.api.CreateGetUrl(parameters);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clear the parameterized uri.
|
||||
/// </summary>
|
||||
public void ClearUriParams()
|
||||
{
|
||||
this.paramizedUri = null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region protected
|
||||
|
||||
/// <summary>
|
||||
/// Abstract declaration of the model deserialize function.
|
||||
/// </summary>
|
||||
/// <param name="modelData">model data as byte array</param>
|
||||
/// <returns>true, is succeeded</returns>
|
||||
protected abstract bool DeserializeModel(byte[] modelData);
|
||||
|
||||
/// <summary>
|
||||
/// Abstract declaration of the model serialize function.
|
||||
/// </summary>
|
||||
/// <returns>model data as byte array</returns>
|
||||
protected abstract byte[] SerializeModel();
|
||||
|
||||
/// <summary>
|
||||
/// Method send a HttpGet
|
||||
/// </summary>
|
||||
/// <param name="url">the url</param>
|
||||
protected abstract void SendHttpGet(Uri url);
|
||||
|
||||
/// <summary>
|
||||
/// Method check if model or file is Not up-to-date
|
||||
/// </summary>
|
||||
/// <param name="checkFunc">the check function</param>
|
||||
/// <returns>true if it is not up-to-date, otherwise false</returns>
|
||||
protected abstract bool CheckIsNotUpToDate(object checkFunc);
|
||||
|
||||
/// <summary>
|
||||
/// Method check if file is Not up-to-date for load process
|
||||
/// </summary>
|
||||
/// <returns>true if it is not up-to-date, otherwise false</returns>
|
||||
protected abstract bool CheckLoadFileIsNotUpToDate();
|
||||
|
||||
/// <summary>
|
||||
/// Method check if file is Not up-to-date for load process
|
||||
/// </summary>
|
||||
/// <returns>true if it is not up-to-date, otherwise false</returns>
|
||||
protected abstract bool CheckSaveFileIsNotUpToDate();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the file object.
|
||||
/// </summary>
|
||||
protected abstract void InitFile();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the web object.
|
||||
/// </summary>
|
||||
protected abstract void InitHttpApi();
|
||||
|
||||
/// <summary>
|
||||
/// Check if the model io type is file.
|
||||
/// </summary>
|
||||
/// <returns>true, if the model io type has file.</returns>
|
||||
protected bool IsFile()
|
||||
{
|
||||
bool retValue = false;
|
||||
|
||||
if ((this.modelType & ModelType.File) != 0)
|
||||
{
|
||||
retValue = true;
|
||||
}
|
||||
|
||||
return retValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if the model io type is feed.
|
||||
/// </summary>
|
||||
/// <returns>true if the model io type has feed.</returns>
|
||||
protected bool IsHttpApi()
|
||||
{
|
||||
bool retValue = false;
|
||||
|
||||
if ((this.modelType & ModelType.Feed) != 0)
|
||||
{
|
||||
retValue = true;
|
||||
}
|
||||
|
||||
return retValue;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region private
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the class. Is called by the constructors.
|
||||
/// </summary>
|
||||
/// <param name="modelType">model IO type</param>
|
||||
/// <param name="fileName">name of the data file</param>
|
||||
/// <param name="url">url of the feed data</param>
|
||||
private void Init(ModelType modelType, string fileName, string url)
|
||||
{
|
||||
this.modelType = modelType;
|
||||
|
||||
if ((url != null) && (url.Equals(string.Empty) == false))
|
||||
{
|
||||
this.httpApiUri = new Uri(url, UriKind.Absolute);
|
||||
}
|
||||
|
||||
this.fileName = fileName;
|
||||
|
||||
if ((this.IsFile() == true)
|
||||
&& (fileName.Equals(string.Empty) == false))
|
||||
{
|
||||
this.InitFile();
|
||||
}
|
||||
|
||||
if ((this.IsHttpApi() == true)
|
||||
&& (url.Equals(string.Empty) == false))
|
||||
{
|
||||
this.InitHttpApi();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the on i/o callback operation.
|
||||
/// </summary>
|
||||
/// <param name="callbackFunc">The callback function.</param>
|
||||
private void RunOnIOCallback(OnIO callbackFunc)
|
||||
{
|
||||
if (callbackFunc != null)
|
||||
{
|
||||
callbackFunc();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Executes the on failed callback operation.</summary>
|
||||
/// <param name="specialFunc">The special function.</param>
|
||||
/// <param name="defaultFunc">The default function.</param>
|
||||
private void RunOnFailedCallback(OnFailed specialFunc, OnFailed defaultFunc)
|
||||
{
|
||||
if (specialFunc != null)
|
||||
{
|
||||
specialFunc();
|
||||
}
|
||||
else if (defaultFunc != null)
|
||||
{
|
||||
defaultFunc();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// <copyright file="IMainModel.cs" company="BTU/IIT">
|
||||
// Company copyright tag.
|
||||
// </copyright>
|
||||
// <author>fiedlchr</author>
|
||||
// <sience>05.07.2013</sience>
|
||||
//-----------------------------------------------------------------------------
|
||||
using CampusAppWPortalLib8.Model;
|
||||
namespace CampusAppWPortalLib8.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// Base model io handling class.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">model type</typeparam>
|
||||
public interface IMainModel<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the Model.
|
||||
/// </summary>
|
||||
T Model { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Load the data if necessary, from web or from file, regarding if
|
||||
/// the file data is up to date.
|
||||
/// </summary>
|
||||
/// <param name="force">if set/not invalid/not default, force to load from web or file</param>
|
||||
void LoadData(ForceType force = ForceType.INVALID);
|
||||
|
||||
/// <summary>
|
||||
/// Save the model data if necessary.
|
||||
/// </summary>
|
||||
/// <param name="force">force saving. DEFAULT: false</param>
|
||||
void SaveData(bool force = false);
|
||||
|
||||
/// <summary>
|
||||
/// Abstract declaration of the model deserialize function.
|
||||
/// </summary>
|
||||
/// <param name="modelData">model data as byte array</param>
|
||||
/// <returns>true, is succeeded</returns>
|
||||
bool DeserializeModel(byte[] modelData);
|
||||
|
||||
/// <summary>
|
||||
/// Abstract declaration of the model serialize function.
|
||||
/// </summary>
|
||||
/// <returns>model data as byte array</returns>
|
||||
byte[] SerializeModel();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
<StyleCopSettings Version="105">
|
||||
<GlobalSettings>
|
||||
<CollectionProperty Name="RecognizedWords">
|
||||
<Value>api</Value>
|
||||
<Value>nfc</Value>
|
||||
<Value>param</Value>
|
||||
<Value>qr</Value>
|
||||
|
||||
Reference in New Issue
Block a user