private void CloseSafely(IDataSource <T> dataSource)
 {
     if (dataSource != default(IDataSource <T>))
     {
         dataSource.Close();
     }
 }
Example #2
0
 /// <summary>
 /// Called whenever an error occurs inside of the pipeline.
 ///
 /// <para />No further results will be produced after this
 /// method is called.
 ///
 /// <para />The throwable resulting from the failure can be
 /// obtained using <code>dataSource.GetFailureCause</code>.
 ///
 /// <param name="dataSource"></param>
 /// </summary>
 public void OnFailure(IDataSource <T> dataSource)
 {
     try
     {
         OnFailureImpl(dataSource);
     }
     finally
     {
         dataSource.Close();
     }
 }
 /// <summary>
 /// Called whenever a new value is ready to be retrieved from
 /// the IDataSource.
 ///
 /// <para />To retrieve the new value, call
 /// <code>dataSource.GetResult()</code>.
 ///
 /// <para />To determine if the new value is the last, use
 /// <code>dataSource.IsFinished</code>.
 /// </summary>
 /// <param name="dataSource">The data source.</param>
 public async Task OnNewResult(IDataSource <bool> dataSource)
 {
     try
     {
         await OnNewResultImpl(dataSource.GetResult()).ConfigureAwait(false);
     }
     finally
     {
         dataSource.Close();
     }
 }
 /// <summary>
 /// Verifies that the underlying data sources get closed when data source provided by
 /// our _dataSourceSupplier gets closed.
 /// </summary>
 protected void TestClose(
     IDataSource <object> dataSource,
     params IDataSource <object>[] underlyingDataSources)
 {
     dataSource.Close();
     if (underlyingDataSources != null)
     {
         foreach (var underlyingDataSource in underlyingDataSources)
         {
             ((MockAbstractDataSource <object>)underlyingDataSource).VerifyMethodInvocation(
                 "Close", 1);
         }
     }
 }
Example #5
0
        /// <summary>
        /// Called whenever a new value is ready to be retrieved
        /// from the IDataSource.
        ///
        /// <para />To retrieve the new value, call
        /// <code>dataSource.GetResult()</code>.
        ///
        /// <para />To determine if the new value is the last, use
        /// <code>dataSource.IsFinished</code>.
        /// </summary>
        /// <param name="dataSource">The data source.</param>
        public async Task OnNewResult(IDataSource <T> dataSource)
        {
            // IsFinished should be checked before calling OnNewResultImpl(),
            // otherwise there would be a race condition: the final data
            // source result might be ready before we call IsFinished here,
            // which would lead to the loss of the final result (because of
            // an early dataSource.Dispose() call).
            bool shouldClose = dataSource.IsFinished();

            try
            {
                await OnNewResultImpl(dataSource).ConfigureAwait(false);
            }
            finally
            {
                if (shouldClose)
                {
                    dataSource.Close();
                }
            }
        }
Example #6
0
        /**************************************************/
        #endregion
        /**************************************************/

        /**************************************************/
        #region SpreadsheetSelect
        /**************************************************/

        private void bSelectSpreadsheet_Click(object sender, EventArgs e)
        {
            // Select File
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            openFileDialog.Filter           = "Spreadsheet|*.xlsx;*.xlsm;*.odc";
            openFileDialog.FilterIndex      = 1;
            openFileDialog.Multiselect      = false;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                // DataSource
                if (dataSource != null)
                {
                    dataSource.Close();
                }
                ResetDataSource();
                ResetPDF();
                dataSource = new Spreadsheet();

                OpenSpreadsheet(openFileDialog.FileName);
            }
        }
Example #7
0
 private void TestClose(Exception throwable)
 {
     _dataSource.Close();
     VerifyClosed(FINISHED, throwable);
 }
