Exemplo n.º 1
0
 /// <summary>
 /// Checks if <paramref name="IDToCheck"/> is already in use. Returns false if it does, else true.
 /// </summary>
 /// <param name="IDToCheck">The ID to check against other wares' ID.</param>
 /// <param name="sql">True if a sql database is in use, false otherwise.</param>
 /// <returns>Returns false if <paramref name="IDToCheck"/> is not unique else true.</returns>
 public static bool UniqueID(string IDToCheck, bool sql = false)
 {
     if (!sql)
     {
         List <string[]> information = WareInformation.GetWareInformation();
         foreach (string[] specificWare in information)
         {
             if (specificWare[1] == IDToCheck)
             {
                 return(false);
             }
         }
     }
     else
     {
         List <List <string> > information = SQLCode.SQLControl.GetValuesAllWare(new string[] { "id" });
         foreach (List <string> list in information)
         {
             foreach (string str in list)
             {
                 if (str == IDToCheck)
                 {
                     return(false);
                 }
             }
         }
     }
     return(true);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Runs the code to view the basic information of all wares.
        /// </summary>
        private void WareViewBasicMenu()
        {
            if (!SQLCode.SQLControl.DatabaseInUse)
            {
                VisualCalculator.WareDisplay(WareInformation.GetWareInformation());
            }
            else
            {
                try
                {
                    List <string[]> informationReady = SQLDataCollection();
                    VisualCalculator.WareDisplay(informationReady);
                }
                catch (Exception e)
                {
                    Support.ErrorHandling(e, $"Could not collect data: {e.Message}");
                }
            }
            Support.WaitOnKeyInput();

            List <string[]> SQLDataCollection()
            {
                List <List <string> > information      = SQLCode.SQLControl.GetValuesAllWare(new string[] { "name", "id", "amount", "type" });
                List <string[]>       informationReady = new List <string[]>();

                foreach (List <string> arrayData in information)
                {
                    informationReady.Add(arrayData.ToArray());
                }
                return(informationReady);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Generates an array of selectable attributes belonging to the type of the ware with the <paramref name="ID"/>.
        /// Finally, it also add an "Exit" to the end of the array.
        /// </summary>
        /// <param name="ID">The ID of the ware. </param>
        /// <param name="databseInUse">True if sql is used, false otherwise.</param>
        /// <returns></returns>
        private static string[] GenerateOptions(string ID, bool databseInUse)
        {
            Type type;
            byte n = databseInUse ? (byte)1 : (byte)0;

            if (databseInUse)
            {
                try
                {
                    type = Type.GetType("StorageSystemCore." + Support.RemoveSpace(SQLCode.StoredProcedures.GetTypeSP($"'{ID}'")[0]));
                }
                catch (NullReferenceException e)
                {
                    throw e;
                }
            }
            else
            {
                type = Publisher.PubWare.GetTypeFromWare(ID);
            }
            List <string[]> attributes = WareInformation.FindSearchableAttributes(type);

            string[] options = new string[attributes.Count + 1];
            for (int i = 0; i < options.Length - 1; i++)
            {
                options[i] = attributes[i][n];
            }
            options[options.Length - 1] = "Done";
            return(options);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Runs the code that allows the user to view specific information of all wares.
        /// </summary>
        private void WareViewMenu()
        {
            Console.Clear();
            List <string> searchAttributes = WareInformation.FindAllSearchableAttributesNames(SQLCode.SQLControl.DatabaseInUse);

            if (!SQLCode.SQLControl.DatabaseInUse) //get attributes
            {
                searchAttributes.Add("Type");
            }
            else
            {
                searchAttributes.Add("type");
            }
            searchAttributes.Add("Done");
            List <string> selectedAttributes = new List <string>();
            byte          selected;

            do //select attributes to view
            {
                selected = VisualCalculator.MenuRun(searchAttributes.ToArray(), "Select Information To Find");
                if (selected != searchAttributes.Count - 1 && !selectedAttributes.Contains(searchAttributes[selected]))
                {
                    selectedAttributes.Add(searchAttributes[selected]);
                }
            } while (selected != searchAttributes.Count - 1);
            List <Dictionary <string, object> > attributesAndValues;

            if (selectedAttributes.Count != 0)
            { //get the data and display it
                if (!SQLCode.SQLControl.DatabaseInUse)
                {
                    try
                    {
                        attributesAndValues = WareInformation.GetWareInformation(selectedAttributes);
                        VisualCalculator.WareDisplay(selectedAttributes, attributesAndValues);
                    }
                    catch (NullReferenceException e)
                    {
                        Support.ErrorHandling(e, $"No attibutes were selected. {e.Message}");
                    }
                }
                else
                {
                    try
                    {
                        List <List <string> > wareValues = SQLCode.SQLControl.GetValuesAllWare(selectedAttributes.ToArray());
                        VisualCalculator.WareDisplay(selectedAttributes.ToArray(), wareValues);
                    }
                    catch (Exception e)
                    {
                        Support.ErrorHandling(e, $"Encountered an error: {e.Message}");
                    }
                }
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Removes a ware. Returns true if the item was found and removed else false
 /// </summary>
 /// <param name="ID">The ID of the ware to remove</param>
 /// <returns>Returns true if the item was found and removed else false</returns>
 public static bool RemoveWare(string ID)
 {
     if (Support.Confirmation())
     {
         if (!SQLCode.SQLControl.DatabaseInUse)
         {
             return(WareInformation.RemoveWare(ID));
         }
         try {
             SQLCode.StoredProcedures.RunDeleteWareSP($"'{ID}'");
         }
         catch (Exception e)
         {
             Support.ErrorHandling(e, $"Failed at removing ware: {e.Message}");
             return(false);
         }
         return(true);
     }
     return(false);
 }
Exemplo n.º 6
0
        /// <summary>
        /// Function used to set database (or no database).
        /// </summary>
        public void DatabaseSelectionMenu()
        {
            string[] options = new string[] { "Window login Authentication", "SQL Server Authentication", "No SQL Database" };
            string[] sqlInfo;
            string   firstConnection;

            bool run = true;

            Reporter.Log("Program starting");
            do
            {
                byte answer = VisualCalculator.MenuRun(options, "Database");
                switch (answer)
                {
                case 0:
                    sqlInfo    = new string[2];
                    sqlInfo[0] = Support.CollectString("Enter Servername");
                    sqlInfo[1] = Support.CollectString("Enter database");
                    if (!ShallDatabaseInitialiseMenu())
                    {
                        try
                        {
                            SQLCode.SQLControl.DataBase = sqlInfo[1];
                            firstConnection             = SQLCode.SQLControl.CreateConnectionString(sqlInfo[0], "master");
                            run = !SQLCode.SQLControl.InitalitionOfDatabase(sqlInfo, firstConnection, true);
                            SQLCode.SQLControl.DatabaseInUse = true;
                        }
                        catch (Exception e)
                        {
                            Support.ErrorHandling(e, $"Encounted an error: {e.Message}");
                            run = true;
                        }
                    }
                    else
                    {
                        try
                        {
                            SQLCode.SQLControl.DataBase = sqlInfo[1];
                            run = !SQLCode.SQLControl.CreateConnection(sqlInfo, true);
                            SQLCode.SQLControl.DatabaseInUse = true;
                        }
                        catch (Exception e)
                        {
                            Support.ErrorHandling(e, $"Encounted an error: {e.Message}");
                            run = true;
                        }
                    }
                    break;

                case 1:
                    sqlInfo    = new string[4];
                    sqlInfo[0] = Support.CollectString("Enter Servername");
                    sqlInfo[1] = Support.CollectString("Enter SQL Username");
                    sqlInfo[2] = Support.HiddenText("Enter Password");
                    sqlInfo[3] = Support.CollectString("Enter database");
                    if (!ShallDatabaseInitialiseMenu())
                    {
                        try
                        {
                            SQLCode.SQLControl.DataBase = sqlInfo[3];
                            firstConnection             = SQLCode.SQLControl.CreateConnectionString(sqlInfo[0], sqlInfo[1], sqlInfo[2], "master");
                            run = !SQLCode.SQLControl.InitalitionOfDatabase(sqlInfo, firstConnection, false);
                            SQLCode.SQLControl.DatabaseInUse = true;
                        }
                        catch (Exception e)
                        {
                            Support.ErrorHandling(e, $"Encounted an error: {e.Message}");
                            run = true;
                        }
                    }
                    else
                    {
                        try {
                            SQLCode.SQLControl.DataBase = sqlInfo[3];
                            run = !SQLCode.SQLControl.CreateConnection(sqlInfo, false);
                            SQLCode.SQLControl.DatabaseInUse = true;
                        }
                        catch (Exception e)
                        {
                            Support.ErrorHandling(e, $"Encounted an error: {e.Message}");
                            run = true;
                        }
                    }

                    break;

                case 2:
                    WareInformation.AddWareDefault();
                    SQLCode.SQLControl.DatabaseInUse = false;
                    break;
                }
                if (answer == options.Length - 1)
                {
                    run = false;
                }
                if (run == true)
                {
                    Console.Clear();
                    Console.WriteLine("Could not establish connection to the database");
                    Support.WaitOnKeyInput();
                }
            } while (run);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Allows for the modifcation of one or more values of the ware with <paramref name="ID"/>.
        ///
        /// </summary>
        /// <param name="ID"></param>
        public static void ModifyWare(string ID)
        {
            if (!SQLCode.SQLControl.DatabaseInUse)
            {
                string[] options = null;
                try
                {
                    options = GenerateOptions(ID, SQLCode.SQLControl.DatabaseInUse);
                }
                catch (Exception e)
                {
                    Support.ErrorHandling(e, $"Encounted an error: {e.Message}");
                }
                if (options != null)
                {
                    Dictionary <string, object> informations = WareInformation.GetWareInformation(ID, out List <Type> valueTypes);
                    byte?answer;
                    do
                    {
                        answer = VisualCalculator.MenuRun(options, "Select entry to modify");
                        if (answer != options.Length - 1)
                        {
                            object oldValue = informations[options[(byte)answer]];
                            if (valueTypes[(byte)answer].IsValueType)
                            {
                                try
                                {
                                    object newValue = CollectValue(valueTypes[(byte)answer], oldValue);
                                    Publisher.PubWare.AlterWare(ID, newValue, options[(byte)answer]);
                                }
                                catch (Exception e)
                                {
                                    ErrorHandling(e);
                                }
                            }
                            else
                            { //string and arrays goes here.
                                if (valueTypes[(byte)answer].FullName == "System.String")
                                {
                                    FillOutString(options, answer, oldValue);
                                }
                                else if (valueTypes[(byte)answer].BaseType.Name == "Array")
                                {
                                    FillOutArray(options, answer, valueTypes, oldValue);
                                }
                            }
                        }
                    } while (answer != options.Length - 1);
                }
            }
            else //SQL
            { //get the type of the ID, find the attributes of that ID
                string[] options        = GenerateOptions(ID, SQLCode.SQLControl.DatabaseInUse);
                string[] columns        = new string[options.Length - 1];
                string[] allColumns     = null;
                string[] allColumnTypes = null;
                try {
                    allColumns = SQLCode.StoredProcedures.GetColumnNamesAndTypesSP(out allColumnTypes);
                }
                catch (Exception e)
                {
                    Support.ErrorHandling(e, $"Could not retrive column names and types: {e.Message}");
                }
                if (allColumns != null && allColumnTypes != null)
                {
                    for (int i = 0; i < columns.Length; i++)
                    {
                        columns[i] = options[i];
                    }
                    List <string> values = SQLCode.SQLControl.GetValuesSingleWare(columns, $"'{ID}'");
                    byte?         answer;
                    do
                    {
                        answer = VisualCalculator.MenuRun(options, "Select entry to modify");
                        if (answer != options.Length - 1)
                        {
                            string oldValue = values[(byte)answer] != "" ? values[(byte)answer] : "Null";
                            OutPut.FullScreenClear();
                            OutPut.DisplayMessage($"Old Value was {oldValue}. Enter new Value: ", true);
                            string newValue = Console.ReadLine(); //MSSQL does not seem like it has arrays as a datatype
                            for (int i = 0; i < allColumns.Length; i++)
                            {
                                if (allColumns[i] == options[(byte)answer])
                                {
                                    if (allColumnTypes[i] == "nvarchar")
                                    {
                                        newValue = $"'{newValue}'";
                                        break;
                                    }
                                }
                            }
                            try
                            {
                                SQLCode.SQLControl.ModifyWare("Inventory", new string[] { options[(byte)answer] }, new string[] { newValue }, $"id = '{ID}'");
                            }
                            catch (Exception e)
                            {
                                Reporter.Report(e);
                                OutPut.FullScreenClear();
                                OutPut.DisplayMessage($"Could not create the ware: {e.Message}", true);
                                Support.WaitOnKeyInput();
                            }
                        }
                    } while (answer != options.Length - 1);
                }
            }

            void ErrorHandling(Exception e)
            {
                Reporter.Report(e);
                OutPut.FullScreenClear();
                OutPut.DisplayMessage(String.Format("Could not convert: {0}", e.InnerException.Message), true);
                Support.WaitOnKeyInput();
            }

            void FillOutString(string[] options, byte?answer, object oldValue)
            {
                OutPut.FullScreenClear();
                OutPut.DisplayMessage($"Old Value was {oldValue ?? "Null"}. Enter new Value: ", true);
                string newValue = Input.GetString();

                try
                {
                    Publisher.PubWare.AlterWare(ID, newValue, options[(byte)answer]);
                }
                catch (Exception e)
                {
                    Support.ErrorHandling(e, $"Encountered an error: {e.Message}");
                }
            }

            void FillOutArray(string[] options, byte?answer, List <Type> valueTypes, object oldValue)
            {
                List <object> objectList = new List <object>();

                string[] addValueOptions = new string[] { "Enter Value", "Done" };
                byte?    valueAnswer;

                do
                {
                    valueAnswer = VisualCalculator.MenuRun(addValueOptions, "Add Data Entry");
                    if (valueAnswer == 0)
                    {
                        if (!valueTypes[(byte)answer].Name.Contains("String"))
                        { //non-string
                            try
                            {
                                objectList.Add(CollectValue(Type.GetType(valueTypes[(byte)answer].FullName.Remove(valueTypes[(byte)answer].FullName.Length - 2, 2)), oldValue)); //code inside of Type.GetType(...) converts an array type to a non-array type
                            }
                            catch (Exception e)
                            {
                                ErrorHandling(e);
                            }
                        }
                        else
                        { //string
                            OutPut.FullScreenClear();
                            objectList.Add(Input.GetString());
                        }
                    }
                } while (valueAnswer != addValueOptions.Length - 1);
                object[] objectArray = objectList.ToArray();
                try
                {
                    Publisher.PubWare.AlterWare(ID, objectArray, options[(byte)answer]);
                }
                catch (Exception e)
                {
                    Support.ErrorHandling(e, $"Encountered an erorr: {e.Message}");
                }
            }
        }
Exemplo n.º 8
0
 /// <summary>
 /// Used for unit testing
 /// </summary>
 /// <param name="ID"></param>
 public static void RemoveWareTesting(string ID)
 {
     WareInformation.RemoveWare(ID);
 }