AbstractMainModel.cs CampusAppWPortalLib8::Model::AbstractMainModel< T > CampusAppWPortalLib8::Model //----------------------------------------------------------------------- //<copyrightfile="AbstractMainModel.cs"company="BTU/IIT"> //TheMITLicense(MIT).Copyright(c)2013BTU/IIT. //</copyright> //<author>fiedlchr</author> //<date>15.10.2013</date> //<summary>Implementstheabstractmainmodelclass</summary> //----------------------------------------------------------------------- namespaceCampusAppWPortalLib8.Model { usingSystem; usingSystem.Collections.Generic; usingCampusAppWPortalLib8.Model.Utility; usingCampusAppWPortalLib8.Utility; publicabstractclassAbstractMainModel<T> { #regionMember privateAbstractFilefile=null; privateModelTypemodelType; privateTmodel=default(T); privateAbstractHttpRequestapi=null; privatestringfileName=string.Empty; privateUrihttpApiUri=null; privateUriparamizedUri=null; #endregion #regionConstructor publicAbstractMainModel(ModelTypemodelType,stringfileName,stringurl) { this.Init(modelType,fileName,url); } publicAbstractMainModel(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); publiceventOnIOOnLoading=null; publiceventOnIOOnLoaded=null; publiceventOnIOOnSaving=null; publiceventOnIOOnSaved=null; publiceventOnFailedOnFailedFile=null; publiceventOnFailedOnFailedWeb=null; publiceventOnFailedOnFailedLoad=null; publiceventOnFailedOnFailedSave=null; publiceventIsModelUpToDateIsModelUpToDateOnLoad=null; #pragmawarningdisable0067 publiceventIsModelUpToDateIsModelUpToDateOnSave=null; #endregion #regionProperty publicTModel { get { returnthis.model; } set { this.model=value; } } publicAbstractFileFile { get{returnthis.file;} protectedset{this.file=value;} } publicModelTypeModelType { get{returnthis.modelType;} protectedset{this.modelType=value;} } publicAbstractHttpRequestApi { get{returnthis.api;} protectedset{this.api=value;} } publicUriHttpApiUri { get{returnthis.httpApiUri;} protectedset{this.httpApiUri=value;} } publicstringFileName { get{returnthis.fileName;} protectedset{this.fileName=value;} } #endregion #regionMethod #regionpublic publicvoidFireLoadFailEvents() { this.RunOnFailedCallback(this.OnFailedWeb,this.OnFailedLoad); } publicvoidFireLoadCompletedEvents() { this.RunOnIOCallback(this.OnLoaded); } 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.CheckLoadFileIsNotUpToDate()==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.SendHttpGet(this.paramizedUri); } else { this.SendHttpGet(this.httpApiUri); } } 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.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();}); } 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; } 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(); protectedabstractvoidSendHttpGet(Uriurl); protectedabstractboolCheckIsNotUpToDate(objectcheckFunc); protectedabstractboolCheckLoadFileIsNotUpToDate(); protectedabstractboolCheckSaveFileIsNotUpToDate(); protectedabstractvoidInitFile(); protectedabstractvoidInitHttpApi(); 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(); } } privatevoidRunOnIOCallback(OnIOcallbackFunc) { if(callbackFunc!=null) { callbackFunc(); } } privatevoidRunOnFailedCallback(OnFailedspecialFunc,OnFaileddefaultFunc) { if(specialFunc!=null) { specialFunc(); } elseif(defaultFunc!=null) { defaultFunc(); } } #endregion #endregion } }