Example #1
0
        /// <summary>
        /// Compares 2 computers, the base and target.
        /// Appends a numerical score to the target computer.
        /// </summary>
        /// <param name="targetComputer">The target computer PC instance.</param>
        /// <param name="baseComputer">The base computer PC instance.</param>
        public static void GetScore(PC targetComputer, PC baseComputer)
        {
            Type currentClass;
            FieldInfo currentList;
            FieldInfo[] classFields;
            IEnumerable baseList;
            IEnumerable targetList;
            double temporaryScore;

            // Reset score first
            targetComputer.Score = 0;

            foreach (string classString in ClassList.DatabaseClasses)
            {
                if (abortOperation) break;
                // Get each class's element list for both base and target computers
                currentClass = Type.GetType("CoreView." + classString);
                currentList = baseComputer.GetType().GetField(classString);

                classFields = currentClass.GetFields();

                // Get the lists as generic enumerable objects
                baseList = (IEnumerable)currentList.GetValue(baseComputer);
                targetList = (IEnumerable)currentList.GetValue(targetComputer);

                // For every field in the object of the element list
                foreach (FieldInfo field in classFields)
                {
                    if (abortOperation) break;
                    // Iterate through each and every object instance in the base/target lists
                    // Find a match between any combination of indexes
                    foreach (object baseObject in baseList)
                    {
                        if (abortOperation) break;
                        foreach (object targetObject in targetList)
                        {
                            if (abortOperation) break;
                            // If a match is found, give the target computer a higher score
                            if (field.GetValue(baseObject) == field.GetValue(targetObject))
                            {
                                // Base weight
                                temporaryScore = Configuration.Weightings[currentClass.Name][field.Name];
                                // Complaint-dependent multipliers
                                foreach (KeyValuePair<string, bool> complaint in Configuration.Complaints)
                                {
                                    if (complaint.Value == true)
                                    {
                                        // If the complaint is set, multiply the score by its respective entry
                                        temporaryScore *= Configuration.Weightings[complaint.Key][currentClass.Name];
                                    }
                                }
                                targetComputer.Score += temporaryScore;
                            }
                        }
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Gets all of the information about a computer from the database in the form of a PC class.
        /// </summary>
        /// <param name="id">The ID of the computer to retrieve from the database.</param>
        /// <returns>A PC class containing all related information.</returns>
        public static PC GetComputer(int id)
        {
            if (Connection != null && !abortOperation)
            {
                PC fetched = new PC();

                SQLiteCommand command = Connection.CreateCommand();
                SQLiteDataReader data;
                FieldInfo field, classField;
                DataTable table;

                // Loop through each class and get each row of the table for the specified index
                foreach (string classString in ClassList.DatabaseClasses)
                {
                    if (abortOperation) break;
                    // Open database connection
                    Connection.Open();

                    // If the database has been opened, continue
                    if (Connection.State == ConnectionState.Open)
                    {
                        // Select the computer entry with the given id
                        command.CommandText = "SELECT * FROM "
                            + classString
                            + " WHERE id='"
                            + id
                            + "'";
                        data = command.ExecuteReader();

                        // Move data to a DataTable
                        table = new DataTable();
                        table.Load(data);

                        // Destroy the connection whilst working on the data to minimise corruption
                        data.Dispose();
                        Connection.Close();

                        // Get the appropriate computer field to write to
                        field = fetched.GetType().GetField(classString);

                        // Iterate through each record and add the data to the class container
                        foreach (DataRow row in table.Rows)
                        {
                            if (abortOperation) break;
                            // Prepare container by making a new instance of the reffered class
                            object classContainer = Activator.CreateInstance(Type.GetType("CoreView." + classString));
                            foreach (DataColumn column in table.Columns)
                            {
                                if (abortOperation) break;
                                // Organise the fetched data into an appropriate class container
                                // Get the field referred to by the current column
                                classField = classContainer.GetType().GetField(column.ColumnName);
                                if (classField != null)
                                {
                                    // Insert the field into the class container converted as its native data type
                                    classField.SetValue(classContainer, Convert.ChangeType(row[column], classField.FieldType));
                                }
                            }
                            // Add this class container to the computer's field list
                            field.FieldType.GetMethod("Add").Invoke(field.GetValue(fetched), new[] { classContainer });
                        }
                    }
                    Connection.Close();
                }

                Connection.Open();
                if (Connection.State == ConnectionState.Open)
                {
                    // Get comments
                    command.CommandText = "SELECT * FROM "
                            + "comments"
                            + " WHERE id='"
                            + id
                            + "'";
                    data = command.ExecuteReader();

                    // The comments are in column 1
                    while (data.Read()) fetched.Comments = Convert.ToString(data[1]);
                }
                Connection.Close();

                // Finally, apprend the ID number
                fetched.ID = id;

                // Return the found data as a whole PC object
                return fetched;
            }
            else
            {
                return null;
            }
        }