Skip to content

Commit d007fa0

Browse files
Add folderkeys to package xml. (#11172)
* Add folderkeys to package xml. * Cleanup * Use folderKeys to find child * Fix potential issue with when no folder * Fixed logic issue. Co-authored-by: Andy Butland <[email protected]>
1 parent 22546fd commit d007fa0

File tree

9 files changed

+71
-28
lines changed

9 files changed

+71
-28
lines changed

src/Umbraco.Core/Services/IContentTypeServiceBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public interface IContentTypeBaseService<TItem> : IContentTypeBaseService, IServ
7878
/// <returns></returns>
7979
bool HasContainerInPath(params int[] ids);
8080

81-
Attempt<OperationResult<OperationResultType, EntityContainer>> CreateContainer(int parentContainerId, string name, int userId = Constants.Security.SuperUserId);
81+
Attempt<OperationResult<OperationResultType, EntityContainer>> CreateContainer(int parentContainerId, Guid key, string name, int userId = Constants.Security.SuperUserId);
8282
Attempt<OperationResult> SaveContainer(EntityContainer container, int userId = Constants.Security.SuperUserId);
8383
EntityContainer GetContainer(int containerId);
8484
EntityContainer GetContainer(Guid containerId);

src/Umbraco.Core/Services/IDataTypeService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public interface IDataTypeService : IService
1717
/// <returns></returns>
1818
IReadOnlyDictionary<Udi, IEnumerable<string>> GetReferences(int id);
1919

20-
Attempt<OperationResult<OperationResultType, EntityContainer>> CreateContainer(int parentId, string name, int userId = Constants.Security.SuperUserId);
20+
Attempt<OperationResult<OperationResultType, EntityContainer>> CreateContainer(int parentId, Guid key, string name, int userId = Constants.Security.SuperUserId);
2121
Attempt<OperationResult> SaveContainer(EntityContainer container, int userId = Constants.Security.SuperUserId);
2222
EntityContainer GetContainer(int containerId);
2323
EntityContainer GetContainer(Guid containerId);
@@ -68,7 +68,7 @@ public interface IDataTypeService : IService
6868
/// <param name="dataTypeDefinitions"><see cref="IDataType"/> to save</param>
6969
/// <param name="userId">Id of the user issuing the save</param>
7070
void Save(IEnumerable<IDataType> dataTypeDefinitions, int userId = Constants.Security.SuperUserId);
71-
71+
7272
/// <summary>
7373
/// Deletes an <see cref="IDataType"/>
7474
/// </summary>

src/Umbraco.Infrastructure/Packaging/PackageDataInstallation.cs

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -546,13 +546,33 @@ private Dictionary<string, int> CreateContentTypeFolderStructure(IEnumerable<XEl
546546
{
547547
var alias = documentType.Element("Info").Element("Alias").Value;
548548
var folders = foldersAttribute.Value.Split(Constants.CharArrays.ForwardSlash);
549+
550+
var folderKeysAttribute = documentType.Attribute("FolderKeys");
551+
552+
var folderKeys = Array.Empty<Guid>();
553+
if (folderKeysAttribute != null)
554+
{
555+
folderKeys = folderKeysAttribute.Value.Split(Constants.CharArrays.ForwardSlash).Select(x=>Guid.Parse(x)).ToArray();
556+
}
557+
549558
var rootFolder = WebUtility.UrlDecode(folders[0]);
550-
//level 1 = root level folders, there can only be one with the same name
551-
var current = _contentTypeService.GetContainers(rootFolder, 1).FirstOrDefault();
559+
560+
EntityContainer current;
561+
Guid? rootFolderKey = null;
562+
if (folderKeys.Length == folders.Length && folderKeys.Length > 0)
563+
{
564+
rootFolderKey = folderKeys[0];
565+
current = _contentTypeService.GetContainer(rootFolderKey.Value);
566+
}
567+
else
568+
{
569+
//level 1 = root level folders, there can only be one with the same name
570+
current = _contentTypeService.GetContainers(rootFolder, 1).FirstOrDefault();
571+
}
552572

553573
if (current == null)
554574
{
555-
var tryCreateFolder = _contentTypeService.CreateContainer(-1, rootFolder);
575+
var tryCreateFolder = _contentTypeService.CreateContainer(-1, rootFolderKey ?? Guid.NewGuid(), rootFolder);
556576
if (tryCreateFolder == false)
557577
{
558578
_logger.LogError(tryCreateFolder.Exception, "Could not create folder: {FolderName}", rootFolder);
@@ -567,7 +587,8 @@ private Dictionary<string, int> CreateContentTypeFolderStructure(IEnumerable<XEl
567587
for (var i = 1; i < folders.Length; i++)
568588
{
569589
var folderName = WebUtility.UrlDecode(folders[i]);
570-
current = CreateContentTypeChildFolder(folderName, current);
590+
Guid? folderKey = (folderKeys.Length == folders.Length) ? folderKeys[i] : null;
591+
current = CreateContentTypeChildFolder(folderName, folderKey ?? Guid.NewGuid(), current);
571592
importedFolders[alias] = current.Id;
572593
}
573594
}
@@ -576,17 +597,17 @@ private Dictionary<string, int> CreateContentTypeFolderStructure(IEnumerable<XEl
576597
return importedFolders;
577598
}
578599

579-
private EntityContainer CreateContentTypeChildFolder(string folderName, IUmbracoEntity current)
600+
private EntityContainer CreateContentTypeChildFolder(string folderName, Guid folderKey, IUmbracoEntity current)
580601
{
581602
var children = _entityService.GetChildren(current.Id).ToArray();
582-
var found = children.Any(x => x.Name.InvariantEquals(folderName));
603+
var found = children.Any(x => x.Name.InvariantEquals(folderName) ||x.Key.Equals(folderKey));
583604
if (found)
584605
{
585606
var containerId = children.Single(x => x.Name.InvariantEquals(folderName)).Id;
586607
return _contentTypeService.GetContainer(containerId);
587608
}
588609

589-
var tryCreateFolder = _contentTypeService.CreateContainer(current.Id, folderName);
610+
var tryCreateFolder = _contentTypeService.CreateContainer(current.Id, folderKey, folderName);
590611
if (tryCreateFolder == false)
591612
{
592613
_logger.LogError(tryCreateFolder.Exception, "Could not create folder: {FolderName}", folderName);
@@ -1057,17 +1078,26 @@ private Dictionary<string, int> CreateDataTypeFolderStructure(IEnumerable<XEleme
10571078
foreach (var datatypeElement in datatypeElements)
10581079
{
10591080
var foldersAttribute = datatypeElement.Attribute("Folders");
1081+
10601082
if (foldersAttribute != null)
10611083
{
10621084
var name = datatypeElement.Attribute("Name").Value;
10631085
var folders = foldersAttribute.Value.Split(Constants.CharArrays.ForwardSlash);
1086+
var folderKeysAttribute = datatypeElement.Attribute("FolderKeys");
1087+
1088+
var folderKeys = Array.Empty<Guid>();
1089+
if (folderKeysAttribute != null)
1090+
{
1091+
folderKeys = folderKeysAttribute.Value.Split(Constants.CharArrays.ForwardSlash).Select(x=>Guid.Parse(x)).ToArray();
1092+
}
10641093
var rootFolder = WebUtility.UrlDecode(folders[0]);
1094+
var rootFolderKey = folderKeys.Length > 0 ? folderKeys[0] : Guid.NewGuid();
10651095
//there will only be a single result by name for level 1 (root) containers
10661096
var current = _dataTypeService.GetContainers(rootFolder, 1).FirstOrDefault();
10671097

10681098
if (current == null)
10691099
{
1070-
var tryCreateFolder = _dataTypeService.CreateContainer(-1, rootFolder);
1100+
var tryCreateFolder = _dataTypeService.CreateContainer(-1, rootFolderKey, rootFolder);
10711101
if (tryCreateFolder == false)
10721102
{
10731103
_logger.LogError(tryCreateFolder.Exception, "Could not create folder: {FolderName}", rootFolder);
@@ -1081,7 +1111,8 @@ private Dictionary<string, int> CreateDataTypeFolderStructure(IEnumerable<XEleme
10811111
for (var i = 1; i < folders.Length; i++)
10821112
{
10831113
var folderName = WebUtility.UrlDecode(folders[i]);
1084-
current = CreateDataTypeChildFolder(folderName, current);
1114+
Guid? folderKey = (folderKeys.Length == folders.Length) ? folderKeys[i] : null;
1115+
current = CreateDataTypeChildFolder(folderName, folderKey ?? Guid.NewGuid(), current);
10851116
importedFolders[name] = current.Id;
10861117
}
10871118
}
@@ -1090,17 +1121,17 @@ private Dictionary<string, int> CreateDataTypeFolderStructure(IEnumerable<XEleme
10901121
return importedFolders;
10911122
}
10921123

1093-
private EntityContainer CreateDataTypeChildFolder(string folderName, IUmbracoEntity current)
1124+
private EntityContainer CreateDataTypeChildFolder(string folderName, Guid folderKey, IUmbracoEntity current)
10941125
{
10951126
var children = _entityService.GetChildren(current.Id).ToArray();
1096-
var found = children.Any(x => x.Name.InvariantEquals(folderName));
1127+
var found = children.Any(x => x.Name.InvariantEquals(folderName) ||x.Key.Equals(folderKey));
10971128
if (found)
10981129
{
10991130
var containerId = children.Single(x => x.Name.InvariantEquals(folderName)).Id;
11001131
return _dataTypeService.GetContainer(containerId);
11011132
}
11021133

1103-
var tryCreateFolder = _dataTypeService.CreateContainer(current.Id, folderName);
1134+
var tryCreateFolder = _dataTypeService.CreateContainer(current.Id, folderKey,folderName);
11041135
if (tryCreateFolder == false)
11051136
{
11061137
_logger.LogError(tryCreateFolder.Exception, "Could not create folder: {FolderName}", folderName);

src/Umbraco.Infrastructure/Services/Implement/ContentTypeServiceBaseOfTRepositoryTItemTService.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ public Attempt<OperationResult<MoveOperationStatusType>> Move(TItem moving, int
830830

831831
protected Guid ContainerObjectType => EntityContainer.GetContainerObjectType(ContainedObjectType);
832832

833-
public Attempt<OperationResult<OperationResultType, EntityContainer>> CreateContainer(int parentId, string name, int userId = Cms.Core.Constants.Security.SuperUserId)
833+
public Attempt<OperationResult<OperationResultType, EntityContainer>> CreateContainer(int parentId, Guid key, string name, int userId = Cms.Core.Constants.Security.SuperUserId)
834834
{
835835
EventMessages eventMessages = EventMessagesFactory.Get();
836836
using (IScope scope = ScopeProvider.CreateScope())
@@ -843,7 +843,8 @@ public Attempt<OperationResult<OperationResultType, EntityContainer>> CreateCont
843843
{
844844
Name = name,
845845
ParentId = parentId,
846-
CreatorId = userId
846+
CreatorId = userId,
847+
Key = key
847848
};
848849

849850
var savingNotification = new EntityContainerSavingNotification(container, eventMessages);

src/Umbraco.Infrastructure/Services/Implement/DataTypeService.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public DataTypeService(
5959

6060
#region Containers
6161

62-
public Attempt<OperationResult<OperationResultType, EntityContainer>> CreateContainer(int parentId, string name, int userId = Cms.Core.Constants.Security.SuperUserId)
62+
public Attempt<OperationResult<OperationResultType, EntityContainer>> CreateContainer(int parentId, Guid key, string name, int userId = Cms.Core.Constants.Security.SuperUserId)
6363
{
6464
var evtMsgs = EventMessagesFactory.Get();
6565
using (var scope = ScopeProvider.CreateScope())
@@ -70,7 +70,8 @@ public Attempt<OperationResult<OperationResultType, EntityContainer>> CreateCont
7070
{
7171
Name = name,
7272
ParentId = parentId,
73-
CreatorId = userId
73+
CreatorId = userId,
74+
Key = key
7475
};
7576

7677
var savingEntityContainerNotification = new EntityContainerSavingNotification(container, evtMsgs);

src/Umbraco.Infrastructure/Services/Implement/EntityXmlSerializer.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,23 @@ public XElement Serialize(IDataType dataType)
191191
xml.Add(new XAttribute("Configuration", _configurationEditorJsonSerializer.Serialize(dataType.Configuration)));
192192

193193
var folderNames = string.Empty;
194+
var folderKeys = string.Empty;
194195
if (dataType.Level != 1)
195196
{
196197
//get URL encoded folder names
197198
var folders = _dataTypeService.GetContainers(dataType)
198-
.OrderBy(x => x.Level)
199-
.Select(x => WebUtility.UrlEncode(x.Name));
199+
.OrderBy(x => x.Level);
200200

201-
folderNames = string.Join("/", folders.ToArray());
201+
folderNames = string.Join("/", folders.Select(x => WebUtility.UrlEncode(x.Name)).ToArray());
202+
folderKeys = string.Join("/", folders.Select(x => x.Key).ToArray());
202203
}
203204

204205
if (string.IsNullOrWhiteSpace(folderNames) == false)
206+
{
205207
xml.Add(new XAttribute("Folders", folderNames));
208+
xml.Add(new XAttribute("FolderKeys", folderKeys));
209+
}
210+
206211

207212
return xml;
208213
}
@@ -490,19 +495,24 @@ public XElement Serialize(IContentType contentType)
490495
tabs);
491496

492497
var folderNames = string.Empty;
498+
var folderKeys = string.Empty;
493499
//don't add folders if this is a child doc type
494500
if (contentType.Level != 1 && masterContentType == null)
495501
{
496502
//get URL encoded folder names
497503
var folders = _contentTypeService.GetContainers(contentType)
498-
.OrderBy(x => x.Level)
499-
.Select(x => WebUtility.UrlEncode(x.Name));
504+
.OrderBy(x => x.Level);
500505

501-
folderNames = string.Join("/", folders.ToArray());
506+
folderNames = string.Join("/", folders.Select(x => WebUtility.UrlEncode(x.Name)).ToArray());
507+
folderKeys = string.Join("/", folders.Select(x => x.Key).ToArray());
502508
}
503509

504510
if (string.IsNullOrWhiteSpace(folderNames) == false)
511+
{
505512
xml.Add(new XAttribute("Folders", folderNames));
513+
xml.Add(new XAttribute("FolderKeys", folderKeys));
514+
}
515+
506516

507517
return xml;
508518
}

src/Umbraco.Web.BackOffice/Controllers/ContentTypeController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public IActionResult DeleteContainer(int id)
292292
[Authorize(Policy = AuthorizationPolicies.TreeAccessDocumentTypes)]
293293
public IActionResult PostCreateContainer(int parentId, string name)
294294
{
295-
var result = _contentTypeService.CreateContainer(parentId, name, _backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser.Id);
295+
var result = _contentTypeService.CreateContainer(parentId, Guid.NewGuid(), name, _backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser.Id);
296296

297297
if (result.Success)
298298
return Ok(result.Result); //return the id

src/Umbraco.Web.BackOffice/Controllers/DataTypeController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public IActionResult DeleteContainer(int id)
260260
public IActionResult PostCreateContainer(int parentId, string name)
261261
{
262262
var currentUser = _backOfficeSecurityAccessor.BackOfficeSecurity.CurrentUser;
263-
var result = _dataTypeService.CreateContainer(parentId, name, currentUser.Id);
263+
var result = _dataTypeService.CreateContainer(parentId, Guid.NewGuid(), name, currentUser.Id);
264264

265265
if (result.Success)
266266
return Ok(result.Result); //return the id

src/Umbraco.Web.BackOffice/Controllers/MediaTypeController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public IActionResult DeleteContainer(int id)
261261
[Authorize(Policy = AuthorizationPolicies.TreeAccessMediaTypes)]
262262
public IActionResult PostCreateContainer(int parentId, string name)
263263
{
264-
var result = _mediaTypeService.CreateContainer(parentId, name, _backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser.Id);
264+
var result = _mediaTypeService.CreateContainer(parentId, Guid.NewGuid(), name, _backofficeSecurityAccessor.BackOfficeSecurity.CurrentUser.Id);
265265

266266
if (result.Success)
267267
return Ok(result.Result); //return the id

0 commit comments

Comments
 (0)