Esempio n. 1
0
        public async Task <BaseSingleResponse <T> > GetAsync <T>(string pEndpoint, string pObjName, Dictionary <string, string> pParamsDictionary, RequestFormat pFormat = RequestFormat.Json)
        {
            try
            {
                ClientCacheEntry OldData = null;
                // Try to find cached results
                if (this.Cache != null)
                {
                    OldData = Cache.FindEntry(pEndpoint);
                }
                if (OldData == null)
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                    var response = Task.Run(() => GetAsync(pEndpoint)).Result;
                    using (var responseStream = await response.Content.ReadAsStreamAsync())
                    {
                        var jsonMessage = new StreamReader(responseStream).ReadToEnd();
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            var returndata = BaseSingleResponse <T> .Deserialize <T>(jsonMessage, pObjName);

                            // Add Header from Response
                            returndata.Headers    = response.Headers;
                            returndata.StatusCode = response.StatusCode;
                            Cache?.AddEntry(pEndpoint, jsonMessage, DateTime.Now, response.Headers, response.StatusCode);
                            return(returndata);
                        }
                        return(new BaseSingleResponse <T>
                        {
                            Headers = response.Headers,
                            StatusCode = response.StatusCode,
                            Message = jsonMessage
                        });
                    }
                }
                else
                {
                    var returndata = BaseSingleResponse <T> .Deserialize <T>(OldData.Data, pObjName);

                    returndata.Headers    = OldData.Headers;
                    returndata.StatusCode = OldData.StatusCode;
                    return(returndata);
                }
            }
            catch (Exception ex)
            {
                if (Debugger.IsAttached)
                {
                    Console.WriteLine(ex.Message);
                }
                return(new BaseSingleResponse <T>
                {
                    Data = default(T),
                    Headers = null,
                    StatusCode = HttpStatusCode.UnsupportedMediaType,
                    Message = ex.Message
                });
            }
        }
Esempio n. 2
0
        public static void Migrate()
        {
            try
            {
                                #if WINDOWS_PHONE
                FoodJournalDB db = new FoodJournalDB(false);
                                #else
                FoodJournalDB db = new FoodJournalDB(true);
                                #endif

                if (!db.DatabaseExists())
                {
                    return;
                }

                if (db.Table <FoodItemDO>().Count() == 0 && db.Table <EntryDO>().Count() == 0)
                {
#if !WINDOWS_PHONE
                    db.Close();
#endif
                    Logging.SessionLog.RecordMilestone("Deleting Legacy FoodItem DB", "");
                    db.DeleteDatabase();
                    return;
                }

                                #if !WINDOWS_PHONE
                if (System.IO.File.Exists(DatabaseFilePath))
                {
                    db.Close();
                    System.IO.File.Copy(DatabaseFilePath, DatabaseFilePath.Replace(".db3", ".bak"));
                    db = new FoodJournalDB(true);
                }
                                #endif

                DateTime start = DateTime.Now;

                int ExceptionCount            = 0;
                Dictionary <int, int> itemmap = new Dictionary <int, int>();

                Logging.SessionLog.RecordTraceValue("Database migration - item count", db.Table <FoodItemDO>().Count().ToString());
                Logging.SessionLog.RecordTraceValue("Database migration - selection count", db.Table <EntryDO>().Count().ToString());

                var AllItems      = db.Table <FoodItemDO>().ToList();
                var AllSelections = db.Table <EntryDO>().ToList();

                List <FoodItemDO> ToDeleteItems      = new List <FoodItemDO>();
                List <EntryDO>    ToDeleteSelections = new List <EntryDO>();

                Dictionary <int, String> items     = new Dictionary <int, string>();
                List <DateTime>          savedates = new List <DateTime>();

                foreach (FoodItemDO item in AllItems)
                {
                    try
                    {
                        items.Add(item.Id, item.TextDB);

                        var NewItem = new FoodJournal.Model.FoodItem(item.TextDB, false);

                        NewItem.Culture        = item.Culture;
                        NewItem.DescriptionDB  = item.DescriptionDB;
                        NewItem.LastAmountDB   = item.LastAmountDB;
                        NewItem.NutritionDB    = item.NutritionDB;
                        NewItem.ServingSizesDB = item.ServingSizesDB;
                        NewItem.SourceID       = item.SourceID;

                        Cache.MergeItem(NewItem);

                        ToDeleteItems.Add(item);
                    }
                    catch (Exception ex) { ExceptionCount++; LittleWatson.ReportException(ex); }
                }

                foreach (EntryDO entry in AllSelections)
                {
                    try
                    {
                        var entryDate = DateTime.Parse(entry.Date);

                        var NewSelection = new FoodJournal.Model.Entry(entryDate, entry.Period, items[entry.FoodItemId]);
                        NewSelection.AmountScaleDB    = entry.AmountScaleDB;
                        NewSelection.AmountSelectedDB = entry.AmountSelectedDB;

                        Cache.AddEntry(NewSelection);

                        ToDeleteSelections.Add(entry);

                        if (!savedates.Contains(entryDate))
                        {
                            savedates.Add(entryDate);
                        }
                    }
                    catch (Exception ex) { ExceptionCount++; LittleWatson.ReportException(ex); }
                }

                foreach (Period period in PeriodList.All)
                {
                    foreach (DateTime date in savedates)
                    {
                        FoodJournalNoSQL.StartSaveDay(date, period);
                    }
                }

                int i = 120;
                while (i-- > 0 && FoodJournalNoSQL.IsSaving())
                {
                    System.Threading.Thread.Sleep(500);
                }

                if (i == 0)
                {
                    SessionLog.RecordMilestone("Upgrade failed in 1 minute", AllSelections.Count.ToString());
                    throw new Exception("Upgrade failed in 1 minute");
                }

                foreach (EntryDO Selection in ToDeleteSelections)
                {
                    try
                    {
                                                #if WINDOWS_PHONE
                        db.Table <EntryDO>().DeleteOnSubmit(Selection);
                        db.SubmitChanges();
                                                #else
                        db.Delete <EntryDO>(Selection.Id);
                                                #endif
                    }
                    catch (Exception ex) { db = new FoodJournalDB(false); ExceptionCount++; LittleWatson.ReportException(ex); }
                }

                foreach (FoodItemDO Item in ToDeleteItems)
                {
                    try
                    {
                                                #if WINDOWS_PHONE
                        db.Table <FoodItemDO>().DeleteOnSubmit(Item);
                        db.SubmitChanges();
                                                #else
                        db.Delete <FoodItemDO>(Item.Id);
                                                #endif
                    }
                    catch (Exception ex) { db = new FoodJournalDB(false); ExceptionCount++; LittleWatson.ReportException(ex); }
                }

                Logging.SessionLog.RecordMilestone("Database migration - exception count", ExceptionCount.ToString());
                Logging.SessionLog.RecordMilestone("Database migration - duration", string.Format("{0} s", DateTime.Now.Subtract(start).TotalSeconds));
            }
            catch (Exception ex) { LittleWatson.ReportException(ex); }
        }