private IEnumerable <IUmbracoEntity> PerformGetAll(Guid objectTypeId, Action <Sql> filter = null)
        {
            bool isContent = objectTypeId == new Guid(Constants.ObjectTypes.Document);
            bool isMedia   = objectTypeId == new Guid(Constants.ObjectTypes.Media);
            var  sql       = GetFullSqlForEntityType(isContent, isMedia, objectTypeId, filter);

            var factory = new UmbracoEntityFactory();

            if (isMedia)
            {
                //for now treat media differently
                //TODO: We should really use this methodology for Content/Members too!! since it includes properties and ALL of the dynamic db fields
                var entities = _work.Database.Fetch <dynamic, UmbracoPropertyDto, UmbracoEntity>(
                    new UmbracoEntityRelator().Map, sql);
                foreach (var entity in entities)
                {
                    yield return(entity);
                }
            }
            else
            {
                var dtos = _work.Database.Fetch <dynamic>(sql);
                foreach (var entity in dtos.Select(dto => factory.BuildEntityFromDynamic(dto)))
                {
                    yield return(entity);
                }
            }
        }
        /// <summary>
        /// Gets entities by query.
        /// </summary>
        /// <param name="query"></param>
        /// <param name="objectTypeId"></param>
        /// <returns></returns>
        /// <remarks>
        /// Note that this will also fetch all property data for media items, which can cause performance problems
        /// when used without paging, in sites with large amounts of data in cmsPropertyData.
        /// </remarks>
        public virtual IEnumerable <IUmbracoEntity> GetByQuery(IQuery <IUmbracoEntity> query, Guid objectTypeId)
        {
            var isContent = objectTypeId == Constants.ObjectTypes.DocumentGuid || objectTypeId == Constants.ObjectTypes.DocumentBlueprintGuid;
            var isMedia   = objectTypeId == Constants.ObjectTypes.MediaGuid;

            var sqlClause = GetBaseWhere(GetBase, isContent, isMedia, null, objectTypeId);

            var translator = new SqlTranslator <IUmbracoEntity>(sqlClause, query);
            var entitySql  = translator.Translate();

            var factory = new UmbracoEntityFactory();

            if (isMedia)
            {
                var wheres = query.GetWhereClauses().ToArray();

                var mediaSql = GetFullSqlForMedia(entitySql.Append(GetGroupBy(isContent, true, false)), sql =>
                {
                    //adds the additional filters
                    foreach (var whereClause in wheres)
                    {
                        sql.Where(whereClause.Item1, whereClause.Item2);
                    }
                });

                //for now treat media differently and include all property data too
                var entities = _work.Database.Fetch <dynamic, UmbracoPropertyDto, UmbracoEntity>(
                    new UmbracoEntityRelator().Map, mediaSql);
                return(entities);
            }
            else
            {
                return(GetByQueryInternal(entitySql, isContent, isMedia));
            }
        }
Esempio n. 3
0
        private IEnumerable <IUmbracoEntity> PerformGetAll(Guid objectTypeId, Action <Sql> filter = null)
        {
            var isContent = objectTypeId == Constants.ObjectTypes.DocumentGuid || objectTypeId == Constants.ObjectTypes.DocumentBlueprintGuid;
            var isMedia   = objectTypeId == Constants.ObjectTypes.MediaGuid;
            var sql       = GetFullSqlForEntityType(isContent, isMedia, objectTypeId, filter);

            var factory = new UmbracoEntityFactory();

            if (isMedia)
            {
                //for now treat media differently and include all property data too
                var entities = _work.Database.Fetch <dynamic, UmbracoPropertyDto, UmbracoEntity>(
                    new UmbracoEntityRelator().Map, sql);
                return(entities);
            }
            else
            {
                //query = read forward data reader, do not load everything into mem
                var dtos       = _work.Database.Query <dynamic>(sql);
                var collection = new EntityDefinitionCollection();
                foreach (var dto in dtos)
                {
                    collection.AddOrUpdate(new EntityDefinition(factory, dto, isContent, false));
                }
                return(collection.Select(x => x.BuildFromDynamic()).ToList());
            }
        }
