public ODataQueryExpressionVisitor(EntityMetadata metadata)
 {
     _mapper       = EntityMapFactory.Create(metadata, new DefaultTypeMapFactory(), null);
     _queryOptions = new Dictionary <string, string>
     {
         { "$count", "true" }
     };
 }
Exemple #2
0
        public VirtualEntityPluginContext(IServiceProvider serviceProvider)
        {
            PluginExecutionContext = serviceProvider.Get <IPluginExecutionContext>();
            var serviceFactory = serviceProvider.Get <IOrganizationServiceFactory>();
            var service        = serviceFactory.CreateOrganizationService(null);

            var retriever    = serviceProvider.Get <IEntityDataSourceRetrieverService>();
            var dataSource   = retriever.RetrieveEntityDataSource();
            var instanceName = dataSource.GetAttributeValue <string>("d365_instancename");
            var userName     = dataSource.GetAttributeValue <string>("d365_username");
            var token        = dataSource.GetAttributeValue <string>("d365_token");

            ZendeskClient = new ZendeskClient(instanceName, userName, token);

            var metadata = service.GetEntityMetadata(PluginExecutionContext.PrimaryEntityName);

            Mapping = EntityMapFactory.Create(metadata, new DefaultTypeMapFactory(), null);

            //var externalName = Mapping.MapAttributeNameExternal("crm_field_name");
        }
 public SqlVisitor(EntityMetadata metadata)
 {
     _metadata = metadata;
     _mapper   = EntityMapFactory.Create(metadata, new DefaultTypeMapFactory(), null);
 }
        private IEnumerable <Entity> ExecuteQuery(string query, EntityMetadata metadata)
        {
            var uri                = _dataSource.GetAttributeValue <string>("bg_uri");
            var key                = _dataSource.GetAttributeValue <string>("bg_key");
            var database           = _dataSource.GetAttributeValue <string>("bg_database");
            var crossParitionQuery = _dataSource.GetAttributeValue <bool>("bg_enablecrosspartitionquery");
            var collection         = metadata.ExternalName;

            var date = DateTime.UtcNow.ToString("R");

            var resourceType = "docs";
            var resourceId   = $"dbs/{database}/colls/{collection}";

            var hash = GenerateHash("POST", resourceType, resourceId, date, key, "master", "1.0");

            var request = WebRequest.CreateHttp($"{uri}/{resourceId}/{resourceType}");

            request.Method      = "POST";
            request.ContentType = "application/query+json";
            request.Headers.Add("Authorization", hash);
            request.Headers.Add("X-MS-Date", date);
            request.Headers.Add("X-MS-Version", "2017-02-22");
            request.Headers.Add("X-MS-DocumentDB-IsQuery", "True");
            if (crossParitionQuery)
            {
                request.Headers.Add("X-MS-DocumentDB-Query-EnableCrossPartition", "True");
            }

            var body = Encoding.UTF8.GetBytes($"{{\"query\": \"{query}\"}}");

            using (var stream = request.GetRequestStream())
            {
                stream.Write(body, 0, body.Length);
            }

            Records records;

            try
            {
                using (var response = request.GetResponse())
                    using (var stream = response.GetResponseStream())
                    {
                        var settings = new DataContractJsonSerializerSettings()
                        {
                            UseSimpleDictionaryFormat = true
                        };
                        var serializer = new DataContractJsonSerializer(typeof(Records), settings);

                        records = serializer.ReadObject(stream) as Records;
                    }
            }
            catch (WebException ex)
            {
                using (var stream = ex.Response.GetResponseStream())
                    using (var reader = new StreamReader(stream))
                    {
                        var error = reader.ReadToEnd();

                        _tracing.Trace(error);

                        throw;
                    }
            }

            var mapper = EntityMapFactory.Create(metadata, new DefaultTypeMapFactory(), null);

            foreach (var record in records.Documents)
            {
                var entityId = record[mapper.MapAttributeNameExternal(metadata.PrimaryIdAttribute)];
                var entity   = new Entity(metadata.LogicalName, new Guid(entityId.ToString()));

                foreach (var attribute in metadata.Attributes)
                {
                    _tracing.Trace("Attribute: " + attribute.LogicalName);
                    var logicalName  = attribute.LogicalName;
                    var externalName = mapper.MapAttributeNameExternal(logicalName);

                    if (record.ContainsKey(externalName))
                    {
                        entity[logicalName] = record[externalName];
                    }
                }

                yield return(entity);
            }
        }