Skip to content
This repository was archived by the owner on Feb 10, 2024. It is now read-only.

Commit a700957

Browse files
authored
Merge pull request #74 from umco/dev/utils
Helper methods for renaming aliases
2 parents 649680e + 9e830d5 commit a700957

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using Newtonsoft.Json.Linq;
3+
4+
namespace Our.Umbraco.DocTypeGridEditor.Extensions
5+
{
6+
internal static class JsonExtensions
7+
{
8+
public static void Rename(this JToken token, string newName)
9+
{
10+
var parent = token.Parent;
11+
12+
if (parent == null)
13+
throw new InvalidOperationException("The parent is missing.");
14+
15+
var newToken = new JProperty(newName, token);
16+
parent.Replace(newToken);
17+
}
18+
}
19+
}

src/Our.Umbraco.DocTypeGridEditor/Helpers/DocTypeGridEditorHelper.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Umbraco.Core.Models;
1111
using Umbraco.Core.Models.Editors;
1212
using Umbraco.Core.Models.PublishedContent;
13+
using Umbraco.Core.Persistence;
1314
using Umbraco.Core.PropertyEditors;
1415
using Umbraco.Core.Services;
1516
using Umbraco.Web;
@@ -149,6 +150,77 @@ private static string GetContentTypeAliasByGuid(Guid contentTypeGuid)
149150
string.Concat("Our.Umbraco.DocTypeGridEditor.Helpers.DocTypeGridEditorHelper.GetContentTypeAliasByGuid_", contentTypeGuid),
150151
() => Services.ContentTypeService.GetAliasByGuid(contentTypeGuid));
151152
}
153+
154+
public static void RemapDocTypeAlias(string oldAlias, string newAlias, Transaction transaction = null)
155+
{
156+
var db = ApplicationContext.Current.DatabaseContext.Database;
157+
158+
// Update references in property data
159+
// We do 2 very similar replace statements, but one is without spaces in the JSON, the other is with spaces
160+
// as we can't guarantee what format it will actually get saved in
161+
var sql1 = string.Format(@"UPDATE cmsPropertyData
162+
SET dataNtext = CAST(REPLACE(REPLACE(CAST(dataNtext AS nvarchar(max)), '""dtgeContentTypeAlias"":""{0}""', '""dtgeContentTypeAlias"":""{1}""'), '""dtgeContentTypeAlias"": ""{0}""', '""dtgeContentTypeAlias"": ""{1}""') AS ntext)
163+
WHERE dataNtext LIKE '%""dtgeContentTypeAlias"":""{0}""%' OR dataNtext LIKE '%""dtgeContentTypeAlias"": ""{0}""%'", oldAlias, newAlias);
164+
165+
if (transaction == null)
166+
{
167+
using (var tr = db.GetTransaction())
168+
{
169+
db.Execute(sql1);
170+
tr.Complete();
171+
}
172+
}
173+
else
174+
{
175+
db.Execute(sql1);
176+
}
177+
}
178+
179+
public static void RemapPropertyAlias(string docTypeAlias, string oldAlias, string newAlias, Transaction transaction = null)
180+
{
181+
var db = ApplicationContext.Current.DatabaseContext.Database;
182+
183+
// Update references in property data
184+
// We have to do it in code because there could be nested JSON so
185+
// we need to make sure it only replaces at the specific level only
186+
Action doQuery = () =>
187+
{
188+
var rows = GetPropertyDataRows(docTypeAlias);
189+
foreach (var row in rows)
190+
{
191+
var tokens = row.Data.SelectTokens(string.Format("$..controls[?(@.value.dtgeContentTypeAlias == '{0}' && @.value.value.{1})].value", docTypeAlias, oldAlias)).ToList();
192+
if (tokens.Any())
193+
{
194+
foreach (var token in tokens)
195+
{
196+
token["value"][oldAlias].Rename(newAlias);
197+
}
198+
db.Execute("UPDATE [cmsPropertyData] SET [dataNtext] = @0 WHERE [id] = @1", row.RawData, row.Id);
199+
}
200+
}
201+
};
202+
203+
if (transaction == null)
204+
{
205+
using (var tr = db.GetTransaction())
206+
{
207+
doQuery();
208+
tr.Complete();
209+
}
210+
}
211+
else
212+
{
213+
doQuery();
214+
}
215+
}
216+
217+
private static IEnumerable<JsonDbRow> GetPropertyDataRows(string docTypeAlias)
218+
{
219+
var db = ApplicationContext.Current.DatabaseContext.Database;
220+
return db.Query<JsonDbRow>(string.Format(
221+
@"SELECT [id], [dataNtext] as [rawdata] FROM cmsPropertyData WHERE dataNtext LIKE '%""dtgeContentTypeAlias"":""{0}""%' OR dataNtext LIKE '%""dtgeContentTypeAlias"": ""{0}""%'",
222+
docTypeAlias)).ToList();
223+
}
152224
}
153225

154226
public class ContentTypeContainer
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
4+
namespace Our.Umbraco.DocTypeGridEditor.Models
5+
{
6+
/// <summary>
7+
/// A utility class used to help modify JSON data from the Umbraco property data table
8+
/// </summary>
9+
internal class JsonDbRow
10+
{
11+
public int Id { get; set; }
12+
13+
public string RawData { get; set; }
14+
15+
public JToken Data
16+
{
17+
get
18+
{
19+
return (JToken)JsonConvert.DeserializeObject(RawData);
20+
}
21+
set
22+
{
23+
RawData = JsonConvert.SerializeObject(value);
24+
}
25+
}
26+
}
27+
}

src/Our.Umbraco.DocTypeGridEditor/Our.Umbraco.DocTypeGridEditor.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,13 @@
275275
</ItemGroup>
276276
<ItemGroup>
277277
<Compile Include="Bootstrap.cs" />
278+
<Compile Include="Extensions\JsonExtensions.cs" />
278279
<Compile Include="Extensions\ViewEnginesCollectionExtensions.cs" />
279280
<Compile Include="Helpers\DocTypeGridEditorHelper.cs" />
280281
<Compile Include="Helpers\XmlHelper.cs" />
281282
<Compile Include="Models\DetachedPublishedContent.cs" />
282283
<Compile Include="Models\DetachedPublishedProperty.cs" />
284+
<Compile Include="Models\JsonDbRow.cs" />
283285
<Compile Include="PackageActions\AddObjectToJsonArray.cs" />
284286
<Compile Include="Properties\AssemblyInfo.cs" />
285287
<Compile Include="Properties\VersionInfo.cs" />

0 commit comments

Comments
 (0)