Esempio n. 1
0
        protected override SyncAttempt <IContent> DeserializeCore(XElement node, SyncSerializerOptions options)
        {
            var attempt = FindOrCreate(node);

            if (!attempt.Success)
            {
                throw attempt.Exception;
            }

            var item = attempt.Result;

            var details = new List <uSyncChange>();

            details.AddRange(DeserializeBase(item, node, options));

            if (node.Element("Info") != null)
            {
                var trashed = node.Element("Info").Element("Trashed").ValueOrDefault(false);
                details.AddNotNull(HandleTrashedState(item, trashed));
            }

            details.AddNotNull(DeserializeTemplate(item, node));

            var propertiesAttempt = DeserializeProperties(item, node, options);

            if (!propertiesAttempt.Success)
            {
                return(SyncAttempt <IContent> .Fail(item.Name, item, ChangeType.ImportFail, "Failed to deserialize properties", attempt.Exception));
            }

            details.AddRange(propertiesAttempt.Result);

            // sort order
            var sortOrder = node.Element("Info").Element("SortOrder").ValueOrDefault(-1);

            details.AddNotNull(HandleSortOrder(item, sortOrder));

            var publishTimer = Stopwatch.StartNew();


            if (details.HasWarning() && options.FailOnWarnings())
            {
                // Fail on warning. means we don't save or publish because something is wrong ?
                return(SyncAttempt <IContent> .Fail(item.Name, item, ChangeType.ImportFail, "Failed with warnings", details,
                                                    new Exception("Import failed because of warnings, and fail on warnings is true")));
            }

            // published status
            // this does the last save and publish
            var saveAttempt = DoSaveOrPublish(item, node, options);

            if (saveAttempt.Success)
            {
                var message = saveAttempt.Result;

                if (details.Any(x => x.Change == ChangeDetailType.Warning))
                {
                    message += $" with warning(s)";
                }

                if (publishTimer.ElapsedMilliseconds > 10000)
                {
                    message += $" (Slow publish {publishTimer.ElapsedMilliseconds}ms)";
                }

                var changeType = options.GetSetting(uSyncConstants.DefaultSettings.OnlyPublishDirty, uSyncConstants.DefaultSettings.OnlyPublishDirty_Default) && !item.IsDirty()
                    ? ChangeType.NoChange : ChangeType.Import;

                // we say no change back, this stops the core second pass function from saving
                // this item (which we have just done with DoSaveOrPublish)
                return(SyncAttempt <IContent> .Succeed(item.Name, item, changeType, message, true, details));
            }
            else
            {
                return(SyncAttempt <IContent> .Fail(item.Name, item, ChangeType.ImportFail, saveAttempt.Result, saveAttempt.Exception));
            }
        }
Esempio n. 2
0
        protected override SyncAttempt <IMedia> DeserializeCore(XElement node, SyncSerializerOptions options)
        {
            var attempt = FindOrCreate(node);

            if (!attempt.Success)
            {
                throw attempt.Exception;
            }

            var item = attempt.Result;

            var details = new List <uSyncChange>();

            details.AddRange(DeserializeBase(item, node, options));

            if (node.Element("Info") != null)
            {
                var trashed = node.Element("Info").Element("Trashed").ValueOrDefault(false);
                details.AddNotNull(HandleTrashedState(item, trashed));
            }

            var propertyAttempt = DeserializeProperties(item, node, options);

            if (!propertyAttempt.Success)
            {
                return(SyncAttempt <IMedia> .Fail(item.Name, item, ChangeType.Fail, "Failed to save properties", propertyAttempt.Exception));
            }

            var info = node.Element("Info");

            var sortOrder = info.Element("SortOrder").ValueOrDefault(-1);

            HandleSortOrder(item, sortOrder);


            if (details.HasWarning() && options.FailOnWarnings())
            {
                // Fail on warning. means we don't save or publish because something is wrong ?
                return(SyncAttempt <IMedia> .Fail(item.Name, item, ChangeType.ImportFail, "Failed with warnings", details,
                                                  new Exception("Import failed because of warnings, and fail on warnings is true")));
            }

            var saveAttempt = mediaService.Save(item);

            if (!saveAttempt.Success)
            {
                var errors = saveAttempt.Result?.EventMessages?.FormatMessages() ?? "";
                return(SyncAttempt <IMedia> .Fail(item.Name, item, ChangeType.Fail, errors, saveAttempt.Exception));
            }

            // add warning messages if things are missing
            var message = "";

            if (details.Any(x => x.Change == ChangeDetailType.Warning))
            {
                message += $" with warning(s)";
            }

            // setting the saved flag on the attempt to true, stops base classes from saving the item.
            return(SyncAttempt <IMedia> .Succeed(item.Name, item, ChangeType.Import, "", true, propertyAttempt.Result));
        }