Esempio n. 4
0
        public virtual IEnumerable <IUmbracoEntity> GetAll(Guid objectTypeId, params int[] ids)
        {
            if (ids.Any())
            {
                foreach (var id in ids)
                {
                    yield return(Get(id, objectTypeId));
                }
            }
            else
            {
                bool isContent = objectTypeId == new Guid(Constants.ObjectTypes.Document);
                bool isMedia   = objectTypeId == new Guid(Constants.ObjectTypes.Media);
                var  sql       = GetBaseWhere(GetBase, isContent, isMedia, string.Empty, objectTypeId).Append(GetGroupBy(isContent, isMedia));
                var  dtos      = isMedia
                               ? _work.Database.Fetch <UmbracoEntityDto, UmbracoPropertyDto, UmbracoEntityDto>(
                    new UmbracoEntityRelator().Map, sql)
                               : _work.Database.Fetch <UmbracoEntityDto>(sql);
                var factory = new UmbracoEntityFactory();

                foreach (var dto in dtos)
                {
                    var entity = factory.BuildEntity(dto);
                    yield return(entity);
                }
            }
        }
 public EntityDefinition(UmbracoEntityFactory factory, dynamic entity, bool isContent, bool isMedia)
 {
     _factory   = factory;
     _entity    = entity;
     _isContent = isContent;
     _isMedia   = isMedia;
 }
        public IUmbracoEntity GetByKey(Guid key, Guid objectTypeId)
        {
            bool isContent = objectTypeId == new Guid(Constants.ObjectTypes.Document);
            bool isMedia   = objectTypeId == new Guid(Constants.ObjectTypes.Media);

            var sql = GetFullSqlForEntityType(key, isContent, isMedia, objectTypeId);

            if (isMedia)
            {
                //for now treat media differently
                //TODO: We should really use this methodology for Content/Members too!! since it includes properties and ALL of the dynamic db fields
                var entities = _work.Database.Fetch <dynamic, UmbracoPropertyDto, UmbracoEntity>(
                    new UmbracoEntityRelator().Map, sql);

                return(entities.FirstOrDefault());
            }
            else
            {
                var nodeDto = _work.Database.FirstOrDefault <dynamic>(sql);
                if (nodeDto == null)
                {
                    return(null);
                }

                var factory = new UmbracoEntityFactory();
                var entity  = factory.BuildEntityFromDynamic(nodeDto);

                return(entity);
            }
        }
Esempio n. 7
0
        public virtual IUmbracoEntity Get(int id, Guid objectTypeId)
        {
            bool isContent = objectTypeId == Constants.ObjectTypes.DocumentGuid || objectTypeId == Constants.ObjectTypes.DocumentBlueprintGuid;
            bool isMedia   = objectTypeId == Constants.ObjectTypes.MediaGuid;

            var sql = GetFullSqlForEntityType(id, isContent, isMedia, objectTypeId);

            var factory = new UmbracoEntityFactory();

            if (isMedia)
            {
                //for now treat media differently and include all property data too
                var entities = _work.Database.Fetch <dynamic, UmbracoPropertyDto, UmbracoEntity>(
                    new UmbracoEntityRelator().Map, sql);

                return(entities.FirstOrDefault());
            }
            else
            {
                //query = read forward data reader, do not load everything into mem
                var dtos       = _work.Database.Query <dynamic>(sql);
                var collection = new EntityDefinitionCollection();
                foreach (var dto in dtos)
                {
                    collection.AddOrUpdate(new EntityDefinition(factory, dto, isContent, false));
                }
                var found = collection.FirstOrDefault();
                return(found != null?found.BuildFromDynamic() : null);
            }
        }
        public IEnumerable <IUmbracoEntity> GetPagedResultsByQuery(IQuery <IUmbracoEntity> query, Guid objectTypeId, long pageIndex, int pageSize, out long totalRecords,
                                                                   string orderBy, Direction orderDirection, IQuery <IUmbracoEntity> filter = null)
        {
            bool isContent = objectTypeId == Constants.ObjectTypes.DocumentGuid || objectTypeId == Constants.ObjectTypes.DocumentBlueprintGuid;
            bool isMedia   = objectTypeId == Constants.ObjectTypes.MediaGuid;
            var  factory   = new UmbracoEntityFactory();

            var sqlClause = GetBaseWhere(GetBase, isContent, isMedia, sql =>
            {
                if (filter != null)
                {
                    foreach (var filterClause in filter.GetWhereClauses())
                    {
                        sql.Where(filterClause.Item1, filterClause.Item2);
                    }
                }
            }, objectTypeId);
            var translator = new SqlTranslator <IUmbracoEntity>(sqlClause, query);
            var entitySql  = translator.Translate();
            var pagedSql   = entitySql.Append(GetGroupBy(isContent, isMedia, false));

            pagedSql = (orderDirection == Direction.Descending) ? pagedSql.OrderByDescending("umbracoNode.id") : pagedSql.OrderBy("umbracoNode.id");

            IEnumerable <IUmbracoEntity> result;

            var pagedResult = UnitOfWork.Database.Page <dynamic>(pageIndex + 1, pageSize, pagedSql);

            result = pagedResult.Items.Select(factory.BuildEntityFromDynamic).Cast <IUmbracoEntity>().ToList();

            //The total items from the PetaPoco page query will be wrong due to the Outer join used on parent, depending on the search this will
            //return duplicate results when the COUNT is used in conjuction with it, so we need to get the total on our own.

            //generate a query that does not contain the LEFT Join for parent, this would cause
            //the COUNT(*) query to return the wrong
            var sqlCountClause = GetBaseWhere(
                (isC, isM, f) => GetBase(isC, isM, f, true), //true == is a count query
                isContent, isMedia, sql =>
            {
                if (filter != null)
                {
                    foreach (var filterClause in filter.GetWhereClauses())
                    {
                        sql.Where(filterClause.Item1, filterClause.Item2);
                    }
                }
            }, objectTypeId);
            var translatorCount = new SqlTranslator <IUmbracoEntity>(sqlCountClause, query);
            var countSql        = translatorCount.Translate();

            totalRecords = UnitOfWork.Database.ExecuteScalar <int>(countSql);

            return(result);
        }
