Skip to content

Commit

Permalink
Move the ProcessCleanAction method into the base, so any handler can …
Browse files Browse the repository at this point in the history
…do it.
  • Loading branch information
KevinJump committed Sep 12, 2023
1 parent 56caeea commit 2a7a348
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 43 deletions.
2 changes: 1 addition & 1 deletion uSync.BackOffice/Services/uSyncService_Single.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public IEnumerable<uSyncAction> ReportPartial(string folder, uSyncPagedImportOpt

if (handlerPair == null)
{
_logger.LogWarning("No handler was found for {alias} item might not process correctly", itemType);
_logger.LogWarning("No handler was found for {alias} ({itemType}) item might not process correctly", itemType);
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion uSync.BackOffice/SyncHandlers/Handlers/ContentHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace uSync.BackOffice.SyncHandlers.Handlers
/// </summary>
[SyncHandler(uSyncConstants.Handlers.ContentHandler, "Content", "Content", uSyncConstants.Priorites.Content
, Icon = "icon-document usync-addon-icon", IsTwoPass = true, EntityType = UdiEntityType.Document)]
public class ContentHandler : ContentHandlerBase<IContent, IContentService>, ISyncHandler, ISyncCleanEntryHandler,
public class ContentHandler : ContentHandlerBase<IContent, IContentService>, ISyncHandler,
INotificationHandler<SavedNotification<IContent>>,
INotificationHandler<DeletedNotification<IContent>>,
INotificationHandler<MovedNotification<IContent>>,
Expand Down
27 changes: 3 additions & 24 deletions uSync.BackOffice/SyncHandlers/Handlers/ContentHandlerBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

Expand All @@ -10,7 +9,6 @@
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Web.BackOffice.Middleware;
using Umbraco.Extensions;

using uSync.BackOffice.Configuration;
Expand All @@ -28,7 +26,7 @@ namespace uSync.BackOffice.SyncHandlers.Handlers
/// places around the tree, so we have to check for file name
/// clashes.
/// </remarks>
public abstract class ContentHandlerBase<TObject, TService> : SyncHandlerTreeBase<TObject, TService>
public abstract class ContentHandlerBase<TObject, TService> : SyncHandlerTreeBase<TObject, TService>
where TObject : IContentBase
where TService : IService
{
Expand Down Expand Up @@ -72,8 +70,8 @@ protected override string GetXmlMatchString(XElement node)

/*
* Config options.
* Include = Paths (comma seperated) (only include if path starts with one of these)
* Exclude = Paths (comma seperated) (exclude if path starts with one of these)
* Include = Paths (comma separated) (only include if path starts with one of these)
* Exclude = Paths (comma separated) (exclude if path starts with one of these)
*
* RulesOnExport = bool (do we apply the rules on export as well as import?)
*/
Expand Down Expand Up @@ -198,25 +196,6 @@ protected override bool ShouldExport(XElement node, HandlerSettings config)
protected override bool DoActionsMatch(uSyncAction a, uSyncAction b)
=> a.key == b.key;

/// <summary>
/// Process any cleanup actions that may have been loaded up
/// </summary>
public virtual IEnumerable<uSyncAction> ProcessCleanActions(string folder, IEnumerable<uSyncAction> actions, HandlerSettings config)
{
var cleans = actions.Where(x => x.Change == ChangeType.Clean && !string.IsNullOrWhiteSpace(x.FileName)).ToList();
if (cleans.Count == 0) return Enumerable.Empty<uSyncAction>();

var results = new List<uSyncAction>();

foreach (var clean in cleans)
{
if (!string.IsNullOrWhiteSpace(clean.FileName))
results.AddRange(CleanFolder(clean.FileName, false, config.UseFlatStructure));
}

return results;
}

/// <summary>
/// Handle the Umbraco Moved to recycle bin notification, (treated like a move)
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions uSync.BackOffice/SyncHandlers/Handlers/LanguageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,5 +210,12 @@ public override void Handle(SavedNotification<ILanguage> notification)

}
}

/// <summary>
/// we don't support language deletion (because the keys are unstable)
/// </summary>
protected override IEnumerable<uSyncAction> DeleteMissingItems(int parentId, IEnumerable<Guid> keys, bool reportOnly)
=> Enumerable.Empty<uSyncAction>();

}
}
51 changes: 38 additions & 13 deletions uSync.BackOffice/SyncHandlers/SyncHandlerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

using Microsoft.Extensions.Logging;

using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Entities;
Expand All @@ -15,19 +14,17 @@

using uSync.BackOffice.Configuration;
using uSync.BackOffice.Services;
using uSync.BackOffice.SyncHandlers.Interfaces;
using uSync.Core;
using uSync.Core.Models;

using static System.Net.WebRequestMethods;

