private static async Task UploadItemsFromCsvAsync(string filePath)
        {
            if (_currentConnection == null)
            {
                Output.WriteLine(Output.Warning, "No connection selected. Please create a new connection or select an existing connection.");
                return;
            }

            var records = CsvDataLoader.LoadDataFromCsv(filePath);

            foreach (var part in records)
            {
                var newItem = new ExternalItem
                {
                    Id      = part.PartNumber.ToString(),
                    Content = new ExternalItemContent
                    {
                        // Need to set to null, service returns 400
                        // if @odata.type property is sent
                        ODataType = null,
                        Type      = ExternalItemContentType.Text,
                        Value     = part.Description
                    },
                    Acl = new List <Acl>
                    {
                        new Acl {
                            AccessType     = AccessType.Grant,
                            Type           = AclType.Everyone,
                            Value          = _tenantId,
                            IdentitySource = "Azure Active Directory"
                        }
                    },
                    Properties = part.AsExternalItemProperties()
                };

                try
                {
                    Output.Write(Output.Info, $"Uploading part number {part.PartNumber}...");
                    await _graphHelper.AddOrUpdateItem(_currentConnection.Id, newItem);

                    Output.WriteLine(Output.Success, "DONE");
                }
                catch (ServiceException serviceException)
                {
                    Output.WriteLine(Output.Error, "FAILED");
                    Output.WriteLine(Output.Error, $"{serviceException.StatusCode} error adding or updating part {part.PartNumber}");
                    Output.WriteLine(Output.Error, serviceException.Message);
                }
            }
        }
Example #2
0
        private static async Task UploadItemsFromCsvAsync(string filePath)
        {
            var records = CsvDataLoader.LoadDataFromCsv(filePath);

            foreach (var part in records)
            {
                var newItem = new ExternalItem
                {
                    Id      = part.RowID.ToString(),
                    Content = new PayloadContent
                    {
                        Value = "<html>" + part.FileName + "</html>"
                    },
                    Acl = new List <Acl>
                    {
                        new Acl {
                            AccessType = "grant",
                            Type       = "everyone",
                            Value      = _tenantId
                        }
                    },
                    Properties = part
                };

                try
                {
                    Output.Write(Output.Info, $"Uploading part number {part.RowID}...");
                    await _graphHelper.AddOrUpdateItem(_currentConnection.Id, newItem);

                    Output.WriteLine(Output.Success, "DONE");
                }
                catch (ServiceException serviceException)
                {
                    Output.WriteLine(Output.Error, "FAILED");
                    Output.WriteLine(Output.Error, $"{serviceException.StatusCode} error adding or updating part {part.RowID}");
                    Output.WriteLine(Output.Error, serviceException.Message);
                }
            }
        }