Esempio n. 9
0
        public virtual IUmbracoEntity Get(int id)
        {
            var sql = GetBaseWhere(GetBase, false, false, id);
            var nodeDto = _work.Database.FirstOrDefault<UmbracoEntityDto>(sql);
            if (nodeDto == null)
                return null;

            var factory = new UmbracoEntityFactory();
            var entity = factory.BuildEntity(nodeDto);

            return entity;
        }
Esempio n. 10
0
        public IUmbracoEntity GetByKey(Guid key)
        {
            var sql = GetBaseWhere(GetBase, false, false, key);
            var nodeDto = _work.Database.FirstOrDefault<dynamic>(sql);
            if (nodeDto == null)
                return null;

            var factory = new UmbracoEntityFactory();
            var entity = factory.BuildEntityFromDynamic(nodeDto);

            return entity;
        }
        public virtual IEnumerable <IUmbracoEntity> GetByQuery(IQuery <IUmbracoEntity> query)
        {
            var sqlClause  = GetBase(false, false, null);
            var translator = new SqlTranslator <IUmbracoEntity>(sqlClause, query);
            var sql        = translator.Translate().Append(GetGroupBy(false, false));

            var dtos = _work.Database.Fetch <dynamic>(sql);

            var factory = new UmbracoEntityFactory();
            var list    = dtos.Select(factory.BuildEntityFromDynamic).Cast <IUmbracoEntity>().ToList();

            return(list);
        }
Esempio n. 12
0
        public virtual IUmbracoEntity Get(int id, Guid objectTypeId)
        {
            bool isContent = objectTypeId == new Guid(Constants.ObjectTypes.Document);
            bool isMedia = objectTypeId == new Guid(Constants.ObjectTypes.Media);
            var sql = GetBaseWhere(GetBase, isContent, isMedia, objectTypeId, id).Append(GetGroupBy(isContent, isMedia));
            var nodeDto = _work.Database.FirstOrDefault<UmbracoEntityDto>(sql);
            if (nodeDto == null)
                return null;

            var factory = new UmbracoEntityFactory();
            var entity = factory.BuildEntity(nodeDto);

            return entity;
        }
Esempio n. 13
0
        public virtual IEnumerable <IUmbracoEntity> GetByQuery(IQuery <IUmbracoEntity> query)
        {
            var wheres     = string.Concat(" AND ", string.Join(" AND ", ((Query <IUmbracoEntity>)query).WhereClauses()));
            var sqlClause  = GetBase(false, false, wheres);
            var translator = new SqlTranslator <IUmbracoEntity>(sqlClause, query);
            var sql        = translator.Translate().Append(GetGroupBy(false, false));

            var dtos = _work.Database.Fetch <UmbracoEntityDto>(sql);

            var factory = new UmbracoEntityFactory();
            var list    = dtos.Select(factory.BuildEntity).Cast <IUmbracoEntity>().ToList();

            return(list);
        }
Esempio n. 14
0
        public IUmbracoEntity GetByKey(Guid key, Guid objectTypeId)
        {
            bool isContent = objectTypeId == new Guid(Constants.ObjectTypes.Document);
            bool isMedia = objectTypeId == new Guid(Constants.ObjectTypes.Media);
            var sql = GetBaseWhere(GetBase, isContent, isMedia, objectTypeId, key).Append(GetGroupBy(isContent, isMedia));
            var nodeDto = _work.Database.FirstOrDefault<dynamic>(sql);
            if (nodeDto == null)
                return null;

            var factory = new UmbracoEntityFactory();
            var entity = factory.BuildEntityFromDynamic(nodeDto);

            return entity;
        }
        public virtual IUmbracoEntity Get(int id)
        {
            var sql     = GetBaseWhere(GetBase, false, false, id);
            var nodeDto = _work.Database.FirstOrDefault <dynamic>(sql);

            if (nodeDto == null)
            {
                return(null);
            }

            var factory = new UmbracoEntityFactory();
            var entity  = factory.BuildEntityFromDynamic(nodeDto);

            return(entity);
        }
        public IUmbracoEntity GetByKey(Guid key)
        {
            var sql     = GetBaseWhere(GetBase, false, false, key);
            var nodeDto = UnitOfWork.Database.FirstOrDefault <dynamic>(sql);

            if (nodeDto == null)
            {
                return(null);
            }

            var factory = new UmbracoEntityFactory();
            var entity  = factory.BuildEntityFromDynamic(nodeDto);

            return(entity);
        }
Esempio n. 17
0
        public virtual IEnumerable <IUmbracoEntity> GetByQuery(IQuery <IUmbracoEntity> query, Guid objectTypeId)
        {
            bool isContent = objectTypeId == new Guid(Constants.ObjectTypes.Document);
            bool isMedia   = objectTypeId == new Guid(Constants.ObjectTypes.Media);

            var wheres     = string.Concat(" AND ", string.Join(" AND ", ((Query <IUmbracoEntity>)query).WhereClauses()));
            var sqlClause  = GetBaseWhere(GetBase, isContent, isMedia, wheres, objectTypeId);
            var translator = new SqlTranslator <IUmbracoEntity>(sqlClause, query);
            var sql        = translator.Translate().Append(GetGroupBy(isContent, isMedia));

            var factory = new UmbracoEntityFactory();

            if (isMedia)
            {
                //treat media differently for now
                var dtos = _work.Database.Fetch <UmbracoEntityDto, UmbracoPropertyDto, UmbracoEntityDto>(
                    new UmbracoEntityRelator().Map, sql);
                return(dtos.Select(factory.BuildEntity).Cast <IUmbracoEntity>().ToArray());
            }
            else
            {
                //use dynamic so that we can get ALL properties from the SQL
                //we'll have to stitch stuff together manually but we can get our
                //additional data to put in the dictionary.
                var dtos        = _work.Database.Fetch <dynamic>(sql);
                var entityProps = TypeHelper.GetPublicProperties(typeof(IUmbracoEntity)).Select(x => x.Name).ToArray();
                var result      = new List <IUmbracoEntity>();
                foreach (var d in dtos)
                {
                    //build the initial entity
                    IUmbracoEntity entity = factory.BuildEntityFromDynamic(d);

                    //convert the dynamic row to dictionary
                    var asDictionary = (IDictionary <string, object>)d;

                    //figure out what extra properties we have that are not on the IUmbracoEntity and add them to additional data
                    foreach (var k in asDictionary.Keys
                             .Select(x => new { orig = x, title = x.ConvertCase(StringAliasCaseType.PascalCase) })
                             .Where(x => entityProps.InvariantContains(x.title) == false))
                    {
                        entity.AdditionalData[k.title] = asDictionary[k.orig];
                    }

                    result.Add(entity);
                }
                return(result);
            }
        }
        private IEnumerable <IUmbracoEntity> GetByQueryInternal(Sql entitySql, bool isContent, bool isMedia)
        {
            var factory = new UmbracoEntityFactory();

            //use dynamic so that we can get ALL properties from the SQL so we can chuck that data into our AdditionalData
            var finalSql = entitySql.Append(GetGroupBy(isContent, isMedia));

            //query = read forward data reader, do not load everything into mem
            var dtos       = UnitOfWork.Database.Query <dynamic>(finalSql);
            var collection = new EntityDefinitionCollection();

            foreach (var dto in dtos)
            {
                collection.AddOrUpdate(new EntityDefinition(factory, dto, isContent, isMedia));
            }
            return(collection.Select(x => x.BuildFromDynamic()).ToList());
        }
Esempio n. 19
0
        public virtual IUmbracoEntity Get(int id, Guid objectTypeId)
        {
            bool isContent = objectTypeId == new Guid(Constants.ObjectTypes.Document);
            bool isMedia   = objectTypeId == new Guid(Constants.ObjectTypes.Media);
            var  sql       = GetBaseWhere(GetBase, isContent, isMedia, objectTypeId, id).Append(GetGroupBy(isContent, isMedia));
            var  nodeDto   = _work.Database.FirstOrDefault <UmbracoEntityDto>(sql);

            if (nodeDto == null)
            {
                return(null);
            }

            var factory = new UmbracoEntityFactory();
            var entity  = factory.BuildEntity(nodeDto);

            return(entity);
        }
