private Dictionary <string, string> GetFieldValues(string contentName, IQPArticle article, string[] fields, bool passNullValues)
        {
            var fieldValues = article.GetType()
                              .GetProperties()
                              .Where(f => fields.Contains(f.Name))
                              .Select(f => new {
                field = MappingResolver.GetAttribute(contentName, f.Name.Replace("_ID", "")).Name,
                value = GetValue(f.GetValue(article))
            })
                              .Where(f => passNullValues || f.value != null)
                              .Distinct()
                              .ToDictionary(
                f => f.field,
                f => f.value
                );

            fieldValues[SystemColumnNames.Id]       = article.Id.ToString(CultureInfo.InvariantCulture);
            fieldValues[SystemColumnNames.Created]  = article.Created.ToString(CultureInfo.InvariantCulture);
            fieldValues[SystemColumnNames.Modified] = article.Modified.ToString(CultureInfo.InvariantCulture);

            if (article.StatusTypeId != 0)
            {
                fieldValues[SystemColumnNames.StatusTypeId] = article.StatusTypeId.ToString();
            }

            return(fieldValues);
        }
        public AttributeInfo GetInfo <Tcontent>(Expression <Func <Tcontent, object> > fieldSelector)
            where Tcontent : IQPArticle
        {
            var contentName   = typeof(Tcontent).Name;
            var expression    = (MemberExpression)fieldSelector.Body;
            var attributeName = expression.Member.Name;

            return(MappingResolver.GetAttribute(contentName, attributeName));
        }
        public string ReplacePlaceholders(string input)
        {
            string result = input;

            if (result != null && MappingResolver.GetSchema().ReplaceUrls)
            {
                result = result.Replace(uploadPlaceholder, UploadUrl);
                result = result.Replace(sitePlaceholder, SiteUrl);
            }
            return(result);
        }
        private void AddLinksToUpdate(IQPArticle article, Dictionary <string, string> fieldValues, List <EntityEntry> links)
        {
            var forwardLinks = links.Where(e => ((IQPLink)e.Entity).Id == article.Id && ((IQPLink)e.Entity).LinkedItemId > 0).ToList();

            if (forwardLinks.Count > 0)
            {
                var relations = (from e in forwardLinks
                                 let relatedId = ((IQPLink)e.Entity).LinkedItemId
                                                 let attribute = MappingResolver.GetAttribute(e.Metadata.Name.Substring(e.Metadata.Name.LastIndexOf(".") + 1))
                                                                 select new
                {
                    RelatedId = relatedId,
                    Field = attribute.MappedName
                })
                                .ToArray();
                var values = relations
                             .GroupBy(r => r.Field).ToDictionary(x => x.Key, x => string.Join(",", x.Select(y => y.RelatedId)));
                MergeValues(fieldValues, values);
                links.RemoveAll(x => forwardLinks.Contains(x));
            }
            var backwardLinks = links.Where(e => ((IQPLink)e.Entity).LinkedItemId == article.Id && ((IQPLink)e.Entity).Id > 0).ToList();

            if (backwardLinks.Count > 0)
            {
                var relations = (from e in forwardLinks
                                 let relatedId = ((IQPLink)e.Entity).Id
                                                 let attribute = MappingResolver.GetAttribute(e.Metadata.Name.Substring(e.Metadata.Name.LastIndexOf(".") + 1))
                                                                 select new
                {
                    RelatedId = relatedId,
                    Field = attribute.RelatedAttribute.MappedName
                })
                                .ToArray();
                var values = relations
                             .GroupBy(r => r.Field).ToDictionary(x => x.Key, x => string.Join(",", x.Select(y => y.RelatedId)));
                MergeValues(fieldValues, values);
                links.RemoveAll(x => backwardLinks.Contains(x));
            }
        }
 public ContentInfo GetInfo <T>()
     where T : IQPArticle
 {
     return(MappingResolver.GetContent(typeof(T).Name));
 }
 public SchemaInfo GetInfo()
 {
     return(MappingResolver.GetSchema());
 }
        private void UpdateObjectStateEntries(IEnumerable <EntityEntry> entries, Func <ContentInfo, EntityEntry, string[]> getProperties, bool passNullValues)
        {
            var links = entries.Where(x => typeof(IQPLink).IsAssignableFrom(x.Entity.GetType())).ToList();

            foreach (var group in entries.Where(e => !typeof(IQPLink).IsAssignableFrom(e.Entity.GetType())).GroupBy(m => m.Entity.GetType().Name))
            {
                var contentName = group.Key;
                var content     = MappingResolver.GetContent(contentName);
                if (!content.IsVirtual)
                {
                    var items = group
                                .Where(item => item.Entity is IQPArticle)
                                .Select(item => {
                        var article     = (IQPArticle)item.Entity;
                        var properties  = getProperties(content, item);
                        var fieldValues = GetFieldValues(contentName, article, properties, passNullValues);
                        if (fieldValues.ContainsKey("CONTENT_ITEM_ID") && Int32.Parse(fieldValues["CONTENT_ITEM_ID"]) < 0)
                        {
                            fieldValues["CONTENT_ITEM_ID"]  = "0";
                            item.Property("Id").IsTemporary = false;
                        }
                        AddLinksToUpdate(article, fieldValues, links);
                        return(new
                        {
                            article,
                            fieldValues
                        });
                    })
                                .ToArray();

                    Cnn.MassUpdate(content.Id, items.Select(item => item.fieldValues), 1);

                    foreach (var item in items)
                    {
                        SyncArticle(item.article, item.fieldValues);
                    }
                }
            }

            foreach (var e in entries.Where(x => typeof(IQPLink).IsAssignableFrom(x.Entity.GetType())))
            {
                if (((IQPLink)e.Entity).Id <= 0)
                {
                    ((IQPLink)e.Entity).Id = ((IQPLink)e.Entity).Item.Id;
                    var p = e.Properties.Where(n => n.Metadata.Name != "ItemId" && !n.Metadata.Name.EndsWith("LinkedItemId") &&
                                               n.Metadata.Name.EndsWith("ItemId")).FirstOrDefault();
                    e.Property(p.Metadata.Name).IsTemporary = false;
                }
                if (((IQPLink)e.Entity).LinkedItemId <= 0)
                {
                    ((IQPLink)e.Entity).LinkedItemId = ((IQPLink)e.Entity).LinkedItem.Id;
                    var p = e.Properties.Where(n => n.Metadata.Name != "LinkedItemId" && n.Metadata.Name.EndsWith("LinkedItemId")).FirstOrDefault();
                    e.Property(p.Metadata.Name).IsTemporary = false;
                }
            }

            var relations = (from e in links
                             where typeof(IQPLink).IsAssignableFrom(e.Entity.GetType())
                             let Id = ((IQPLink)e.Entity).Id
                                      let relatedId = ((IQPLink)e.Entity).LinkedItemId
                                                      let attribute = MappingResolver.GetAttribute(e.Metadata.Name.Substring(e.Metadata.Name.LastIndexOf(".") + 1))
                                                                      let item = new
            {
                Id = Id,
                RelatedId = relatedId,
                ContentId = attribute.ContentId,
                Field = attribute.MappedName
            }
                             group item by item.ContentId into g
                             select new { ContentId = g.Key, Items = g.ToArray() }
                             )
                            .ToArray();

            foreach (var relation in relations)
            {
                var values = relation.Items
                             .GroupBy(r => r.Id)
                             .Select(g =>
                {
                    var d = g.GroupBy(x => x.Field).ToDictionary(x => x.Key, x => string.Join(",", x.Select(y => y.RelatedId)));
                    d[SystemColumnNames.Id] = g.Key.ToString();
                    return(d);
                })
                             .ToArray();

                Cnn.MassUpdate(relation.ContentId, values, 1);
            }
        }
 public PostgreQpDataContext(DbContextOptions <PostgreQpDataContext> options, ModelReader schema)
     : base(options)
 {
     MappingResolver = new MappingResolver(schema);
     OnContextCreated();
 }
 public PostgreQpDataContext(ModelReader schema) : base(DefaultConnectionOptions())
 {
     MappingResolver = new MappingResolver(schema);
     OnContextCreated();
 }