Example #1
0
        /// <summary>
        /// Invoked during execution of the query, with the
        /// pre populated expression tree.
        /// </summary>
        /// <param name="expression">Target expression block</param>
        /// <returns>Expected result</returns>
        public IEnumerable <T> Execute(Ast.Expression expression)
        {
            Visit(expression);

            var optionsIn     = string.Join(";", Options.Select(x => $"{x.Key}={x.Value}"));
            var objectType    = ObjectTypes.GetObjectType <T>();
            var xmlIn         = WitsmlParser.ToXml(Query, true);
            var originalXmlIn = xmlIn;

            if (Context.Connection.CompressRequests)
            {
                ClientCompression.Compress(ref xmlIn, ref optionsIn);
            }

            Context.LogQuery(Functions.GetFromStore, objectType, originalXmlIn, optionsIn);

            using (var client = Context.Connection.CreateClientProxy().WithUserAgent())
            {
                var    wmls = (IWitsmlClient)client;
                string suppMsgOut, xmlOut = string.Empty;
                var    result = Enumerable.Empty <T>();
                short  returnCode;

                try
                {
                    returnCode = wmls.WMLS_GetFromStore(objectType, xmlIn, optionsIn, null, out xmlOut, out suppMsgOut);
                }
                catch (Exception ex)
                {
                    Logger.ErrorFormat("Error querying store: {0}", ex);
                    returnCode = -1;
                    suppMsgOut = "Error querying store:" + ex.GetBaseException().Message;
                }

                try
                {
                    // Handle servers that compress the response to a compressed request.
                    if (Context.Connection.CompressRequests)
                    {
                        xmlOut = ClientCompression.SafeDecompress(xmlOut);
                    }

                    if (returnCode > 0)
                    {
                        var document = WitsmlParser.Parse(xmlOut);
                        var response = WitsmlParser.Parse <TList>(document.Root);
                        result = (IEnumerable <T>)response.Items;
                    }
                }
                catch (WitsmlException ex)
                {
                    Logger.ErrorFormat("Error parsing query response: {0}{2}{2}{1}", xmlOut, ex, Environment.NewLine);
                    returnCode = (short)ex.ErrorCode;
                    suppMsgOut = ex.Message + " " + ex.GetBaseException().Message;
                }

                Context.LogResponse(Functions.GetFromStore, objectType, originalXmlIn, optionsIn, xmlOut, returnCode, suppMsgOut);
                return(result);
            }
        }
Example #2
0
        private IEnergisticsCollection ExecuteGetFromStoreQuery(string objectType, string xmlIn, string optionsIn, bool logQuery = true, bool logResponse = true)
        {
            IEnergisticsCollection result = null;
            string suppMsgOut, xmlOut;
            var    originalXmlIn = xmlIn;

            if (Connection.CompressRequests)
            {
                ClientCompression.Compress(ref xmlIn, ref optionsIn);
            }

            if (logQuery)
            {
                LogQuery(Functions.GetFromStore, objectType, originalXmlIn, optionsIn);
            }

            var returnCode = ExecuteQuery(Functions.GetFromStore, objectType, xmlIn, optionsIn, out xmlOut, out suppMsgOut);

            if (returnCode < 1)
            {
                if (logResponse)
                {
                    LogResponse(Functions.GetFromStore, objectType, xmlIn, optionsIn, null, returnCode, suppMsgOut);
                }

                return(null);
            }

            try
            {
                // Handle servers that compress the response to a compressed request.
                if (Connection.CompressRequests)
                {
                    xmlOut = ClientCompression.SafeDecompress(xmlOut);
                }

                if (returnCode > 0)
                {
                    var document = WitsmlParser.Parse(xmlOut);
                    var family   = ObjectTypes.GetFamily(document.Root);
                    var listType = ObjectTypes.GetObjectGroupType(objectType, family, DataSchemaVersion);

                    result = WitsmlParser.Parse(listType, document.Root) as IEnergisticsCollection;
                }
            }
            catch (WitsmlException ex)
            {
                _log.ErrorFormat("Error parsing query response: {0}{2}{2}{1}", xmlOut, ex, Environment.NewLine);
                returnCode = (short)ex.ErrorCode;
                suppMsgOut = ex.Message + " " + ex.GetBaseException().Message;
            }

            if (logResponse)
            {
                LogResponse(Functions.GetFromStore, objectType, originalXmlIn, optionsIn, xmlOut, returnCode, suppMsgOut);
            }

            return(result);
        }
