<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Umbraco.Core.Models;

using Jumoo.uSync.Core.Interfaces;
using Jumoo.uSync.Core.Extensions;
using Umbraco.Core;
using Jumoo.uSync.Core.Helpers;
using Umbraco.Core.Logging;

namespace Jumoo.uSync.Core.Serializers
{
    public class ContentSerializer : ContentBaseSerializer<IContent>
    {
        public ContentSerializer() : base(string.Empty)
        { }

        internal override SyncAttempt<IContent> DeserializeCore(XElement node, int parentId, bool forceUpdate = false)
        {
            var nodeGuid = node.Attribute("guid");
            if (nodeGuid == null)
                return SyncAttempt<IContent>.Fail(node.NameFromNode(), ChangeType.Import, "No Guid in XML");

            Guid guid = new Guid(nodeGuid.Value);

            var name = node.Attribute("nodeName").Value;
            var type = 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);

            // because later set the guid, we are going for a match at this point
            var item = _contentService.GetById(guid);
            if (item == null)
            {
                item = _contentService.CreateContent(name, parentId, type);
            }
            else { 
                // update is different for content, we go on publish times..
                if (!forceUpdate)
                {
                    DateTime updateTime = node.Attribute("updated").ValueOrDefault(DateTime.Now);

                    if (DateTime.Compare(updateTime, item.UpdateDate.ToLocalTime()) <= 0)
                    {
                        // the import is older than the content on this site;
                        return SyncAttempt<IContent>.Succeed(item.Name, item, ChangeType.NoChange);
                    }
                }
            }

            if (item == null)
                return SyncAttempt<IContent>.Fail(node.NameFromNode(), ChangeType.ImportFail, "Cannot find or create content item");

            // if it's in the trash remove it. 
            if (item.Trashed)
                item.ChangeTrashedState(false);

            var template = ApplicationContext.Current.Services.FileService.GetTemplate(templateAlias);
            if (template != null)
                item.Template = template;

            item.Key = guid;

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

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

            var properties = node.Elements().Where(x => x.Attribute("isDoc") == null);
            foreach (var property in properties)
            {
                var propertyTypeAlias = property.Name.LocalName;
                if (item.HasProperty(propertyTypeAlias))
                {
                    var propType = item.Properties[propertyTypeAlias].PropertyType;
                    var newValue = GetImportIds(propType, GetImportXml(property));

                    LogHelper.Debug<Events>("#### Setting property: [{0}] to {1}", () => propertyTypeAlias, () => newValue);
                    item.SetValue(propertyTypeAlias, newValue );
                }
            }<commit_msg>Content: Change Content Type on<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Umbraco.Core.Models;

using Jumoo.uSync.Core.Interfaces;
using Jumoo.uSync.Core.Extensions;
using Umbraco.Core;
using Jumoo.uSync.Core.Helpers;
using Umbraco.Core.Logging;

namespace Jumoo.uSync.Core.Serializers
{
    public class ContentSerializer : ContentBaseSerializer<IContent>
    {
        public ContentSerializer() : base(string.Empty)
        { }

        internal override SyncAttempt<IContent> DeserializeCore(XElement node, int parentId, bool forceUpdate = false)
        {
            var nodeGuid = node.Attribute("guid");
            if (nodeGuid == null)
                return SyncAttempt<IContent>.Fail(node.NameFromNode(), ChangeType.Import, "No Guid in XML");

            Guid guid = new Guid(nodeGuid.Value);

            var name = node.Attribute("nodeName").Value;
            var type = 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);

            // because later set the guid, we are going for a match at this point
            var item = _contentService.GetById(guid);
            if (item == null)
            {
                item = _contentService.CreateContent(name, parentId, type);
            }
            else { 
                // update is different for content, we go on publish times..
                if (!forceUpdate)
                {
                    DateTime updateTime = node.Attribute("updated").ValueOrDefault(DateTime.Now);

                    if (DateTime.Compare(updateTime, item.UpdateDate.ToLocalTime()) <= 0)
                    {
                        // the import is older than the content on this site;
                        return SyncAttempt<IContent>.Succeed(item.Name, item, ChangeType.NoChange);
                    }
                }
            }

            if (item == null)
                return SyncAttempt<IContent>.Fail(node.NameFromNode(), ChangeType.ImportFail, "Cannot find or create content item");

            // if it's in the trash remove it. 
            if (item.Trashed)
                item.ChangeTrashedState(false);

            //
            // Change doctype if it changes, we could lose values here, but we 
            // are going to set them all later so should be fine. 
            //
            var contentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(type);
            if (item.ContentTypeId != contentType.Id)
            {
                item.ChangeContentType(contentType);
            }

            var template = ApplicationContext.Current.Services.FileService.GetTemplate(templateAlias);
            if (template != null)
                item.Template = template;

            item.Key = guid;

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

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

            var properties = node.Elements().Where(x => x.Attribute("isDoc") == null);
            foreach (var property in properties)
            {
                var propertyTypeAlias = property.Name.LocalName;
                if (item.HasProperty(propertyTypeAlias))
                {
                    var propType = item.Properties[propertyTypeAlias].PropertyType;
                    var newValue = GetImportIds(propType, GetImportXml(property));

                    LogHelper.Debug<Events>("#### Setting property: [{0}] to {1}", () => propertyTypeAlias, () => newValue);
                    item.SetValue(propertyTypeAlias, newValue );
                }
            }
{'fraction_non_alphanumeric': 0.09956355701036552, 'fraction_numerical': 0.0008183306055646482, 'mean_word_length': 1.8984189723320157, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5929444', 'n_tokens_mistral': 1883, 'n_tokens_neox': 1798, 'n_words': 448}