Ejemplo n.º 1
0
 public void TestLoadingWithoutCsv(ConversionsDataContext db)
 {
     Pocketailor.Model.ConversionData cd = new Pocketailor.Model.ConversionData()
     {
         Measurements = new Dictionary <MeasurementId, List <double> >()
         {
             { MeasurementId.Chest, new List <double>()
               {
                   -1, 2, 3, 4, -1
               } },
             { MeasurementId.Hips, new List <double>()
               {
                   1, 2, 3, 4, 5
               } },
             { MeasurementId.Waist, new List <double>()
               {
                   1, 2, 3, 4, 5
               } },
         },
         Conversion = ConversionId.DressSize,
         Region     = "GB",
         Brand      = BrandId.MarksSpencer,
         Gender     = GenderId.Female,
         Sizes      = new List <string>()
         {
             "XS", "S", "M", "L", "XL"
         },
     };
     cd.JsonifyData();
     db.ConversionData.InsertOnSubmit(cd);
     db.SubmitChanges();
 }
Ejemplo n.º 2
0
 public static void LoadConversions(ConversionsDataContext db)
 {
     CsvFileReader.ReadFileIntoDb(db, ConversionId.SuitSize);
     CsvFileReader.ReadFileIntoDb(db, ConversionId.ShirtSize);
     CsvFileReader.ReadFileIntoDb(db, ConversionId.TrouserSize);
     CsvFileReader.ReadFileIntoDb(db, ConversionId.HatSize);
     CsvFileReader.ReadFileIntoDb(db, ConversionId.DressSize);
     CsvFileReader.ReadFileIntoDb(db, ConversionId.BraSize);
     CsvFileReader.ReadFileIntoDb(db, ConversionId.HosierySize);
     CsvFileReader.ReadFileIntoDb(db, ConversionId.ShoeSize);
     CsvFileReader.ReadFileIntoDb(db, ConversionId.SkiBootSize);
     CsvFileReader.ReadFileIntoDb(db, ConversionId.WetsuitSize);
 }
Ejemplo n.º 3
0
        public void LoadUpCsv()
        {
            string dbConnectionString;

            if (VersionHelpers.IsWP8)
            {
                dbConnectionString = "Data Source=isostore:/PocketailorConversionsWP8.sdf";
            }
            else
            {
                dbConnectionString = "Data Source=isostore:/PocketailorConversionsWP7.sdf";
            }
            ConversionsDataContext conversionsDb = new ConversionsDataContext(dbConnectionString);

            if (conversionsDb.DatabaseExists())
            {
                conversionsDb.DeleteDatabase();
            }
            conversionsDb.CreateDatabase();

            //this.TestLoadingWithoutCsv(conversionsDb);
            PocketailorDatabaseCreator.Model.SetupDatabase.LoadConversions(conversionsDb);
        }
Ejemplo n.º 4
0
        public static void ReadFileIntoDb(ConversionsDataContext db, ConversionId conversionId)
        {
            Uri uri = new Uri(AppConstants.CSV_DATA_DIRECTORY + conversionId.ToString() + ".txt", UriKind.Relative);
            var res = System.Windows.Application.GetResourceStream(uri);

            System.IO.StreamReader fh = new System.IO.StreamReader(res.Stream);

            int           lineNum = 0;
            List <string> headers = new List <string>();

            Pocketailor.Model.ConversionData cd = null;

            while (!fh.EndOfStream)
            {
                lineNum++;
                string line = fh.ReadLine();
                // Read headers
                if (lineNum == 1)
                {
                    headers = line.Split(AppConstants.CSV_DELIMITERS).ToList <string>();
                    continue;
                }
                // Skip commented lines
                if (line.StartsWith("#"))
                {
                    continue;
                }
                // Skip empty lines
                if (line.Trim() == String.Empty)
                {
                    continue;
                }
                var els = line.Split(AppConstants.CSV_DELIMITERS).ToList <string>();
                // We make the assumption that data for a particular brand, gender, region, conversion are contiguous,
                // so when any one of these change, we change the object
                CsvLine csvLine = new CsvLine();
                csvLine.Conversion = conversionId;
                for (int i = 0; i < els.Count; i++)
                {
                    els[i] = els[i].Trim();
                    switch (headers[i].ToLower())
                    {
                    //case "conversion":
                    //    csvLine.Conversion = (ConversionId)Enum.Parse(typeof(ConversionId), els[i], true);
                    //    continue;
                    case "brand":
                        csvLine.Brand = (BrandId)Enum.Parse(typeof(BrandId), els[i], true);
                        continue;

                    case "region":
                        csvLine.Region = els[i];
                        continue;

                    case "gender":
                        csvLine.Gender = (GenderId)Enum.Parse(typeof(GenderId), els[i], true);
                        continue;

                    case "sizeid":
                        csvLine.SizeId = Int32.Parse(els[i]);
                        continue;

                    case "size":
                        csvLine.Size = els[i];
                        continue;

                    // Assume all remaining properties are numbers
                    default:
                        double d;
                        if (els[i] == String.Empty)
                        {
                            d = -1;
                        }
                        else
                        {
                            d = Double.Parse(els[i]);
                        }
                        MeasurementId mId = (MeasurementId)Enum.Parse(typeof(MeasurementId), headers[i], true);
                        csvLine.Measurements.Add(mId, d);
                        break;
                    }
                }
                // Take into account the first line
                if (cd == null)
                {
                    cd            = new Pocketailor.Model.ConversionData();
                    cd.Region     = csvLine.Region;
                    cd.Brand      = csvLine.Brand;
                    cd.Conversion = csvLine.Conversion;
                    cd.Gender     = csvLine.Gender;
                }
                // See if need to write a new object to the database i.e. the dataset has changed
                if (csvLine.Region != cd.Region ||
                    csvLine.Brand != cd.Brand ||
                    csvLine.Conversion != cd.Conversion ||
                    csvLine.Gender != cd.Gender)
                {
                    // Write to DB
                    cd.JsonifyData();
                    db.ConversionData.InsertOnSubmit(cd);
                    db.SubmitChanges();
                    // Create next database object
                    cd            = new Pocketailor.Model.ConversionData();
                    cd.Region     = csvLine.Region;
                    cd.Brand      = csvLine.Brand;
                    cd.Conversion = csvLine.Conversion;
                    cd.Gender     = csvLine.Gender;
                }
                // Add the measurements for this line ...
                foreach (MeasurementId mId in csvLine.Measurements.Keys)
                {
                    if (!cd.Measurements.Keys.Contains(mId))
                    {
                        cd.Measurements.Add(mId, new List <double>());
                    }
                    cd.Measurements[mId].Add(csvLine.Measurements[mId]);
                }
                cd.Sizes.Add(csvLine.Size);
                cd.SizeIds.Add(csvLine.SizeId);

                // Write to db is obj count too high
                //if ((objNum % AppConstants.DB_OBJECT_BUFFER_BEFORE_WRITE) == 0)
                //{
                //    db.SubmitChanges();
                //}
            }
            // Write any remaining object to the DB
            if (cd != null)
            {
                cd.JsonifyData();
                db.ConversionData.InsertOnSubmit(cd);
                db.SubmitChanges();
            }
        }