Example #3
0
        /// <summary>
        /// Submits the query to the WITSML server for the given function type and input XML.
        /// </summary>
        /// <param name="functionType">Type of the function to execute.</param>
        /// <param name="xmlIn">The XML in.</param>
        /// <param name="optionsIn">The options in.</param>
        /// <returns>
        /// A tuple of four result values in the following order: xmlOut, suppMsgOut, optionsIn and returnCode.
        /// </returns>
        internal async Task <WitsmlResult> SubmitQuery(Functions functionType, string xmlIn, string optionsIn)
        {
            string objectType = null;
            string xmlOut     = null;
            short  returnCode = 0;

            try
            {
                // Compute the object type of the incoming xml.
                if (functionType.RequiresObjectType() && !string.IsNullOrWhiteSpace(xmlIn))
                {
                    try
                    {
                        var document = WitsmlParser.Parse(xmlIn);
                        objectType = ObjectTypes.GetObjectTypeFromGroup(document.Root);
                    }
                    catch (WitsmlException ex)
                    {
                        returnCode = (short)ex.ErrorCode;
                        throw;
                    }
                }

                using (var client = Proxy.CreateClientProxy().WithUserAgent())
                {
                    var    wmls = (IWitsmlClient)client;
                    string suppMsgOut;

                    if (wmls.CompressRequests && functionType.SupportsRequestCompression())
                    {
                        xmlIn = ClientCompression.GZipCompressAndBase64Encode(xmlIn);
                    }

                    // Execute the WITSML server function for the given functionType
                    switch (functionType)
                    {
                    case Functions.GetCap:
                        returnCode = wmls.WMLS_GetCap(optionsIn, out xmlOut, out suppMsgOut);
                        ProcessCapServer(xmlOut);
                        break;

                    case Functions.GetBaseMsg:
                        returnCode = Model.ErrorCode.GetValueOrDefault();
                        suppMsgOut = wmls.WMLS_GetBaseMsg(returnCode);
                        break;

                    case Functions.AddToStore:
                        returnCode = wmls.WMLS_AddToStore(objectType, xmlIn, optionsIn, null, out suppMsgOut);
                        break;

                    case Functions.UpdateInStore:
                        returnCode = wmls.WMLS_UpdateInStore(objectType, xmlIn, optionsIn, null, out suppMsgOut);
                        break;

                    case Functions.DeleteFromStore:
                        returnCode = wmls.WMLS_DeleteFromStore(objectType, xmlIn, optionsIn, null, out suppMsgOut);
                        break;

                    default:
                        returnCode = wmls.WMLS_GetFromStore(objectType, xmlIn, optionsIn, null, out xmlOut, out suppMsgOut);
                        break;
                    }

                    if (returnCode > 0)
                    {
                        // Handle servers that compress the response to a compressed request.
                        if (wmls.CompressRequests)
                        {
                            xmlOut = ClientCompression.SafeDecompress(xmlOut);
                        }
                    }

                    return(await Task.FromResult(new WitsmlResult(objectType, xmlIn, optionsIn, null, xmlOut, suppMsgOut, returnCode)));
                }
            }
            catch (Exception ex)
            {
                var message = string.Format("Error calling WITSML Store API method '{0}'{3}{3}Error Message: {1}{3}{3}Stack Trace:{3}{2}{3}",
                                            functionType, ex.Message, ex.StackTrace, Environment.NewLine);

                // Log the error message
                _log.Error(message);

                // Return the error to the caller so message and call stack can be displayed to the user
                return(await Task.FromResult(new WitsmlResult(objectType, xmlIn, optionsIn, null, xmlOut, message, returnCode)));
            }
        }