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); }
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); }