private Fetch FilterGlobalRecords(ViewConfiguration viewConfiguration) { SavedQueryView queryView = viewConfiguration.GetSavedQueryView(_serviceContext); var objectName = queryView.Name; Fetch fetch; if (viewConfiguration.FetchXml != null) { fetch = Fetch.Parse(viewConfiguration.FetchXml); } else { fetch = Fetch.Parse(queryView.FetchXml); } String webRole = String.Empty; String parentustomerId = String.Empty; String parentustomerType = String.Empty; var accountField = String.Empty; var context = HttpContext.Current; var request = context.Request.RequestContext; var cookies = request.HttpContext.Request.Cookies; if (cookies != null) { if (cookies["Branch"] != null) { webRole = cookies["Branch"]["webRoleName"]; parentustomerId = cookies["Branch"]["parentCustomerId"]; parentustomerType = cookies["Branch"]["parentCustomerType"]; if (parentustomerType == "100000000") { accountField = "gsc_dealerid"; } else if (parentustomerType == "100000001") { accountField = "gsc_branchid"; } } } if (!webRole.Contains("MMPC") && webRole != "Administrators") { //Check if entity is a global entity if (!CheckIfGlobalEntity(queryView)) { return(fetch); } else { Filter filter = new Filter { Type = LogicalOperator.Or }; filter.Conditions = new List <Condition>(); filter.Conditions.Add(new Condition { Attribute = accountField, Operator = ConditionOperator.Equal, Value = parentustomerId }); filter.Conditions.Add(new Condition { Attribute = "gsc_isglobalrecord", Operator = ConditionOperator.Equal, Value = true }); fetch.Entity.Filters.Add(filter); } } return(fetch); }
private ActionResult GetData(ViewConfiguration viewConfiguration, string sortExpression, string search, string filter, string metaFilter, int page, int pageSize = DefaultPageSize, bool applyRecordLevelFilters = true, bool applyRelatedRecordFilter = false, string filterRelationshipName = null, string filterEntityName = null, string filterAttributeName = null, Guid?filterValue = null, bool overrideMaxPageSize = false, string createdOnFilter = null) { if (viewConfiguration == null) { return(new HttpStatusCodeResult(HttpStatusCode.Forbidden, "Invalid Request.")); } if (pageSize < 0) { pageSize = DefaultPageSize; } viewConfiguration = EnableSearchForPriceList(viewConfiguration); if (pageSize > DefaultMaxPageSize && !overrideMaxPageSize) { Tracing.FrameworkInformation(GetType().FullName, "GetData", "pageSize={0} is greater than the allowed maximum page size of {1}. Page size has been constrained to {1}.", pageSize, DefaultMaxPageSize); pageSize = DefaultMaxPageSize; } var dataAdapterDependencies = new PortalConfigurationDataAdapterDependencies(requestContext: Request.RequestContext, portalName: viewConfiguration.PortalName); #region - DMS Custom Filtering - CustomFetchXml converter = new CustomFetchXml(dataAdapterDependencies.GetServiceContext(), new XrmConnection()); if (metaFilter != null) { if (metaFilter.IndexOf(",date") > 0) { string dateFilterString = metaFilter.Substring(metaFilter.IndexOf("date=") + 5); string[] dateFilters = dateFilterString.Split(new string[] { ",date=" }, StringSplitOptions.None); /*int dateFromIndexStart = metaFilter.LastIndexOf("DateFrom=") + 9; * int dateFromIndexEnd = metaFilter.LastIndexOf("&DateTo=") + 8; * dateFilter = metaFilter.Substring(metaFilter.IndexOf("date=") + 5); * int fieldNameIndex = metaFilter.LastIndexOf("&field=") + 7; * string dateFromValue = metaFilter.Substring(dateFromIndexStart, 10); * string dateToValue = metaFilter.Substring(dateFromIndexEnd, 10); * string entityFieldName = metaFilter.Substring(fieldNameIndex, (metaFilter.Length - fieldNameIndex));*/ foreach (string dateFilter in dateFilters) { string[] dateFilterValues = dateFilter.Split('&'); DateTime?dateFromValue = dateFilterValues[0].Split('=')[1] != "" ? (DateTime?)Convert.ToDateTime(dateFilterValues[0].Split('=')[1]) : null; DateTime?dateToValue = dateFilterValues[1].Split('=')[1] != "" ? (DateTime?)Convert.ToDateTime(dateFilterValues[1].Split('=')[1]) : null; string entityFieldName = dateFilterValues[2].Split('=')[1]; viewConfiguration = converter.SetCustomFetchXml(viewConfiguration, dateFromValue, dateToValue, entityFieldName); int start = metaFilter.LastIndexOf(",date"); metaFilter = metaFilter.Substring(0, start); } } if (metaFilter.IndexOf(",prospect") > 0) { int start = metaFilter.LastIndexOf(",prospect"); viewConfiguration = converter.FilterProspect(viewConfiguration); metaFilter = metaFilter.Substring(0, start); } if (metaFilter.IndexOf(",statecode") > 0) { int start = metaFilter.LastIndexOf(",statecode"); string statecode = metaFilter.Substring(metaFilter.IndexOf("statecode=") + 10); viewConfiguration = converter.FilterRecordsbyStateCode(viewConfiguration, statecode); metaFilter = metaFilter.Substring(0, start); } if (metaFilter.IndexOf(",vehiclecolor") > 0) { int start = metaFilter.LastIndexOf(",vehiclecolor"); string vehicleColor = metaFilter.Substring(metaFilter.IndexOf("vehiclecolor=") + 13); viewConfiguration = converter.FilterRecordsbyVehicleColor(viewConfiguration, vehicleColor); metaFilter = metaFilter.Substring(0, start); } } viewConfiguration = converter.CustomFilterViews(viewConfiguration); // viewConfiguration = converter.FilterRootBusinessUnitRecords(viewConfiguration); #endregion //disable related record filtering for Vehicle Lookup in PO var serviceContext2 = dataAdapterDependencies.GetServiceContext(); SavedQueryView queryView = viewConfiguration.GetSavedQueryView(serviceContext2); if (queryView.Name == "Vehicle Lookup - PO Portal View") { applyRecordLevelFilters = false; } var viewDataAdapter = applyRelatedRecordFilter && (!string.IsNullOrWhiteSpace(filterRelationshipName) && !string.IsNullOrWhiteSpace(filterEntityName)) ? new CustomViewAdapter(viewConfiguration, dataAdapterDependencies, filterRelationshipName, filterEntityName, filterAttributeName, filterValue ?? Guid.Empty, page, search, sortExpression, filter, metaFilter, applyRecordLevelFilters) : new CustomViewAdapter(viewConfiguration, dataAdapterDependencies, page, search, sortExpression, filter, metaFilter, applyRecordLevelFilters); var result = viewDataAdapter.CustomFetchEntities(viewConfiguration); var resultRecords = result.Records; // var filteredRecords = converter.FilterSharedEntityScope(viewConfiguration, result.Records); // var globalRecords = converter.FilterRootBusinessUnitRecords(viewConfiguration, filterEntityName, filterRelationshipName, filterValue, search); //var combinedResults = filteredRecords.Union(globalRecords); // combinedResults = combinedResults.GroupBy(x => x.Id).Select(y => y.First()); //Custom Order By /* string[] order = sortExpression.Split(' '); * var count = 0; * * foreach (var combinedResult in resultRecords) * { * foreach (var attributes in combinedResult.Attributes) * { * var name = attributes.Key; * if (name.Equals(order[0])) * { * count++; * * var value = attributes.Value; * var valueType = value.GetType().Name; * if (valueType == "String") * { * if (order[1] == "DESC") * resultRecords = resultRecords.OrderByDescending(x => x.Contains(order[0]) ? x.Attributes[order[0]] : ""); * else * resultRecords = resultRecords.OrderBy(x => x.Contains(order[0]) ? x.Attributes[order[0]] : ""); * } * else if (valueType == "Money") * { * if (order[1] == "DESC") * resultRecords = resultRecords.OrderByDescending(x => x.Contains(order[0]) ? x.GetAttributeValue<Money>(order[0]).ToString() : ""); * else * resultRecords = resultRecords.OrderBy(x => x.Contains(order[0]) ? x.GetAttributeValue<Money>(order[0]).ToString() : ""); * } * else if (valueType == "OptionSetValue") * { * if (order[1] == "DESC") * resultRecords = resultRecords.OrderByDescending(x => x.Contains(order[0]) ? x.FormattedValues[order[0]] : ""); * else * resultRecords = resultRecords.OrderBy(x => x.Contains(order[0]) ? x.FormattedValues[order[0]] : ""); * } * else if (valueType == "EntityReference") * { * if (order[1] == "DESC") * resultRecords = resultRecords.OrderByDescending(x => x.GetAttributeValue<EntityReference>(order[0]) != null ? x.GetAttributeValue<EntityReference>(order[0]).Name : ""); * else * resultRecords = resultRecords.OrderBy(x => x.GetAttributeValue<EntityReference>(order[0]) != null ? x.GetAttributeValue<EntityReference>(order[0]).Name : ""); * } * break; * } * } * * if (count > 0) * break; * } * //Custom Order By Ends Here*/ if (result.EntityPermissionDenied) { var permissionResult = new EntityPermissionResult(true); return(Json(permissionResult)); } IEnumerable <EntityRecord> records; if (viewConfiguration.EnableEntityPermissions && AdxstudioCrmConfigurationManager.GetCrmSection().ContentMap.Enabled) { var serviceContext = dataAdapterDependencies.GetServiceContext(); var crmEntityPermissionProvider = new CrmEntityPermissionProvider(); records = resultRecords.Select(e => new EntityRecord(e, serviceContext, crmEntityPermissionProvider, viewDataAdapter.EntityMetadata, true)); } else { records = resultRecords.Select(e => new EntityRecord(e, viewDataAdapter.EntityMetadata)); } var totalRecordCount = result.TotalRecordCount; var data = new PaginatedGridData(records, totalRecordCount, page, pageSize); var json = Json(data); json.MaxJsonLength = int.MaxValue; // return(json); }
private Fetch FilterSharedEntityScope(Fetch fetch, ViewConfiguration viewConfiguration) { SavedQueryView queryView = viewConfiguration.GetSavedQueryView(_serviceContext); var objectName = queryView.Name; EntityCollection entityPermission = GetSharedEntityScope(fetch); if (entityPermission == null) { return(fetch); } String scope = ""; if (objectName.Equals("Individual") || objectName.Equals("Corporate")) { scope = GetReadAppendScope(entityPermission, "adx_append"); } else { scope = GetReadAppendScope(entityPermission, "adx_read"); } if (scope == "Global") { return(fetch); } else if (scope == "Account") { Guid branchId = Guid.Empty; Guid userId = Guid.Empty; String webRole = String.Empty; var context = HttpContext.Current; var request = context.Request.RequestContext; var cookies = request.HttpContext.Request.Cookies; if (cookies != null) { if (cookies["Branch"] != null) { branchId = new Guid(cookies["Branch"]["branchId"]); userId = new Guid(cookies["Branch"]["userId"]); webRole = cookies["Branch"]["webRoleName"]; } } if (webRole.Contains("Sales Supervisor") && (objectName.Equals("Individual") || objectName.Equals("Corporate"))) { Link relatedEntity = new Link(); relatedEntity.Alias = "Related"; relatedEntity.FromAttribute = "contactid"; relatedEntity.Name = "contact"; relatedEntity.ToAttribute = "gsc_salesexecutiveid"; fetch.Entity.Links.Add(relatedEntity); Filter filter = new Filter { Type = LogicalOperator.Or }; filter.Conditions = new List <Condition>(); filter.Conditions.Add(new Condition { Attribute = "gsc_salesexecutiveid", Operator = ConditionOperator.Equal, Value = userId }); filter.Conditions.Add(new Condition { EntityName = "Related", Attribute = "gsc_reportsto", Operator = ConditionOperator.Equal, Value = userId }); fetch.Entity.Filters.Add(filter); } else { Filter filter = new Filter { Type = LogicalOperator.And }; filter.Conditions = new List <Condition>(); filter.Conditions.Add(new Condition { Attribute = "gsc_branchid", Operator = ConditionOperator.Equal, Value = branchId }); fetch.Entity.Filters.Add(filter); } } else if (scope == "Contact") { Guid userId = Guid.Empty; var context = HttpContext.Current; var request = context.Request.RequestContext; var cookies = request.HttpContext.Request.Cookies; if (cookies != null) { if (cookies["Branch"] != null) { userId = new Guid(cookies["Branch"]["userId"]); } } Filter filter = new Filter { Type = LogicalOperator.And }; filter.Conditions = new List <Condition>(); if (objectName.Equals("Individual") || objectName.Equals("Corporate")) { filter.Conditions.Add(new Condition { Attribute = "gsc_salesexecutiveid", Operator = ConditionOperator.Equal, Value = userId }); } else { filter.Conditions.Add(new Condition { Attribute = "gsc_recordownerid", Operator = ConditionOperator.Equal, Value = userId }); } fetch.Entity.Filters.Add(filter); } return(fetch); }