MainModel.cs CampusAppWP8::MainModel< T > CampusAppWP8 System::Net //----------------------------------------------------------------------------- //<copyrightfile="MainModel.cs"company="BTU/IIT"> //Companycopyrighttag. //</copyright> //<author>fiedlchr</author> //<sience>05.07.2013</sience> //----------------------------------------------------------------------------- namespaceCampusAppWP8 { usingSystem; usingSystem.Collections.Generic; usingSystem.IO; usingSystem.Net; usingSystem.Text; usingCampusAppWP8.Model.Utility; usingCampusAppWP8.Utility; publicabstractclassMainModel<T>:IDisposable { privateModelTypemodelType; privateTmodel=default(T); privateCampusAppWP8.Utility.Filefile=null; privateHttpRequestapi=null; privatestringfileName=string.Empty; privateUrihttpApiUri=null; privateUriparamizedUri=null; publicMainModel(ModelTypemodelType,stringfileName,stringurl) { this.Init(modelType,fileName,url); } publicMainModel(ModelTypemodelType,stringsourceName) { if(modelType==ModelType.File) { this.Init(modelType,sourceName,string.Empty); } elseif(modelType==ModelType.Feed) { this.Init(modelType,string.Empty,sourceName); } else { thrownewNotSupportedException("WrongconstructorwascalledforFeedandFilesupport."); } } ~MainModel() { this.SaveData(); } publicdelegatevoidOnLoading(); publicdelegatevoidOnLoaded(); publicdelegatevoidOnSaving(); publicdelegatevoidOnSaved(); publicdelegatevoidOnFailed(); publicdelegateboolIsModelUpToDate(Tmodel); publicdelegateboolIsFileUpToDate(Tmodel,FileInfofileInfo); publiceventOnLoadingonLoading=null; publiceventOnLoadedonLoaded=null; publiceventOnSavingonSaving=null; publiceventOnSavedonSaved=null; publiceventOnFailedonFailedFile=null; publiceventOnFailedonFailedWeb=null; publiceventOnFailedonFailed=null; publiceventOnFailedonFailedSave=null; publiceventIsFileUpToDateisFileUpToDateOnLoad=null; publiceventIsFileUpToDateisFileUpToDateOnSave=null; publiceventIsModelUpToDateisModelUpToDateOnLoad=null; publiceventIsModelUpToDateisModelUpToDateOnSave=null; publicenumModelType { INVALID=0, File=1, Feed=2, FileAndFeed=3 } publicTModel { get { returnthis.model; } set { this.model=value; } } publicvoidDispose() { this.SaveData(); } publicvoidForceWebUpdate() { if(this.api!=null) { if(this.onLoading!=null) { this.onLoading(); } this.api.HttpGet(this.httpApiUri,this.OnLoadDataComplete); } } publicvoidForceReadFile() { if(this.file!=null) { if(this.onLoading!=null) { this.onLoading(); } stringdata=this.file.ReadFile(); if(data==null) { if(this.onFailedFile!=null) { this.onFailedFile(); } elseif(this.onFailed!=null) { this.onFailed(); } } elseif(!data.Equals(string.Empty)) { this.DeserializeModel(Encoding.UTF8.GetBytes(data)); } if((data!=null)&&(this.onLoaded!=null)) { this.onLoaded(); } } } publicvoidLoadData() { boolloadFromFile=true; if(this.onLoading!=null) { this.onLoading(); } if(((this.isModelUpToDateOnLoad==null) ||(this.isModelUpToDateOnLoad(this.model)==false)) &&((this.file!=null)||this.api!=null)) { if(this.file!=null) { if((this.file.Exist()==false) ||(this.file.GetFileInfo().Length==0)) { loadFromFile=false; } if(((this.isFileUpToDateOnLoad!=null)&&(this.isFileUpToDateOnLoad(this.model,this.file.GetFileInfo())==false)) ||(this.isFileUpToDateOnLoad==null)) { loadFromFile=false; } } else { loadFromFile=false; } if(this.api==null) { loadFromFile=true; } if(loadFromFile==false) { if(this.paramizedUri!=null) { this.api.HttpGet(this.paramizedUri,this.OnLoadDataComplete); } else { this.api.HttpGet(this.httpApiUri,this.OnLoadDataComplete); } } else { stringdata=this.file.ReadFile(); if(data==null) { if(this.onFailedFile!=null) { this.onFailedFile(); } elseif(this.onFailed!=null) { this.onFailed(); } } elseif(!data.Equals(string.Empty)) { this.DeserializeModel(Encoding.UTF8.GetBytes(data)); } } } if(loadFromFile==true) { if(this.onLoaded!=null) { this.onLoaded(); } } } publicvoidSaveData(boolforce=false) { if((this.file!=null) &&((this.isFileUpToDateOnSave==null) ||(this.isFileUpToDateOnSave(this.model,this.file.GetFileInfo())==false) ||(force==true))) { if(this.onSaving!=null) { this.onSaving(); } byte[]data=this.SerializeModel(); if((this.onSaved!=null)&&(this.onFailedSave!=null)) { this.file.WriteFile(data,delegate(){this.onSaved();},delegate(){this.onFailedSave();}); } elseif(this.onSaved!=null) { this.file.WriteFile(data,delegate(){this.onSaved();},null); } elseif(this.onFailedSave!=null) { this.file.WriteFile(data,null,delegate(){this.onFailedSave();}); } else { this.file.WriteFile(data,null,null); } /* if(this.onSaved!=null) { this.onSaved(); } */ } } publicModelTypeGetModelType() { returnthis.modelType; } publicTGetModel() { returnthis.model; } publicvoidSetUriParams(List<UrlParamModel>parameters) { if(this.api!=null) { this.paramizedUri=this.api.CreateGetUrl(parameters); } } publicvoidClearUriParams() { this.paramizedUri=null; } protectedabstractboolDeserializeModel(byte[]modelData); protectedabstractbyte[]SerializeModel(); protectedboolIsFile() { boolretValue=false; if((this.modelType&ModelType.File)!=0) { retValue=true; } returnretValue; } protectedboolIsHttpApi() { boolretValue=false; if((this.modelType&ModelType.Feed)!=0) { retValue=true; } returnretValue; } privatevoidInit(ModelTypemodelType,stringfileName,stringurl) { this.modelType=modelType; if((url!=null)&&(url.Equals(string.Empty)==false)) { this.httpApiUri=newUri(url,UriKind.Absolute); } this.fileName=fileName; if((this.IsFile()==true) &&(fileName.Equals(string.Empty)==false)) { this.InitFile(CampusAppWP8.Utility.File.IOTypeRead.ReadSync,CampusAppWP8.Utility.File.IOTypeWrite.WriteAsync); } if((this.IsHttpApi()==true) &&(url.Equals(string.Empty)==false)) { this.InitHttpApi(); } } privatevoidInitFile(CampusAppWP8.Utility.File.IOTypeReadreadType=CampusAppWP8.Utility.File.IOTypeRead.ReadSync,CampusAppWP8.Utility.File.IOTypeWritewriteType=CampusAppWP8.Utility.File.IOTypeWrite.WriteAsync) { if((this.IsFile()==true) &&(this.file==null)) { this.file=newCampusAppWP8.Utility.File(this.fileName,readType,writeType); } } privatevoidInitHttpApi() { if((this.IsHttpApi()==true) &&(this.api==null)) { this.api=newHttpRequest(this.httpApiUri); } } privatevoidOnLoadDataComplete(objectsender,DownloadStringCompletedEventArgse) { ExceptiondownloadError=e.Error; if(downloadError!=null) { if(this.onFailedWeb!=null) { this.onFailedWeb(); } elseif(this.onFailed!=null) { this.onFailed(); } return; } stringdownloadResult=e.Result; if(downloadResult!=null&&!downloadResult.Equals(string.Empty)) { this.DeserializeModel(Encoding.UTF8.GetBytes(downloadResult)); } if(this.onLoaded!=null) { this.onLoaded(); } } } }