This repository has been archived by the owner on Mar 7, 2019. It is now read-only.
/
NodeHelper.cs
143 lines (123 loc) · 4.72 KB
/
NodeHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Web;
using umbraco.cms.businesslogic.web;
using Umbraco.Core.Logging;
using Umbraco.Core;
using G42.UmbracoGrease.G42NodeHelper.Models;
namespace G42.UmbracoGrease.G42NodeHelper
{
/// <summary>
/// Singleton class that represents a basic website structure and caches the result.
/// </summary>
public sealed class NodeHelper
{
private static volatile NodeHelper _instance;
private static object _padLock = new Object();
private static Type _siteType;
/// <summary>
/// Prevents a default instance of the <see cref="NodeHelper"/> class from being created.
/// </summary>
/// <exception cref="Exception">You may only have one Site type.</exception>
private NodeHelper()
{
var baseType = typeof (Site);
var types = PluginManager.Current.ResolveTypes<Site>().Where(x => x.IsSubclassOf(baseType));
//if more than one, reject
if (types.Count() > 1)
{
throw new Exception("You may only have one Site type.");
}
//find an implementation of Site
_siteType = types.FirstOrDefault(x => x.IsSubclassOf(baseType));
if (_siteType == null)
{
LogHelper.Info<NodeHelper>("Using default Site Type.");
_siteType = baseType;
}
else
{
LogHelper.Info<NodeHelper>("Using " + _siteType.ToString());
}
}
public List<Site> Sites = new List<Site>();
public DateTime CreatedOn { get; private set; }
/// <summary>
/// Gets the current site.
/// </summary>
/// <value>
/// The current site.
/// </value>
public Site CurrentSite
{
get { return Sites.FirstOrDefault(x => x.Domain == HttpContext.Current.Request.Url.Host); }
}
/// <summary>
/// Determines whether this instance is initialized.
/// </summary>
/// <returns></returns>
public static bool IsInitialized()
{
return _instance != null;
}
/// <summary>
/// Gets the instance.
/// </summary>
/// <value>
/// The instance.
/// </value>
public static NodeHelper Instance
{
get
{
if (_instance == null)
{
lock (_padLock)
{
if (_instance == null)
{
_instance = new NodeHelper();
_instance.CreatedOn = DateTime.UtcNow;
var umbHelper = new UmbracoHelper(UmbracoContext.Current);
foreach (var domain in Domain.GetDomains())
{
var rootNode = umbHelper.TypedContent(domain.RootNodeId);
if (rootNode != null)
{
LogHelper.Info<NodeHelper>("Caching: " + domain.Name);
var siteRootNode = umbHelper.TypedContent(domain.RootNodeId);
if (siteRootNode != null)
{
var siteSettings = siteRootNode.Siblings().FirstOrDefault(x => x.DocumentTypeAlias.EndsWith("SiteSettings"));
var site = ((Site)Activator.CreateInstance(_siteType));
site.Domain = domain.Name;
site.RootId = rootNode.Id;
site.HomeId = siteRootNode.Id;
site.SiteSettingsId = (siteSettings != null) ? siteSettings.Id : siteRootNode.Id;
site.MapSite(site, domain, rootNode, siteRootNode, siteSettings);
_instance.Sites.Add(site);
}
}
}
}
}
}
return _instance;
}
private set
{
_instance = value;
}
}
/// <summary>
/// Clears this instance.
/// </summary>
public static void Clear()
{
LogHelper.Info<NodeHelper>("Clearing...");
_instance = null;
}
}
}