Exemplo n.º 1
0
        public static Expression <Func <Workflow, bool> > GenerateWorkflowPredicate(IPrincipal currentUser, int?uloId, string pegasysDocumentNumber, string organization,
                                                                                    IList <int> regions, IList <int> zones, string fund, IList <string> baCode, string pegasysTitleNumber, string pegasysVendorName, IList <string> docType, string contractingOfficersName, string currentlyAssignedTo, string hasBeenAssignedTo, string awardNumber, IList <string> reasonIncludedInReview, IList <bool> valid, IList <string> status, IList <int> reviewId, bool?reassignableByMe)
        {
            pegasysDocumentNumber = StringHelpers.TrimOrNull(pegasysDocumentNumber);
            organization          = StringHelpers.TrimOrNull(organization);
            fund = StringHelpers.TrimOrNull(fund);
            pegasysTitleNumber      = StringHelpers.TrimOrNull(pegasysTitleNumber);
            pegasysVendorName       = StringHelpers.TrimOrNull(pegasysVendorName);
            contractingOfficersName = StringHelpers.TrimOrNull(contractingOfficersName);
            currentlyAssignedTo     = StringHelpers.TrimOrNull(currentlyAssignedTo);
            hasBeenAssignedTo       = StringHelpers.TrimOrNull(hasBeenAssignedTo);
            awardNumber             = StringHelpers.TrimOrNull(awardNumber);
            reasonIncludedInReview  = reasonIncludedInReview ?? Empty.StringArray;

            bool hasFilters = false;

            var originalPredicate = PredicateBuilder.Create <Workflow>(wf => true);

            var predicate = originalPredicate;

            if (uloId != null)
            {
                hasFilters = true;
                predicate  = predicate.And(wf => wf.TargetUloId == uloId);
            }

            if (pegasysDocumentNumber != null)
            {
                hasFilters = true;
                var criteria = pegasysDocumentNumber.Replace(Wildcard, "");
                if (pegasysDocumentNumber.StartsWith(Wildcard) && pegasysDocumentNumber.EndsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.UnliquidatedObligation.PegasysDocumentNumber.Contains(criteria));
                }
                else if (pegasysDocumentNumber.StartsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.UnliquidatedObligation.PegasysDocumentNumber.TrimEnd().EndsWith(criteria));
                }
                else if (pegasysDocumentNumber.EndsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.UnliquidatedObligation.PegasysDocumentNumber.TrimStart().StartsWith(criteria));
                }
                else
                {
                    predicate =
                        predicate.And(
                            wf => wf.UnliquidatedObligation.PegasysDocumentNumber.Trim() == criteria);
                }
            }

            if (organization != null)
            {
                hasFilters = true;
                var criteria = organization.Replace(Wildcard, "");
                if (organization.StartsWith(Wildcard) && organization.EndsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.UnliquidatedObligation.Organization.Contains(criteria));
                }
                else if (organization.StartsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.UnliquidatedObligation.Organization.Trim().EndsWith(criteria));
                }
                else if (organization.EndsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.UnliquidatedObligation.Organization.Trim().StartsWith(criteria));
                }
                else
                {
                    predicate =
                        predicate.And(wf => wf.UnliquidatedObligation.Organization.Trim() == criteria);
                }
            }

            if (regions != null && regions.Count > 0)
            {
                hasFilters = true;
                predicate  = predicate.And(wf => wf.UnliquidatedObligation.RegionId != null && regions.Contains((int)wf.UnliquidatedObligation.RegionId));
            }

            if (zones != null && zones.Count > 0)
            {
                hasFilters = true;
                predicate  = predicate.And(wf => zones.Contains(wf.UnliquidatedObligation.Region.ZoneId));
            }

            if (fund != null)
            {
                hasFilters = true;
                var criteria = fund.Replace(Wildcard, "");
                if (fund.StartsWith(Wildcard) && fund.EndsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.UnliquidatedObligation.Fund.Contains(criteria));
                }
                else if (fund.StartsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.UnliquidatedObligation.Fund.Trim().EndsWith(criteria));
                }
                else if (fund.EndsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.UnliquidatedObligation.Fund.StartsWith(criteria));
                }
                else
                {
                    predicate = predicate.And(wf => wf.UnliquidatedObligation.Fund.Trim() == criteria);
                }
            }

            if (baCode != null && baCode.Count > 0)
            {
                hasFilters = true;
                predicate  = predicate.And(wf => baCode.Contains(wf.UnliquidatedObligation.Prog.Trim()));
            }

            if (pegasysTitleNumber != null)
            {
                hasFilters = true;
                var criteria = pegasysTitleNumber.Replace(Wildcard, "");
                if (pegasysTitleNumber.StartsWith(Wildcard) && pegasysTitleNumber.EndsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.UnliquidatedObligation.PegasysTitleNumber.Contains(criteria));
                }
                else if (pegasysTitleNumber.StartsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.UnliquidatedObligation.PegasysTitleNumber.Trim().EndsWith(criteria));
                }
                else if (pegasysTitleNumber.EndsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.UnliquidatedObligation.PegasysTitleNumber.Trim().StartsWith(criteria));
                }
                else
                {
                    predicate =
                        predicate.And(
                            wf =>
                            wf.UnliquidatedObligation.PegasysTitleNumber.Trim() ==
                            criteria);
                }
            }

            if (pegasysVendorName != null)
            {
                hasFilters = true;
                var criteria = pegasysVendorName.Replace(Wildcard, "");
                if (pegasysVendorName.StartsWith(Wildcard) && pegasysVendorName.EndsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.UnliquidatedObligation.VendorName.Contains(criteria));
                }
                else if (pegasysVendorName.StartsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.UnliquidatedObligation.VendorName.Trim().EndsWith(criteria));
                }
                else if (pegasysVendorName.EndsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.UnliquidatedObligation.VendorName.Trim().StartsWith(criteria));
                }
                else
                {
                    predicate =
                        predicate.And(wf =>
                                      wf.UnliquidatedObligation.VendorName.Trim() == criteria);
                }
            }

            if (docType != null && docType.Count > 0)
            {
                hasFilters = true;
                predicate  = predicate.And(wf => docType.Contains(wf.UnliquidatedObligation.DocType));
            }

            if (contractingOfficersName != null)
            {
                hasFilters = true;
                var criteria = contractingOfficersName.Replace(Wildcard, "");
                if (contractingOfficersName.StartsWith(Wildcard) && contractingOfficersName.EndsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.UnliquidatedObligation.ContractingOfficersName.Contains(criteria));
                }
                else if (contractingOfficersName.StartsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.UnliquidatedObligation.ContractingOfficersName.Trim().EndsWith(criteria));
                }
                else if (contractingOfficersName.EndsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.UnliquidatedObligation.ContractingOfficersName.StartsWith(criteria));
                }
                else
                {
                    predicate =
                        predicate.And(
                            wf =>
                            wf.UnliquidatedObligation.ContractingOfficersName.Trim() ==
                            criteria);
                }
            }

            if (currentlyAssignedTo != null)
            {
                hasFilters = true;
                var criteria = currentlyAssignedTo.Replace(Wildcard, "");
                if (currentlyAssignedTo.StartsWith(Wildcard) && currentlyAssignedTo.EndsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.AspNetUser.UserName.Contains(criteria));
                }
                else if (currentlyAssignedTo.StartsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.AspNetUser.UserName.Trim().EndsWith(criteria));
                }
                else if (currentlyAssignedTo.EndsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.AspNetUser.UserName.Trim().StartsWith(criteria));
                }
                else
                {
                    predicate = predicate.And(wf => wf.AspNetUser.UserName.Trim() == currentlyAssignedTo);
                }
            }

            if (hasBeenAssignedTo != null)
            {
                hasFilters = true;
                var criteria = hasBeenAssignedTo.Replace(Wildcard, "");
                if (hasBeenAssignedTo.StartsWith(Wildcard) && hasBeenAssignedTo.EndsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.WorkflowHistories.Any(wfh => wfh.AspNetUser.UserName.Contains(criteria)));
                }
                else if (hasBeenAssignedTo.StartsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.WorkflowHistories.Any(wfh => wfh.AspNetUser.UserName.EndsWith(criteria)));
                }
                else if (hasBeenAssignedTo.EndsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.WorkflowHistories.Any(wfh => wfh.AspNetUser.UserName.StartsWith(criteria)));
                }
                else
                {
                    predicate =
                        predicate.And(
                            wf => wf.WorkflowHistories.Any(wfh => wfh.AspNetUser.UserName == criteria));
                }
            }

            if (awardNumber != null)
            {
                hasFilters = true;
                var criteria = awardNumber.Replace(Wildcard, "");
                if (awardNumber.StartsWith(Wildcard) && awardNumber.EndsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.UnliquidatedObligation.AwardNbr.Contains(criteria));
                }
                else if (awardNumber.StartsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.UnliquidatedObligation.AwardNbr.Trim().EndsWith(criteria));
                }
                else if (awardNumber.EndsWith(Wildcard))
                {
                    predicate =
                        predicate.And(
                            wf => wf.UnliquidatedObligation.AwardNbr.Trim().StartsWith(criteria));
                }
                else
                {
                    predicate = predicate.And(wf => wf.UnliquidatedObligation.AwardNbr.Trim() == criteria);
                }
            }

            if (reasonIncludedInReview != null && reasonIncludedInReview.Count > 0)
            {
                hasFilters = true;
                predicate  = predicate.And(wf => reasonIncludedInReview.Contains(wf.UnliquidatedObligation.ReasonIncludedInReview.Trim()));
            }

            if (valid != null && valid.Count == 1)
            {
                hasFilters = true;
                var v = valid[0];
                predicate = predicate.And(wf => wf.UnliquidatedObligation.Valid == v);
            }

            if (status != null && status.Count > 0)
            {
                hasFilters = true;
                predicate  = predicate.And(wf => status.Contains(wf.UnliquidatedObligation.Status.Trim()));
            }

            if (reviewId != null && reviewId.Count > 0)
            {
                hasFilters = true;
                predicate  = predicate.And(wf => reviewId.Contains(wf.UnliquidatedObligation.ReviewId));
            }

            if (reassignableByMe.GetValueOrDefault())
            {
                hasFilters = true;
                var regionIds = GetUserGroupRegions(currentUser, PortalHelpers.ReassignGroupUserId);
                predicate = predicate.And(GetWorkflowsRegionIdPredicate(regionIds));
            }

            return(hasFilters ? predicate : null);
        }