Inheritance: SoftwareImage
Esempio n. 1
0
        /// <summary>
        /// Register database commit hooks
        /// </summary>
        public static void RegisterHooks() {

            Hook<Suite>.CommitInsert += (sender, suite) => {

                if (suite.Organization != null) {
                    // Grant all organization users access to the app by creating permissions for each user
                    QueryResultRows<OrganizationPermission> orgPermissions = Db.SQL<OrganizationPermission>("SELECT o FROM Warehouse.OrganizationPermission o WHERE o.Organization=?", suite.Organization);
                    foreach (OrganizationPermission permission in orgPermissions) {

                        SoftwarePermission softwarePermission = Db.SQL<SoftwarePermission>("SELECT o FROM Warehouse.SoftwarePermission o WHERE o.User=?", permission.User).First;
                        if (softwarePermission != null) {
                            continue; // Duplicated
                        }
                        new SoftwarePermission() { User = permission.User, Software = suite };
                    }

                    Settings settings = Settings.GetSettings();
                    if (settings == null) {
                        throw new InvalidOperationException("Warehouse has not be configured, Visit the settings page.");
                    }


                    // Set default image is no images is set.
                    SoftwareIcon img = Db.SQL<SoftwareIcon>("SELECT o FROM Warehouse.SoftwareIcon o WHERE o.Software=?", suite).First;
                    if (img == null) {
                        // Add default image
                        img = new SoftwareIcon();
                        img.KeepFile = true;
                        img.Software = suite;
                        string destination = Path.Combine(settings.PublicFolder, "warehouse\\images");
                        img.File = Path.Combine(destination, "folder.svg");
                        img.Position = 0;
                    }
                    return;
                }

                throw new InvalidOperationException("Applications is not connected to an organization");
            };

            Hook<Suite>.CommitUpdate += Software.UpdateHook;
            Hook<Suite>.BeforeDelete += Software.DeleteHook;

            Hook<Suite>.BeforeDelete += (sender, suite) => {
                QueryResultRows<SuiteSoftware> suiteSoftwares = Db.SQL<SuiteSoftware>("SELECT o FROM Warehouse.SuiteSoftware o WHERE o.Suite=?", suite);
                foreach (SuiteSoftware relation in suiteSoftwares) {
                    relation.Delete();
                }
            };
        }