Esempio n. 20
0
        public virtual IEnumerable <IUmbracoEntity> GetByQuery(IQuery <IUmbracoEntity> query, Guid objectTypeId)
        {
            bool isContent = objectTypeId == Constants.ObjectTypes.DocumentGuid || objectTypeId == Constants.ObjectTypes.DocumentBlueprintGuid;
            bool isMedia   = objectTypeId == Constants.ObjectTypes.MediaGuid;

            var sqlClause = GetBaseWhere(GetBase, isContent, isMedia, null, objectTypeId);

            var translator = new SqlTranslator <IUmbracoEntity>(sqlClause, query);
            var entitySql  = translator.Translate();

            var factory = new UmbracoEntityFactory();

            if (isMedia)
            {
                var wheres = query.GetWhereClauses().ToArray();

                var mediaSql = GetFullSqlForMedia(entitySql.Append(GetGroupBy(isContent, true, false)), sql =>
                {
                    //adds the additional filters
                    foreach (var whereClause in wheres)
                    {
                        sql.Where(whereClause.Item1, whereClause.Item2);
                    }
                });

                //for now treat media differently and include all property data too
                var entities = _work.Database.Fetch <dynamic, UmbracoPropertyDto, UmbracoEntity>(
                    new UmbracoEntityRelator().Map, mediaSql);
                return(entities);
            }
            else
            {
                //use dynamic so that we can get ALL properties from the SQL so we can chuck that data into our AdditionalData
                var finalSql = entitySql.Append(GetGroupBy(isContent, false));

                //query = read forward data reader, do not load everything into mem
                var dtos       = _work.Database.Query <dynamic>(finalSql);
                var collection = new EntityDefinitionCollection();
                foreach (var dto in dtos)
                {
                    collection.AddOrUpdate(new EntityDefinition(factory, dto, isContent, false));
                }
                return(collection.Select(x => x.BuildFromDynamic()).ToList());
            }
        }
        private IEnumerable <IUmbracoEntity> PerformGetAll(Guid objectTypeId, Action <Sql> filter = null)
        {
            var isContent = objectTypeId == Constants.ObjectTypes.DocumentGuid || objectTypeId == Constants.ObjectTypes.DocumentBlueprintGuid;
            var isMedia   = objectTypeId == Constants.ObjectTypes.MediaGuid;
            var sql       = GetFullSqlForEntityType(isContent, isMedia, objectTypeId, filter);

            var factory = new UmbracoEntityFactory();

            //query = read forward data reader, do not load everything into mem
            var dtos       = UnitOfWork.Database.Query <dynamic>(sql);
            var collection = new EntityDefinitionCollection();

            foreach (var dto in dtos)
            {
                collection.AddOrUpdate(new EntityDefinition(factory, dto, isContent, isMedia));
            }
            return(collection.Select(x => x.BuildFromDynamic()).ToList());
        }
Esempio n. 22
0
        public virtual IEnumerable <IUmbracoEntity> GetByQuery(IQuery <IUmbracoEntity> query, Guid objectTypeId)
        {
            bool isContent = objectTypeId == new Guid(Constants.ObjectTypes.Document);
            bool isMedia   = objectTypeId == new Guid(Constants.ObjectTypes.Media);

            var wheres     = string.Concat(" AND ", string.Join(" AND ", ((Query <IUmbracoEntity>)query).WhereClauses()));
            var sqlClause  = GetBaseWhere(GetBase, isContent, isMedia, wheres, objectTypeId);
            var translator = new SqlTranslator <IUmbracoEntity>(sqlClause, query);
            var sql        = translator.Translate().Append(GetGroupBy(isContent, isMedia));

            var dtos = isMedia
                           ? _work.Database.Fetch <UmbracoEntityDto, UmbracoPropertyDto, UmbracoEntityDto>(
                new UmbracoEntityRelator().Map, sql)
                           : _work.Database.Fetch <UmbracoEntityDto>(sql);

            var factory = new UmbracoEntityFactory();
            var list    = dtos.Select(factory.BuildEntity).Cast <IUmbracoEntity>().ToList();

            return(list);
        }
        public virtual IUmbracoEntity Get(int id, Guid objectTypeId)
        {
            bool isContent = objectTypeId == Constants.ObjectTypes.DocumentGuid || objectTypeId == Constants.ObjectTypes.DocumentBlueprintGuid;
            bool isMedia   = objectTypeId == Constants.ObjectTypes.MediaGuid;

            var sql = GetFullSqlForEntityType(id, isContent, isMedia, objectTypeId);

            var factory = new UmbracoEntityFactory();

            //query = read forward data reader, do not load everything into mem
            var dtos       = UnitOfWork.Database.Query <dynamic>(sql);
            var collection = new EntityDefinitionCollection();

            foreach (var dto in dtos)
            {
                collection.AddOrUpdate(new EntityDefinition(factory, dto, isContent, false));
            }
            var found = collection.FirstOrDefault();

            return(found != null?found.BuildFromDynamic() : null);
        }
        public virtual IEnumerable <IUmbracoEntity> GetByQuery(IQuery <IUmbracoEntity> query, Guid objectTypeId)
        {
            bool isContent = objectTypeId == new Guid(Constants.ObjectTypes.Document);
            bool isMedia   = objectTypeId == new Guid(Constants.ObjectTypes.Media);

            var sqlClause = GetBaseWhere(GetBase, isContent, isMedia, null, objectTypeId);

            var translator = new SqlTranslator <IUmbracoEntity>(sqlClause, query);
            var entitySql  = translator.Translate();

            var factory = new UmbracoEntityFactory();

            if (isMedia)
            {
                var wheres = query.GetWhereClauses().ToArray();

                var mediaSql = GetFullSqlForMedia(entitySql.Append(GetGroupBy(isContent, true, false)), sql =>
                {
                    //adds the additional filters
                    foreach (var whereClause in wheres)
                    {
                        sql.Where(whereClause.Item1, whereClause.Item2);
                    }
                });

                //treat media differently for now
                //TODO: We should really use this methodology for Content/Members too!! since it includes properties and ALL of the dynamic db fields
                var entities = _work.Database.Fetch <dynamic, UmbracoPropertyDto, UmbracoEntity>(
                    new UmbracoEntityRelator().Map, mediaSql);
                return(entities);
            }
            else
            {
                //use dynamic so that we can get ALL properties from the SQL so we can chuck that data into our AdditionalData
                var finalSql = entitySql.Append(GetGroupBy(isContent, false));
                var dtos     = _work.Database.Fetch <dynamic>(finalSql);
                return(dtos.Select(factory.BuildEntityFromDynamic).Cast <IUmbracoEntity>().ToList());
            }
        }
