public ObjectViewModel Build(Object @object)
        {
            Class @class = this.handler.Storage.GetRepository<IClassRepository>().WithKey(@object.ClassId);
              IDictionary<string, DataSourceViewModel> dataSourceInstances = new Dictionary<string, DataSourceViewModel>();

              foreach (DataSource dataSource in this.handler.Storage.GetRepository<IDataSourceRepository>().FilteredByClassId(@class.Id))
              {
            System.Type type = System.Type.GetType(dataSource.CSharpClassName);
            IDataSource dataSourceInstance = System.Activator.CreateInstance(type) as IDataSource;

            dataSourceInstance.Initialize(this.handler, @object, this.GetParameters(dataSource.Parameters));
            dataSourceInstances.Add(dataSource.Code, new DataSourceViewModelBuilder(this.handler).Build(dataSourceInstance.GetObjects()));
              }

              return new ObjectViewModel()
              {
            Id = @object.Id,
            Class = new ClassViewModelBuilder(this.handler).Build(@class.Id, @class.ViewName),
            Url = @object.Url,
            Properties = this.handler.Storage.GetRepository<IPropertyRepository>().FilteredByObjectId(@object.Id).ToDictionary(
              p => this.handler.Storage.GetRepository<IMemberRepository>().WithKey(p.MemberId).Code,
              p => new PropertyViewModelBuilder(this.handler).Build(p)
            ),
            DataSources = dataSourceInstances
              };
        }
        public ObjectViewModel Build(CachedObject cachedObject)
        {
            IDictionary<string, DataSourceViewModel> dataSourceInstances = new Dictionary<string, DataSourceViewModel>();

              if (!string.IsNullOrEmpty(cachedObject.CachedDataSources))
              {
            foreach (CachedDataSource cachedDataSource in JsonConvert.DeserializeObject<IEnumerable<CachedDataSource>>(cachedObject.CachedDataSources))
            {
              System.Type type = System.Type.GetType(cachedDataSource.CSharpClassName);
              IDataSource dataSourceInstance = System.Activator.CreateInstance(type) as IDataSource;

              dataSourceInstance.Initialize(this.handler, cachedObject, this.GetParameters(cachedDataSource.Parameters));
              dataSourceInstances.Add(cachedDataSource.Code, new DataSourceViewModelBuilder(this.handler).Build(dataSourceInstance.GetCachedObjects()));
            }
              }

              IEnumerable<CachedProperty> cachedProperties = JsonConvert.DeserializeObject<IEnumerable<CachedProperty>>(cachedObject.CachedProperties);

              return new ObjectViewModel()
              {
            Id = cachedObject.ObjectId,
            Class = new ClassViewModelBuilder(this.handler).Build(cachedObject.ClassId, cachedObject.ClassViewName),
            Url = cachedObject.Url,
            Properties = cachedProperties.ToDictionary(
              cp => cp.MemberCode,
              cp => new PropertyViewModelBuilder(this.handler).Build(cp)
            ),
            DataSources = dataSourceInstances
              };
        }
        private void CreateLocalizations(PropertyInfo propertyInfo, Dictionary dictionary)
        {
            IEnumerable<Culture> cultures = this.Storage.GetRepository<ICultureRepository>().All();

              foreach (Culture culture in cultures)
              {
            Localization localization = new Localization();

            localization.DictionaryId = dictionary.Id;
            localization.CultureId = culture.Id;

            string identity = propertyInfo.Name + culture.Code;
            string value = this.Request.Form[identity];

            localization.Value = value;
            this.Storage.GetRepository<ILocalizationRepository>().Create(localization);
              }

              this.Storage.Save();
        }
        private IDictionary<TabViewModel, IEnumerable<MemberViewModel>> GetMembersByTabs(Object @object, int? classId = null, int? objectId = null)
        {
            Dictionary<TabViewModel, IEnumerable<MemberViewModel>> membersByTabs = new Dictionary<TabViewModel, IEnumerable<MemberViewModel>>();

              membersByTabs.Add(new TabViewModel() { Name = "General" }, new List<MemberViewModel>());

              foreach (Tab tab in this.handler.Storage.GetRepository<ITabRepository>().FilteredByClassId(@object != null ? @object.ClassId : (int)classId))
            membersByTabs.Add(new TabViewModelBuilder(this.handler).Build(tab), new List<MemberViewModel>());

              foreach (Member member in this.handler.Storage.GetRepository<IMemberRepository>().FilteredByClassId(@object != null ? @object.ClassId : (int)classId))
              {
            TabViewModel tab = null;

            if (member.TabId == null)
              tab = membersByTabs.Keys.FirstOrDefault(t => t.Id == 0);

            else tab = membersByTabs.Keys.FirstOrDefault(t => t.Id == (int)member.TabId);

            (membersByTabs[tab] as List<MemberViewModel>).Add(new MemberViewModelBuilder(this.handler).Build(member, @object, objectId));
              }

              return membersByTabs;
        }
        private void CreateProperty(Object @object, int memberId, string cultureCode, string value)
        {
            Property property = this.Storage.GetRepository<IPropertyRepository>().WithObjectIdAndMemberId(@object.Id, memberId);

              if (property == null)
              {
            Dictionary html = new Dictionary();

            this.Storage.GetRepository<IDictionaryRepository>().Create(html);
            this.Storage.Save();
            property = new Property();
            property.ObjectId = @object.Id;
            property.MemberId = memberId;
            property.HtmlId = html.Id;
            this.Storage.GetRepository<IPropertyRepository>().Create(property);
            this.Storage.Save();
              }

              Localization localization = new Localization();

              localization.DictionaryId = property.HtmlId;
              localization.CultureId = this.Storage.GetRepository<ICultureRepository>().WithCode(cultureCode).Id;
              localization.Value = value;
              this.Storage.GetRepository<ILocalizationRepository>().Create(localization);
        }
        private Dictionary GetOrCreateDictionaryForProperty(IEntity entity, PropertyInfo propertyInfo)
        {
            PropertyInfo dictionaryIdPropertyInfo = entity.GetType().GetProperty(propertyInfo.Name + "Id");
              int dictionaryId = (int)dictionaryIdPropertyInfo.GetValue(entity);
              Dictionary dictionary = null;

              if (dictionaryId == 0)
              {
            dictionary = new Dictionary();
            this.Storage.GetRepository<IDictionaryRepository>().Create(dictionary);
            this.Storage.Save();
            dictionaryIdPropertyInfo.SetValue(entity, dictionary.Id);
              }

              else dictionary = this.Storage.GetRepository<IDictionaryRepository>().WithKey(dictionaryId);

              return dictionary;
        }
        private void DeleteLocalizations(Dictionary dictionary)
        {
            foreach (Localization localization in this.Storage.GetRepository<ILocalizationRepository>().FilteredByDictionaryId(dictionary.Id))
            this.Storage.GetRepository<ILocalizationRepository>().Delete(localization);

              this.Storage.Save();
        }
 public void Initialize(IHandler handler, CachedObject cachedObject, params KeyValuePair<string, string>[] args)
 {
     this.handler = handler;
       this.cachedObject = cachedObject;
       this.args = args.ToDictionary(a => a.Key, a => a.Value);
 }