public bool Upsert(BingoBall bingoBall)
        {
            var success = true;

            try
            {
                string targetBall = null;

                using (var query = QueryBuilder.Select(SelectResult.Expression(Meta.ID))
                                   .From(DataSource.Database(_database))
                                   .Where(Expression.Property("Name").EqualTo(Expression.String(bingoBall.Name))))
                {
                    var resultArray = query.Execute().ToArray();
                    if (resultArray.Length > 0)
                    {
                        targetBall = resultArray[0].GetValue(0)?.ToString();
                    }
                }

                if (targetBall == null)
                {
                    using var mutableDoc = new MutableDocument();

                    mutableDoc.SetString("Name", bingoBall.Name)
                    .SetBoolean("IsPlayed", false)
                    .SetBoolean("IsMatched", false)
                    .SetArray("PlayedBy", new MutableArrayObject())
                    .SetArray("MatchedBy", new MutableArrayObject());

                    _database.Save(mutableDoc);
                }
                else
                {
                    MutableDocument doc = _database.GetDocument(targetBall)?.ToMutable();
                    doc.SetBoolean("IsPlayed", bingoBall.IsPlayed);
                    doc.SetBoolean("IsMatched", bingoBall.IsMatched);
                    doc.SetArray("PlayedBy", new MutableArrayObject(bingoBall.PlayedBy));
                    doc.SetArray("MatchedBy", new MutableArrayObject(bingoBall.MatchedBy));

                    _database.Save(doc);
                }
            }
            catch (Exception e)
            {
                success = false;
            }
            return(success);
        }
        public void TestGetArray()
        {
            var mNestedArray = new MutableArrayObject();

            mNestedArray.AddLong(1L);
            mNestedArray.AddString("Hello");
            mNestedArray.AddValue(null);

            var mArray = new MutableArrayObject();

            mArray.AddLong(1L);
            mArray.AddString("Hello");
            mArray.AddValue(null);
            mArray.AddArray(mNestedArray);

            using (var mDoc = new MutableDocument("test")) {
                mDoc.SetArray("array", mArray);

                using (var doc = Db.Save(mDoc)) {
                    var array = doc.GetArray("array");
                    array.Should().NotBeNull();
                    array.GetArray(0).Should().BeNull();
                    array.GetArray(1).Should().BeNull();
                    array.GetArray(2).Should().BeNull();
                    array.GetArray(3).Should().NotBeNull();

                    var nestedArray = array.GetArray(3);
                    nestedArray.ShouldBeEquivalentTo(mNestedArray);
                    array.ShouldBeEquivalentTo(mArray);
                }
            }
        }
        private MutableDocument CreateDocumentWithTag(string id, string tag)
        {
            var doc = new MutableDocument(id);

            doc.SetString("tag", tag);

            doc.SetString("firstName", "Daniel");
            doc.SetString("lastName", "Tiger");

            var address = new MutableDictionary();

            address.SetString("street", "1 Main street");
            address.SetString("city", "Mountain View");
            address.SetString("state", "CA");
            doc.SetDictionary("address", address);

            var phones = new MutableArray();

            phones.AddString("650-123-0001").AddString("650-123-0002");
            doc.SetArray("phones", phones);

            doc.SetDate("updated", DateTimeOffset.UtcNow);

            return(doc);
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            #region Data Control

            if (!String.IsNullOrEmpty(OperationTextBox.Text) && OperationComboBox.SelectedIndex == -1)
            {
                MessageBox.Show("Select a Operation value.", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (!String.IsNullOrEmpty(MaintenanceTextBox.Text) && MaintenanceComboBox.SelectedIndex == -1)
            {
                MessageBox.Show("Select a Maintenance value.", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (!String.IsNullOrEmpty(TechAssistanceTextBox.Text) && TechAssistanceComboBox.SelectedIndex == -1)
            {
                MessageBox.Show("Select a Tech Assistance value.", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (!String.IsNullOrEmpty(TechPubTextBox.Text) && TechPubComboBox.SelectedIndex == -1)
            {
                MessageBox.Show("Select a Tech Pub value.", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (!String.IsNullOrEmpty(LogisticsTextBox.Text) && LogisticsComboBox.SelectedIndex == -1)
            {
                MessageBox.Show("Select a Logistics value.", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (!String.IsNullOrEmpty(CSCTextBox.Text) && CSCComboBox.SelectedIndex == -1)
            {
                MessageBox.Show("Select a CSC value.", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (!String.IsNullOrEmpty(CommunicationTextBox.Text) && CommunicationComboBox.SelectedIndex == -1)
            {
                MessageBox.Show("Select a CSC value.", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (!String.IsNullOrEmpty(CommercialSupportTextBox.Text) && CommercialSupportComboBox.SelectedIndex == -1)
            {
                MessageBox.Show("Select a Commercial Support value.", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (!String.IsNullOrEmpty(CostOfOperationTextBox.Text) && CostOfOperationComboBox.SelectedIndex == -1)
            {
                MessageBox.Show("Select a Cost Of Operation value.", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (!String.IsNullOrEmpty(WarrentyAdministrationTextBox.Text) && WarrentyAdministrationComboBox.SelectedIndex == -1)
            {
                MessageBox.Show("Select a Administration value.", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (!String.IsNullOrEmpty(OEMServiceTextBox.Text) && OEMServiceComboBox.SelectedIndex == -1)
            {
                MessageBox.Show("Select a OEM Service value.", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (!String.IsNullOrEmpty(VendorManagementTextBox.Text) && VendorManagementComboBox.SelectedIndex == -1)
            {
                MessageBox.Show("Select a Vendor Management value.", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            if (CustomerComboBox.SelectedIndex == -1)
            {
                MessageBox.Show("Select a Customer for this Report.", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            #endregion

            string id = null;
            using (var mutableDoc = new MutableDocument())
            {
                if (!string.IsNullOrWhiteSpace(GlobalCommentTextBox.Text))
                {
                    mutableDoc.SetString("commentaireGenerale", GlobalCommentTextBox.Text.Trim().ToLower());
                }

                mutableDoc.SetString("customer", CustomerComboBox.Text.Trim().ToLower());

                mutableDoc.SetString("type", "report");
                mutableDoc.SetString("mois", DateTime.Now.ToString("MMMMMMMMMMMMM").ToLower());

                var rubriques = new MutableArrayObject();
                mutableDoc.SetArray("rubriques", rubriques);

                if (OperationComboBox.SelectedIndex != -1)
                {
                    var operation = new MutableDictionaryObject();
                    operation.SetString("nom", "operation")
                    .SetInt("note", Int32.Parse(((ComboBoxItem)OperationComboBox.SelectedItem).Content.ToString()));
                    if (!String.IsNullOrEmpty(OperationTextBox.Text))
                    {
                        operation.SetString("commentaire", OperationTextBox.Text.ToLower().TrimEnd().TrimStart());
                    }
                    rubriques.AddDictionary(operation);
                }

                if (MaintenanceComboBox.SelectedIndex != -1)
                {
                    var maintenance = new MutableDictionaryObject();
                    maintenance.SetString("nom", "maintenance")
                    .SetInt("note", Int32.Parse(((ComboBoxItem)MaintenanceComboBox.SelectedItem).Content.ToString()));
                    if (!String.IsNullOrEmpty(MaintenanceTextBox.Text))
                    {
                        maintenance.SetString("commentaire", MaintenanceTextBox.Text.ToLower().TrimEnd().TrimStart());
                    }
                    rubriques.AddDictionary(maintenance);
                }

                if (TechAssistanceComboBox.SelectedIndex != -1)
                {
                    var techAssistance = new MutableDictionaryObject();
                    techAssistance.SetString("nom", "techAssistance")
                    .SetInt("note", Int32.Parse(((ComboBoxItem)TechAssistanceComboBox.SelectedItem).Content.ToString()));
                    if (!String.IsNullOrEmpty(TechAssistanceTextBox.Text))
                    {
                        techAssistance.SetString("commentaire", TechAssistanceTextBox.Text.ToLower().TrimEnd().TrimStart());
                    }
                    rubriques.AddDictionary(techAssistance);
                }

                if (TechPubComboBox.SelectedIndex != -1)
                {
                    var techPub = new MutableDictionaryObject();
                    techPub.SetString("nom", "techPub")
                    .SetInt("note", Int32.Parse(((ComboBoxItem)TechPubComboBox.SelectedItem).Content.ToString()));
                    if (!String.IsNullOrEmpty(TechPubTextBox.Text))
                    {
                        techPub.SetString("commentaire", TechPubTextBox.Text.ToLower().TrimEnd().TrimStart());
                    }
                    rubriques.AddDictionary(techPub);
                }

                if (LogisticsComboBox.SelectedIndex != -1)
                {
                    var logistics = new MutableDictionaryObject();
                    logistics.SetString("nom", "logistics")
                    .SetInt("note", Int32.Parse(((ComboBoxItem)LogisticsComboBox.SelectedItem).Content.ToString()));
                    if (!String.IsNullOrEmpty(LogisticsTextBox.Text))
                    {
                        logistics.SetString("commentaire", LogisticsTextBox.Text.ToLower().TrimEnd().TrimStart());
                    }
                    rubriques.AddDictionary(logistics);
                }

                if (CSCComboBox.SelectedIndex != -1)
                {
                    var csc = new MutableDictionaryObject();
                    csc.SetString("nom", "csc")
                    .SetInt("note", Int32.Parse(((ComboBoxItem)CSCComboBox.SelectedItem).Content.ToString()));
                    if (!String.IsNullOrEmpty(CSCTextBox.Text))
                    {
                        csc.SetString("commentaire", CSCTextBox.Text.ToLower().TrimEnd().TrimStart());
                    }
                    rubriques.AddDictionary(csc);
                }

                if (CommunicationComboBox.SelectedIndex != -1)
                {
                    var communication = new MutableDictionaryObject();
                    communication.SetString("nom", "communication")
                    .SetInt("note", Int32.Parse(((ComboBoxItem)CommunicationComboBox.SelectedItem).Content.ToString()));
                    if (!String.IsNullOrEmpty(CommunicationTextBox.Text))
                    {
                        communication.SetString("commentaire", CommunicationTextBox.Text.ToLower().TrimEnd().TrimStart());
                    }
                    rubriques.AddDictionary(communication);
                }

                if (CommercialSupportComboBox.SelectedIndex != -1)
                {
                    var commercialSupport = new MutableDictionaryObject();
                    commercialSupport.SetString("nom", "commercialSupport")
                    .SetInt("note", Int32.Parse(((ComboBoxItem)CommercialSupportComboBox.SelectedItem).Content.ToString()));
                    if (!String.IsNullOrEmpty(CommercialSupportTextBox.Text))
                    {
                        commercialSupport.SetString("commentaire", CommercialSupportTextBox.Text.ToLower().TrimEnd().TrimStart());
                    }
                    rubriques.AddDictionary(commercialSupport);
                }

                if (CostOfOperationComboBox.SelectedIndex != -1)
                {
                    var CostOfOperation = new MutableDictionaryObject();
                    CostOfOperation.SetString("nom", "costOfOperation")
                    .SetInt("note", Int32.Parse(((ComboBoxItem)CostOfOperationComboBox.SelectedItem).Content.ToString()));
                    if (!String.IsNullOrEmpty(CostOfOperationTextBox.Text))
                    {
                        CostOfOperation.SetString("commentaire", CostOfOperationTextBox.Text.ToLower().TrimEnd().TrimStart());
                    }
                    rubriques.AddDictionary(CostOfOperation);
                }

                if (WarrentyAdministrationComboBox.SelectedIndex != -1)
                {
                    var WarrentyAdministration = new MutableDictionaryObject();
                    WarrentyAdministration.SetString("nom", "warrentyAdministration")
                    .SetInt("note", Int32.Parse(((ComboBoxItem)WarrentyAdministrationComboBox.SelectedItem).Content.ToString()));
                    if (!String.IsNullOrEmpty(WarrentyAdministrationTextBox.Text))
                    {
                        WarrentyAdministration.SetString("commentaire", WarrentyAdministrationTextBox.Text.ToLower().TrimEnd().TrimStart());
                    }
                    rubriques.AddDictionary(WarrentyAdministration);
                }

                if (OEMServiceComboBox.SelectedIndex != -1)
                {
                    var oemService = new MutableDictionaryObject();
                    oemService.SetString("nom", "oemService")
                    .SetInt("note", Int32.Parse(((ComboBoxItem)OEMServiceComboBox.SelectedItem).Content.ToString()));
                    if (!String.IsNullOrEmpty(OEMServiceTextBox.Text))
                    {
                        oemService.SetString("commentaire", OEMServiceTextBox.Text.ToLower().TrimEnd().TrimStart());
                    }
                    rubriques.AddDictionary(oemService);
                }

                if (VendorManagementComboBox.SelectedIndex != -1)
                {
                    var vendorManagement = new MutableDictionaryObject();
                    vendorManagement.SetString("nom", "vendorManagement")
                    .SetInt("note", Int32.Parse(((ComboBoxItem)VendorManagementComboBox.SelectedItem).Content.ToString()));
                    if (!String.IsNullOrEmpty(VendorManagementTextBox.Text))
                    {
                        vendorManagement.SetString("commentaire", VendorManagementTextBox.Text.ToLower().TrimEnd().TrimStart());
                    }
                    rubriques.AddDictionary(vendorManagement);
                }

                Database.Save(mutableDoc);
                id = mutableDoc.Id;
            }

            Console.WriteLine("Document saved with : " + id);
            Logger.Info("Document saved with : " + id);

            MessageBox.Show("Report saved with : " + id,
                            "Report Saved",
                            MessageBoxButton.OK,
                            MessageBoxImage.Information);
        }