internal static List<Bookmark> GetBookmarksForDisplay()
        {
            SqlFilter filter = new SqlFilter(typeof(Bookmark));
            filter.AddConstraint("islocaldeleted", false);

            // return...
            return filter.ExecuteEntityCollection<Bookmark>();
        }
Beispiel #2
0
        private void InitializeCurrentSet()
        {
            if (Keys == null)
            {
                throw new InvalidOperationException("Keys is null.");
            }
            if (KeyField == null)
            {
                throw new InvalidOperationException("KeyFields is null.");
            }

            // build some ids...
            ArrayList ids = new ArrayList();

            while (this.CurrentMasterPosition < this.Keys.Length && ids.Count < PageSize)
            {
                // add the current master position...
                ids.Add(this.Keys[this.CurrentMasterPosition][0]);

                // next...
                this.CurrentMasterPosition++;
            }

            // do we have anything?
            IList newSet = this.EntityType.CreateCollectionInstance();

            if (ids.Count > 0)
            {
                // create a statement to load that lot...
                SqlFilter filter = new SqlFilter(this.EntityType);

                // if the master defines fields, we'll need the same ones...
                if (this.Source.Fields.Count > 0)
                {
                    // add the fields - but we need to know if the key field is in there...
                    bool keyFound = false;
                    foreach (EntityField field in this.Source.Fields)
                    {
                        // found it?
                        if (field == this.KeyField)
                        {
                            keyFound = true;
                        }

                        // add it...
                        filter.Fields.Add(field);
                    }

                    // key?  we'll need that to get the items in the right order...
                    if (!(keyFound))
                    {
                        filter.Fields.Add(this.KeyField);
                    }
                }

                // build some sql...
                StringBuilder builder = new StringBuilder();
                builder.Append(filter.Dialect.FormatColumnName(this.KeyField.Name));
                builder.Append(" in (");
                for (int index = 0; index < ids.Count; index++)
                {
                    if (index > 0)
                    {
                        builder.Append(", ");
                    }
                    builder.Append(filter.Dialect.FormatVariableNameForQueryText(filter.ExtraParameters.Add(this.KeyField.DBType, ids[index])));
                }
                builder.Append(")");

                // add...
                filter.Constraints.AddFreeConstraint(builder.ToString());

                // run it...
                IList newEntities = filter.ExecuteEntityCollection();
                if (newEntities == null)
                {
                    throw new InvalidOperationException("newEntities is null.");
                }

                // comparer...
                IComparer comparer = ComparerBase.GetComparer(this.KeyField.DBType, Cultures.System);
                if (comparer == null)
                {
                    throw new InvalidOperationException("comparer is null.");
                }

                // we now have to get the data in the right order...
                object[] entities = new object[ids.Count];
                for (int index = 0; index < ids.Count; index++)
                {
                    // find it in the data set...
                    foreach (object newEntity in newEntities)
                    {
                        object[] newKeys = this.EntityType.Storage.GetKeyValues(newEntity);
                        if (newKeys == null)
                        {
                            throw new InvalidOperationException("newKeys is null.");
                        }
                        if (newKeys.Length != 1)
                        {
                            throw new InvalidOperationException("New keys length is invalid.");
                        }

                        // check...
                        if (comparer.Compare(ids[index], newKeys[0]) == 0)
                        {
                            entities[index] = newEntity;
                            break;
                        }
                    }
                }

                // create a new set from the ordered set...
                foreach (object entity in entities)
                {
                    if (entity != null)
                    {
                        newSet.Add(entity);
                    }
                    else
                    {
                        switch (Mode)
                        {
                        // do nothing if skip missing...
                        case OptimisticReadMode.SkipMissing:
                            break;

                        // add null in...
                        case OptimisticReadMode.ThrowIfMissing:
                            throw new InvalidOperationException("Item in keyset could not be found in the database.");

                        default:
                            throw new NotSupportedException(string.Format("Cannot handle '{0}' ({1}).", Mode, Mode.GetType()));
                        }
                    }
                }
            }

            // update...
            _currentSet = newSet;

            // position...
            _currentSetPosition = 0;
        }
Beispiel #3
0
        //public static PagedDataResult<T> ToDtos<T>(this IEnumerable<IDtoCapable> items, PagedDataRequest request)
        //    where T : IDtoBase
        //{
        //    throw new NotImplementedException("This operation has not been implemented.");
        //}

        public static List <T> ToDtos <T>(this IEnumerable <IDtoCapable> items)
            where T : IDtoBase
        {
            var dtoType = DtoType.InferDtoType(items);

            // get...
            using (var conn = Database.CreateConnection(dtoType.ConcreteEntityType))
            {
                conn.BeginTransaction();
                try
                {
                    var dtos = new List <T>();
                    foreach (var item in items)
                    {
                        dtos.Add(item.ToDto <T>());
                    }

                    // next -- preload any linked data that we can find...
                    foreach (var link in dtoType.Links)
                    {
                        var map = new Dictionary <long, IDtoBase>();
                        foreach (var dto in dtos)
                        {
                            // get...
                            var parentId = dto.GetValue <long>(link.ReferenceField);
                            if (parentId != 0 && !(map.ContainsKey(parentId)))
                            {
                                map[parentId] = null;
                            }
                        }

                        // load everything up...
                        var pages = Runtime.Current.GetPages(map.Keys, 500);
                        foreach (var page in pages)
                        {
                            var filter   = new SqlFilter(link.Link.ParentEntityType);
                            var keyField = link.Link.ParentEntityType.GetKeyFields()[0];
                            filter.Constraints.AddValueListConstraint(keyField, page);

                            // add...
                            var gots = filter.ExecuteEntityCollection();
                            foreach (IDtoCapable got in gots)
                            {
                                var id = ConversionHelper.ToInt64(filter.EntityType.Storage.GetValue(got, keyField));
                                if (map.ContainsKey(id))
                                {
                                    map[id] = got.ToDto();
                                }
                            }
                        }

                        // go back and fill them in...
                        foreach (var dto in dtos)
                        {
                            var parentId = dto.GetValue <long>(link.ReferenceField);
                            if (parentId != 0)
                            {
                                dto.SetLink(link, map[parentId]);
                            }
                        }
                    }

                    // ok...
                    conn.Commit();

                    // return...
                    return(dtos);
                }
                catch (Exception ex)
                {
                    conn.Rollback();
                    throw new InvalidOperationException("The operation failed", ex);
                }
            }
        }