Esempio n. 1
0
        public override string GetPath(ContentItem item, bool wild = false)
        {
            var sb = new StringBuilder(60);

            sb.Append("c-");
            sb.Append(item.ID.ToString().PadLeft(6, '0'));
            sb.Append('-');
            if (wild)
            {
                sb.Append('*');
            }
            else
            {
                var typeName = item.GetType().Name;
                if (typeName.EndsWith("Proxy"))
                {
                    typeName = typeName.Substring(0, typeName.Length - 5);                             // remove Proxy from Name
                }
                XmlPersistenceUtility.AppendTextSafe(sb, typeName);
                if (!string.IsNullOrEmpty(item.Name) && !item.Name.Equals(item.ID.ToString()))
                {
                    sb.Append('-');
                    XmlPersistenceUtility.AppendTextSafe(sb, item.Name);
                }
            }
            sb.Append(".xml");
            return(sb.ToString());
        }
Esempio n. 2
0
        public override void SaveOrUpdate(ContentItem item)
        {
            if (_readonly)
            {
                throw new Exception("Repository is in read only mode");
            }

            try
            {
                lock (this)
                {
                    if (item.ID == 0)
                    {
                        if (Database.All(x => x.Value != item))
                        {
                            item.ID = Database.Count > 0 ? AllContentItems.Max(f => f.ID) + 1 : 1;
                        }
                    }

                    var old = Database.ContainsKey(item.ID) ? Database[item.ID] : null;
                    if (old != null && !System.Object.ReferenceEquals(old, item))
                    {
                        // ALTERNATIVE (old as IUpdatable<ContentItem>).UpdateFrom(item);
                        var oldParent = old.Parent;
                        if (oldParent != null)
                        {
                            _logger.InfoFormat("Removing old version of {0}:{1} from {2}:{3}", item.ID, GetContentItemName(item), oldParent.ID, GetContentItemName(oldParent));
                            old.AddTo(null);
                            item.AddTo(oldParent); // will be overridden by final run of unresolved links
                        }

                        var oldChildPageIds = old.Children.Where(i => i.IsPage).Select(i => i.ID).ToArray();
                        foreach (var oldChildPageId in oldChildPageIds)
                        {
                            if (Database.ContainsKey(oldChildPageId))
                            {
                                var oldChildPage = Database[oldChildPageId];
                                if (item.Children.Any(i => i.ID == oldChildPageId))
                                {
                                    var itemToRemove = item.Children.First(i => i.ID == oldChildPageId);
                                    item.Children.Remove(itemToRemove);
                                }

                                _logger.InfoFormat("Transfering children from old to new version of {0}:{1}", item.ID, GetContentItemName(item));
                                oldChildPage.AddTo(item);
                                old.Children.Remove(oldChildPage);
                            }
                            else
                            {
                                _logger.Error(string.Format("Unable to link child page {0} to item {1}.", oldChildPageId, item.ID));
                            }
                        }
                        ContentVersion.ReorderBySortOrderRecursive(item);
#if DEBUG
                        var oldChildren = old.Children.Select(c => c.ID).ToList();
                        var newChildren = item.Children.Select(c => c.ID).ToList();
                        if (!(newChildren.All(oldChildren.Contains) && oldChildren.All(newChildren.Contains)))
                        {
                            _logger.DebugFormat("Replacing data {0}  - children {1} -> {2}",
                                                item.ID, string.Join(",", oldChildren), string.Join(",", newChildren));
                        }
#endif
                        Delete(old);
                    }

                    _logger.DebugFormat("Adding or Replacing item {0}:{1}", item.ID, item.Name);
                    Database[item.ID] = item;

                    if (item.IsPage)
                    {
                        InternalDeleteFiles(GetPath(item, true));
                        var path = Path.Combine(DataDirectoryPhysical, GetPath(item));
                        _logger.InfoFormat("Creating file \"{0}\"", path);
                        XmlPersistenceUtility.SaveXml(exporter, item, path);
                    }
                    else if (item.Parent != null)
                    {
                        SaveOrUpdate(item.Parent); // TODO optimize multiple saves during import by creating a real xml transaction on the page level
                    }
                    else
                    {
                        throw new ArgumentException("Invalid item: not a page and parent = null");
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error("SaveOrUpdate failed on " + item, ex);
            }
        }