Example #8
0
        public ResultBase Execute(CommandBase command)
        {
            data.Open();
            ResultBase output = null;

            switch (command)
            {
            case CreateRecordCommand _:
            {
                CreateRecordCommand createCommand    = (CreateRecordCommand)command;
                RecordType          type             = data.GetRecordType(createCommand.Target.Type);
                List <string>       sourceFieldNames = type.Fields.Select(t => t.Name).ToList();
                foreach (string field in createCommand.Target.Data.Keys)
                {
                    if (!sourceFieldNames.Contains(field))
                    {
                        throw new MissingFieldException($"Record with type {createCommand.Target.Type} doesn't have the column {field}");
                    }
                }
                if (createCommand.Target.Id == Guid.Empty)
                {
                    createCommand.Target.Id = Guid.NewGuid();
                }
                createCommand.Target["createdon"]  = DateTime.Now;
                createCommand.Target["modifiedon"] = DateTime.Now;
                Guid result = this.data.CreateRecord(createCommand.Target);
                output = new CreateRecordResult()
                {
                    RecordId = result
                };
                break;
            }

            case RetrieveRecordCommand _:
            {
                RetrieveRecordCommand retrieveCommand = (RetrieveRecordCommand)command;
                RecordType            type            = data.GetRecordType(retrieveCommand.Type);
                List <string>         fieldsToGet;
                if (retrieveCommand.AllFields)
                {
                    fieldsToGet = EnsureIdColumn(type.FieldNames, retrieveCommand.Type);
                }
                else
                {
                    string badFieldName = retrieveCommand.Fields.FirstOrDefault(f => !type.FieldNames.Contains(f));
                    if (badFieldName != null)
                    {
                        throw new Exception($"Record Type {retrieveCommand.Type} doesn't contain the field {badFieldName}");
                    }

                    fieldsToGet = retrieveCommand.Fields;
                }

                output = new RetrieveRecordResult()
                {
                    Result = this.data.RetrieveRecord(retrieveCommand.Type, retrieveCommand.Id, fieldsToGet)
                };
                break;
            }

            case CreateRecordTypeCommand _:
            {
                CreateRecordTypeCommand createCommand = (CreateRecordTypeCommand)command;

                createCommand.Target.Fields.Add(new PrimaryField()
                    {
                        Name = createCommand.Target.TableName + "Id"
                    });
                createCommand.Target.Fields.Add(new DateTimeField()
                    {
                        Name = "createdon", Nullable = false
                    });
                createCommand.Target.Fields.Add(new DateTimeField()
                    {
                        Name = "modifiedon", Nullable = false
                    });
                this.data.CreateRecordType(createCommand.Target);
                output = new CreateRecordTypeResult();
                break;
            }

            case RetrieveAllCommand _:
            {
                RetrieveAllCommand retreiveCommand = (RetrieveAllCommand)command;
                if (retreiveCommand.Columns == null)
                {
                    retreiveCommand.Columns = new List <string>();
                }
                BasicQuery query = new BasicQuery()
                {
                    Columns    = EnsureIdColumn(retreiveCommand.Columns, retreiveCommand.RecordType),
                    RecordType = retreiveCommand.RecordType,
                };
                var result = this.data.Query(query);
                output = new RetrieveAllResult()
                {
                    Result = result
                };
                break;
            }

            case RetrieveRecordTypeCommand _:
            {
                RetrieveRecordTypeCommand retrievecommand = (RetrieveRecordTypeCommand)command;
                var result = this.data.GetRecordType(retrievecommand.RecordType);
                output = new RetrieveRecordTypeResult()
                {
                    Type = result
                };
                break;
            }

            case DeleteRecordCommand _:
            {
                DeleteRecordCommand deleteCommand = (DeleteRecordCommand)command;
                this.data.DeleteRecord(deleteCommand.Type, deleteCommand.Id);
                output = new DeleteRecordResult();
                break;
            }

            case UpdateRecordCommand _:
            {
                UpdateRecordCommand updateCommand = (UpdateRecordCommand)command;
                this.data.UpdateRecord(updateCommand.Target);
                output = new UpdateRecordResult();
                break;
            }

            case RetrieveAllRecordTypesCommand _:
            {
                var allTypes = this.data.RetrieveAllRecordTypes();
                output = new RetrieveAllRecordTypesResult()
                {
                    RecordTypes = allTypes
                };
                break;
            }

            case AddFieldToRecordTypeCommand castedCommand:
            {
                this.data.AddFieldToRecordType(castedCommand.RecordType, castedCommand.Field);
                output = new AddFieldToRecordTypeResult();
            }
            break;

            case RemoveFieldFromRecordTypeCommand castedCommand:
            {
                this.data.RemoveFieldFromRecordType(castedCommand.RecordType, castedCommand.FieldName);
                output = new RemoveFieldFromRecordTypeResult();
                break;
            }

            case DeleteRecordTypeCommand castedCommand:
            {
                var recordType = this.data.GetRecordType(castedCommand.RecordType);
                this.data.DeleteRecordType(recordType.RecordTypeId);
                break;
            }

            case QueryRecordsCommand castedCommand:
            {
                var query = castedCommand.Query;
                query.Columns = EnsureIdColumn(query.Columns, query.RecordType);
                var result = this.data.Query(query);
                output = new QueryRecordsResult()
                {
                    Result = result
                };
                break;
            }

            default:
            {
                throw new Exception("Unknown command");
            }
            }
            data.Close();
            return(output);
        }