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; usingCampusAppWP8.Model.Utility; usingCampusAppWP8.Utility; publicabstractclassMainModel<T> { #regionMember protectedCampusAppWP8.Utility.Filefile=null; privateModelTypemodelType; privateTmodel=default(T); privateHttpRequestapi=null; privatestringfileName=string.Empty; privateUrihttpApiUri=null; privateUriparamizedUri=null; #endregion #regionConstructor 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."); } } #endregion #regionEvents publicdelegatevoidOnIO(); publicdelegatevoidOnFailed(); publicdelegateboolIsModelUpToDate(Tmodel); publicdelegateboolIsFileUpToDate(Tmodel,FileInfofileInfo); publiceventOnIOOnLoading=null; publiceventOnIOOnLoaded=null; publiceventOnIOOnSaving=null; publiceventOnIOOnSaved=null; publiceventOnFailedOnFailedFile=null; publiceventOnFailedOnFailedWeb=null; publiceventOnFailedOnFailedLoad=null; publiceventOnFailedOnFailedSave=null; publiceventIsFileUpToDateIsFileUpToDateOnLoad=null; publiceventIsFileUpToDateIsFileUpToDateOnSave=null; publiceventIsModelUpToDateIsModelUpToDateOnLoad=null; publiceventIsModelUpToDateIsModelUpToDateOnSave=null; #endregion #regionEnum publicenumModelType { INVALID=0, File=1, Feed=2, FileAndFeed=3 } publicenumForceType { INVALID=0, FORCE_FILE=1, FORCE_WEB=2 } #endregion #regionProperty publicTModel { get { returnthis.model; } set { this.model=value; } } #endregion #regionMethod #regionpublic publicvoidForceWebUpdate() { this.LoadData(ForceType.FORCE_WEB); } publicvoidForceReadFile() { this.LoadData(ForceType.FORCE_FILE); } publicvoidLoadData(ForceTypeforce=ForceType.INVALID) { this.RunOnIOCallback(this.OnLoading); //checkwhichsourceisusedforloadingthedata if(force==ForceType.INVALID) { //ifthemodelisnotuptodate if(this.CheckIsNotUpToDate(this.IsModelUpToDateOnLoad)==true) { force=ForceType.FORCE_FILE; if(this.file!=null) { //ifthefiledoesnotexistorissizeof0orisnot //uptodate,thenloadfromweb if((this.file.Exist()==false) ||(this.file.GetFileInfo().Length==0) ||(this.CheckIsNotUpToDate(this.IsFileUpToDateOnLoad)==true)) { force=ForceType.FORCE_WEB; } } else { //ifthefileobjectdoesnotexist,loadfromweb force=ForceType.FORCE_WEB; } //ifthewebobjectdoesnotexist,loadfromfile if(this.api==null) { force=ForceType.FORCE_FILE; } } else { //ifitisuptodate,nothinghastobeloaded this.RunOnIOCallback(this.OnLoaded); } } //loadfromweb 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 { //ifwebobjectdoesnotexist,callOnFailedcallbacks this.RunOnFailedCallback(this.OnFailedWeb,this.OnFailedLoad); } } //loadfromfile 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 { //iffileobjectdoesnotexist,callOnFailedcallbacks this.RunOnFailedCallback(this.OnFailedFile,this.OnFailedLoad); } } } publicvoidSaveData(boolforce=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();}); } 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); } } } publicModelTypeGetModelType() { returnthis.modelType; } publicTGetModel() { returnthis.model; } publicvoidSetUriParams(List<UrlParamModel>parameters) { if(this.api!=null) { this.paramizedUri=this.api.CreateGetUrl(parameters); } } publicvoidClearUriParams() { this.paramizedUri=null; } #endregion #regionprotected 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; } #endregion #regionprivate 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(); } if((this.IsHttpApi()==true) &&(url.Equals(string.Empty)==false)) { this.InitHttpApi(); } } privatevoidInitFile() { if((this.IsFile()==true) &&(this.file==null)) { this.file=newCampusAppWP8.Utility.File(this.fileName); } } privatevoidInitHttpApi() { if((this.IsHttpApi()==true) &&(this.api==null)) { this.api=newHttpRequest(this.httpApiUri); } } privatevoidOnLoadDataComplete(objectsender,OpenReadCompletedEventArgse) { ExceptiondownloadError=e.Error; if(downloadError!=null) { this.RunOnFailedCallback(this.OnFailedWeb,this.OnFailedLoad); } else { byte[]data; using(MemoryStreamms=newMemoryStream()) { e.Result.CopyTo(ms); data=ms.ToArray(); } if(data!=null&&data.Length>0) { this.DeserializeModel(data); } this.RunOnIOCallback(this.OnLoaded); } } privatevoidRunOnIOCallback(OnIOcallbackFunc) { if(callbackFunc!=null) { callbackFunc(); } } privatevoidRunOnFailedCallback(OnFailedspecialFunc,OnFaileddefaultFunc) { if(specialFunc!=null) { specialFunc(); } elseif(defaultFunc!=null) { defaultFunc(); } } privateboolCheckIsNotUpToDate(objectcheckFunc) { boolretValue=false; //ifthereisnocheckfunction,themodelorfileisnotuptodate if(checkFunc==null) { retValue=true; } else { TypefuncType=checkFunc.GetType(); if(funcType.Equals(typeof(IsFileUpToDate))) { retValue=!(checkFuncasIsFileUpToDate)(this.model,this.file.GetFileInfo()); } elseif(funcType.Equals(typeof(IsModelUpToDate))) { retValue=!(checkFuncasIsModelUpToDate)(this.model); } } returnretValue; } #endregion #endregion } }