Example #1
0
 /// <summary>
 /// IndexOperator for accessing configuration property directly with their names
 /// </summary>
 /// <param name="propertyName">Key of the variable.</param>
 /// <returns>The variable</returns>
 public object this[string propertyName] {
     get {
         if (hasProperty(propertyName))
         {
             if (singleValueProperties.ContainsKey(propertyName))
             {
                 return(singleValueProperties[propertyName]);
             }
             if (multiValueProperties.ContainsKey(propertyName))
             {
                 return(multiValueProperties[propertyName]);
             }
         }
         object defaultValue = AppDefaults.getDefaultValue(propertyName);
         if (defaultValue != null)
         {
             this[propertyName] = AppDefaults.getDefaultValue(propertyName);
             return(this[propertyName]);
         }
         log.Error("Couldn't find property " + propertyName + " in " + configFilePath);
         return(null);
     }
     set {
         if (hasProperty(propertyName))
         {
             var s = value as string;
             if (s != null)
             {
                 changeProperty(propertyName, s);
             }
             else if (value is List <string> )
             {
                 changeProperty(propertyName, (List <string>)value);
             }
         }
         else
         {
             var s = value as string;
             if (s != null)
             {
                 addProperty(propertyName, s);
             }
             else if (value is List <string> )
             {
                 addProperty(propertyName, (List <string>)value);
             }
         }
     }
 }
        public DataConnections(Person person, Vehicle vehicle, AppDefaults appDefaults, Travel travel, Places places, Problem problem,
                               Maintenance maintenance, FuelMileage fuelMileage, Page page)
        {
            this.Vehicle     = vehicle;
            this.Person      = person;
            this.AppDefaults = appDefaults;
            this.Travel      = travel;
            this.Problem     = problem;
            this.Places      = places;
            this.Maintenance = maintenance;
            this.FuelMileage = fuelMileage;



            DefaultsData = new SQLiteConnection(path);
            //DefaultsData.CreateTable<AppDefaults>(CreateFlags.ImplicitPK);
            VehicleData = new SQLiteConnection(path2);
            //VehicleData.CreateTable<Vehicle>(CreateFlags.ImplicitPK);
            PersonData = new SQLiteConnection(path3);
            //PersonData.CreateTable<Person>(CreateFlags.ImplicitPK);
            TravelData = new SQLiteConnection(path4);
            // TravelData.CreateTable<Travel>(CreateFlags.ImplicitPK);
            PlacesData = new SQLiteConnection(path6);
            //PlacesData.CreateTable<Places>(CreateFlags.ImplicitPK);
            ProblemData = new SQLiteConnection(path6);
            // ProblemData.CreateTable<Problem>(CreateFlags.ImplicitPK);
            MaintenanceData = new SQLiteConnection(path7);
            // MaintenanceData.CreateTable<Maintenance>(CreateFlags.ImplicitPK);
            FuelData = new SQLiteConnection(path8);
            // FuelData.CreateTable<FuelMileage>(CreateFlags.ImplicitPK);


            var maxPk = DefaultsData.Table <AppDefaults>().OrderByDescending(d => d.Id).FirstOrDefault();

            appDefaults = new AppDefaults()
            {
                Id = (maxPk == null ? 1 : maxPk.Id + 1),
                DriversLicenseNumber = "1",
                FirstName            = "Christopher",
                LastName             = "Columbus",
                LicensePlate         = "N3w4pp",
                Year    = "1492",
                Make    = "Santa",
                Model   = "Maria",
                Mileage = "12,200 ",
            };
        }
Example #3
0
        /// <summary>
        /// Parse the given configuration file and stores the data to the config
        /// </summary>
        /// <param name="configFilepath">the path to the config file to read</param>
        public static ConfigurationWrapper readConfiguration(string configFilepath)
        {
            FileStream    configFileStream  = null;
            StreamReader  configFileReader  = null;
            Configuration someConfiguration = new Configuration();

            try {
                ParserMode    mode = ParserMode.Normal;
                string        currentLine;
                int           lineCounter         = 0;
                string        currentPropertyName = null;
                List <string> multipleValues      = new List <string>();

                configFileStream = File.Open(configFilepath, FileMode.OpenOrCreate, FileAccess.Read);
                configFileReader = new StreamReader(configFileStream);

                while ((currentLine = configFileReader.ReadLine()) != null)
                {
                    lineCounter++;

                    currentLine = cleanupLine(currentLine);

                    if (String.IsNullOrEmpty(currentLine) || isComment(currentLine))
                    {
                        continue;
                    }

                    switch (mode)
                    {
                    case ParserMode.MultiValueProperty:
                        if (currentLine.Equals(Configuration.END_MULTI_VALUE_FIELD))
                        {
                            someConfiguration[currentPropertyName] = multipleValues;
                            mode = ParserMode.Normal;
                        }
                        else
                        {
                            multipleValues.Add(currentLine);
                        }
                        continue;

                    case ParserMode.Normal:
                        int keyValueSeparatorIndex =
                            currentLine.IndexOf(Configuration.KEY_VALUE_SEPARATOR, StringComparison.Ordinal);
                        if (keyValueSeparatorIndex <= 0)
                        {
                            logCorruptLine(lineCounter);
                            continue;
                        }
                        currentPropertyName = currentLine.Substring(0, keyValueSeparatorIndex).Trim();
                        string valuePart = currentLine.Substring(keyValueSeparatorIndex + 1).Trim();

                        if (String.IsNullOrEmpty(valuePart))
                        {
                            log.Debug("Property " + currentPropertyName + " is not set");
                            continue;
                        }
                        if (valuePart.Equals(Configuration.BEGIN_MULTI_VALUE_PROPERTY))
                        {
                            mode = ParserMode.MultiValueProperty;
                            multipleValues.Clear();
                        }
                        else
                        {
                            someConfiguration[currentPropertyName] = valuePart;
                        }
                        break;

                    default:
                        continue;
                    }
                }
            } catch (Exception ex) {
                AppConfigurationWrapper appConfiguration = AppDefaults.getDefaultConfiguration();
                writeConfiguration(appConfiguration, configFilepath);
                log.Error("Couldn't process config file " + configFilepath + ":" + ex.Message);
                log.Warn("Generating configuration file using default values.");
            } finally {
                configFileStream?.Close();
                configFileReader?.Close();
            }
            return(new ConfigurationWrapper(someConfiguration));
        }
 private void resetToDefaults()
 {
     readConfiguration(AppDefaults.getDefaultConfiguration());
 }