using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using umbraco.DataLayer;
namespace umbraco.BusinessLogic
{
///
/// umbraco.BusinessLogic.ApplicationTree provides access to the application tree structure in umbraco.
/// An application tree is a collection of nodes belonging to one or more application(s).
/// Through this class new application trees can be created, modified and deleted.
///
public class ApplicationTree
{
private const string CACHE_KEY = "ApplicationTreeCache";
///
/// The cache storage for all application trees
///
private static List AppTrees
{
get
{
//ensure cache exists
if (HttpRuntime.Cache[CACHE_KEY] == null)
ReCache();
return HttpRuntime.Cache[CACHE_KEY] as List;
}
set
{
HttpRuntime.Cache.Insert(CACHE_KEY, value);
}
}
private static string _ConnString = GlobalSettings.DbDSN;
private static ISqlHelper _sqlHelper = DataLayerHelper.CreateSqlHelper(_ConnString);
///
/// Gets the SQL helper.
///
/// The SQL helper.
public static ISqlHelper SqlHelper
{
get { return _sqlHelper; }
}
private bool _silent;
///
/// Gets or sets a value indicating whether this is silent.
///
/// true if silent; otherwise, false.
public bool Silent
{
get { return _silent; }
set { _silent = value; }
}
private bool _initialize;
///
/// Gets or sets a value indicating whether this should initialize.
///
/// true if initialize; otherwise, false.
public bool Initialize
{
get { return _initialize; }
set { _initialize = value; }
}
private byte _sortOrder;
///
/// Gets or sets the sort order.
///
/// The sort order.
public byte SortOrder
{
get { return _sortOrder; }
set { _sortOrder = value; }
}
private string _applicationAlias;
///
/// Gets the application alias.
///
/// The application alias.
public string ApplicationAlias
{
get { return _applicationAlias; }
}
private string _alias;
///
/// Gets the tree alias.
///
/// The alias.
public string Alias
{
get { return _alias; }
}
private string _title;
///
/// Gets or sets the tree title.
///
/// The title.
public string Title
{
get { return _title; }
set { _title = value; }
}
private string _iconClosed;
///
/// Gets or sets the icon closed.
///
/// The icon closed.
public string IconClosed
{
get { return _iconClosed; }
set { _iconClosed = value; }
}
private string _iconOpened;
///
/// Gets or sets the icon opened.
///
/// The icon opened.
public string IconOpened
{
get { return _iconOpened; }
set { _iconOpened = value; }
}
private string _assemblyName;
///
/// Gets or sets the name of the assembly.
///
/// The name of the assembly.
public string AssemblyName
{
get { return _assemblyName; }
set { _assemblyName = value; }
}
private string _type;
///
/// Gets or sets the tree type.
///
/// The type.
public string Type
{
get { return _type; }
set { _type = value; }
}
private string _action;
///
/// Gets or sets the default tree action.
///
/// The action.
public string Action
{
get { return _action; }
set { _action = value; }
}
///
/// A static constructor that will cache all application trees
///
static ApplicationTree()
{
Cache();
}
///
/// Initializes a new instance of the class.
///
public ApplicationTree() { }
///
/// Initializes a new instance of the class.
///
/// if set to true [silent].
/// if set to true [initialize].
/// The sort order.
/// The application alias.
/// The tree alias.
/// The tree title.
/// The icon closed.
/// The icon opened.
/// Name of the assembly.
/// The tree type.
/// The default tree action.
public ApplicationTree(bool silent, bool initialize, byte sortOrder, string applicationAlias, string alias, string title, string iconClosed, string iconOpened, string assemblyName, string type, string action)
{
this._silent = silent;
this._initialize = initialize;
this._sortOrder = sortOrder;
this._applicationAlias = applicationAlias;
this._alias = alias;
this._title = title;
this._iconClosed = iconClosed;
this._iconOpened = iconOpened;
this._assemblyName = assemblyName;
this._type = type;
this._action = action;
}
///
/// Creates a new application tree.
///
/// if set to true [silent].
/// if set to true [initialize].
/// The sort order.
/// The application alias.
/// The alias.
/// The title.
/// The icon closed.
/// The icon opened.
/// Name of the assembly.
/// The type.
/// The action.
public static void MakeNew(bool silent, bool initialize, byte sortOrder, string applicationAlias, string alias, string title, string iconClosed, string iconOpened, string assemblyName, string type, string action)
{
SqlHelper.ExecuteNonQuery(@"insert into umbracoAppTree(treeSilent, treeInitialize, treeSortOrder, appAlias, treeAlias, treeTitle,
treeIconClosed, treeIconOpen, treeHandlerAssembly, treeHandlerType, action)
values(@treeSilent, @treeInitialize, @treeSortOrder, @appAlias, @treeAlias, @treeTitle, @treeIconClosed, @treeIconOpen, @treeHandlerAssembly, @treeHandlerType, @action)"
,
SqlHelper.CreateParameter("@treeSilent", silent),
SqlHelper.CreateParameter("@treeInitialize", initialize),
SqlHelper.CreateParameter("@treeSortOrder", sortOrder),
SqlHelper.CreateParameter("@treeAlias", alias),
SqlHelper.CreateParameter("@appAlias", applicationAlias),
SqlHelper.CreateParameter("@treeTitle", title),
SqlHelper.CreateParameter("@treeIconClosed", iconClosed),
SqlHelper.CreateParameter("@treeIconOpen", iconOpened),
SqlHelper.CreateParameter("@treeHandlerAssembly", assemblyName),
SqlHelper.CreateParameter("@treeHandlerType", type),
SqlHelper.CreateParameter("@action", action)
);
ReCache();
}
///
/// Saves this instance.
///
public void Save()
{
SqlHelper.ExecuteNonQuery(@"Update umbracoAppTree set treeSilent = @treeSilent, treeInitialize = @treeInitialize, treeSortOrder = @treeSortOrder, treeTitle = @treeTitle,
treeIconClosed = @treeIconClosed, treeIconOpen = @treeIconOpen, treeHandlerAssembly = @treeHandlerAssembly, treeHandlerType = @treeHandlerType, action = @action
where treeAlias = @treeAlias AND appAlias = @appAlias",
SqlHelper.CreateParameter("@treeSilent", this.Silent),
SqlHelper.CreateParameter("@treeInitialize", this.Initialize),
SqlHelper.CreateParameter("@treeSortOrder", this.SortOrder),
SqlHelper.CreateParameter("@treeTitle", this.Title),
SqlHelper.CreateParameter("@treeIconClosed", this.IconClosed),
SqlHelper.CreateParameter("@treeIconOpen", this.IconOpened),
SqlHelper.CreateParameter("@treeHandlerAssembly", this.AssemblyName),
SqlHelper.CreateParameter("@treeHandlerType", this.Type),
SqlHelper.CreateParameter("@treeAlias", this.Alias),
SqlHelper.CreateParameter("@appAlias", this.ApplicationAlias),
SqlHelper.CreateParameter("@action", this.Action)
);
ReCache();
}
///
/// Deletes this instance.
///
public void Delete()
{
SqlHelper.ExecuteNonQuery("delete from umbracoAppTree where appAlias = @appAlias AND treeAlias = @treeAlias",
SqlHelper.CreateParameter("@appAlias", this.ApplicationAlias), SqlHelper.CreateParameter("@treeAlias", this.Alias));
ReCache();
}
///
/// Gets an ApplicationTree by it's tree alias.
///
/// The tree alias.
/// An ApplicationTree instance
public static ApplicationTree getByAlias(string treeAlias)
{
return AppTrees.Find(
delegate(ApplicationTree t)
{
return (t.Alias == treeAlias);
}
);
}
///
/// Gets all applicationTrees registered in umbraco from the umbracoAppTree table..
///
/// Returns a ApplicationTree Array
public static ApplicationTree[] getAll()
{
return AppTrees.ToArray();
}
///
/// Gets the application tree for the applcation with the specified alias
///
/// The application alias.
/// Returns a ApplicationTree Array
public static ApplicationTree[] getApplicationTree(string applicationAlias)
{
return getApplicationTree(applicationAlias, false);
}
///
/// Gets the application tree for the applcation with the specified alias
///
/// The application alias.
///
/// Returns a ApplicationTree Array
public static ApplicationTree[] getApplicationTree(string applicationAlias, bool onlyInitializedApplications)
{
List list = AppTrees.FindAll(
delegate(ApplicationTree t)
{
if (onlyInitializedApplications)
return (t.ApplicationAlias == applicationAlias && t.Initialize);
else
return (t.ApplicationAlias == applicationAlias);
}
);
return list.ToArray();
}
///
/// Removes the ApplicationTree cache and re-reads the data from the db.
///
private static void ReCache()
{
HttpRuntime.Cache.Remove(CACHE_KEY);
Cache();
}
///
/// Read all ApplicationTree data and store it in cache.
///
private static void Cache()
{
//don't query the database is the cache is not null
if (HttpRuntime.Cache[CACHE_KEY] != null)
return;
List list = new List();
using (IRecordsReader dr = SqlHelper.ExecuteReader(@"Select treeSilent, treeInitialize, treeSortOrder, appAlias, treeAlias, treeTitle, treeIconClosed,
treeIconOpen, treeHandlerAssembly, treeHandlerType, action from umbracoAppTree order by treeSortOrder"))
{
while (dr.Read())
{
list.Add(new ApplicationTree(
dr.GetBoolean("treeSilent"),
dr.GetBoolean("treeInitialize"),
dr.GetByte("treeSortOrder"),
dr.GetString("appAlias"),
dr.GetString("treeAlias"),
dr.GetString("treeTitle"),
dr.GetString("treeIconClosed"),
dr.GetString("treeIconOpen"),
dr.GetString("treeHandlerAssembly"),
dr.GetString("treeHandlerType"),
dr.GetString("action")));
}
}
AppTrees = list;
}
}
}