Пример #1
0
        public static void logError(string customMessage, string errorType, string expMessage = "", string targetSite = "", string innerException = "")
        {
            try
            {
                ErrorLog newError = new ErrorLog()
                {
                    CustomMessage    = customMessage,
                    ExceptionMessage = expMessage,
                    ExceptionType    = errorType,
                    TargetSite       = targetSite,
                    LoggedAt         = DateTime.Now,
                    LoggedBy         = MyUserID,
                    InnerException   = innerException
                };

                ProjectTileSqlDatabase existingPtDb = SqlServerConnection.ExistingPtDbConnection();
                using (existingPtDb)
                {
                    existingPtDb.ErrorLog.Add(newError);
                    existingPtDb.SaveChanges();
                }
            }
            catch // (Exception e)
            {
                // MessageBox.Show(e.Message + ": " + e.InnerException.ToString());
                // Do nothing - no point throwing another error!
            }
        }
Пример #2
0
        public static bool AmendProduct(int productID, string productName, string productDescription, string version)
        {
            try
            {
                decimal versionNumber;

                if (!Decimal.TryParse(version, out versionNumber))
                {
                    MessageFunctions.InvalidMessage("Cannot amend product '" + productName + "': new version number is not a decimal.", "Invalid Version");
                    return(false);
                }

                try
                {
                    ProjectTileSqlDatabase existingPtDb = SqlServerConnection.ExistingPtDbConnection();
                    using (existingPtDb)
                    {
                        Products thisProduct = existingPtDb.Products.Find(productID);
                        if (thisProduct.LatestVersion > versionNumber)
                        {
                            bool carryOn = MessageFunctions.WarningYesNo("The new version number is lower than the existing one. Is that correct?", "Unexpected Version");
                            if (!carryOn)
                            {
                                return(false);
                            }
                        }
                        thisProduct.ProductName        = productName;
                        thisProduct.ProductDescription = productDescription;
                        thisProduct.LatestVersion      = versionNumber;

                        if (ValidateProduct(ref thisProduct, productID))
                        {
                            existingPtDb.SaveChanges();
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }
                catch (Exception generalException)
                {
                    MessageFunctions.Error("Problem saving changes to product '" + productName + "'", generalException);
                    return(false);
                }
            }
            catch (Exception generalException)
            {
                MessageFunctions.Error("Error amending product '" + productName + "'", generalException);
                return(false);
            }
        }
Пример #3
0
        public static int NewProduct(string productName, string productDescription, string version)
        {
            try
            {
                decimal versionNumber;

                if (!Decimal.TryParse(version, out versionNumber))
                {
                    MessageFunctions.InvalidMessage("Cannot create new product: version number is not a decimal.", "Invalid Version");
                    return(0);
                }

                Products newProduct = new Products()
                {
                    ProductName = productName, ProductDescription = productDescription, LatestVersion = versionNumber
                };
                if (ValidateProduct(ref newProduct, 0))
                {
                    try
                    {
                        ProjectTileSqlDatabase existingPtDb = SqlServerConnection.ExistingPtDbConnection();
                        using (existingPtDb)
                        {
                            existingPtDb.Products.Add(newProduct);
                            existingPtDb.SaveChanges();
                            return(newProduct.ID);
                        }
                    }
                    catch (Exception generalException)
                    {
                        MessageFunctions.Error("Problem saving new product", generalException);
                        return(0);
                    }
                }
                else
                {
                    return(0);
                }
            }
            catch (Exception generalException)
            {
                MessageFunctions.Error("Error creating new product", generalException);
                return(0);
            }
        }
Пример #4
0
        // Default Entity functions

        public static void SetDefaultEntity(ref Entities selectedEntity, int staffID = 0)
        {
            if (selectedEntity == null)
            {
                MessageFunctions.InvalidMessage("Please select an Entity to amend from the drop-down list.", "No Entity Selected");
                return;
            }

            ProjectTileSqlDatabase existingPtDb = SqlServerConnection.ExistingPtDbConnection();

            using (existingPtDb)
            {
                try
                {
                    if (staffID == 0)
                    {
                        staffID = MyStaffID;
                    }

                    Staff thisUser = existingPtDb.Staff.Find(staffID);
                    thisUser.DefaultEntity = selectedEntity.ID;

                    existingPtDb.SaveChanges();
                    if (staffID == MyStaffID)
                    {
                        UpdateMyDefaultEntity(ref selectedEntity);
                    }
                }
                catch (SqlException sqlException)
                {
                    MessageFunctions.Error("SQL error saving new default Entity preference to the database", sqlException);
                    return;
                }
                catch (Exception generalException)
                {
                    MessageFunctions.Error("Error saving new default Entity preference to the database", generalException);
                    return;
                }
            }
        }
Пример #5
0
        // Entity changes

        public static bool AllowEntity(int entityID, int staffID)
        {
            try
            {
                ProjectTileSqlDatabase existingPtDb = SqlServerConnection.ExistingPtDbConnection();
                using (existingPtDb)
                {
                    StaffEntities createStaffEntity = new StaffEntities();
                    createStaffEntity.EntityID = entityID;
                    createStaffEntity.StaffID  = staffID;
                    existingPtDb.StaffEntities.Add(createStaffEntity);

                    existingPtDb.SaveChanges();
                    return(true);
                }
            }
            catch (Exception generalException)
            {
                MessageFunctions.Error("Error retrieving an Entity with ID " + entityID.ToString() + " from the database", generalException);
                return(false);
            }
        }
Пример #6
0
        public static bool ChangeLoginDetails(int staffID, string userID, string newPassword, string confirmPassword)
        {
            bool passwordChange = (newPassword != "");
            bool userIDChange   = false;

            if (userID == "")
            {
                MessageFunctions.Error("UserID has not been passed to this function.", null); // UserID is required to check complexity so that userID cannot equal password
                return(false);
            }

            if (passwordChange && newPassword != confirmPassword)
            {
                MessageFunctions.InvalidMessage("New password does not match confirmation. Please check both fields and try again.", "Password Mismatch");
                return(false);
            }
            else if (passwordChange && !PasswordComplexityOK(userID, newPassword))
            {
                return(false);
            }
            else
            {
                try
                {
                    // Log in as the administration user to allow the change to be made
                    ProjectTileSqlDatabase defaultPtDb = SqlServerConnection.DefaultPtDbConnection();
                    using (defaultPtDb)
                    {
                        try
                        {
                            Staff thisUser = defaultPtDb.Staff.FirstOrDefault(s => s.ID == staffID);
                            if (thisUser == null)
                            {
                                MessageFunctions.Error("Error amending login details in the database: user with ID " + staffID.ToString() + " not found.", null);
                                return(false);
                            }

                            if (passwordChange)
                            {
                                thisUser.Passwd = newPassword;
                            }
                            if (thisUser.UserID == null || thisUser.UserID != userID)
                            {
                                Staff checkUserID = defaultPtDb.Staff.FirstOrDefault(s => s.UserID == userID && s.ID != staffID);
                                if (checkUserID != null)
                                {
                                    MessageFunctions.InvalidMessage("A different staff member with UserID '" + userID +
                                                                    "' already exists. Please try a different one.", "Duplicate UserID");
                                    return(false);
                                }

                                userIDChange    = true;
                                thisUser.UserID = userID;
                            }
                            defaultPtDb.SaveChanges();

                            // Now amend any history records, to show that the user effectively made this change
                            DateTime timeFrom      = System.DateTime.Now.AddMinutes(-5);
                            int[]    auditEntryIDs = defaultPtDb.AuditEntries
                                                     .Where(ae => ae.TableName == "Staff" &&
                                                            ae.ChangeTime >= timeFrom &&
                                                            ae.ActionType == "Updated" &&
                                                            ae.PrimaryValue == staffID.ToString() &&
                                                            ae.UserName.Substring(0, 5) != DbUserPrefix &&
                                                            ((passwordChange && ae.ChangeColumn == "PasswordHash") || (userIDChange && ae.ChangeColumn == "UserID"))
                                                            )
                                                     .OrderByDescending(ae => ae.ChangeTime)
                                                     .Select(ae => (int)ae.ID)
                                                     .ToArray();

                            foreach (int entry in auditEntryIDs)
                            {
                                AuditEntries lastAuditEntry = defaultPtDb.AuditEntries.Find(entry);
                                lastAuditEntry.UserName = DbUserPrefix + MyUserID;
                                defaultPtDb.SaveChanges();
                            }

                            if (staffID == MyStaffID)
                            {
                                string databaseLogin            = DbUserPrefix + userID;
                                ProjectTileSqlDatabase userPtDb = SqlServerConnection.UserPtDbConnection(databaseLogin, newPassword); // Log in again so that future database calls have the new password
                            }

                            return(true);
                        }
                        catch (SqlException sqlException)
                        {
                            MessageFunctions.Error("Error amending login details in the database", sqlException);
                            return(false);
                        }
                        catch (Exception generalException)
                        {
                            MessageFunctions.Error("Error amending login details", generalException);
                            return(false);
                        }
                    }
                }
                catch (SqlException sqlException)
                {
                    MessageFunctions.Error("Error accessing the database", sqlException);
                    return(false);
                }
                catch (Exception generalException)
                {
                    MessageFunctions.Error("Error checking existing login", generalException);
                    return(false);
                }
            }
        }
Пример #7
0
        public static void AmendEntity(ref Entities selectedEntity, string entityName, string entityDescription)
        {
            int intSelectedEntityID;

            if (selectedEntity == null)
            {
                MessageFunctions.InvalidMessage("Please select an Entity to amend from the drop-down list.", "No Entity Selected");
                return;
            }

            if (!PageFunctions.SqlInputOK(entityName, true, "Entity name"))
            {
                return;
            }
            else if (!PageFunctions.SqlInputOK(entityDescription, true, "Entity description"))
            {
                return;
            }

            try
            {
                ProjectTileSqlDatabase existingPtDb = SqlServerConnection.ExistingPtDbConnection();
                using (existingPtDb)
                {
                    intSelectedEntityID = selectedEntity.ID;

                    Entities checkNewName = existingPtDb.Entities.FirstOrDefault(ent => ent.EntityName == entityName && ent.ID != intSelectedEntityID);
                    if (checkNewName != null)
                    {
                        MessageFunctions.InvalidMessage("Could not amend Entity. Another Entity with name '" + entityName + "' already exists.", "Duplicate Name");
                        return;
                    }

                    Entities checkNewDescription = existingPtDb.Entities.FirstOrDefault(ent => ent.EntityDescription == entityDescription && ent.ID != intSelectedEntityID);
                    if (checkNewDescription != null)
                    {
                        MessageFunctions.InvalidMessage("Could not amend Entity. Another Entity with description '" + entityDescription + "' already exists.", "Duplicate Description");
                        return;
                    }

                    try
                    {
                        try
                        {
                            string nameChange   = "";
                            string originalName = selectedEntity.EntityName;

                            if (originalName != entityName)
                            {
                                nameChange = " to '" + entityName + "'";
                            }
                            ;

                            Entities changeDbEntity = existingPtDb.Entities.Find(intSelectedEntityID);
                            changeDbEntity.EntityName        = entityName;
                            changeDbEntity.EntityDescription = entityDescription;
                            existingPtDb.SaveChanges();

                            MessageFunctions.SuccessAlert("Entity '" + originalName + "' has been amended" + nameChange + ".", "Entity Amended");
                            if (changeDbEntity.ID == CurrentEntityID)
                            {
                                UpdateCurrentEntity(ref changeDbEntity);
                            }
                            if (changeDbEntity.ID == MyDefaultEntityID)
                            {
                                UpdateMyDefaultEntity(ref changeDbEntity);
                            }
                            PageFunctions.ShowTilesPage();
                        }
                        catch (Exception generalException)
                        {
                            MessageFunctions.Error("Error amending database record", generalException);
                            return;
                        }
                    }
                    catch (Exception generalException) { MessageFunctions.Error("Error creating new database", generalException); }
                }
            }
            catch (Exception generalException) { MessageFunctions.Error("Error checking new database details", generalException); }
        }
Пример #8
0
        public static void NewEntity(string entityName, string entityDescription, bool switchTo, bool makeDefault)
        {
            int      newEntityID;
            Entities newEntity;

            if (!PageFunctions.SqlInputOK(entityName, true, "Entity name"))
            {
                return;
            }
            else if (!PageFunctions.SqlInputOK(entityDescription, true, "Entity description"))
            {
                return;
            }

            try
            {
                ProjectTileSqlDatabase existingPtDb = SqlServerConnection.ExistingPtDbConnection();
                using (existingPtDb)
                {
                    Entities checkNewName = existingPtDb.Entities.FirstOrDefault(ent => ent.EntityName == entityName);
                    if (checkNewName != null)
                    {
                        MessageFunctions.InvalidMessage("Could not create new Entity. An Entity with name '" + entityName + "' already exists.", "Duplicate Name");
                        return;
                    }

                    Entities checkNewDescription = existingPtDb.Entities.FirstOrDefault(ent => ent.EntityDescription == entityDescription);
                    if (checkNewDescription != null)
                    {
                        MessageFunctions.InvalidMessage("Could not create new Entity. An Entity with description '" + entityDescription + "' already exists.", "Duplicate Description");
                        return;
                    }

                    try
                    {
                        try
                        {
                            newEntity                   = new Entities();
                            newEntity.EntityName        = entityName;
                            newEntity.EntityDescription = entityDescription;

                            try
                            {
                                existingPtDb.Entities.Add(newEntity);
                                existingPtDb.SaveChanges();
                                newEntityID = newEntity.ID;
                            }
                            catch (Exception generalException)
                            {
                                MessageFunctions.Error("Problem creating entity ID", generalException);
                                return;
                            }
                        }
                        catch (Exception generalException)
                        {
                            MessageFunctions.Error("Error creating database record", generalException);
                            return;
                        }

                        try
                        {
                            Staff currentUser = MyStaffRecord;
                            AllowEntity(newEntityID, currentUser.ID);
                        }
                        catch (Exception generalException)
                        {
                            MessageFunctions.Error("Error providing access to the new database", generalException);
                            return;
                        }

                        try
                        {
                            existingPtDb.SaveChanges();
                            string switched = ". Use the 'Change Current Entity' function to log into it if you wish to work in this Entity.";

                            if (switchTo)
                            {
                                UpdateCurrentEntity(ref newEntity);
                                switched = " and you are now logged into it.";
                            }

                            if (makeDefault)
                            {
                                SetDefaultEntity(ref newEntity);
                            }

                            MessageFunctions.SuccessAlert("Entity '" + entityName + "' has been created" + switched, "New Entity Created");
                            PageFunctions.ShowTilesPage();
                        }
                        catch (SqlException sqlException)
                        {
                            MessageFunctions.Error("SQL error saving changes to the database", sqlException);
                            return;
                        }
                        catch (Exception generalException)
                        {
                            MessageFunctions.Error("Error saving changes to the database", generalException);
                            return;
                        }
                    }
                    catch (Exception generalException) { MessageFunctions.Error("Error creating new database", generalException); }
                }
            }
            catch (Exception generalException) { MessageFunctions.Error("Error checking new database details", generalException); }
        }