Example #1
0
        protected virtual PropertyStore GetNewRecord(Type type, string path)
        {
            object      newContent = Activator.CreateInstance(type);
            ContentItem newCI      = new ContentItem
            {
                Path     = path,
                DataType = type.FullName
            };

            newCI.SetContent(newContent);
            return(new PropertyStore().Inject(newCI));
        }
        public virtual void SetContent <T>(ContentItem contentItem, T content) where T : BaseContent
        {
            var rpsAttributes = typeof(T)
                                .GetCustomAttributes(typeof(RedirectPropertySourceAttribute), false)
                                .Cast <RedirectPropertySourceAttribute>()
                                .Where(rpsa => !rpsa.ReadOnly)
                                .ToList();
            List <ContentAddress> addresses = rpsAttributes
                                              .Select(a => contentItem.ContentAddress.Redirect(a.SourceDescriptor))
                                              .Distinct()
                                              .ToList();
            List <ContentItem> items = ContentRepository.Instance.GetContentItems(addresses);

            JObject jObjectContent = JObject.FromObject(content);

            if (rpsAttributes.Any())
            {
                foreach (var rpsAttribute in rpsAttributes)
                {
                    ContentAddress refdAddress = contentItem.ContentAddress.Redirect(rpsAttribute.SourceDescriptor);
                    if (refdAddress == contentItem.ContentAddress) // redirected to itself
                    {
                        continue;
                    }
                    ContentItem refdItem = items.FirstOrDefault(ci => ci.ContentAddress == refdAddress);
                    if (refdItem != null)
                    {
                        foreach (string path in rpsAttribute.Paths)
                        {
                            UpdateItemForPathSource(refdItem, path, jObjectContent);
                        }
                    }
                }
            }

            contentItem.SetContent <T>(jObjectContent);

            ContentRepository.Instance.Save();
        }
Example #3
0
        private ContentItem GetContentItem(Address a, object data)
        {
            string      dataPath    = null;
            string      routePath   = null;
            ContentItem contentItem = null;

            // try and get path from data via attributes
            Address address = new Address(data);

            if (address.Count > 0)
            {
                dataPath = address.GetAsContentPath();
            }

            // Get requested path
            if (a != null)
            {
                routePath = a.GetAsContentPath();
            }

            // We have path in data and address parameter and they disagree
            if (dataPath != null && routePath != null && dataPath != routePath)
            {
                // Raise event here for when address is changed via changing addressed mapped fields on data
                System.Events.ProcessEvent("Content.Move", this, Tuple.Create(a, data));
                // regenerate path in case event processor changed data
                address   = new Address(data);
                dataPath  = address.GetAsContentPath();
                routePath = a.GetAsContentPath();
            }

            string path = dataPath ?? routePath;

            // if we have an extended content object, we can use the OriginalRecord if must as we have no path, or if the path is the same
            if (data is ICoreMetadata)
            {
                contentItem = (ContentItem)Activator.CreateInstance(System.Extender[typeof(ContentItem)] ?? typeof(ContentItem));
                TypeExtender.CopyExtensionData(data, contentItem);
                contentItem.DataType = data.GetType().UnextendedType().FullName;

                if (path == null || path == contentItem.Path)
                {
                    contentItem.SetContent(System, data);
                    return(contentItem);
                }
            }

            // If we get to here, we can't find the path
            if (path == null)
            {
                throw new ArgumentException("Cannot find path of " + data.GetType().FullName);
            }

            // Now we have to get the content item from the db so we get the right ids etc
            var findPath     = routePath ?? dataPath;
            var contentItems = System.Repository.Get <ContentItem>(data.GetType(), iq => iq.Where(ci => ci.Path == findPath)).ToList();

            if (contentItems.Count > 1)
            {
                throw new Exception("Duplicate content items at " + findPath + " of type " + data.GetType().FullName);
            }

            contentItem = contentItems.SingleOrDefault();
            // If we can't we build a new one
            if (contentItem == null)
            {
                contentItem          = Repository.New <ContentItem>();
                contentItem.DataType = data.GetType().FullName;
            }

            contentItem.Path = path;

            contentItem.SetContent(System, data);

            if (data is ICoreMetadata)
            {
                TypeExtender.CopyExtensionData(contentItem, data);
            }

            return(contentItem);
        }
Example #4
0
 protected virtual PropertyStore GetNewRecord(Type type, string path)
 {
     object newContent = Activator.CreateInstance(type);
     ContentItem newCI = new ContentItem
     {
         Path = path,
         DataType = type.FullName
     };
     newCI.SetContent(newContent);
     return new PropertyStore().Inject(newCI);
 }
Example #5
0
        private ContentItem GetContentItem(Address a, object data)
        {
            string      dataPath    = null;
            string      routePath   = null;
            ContentItem contentItem = null;

            // try and get path from data, or else from rvdict
            Address address = new Address(data);

            if (address.Count > 0)
            {
                dataPath = address.GetAsContentPath();
            }

            if (a != null)
            {
                routePath = a.GetAsContentPath();
            }

            if (dataPath != null && routePath != null && dataPath != routePath)
            {
                // Raise event here for when address is changed via changing addressed mapped fields on data
                EventHub.Instance.ProcessEvent("Content.Move", this, Tuple.Create(a, data));
                // regenerate path in case event processor changed data
                address   = new Address(data);
                dataPath  = address.GetAsContentPath();
                routePath = a.GetAsContentPath();
            }

            string path = dataPath ?? routePath;

            // if we have a BaseContent, we can use the OriginalRecord if must as we have no path, or if the path is the same
            if (data is BaseContent)
            {
                contentItem = ((BaseContent)data).OriginalRecord;
                if (contentItem != null && (path == null || path == contentItem.Path))
                {
                    contentItem.SetContent(data);
                    return(contentItem);
                }
            }

            // If we get to here, we can't find the path
            if (path == null)
            {
                throw new ArgumentException("Cannot find path of " + data.GetType().FullName);
            }

            // Now we have to get the content item from the db so we get the right ids etc
            var findPath     = routePath ?? dataPath;
            var contentItems = Repository.Get <ContentItem>(data.GetType(), iq => iq.Where(ci => ci.Path == findPath)).ToList();

            if (contentItems.Count > 1)
            {
                throw new Exception("Duplicate content items at " + findPath + " of type " + data.GetType().FullName);
            }

            contentItem = contentItems.SingleOrDefault();
            // If we can't we build a new one
            if (contentItem == null)
            {
                contentItem          = Repository.New <ContentItem>();
                contentItem.DataType = data.GetType().FullName;
            }

            contentItem.Path = path;

            contentItem.SetContent(data);

            if (data is BaseContent)
            {
                ((BaseContent)data).OriginalRecord = contentItem;
            }

            return(contentItem);
        }