Example #3
0
        private static async Task UpdateItemsFromDatabase(bool uploadModifiedOnly)
        {
            if (_currentConnection == null)
            {
                Output.WriteLine(Output.Warning, "No connection selected. Please create a new connection or select an existing connection.");
                return;
            }

            List <AppliancePart> partsToUpload = null;
            List <AppliancePart> partsToDelete = null;

            var newUploadTime = DateTime.UtcNow;

            using (var db = new ApplianceDbContext())
            {
                if (uploadModifiedOnly)
                {
                    // Load the last upload timestamp
                    var lastUploadTime = GetLastUploadTime();
                    Output.WriteLine(Output.Info, $"Uploading changes since last upload at {lastUploadTime.ToLocalTime().ToString()}");

                    partsToUpload = db.Parts
                                    .Where(p => EF.Property <DateTime>(p, "LastUpdated") > lastUploadTime)
                                    .ToList();

                    partsToDelete = db.Parts
                                    .IgnoreQueryFilters() // Normally items with IsDeleted = 1 aren't included in queries
                                    .Where(p => (EF.Property <bool>(p, "IsDeleted") && EF.Property <DateTime>(p, "LastUpdated") > lastUploadTime))
                                    .ToList();
                }
                else
                {
                    partsToUpload = db.Parts
                                    .ToList();

                    partsToDelete = db.Parts
                                    .IgnoreQueryFilters() // Normally items with IsDeleted = 1 aren't included in queries
                                    .Where(p => EF.Property <bool>(p, "IsDeleted"))
                                    .ToList();
                }
            }

            Output.WriteLine(Output.Info, $"Processing {partsToUpload.Count()} add/updates, {partsToDelete.Count()} deletes");
            bool success = true;

            foreach (var part in partsToUpload)
            {
                var newItem = new ExternalItem
                {
                    Id      = part.PartNumber.ToString(),
                    Content = new ExternalItemContent
                    {
                        // Need to set to null, service returns 400
                        // if @odata.type property is sent
                        ODataType = null,
                        Type      = ExternalItemContentType.Text,
                        Value     = part.Description
                    },
                    Acl = new List <Acl>
                    {
                        new Acl {
                            AccessType     = AccessType.Grant,
                            Type           = AclType.Everyone,
                            Value          = _tenantId,
                            IdentitySource = "Azure Active Directory"
                        }
                    },
                    Properties = part.AsExternalItemProperties()
                };

                try
                {
                    Output.Write(Output.Info, $"Uploading part number {part.PartNumber}...");
                    await _graphHelper.AddOrUpdateItem(_currentConnection.Id, newItem);

                    Output.WriteLine(Output.Success, "DONE");
                }
                catch (ServiceException serviceException)
                {
                    success = false;
                    Output.WriteLine(Output.Error, "FAILED");
                    Output.WriteLine(Output.Error, $"{serviceException.StatusCode} error adding or updating part {part.PartNumber}");
                    Output.WriteLine(Output.Error, serviceException.Message);
                }
            }

            foreach (var part in partsToDelete)
            {
                try
                {
                    Output.Write(Output.Info, $"Deleting part number {part.PartNumber}...");
                    await _graphHelper.DeleteItem(_currentConnection.Id, part.PartNumber.ToString());

                    Output.WriteLine(Output.Success, "DONE");
                }
                catch (ServiceException serviceException)
                {
                    if (serviceException.StatusCode.Equals(System.Net.HttpStatusCode.NotFound))
                    {
                        Output.WriteLine(Output.Warning, "Not found");
                    }
                    else
                    {
                        success = false;
                        Output.WriteLine(Output.Error, "FAILED");
                        Output.WriteLine(Output.Error, $"{serviceException.StatusCode} error deleting part {part.PartNumber}");
                        Output.WriteLine(Output.Error, serviceException.Message);
                    }
                }
            }

            // If no errors, update our last upload time
            if (success)
            {
                SaveLastUploadTime(newUploadTime);
            }
        }
Example #4
0
        private static async Task UpdateItems(bool uploadModifiedOnly)
        {
            if (_currentConnection == null)
            {
                Output.WriteLine(Output.Warning, "No connection selected. Please create a new connection or select an existing connection.");
                return;
            }

            List <AADGroup> groupsToUpload = null;
            List <AADGroup> groupsToDelete = null;

            var newUploadTime = DateTime.UtcNow;

            // Get all AAD Groups
            var groups = await _graphHelper.GetAllGroups();

            if (uploadModifiedOnly)
            {
                // Load the last upload timestamp
                var lastUploadTime = GetLastUploadTime();
                Output.WriteLine(Output.Info, $"Uploading changes since last upload at {lastUploadTime.ToLocalTime().ToString()}");

                groupsToUpload = groups
                                 .Where(p => p.DeletedDateTime == DateTime.MinValue && p.CreatedDateTime > lastUploadTime)
                                 .ToList();

                groupsToDelete = groups
                                 .Where(p => p.DeletedDateTime != DateTime.MinValue && p.DeletedDateTime > lastUploadTime)
                                 .ToList();
            }
            else
            {
                groupsToUpload = groups
                                 .Where(p => p.DeletedDateTime == DateTime.MinValue)
                                 .ToList();

                groupsToDelete = new List <AADGroup>();
            }

            Output.WriteLine(Output.Info, $"Processing {groupsToUpload.Count()} add/updates, {groupsToDelete.Count()} deletes");
            bool success = true;

            foreach (var group in groupsToUpload)
            {
                var newItem = new ExternalItem
                {
                    Id      = group.Id.ToString(),
                    Content = new ExternalItemContent
                    {
                        // Need to set to null, service returns 400
                        // if @odata.type property is sent
                        ODataType = null,
                        Type      = ExternalItemContentType.Text,
                        Value     = group.Description
                    },
                    Acl = new List <Acl>
                    {
                        new Acl {
                            AccessType     = AccessType.Grant,
                            Type           = AclType.Group,
                            Value          = group.Id,
                            IdentitySource = "Azure Active Directory"
                        }
                    },
                    Properties = group.AsExternalItemProperties()
                };

                try
                {
                    Output.Write(Output.Info, $"Uploading group {group.DisplayName} ({group.Id})...");
                    await _graphHelper.AddOrUpdateItem(_currentConnection.Id, newItem);

                    Output.WriteLine(Output.Success, "DONE");
                }
                catch (ServiceException serviceException)
                {
                    success = false;
                    Output.WriteLine(Output.Error, "FAILED");
                    Output.WriteLine(Output.Error, $"{serviceException.StatusCode} error adding or updating group {group.Id}");
                    Output.WriteLine(Output.Error, serviceException.Message);
                }
            }

            foreach (var part in groupsToDelete)
            {
                try
                {
                    Output.Write(Output.Info, $"Deleting part number {part.Id}...");
                    await _graphHelper.DeleteItem(_currentConnection.Id, part.Id.ToString());

                    Output.WriteLine(Output.Success, "DONE");
                }
                catch (ServiceException serviceException)
                {
                    if (serviceException.StatusCode.Equals(System.Net.HttpStatusCode.NotFound))
                    {
                        Output.WriteLine(Output.Warning, "Not found");
                    }
                    else
                    {
                        success = false;
                        Output.WriteLine(Output.Error, "FAILED");
                        Output.WriteLine(Output.Error, $"{serviceException.StatusCode} error deleting part {part.Id}");
                        Output.WriteLine(Output.Error, serviceException.Message);
                    }
                }
            }

            // If no errors, update our last upload time
            if (success)
            {
                SaveLastUploadTime(newUploadTime);
            }
        }
Example #5
0
        private static async Task UpdateItemsFromDatabase(bool uploadModifiedOnly)
        {
            if (_currentConnection == null)
            {
                Output.WriteLine(Output.Warning, "No connection selected. Please create a new connection or select an existing connection.");
                return;
            }

            List <AppliancePart> partsToUpload = CsvDataLoader.LoadPartsFromCsv("ApplianceParts.csv");
            List <AppliancePart> partsToDelete = new List <AppliancePart>();

            var newUploadTime = DateTime.UtcNow;

            Output.WriteLine(Output.Info, $"Processing {partsToUpload.Count()} add/updates, {partsToDelete.Count()} deletes");
            bool success = true;

            foreach (var part in partsToUpload)
            {
                var newItem = new ExternalItem
                {
                    Id      = part.Id.ToString(),
                    Content = new ExternalItemContent
                    {
                        // Need to set to null, service returns 400
                        // if @odata.type property is sent
                        ODataType = null,
                        Type      = ExternalItemContentType.Text,
                        Value     = part.Text
                    },
                    Acl = new List <Acl>
                    {
                        new Acl {
                            AccessType     = AccessType.Grant,
                            Type           = AclType.Everyone,
                            Value          = _tenantId,
                            IdentitySource = "Azure Active Directory"
                        }
                    },
                    Properties = part.AsExternalItemProperties()
                };

                try
                {
                    Output.Write(Output.Info, $"Uploading part number {part.Id}...");
                    await _graphHelper.AddOrUpdateItem(_currentConnection.Id, newItem);

                    Output.WriteLine(Output.Success, "DONE");
                }
                catch (ServiceException serviceException)
                {
                    success = false;
                    Output.WriteLine(Output.Error, "FAILED");
                    Output.WriteLine(Output.Error, $"{serviceException.StatusCode} error adding or updating part {part.Id}");
                    Output.WriteLine(Output.Error, serviceException.Message);
                }
            }

            foreach (var part in partsToDelete)
            {
                try
                {
                    Output.Write(Output.Info, $"Deleting part number {part.Id}...");
                    await _graphHelper.DeleteItem(_currentConnection.Id, part.Id.ToString());

                    Output.WriteLine(Output.Success, "DONE");
                }
                catch (ServiceException serviceException)
                {
                    if (serviceException.StatusCode.Equals(System.Net.HttpStatusCode.NotFound))
                    {
                        Output.WriteLine(Output.Warning, "Not found");
                    }
                    else
                    {
                        success = false;
                        Output.WriteLine(Output.Error, "FAILED");
                        Output.WriteLine(Output.Error, $"{serviceException.StatusCode} error deleting part {part.Id}");
                        Output.WriteLine(Output.Error, serviceException.Message);
                    }
                }
            }

            // If no errors, update our last upload time
            if (success)
            {
                SaveLastUploadTime(newUploadTime);
            }
        }