Advertisement
Guest User

Untitled

a guest
Aug 1st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 28.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Xrm;
  6. using System.Data;
  7. using System.Data.OleDb;
  8. using System.Collections.Specialized;
  9. using System.Net;
  10. using System.IO;
  11.  
  12. namespace DataExport
  13. {
  14.     public class GenerateMDB
  15.     {
  16.         private static Guid ESSENSCIA_GUID;
  17.         private static string NEW_PASSWORD = "infochem";
  18.  
  19.         private static XrmDataContext XrmContext
  20.         {
  21.             get
  22.             {
  23.                 return Program.XrmContext;
  24.             }
  25.         }
  26.  
  27.         public static void Execute(string outputPath)
  28.         {
  29.             Console.WriteLine("GenerateMDB: Starting");
  30.             Console.WriteLine();
  31.  
  32.             // Get Essenscia account
  33.             ESSENSCIA_GUID = (from a in XrmContext.accounts
  34.                               where a.name == "Essenscia"
  35.                               select a.accountid).Single();
  36.  
  37.             CreateUsersTable();
  38.             Console.WriteLine();
  39.  
  40.             CreateCompaniesTable();
  41.             Console.WriteLine();
  42.  
  43.             CreateGroupsTable();
  44.             Console.WriteLine();
  45.  
  46.             CreateUsersGroupsTable();
  47.             Console.WriteLine();
  48.  
  49.             CreateUserCompaniesTable();
  50.             Console.WriteLine();
  51.  
  52.             UpdateDisappearedUsers();
  53.             Console.WriteLine();
  54.  
  55.             SaveFile(outputPath);
  56.             Console.WriteLine();
  57.  
  58.             Console.WriteLine("GenerateMDB: Complete");
  59.         }
  60.  
  61.         private static void CreateUsersTable()
  62.         {
  63.             Console.WriteLine("GenerateMDB: Read [Users]");
  64.  
  65.             using (GenericAccessDataSetTableAdapters.UsersTableAdapter userAdapter = new GenericAccessDataSetTableAdapters.UsersTableAdapter())
  66.             {
  67.                 Console.WriteLine("GenerateMDB: Removing records from [Users]");
  68.                 userAdapter.Clear();
  69.  
  70.                 // Get the data from the DataTable
  71.                 using (GenericAccessDataSet.UsersDataTable users = userAdapter.GetData())
  72.                 {
  73.                     // Get all contacts where (extranet = true or eNews = true) AND organization != essenscia
  74.                     // We use "description" to store the "genericaccessmdb" parameter because we want to limit our select
  75.                     Console.WriteLine("GenerateMDB: Retrieve contacts from CRM");
  76.                     var contacts = (
  77.                         from c in XrmContext.contacts
  78.                         where
  79.                             // Extranet / eNews
  80.                             (c.qess_extranet == true || c.qess_enews == true)
  81.                             // Cannot belong to Essenscia company
  82.                             && c.parentcustomerid.Value != ESSENSCIA_GUID
  83.                             // Cannot be inactive / deceased / whatever reason to be inactive
  84.                             && c.statecode == "Active"
  85.                             && c.qess_logonname != null
  86.                         select new
  87.                         {
  88.                             ContactId = c.contactid,
  89.                             FirstName = c.firstname,
  90.                             LastName = c.lastname,
  91.                             IDContact = c.qess_idcontact,
  92.                             Title = c.qess_title_contact.qess_genericaccessmdb,
  93.                             Language = c.qbe_language,
  94.                             JobTitle = c.jobtitle,
  95.                             Telephone = c.telephone1,
  96.                             Fax = c.fax,
  97.                             Email = c.emailaddress1,
  98.                             LogonName = c.qess_logonname,
  99.                             NotifyUser = c.qess_notifyuser,
  100.                             NotificationFormat = c.qess_notificationformat
  101.                         });
  102.  
  103.                     foreach (var contact in contacts)
  104.                     {
  105.                         // Construct user row
  106.                         GenericAccessDataSet.UsersRow row = users.NewUsersRow();
  107.  
  108.                         row.UserID = contact.IDContact;
  109.                         row.TitleID = contact.Title;
  110.                         row.LastName = contact.LastName;
  111.                         if (!string.IsNullOrEmpty(contact.FirstName))
  112.                         {
  113.                             row.FirstName = contact.FirstName;
  114.                         }
  115.  
  116.                         switch (contact.Language.Value)
  117.                         {
  118.                             case 1:
  119.                                 row.LanguageID = "NL";
  120.                                 break;
  121.  
  122.                             case 2:
  123.                                 row.LanguageID = "FR";
  124.                                 break;
  125.  
  126.                             case 3:
  127.                                 row.LanguageID = "UK";
  128.                                 break;
  129.                         }
  130.  
  131.                         row.UserFunction = contact.JobTitle;
  132.                         row.IsManager = false;
  133.                         if (!string.IsNullOrEmpty(contact.Telephone))
  134.                         {
  135.                             row.Tel1 = contact.Telephone;
  136.                         }
  137.                         row.Fax1 = contact.Fax;
  138.                         row.Email = contact.Email;
  139.                         row.IsFullTextSearchAllowed = true;
  140.                         row.AcceptNotification = true;
  141.                         row.UserName = contact.LogonName;
  142.  
  143.                         // If NotifyUser is not filled, it's a new user - set to true
  144.                         row.NotifyUser = (contact.NotifyUser.HasValue ? contact.NotifyUser.Value : true);
  145.  
  146.                         // If NotifyUser is true, it's a new user ==> change to false to make sure next time correct update happens
  147.                         // If user is new, then notificationFormat = 2, else null
  148.                         if (row.NotifyUser)
  149.                         {
  150.                             // <-- Start ofXRM Update -->
  151.                             XrmContext.UpdateObject(new contact()
  152.                             {
  153.                                 contactid = contact.ContactId,
  154.                                 qess_notifyuser = false
  155.                             });
  156.                             XrmContext.SaveChanges();
  157.                             // <-- End of XRM Update -->
  158.  
  159.                             if (contact.NotificationFormat == true)
  160.                             {
  161.                                 row.NotificationFormat = "2";
  162.                             }
  163.                             else
  164.                             {
  165.                                 row.NotificationFormat = "1";
  166.                             }
  167.                             row.Password = NEW_PASSWORD;
  168.                         }
  169.                         else
  170.                         {
  171.                             row.SetNotificationFormatNull();
  172.                         }
  173.  
  174.                         row.AccountDisabled = false;
  175.                         row.CreateLocalUser = false;
  176.                         row.IsNTDomainUser = false;
  177.                         row.IsChangePasswordNextLogon = false;
  178.                         row.IsWhosWho = false;
  179.                         row.IsWhosWhoAllowed = false;
  180.                         row.IsMailUsUser = false;
  181.                         row.IsChatUser = false;
  182.                         row.IsOnlinePayment = false;
  183.                         row.IsUserWallet = false;
  184.                         row.IsUserCompanyWallet = false;
  185.                         row.IsGuest = false;
  186.                         row.IsENewsNotification = false;
  187.                         row.IsENewsAllowed = false;
  188.                         row.IsFileBasketConfirmationMessageHidden = false;
  189.                         row.IsDelegateRestricted = false;
  190.                         row.IsContactOnly = false;
  191.                         // Add the row
  192.                         try
  193.                         {
  194.                             users.Rows.Add(row);
  195.                             Console.WriteLine("GenerateMDB: Added row for user " + row.UserID);
  196.                         }
  197.                         catch (Exception ex)
  198.                         {
  199.                             Console.WriteLine("GenerateMDB: Error adding row: " + ex.Message);
  200.                         }
  201.                     }
  202.  
  203.                     // Reinject the data
  204.                     Console.WriteLine("GenerateMDB: Update [Users]");
  205.  
  206.                     userAdapter.Update(users);
  207.                     Console.WriteLine("GenerateMDB: " + users.Count + " records written to [Users]");
  208.                 }
  209.             }
  210.         }
  211.  
  212.         private static void CreateCompaniesTable()
  213.         {
  214.             Console.WriteLine("GenerateMDB: Read [Companies]");
  215.  
  216.             using (GenericAccessDataSetTableAdapters.CompaniesTableAdapter companyAdapter = new GenericAccessDataSetTableAdapters.CompaniesTableAdapter())
  217.             {
  218.                 Console.WriteLine("GenerateMDB: Removing records from [Companies]");
  219.                 companyAdapter.Clear();
  220.  
  221.                 // Get the data from the DataTable
  222.                 using (GenericAccessDataSet.CompaniesDataTable companies = companyAdapter.GetData())
  223.                 {
  224.                     Console.WriteLine("GenerateMDB: Retrieve contacts from CRM");
  225.                     var contacts = (
  226.                         from c in XrmContext.contacts
  227.                         where
  228.                             // Extranet / eNews
  229.                             (c.qess_extranet == true || c.qess_enews == true)
  230.                             // Cannot belong to Essenscia company
  231.                             && c.parentcustomerid.Value != ESSENSCIA_GUID && c.parentcustomerid != null
  232.                             // Cannot be inactive / deceased / whatever reason to be inactive
  233.                             && c.statecode == "Active"
  234.                             && c.qess_logonname != null
  235.                             // Must be a member
  236.                             && c.qess_contacttype == 2
  237.                         select new
  238.                         {
  239.                             AccountName = c.parentcustomerid.Name,
  240.                             AccountIDContact = c.contact_customer_accounts.qess_idcontact,
  241.                             AccountStatuscode = c.contact_customer_accounts.statuscode,
  242.                             AccountID = c.parentcustomerid.Value,
  243.                             ContactStatuscode = c.statuscode.Value
  244.                         });
  245.  
  246.                     // Duplicate checking
  247.                     List<Guid> addedCompanies = new List<Guid>();
  248.  
  249.                     foreach (var c in contacts)
  250.                     {
  251.                         // Must be a member - this cannot be checked in the query somehow
  252.                         if (addedCompanies.Contains(c.AccountID)) continue;
  253.  
  254.                         GenericAccessDataSet.CompaniesRow row = companies.NewCompaniesRow();
  255.                         row.UserCompanyID = c.AccountIDContact;
  256.                         row.CompanyName = c.AccountName;
  257.                         row.IsUserCompanyWallet = false;
  258.  
  259.                         // Add the row
  260.                         try
  261.                         {
  262.                             companies.Rows.Add(row);
  263.                             addedCompanies.Add(c.AccountID);
  264.                             Console.WriteLine("GenerateMDB: Added row for company " + row.UserCompanyID);
  265.                         }
  266.                         catch (Exception ex)
  267.                         {
  268.                             Console.WriteLine("GenerateMDB: Error adding row: " + ex.Message);
  269.                         }
  270.                     }
  271.  
  272.                     // Reinject the data
  273.                     Console.WriteLine("GenerateMDB: Update [Companies]");
  274.  
  275.                     companyAdapter.Update(companies);
  276.                     Console.WriteLine("GenerateMDB: " + companies.Count + " records written to [Companies]");
  277.                 }
  278.             }
  279.         }
  280.  
  281.         private static void CreateUsersGroupsTable()
  282.         {
  283.             Console.WriteLine("GenerateMDB: Read [UserGroups]");
  284.  
  285.             using (GenericAccessDataSetTableAdapters.UsersGroupsTableAdapter userGroupAdapter = new GenericAccessDataSetTableAdapters.UsersGroupsTableAdapter())
  286.             {
  287.                 Console.WriteLine("GenerateMDB: Removing records from [UserGroups]");
  288.                 userGroupAdapter.Clear();
  289.  
  290.                 // Get the data from the DataTable
  291.                 using (GenericAccessDataSet.UsersGroupsDataTable userGroups = userGroupAdapter.GetData())
  292.                 {
  293.                     Console.WriteLine("GenerateMDB: Retrieve contacts from CRM (qess_extranet attribute)");
  294.  
  295.                     var contacts = (
  296.                         from c in XrmContext.contacts
  297.                         where
  298.                             // Extranet / eNews
  299.                             (c.qess_extranet == true || c.qess_enews == true)
  300.                             // Cannot belong to Essenscia company
  301.                             && c.parentcustomerid.Value != ESSENSCIA_GUID
  302.                             // Cannot be inactive / deceased / whatever reason to be inactive
  303.                             && c.statecode == "Active"
  304.                             && c.qess_logonname != null
  305.                         select new
  306.                         {
  307.                             ContactId = c.contactid,
  308.                             IDContact = c.qess_idcontact,
  309.                             Extranet = c.qess_extranet,
  310.                             ENews = c.qess_enews,
  311.                             ContactType = c.qess_contacttype
  312.                         });
  313.  
  314.                     // Debug variables
  315.                     int DB_contactsExtranet = 0;
  316.                     int DB_contactsWithoutCommissions = 0;
  317.                     int DB_contactsSingleSpecialCommission = 0;
  318.                     int DB_contactsSingleNotSpecialCommission = 0;
  319.                     int DB_contactsMultipleCommissionAtLeastOneSpecial = 0;
  320.                     int DB_contactsMultipleCommissionAllSpecial = 0;
  321.  
  322.                     foreach (var c in contacts)
  323.                     {
  324.                         if (c.ENews == true)
  325.                         {
  326.                             // Add a record for E-news Reader
  327.                             GenericAccessDataSet.UsersGroupsRow newsRow = userGroups.NewUsersGroupsRow();
  328.                             newsRow.UserID = c.IDContact;
  329.                             newsRow.GroupID = "E-news Read";
  330.  
  331.                             userGroups.Rows.Add(newsRow);
  332.                         }
  333.  
  334.                         // Get all associated comissions for this contact
  335.                         var boards = (from b in XrmContext.qbe_boards
  336.                                       where b.qbe_contactid == c.ContactId && b.qbe_commissionid != null
  337.                                       && b.statecode == "Active"
  338.                                       select new
  339.                                       {
  340.                                           BoardID = b.qbe_boardid,
  341.                                           CommissionID = b.qbe_commissionid,
  342.                                           SpecialCommission = b.qbe_commission_qbe_board.qess_specialgroup,
  343.                                           ExtranetCommission = b.qbe_commission_qbe_board.qess_extranet
  344.                                       });
  345.  
  346.                         // If contact is a member (type 2), create a group ID 1 row
  347.                         if (c.ContactType == 2)
  348.                         {
  349.                             GenericAccessDataSet.UsersGroupsRow oneRow = userGroups.NewUsersGroupsRow();
  350.                             oneRow.UserID = c.IDContact;
  351.                             oneRow.GroupID = "1";
  352.                             try
  353.                             {
  354.                                 userGroups.Rows.Add(oneRow);
  355.                                 Console.WriteLine("GenerateMDB: Added row for UserGroup " + oneRow.UserID + "/" + oneRow.GroupID);
  356.                             }
  357.                             catch (Exception ex)
  358.                             {
  359.                                 Console.WriteLine("GenerateMDB: Failed to add row to [UserGroups]: " + ex.Message);
  360.                             }
  361.                         }
  362.  
  363.                         // Create a record for every commission
  364.                         // If contact is NOT a member (type 2) and at least 1 commission is extranet = true
  365.                         // create row with group ID 1
  366.                         if (c.ContactType != 2 && boards.Count(b => b.ExtranetCommission == true) > 1)
  367.                         {
  368.                             GenericAccessDataSet.UsersGroupsRow oneRow = userGroups.NewUsersGroupsRow();
  369.                             oneRow.UserID = c.IDContact;
  370.                             oneRow.GroupID = "1";
  371.                             try
  372.                             {
  373.                                 userGroups.Rows.Add(oneRow);
  374.                                 Console.WriteLine("GenerateMDB: Added row for UserGroup " + oneRow.UserID + "/" + oneRow.GroupID);
  375.                             }
  376.                             catch (Exception ex)
  377.                             {
  378.                                 Console.WriteLine("GenerateMDB: Failed to add row to [UserGroups]: " + ex.Message);
  379.                             }
  380.                         }
  381.  
  382.                         // No extranet, continue (skip contact)
  383.                         if (c.Extranet != true) continue;
  384.  
  385.                         if (boards.Count() == 0)
  386.                         {
  387.                             DB_contactsWithoutCommissions = 0;
  388.                         }
  389.  
  390.                         // If contact only part of ONE commission, check "special" parameter
  391.                         // If it's true, continue (skip contact)
  392.                         if (boards.Count() == 1 && boards.First().SpecialCommission == true)
  393.                         {
  394.                             DB_contactsSingleSpecialCommission++;
  395.                             continue;
  396.                         }
  397.  
  398.                         if (boards.Count() == 1 && boards.First().SpecialCommission == false)
  399.                         {
  400.                             DB_contactsSingleNotSpecialCommission++;
  401.                         }
  402.  
  403.                         // There are more than 1 commissions, if these are ALL special, continue (skip contact)
  404.                         if (boards.Count() > 1 && boards.Count() == boards.Count(b => b.SpecialCommission == true))
  405.                         {
  406.                             DB_contactsMultipleCommissionAllSpecial++;
  407.                             continue;
  408.                         }
  409.  
  410.                         if (boards.Count() > 1 && boards.Count(b => b.SpecialCommission == true) > 0)
  411.                         {
  412.                             DB_contactsMultipleCommissionAtLeastOneSpecial++;
  413.                         }
  414.  
  415.                         // Create a record with Group ID 1
  416.                         GenericAccessDataSet.UsersGroupsRow groupOneRow = userGroups.NewUsersGroupsRow();
  417.                         groupOneRow.UserID = c.IDContact;
  418.                         groupOneRow.GroupID = "1";
  419.                         try
  420.                         {
  421.                             userGroups.Rows.Add(groupOneRow);
  422.                             Console.WriteLine("GenerateMDB: Added row for UserGroup " + groupOneRow.UserID + "/" + groupOneRow.GroupID);
  423.                         }
  424.                         catch (Exception ex)
  425.                         {
  426.                             Console.WriteLine("GenerateMDB: Failed to add row to [UserGroups]: " + ex.Message);
  427.                         }
  428.  
  429.                         foreach (var b in boards)
  430.                         {
  431.                             var commission = (from comm in XrmContext.qbe_commissions
  432.                                               where comm.qbe_commissionid == b.CommissionID.Value &&
  433.                                               (comm.qess_specialgroup == true || comm.qess_extranet == true) &&
  434.                                               comm.statecode == "Active"
  435.                                               select new
  436.                                               {
  437.                                                   IDContact = comm.qess_idcontact,
  438.                                                   Statecode = comm.statecode
  439.                                               }).First();
  440.                             if (commission == null || commission.Statecode == "Active") continue;
  441.  
  442.                             GenericAccessDataSet.UsersGroupsRow row = userGroups.NewUsersGroupsRow();
  443.                             row.UserID = c.IDContact;
  444.                             row.GroupID = commission.IDContact;
  445.  
  446.                             try
  447.                             {
  448.                                 userGroups.Rows.Add(row);
  449.                                 Console.WriteLine("GenerateMDB: Added row for UserGroup " + row.UserID + "/" + row.GroupID);
  450.                             }
  451.                             catch (Exception ex)
  452.                             {
  453.                                 Console.WriteLine("GenerateMDB: Failed to add row to [UserGroups]: " + ex.Message);
  454.                             }
  455.                         }
  456.  
  457.                         DB_contactsExtranet++;
  458.  
  459.                     }
  460.  
  461.                     // Reinject the data
  462.                     Console.WriteLine("GenerateMDB: Update [UserGroups]");
  463.  
  464.                     userGroupAdapter.Update(userGroups);
  465.                     Console.WriteLine("GenerateMDB: " + userGroups.Count + " records written to [UserGroups]");
  466.                 }
  467.             }
  468.         }
  469.  
  470.         private static void CreateUserCompaniesTable()
  471.         {
  472.             Console.WriteLine("GenerateMDB: Read [UserCompanies]");
  473.  
  474.             using (GenericAccessDataSetTableAdapters.UsersCompaniesTableAdapter userCompanyAdapter = new GenericAccessDataSetTableAdapters.UsersCompaniesTableAdapter())
  475.             {
  476.                 Console.WriteLine("GenerateMDB: Removing records from [UserCompanies]");
  477.                 userCompanyAdapter.Clear();
  478.  
  479.                 // Get the data from the DataTable
  480.                 using (GenericAccessDataSet.UsersCompaniesDataTable userCompanies = userCompanyAdapter.GetData())
  481.                 {
  482.                     Console.WriteLine("GenerateMDB: Retrieve contacts from CRM");
  483.  
  484.                     var contacts = (
  485.                         from c in XrmContext.contacts
  486.                         where
  487.                             // Extranet / eNews
  488.                             (c.qess_extranet == true || c.qess_enews == true)
  489.                             // Cannot belong to Essenscia company
  490.                             && c.parentcustomerid.Value != ESSENSCIA_GUID
  491.                             // Cannot be inactive / deceased / whatever reason to be inactive
  492.                             && c.statecode == "Active"
  493.                             && c.qess_logonname != null
  494.                             && c.parentcustomerid != null
  495.                             // Must be a member
  496.                             && c.qess_contacttype == 2
  497.                         select new
  498.                         {
  499.                             IDContact = c.qess_idcontact,
  500.                             AccountIDContact = c.contact_customer_accounts.qess_idcontact
  501.                         });
  502.  
  503.                     foreach (var c in contacts)
  504.                     {
  505.                         GenericAccessDataSet.UsersCompaniesRow row = userCompanies.NewUsersCompaniesRow();
  506.                         row.UserID = c.IDContact;
  507.                         row.UserCompanyID = c.AccountIDContact;
  508.                         row.IsMainCompany = true;
  509.  
  510.                         try
  511.                         {
  512.                             userCompanies.Rows.Add(row);
  513.                             Console.WriteLine("GenerateMDB: Added row for UserCompany " + row.UserID + "/" + row.UserCompanyID);
  514.                         }
  515.                         catch (Exception ex)
  516.                         {
  517.                             // Probably the same combination happened
  518.                             Console.WriteLine("GenerateMDB: Failed to add row to [UserCompanies]: " + ex.Message);
  519.                         }
  520.                     }
  521.  
  522.                     // Reinject the data
  523.                     Console.WriteLine("GenerateMDB: Update [UserCompanies]");
  524.  
  525.                     userCompanyAdapter.Update(userCompanies);
  526.                     Console.WriteLine("GenerateMDB: " + userCompanies.Count + " records written to [UserCompanies]");
  527.                 }
  528.             }
  529.         }
  530.  
  531.         private static void CreateGroupsTable()
  532.         {
  533.             Console.WriteLine("GenerateMDB: Read [Groups]");
  534.  
  535.             using (GenericAccessDataSetTableAdapters.GroupsTableAdapter groupAdapter = new GenericAccessDataSetTableAdapters.GroupsTableAdapter())
  536.             {
  537.                 Console.WriteLine("GenerateMDB: Removing records from [Groups]");
  538.                 groupAdapter.Clear();
  539.  
  540.                 // Get the data from the DataTable
  541.                 using (GenericAccessDataSet.GroupsDataTable groups = groupAdapter.GetData())
  542.                 {
  543.                     Console.WriteLine("GenerateMDB: Retrieve contacts from CRM");
  544.  
  545.                     var commissions = (from c in XrmContext.qbe_commissions
  546.                                        where
  547.                                            (c.qess_extranet == true || c.qess_specialgroup == true)
  548.                                            && c.statecode == "Active"
  549.                                        select new
  550.                                        {
  551.                                            IDContact = c.qess_idcontact,
  552.                                            Name = c.qbe_name,
  553.                                            Statecode = c.statecode
  554.                                        });
  555.  
  556.                     foreach (var c in commissions)
  557.                     {
  558.                        
  559.                         GenericAccessDataSet.GroupsRow row = groups.NewGroupsRow();
  560.                         row.GroupID = c.IDContact;
  561.                         row.GroupCode = c.IDContact;
  562.                         row.GroupName = c.Name;
  563.                         row.SiteID = "1";
  564.                         row.AuthorGroup = false;
  565.  
  566.                         try
  567.                         {
  568.                             groups.Rows.Add(row);
  569.                             Console.WriteLine("GenerateMDB: Added row for Group " + row.GroupID);
  570.                         }
  571.                         catch (Exception ex)
  572.                         {
  573.                             // Probably the same combination happened
  574.                             Console.WriteLine("GenerateMDB: Failed to add row to [Groups]: " + ex.Message);
  575.                         }
  576.                     }
  577.  
  578.                     // Reinject the data
  579.                     Console.WriteLine("GenerateMDB: Update [Groups]");
  580.  
  581.                     groupAdapter.Update(groups);
  582.                     Console.WriteLine("GenerateMDB: " + groups.Count + " records written to [Groups]");
  583.                 }
  584.             }
  585.         }
  586.  
  587.         private static void UpdateDisappearedUsers()
  588.         {
  589.             Console.WriteLine("GenerateMDB: Find disappeared contacts");
  590.  
  591.             var contacts = (from c in XrmContext.contacts
  592.                             where c.qess_disappeareduser == true
  593.                             select new contact()
  594.                             {
  595.                                 contactid = c.contactid,
  596.                                 qess_disappeareduser = c.qess_disappeareduser
  597.                             });
  598.  
  599.             int updates = 0;
  600.             foreach (var c in contacts)
  601.             {
  602.                 c.qess_disappeareduser = false;
  603.                 XrmContext.UpdateObject(c);
  604.                 XrmContext.SaveChanges();
  605.                 updates++;
  606.             }
  607.  
  608.             Console.WriteLine("GenerateMDB: " + updates + " disappeared contacts updated");
  609.         }
  610.  
  611.         private static void SaveFile(string outputPath)
  612.         {
  613.             File.Copy(Environment.CurrentDirectory + "\\GenericAccess.mdb", outputPath + "\\GenericAccess.mdb");
  614.             Console.WriteLine("GenerateMDB: File saved");
  615.         }
  616.  
  617.         // Equality comparer used to compare boards by the extranet parameter
  618.         class BoardComparerByExtranet : IEqualityComparer<qbe_board>
  619.         {
  620.             #region IEqualityComparer<qbe_commission> Members
  621.  
  622.             public bool Equals(qbe_board x, qbe_board y)
  623.             {
  624.                 return (x.qbe_commission_qbe_board.qess_extranet.HasValue && y.qbe_commission_qbe_board.qess_extranet.HasValue && x.qbe_commission_qbe_board.qess_extranet.Value == y.qbe_commission_qbe_board.qess_extranet.Value);
  625.             }
  626.  
  627.             public int GetHashCode(qbe_board obj)
  628.             {
  629.                 return obj.qbe_commission_qbe_board.qess_extranet.GetHashCode();
  630.             }
  631.  
  632.             #endregion
  633.         }
  634.     }
  635. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement