diff --git a/CampusAppWP8/CampusAppWP8/File/Exams/ExamFile.cs b/CampusAppWP8/CampusAppWP8/File/Exams/ExamFile.cs index 2e7e84b0..d5334565 100644 --- a/CampusAppWP8/CampusAppWP8/File/Exams/ExamFile.cs +++ b/CampusAppWP8/CampusAppWP8/File/Exams/ExamFile.cs @@ -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 /// Stubbfel, 03.09.2013. public void SaveAndLaunchFile() { - if (this.file.Exist()) + if (this.File.Exist()) { this.LaunchFile(); } diff --git a/CampusAppWP8/CampusAppWP8/Model/BinaryModel.cs b/CampusAppWP8/CampusAppWP8/Model/BinaryModel.cs index 24a2e809..9aa6e9b3 100644 --- a/CampusAppWP8/CampusAppWP8/Model/BinaryModel.cs +++ b/CampusAppWP8/CampusAppWP8/Model/BinaryModel.cs @@ -42,7 +42,7 @@ namespace CampusAppWP8.Model /// Stubbfel, 03.09.2013. /// Information describing the model. /// true if it succeeds, false if it fails. - public override bool DeserializeModel(byte[] modelData) + protected override bool DeserializeModel(byte[] modelData) { bool retValue = true; @@ -61,7 +61,7 @@ namespace CampusAppWP8.Model /// Gets the serialize model. /// Stubbfel, 03.09.2013. /// an byte Array. - public override byte[] SerializeModel() + protected override byte[] SerializeModel() { return this.Model; } diff --git a/CampusAppWP8/CampusAppWP8/Model/MainModel.cs b/CampusAppWP8/CampusAppWP8/Model/MainModel.cs index c87bf18c..74abbcac 100644 --- a/CampusAppWP8/CampusAppWP8/Model/MainModel.cs +++ b/CampusAppWP8/CampusAppWP8/Model/MainModel.cs @@ -5,62 +5,20 @@ // fiedlchr // 05.07.2013 //----------------------------------------------------------------------------- -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; + /// /// Base model io handling class. /// /// model type - public abstract class MainModel : IMainModel + public abstract class MainModel : AbstractMainModel { - #region Member - - /// - /// File object. - /// - protected CampusAppWP8.Utility.File file = null; - - /// - /// Model io type. - /// - private ModelType modelType; - - /// - /// Model object. - /// - private T model = default(T); - - /// - /// Web object. - /// - private HttpRequest api = null; - - /// - /// Filename of saved data. - /// - private string fileName = string.Empty; - - /// - /// Url of the feed data. - /// - private Uri httpApiUri = null; - - /// - /// Parameterized uri of the feed. - /// - private Uri paramizedUri = null; - - #endregion - #region Constructor /// @@ -70,8 +28,8 @@ namespace CampusAppWP8 /// name of the file /// url of the feed public MainModel(ModelType modelType, string fileName, string url) + : base(modelType, fileName, url) { - this.Init(modelType, fileName, url); } /// @@ -80,42 +38,14 @@ namespace CampusAppWP8 /// Model IO type /// name of the file or the url of the feed 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 - /// - /// Delegate of the OnIO callback function. - /// - public delegate void OnIO(); - - /// - /// Delegate of the OnFailed(File/Web) callback function. - /// - public delegate void OnFailed(); - - /// - /// Delegate of the IsModelUpToDate callback function. - /// - /// data model - /// true, model is up to date - public delegate bool IsModelUpToDate(T model); - /// /// Delegate of the IsFileUpToDate callback function. /// @@ -124,47 +54,6 @@ namespace CampusAppWP8 /// true, is file is up to date public delegate bool IsFileUpToDate(T model, FileInfo fileInfo); - /// - /// Callback pointer, called before loading. - /// - public event OnIO OnLoading = null; - - /// - /// Callback pointer, called after loading. - /// - public event OnIO OnLoaded = null; - - /// - /// Callback pointer, called before saving. - /// - public event OnIO OnSaving = null; - - /// - /// Callback pointer, called after saving. - /// - public event OnIO OnSaved = null; - - /// - /// Callback pointer, called after failed file loading. - /// - public event OnFailed OnFailedFile = null; - - /// - /// Callback pointer, called after failed web loading. - /// - public event OnFailed OnFailedWeb = null; - - /// - /// Callback pointer, called after failed file or web loading, if there - /// is no specialized onFailed callback set. - /// - public event OnFailed OnFailedLoad = null; - - /// - /// Callback pointer, called after failed saving data to file. - /// - public event OnFailed OnFailedSave = null; - /// /// Callback pointer, for checking if file is up to date at loading. /// @@ -175,272 +64,123 @@ namespace CampusAppWP8 /// public event IsFileUpToDate IsFileUpToDateOnSave = null; - /// - /// Callback pointer, for checking if model is up to date at loading. - /// - public event IsModelUpToDate IsModelUpToDateOnLoad = null; - - /// - /// Callback pointer, for checking if model is up to date at saving. - /// (currently unused) - /// - #pragma warning disable 0067 - public event IsModelUpToDate IsModelUpToDateOnSave = null; - #endregion - #region Property + #region property /// - /// Gets or sets the Model. + /// Gets or sets the file /// - 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; } } + /// + /// Gets or sets the api + /// + public new HttpRequest Api + { + get + { + return (HttpRequest)base.Api; + } + + protected set + { + base.Api = value; + } + } #endregion #region Method - #region public - - /// - /// Forces a update from web. - /// - public void ForceWebUpdate() - { - this.LoadData(ForceType.FORCE_WEB); - } - - /// - /// Forces a update from file. - /// - public void ForceReadFile() - { - this.LoadData(ForceType.FORCE_FILE); - } - - /// - /// Load the data if necessary, from web or from file, regarding if - /// the file data is up to date. - /// - /// if set/not invalid/not default, force to load from web or file - 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); - } - } - } - - /// - /// Save the model data if necessary. - /// - /// force saving. DEFAULT: false - 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); - } - } - } - - /// - /// Return the model io type. - /// - /// model io type - public ModelType GetModelType() - { - return this.modelType; - } - - /// - /// Return the model. - /// - /// model object - public T GetModel() - { - return this.model; - } - - /// - /// Create the parameterized uri. - /// - /// uri parameter list - public void SetUriParams(List parameters) - { - if (this.api != null) - { - this.paramizedUri = this.api.CreateGetUrl(parameters); - } - } - - /// - /// Clear the parameterized uri. - /// - public void ClearUriParams() - { - this.paramizedUri = null; - } - - /// - /// Abstract declaration of the model deserialize function. - /// - /// model data as byte array - /// true, is succeeded - public abstract bool DeserializeModel(byte[] modelData); - - /// - /// Abstract declaration of the model serialize function. - /// - /// model data as byte array - public abstract byte[] SerializeModel(); - - #endregion - #region protected /// - /// Check if the model io type is file. + /// Method overrides the base CheckLoadFileIsNotUpToDate Method /// - /// true, if the model io type has file. - protected bool IsFile() + /// true if it is not up-to-date, otherwise false + protected override bool CheckLoadFileIsNotUpToDate() { - bool retValue = false; - - if ((this.modelType & ModelType.File) != 0) - { - retValue = true; - } - - return retValue; + return this.CheckIsNotUpToDate(this.IsFileUpToDateOnLoad); } /// - /// Check if the model io type is feed. + /// Method overrides the base CheckSaveFileIsNotUpToDate Method /// - /// true if the model io type has feed. - protected bool IsHttpApi() + /// true if it is not up-to-date, otherwise false + protected override bool CheckSaveFileIsNotUpToDate() + { + return this.CheckIsNotUpToDate(this.IsFileUpToDateOnSave); + } + + /// + /// Method overrides the base SendHttpGet Method + /// + /// the url + protected override void SendHttpGet(Uri url) + { + ((HttpRequest)this.Api).HttpGet(url, this.OnLoadDataComplete); + } + + /// + /// Initializes the file object. + /// + protected override void InitFile() + { + if ((this.IsFile() == true) + && (this.File == null)) + { + this.File = new CampusAppWP8.Utility.File(this.FileName); + } + } + + /// + /// Initializes the web object. + /// + protected override void InitHttpApi() + { + if ((this.IsHttpApi() == true) + && (this.Api == null)) + { + this.Api = new HttpRequest(this.HttpApiUri); + } + } + + /// Check if model or file is not up to date. + /// The check function. + /// true if model or file is not up to date, false if it is. + 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 - /// - /// Initialize the class. Is called by the constructors. - /// - /// model IO type - /// name of the data file - /// url of the feed data - 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(); - } - } - - /// - /// Initializes the file object. - /// - private void InitFile() - { - if ((this.IsFile() == true) - && (this.file == null)) - { - this.file = new CampusAppWP8.Utility.File(this.fileName); - } - } - - /// - /// Initializes the web object. - /// - private void InitHttpApi() - { - if ((this.IsHttpApi() == true) - && (this.api == null)) - { - this.api = new HttpRequest(this.httpApiUri); - } - } - /// /// Is called after the loading from web is complete. /// @@ -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(); } } - /// - /// Executes the on i/o callback operation. - /// - /// The callback function. - private void RunOnIOCallback(OnIO callbackFunc) - { - if (callbackFunc != null) - { - callbackFunc(); - } - } - - /// Executes the on failed callback operation. - /// The special function. - /// The default function. - private void RunOnFailedCallback(OnFailed specialFunc, OnFailed defaultFunc) - { - if (specialFunc != null) - { - specialFunc(); - } - else if (defaultFunc != null) - { - defaultFunc(); - } - } - - /// Check if model or file is not up to date. - /// The check function. - /// true if model or file is not up to date, false if it is. - 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 diff --git a/CampusAppWP8/CampusAppWP8/Model/XmlModel.cs b/CampusAppWP8/CampusAppWP8/Model/XmlModel.cs index 83c936ad..972c8f9c 100644 --- a/CampusAppWP8/CampusAppWP8/Model/XmlModel.cs +++ b/CampusAppWP8/CampusAppWP8/Model/XmlModel.cs @@ -61,7 +61,7 @@ namespace CampusAppWP8.Model /// /// model data /// true, if succeeded - 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. /// /// model data - public override byte[] SerializeModel() + protected override byte[] SerializeModel() { byte[] retValue = null; diff --git a/CampusAppWP8/CampusAppWP8/Pages/Departments/DepartmentFavoritePage.xaml.cs b/CampusAppWP8/CampusAppWP8/Pages/Departments/DepartmentFavoritePage.xaml.cs index 0dd73ecd..c6e21ac6 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/Departments/DepartmentFavoritePage.xaml.cs +++ b/CampusAppWP8/CampusAppWP8/Pages/Departments/DepartmentFavoritePage.xaml.cs @@ -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(Constants.IsolatedStorage_DepartmentFavoriteModel, DepartmentIndexPage.FavoriteFile.GetModel()); + App.SaveToIsolatedStorage(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); } diff --git a/CampusAppWP8/CampusAppWP8/Pages/Departments/DepartmentIndexPage.xaml.cs b/CampusAppWP8/CampusAppWP8/Pages/Departments/DepartmentIndexPage.xaml.cs index d161e280..a07fcc3b 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/Departments/DepartmentIndexPage.xaml.cs +++ b/CampusAppWP8/CampusAppWP8/Pages/Departments/DepartmentIndexPage.xaml.cs @@ -184,7 +184,7 @@ namespace CampusAppWP8.Pages.Departments /// 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 /// 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")); } diff --git a/CampusAppWP8/CampusAppWP8/Pages/Departments/DepartmentPage.xaml.cs b/CampusAppWP8/CampusAppWP8/Pages/Departments/DepartmentPage.xaml.cs index d03b97db..95b27075 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/Departments/DepartmentPage.xaml.cs +++ b/CampusAppWP8/CampusAppWP8/Pages/Departments/DepartmentPage.xaml.cs @@ -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(Constants.IsolatedStorage_DepartmentModel, DepartmentIndexPage.Feed.GetModel()); + App.SaveToIsolatedStorage(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); } } diff --git a/CampusAppWP8/CampusAppWP8/Pages/Events/EventPage.xaml.cs b/CampusAppWP8/CampusAppWP8/Pages/Events/EventPage.xaml.cs index 685568fa..c2b20c99 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/Events/EventPage.xaml.cs +++ b/CampusAppWP8/CampusAppWP8/Pages/Events/EventPage.xaml.cs @@ -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(Constants.IsolatedStorage_EventRSSModel, EventIndexPage.GetEventFeed().GetModel()); + App.SaveToIsolatedStorage(Constants.IsolatedStorage_EventRSSModel, EventIndexPage.GetEventFeed().Model); } if (this.synth != null) @@ -222,7 +222,7 @@ namespace CampusAppWP8.Pages.Events { string ssmlPrompt = ""; - ssmlPrompt += EventIndexPage.GetEventFeed().GetModel().Channel[0].Item[this.EventPivot.SelectedIndex].Text; + ssmlPrompt += EventIndexPage.GetEventFeed().Model.Channel[0].Item[this.EventPivot.SelectedIndex].Text; ssmlPrompt += ""; this.isInSpeech = true; diff --git a/CampusAppWP8/CampusAppWP8/Pages/News/NewsPage.xaml.cs b/CampusAppWP8/CampusAppWP8/Pages/News/NewsPage.xaml.cs index d6ab0753..1c5556d2 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/News/NewsPage.xaml.cs +++ b/CampusAppWP8/CampusAppWP8/Pages/News/NewsPage.xaml.cs @@ -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(Constants.IsolatedStorage_NewsRSSModel, NewsIndexPage.GetNewsFeed().GetModel()); + App.SaveToIsolatedStorage(Constants.IsolatedStorage_NewsRSSModel, NewsIndexPage.GetNewsFeed().Model); } if (this.synth != null) @@ -199,7 +199,7 @@ namespace CampusAppWP8.Pages.News { string ssmlPrompt = ""; - ssmlPrompt += NewsIndexPage.GetNewsFeed().GetModel().Channel[0].Item[this.NewsPivot.SelectedIndex].Text; + ssmlPrompt += NewsIndexPage.GetNewsFeed().Model.Channel[0].Item[this.NewsPivot.SelectedIndex].Text; ssmlPrompt += ""; this.isInSpeech = true; diff --git a/CampusAppWP8/CampusAppWP8/Pages/Openinghours/OpeninghoursPage.xaml.cs b/CampusAppWP8/CampusAppWP8/Pages/Openinghours/OpeninghoursPage.xaml.cs index 725f7d2f..d8147b93 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/Openinghours/OpeninghoursPage.xaml.cs +++ b/CampusAppWP8/CampusAppWP8/Pages/Openinghours/OpeninghoursPage.xaml.cs @@ -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(Constants.IsolatedStorage_OpeninghoursModel, this.feed.GetModel()); + App.SaveToIsolatedStorage(Constants.IsolatedStorage_OpeninghoursModel, this.feed.Model); } } diff --git a/CampusAppWP8/CampusAppWP8/Utility/File.cs b/CampusAppWP8/CampusAppWP8/Utility/File.cs index 7b816090..b247c0e7 100644 --- a/CampusAppWP8/CampusAppWP8/Utility/File.cs +++ b/CampusAppWP8/CampusAppWP8/Utility/File.cs @@ -91,7 +91,15 @@ namespace CampusAppWP8.Utility /// true, if file exists, otherwise false public override bool Exist() { - return this.GetFileInfo().Exists; + FileInfo info = this.GetFileInfo(); + if (info.Exists && info.Length > 0) + { + return true; + } + else + { + return false; + } } /// Converts this object to a storage file. diff --git a/CampusAppWP8/CampusAppWP8/Utility/HttpRequest.cs b/CampusAppWP8/CampusAppWP8/Utility/HttpRequest.cs index 2d3b73c1..3d1b2ae5 100644 --- a/CampusAppWP8/CampusAppWP8/Utility/HttpRequest.cs +++ b/CampusAppWP8/CampusAppWP8/Utility/HttpRequest.cs @@ -12,20 +12,13 @@ namespace CampusAppWP8.Utility using System.Net; using CampusAppWP8.Model.Utility; using CampusAppWPortalLib8.Model.Utility; + using CampusAppWPortalLib8.Utility; /// /// Class realize the access of restful HttpRequest /// - public class HttpRequest + public class HttpRequest : AbstractHttpRequest { - #region Member - - /// - /// BaseAddress of the webClient - /// - private string baseAddress; - #endregion - #region Constructor /// @@ -41,7 +34,7 @@ namespace CampusAppWP8.Utility /// the url of the HttpRequest base address public HttpRequest(Uri apiBaseAddress) { - this.baseAddress = apiBaseAddress.AbsoluteUri; + this.BaseAddress = apiBaseAddress.AbsoluteUri; } #endregion @@ -72,29 +65,6 @@ namespace CampusAppWP8.Utility client.OpenReadAsync(url); } - /// - /// Method create the Url for the http-get-method - /// - /// list of parameters - /// absolute API-Url include GetParameter - public Uri CreateGetUrl(List 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); - } - /// /// Method realize the http-delete-method /// diff --git a/CampusAppWP8/CampusAppWPortalLib8/CampusAppWPortalLib8.csproj b/CampusAppWP8/CampusAppWPortalLib8/CampusAppWPortalLib8.csproj index 5e82722f..908175e7 100644 --- a/CampusAppWP8/CampusAppWPortalLib8/CampusAppWPortalLib8.csproj +++ b/CampusAppWP8/CampusAppWPortalLib8/CampusAppWPortalLib8.csproj @@ -33,7 +33,6 @@ 4 - @@ -43,6 +42,7 @@ + diff --git a/CampusAppWP8/CampusAppWPortalLib8/Model/AbstractMainModel.cs b/CampusAppWP8/CampusAppWPortalLib8/Model/AbstractMainModel.cs new file mode 100644 index 00000000..d43496b5 --- /dev/null +++ b/CampusAppWP8/CampusAppWPortalLib8/Model/AbstractMainModel.cs @@ -0,0 +1,577 @@ +//----------------------------------------------------------------------------- +// +// Company copyright tag. +// +// fiedlchr +// 05.07.2013 +//----------------------------------------------------------------------------- +namespace CampusAppWPortalLib8.Model +{ + using System; + using System.Collections.Generic; + using CampusAppWPortalLib8.Model.Utility; + using CampusAppWPortalLib8.Utility; + + /// + /// abstract Base model io handling class. + /// + /// model type + public abstract class AbstractMainModel + { + #region Member + + /// + /// File object. + /// + private AbstractFile file = null; + + /// + /// Model io type. + /// + private ModelType modelType; + + /// + /// Model object. + /// + private T model = default(T); + + /// + /// Web object. + /// + private AbstractHttpRequest api = null; + + /// + /// Filename of saved data. + /// + private string fileName = string.Empty; + + /// + /// Url of the feed data. + /// + private Uri httpApiUri = null; + + /// + /// Parameterized uri of the feed. + /// + private Uri paramizedUri = null; + + #endregion + + #region Constructor + + /// + /// Initializes a new instance of the class. + /// + /// Model IO type + /// name of the file + /// url of the feed + public AbstractMainModel(ModelType modelType, string fileName, string url) + { + this.Init(modelType, fileName, url); + } + + /// + /// Initializes a new instance of the class. + /// + /// Model IO type + /// name of the file or the url of the feed + 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 + + /// + /// Delegate of the OnIO callback function. + /// + public delegate void OnIO(); + + /// + /// Delegate of the OnFailed(File/Web) callback function. + /// + public delegate void OnFailed(); + + /// + /// Delegate of the IsModelUpToDate callback function. + /// + /// data model + /// true, model is up to date + public delegate bool IsModelUpToDate(T model); + + /// + /// Callback pointer, called before loading. + /// + public event OnIO OnLoading = null; + + /// + /// Callback pointer, called after loading. + /// + public event OnIO OnLoaded = null; + + /// + /// Callback pointer, called before saving. + /// + public event OnIO OnSaving = null; + + /// + /// Callback pointer, called after saving. + /// + public event OnIO OnSaved = null; + + /// + /// Callback pointer, called after failed file loading. + /// + public event OnFailed OnFailedFile = null; + + /// + /// Callback pointer, called after failed web loading. + /// + public event OnFailed OnFailedWeb = null; + + /// + /// Callback pointer, called after failed file or web loading, if there + /// is no specialized onFailed callback set. + /// + public event OnFailed OnFailedLoad = null; + + /// + /// Callback pointer, called after failed saving data to file. + /// + public event OnFailed OnFailedSave = null; + + /// + /// Callback pointer, for checking if model is up to date at loading. + /// + public event IsModelUpToDate IsModelUpToDateOnLoad = null; + + /// + /// Callback pointer, for checking if model is up to date at saving. + /// (currently unused) + /// +#pragma warning disable 0067 + public event IsModelUpToDate IsModelUpToDateOnSave = null; + + #endregion + + #region Property + + /// + /// Gets or sets the Model. + /// + public T Model + { + get + { + return this.model; + } + + set + { + this.model = value; + } + } + + /// + /// Gets or sets the File. + /// + public AbstractFile File + { + get { return this.file; } + protected set { this.file = value; } + } + + /// + /// Gets or sets the ModelType. + /// + public ModelType ModelType + { + get { return this.modelType; } + protected set { this.modelType = value; } + } + + /// + /// Gets or sets the Api. + /// + public AbstractHttpRequest Api + { + get { return this.api; } + protected set { this.api = value; } + } + + /// + /// Gets or sets the ApiUrl. + /// + public Uri HttpApiUri + { + get { return this.httpApiUri; } + protected set { this.httpApiUri = value; } + } + + /// + /// Gets or sets the FileName. + /// + public string FileName + { + get { return this.fileName; } + protected set { this.fileName = value; } + } + + #endregion + + #region Method + + #region public + + /// + /// Method Fire all Events for fail load + /// + public void FireLoadFailEvents() + { + this.RunOnFailedCallback(this.OnFailedWeb, this.OnFailedLoad); + } + + /// + /// Method Fire all Events for load is complete + /// + public void FireLoadCompletedEvents() + { + this.RunOnIOCallback(this.OnLoaded); + } + + /// + /// Forces a update from web. + /// + public void ForceWebUpdate() + { + this.LoadData(ForceType.FORCE_WEB); + } + + /// + /// Forces a update from file. + /// + public void ForceReadFile() + { + this.LoadData(ForceType.FORCE_FILE); + } + + /// + /// Load the data if necessary, from web or from file, regarding if + /// the file data is up to date. + /// + /// if set/not invalid/not default, force to load from web or file + 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); + } + } + } + + /// + /// Save the model data if necessary. + /// + /// force saving. DEFAULT: false + 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); + } + } + } + + /// + /// Return the model io type. + /// + /// model io type + public ModelType GetModelType() + { + return this.modelType; + } + + /// + /// Create the parameterized uri. + /// + /// uri parameter list + public void SetUriParams(List parameters) + { + if (this.api != null) + { + this.paramizedUri = this.api.CreateGetUrl(parameters); + } + } + + /// + /// Clear the parameterized uri. + /// + public void ClearUriParams() + { + this.paramizedUri = null; + } + + #endregion + + #region protected + + /// + /// Abstract declaration of the model deserialize function. + /// + /// model data as byte array + /// true, is succeeded + protected abstract bool DeserializeModel(byte[] modelData); + + /// + /// Abstract declaration of the model serialize function. + /// + /// model data as byte array + protected abstract byte[] SerializeModel(); + + /// + /// Method send a HttpGet + /// + /// the url + protected abstract void SendHttpGet(Uri url); + + /// + /// Method check if model or file is Not up-to-date + /// + /// the check function + /// true if it is not up-to-date, otherwise false + protected abstract bool CheckIsNotUpToDate(object checkFunc); + + /// + /// Method check if file is Not up-to-date for load process + /// + /// true if it is not up-to-date, otherwise false + protected abstract bool CheckLoadFileIsNotUpToDate(); + + /// + /// Method check if file is Not up-to-date for load process + /// + /// true if it is not up-to-date, otherwise false + protected abstract bool CheckSaveFileIsNotUpToDate(); + + /// + /// Initializes the file object. + /// + protected abstract void InitFile(); + + /// + /// Initializes the web object. + /// + protected abstract void InitHttpApi(); + + /// + /// Check if the model io type is file. + /// + /// true, if the model io type has file. + protected bool IsFile() + { + bool retValue = false; + + if ((this.modelType & ModelType.File) != 0) + { + retValue = true; + } + + return retValue; + } + + /// + /// Check if the model io type is feed. + /// + /// true if the model io type has feed. + protected bool IsHttpApi() + { + bool retValue = false; + + if ((this.modelType & ModelType.Feed) != 0) + { + retValue = true; + } + + return retValue; + } + + #endregion + + #region private + + /// + /// Initialize the class. Is called by the constructors. + /// + /// model IO type + /// name of the data file + /// url of the feed data + 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(); + } + } + + /// + /// Executes the on i/o callback operation. + /// + /// The callback function. + private void RunOnIOCallback(OnIO callbackFunc) + { + if (callbackFunc != null) + { + callbackFunc(); + } + } + + /// Executes the on failed callback operation. + /// The special function. + /// The default function. + private void RunOnFailedCallback(OnFailed specialFunc, OnFailed defaultFunc) + { + if (specialFunc != null) + { + specialFunc(); + } + else if (defaultFunc != null) + { + defaultFunc(); + } + } + + #endregion + + #endregion + } +} \ No newline at end of file diff --git a/CampusAppWP8/CampusAppWPortalLib8/Model/IMainModel.cs b/CampusAppWP8/CampusAppWPortalLib8/Model/IMainModel.cs deleted file mode 100644 index 154af71c..00000000 --- a/CampusAppWP8/CampusAppWPortalLib8/Model/IMainModel.cs +++ /dev/null @@ -1,48 +0,0 @@ -//----------------------------------------------------------------------------- -// -// Company copyright tag. -// -// fiedlchr -// 05.07.2013 -//----------------------------------------------------------------------------- -using CampusAppWPortalLib8.Model; -namespace CampusAppWPortalLib8.Utility -{ - /// - /// Base model io handling class. - /// - /// model type - public interface IMainModel - { - /// - /// Gets or sets the Model. - /// - T Model { get; set; } - - /// - /// Load the data if necessary, from web or from file, regarding if - /// the file data is up to date. - /// - /// if set/not invalid/not default, force to load from web or file - void LoadData(ForceType force = ForceType.INVALID); - - /// - /// Save the model data if necessary. - /// - /// force saving. DEFAULT: false - void SaveData(bool force = false); - - /// - /// Abstract declaration of the model deserialize function. - /// - /// model data as byte array - /// true, is succeeded - bool DeserializeModel(byte[] modelData); - - /// - /// Abstract declaration of the model serialize function. - /// - /// model data as byte array - byte[] SerializeModel(); - } -} \ No newline at end of file diff --git a/CampusAppWP8/CampusAppWPortalLib8/Settings.StyleCop b/CampusAppWP8/CampusAppWPortalLib8/Settings.StyleCop index 54487d17..e1e8289d 100644 --- a/CampusAppWP8/CampusAppWPortalLib8/Settings.StyleCop +++ b/CampusAppWP8/CampusAppWPortalLib8/Settings.StyleCop @@ -1,6 +1,7 @@ + api nfc param qr