Ejemplo n.º 1
0
        /// <summary>
        /// Creates a Linq Expression that can be applied to an IQueryable to filter the result set.
        /// </summary>
        /// <param name="entityType">The type of entity in the result set.</param>
        /// <param name="serviceInstance">A service instance that can be queried to obtain the result set.</param>
        /// <param name="parameterExpression">The input parameter that will be injected into the filter expression.</param>
        /// <param name="selection">A formatted string representing the filter settings.</param>
        /// <returns>
        /// A Linq Expression that can be used to filter an IQueryable.
        /// </returns>
        /// <exception cref="System.Exception">Filter issue(s):  + errorMessages.AsDelimited( ;  )</exception>
        public override Expression GetExpression(Type entityType, IService serviceInstance, ParameterExpression parameterExpression, string selection)
        {
            var settings = new FilterSettings(selection);

            var context = ( RockContext )serviceInstance.Context;

            // Get the Benevolence Request Data View.
            var dataView = DataComponentSettingsHelper.GetDataViewForFilterComponent(settings.DataViewGuid, context);

            // Evaluate the Data View that defines the Benevolence Result.
            var benevolenceResultService = new BenevolenceResultService(context);

            var benevolenceResultQuery = benevolenceResultService.Queryable();

            if (dataView != null)
            {
                benevolenceResultQuery = DataComponentSettingsHelper.FilterByDataView(benevolenceResultQuery, dataView, benevolenceResultService);
            }

            var benevolenceRequestKey = benevolenceResultQuery.Select(a => a.BenevolenceRequestId);
            // Get all of the result corresponding to the qualifying Benevolence Requests.
            var qry = new BenevolenceRequestService(context).Queryable()
                      .Where(g => benevolenceRequestKey.Contains(g.Id));

            // Retrieve the Filter Expression.
            var extractedFilterExpression = FilterExpressionExtractor.Extract <Model.BenevolenceRequest>(qry, parameterExpression, "g");

            return(extractedFilterExpression);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Loads the BenevolenceResult data.
        /// </summary>
        /// <param name="csvData">The CSV data.</param>
        private int LoadBenevolenceResult(CSVInstance csvData)
        {
            var lookupContext             = new RockContext();
            var benevolenceResultService  = new BenevolenceResultService(lookupContext);
            var benevolenceRequestService = new BenevolenceRequestService(lookupContext);
            var resultTypeDTGuid          = Rock.SystemGuid.DefinedType.BENEVOLENCE_RESULT_TYPE.AsGuid();
            var benevolenceResultList     = new List <BenevolenceResult>();

            var importedRequestIds = new List <int>();

            var completed            = 0;
            var importedCount        = 0;
            var alreadyImportedCount = benevolenceResultService.Queryable().AsNoTracking().Count(i => i.ForeignKey != null);

            ReportProgress(0, $"Starting Benevolence Result import ({alreadyImportedCount:N0} already exist).");

            string[] row;
            // Uses a look-ahead enumerator: this call will move to the next record immediately
            while ((row = csvData.Database.FirstOrDefault()) != null)
            {
                var benevolenceResultRequestId   = row[BenevolenceResultRequestId];
                var benevolenceResultType        = row[BenevolenceResultType];
                var benevolenceResultId          = row[BenevolenceResultId];
                var benevolenceResultAmount      = row[BenevolenceResultAmount];
                var benevolenceResultSummary     = row[BenevolenceResultSummary];
                var benevolenceResultCreatedById = row[BenevolenceResultCreatedById];
                var benevolenceResultCreatedDate = row[BenevolenceResultCreatedDate];

                //
                // Verify the Benevolence Result has a Result Type provided in the csv file.
                //
                if (string.IsNullOrWhiteSpace(benevolenceResultType))
                {
                    ReportProgress(0, $"Benevolence Result {benevolenceResultId} has no BenevolenceResultType value provided. Skipping Benevolence Result {benevolenceResultId}.");
                    LogException("InvalidBenevolenceResult", string.Format("ResultId: {0} - Missing BenevolenceResultType value. Benevolence Result {0} was not imported.", benevolenceResultId));
                    completed++;
                    continue;
                }

                BenevolenceRequest benevolenceRequest = null;
                if (benevolenceRequestService.Queryable().AsNoTracking().Any(r => r.ForeignKey == benevolenceResultRequestId))
                {
                    benevolenceRequest = benevolenceRequestService.Queryable().AsNoTracking().FirstOrDefault(r => r.ForeignKey == benevolenceResultRequestId);
                }

                //
                // Verify the Benevolence Request exists.
                //
                if (benevolenceRequest == null || benevolenceRequest.Id < 1)
                {
                    ReportProgress(0, $"Benevolence Request {benevolenceResultRequestId} not found. Skipping Benevolence Result {benevolenceResultId}.");
                    LogException("InvalidBenevolenceResult", string.Format("ResultId: {0} - BenevolenceResultRequestId {1} does not exist in imported Benevolence Requests. Benevolence Result {0} was not imported.", benevolenceResultId, benevolenceResultRequestId));
                    completed++;
                    continue;
                }

                //
                // Check that this Benevolence Result doesn't already exist.
                //
                var exists = false;
                if (alreadyImportedCount > 0)
                {
                    exists = benevolenceResultService.Queryable().AsNoTracking().Any(r => r.ForeignKey == benevolenceResultId);
                }

                if (!exists)
                {
                    // Handle Result Type
                    var resultTypeDV = FindDefinedValueByTypeAndName(lookupContext, resultTypeDTGuid, benevolenceResultType);
                    if (resultTypeDV == null)
                    {
                        resultTypeDV = AddDefinedValue(new RockContext(), resultTypeDTGuid.ToString(), benevolenceResultType);
                    }

                    // Format created date
                    var resultCreatedDate = ( DateTime )ParseDateOrDefault(benevolenceResultCreatedDate, Bulldozer.BulldozerComponent.ImportDateTime);

                    // Handle created by
                    int?createdByAliasId    = null;
                    var createdByPersonKeys = GetPersonKeys(benevolenceResultCreatedById);
                    if (createdByPersonKeys != null)
                    {
                        createdByAliasId = createdByPersonKeys.PersonAliasId;
                    }

                    //
                    // Create and populate the new Benevolence Result.
                    //
                    var benevolenceResult = new BenevolenceResult
                    {
                        BenevolenceRequestId = benevolenceRequest.Id,
                        ResultSummary        = benevolenceResultSummary,
                        ResultTypeValueId    = resultTypeDV.Id,
                        Amount                 = benevolenceResultAmount.AsType <decimal?>(),
                        ForeignKey             = benevolenceResultId,
                        ForeignId              = benevolenceResultId.AsType <int?>(),
                        CreatedDateTime        = resultCreatedDate,
                        CreatedByPersonAliasId = createdByAliasId,
                    };

                    benevolenceResultList.Add(benevolenceResult);

                    importedCount++;
                }

                //
                // Notify user of our status.
                //
                completed++;
                if (completed % (ReportingNumber * 10) < 1)
                {
                    ReportProgress(0, $"{completed:N0} Benevolence Request records processed, {importedCount:N0} imported.");
                }

                if (completed % ReportingNumber < 1)
                {
                    SaveBenevolenceResults(benevolenceResultList);
                    ReportPartialProgress();
                    benevolenceResultList.Clear();

                    // Clear out variables
                    benevolenceRequestService = new BenevolenceRequestService(lookupContext);
                }
            }

            if (benevolenceResultList.Any())
            {
                SaveBenevolenceResults(benevolenceResultList);
            }

            ReportProgress(0, $"Finished Benevolence Result import: {importedCount:N0} records added.");

            return(completed);
        }