Skip to content

Instantly share code, notes, and snippets.

@KevinJump
Created March 31, 2020 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KevinJump/db94e963842c36aa85edca42844e793a to your computer and use it in GitHub Desktop.
Save KevinJump/db94e963842c36aa85edca42844e793a to your computer and use it in GitHub Desktop.
Quick and dirty Health check to fix non-json vorto properties, if you change from a text property to a vorto one, then the stored values will need updating, this health check can do that
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Web.HealthCheck;
namespace VortoHealthCheck
{
/// <summary>
/// Finds all vorto properties in content, and checks / fixes them if they are not storing values in JSON format.
/// </summary>
/// <remarks>
/// TEST THIS ON A DB BACKUP BEFORE ALTERING LIVE DATA!
/// </remarks>
[HealthCheck("1DD9A117-85CB-4DDA-8385-9AF41BC3EA81",
"Vorto Health Check",
Description = "Checks the status of vorto fields",
Group = "Vorto")]
public class VortoHealthCheck : HealthCheck
{
private readonly IContentService contentService;
private readonly IDataTypeService dataTypeService;
private readonly string defaultLanguage;
public VortoHealthCheck(HealthCheckContext healthCheckContext)
: base(healthCheckContext)
{
contentService = healthCheckContext.ApplicationContext.Services.ContentService;
dataTypeService = healthCheckContext.ApplicationContext.Services.DataTypeService;
// SET TO THE LANGUAGE YOU WANT TO USE AS A DEFAULT
defaultLanguage = "en-US";
}
/// <summary>
/// check.
/// </summary>
public override IEnumerable<HealthCheckStatus> GetStatus()
{
var rootContent = contentService.GetRootContent();
int count = 0;
foreach(var item in rootContent)
{
count += CheckContent(item);
}
return new HealthCheckStatus("Vorto Properties")
{
ResultType = StatusResultType.Info,
Description = $"Found {count} vorto properties that are not stored as JSON",
Actions = new HealthCheckAction("Fix", this.Id)
{
Name = "Fix"
}.AsEnumerableOfOne<HealthCheckAction>()
}.AsEnumerableOfOne<HealthCheckStatus>();
}
public int CheckContent(IContent content)
{
int count = 0;
var properties = content
.Properties
.Where(x => x.PropertyType.PropertyEditorAlias == "Our.Umbraco.Vorto")
.ToList();
foreach(var property in properties)
{
var stringValue = property.Value.TryConvertTo<string>();
if (stringValue.Success)
{
if (!string.IsNullOrWhiteSpace(stringValue.Result) && !stringValue.Result.DetectIsJson())
{
// not json.
count++;
}
}
}
foreach(var child in content.Children())
{
count += CheckContent(child);
}
return count;
}
// fix
public override HealthCheckStatus ExecuteAction(HealthCheckAction action)
{
var rootContent = contentService.GetRootContent();
int count = 0;
foreach (var item in rootContent)
{
count += FixContent(item);
}
return new HealthCheckStatus("Done")
{
Description = $"Fixed {count} properties"
};
}
public int FixContent(IContent content)
{
int count = 0;
var properties = content
.Properties
.Where(x => x.PropertyType.PropertyEditorAlias == "Our.Umbraco.Vorto")
.ToList();
foreach (var property in properties)
{
var stringValue = property.Value.TryConvertTo<string>();
if (stringValue.Success)
{
if (!string.IsNullOrWhiteSpace(stringValue.Result) && !stringValue.Result.DetectIsJson())
{
// we need to go to the datatype - get its config and work out what type of
// property is stored within the vorto element.
var dataType = dataTypeService.GetDataTypeDefinitionById(
property.PropertyType.DataTypeDefinitionId);
if (dataType != null)
{
var preValues = dataTypeService.GetPreValuesCollectionByDataTypeId(dataType.Id)
.FormatAsDictionary();
if (preValues.ContainsKey("dataType"))
{
var vortoConfig = JsonConvert.DeserializeObject<JObject>(preValues["dataType"].Value);
var dtdGuid = vortoConfig.Value<string>("guid");
count++;
// vorto property json looks like :
// {"values":{"en-US":""},"dtdGuid":"7583da93-4eca-458d-85b7-a68931ef34c9"}
property.Value = $"{{\"values\": {{\"{defaultLanguage}\": \"{stringValue.Result}\"}},\"dtdGuid\": \"{dtdGuid}\"}}";
}
}
}
}
}
if (content.IsDirty())
{
contentService.Save(content);
}
foreach (var child in content.Children())
{
count += FixContent(child);
}
return count;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment