Exemple #1
0
 protected virtual bool WriteResultsToReport <T>(T entity, string fileName, string filePath = "", string fileFormat = "")
 {
     fileFormat = this.ProcessReportsFileFormat(fileFormat);
     filePath   = this.ProcessReportsFilePathForWriters(filePath, fileName, fileFormat);
     if (fileFormat == _settings.FILE_FORMAT_JSON)
     {
         var converter = new BoxJsonConverter();
         File.WriteAllText(filePath, converter.Serialize <T>(entity));
         return(true);
     }
     else if (fileFormat == _settings.FILE_FORMAT_CSV)
     {
         try
         {
             using (StreamWriter fs = new StreamWriter(File.Open(filePath, FileMode.OpenOrCreate), System.Text.Encoding.UTF8))
                 using (var csv = new CsvWriter(fs))
                 {
                     csv.WriteRecord(entity);
                 }
             return(true);
         }
         catch
         {
             return(false);
         }
     }
     else
     {
         throw new Exception($"File format {fileFormat} is not currently supported.");
     }
 }
Exemple #2
0
 protected virtual bool WriteListResultsToReport <T, M>(List <T> entity, string fileName, string filePath = "", string fileFormat = "")
     where T : BoxEntity, new()
 {
     fileFormat = this.ProcessReportsFileFormat(fileFormat);
     filePath   = this.ProcessReportsFilePathForWriters(filePath, fileName, fileFormat);
     if (fileFormat == _settings.FILE_FORMAT_JSON)
     {
         try
         {
             var converter  = new BoxJsonConverter();
             var collection = new BoxCollection <T>();
             collection.Entries = new List <T>();
             collection.Entries.AddRange(entity);
             collection.TotalCount = entity.Count();
             File.WriteAllText(filePath, converter.Serialize <BoxCollection <T> >(collection));
             return(true);
         }
         catch (Exception e)
         {
             Reporter.WriteError(e.Message);
             return(false);
         }
     }
     else if (fileFormat == _settings.FILE_FORMAT_CSV)
     {
         try
         {
             using (StreamWriter fs = new StreamWriter(File.Open(filePath, FileMode.OpenOrCreate), System.Text.Encoding.UTF8))
                 using (var csv = new CsvWriter(fs))
                 {
                     csv.Configuration.RegisterClassMap(typeof(M));
                     csv.WriteRecords(entity);
                 }
             return(true);
         }
         catch (Exception e)
         {
             Reporter.WriteError(e.Message);
             return(false);
         }
     }
     else
     {
         throw new Exception($"File format {fileFormat} is not currently supported.");
     }
 }
Exemple #3
0
 protected virtual bool WriteMetadataCollectionResultsToReport(List <BoxMetadataForCsv> entity, string fileName, string filePath = "", string fileFormat = "")
 {
     fileFormat = this.ProcessReportsFileFormat(fileFormat);
     filePath   = this.ProcessReportsFilePathForWriters(filePath, fileName, fileFormat);
     if (fileFormat.ToLower() == this._settings.FILE_FORMAT_JSON)
     {
         try
         {
             var converter = new BoxJsonConverter();
             File.WriteAllText(filePath, converter.Serialize <List <BoxMetadataForCsv> >(entity));
             return(true);
         }
         catch (Exception e)
         {
             Reporter.WriteError(e.Message);
             return(false);
         }
     }
     else if (fileFormat.ToLower() == this._settings.FILE_FORMAT_CSV)
     {
         try
         {
             using (StreamWriter fs = new StreamWriter(File.Open(filePath, FileMode.OpenOrCreate), System.Text.Encoding.UTF8))
                 using (var csv = new CsvWriter(fs))
                 {
                     csv.Configuration.RegisterClassMap(typeof(BoxMetadataMap));
                     csv.WriteRecords(entity);
                 }
             return(true);
         }
         catch (Exception e)
         {
             Reporter.WriteError(e.Message);
             return(false);
         }
     }
     else
     {
         throw new Exception($"File format {fileFormat} is not currently supported.");
     }
 }
        public static async Task Run([TimerTrigger("%TimerScheduleExpression%")] TimerInfo myTimer, TraceWriter log, ExecutionContext context)
        {
            var config = GetConfiguration(context);

            //Box setup
            var box = GetBoxAdminClient(config);

            //Azure Table setup
            var table = await SetupTable(config);

            string nextStreamPosition = null;

            if (resetNextStreamPosition_DebugOnly)
            {
                log.Warning("Resetting Table to re-initiate stream position lookback");

                var nextStreamPositionEntityToDelete = await RetrieveNextStreamPosition(table, config);

                if (nextStreamPositionEntityToDelete != null)
                {
                    var deleteOperation = TableOperation.Delete(nextStreamPositionEntityToDelete);
                    await table.ExecuteAsync(deleteOperation);

                    log.Info("Deleted existing BoxStreamPositionEntity");
                }
                else
                {
                    log.Info("Could not retrieve existing BoxStreamPositionEntity");
                }

                resetNextStreamPosition_DebugOnly = false;
            }

            var nextStreamPositionEntity = await RetrieveNextStreamPosition(table, config);

            if (nextStreamPositionEntity == null)
            {
                log.Warning("NextStreamPosition not found in Table; Performing lookback to establish stream position");
                var startDate = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(INITIAL_LOOKBACK_MINUTES));
                var events    = await box.EventsManager.EnterpriseEventsAsync(createdAfter : startDate, createdBefore : null);

                if (events.NextStreamPosition == "0") //this means that no events occurred in the lookback period; you need to generate an event in Box that will be picked up during the next attempt
                {
                    log.Warning("NextStreamPosition was returned as '0'.  Sleeping until next invocation.  Generate at least one event in Box to start fetching via stream position.  Note that events from Box are delayed by up to one minute.");
                    return;
                }
                else
                {
                    events.Entries.ForEach(e => log.Info($"Box Event Received (Type='{e.EventType}', Id={e.EventId})"));
                    nextStreamPosition = events.NextStreamPosition;

                    //create initial stream position in Table
                    StoreNextStreamPosition(nextStreamPosition, table, config, log);
                }
            }
            else
            {
                nextStreamPosition = nextStreamPositionEntity.NextStreamPosition;
                log.Info($"NextStreamPosition found in Table: '{nextStreamPosition}'");
            }

            //Service Bus setup
            //https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-how-to-use-topics-subscriptions
            var          serviceBusConnectionString = config[SERVICE_BUS_CONNECTION_STRING_KEY];
            var          topicName   = config[SERVICE_BUS_TOPIC_NAME_KEY];
            ITopicClient topicClient = new TopicClient(serviceBusConnectionString, topicName);

            var currFetch = 0;

            while (currFetch++ < 4)
            {
                log.Info($"Fetching batch {currFetch} of events using stream position '{nextStreamPosition}'");

                var events = await box.EventsManager.EnterpriseEventsAsync(streamPosition : nextStreamPosition);

                nextStreamPosition = events.NextStreamPosition;

                if (events.Entries.Count == 0)
                {
                    //Store stream position even though no events were returned
                    StoreNextStreamPosition(nextStreamPosition, table, config, log);

                    log.Info("No more event entries found. Exiting this invocation.");
                    break;
                }

                try
                {
                    BoxJsonConverter bjc = new BoxJsonConverter();
                    foreach (var e in events.Entries)
                    {
                        log.Info($"Box Event Received (Type='{e.EventType}', Id={e.EventId})");
                        var eventJson = bjc.Serialize <BoxEnterpriseEvent>(e);
                        var messageId = e.EventId; //this is the value that is used by Service Bus for deduplication
                        var message   = new Message(Encoding.UTF8.GetBytes(eventJson))
                        {
                            ContentType = "application/json",
                            MessageId   = messageId,
                        };

                        await topicClient.SendAsync(message);

                        log.Info($"Sent message with Id '{messageId}' to Service Bus topic '{topicName}'");
                    }

                    //Successful propogation of all events to Service Bus. Store the stream position in Table
                    StoreNextStreamPosition(nextStreamPosition, table, config, log);
                }
                catch (Exception ex)
                {
                    log.Error($"Error while propogating events to Service Bus.  Error Message: {ex.Message}");
                    break;
                }
            }

            await topicClient.CloseAsync();
        }
Exemple #5
0
 protected virtual bool WriteMetadataTemplateCollectionResultsToReport(List <BoxMetadataTemplate> entity,
                                                                       string fileNameTemplate, string fileNameFields, string filePathTemplate = "", string filePathFields = "", string fileFormat = "")
 {
     fileFormat       = this.ProcessReportsFileFormat(fileFormat);
     filePathTemplate = this.ProcessReportsFilePathForWriters(filePathTemplate, fileNameTemplate, fileFormat);
     filePathFields   = this.ProcessReportsFilePathForWriters(filePathFields, fileNameFields, fileFormat);
     if (fileFormat.ToLower() == this._settings.FILE_FORMAT_JSON)
     {
         try
         {
             var converter = new BoxJsonConverter();
             File.WriteAllText(filePathTemplate, converter.Serialize <List <BoxMetadataTemplate> >(entity));
             return(true);
         }
         catch (Exception e)
         {
             Reporter.WriteError(e.Message);
             return(false);
         }
     }
     else if (fileFormat.ToLower() == this._settings.FILE_FORMAT_CSV)
     {
         try
         {
             var fields = new List <BoxMetadataTemplateFieldForCsv>();
             foreach (var template in entity)
             {
                 if (template.Fields != null && template.Fields.Count > 0)
                 {
                     foreach (var field in template.Fields)
                     {
                         var optionsList = new List <string>();
                         if (field.Options != null && field.Options.Count > 0)
                         {
                             foreach (var option in field.Options)
                             {
                                 optionsList.Add(option.Key);
                             }
                         }
                         var fieldForCsv = new BoxMetadataTemplateFieldForCsv();
                         fieldForCsv.DisplayName    = field.DisplayName;
                         fieldForCsv.Hidden         = field.Hidden;
                         fieldForCsv.Key            = field.Key;
                         fieldForCsv.TemplateKey    = template.TemplateKey;
                         fieldForCsv.OptionsFromCsv = optionsList;
                         fieldForCsv.Type           = field.Type;
                         fields.Add(fieldForCsv);
                     }
                 }
             }
             using (StreamWriter fs = new StreamWriter(File.Open(filePathTemplate, FileMode.OpenOrCreate), System.Text.Encoding.UTF8))
                 using (var csv = new CsvWriter(fs))
                 {
                     csv.Configuration.RegisterClassMap(typeof(BoxMetadataTemplateMap));
                     csv.WriteRecords(entity);
                 }
             using (StreamWriter fs = new StreamWriter(File.Open(filePathFields, FileMode.OpenOrCreate), System.Text.Encoding.UTF8))
                 using (var csv = new CsvWriter(fs))
                 {
                     csv.Configuration.RegisterClassMap(typeof(BoxMetadataTemplateFieldMap));
                     csv.WriteRecords(fields);
                 }
             return(true);
         }
         catch (Exception e)
         {
             Reporter.WriteError(e.Message);
             return(false);
         }
     }
     else
     {
         throw new Exception($"File format {fileFormat} is not currently supported.");
     }
 }
Exemple #6
0
        protected virtual void OutputJson <T>(T entity)
        {
            var converter = new BoxJsonConverter();

            Reporter.WriteInformation(JValue.Parse(converter.Serialize <T>(entity)).ToString(Formatting.Indented));
        }