示例#1
0
        /*
         * Insert the data from the JSON parser into the database
         */
        private bool insertDevInfoIntoDB(DeviceInfoHolder devInfo)
        {
            //something is wrong and the device was not found. Bail
            if (devInfo == null)
            {
                Debug.WriteLine("something went terribly wrong with a DeviceInfo. Bailing");
                NavigationService.GoBack();
                return false;
            }

            Topic top = null;
            Debug.WriteLine("putting device info into DB...");

            //try to get a topic of this name from the DB
            //if it fails, make a new one. if it works, update the old
            using (iFixitDataContext db = new iFixitDataContext(App.DBConnectionString))
            {
                bool gotTopicFromDB = true;
                top = DBHelpers.GetCompleteTopic(devInfo.topic_info.name, db);

                if (top == null)
                {
                    top = new Topic();
                    gotTopicFromDB = false;
                }

                //translate devInfo in a Topic()
                //name is already the same
                top.Name = devInfo.topic_info.name;
                top.parentName = devInfo.title;
                top.Contents = devInfo.description;
                top.ImageURL = devInfo.image.text + ".medium";
                top.Populated = true;

                //TODO inject metatdata here like # answers
                top.Description = "";
                top.Description += prepHTML(devInfo.contents, devInfo);

                //now do the same for all attached guides
                foreach (DIGuides g in devInfo.guides)
                {
                    Debug.WriteLine("\tguide " + g.title);

                    //search if the guide already exists, and get or update it
                    Guide gOld = new Guide();
                    gOld.FillFieldsFromDeviceInfo(navTopicName, g);
                    gOld = db.GuidesTable.FirstOrDefault(other => other.GuideID == gOld.GuideID);
                    if (gOld == null)
                    {
                        gOld = new Guide();
                        db.GuidesTable.InsertOnSubmit(gOld);
                    }

                    gOld.FillFieldsFromDeviceInfo(navTopicName, g);

                    // hang it below the topic, it its collection of guides
                    top.AddGuide(gOld);

                    //FIXME do we need to specifically add this to the guide table? is that magic?
                    db.SubmitChanges();
                }

                if (!gotTopicFromDB)
                {
                    db.TopicsTable.InsertOnSubmit(top);
                }

                //update the Topic() into the database
                db.SubmitChanges();

                //force the view model to update
                infoVM.UpdateData();
                updateInfoBrowser();

                //force the views to update
                this.InfoStack.DataContext = infoVM;
                this.GuidesStack.DataContext = infoVM;

                //disable the loading bars
                this.LoadingBarInfo.Visibility = System.Windows.Visibility.Collapsed;
                this.LoadingBarGuides.Visibility = System.Windows.Visibility.Collapsed;

            }

            return true;
        }