Esempio n. 25
0
        public IEnumerable <IUmbracoEntity> GetPagedResultsByQuery(IQuery <IUmbracoEntity> query, Guid objectTypeId, long pageIndex, int pageSize, out long totalRecords,
                                                                   string orderBy, Direction orderDirection, IQuery <IUmbracoEntity> filter = null)
        {
            bool isContent = objectTypeId == Constants.ObjectTypes.DocumentGuid || objectTypeId == Constants.ObjectTypes.DocumentBlueprintGuid;
            bool isMedia   = objectTypeId == Constants.ObjectTypes.MediaGuid;
            var  factory   = new UmbracoEntityFactory();

            var sqlClause = GetBaseWhere(GetBase, isContent, isMedia, sql =>
            {
                if (filter != null)
                {
                    foreach (var filterClause in filter.GetWhereClauses())
                    {
                        sql.Where(filterClause.Item1, filterClause.Item2);
                    }
                }
            }, objectTypeId);
            var translator = new SqlTranslator <IUmbracoEntity>(sqlClause, query);
            var entitySql  = translator.Translate();
            var pagedSql   = entitySql.Append(GetGroupBy(isContent, isMedia, false)).OrderBy("umbracoNode.id");

            IEnumerable <IUmbracoEntity> result;

            if (isMedia)
            {
                //Treat media differently for now, as an Entity it will be returned with ALL of it's properties in the AdditionalData bag!
                var pagedResult = _work.Database.Page <dynamic>(pageIndex + 1, pageSize, pagedSql);

                var ids      = pagedResult.Items.Select(x => (int)x.id).InGroupsOf(2000);
                var entities = pagedResult.Items.Select(factory.BuildEntityFromDynamic).Cast <IUmbracoEntity>().ToList();

                //Now we need to merge in the property data since we need paging and we can't do this the way that the big media query was working before
                foreach (var idGroup in ids)
                {
                    var propSql = GetPropertySql(Constants.ObjectTypes.Media)
                                  .Where("contentNodeId IN (@ids)", new { ids = idGroup })
                                  .OrderBy("contentNodeId");

                    //This does NOT fetch all data into memory in a list, this will read
                    // over the records as a data reader, this is much better for performance and memory,
                    // but it means that during the reading of this data set, nothing else can be read
                    // from SQL server otherwise we'll get an exception.
                    var allPropertyData = _work.Database.Query <dynamic>(propSql);

                    //keep track of the current property data item being enumerated
                    var propertyDataSetEnumerator = allPropertyData.GetEnumerator();
                    var hasCurrent = false; // initially there is no enumerator.Current

                    try
                    {
                        //This must be sorted by node id (which is done by SQL) because this is how we are sorting the query to lookup property types above,
                        // which allows us to more efficiently iterate over the large data set of property values.
                        foreach (var entity in entities)
                        {
                            // assemble the dtos for this def
                            // use the available enumerator.Current if any else move to next
                            while (hasCurrent || propertyDataSetEnumerator.MoveNext())
                            {
                                if (propertyDataSetEnumerator.Current.contentNodeId == entity.Id)
                                {
                                    hasCurrent = false; // enumerator.Current is not available

                                    //the property data goes into the additional data
                                    entity.AdditionalData[propertyDataSetEnumerator.Current.propertyTypeAlias] = new UmbracoEntity.EntityProperty
                                    {
                                        PropertyEditorAlias = propertyDataSetEnumerator.Current.propertyEditorAlias,
                                        Value = StringExtensions.IsNullOrWhiteSpace(propertyDataSetEnumerator.Current.dataNtext)
                                            ? propertyDataSetEnumerator.Current.dataNvarchar
                                            : StringExtensions.ConvertToJsonIfPossible(propertyDataSetEnumerator.Current.dataNtext)
                                    };
                                }
                                else
                                {
                                    hasCurrent = true; // enumerator.Current is available for another def
                                    break;             // no more propertyDataDto for this def
                                }
                            }
                        }
                    }
                    finally
                    {
                        propertyDataSetEnumerator.Dispose();
                    }
                }

                result = entities;
            }
            else
            {
                var pagedResult = _work.Database.Page <dynamic>(pageIndex + 1, pageSize, pagedSql);
                result = pagedResult.Items.Select(factory.BuildEntityFromDynamic).Cast <IUmbracoEntity>().ToList();
            }

            //The total items from the PetaPoco page query will be wrong due to the Outer join used on parent, depending on the search this will
            //return duplicate results when the COUNT is used in conjuction with it, so we need to get the total on our own.

            //generate a query that does not contain the LEFT Join for parent, this would cause
            //the COUNT(*) query to return the wrong
            var sqlCountClause = GetBaseWhere(
                (isC, isM, f) => GetBase(isC, isM, f, true), //true == is a count query
                isContent, isMedia, sql =>
            {
                if (filter != null)
                {
                    foreach (var filterClause in filter.GetWhereClauses())
                    {
                        sql.Where(filterClause.Item1, filterClause.Item2);
                    }
                }
            }, objectTypeId);
            var translatorCount = new SqlTranslator <IUmbracoEntity>(sqlCountClause, query);
            var countSql        = translatorCount.Translate();

            totalRecords = _work.Database.ExecuteScalar <int>(countSql);

            return(result);
        }
