using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Web; using umbraco.DataLayer; using umbraco.interfaces; using umbraco.BusinessLogic.Utils; using System.Runtime.CompilerServices; namespace umbraco.BusinessLogic { /// /// Class for handling all registered applications in Umbraco. /// public class Application { /// /// Applications found through reflection /// private static readonly List _applications = new List(); private static ISqlHelper _sqlHelper; private const string CACHE_KEY = "ApplicationCache"; /// /// The cache storage for all applications /// private static List Apps { 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 string _name; private string _alias; private string _icon; /// /// Gets the SQL helper. /// /// The SQL helper. public static ISqlHelper SqlHelper { get { if (_sqlHelper == null) { try { _sqlHelper = DataLayerHelper.CreateSqlHelper(GlobalSettings.DbDSN); } catch { } } return _sqlHelper; } } /// /// A static constructor that will cache all application trees /// static Application() { RegisterIApplications(); Cache(); } /// /// Initializes a new instance of the class. /// public Application() { } /// /// Initializes a new instance of the class. /// /// The application name. /// The application alias. /// The application icon. public Application(string name, string alias, string icon) { this.name = name; this.alias = alias; this.icon = icon; } /// /// Gets or sets the application name. /// /// The name. public string name { get { return _name; } set { _name = value; } } /// /// Gets or sets the application alias. /// /// The alias. public string alias { get { return _alias; } set { _alias = value; } } /// /// Gets or sets the application icon. /// /// The application icon. public string icon { get { return _icon; } set { _icon = value; } } /// /// Creates a new applcation if no application with the specified alias is found. /// /// The application name. /// The application alias. /// The application icon, which has to be located in umbraco/images/tray folder. [MethodImpl(MethodImplOptions.Synchronized)] public static void MakeNew(string name, string alias, string icon) { bool exist = false; foreach (Application app in getAll()) { if (app.alias == alias) exist = true; } if (!exist) { int sortOrder = (1 + SqlHelper.ExecuteScalar("SELECT MAX(sortOrder) FROM umbracoApp")); SqlHelper.ExecuteNonQuery(@" insert into umbracoApp (appAlias,appIcon,appName, sortOrder) values (@alias,@icon,@name,@sortOrder)", SqlHelper.CreateParameter("@alias", alias), SqlHelper.CreateParameter("@icon", icon), SqlHelper.CreateParameter("@name", name), SqlHelper.CreateParameter("@sortOrder", sortOrder)); ReCache(); } } //public static void MakeNew(IApplication Iapp, bool installAppTrees) { // MakeNew(Iapp.Name, Iapp.Alias, Iapp.Icon); // if (installAppTrees) { // } //} /// /// Gets the application by its alias. /// /// The application alias. /// public static Application getByAlias(string appAlias) { return Apps.Find( delegate(Application t) { return (t.alias == appAlias); } ); } /// /// Deletes this instance. /// public void Delete() { //delete the assigned applications SqlHelper.ExecuteNonQuery("delete from umbracoUser2App where app = @appAlias", SqlHelper.CreateParameter("@appAlias", this.alias)); //delete the assigned trees var trees = ApplicationTree.getApplicationTree(this.alias); foreach (var t in trees) { t.Delete(); } SqlHelper.ExecuteNonQuery("delete from umbracoApp where appAlias = @appAlias", SqlHelper.CreateParameter("@appAlias", this._alias)); ReCache(); } /// /// Gets all applications registered in umbraco from the umbracoApp table.. /// /// Returns a Application Array public static List getAll() { return Apps; } /// /// Stores all references to classes that are of type IApplication /// public static void RegisterIApplications() { if (GlobalSettings.Configured) { List types = TypeFinder.FindClassesOfType(); foreach (Type t in types) { try { IApplication typeInstance = Activator.CreateInstance(t) as IApplication; if (typeInstance != null) { _applications.Add(typeInstance); if (HttpContext.Current != null) HttpContext.Current.Trace.Write("registerIapplications", " + Adding application '" + typeInstance.Alias); } } catch (Exception ee) { Log.Add(LogTypes.Error, -1, "Error loading IApplication: " + ee.ToString()); } } } } /// /// Removes the Application cache and re-reads the data from the db. /// private static void ReCache() { HttpRuntime.Cache.Remove(CACHE_KEY); Cache(); } /// /// Read all Application 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; try { List tmp = new List(); using (IRecordsReader dr = SqlHelper.ExecuteReader("Select appAlias, appIcon, appName from umbracoApp")) { while (dr.Read()) { tmp.Add(new Application(dr.GetString("appName"), dr.GetString("appAlias"), dr.GetString("appIcon"))); } } Apps = tmp; } catch { //this is a bit of a hack that just ensures the application doesn't crash when the //installer is run and there is no database or connection string defined. //the reason this method may get called during the installation is that the //SqlHelper of this class is shared amongst everything "Application" wide. } } } }