Skip to content

Commit

Permalink
Merge RelationTypes into ContentEdition.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Jump committed May 18, 2020
1 parent 4ff7599 commit 28bcf47
Show file tree
Hide file tree
Showing 15 changed files with 569 additions and 4 deletions.
4 changes: 3 additions & 1 deletion uSync8.BackOffice/BackOfficeConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public static class Priorites

public const int DomainSettings = USYNC_RESERVED_LOWER + 219;

public const int DataTypeMappings = USYNC_RESERVED_LOWER + 220;
public const int RelationTypes = USYNC_RESERVED_LOWER + 220;

public const int DataTypeMappings = USYNC_RESERVED_LOWER + 250;
}

public static class Groups
Expand Down
14 changes: 14 additions & 0 deletions uSync8.BackOffice/Configuration/uSyncHandlerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Umbraco.Core;

namespace uSync8.BackOffice.Configuration
{
Expand All @@ -27,6 +28,19 @@ public HandlerSettings(string alias, bool enabled)
Alias = alias;
Enabled = enabled;
}

public TResult GetSetting<TResult>(string key, TResult defaultValue)
{
if (this.Settings != null && this.Settings.ContainsKey(key))
{
var attempt = this.Settings[key].TryConvertTo<TResult>();
if (attempt.Success)
return attempt.Result;
}

return defaultValue;
}

}

}
4 changes: 2 additions & 2 deletions uSync8.BackOffice/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("8.6.2.0")]
[assembly: AssemblyFileVersion("8.6.2.0")]
[assembly: AssemblyVersion("8.7.0")]
[assembly: AssemblyFileVersion("8.7.0")]
2 changes: 1 addition & 1 deletion uSync8.BackOffice/Services/uSyncService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public IEnumerable<uSyncAction> Import(string folder, bool force, IEnumerable<Ex
var summary = new SyncProgressSummary(handlers.Select(x => x.Handler), "Importing", handlers.Count() + 1);
summary.Handlers.Add(new SyncHandlerSummary()
{
Icon = "icon-traffic",
Icon = "icon-defrag",
Name = "Post Import",
Status = HandlerStatus.Pending
});
Expand Down
27 changes: 27 additions & 0 deletions uSync8.ContentEdition/Checkers/RelationTypeChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Umbraco.Core;
using Umbraco.Core.Models;
using uSync8.Core.Dependency;

namespace uSync8.ContentEdition.Checkers
{
public class RelationTypeChecker : ISyncDependencyChecker<IRelationType>
{
public UmbracoObjectTypes ObjectType => UmbracoObjectTypes.RelationType;

public IEnumerable<uSyncDependency> GetDependencies(IRelationType item, DependencyFlags flags)
{
return new uSyncDependency()
{
Name = item.Name,
Udi = item.GetUdi(),
Order = DependencyOrders.RelationTypes,
Flags = flags
}.AsEnumerableOfOne();
}
}
}
33 changes: 33 additions & 0 deletions uSync8.ContentEdition/Config/uSync8.config.install.xdt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8" ?>
<uSync xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<BackOffice>
<HandlerSets>
<Handlers Name="default" xdt:Locator="Match(Name)">
<Handler Alias="contentHandler" Enabled="true" Actions="All"
xdt:Locator="Match(Alias)"
xdt:Transform="Insert"/>

<Handler Alias="mediaHandler" Enabled="true" Actions="All"
xdt:Locator="Match(Alias)"
xdt:Transform="Insert"/>

<Handler Alias="domainHandler" Enabled="true" Actions="All"
xdt:Locator="Match(Alias)"
xdt:Transform="Insert"/>

<Handler Alias="dictionaryHandler" Enabled="true" Actions="All"
xdt:Locator="Match(Alias)"
xdt:Transform="Insert"/>

<Handler Alias="contentTemplateHandler" Enabled="true" Actions="All"
xdt:Locator="Match(Alias)"
xdt:Transform="Insert"/>

<Handler Alias="relationTypeHandler" Enabled="true" Actions="All"
xdt:Locator="Match(Alias)"
xdt:Transform="Insert"/>

</Handlers>
</HandlerSets>
</BackOffice>
</uSync>
32 changes: 32 additions & 0 deletions uSync8.ContentEdition/Config/uSync8.config.uninstall.xdf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8" ?>
<uSync xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<BackOffice>
<HandlerSets>
<Handlers Name="default" xdt:Locator="Match(Name)">
<Handler Alias="contentHandler" Enabled="true" Actions="All"
xdt:Locator="Match(Alias)"
xdt:Transform="Remove"/>

<Handler Alias="mediaHandler" Enabled="true" Actions="All"
xdt:Locator="Match(Alias)"
xdt:Transform="Remove"/>

<Handler Alias="domainHandler" Enabled="true" Actions="All"
xdt:Locator="Match(Alias)"
xdt:Transform="Remove"/>

<Handler Alias="dictionaryHandler" Enabled="true" Actions="All"
xdt:Locator="Match(Alias)"
xdt:Transform="Remove"/>

<Handler Alias="contentTemplateHandler" Enabled="true" Actions="All"
xdt:Locator="Match(Alias)"
xdt:Transform="Remove"/>

<Handler Alias="relationTypeHandler" Enabled="true" Actions="All"
xdt:Locator="Match(Alias)"
xdt:Transform="Remove"/>
</Handlers>
</HandlerSets>
</BackOffice>
</uSync>
189 changes: 189 additions & 0 deletions uSync8.ContentEdition/Handlers/RelationTypeHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Timers;
using System.Xml.Linq;
using NPoco.Expressions;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Core.Services.Implement;
using uSync8.BackOffice;
using uSync8.BackOffice.Configuration;
using uSync8.BackOffice.Services;
using uSync8.BackOffice.SyncHandlers;
using uSync8.Core.Dependency;
using uSync8.Core.Extensions;
using uSync8.Core.Serialization;
using uSync8.Core.Tracking;
using static Umbraco.Core.Constants;

namespace uSync8.ContentEdition.Handlers
{
[SyncHandler("relationTypeHandler", "Relations",
"RelationTypes", uSyncBackOfficeConstants.Priorites.RelationTypes,
Icon = "icon-traffic usync-addon-icon",
EntityType = UdiEntityType.RelationType, IsTwoPass = false)]
public class RelationTypeHandler : SyncHandlerBase<IRelationType, IRelationService>,
ISyncExtendedHandler
{
private readonly IRelationService relationService;

public override string Group => uSyncBackOfficeConstants.Groups.Content;

public RelationTypeHandler(
IEntityService entityService,
IRelationService relationService,
IProfilingLogger logger,
ISyncSerializer<IRelationType> serializer,
ISyncTracker<IRelationType> tracker,
AppCaches appCaches,
SyncDependencyCollection checkers,
SyncFileService syncFileService)
: base(entityService, logger, serializer, tracker, appCaches, checkers, syncFileService)
{
this.relationService = relationService;
}

public override IEnumerable<uSyncAction> ExportAll(string folder, HandlerSettings config, SyncUpdateCallback callback)
{
var actions = new List<uSyncAction>();

var items = relationService.GetAllRelationTypes().ToList();

foreach (var item in items.Select((relationType, index) => new { relationType, index }))
{
callback?.Invoke(item.relationType.Name, item.index, items.Count);
actions.AddRange(Export(item.relationType, folder, config));
}

return actions;
}

/// <summary>
/// Workout if we are excluding this relationType from export/import
/// </summary>
protected override bool ShouldExport(XElement node, HandlerSettings config)
{
var exclude = config.GetSetting<string>("Exclude", string.Empty);

if (!string.IsNullOrWhiteSpace(exclude) && exclude.Contains(node.GetAlias()))
return false;

return true;
}

protected override bool ShouldImport(XElement node, HandlerSettings config)
=> ShouldExport(node, config);


protected override void DeleteViaService(IRelationType item)
=> relationService.Delete(item);

protected override IRelationType GetFromService(int id)
=> relationService.GetRelationTypeById(id);

protected override IRelationType GetFromService(Guid key)
=> relationService.GetRelationTypeById(key);

protected override IRelationType GetFromService(string alias)
=> relationService.GetRelationTypeByAlias(alias);

protected override string GetItemName(IRelationType item)
=> item.Name;

protected override string GetItemPath(IRelationType item, bool useGuid, bool isFlat)
=> useGuid ? item.Key.ToString() : item.Alias.ToSafeAlias();

protected override void InitializeEvents(HandlerSettings settings)
{
RelationService.SavedRelationType += EventSavedItem;
RelationService.DeletedRelationType += EventDeletedItem;

if (settings.GetSetting<bool>("IncludeRelations", false))
{
// relation saving is noisy, for example if you copy a load of
// pages the save event fires a lot.
RelationService.SavedRelation += RelationService_SavedRelation;
RelationService.DeletedRelation += RelationService_DeletedRelation;

// the lock and timer are used so we don't do multiple saves
// instead we queue things and when nothing has changed
// for about 4 seconds, then we save everything in the queue.
saveTimer = new Timer(4064);
saveTimer.Elapsed += SaveTimer_Elapsed;

pendingSaveIds = new List<int>();
saveLock = new object();
}
}

private void RelationService_SavedRelation(IRelationService sender, Umbraco.Core.Events.SaveEventArgs<IRelation> e)
{
if (uSync8BackOffice.eventsPaused) return;

lock (saveLock)
{
saveTimer.Stop();
saveTimer.Start();

// add each item to the save list (if we haven't already)
foreach (var item in e.SavedEntities)
{
if (!pendingSaveIds.Contains(item.RelationTypeId))
pendingSaveIds.Add(item.RelationTypeId);
}
}
}

private void SaveTimer_Elapsed(object sender, ElapsedEventArgs e)
{
lock (saveLock)
{
UpdateRelationTypes(pendingSaveIds);
pendingSaveIds.Clear();
}
}

private static Timer saveTimer;
private static List<int> pendingSaveIds;
private static object saveLock;

private void RelationService_DeletedRelation(IRelationService sender, Umbraco.Core.Events.DeleteEventArgs<IRelation> e)
{
if (uSync8BackOffice.eventsPaused) return;

var types = new List<int>();

foreach (var item in e.DeletedEntities)
{
if (!types.Contains(item.RelationTypeId))
types.Add(item.RelationTypeId);
}

UpdateRelationTypes(types);
}

private void UpdateRelationTypes(IEnumerable<int> types)
{
foreach (var type in types)
{
var relationType = relationService.GetRelationTypeById(type);

var attempts = Export(relationType, Path.Combine(rootFolder, this.DefaultFolder), DefaultConfig);

if (!(this.DefaultConfig.GuidNames && this.DefaultConfig.UseFlatStructure))
{
foreach (var attempt in attempts.Where(x => x.Success))
{
this.CleanUp(relationType, attempt.FileName, Path.Combine(rootFolder, this.DefaultFolder));
}
}
}
}
}
}
Loading

0 comments on commit 28bcf47

Please sign in to comment.