private void AppendAvailableRecordIds(List <string> availableRecordIdentifiers, EntityCollection rightsCollection, string entityName, ESSearchResponse esResponse) { if (GetIsFileEntity(entityName)) { var availableIds = rightsCollection.Select(e => e.PrimaryColumnValue.ToString().ToLower()); try { EntitySchema fileSchema = _userConnection.EntitySchemaManager.FindInstanceByName(entityName); var parentColumnName = entityName.Replace(FileEntityNameSuffix, ""); var column = fileSchema.Columns.FindByName(parentColumnName); if (column != null) { var masterPrimaryColumnAlias = GlobalSearchColumnUtils.GetPrimaryColumnAlias(column, entityName); var availableFileHits = _userConnection.DBSecurityEngine.GetSchemaUseMasterRecordRights(entityName) ? esResponse.SearchResult.Hits.Where( x => x.Type == entityName && availableIds.Contains(x.Id.ToLower())).ToList() : esResponse.SearchResult.Hits.Where( x => x.Source.ContainsKey(masterPrimaryColumnAlias) && x.Type == entityName && availableIds.Contains(x.Source[masterPrimaryColumnAlias].ToLower())).ToList(); availableRecordIdentifiers.AddRange(availableFileHits.Select(x => x.Id.ToLower())); } } catch (Exception ex) { _log.ErrorFormat(@"Entity name: {0}, Identifiers: {1} Exception: {2}" , entityName, string.Join(", ", availableIds), ex.Message); } return; } availableRecordIdentifiers.AddRange(rightsCollection .Select(e => e.PrimaryColumnValue.ToString().ToLower())); }
private Dictionary <string, List <string> > PrepareFileRightsConditions(ESSearchResponse esResponse, Dictionary <string, List <string> > conditions) { var entityName = conditions.First().Key; var listOfIds = conditions.First().Value; var parentEntityName = entityName.Replace(FileEntityNameSuffix, ""); var parentEntity = _userConnection.EntitySchemaManager.FindInstanceByName(parentEntityName); if (parentEntity != null) { try { var currentFileHits = esResponse.SearchResult.Hits.Where(x => listOfIds.Contains(x.Id)).ToList(); EntitySchema fileSchema = _userConnection.EntitySchemaManager.FindInstanceByName(entityName); var column = fileSchema.Columns.FindByName(parentEntityName); if (column != null) { var masterPrimaryColumnAlias = GlobalSearchColumnUtils.GetPrimaryColumnAlias(column, entityName); return(new Dictionary <string, List <string> > { { parentEntityName, currentFileHits .Where(x => x.Source.ContainsKey(masterPrimaryColumnAlias)) .Select(x => x.Source[masterPrimaryColumnAlias]).ToList() } }); } } catch (Exception ex) { _log.ErrorFormat(@"Entity name: {0}, Identifiers: {1} Exception: {2}" , entityName, string.Join(", ", listOfIds), ex.Message); } } return(conditions); }
private List <string> GetAvailableRecordIdentifiers(ESSearchResponse esResponse) { IEnumerable <IGrouping <string, string> > groupedTypes = esResponse.SearchResult.Hits.GroupBy(x => x.Type, x => x.Id); List <string> availableRecordIdentifiers = new List <string>(); Stopwatch sw = new Stopwatch(); sw.Start(); foreach (var groupedType in groupedTypes) { var condition = PrepareRightsCondition(esResponse, groupedType.Key, groupedType.ToList()); if (_userConnection.EntitySchemaManager.FindInstanceByName(condition.Key) == null || condition.Value.Count == 0) { _log.InfoFormat("Instance of entity with name {0} was not found while checking record permisiions.", condition.Key); continue; } var esq = new EntitySchemaQuery(_userConnection.EntitySchemaManager, condition.Key); esq.PrimaryQueryColumn.IsAlwaysSelect = true; foreach (string column in _userConnection.DBSecurityEngine.GetSchemaSignificantColumns(condition.Key)) { if (esq.RootSchema.Columns.FindByName(column) != null) { esq.AddColumn(column); } } esq.Filters.Add(esq.CreateFilterWithParameters(FilterComparisonType.Equal, esq.PrimaryQueryColumn.Name, condition.Value)); EntityCollection collection = esq.GetEntityCollection(_userConnection); AppendAvailableRecordIds(availableRecordIdentifiers, collection, groupedType.Key, esResponse); } sw.Stop(); _metricReporter.Report(new SearchMetric { Duration = sw.ElapsedMilliseconds, Source = SearchSource.Bpm }); return(availableRecordIdentifiers); }
private void SetResponseNextFrom(BpmSearchResponse bpmSearchResponse, ESSearchResponse esSearchResponse, SearchRequestQuery searchRequestQuery) { var lastNeededHitId = bpmSearchResponse.Data.Last().Id; var lastIndexFromElasticResponce = esSearchResponse.SearchResult.Hits.ToList() .FindIndex(x => string.Equals(x.Id, lastNeededHitId, StringComparison.InvariantCultureIgnoreCase)); bpmSearchResponse.NextFrom = searchRequestQuery.From + lastIndexFromElasticResponce + 1; }
private KeyValuePair <string, List <string> > PrepareRightsCondition(ESSearchResponse esResponse, string entityName, List <string> listOfRecordsId) { var conditions = new Dictionary <string, List <string> > { { entityName, listOfRecordsId } }; if (GetIsFileEntity(entityName) && !_userConnection.DBSecurityEngine.GetSchemaUseMasterRecordRights(entityName)) { conditions = PrepareFileRightsConditions(esResponse, conditions); } return(conditions.First()); }
/// <summary> /// Proccess elastic search response. /// </summary> /// <param name="esResponse"><see cref="ESSearchResponse"/> instance.</param> /// <returns><see cref="BpmSearchResponse"/> instance.</returns> public BpmSearchResponse ProccessESSearchResponse(ESSearchResponse esResponse) { var availableRecordIdentifiers = GetAvailableRecordIdentifiers(esResponse); BpmSearchResponse searchResponse = new BpmSearchResponse(); searchResponse.Took = esResponse.Took; searchResponse.Total = esResponse.SearchResult.Total; searchResponse.Data = new List <BpmSearchResponseEntity>(); searchResponse.Success = true; foreach (ESHit esHit in esResponse.SearchResult.Hits .Where(x => availableRecordIdentifiers.Contains(x.Id.ToLower())).ToList()) { if (_userConnection.EntitySchemaManager.FindInstanceByName(esHit.Type) != null) { var responseEntity = FillBpmResponseEntity(esHit); if (responseEntity.ColumnValues.Any()) { searchResponse.Data.Add(responseEntity); } } } return(searchResponse); }