namespace uSync.BackOffice.SyncHandlers
{
/// <summary>
/// Base class for any Handlers that manage IEntity type objects
/// </summary>
public abstract class SyncHandlerBase<TObject, TService>
: SyncHandlerRoot<TObject, IEntity>

: SyncHandlerRoot<TObject, IEntity>, ISyncCleanEntryHandler
where TObject : IEntity
where TService : IService
{
Expand Down Expand Up @@ -96,14 +93,29 @@ protected override IEnumerable<uSyncAction> CleanFolder(string cleanFile, bool r

private int GetCleanParentId(string cleanFile)
{
var parent = GetCleanParent(cleanFile);
if (parent == null)
var node = XElement.Load(cleanFile);
var id = node.Attribute("Id").ValueOrDefault(0);
if (id != 0) return id;
return GetCleanParent(cleanFile)?.Id ?? 0;
}

/// <summary>
/// Process any cleanup actions that may have been loaded up
/// </summary>
public virtual IEnumerable<uSyncAction> ProcessCleanActions(string folder, IEnumerable<uSyncAction> actions, HandlerSettings config)
{
var cleans = actions.Where(x => x.Change == ChangeType.Clean && !string.IsNullOrWhiteSpace(x.FileName)).ToList();
if (cleans.Count == 0) return Enumerable.Empty<uSyncAction>();

var results = new List<uSyncAction>();

foreach (var clean in cleans)
{
var node = XElement.Load(cleanFile);
var id = node.Attribute("Id").ValueOrDefault(0);
if (id == Constants.System.Root) return Constants.System.Root;
if (!string.IsNullOrWhiteSpace(clean.FileName))
results.AddRange(CleanFolder(clean.FileName, false, config.UseFlatStructure));
}
return parent?.Id ?? 0;

return results;
}

/// <inheritdoc/>
Expand All @@ -120,15 +132,28 @@ protected override IEnumerable<uSyncAction> DeleteMissingItems(int parentId, IEn
var actions = new List<uSyncAction>();
foreach (var item in items.Where(x => !keys.Contains(x.Key)))
{
logger.LogDebug("DeleteMissingItems: Found {item} that is not in file list (Reporting: {reportOnly})", item.Id, reportOnly);

var name = String.Empty;
if (item is IEntitySlim slim) name = slim.Name;

if (string.IsNullOrEmpty(name) || !reportOnly)
{
var actualItem = GetFromService(item.Key);
var actualItem = GetFromService(item.Id);
if (actualItem == null)
{
logger.LogDebug("Actual Item {id} can't be found", item.Id);
continue;
}

name = GetItemName(actualItem);

// actually do the delete if we are really not reporting
if (!reportOnly) DeleteViaService(actualItem);
if (!reportOnly)
{
logger.LogInformation("Deleting item: {id} {name} as part of a 'clean' import", actualItem.Id, name);
DeleteViaService(actualItem);
}
}

// for reporting - we use the entity name,
Expand Down
4 changes: 2 additions & 2 deletions uSync.BackOffice/SyncHandlers/SyncHandlerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public IEnumerable<HandlerConfigPair> GetValidHandlers(SyncHandlerOptions option
{
if (!options.IncludeDisabled && handlerSetSettings.DisabledHandlers.InvariantContains(handler.Alias))
{
_logger.LogDebug("Handler {hadler} is in the disabled handler list", handler.Alias);
_logger.LogTrace("Handler {handler} is in the disabled handler list", handler.Alias);
continue;
}

Expand All @@ -220,7 +220,7 @@ public IEnumerable<HandlerConfigPair> GetValidHandlers(SyncHandlerOptions option
{
_logger.LogDebug("No Handler with {alias} has been loaded", handler.Alias);
// only log if we are doing the default 'everything' group
// because weh nfoing groups we choose not to load things.
// because when foing groups we choose not to load things.
if (string.IsNullOrWhiteSpace(options.Group))
_logger.LogWarning("No Handler with {alias} has been loaded", handler.Alias);
}
Expand Down
6 changes: 5 additions & 1 deletion uSync.BackOffice/SyncHandlers/SyncHandlerRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,10 @@ virtual public IEnumerable<uSyncAction> ImportSecondPass(uSyncAction action, Han
try
{
var file = action.FileName;

if (!syncFileService.FileExists(file))
return Enumerable.Empty<uSyncAction>();

var node = syncFileService.LoadXElement(file);
var item = GetFromService(node.GetKey());
if (item == null) return Enumerable.Empty<uSyncAction>();
Expand Down Expand Up @@ -632,7 +636,7 @@ protected IList<Guid> GetFolderKeys(string folder, bool flat)
{
var node = XElement.Load(file);
var key = node.GetKey();
if (!keys.Contains(key))
if (key != Guid.Empty && !keys.Contains(key))
{
keys.Add(key);
}
Expand Down
3 changes: 2 additions & 1 deletion uSync.Core/Serialization/SyncSerializerRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,11 @@ protected SyncAttempt<TObject> ProcessAction(XElement node, SyncSerializerOption

var actionType = node.Attribute("Change").ValueOrDefault<SyncActionType>(SyncActionType.None);

logger.LogDebug("Empty Node : Processing Action {0}", actionType);

var (key, alias) = FindKeyAndAlias(node);

logger.LogDebug("Empty Node : Processing Action {actionType} ({key} {alias})", actionType, key, alias);

switch (actionType)
{
case SyncActionType.Delete:
Expand Down

0 comments on commit 2a7a348

Please sign in to comment.