Esempio n. 26
0
        public virtual IEnumerable<IUmbracoEntity> GetByQuery(IQuery<IUmbracoEntity> query, Guid objectTypeId)
        {
            bool isContent = objectTypeId == new Guid(Constants.ObjectTypes.Document);
            bool isMedia = objectTypeId == new Guid(Constants.ObjectTypes.Media);

            var wheres = string.Concat(" AND ", string.Join(" AND ", ((Query<IUmbracoEntity>)query).WhereClauses()));
            var sqlClause = GetBaseWhere(GetBase, isContent, isMedia, wheres, objectTypeId);
            var translator = new SqlTranslator<IUmbracoEntity>(sqlClause, query);
            var sql = translator.Translate().Append(GetGroupBy(isContent, isMedia));

            var dtos = isMedia
                           ? _work.Database.Fetch<UmbracoEntityDto, UmbracoPropertyDto, UmbracoEntityDto>(
                               new UmbracoEntityRelator().Map, sql)
                           : _work.Database.Fetch<UmbracoEntityDto>(sql);

            var factory = new UmbracoEntityFactory();
            var list = dtos.Select(factory.BuildEntity).Cast<IUmbracoEntity>().ToList();

            return list;
        }
Esempio n. 27
0
        public virtual IEnumerable<IUmbracoEntity> GetByQuery(IQuery<IUmbracoEntity> query, Guid objectTypeId)
        {
            bool isContent = objectTypeId == new Guid(Constants.ObjectTypes.Document);
            bool isMedia = objectTypeId == new Guid(Constants.ObjectTypes.Media);

            var wheres = string.Concat(" AND ", string.Join(" AND ", ((Query<IUmbracoEntity>)query).WhereClauses()));
            var sqlClause = GetBaseWhere(GetBase, isContent, isMedia, wheres, objectTypeId);
            var translator = new SqlTranslator<IUmbracoEntity>(sqlClause, query);
            var sql = translator.Translate().Append(GetGroupBy(isContent, isMedia));

            var factory = new UmbracoEntityFactory();

            if (isMedia)
            {
                //treat media differently for now
                var dtos = _work.Database.Fetch<UmbracoEntityDto, UmbracoPropertyDto, UmbracoEntityDto>(
                    new UmbracoEntityRelator().Map, sql);
                return dtos.Select(factory.BuildEntity).Cast<IUmbracoEntity>().ToArray();
            }
            else
            {
                //use dynamic so that we can get ALL properties from the SQL
                //we'll have to stitch stuff together manually but we can get our
                //additional data to put in the dictionary.
                var dtos = _work.Database.Fetch<dynamic>(sql);
                var entityProps = TypeHelper.GetPublicProperties(typeof (IUmbracoEntity)).Select(x => x.Name).ToArray();
                var result = new List<IUmbracoEntity>();
                foreach (var d in dtos)
                {
                    //build the initial entity
                    IUmbracoEntity entity = factory.BuildEntityFromDynamic(d);

                    //convert the dynamic row to dictionary
                    var asDictionary = (IDictionary<string, object>) d;
                    
                    //figure out what extra properties we have that are not on the IUmbracoEntity and add them to additional data
                    foreach (var k in asDictionary.Keys
                        .Select(x => new {orig = x, title = x.ConvertCase(StringAliasCaseType.PascalCase)})
                        .Where(x => entityProps.InvariantContains(x.title) == false))
                    {
                        entity.AdditionalData[k.title] = asDictionary[k.orig];
                    }

                    result.Add(entity);
                }
                return result;
            }
        }
