private string GetExportIds(string value)
        {
            Dictionary <string, string> replacements = new Dictionary <string, string>();

            foreach (Match m in Regex.Matches(value, @"\d{1,9}"))
            {
                int id;

                if (int.TryParse(m.Value, out id))
                {
                    Guid?localGuid = GetGuidFromId(id);
                    if (localGuid != null)
                    {
                        if (!replacements.ContainsKey(m.Value))
                        {
                            Guid sourceGuid = NodeIdMapper.GetSourceGuid(localGuid.Value);
                            replacements.Add(m.Value, sourceGuid.ToString());
                        }
                    }
                }
            }

            foreach (KeyValuePair <string, string> pair in replacements)
            {
                value = value.Replace(pair.Key, pair.Value);
            }

            return(value);
        }
        /// <summary>
        ///  turns a Guid into a ID piece of content
        /// </summary>
        /// <param name="guid">the guid from a bit of conent (will be using a master guid)</param>
        /// <returns>the ID for the local content with that guid</returns>
        internal int GetIdFromGuid(Guid guid)
        {
            Guid sourceGuid = NodeIdMapper.GetTargetGuid(guid);

            var c = ApplicationContext.Current.Services.ContentService.GetById(sourceGuid);

            if (c != null)
            {
                return(c.Id);
            }

            // try media
            var m = ApplicationContext.Current.Services.MediaService.GetById(sourceGuid);

            if (m != null)
            {
                return(m.Id);
            }

            return(-1);
        }
        public XElement uSyncIContentExportBase(IContentBase item, string type)
        {
            var node = new XElement(type);

            Guid sourceGuid = NodeIdMapper.GetSourceGuid(item.Key);

            node.Add(new XAttribute("guid", sourceGuid));
            node.Add(new XAttribute("id", item.Id));
            node.Add(new XAttribute("nodeName", item.Name));
            node.Add(new XAttribute("isDoc", ""));
            node.Add(new XAttribute("updated", item.UpdateDate));

            foreach (var prop in item.Properties.Where(p => p != null))
            {
                XElement propNode = null;

                try
                {
                    propNode = prop.ToXml();
                }
                // sometime you can't serialize the property
                catch
                {
                    propNode = new XElement(prop.Alias, prop.Value);
                }

                string xml = "";
                xml = GetExportIds(GetInnerXML(propNode));

                var updatedNode = XElement.Parse(
                    String.Format("<{0}>{1}</{0}>", propNode.Name.ToString(), xml),
                    LoadOptions.PreserveWhitespace);

                node.Add(updatedNode);
            }

            return(node);
        }
示例#4
0
        public IMedia Import(XElement node, bool forceUpdate = false, int parentId = -1)
        {
            bool _new = false;

            var guidNode = node.Attribute("guid");

            if (guidNode == null)
            {
                return(null);
            }

            Guid mediaGuid = new Guid(guidNode.Value);

            Guid _guid = NodeIdMapper.GetTargetGuid(mediaGuid);

            string name       = node.Attribute("nodeName").Value;
            string mediaAlias = node.Attribute("mediaTypeAlias").Value;

            DateTime updateDate = DateTime.Now;

            if (node.Attribute("updated") != null)
            {
                updateDate = DateTime.Parse(node.Attribute("updated").Value);
            }

            IMedia item = ApplicationContext.Current.Services.MediaService.GetById(_guid);

            if (item != null)
            {
                _new = true;
                item = ApplicationContext.Current.Services.MediaService.CreateMedia(name, parentId, mediaAlias);
            }
            else
            {
                if (item.Trashed)
                {
                    // trashed, so we create new one...
                    _new = true;
                    item = ApplicationContext.Current.Services.MediaService.CreateMedia(name, parentId, mediaAlias);
                }
                else
                {
                    // not new - we can do some is this updated stuff..

                    if (!forceUpdate)
                    {
                        if (DateTime.Compare(updateDate, item.UpdateDate.ToLocalTime()) < 0)
                        {
                            // xml is older - don't the update
                            return(item);
                        }
                    }
                }
            }

            if (item != null)
            {
                // we do this which checks - because it stops
                // the properties getting marked as dirty when
                // their is no change - making it a bit faster
                if (item.Name != name)
                {
                    item.Name = name;
                }

                if (item.ParentId != parentId)
                {
                    item.ParentId = parentId;
                }

                // do the impressive file import here....
                // ImportMediaFile(mediaGuid, item);

                ApplicationContext.Current.Services.MediaService.Save(item);
            }

            if (_new)
            {
                NodeIdMapper.AddPair(mediaGuid, item.Key);
            }

            return(item);
        }
示例#5
0
        public IContent Import(XElement node, bool forceUpdate = false, int parentId = -1)
        {
            bool newItem = false;

            var guidNode = node.Attribute("guid");

            if (guidNode == null)
            {
                return(null);
            }

            Guid contentGuid = new Guid(guidNode.Value);
            Guid _guid       = NodeIdMapper.GetTargetGuid(contentGuid);

            var name          = node.Attribute("nodeName").Value;
            var nodeType      = node.Attribute("nodeTypeAlias").Value;
            var templateAlias = node.Attribute("templateAlias").Value;

            var sortOrder = int.Parse(node.Attribute("sortOrder").Value);
            var published = bool.Parse(node.Attribute("published").Value);

            // try to load the content
            var _contentService = ApplicationContext.Current.Services.ContentService;
            var item            = _contentService.GetById(_guid);

            if (item == null)
            {
                item    = _contentService.CreateContent(name, parentId, nodeType);
                newItem = true;
            }
            else
            {
                if (item.Trashed)
                {
                    // in the bin - we create a new one.
                    item    = _contentService.CreateContent(name, parentId, nodeType);
                    newItem = true;
                }
                else
                {
                    // it's not new
                    if (!forceUpdate)
                    {
                        // do some checking to see if we can skip an update
                        // for content we do this based on update date.
                        DateTime updateTime = DateTime.Now;
                        if (node.Element("updated") != null)
                        {
                            updateTime = DateTime.Parse(node.Element("updated").Value);
                        }

                        if (DateTime.Compare(updateTime, item.UpdateDate.ToLocalTime()) <= 0)
                        {
                            // no change
                            return(item);
                        }
                    }
                }
            }

            // shouldn't happen but might if create fails for some reason
            if (item == null)
            {
                return(null);
            }

            var template = ApplicationContext.Current.Services.FileService.GetTemplate(templateAlias);

            if (template != null)
            {
                item.Template = template;
            }

            item.SortOrder = sortOrder;
            item.Name      = name;

            if (item.ParentId != parentId)
            {
                item.ParentId = parentId;
            }

            var props = from property in node.Elements()
                        where property.Attribute("isDoc") == null
                        select property;

            foreach (var prop in props)
            {
                var propTypeAlias = prop.Name.LocalName;
                if (item.HasProperty(propTypeAlias))
                {
                    item.SetValue(propTypeAlias, GetImportIds(GetImportXML(prop)));
                }
            }


            if (published)
            {
                var attempt = _contentService.SaveAndPublishWithStatus(item, 0, false);
                if (!attempt.Success)
                {
                    // didn't work for some reason
                    LogHelper.Info <uSyncContent>("Failed to publish {0}", () => attempt.Exception.ToString());
                }
            }
            else
            {
                _contentService.Save(item, 0, false);

                // if it was published - we want to unpublish
                if (item.Published)
                {
                    _contentService.UnPublish(item);
                }
            }

            if (newItem)
            {
                // if it was a new item, we need to update our mapping
                // tables
                NodeIdMapper.AddPair(contentGuid, item.Key);
            }

            return(item);
        }