public static string GetObjectName(AccountFeedItem instance, int id) { return string.Format("{0}: {1}", instance.AccountFeed.Name, instance.Title); }
 public static ACL GetACL(ISession session, AccountFeedItem instance, Type type) { return new ManagedAccountFeedItem(session, instance).GetACL(type); }
        public int Update(ManagedSecurityContext sec)
        {
            GetACL().Check(sec, DataOperation.Update);

            mInstance.Updated = DateTime.UtcNow;

            IList <AccountFeedItem> deleted = mInstance.AccountFeedItems;
            List <AccountFeedItem>  updated = new List <AccountFeedItem>();

            bool fUpdated = Update(RssFeed.Read(GetFeedStream()), deleted, updated);

            if (!fUpdated)
            {
                fUpdated = Update(AtomFeed.Load(GetFeedStream(),
                                                new Uri("http://www.w3.org/2005/Atom")), deleted, updated);
            }

            if (!fUpdated)
            {
                fUpdated = Update(AtomFeed.Load(GetFeedStream(),
                                                new Uri("http://purl.org/atom/ns#")), deleted, updated);
            }

            if (!fUpdated)
            {
                throw new Exception("Invalid or empty RSS or ATOM feed.");
            }

            try
            {
                if (deleted != null)
                {
                    foreach (AccountFeedItem item in deleted)
                    {
                        ManagedAccountFeedItem m_item = new ManagedAccountFeedItem(Session, item);
                        m_item.Delete(sec);
                    }
                }

                // group items for auditing
                ManagedAccountAuditEntryCollection audit_coll = new ManagedAccountAuditEntryCollection();
                audit_coll.MessageFormat = string.Format("[user:{0}] x-posted {{0}} in [feed:{1}]",
                                                         mInstance.Account.Id, mInstance.Id);

                for (int i = 0; i < updated.Count; i++)
                {
                    AccountFeedItem item = updated[i];
                    DataOperation   op   = (item.Id == 0 ? DataOperation.Create : DataOperation.Update);
                    Session.Save(item);
                    if (op == DataOperation.Create)
                    {
                        string url   = string.Format("AccountFeedItemView.aspx?id={0}", item.Id);
                        string trace = string.Format("<a href=\"{0}\">{1}</a>", url, Renderer.Render(item.Title));
                        audit_coll.Add(trace);
                    }
                }

                IEnumerable <AccountAuditEntry> audit_entries = audit_coll.GetAccountAuditEntries(
                    Session, mInstance.Account, string.Format("AccountFeedView.aspx?id={0}", mInstance.Id));

                foreach (AccountAuditEntry entry in audit_entries)
                {
                    entry.Created = entry.Updated = mInstance.Updated;
                    entry.Md5     = ManagedAccountAuditEntry.GetHash(entry.Description);
                    Session.Save(entry);
                }

                mInstance.AccountFeedItems = updated;
                mInstance.LastError        = string.Empty;
                Session.Save(mInstance);
            }
            catch (Exception ex)
            {
                mInstance.LastError = ex.Message;
                Session.Save(mInstance);
                throw;
            }

            return(updated.Count);
        }
 public static int GetOwnerId(AccountFeedItem instance, int id) { return instance.AccountFeed.Account.Id; }
        public bool Update(RssFeed feed, IList <AccountFeedItem> deleted, List <AccountFeedItem> updated)
        {
            if (feed.Channels.Count == 0)
            {
                return(false);
            }

            foreach (RssChannel rsschannel in feed.Channels)
            {
                if (string.IsNullOrEmpty(mInstance.Name))
                {
                    mInstance.Name = HttpUtility.HtmlDecode(rsschannel.Title);
                }

                if (string.IsNullOrEmpty(mInstance.Description))
                {
                    mInstance.Description = HttpUtility.HtmlDecode(rsschannel.Description);
                }

                if (string.IsNullOrEmpty(mInstance.LinkUrl))
                {
                    mInstance.LinkUrl = rsschannel.Link.ToString();
                }

                foreach (RssItem rssitem in rsschannel.Items)
                {
                    // item is too far in the future, clearly bogus or purposeful in the future
                    if ((rssitem.PubDate.Ticks > 0) && (DateTime.UtcNow.AddDays(1) < rssitem.PubDate.ToUniversalTime()))
                    {
                        continue;
                    }

                    // fetch an existing item if any

                    AccountFeedItem item = null;

                    if (rssitem.Guid != null && !string.IsNullOrEmpty(rssitem.Guid.Name))
                    {
                        item = (AccountFeedItem)Session.CreateCriteria(typeof(AccountFeedItem))
                               .Add(Expression.Eq("Guid", rssitem.Guid.Name))
                               .Add(Expression.Eq("AccountFeed.Id", Id))
                               .UniqueResult();
                    }
                    else if (rssitem.Link != null && !string.IsNullOrEmpty(rssitem.Link.ToString()))
                    {
                        item = (AccountFeedItem)Session.CreateCriteria(typeof(AccountFeedItem))
                               .Add(Expression.Eq("Link", rssitem.Link.ToString()))
                               .Add(Expression.Eq("AccountFeed.Id", Id))
                               .UniqueResult();
                    }

                    if (item == null)
                    {
                        item         = new AccountFeedItem();
                        item.Created = item.Updated = DateTime.UtcNow;
                    }
                    else if ((rssitem.PubDate.Ticks > 0) && (rssitem.PubDate.ToUniversalTime() <= item.Updated))
                    {
                        // item has not been modified since last update
                        deleted.Remove(item);
                        continue;
                    }
                    else
                    {
                        // remove the item from obsolete/deleted items
                        item.Updated = DateTime.UtcNow;
                        deleted.Remove(item);
                    }

                    item.AccountFeed = mInstance;
                    item.Description = string.IsNullOrEmpty(rssitem.Content)
                        ? HttpUtility.HtmlDecode(rssitem.Description)
                        : HttpUtility.HtmlDecode(rssitem.Content);
                    item.Title = HttpUtility.HtmlDecode(rssitem.Title);
                    if (rssitem.Link != null)
                    {
                        item.Link = rssitem.Link.ToString();
                    }
                    if (rssitem.Guid != null)
                    {
                        item.Guid = rssitem.Guid.Name;
                    }
                    if (rssitem.PubDate.Ticks > 0)
                    {
                        item.Created = rssitem.PubDate.ToUniversalTime();
                    }
                    updated.Add(item);
                }
            }

            return(true);
        }
        public bool Update(AtomFeed feed, IList <AccountFeedItem> deleted, List <AccountFeedItem> updated)
        {
            if (feed.Entries.Count == 0)
            {
                return(false);
            }

            if (string.IsNullOrEmpty(mInstance.Name) && feed.Title != null)
            {
                mInstance.Name = HttpUtility.HtmlDecode(feed.Title.Content);
            }

            if (string.IsNullOrEmpty(mInstance.Description) && feed.SubTitle != null)
            {
                mInstance.Description = HttpUtility.HtmlDecode(feed.SubTitle.Content);
            }

            if (string.IsNullOrEmpty(mInstance.Description) && (feed.Tagline != null))
            {
                mInstance.Description = HttpUtility.HtmlDecode(feed.Tagline.Content);
            }

            if (string.IsNullOrEmpty(mInstance.LinkUrl) && feed.Links.Count > 0)
            {
                foreach (AtomLink link in feed.Links)
                {
                    if (link.Rel == Relationship.Alternate)
                    {
                        mInstance.LinkUrl = link.HRef.ToString();
                    }
                }
            }

            foreach (AtomEntry atomitem in feed.Entries)
            {
                // item is too far in the future, clearly bogus or purposeful in the future
                if (atomitem.Created != null &&
                    atomitem.Created.DateTime.Ticks > 0 &&
                    DateTime.UtcNow.AddDays(1) < atomitem.Created.DateTime.ToUniversalTime())
                {
                    continue;
                }

                // fetch an existing item if any

                AccountFeedItem item = null;

                if (atomitem.Id != null && !string.IsNullOrEmpty(atomitem.Id.ToString()))
                {
                    item = (AccountFeedItem)Session.CreateCriteria(typeof(AccountFeedItem))
                           .Add(Expression.Eq("Guid", atomitem.Id.ToString()))
                           .Add(Expression.Eq("AccountFeed.Id", Id))
                           .UniqueResult();
                }

                if (item == null)
                {
                    item         = new AccountFeedItem();
                    item.Created = item.Updated = DateTime.UtcNow;
                }
                else if (
                    atomitem.Modified != null &&
                    atomitem.Modified.DateTime.Ticks > 0 &&
                    atomitem.Modified.DateTime.ToUniversalTime() <= item.Updated)
                {
                    // item has not been modified since last update
                    deleted.Remove(item);
                    continue;
                }
                else if (
                    atomitem.Modified == null &&
                    atomitem.Created != null &&
                    atomitem.Created.DateTime.Ticks > 0 &&
                    atomitem.Created.DateTime.ToUniversalTime() <= item.Updated)
                {
                    // item creation date has not been modified since last update and there's no modified date
                    deleted.Remove(item);
                    continue;
                }
                else
                {
                    // remove the item from obsolete/deleted items
                    item.Updated = DateTime.UtcNow;
                    deleted.Remove(item);
                }

                item.AccountFeed = mInstance;
                item.Description = string.Empty;
                foreach (AtomContent content in atomitem.Contents)
                {
                    if (!string.IsNullOrEmpty(item.Description))
                    {
                        item.Description += "\n";
                    }

                    switch (content.Type)
                    {
                    case MediaType.TextHtml:
                        item.Description += HttpUtility.HtmlDecode(content.Content);
                        break;

                    default:
                        item.Description += content.Content;
                        break;
                    }
                }

                if (string.IsNullOrEmpty(item.Description))
                {
                    item.Description = HttpUtility.HtmlDecode(atomitem.Summary.Content);
                }

                item.Title = HttpUtility.HtmlDecode(atomitem.Title.Content);

                if (atomitem.Links.Count > 0)
                {
                    foreach (AtomLink link in atomitem.Links)
                    {
                        if (link.Rel == Relationship.Alternate)
                        {
                            item.Link = link.HRef.ToString();
                        }
                    }
                }

                if (atomitem.Id != null && !string.IsNullOrEmpty(atomitem.Id.ToString()))
                {
                    item.Guid = atomitem.Id.ToString();
                }
                if (atomitem.Created != null && atomitem.Created.DateTime.Ticks > 0)
                {
                    item.Created = atomitem.Created.DateTime.ToUniversalTime();
                }

                updated.Add(item);
            }

            return(true);
        }
 public static ACL GetACL(ISession session, AccountFeedItem instance, Type type)
 {
     return(new ManagedAccountFeedItem(session, instance).GetACL(type));
 }
 public static string GetObjectName(AccountFeedItem instance, int id)
 {
     return(string.Format("{0}: {1}", instance.AccountFeed.Name, instance.Title));
 }
 public static int GetOwnerId(AccountFeedItem instance, int id)
 {
     return(instance.AccountFeed.Account.Id);
 }