Esempio n. 28
0
        public virtual IEnumerable<IUmbracoEntity> GetByQuery(IQuery<IUmbracoEntity> query, Guid objectTypeId)
        {
            bool isContent = objectTypeId == new Guid(Constants.ObjectTypes.Document);
            bool isMedia = objectTypeId == new Guid(Constants.ObjectTypes.Media);

            var wheres = string.Concat(" AND ", string.Join(" AND ", ((Query<IUmbracoEntity>)query).WhereClauses()));
            var sqlClause = GetBaseWhere(GetBase, isContent, isMedia, wheres, objectTypeId);
            var translator = new SqlTranslator<IUmbracoEntity>(sqlClause, query);
            var sql = translator.Translate().Append(GetGroupBy(isContent, isMedia));

            var factory = new UmbracoEntityFactory();

            if (isMedia)
            {
                //treat media differently for now 
                //TODO: We should really use this methodology for Content/Members too!! since it includes properties and ALL of the dynamic db fields
                var entities = _work.Database.Fetch<dynamic, UmbracoPropertyDto, UmbracoEntity>(
                    new UmbracoEntityRelator().Map, sql);
                return entities;
            }
            else
            {
                //use dynamic so that we can get ALL properties from the SQL so we can chuck that data into our AdditionalData
                var dtos = _work.Database.Fetch<dynamic>(sql);
                return dtos.Select(factory.BuildEntityFromDynamic).Cast<IUmbracoEntity>().ToList();
            }
        }
Esempio n. 29
0
        public virtual IEnumerable<IUmbracoEntity> GetAll(Guid objectTypeId, params int[] ids)
        {
            if (ids.Any())
            {
                foreach (var id in ids)
                {
                    yield return Get(id, objectTypeId);
                }
            }
            else
            {
                bool isContent = objectTypeId == new Guid(Constants.ObjectTypes.Document);
                bool isMedia = objectTypeId == new Guid(Constants.ObjectTypes.Media);
                var sql = GetBaseWhere(GetBase, isContent, isMedia, string.Empty, objectTypeId).Append(GetGroupBy(isContent, isMedia));
                var dtos = isMedia
                               ? _work.Database.Fetch<UmbracoEntityDto, UmbracoPropertyDto, UmbracoEntityDto>(
                                   new UmbracoEntityRelator().Map, sql)
                               : _work.Database.Fetch<UmbracoEntityDto>(sql);
                var factory = new UmbracoEntityFactory();

                foreach (var dto in dtos)
                {
                    var entity = factory.BuildEntity(dto);
                    yield return entity;
                }
            }
        }
Esempio n. 30
0
        public virtual IEnumerable<IUmbracoEntity> GetByQuery(IQuery<IUmbracoEntity> query)
        {
            var wheres = string.Concat(" AND ", string.Join(" AND ", ((Query<IUmbracoEntity>) query).WhereClauses()));
            var sqlClause = GetBase(false, false, wheres);
            var translator = new SqlTranslator<IUmbracoEntity>(sqlClause, query);
            var sql = translator.Translate().Append(GetGroupBy(false, false));

            var dtos = _work.Database.Fetch<UmbracoEntityDto>(sql);

            var factory = new UmbracoEntityFactory();
            var list = dtos.Select(factory.BuildEntity).Cast<IUmbracoEntity>().ToList();

            return list;
        }
Esempio n. 31
0
        public virtual IEnumerable<IUmbracoEntity> GetAll(Guid objectTypeId, params int[] ids)
        {
            if (ids.Any())
            {
                foreach (var id in ids)
                {
                    yield return Get(id, objectTypeId);
                }
            }
            else
            {
                bool isContent = objectTypeId == new Guid(Constants.ObjectTypes.Document);
                bool isMedia = objectTypeId == new Guid(Constants.ObjectTypes.Media);
                var sql = GetBaseWhere(GetBase, isContent, isMedia, string.Empty, objectTypeId).Append(GetGroupBy(isContent, isMedia));

                var factory = new UmbracoEntityFactory();

                if (isMedia)
                {
                    //for now treat media differently
                    //TODO: We should really use this methodology for Content/Members too!! since it includes properties and ALL of the dynamic db fields
                    var entities = _work.Database.Fetch<dynamic, UmbracoPropertyDto, UmbracoEntity>(
                        new UmbracoEntityRelator().Map, sql);
                    foreach (var entity in entities)
                    {
                        yield return entity;
                    }
                }
                else
                {
                    var dtos = _work.Database.Fetch<dynamic>(sql);
                    foreach (var entity in dtos.Select(dto => factory.BuildEntityFromDynamic(dto)))
                    {
                        yield return entity;
                    }
                }
            }
        }