Esempio n. 1
0
        /// <summary>
        /// Sets the properties of the <see cref="DataObject"/> instance.
        /// </summary>
        /// <typeparam name="T">The type of entity.</typeparam>
        /// <param name="dataObject">The data object.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="uri">The URI.</param>
        /// <param name="name">The name.</param>
        /// <param name="childCount">The child count.</param>
        /// <param name="lastChanged">The last changed in microseconds.</param>
        /// <param name="compress">if set to <c>true</c> compress the data object.</param>
        public static void SetDataObject<T>(DataObject dataObject, T entity, EtpUri uri, string name, int childCount = -1, long lastChanged = 0, bool compress = true)
        {
            if (entity == null)
            {
                dataObject.SetString(null);
            }
            else if (entity is string)
            {
                dataObject.SetString(entity.ToString());
            }
            else
            {
                var data = EtpContentType.Json.EqualsIgnoreCase(uri.ContentType.Format)
                    ? Energistics.Common.EtpExtensions.Serialize(entity)
                    : WitsmlParser.ToXml(entity);

                dataObject.SetString(data, compress);
            }

            dataObject.Resource = new Resource()
            {
                Uri = uri,
                Uuid = uri.ObjectId,
                Name = name,
                HasChildren = childCount,
                ContentType = uri.ContentType,
                ResourceType = ResourceTypes.DataObject.ToString(),
                CustomData = new Dictionary<string, string>(),
                LastChanged = lastChanged,
                ChannelSubscribable = uri.IsChannelSubscribable(),
                ObjectNotifiable = uri.IsObjectNotifiable()
            };
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a new <see cref="WitsmlQueryParser"/> from the specified data object.
        /// </summary>
        /// <param name="dataObject">The data object.</param>
        /// <returns>A new <see cref="WitsmlQueryParser"/> instance.</returns>
        protected virtual WitsmlQueryParser CreateQueryParser(TObject dataObject)
        {
            var document   = WitsmlParser.Parse(WitsmlParser.ToXml(dataObject));
            var objectType = ObjectTypes.GetObjectType(dataObject as AbstractObject);

            return(new WitsmlQueryParser(document.Root, objectType, null));
        }
        /// <summary>
        /// Sends the notification messages for the speficed entity.
        /// </summary>
        /// <typeparam name="T">The data object type.</typeparam>
        /// <param name="entity">The changed entity.</param>
        /// <param name="auditHistory">The audit history.</param>
        public void SendNotifications <T>(T entity, DbAuditHistory auditHistory)
        {
            // No action if broker list not configured
            if (string.IsNullOrWhiteSpace(KafkaSettings.BrokerList))
            {
                return;
            }

            var uri = auditHistory.Uri.ToLowerInvariant();
            var xml = WitsmlParser.ToXml(entity);

            var topic = auditHistory.LastChangeType == ChangeInfoType.delete
                ? KafkaSettings.DeleteTopicName
                : KafkaSettings.UpsertTopicName;

            Task.Run(() =>
            {
                try
                {
                    using (var producer = new Producer <string, string>(_config, _serializer, _serializer))
                    {
                        _log.Debug($"{producer.Name} producing on {topic}.");

                        var task   = producer.ProduceAsync(topic, uri, xml);
                        var result = task.Result;

                        _log.Debug($"Partition: {result.Partition}; Offset: {result.Offset}");
                    }
                }
                catch (Exception ex)
                {
                    _log.Warn($"Error producing on topic: {topic}", ex);
                }
            });
        }
        public void Log200DataAdapter_Log_Can_Be_Added_With_Secondary_Index()
        {
            AddParents();

            var mdChannelIndex = DevKit.LogGenerator.CreateMeasuredDepthIndex(IndexDirection.increasing);

            DevKit.InitHeader(Log, LoggingMethod.MWD, mdChannelIndex);

            var secondaryIndex = DevKit.LogGenerator.CreateDateTimeIndex();
            var channelSet     = Log.ChannelSet.First();

            channelSet.Index.Add(secondaryIndex);

            // Generate mock data
            DevKit.CreateMockChannelSetData(channelSet, channelSet.Index);

            File.WriteAllText("TestData/DepthLogWithSecondaryIndex-2.0-Well.xml", WitsmlParser.ToXml(Well));
            File.WriteAllText("TestData/DepthLogWithSecondaryIndex-2.0-Wellbore.xml", WitsmlParser.ToXml(Wellbore));
            File.WriteAllText("TestData/DepthLogWithSecondaryIndex-2.0.xml", WitsmlParser.ToXml(Log));

            DevKit.AddAndAssert(Log);
            var log = DevKit.GetAndAssert(Log);

            Assert.AreEqual(Log.Citation.Title, log.Citation.Title);
            Assert.AreEqual(Log.Uuid, log.Uuid);
        }
Esempio n. 5
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);
            }
        }
        /// <summary>
        /// Sends the notification messages for the specified entity.
        /// </summary>
        /// <typeparam name="T">The data object type.</typeparam>
        /// <param name="entity">The changed entity.</param>
        /// <param name="auditHistory">The audit history.</param>
        public void SendNotifications <T>(T entity, DbAuditHistory auditHistory)
        {
            // No action if broker list not configured
            if (string.IsNullOrWhiteSpace(KafkaSettings.BrokerList))
            {
                return;
            }

            var uri = auditHistory.Uri.ToLowerInvariant();
            var xml = WitsmlParser.ToXml(entity);

            var topic = auditHistory.LastChangeType == ChangeInfoType.delete
                ? KafkaSettings.DeleteTopicName
                : auditHistory.LastChangeType == ChangeInfoType.add
                    ? KafkaSettings.InsertTopicName
                    : KafkaSettings.UpdateTopicName;

            SendNotification(topic, uri, xml);

            // For backward compatibility with ETP v1.1
            if (auditHistory.LastChangeType != ChangeInfoType.delete)
            {
                SendNotification(KafkaSettings.UpsertTopicName, uri, xml);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Creates a new <see cref="WitsmlQueryParser"/> from the specified data object.
        /// </summary>
        /// <param name="dataObject">The data object.</param>
        /// <returns>A new <see cref="WitsmlQueryParser"/> instance.</returns>
        protected override WitsmlQueryParser CreateQueryParser(TObject dataObject)
        {
            var container  = CreateCollection(dataObject.AsList());
            var document   = WitsmlParser.Parse(WitsmlParser.ToXml(container));
            var objectType = ObjectTypes.GetObjectType(container);

            return(new WitsmlQueryParser(document.Root, objectType, null));
        }
Esempio n. 8
0
        /// <summary>
        /// Called when a data object is selected.
        /// </summary>
        private void OnDataObjectSelected()
        {
            if (DataObject == null || DataObject == QueryTemplateText)
            {
                return;
            }

            var type  = ObjectTypes.GetObjectGroupType(DataObject, Model.WitsmlVersion);
            var query = Proxy.BuildEmtpyQuery(type, Model.WitsmlVersion);

            XmlQuery.SetText(WitsmlParser.ToXml(query));
        }
Esempio n. 9
0
        public async Task Trajectory200_PutGrowingPart_Can_Add_TrajectoryStation()
        {
            AddParents();
            await RequestSessionAndAssert();

            var handler = _client.Handler <IStoreCustomer>();
            var uri     = Trajectory.GetUri();

            var dataObject = CreateDataObject(uri, Trajectory);

            // Put a Trajectory with no stations in the store.
            await PutAndAssert(handler, dataObject);

            // Create a TrajectoryStation and Encode it
            var uid = "TrajStation-1";
            var trajectoryStation = CreateTrajectoryStation(uid, LengthUom.ft, "TestDatum", 1);
            var data        = Encoding.UTF8.GetBytes(WitsmlParser.ToXml(trajectoryStation));
            var contentType = EtpContentTypes.Witsml200.For(ObjectTypes.TrajectoryStation);

            // Call PutGrowingPart to add the TrajectoryStation to the Trajectory
            var dataAdapter = DevKit.Container.Resolve <IGrowingObjectDataAdapter>(ObjectNames.Trajectory200);

            dataAdapter.PutGrowingPart(_client.Adapter, uri, contentType, data);

            // Get the Trajectory Object from the store
            var args = await GetAndAssert(handler, uri);

            // Check Data Object XML
            Assert.IsNotNull(args?.Message.DataObject);
            var xml = args.Message.DataObject.GetString();

            var result = Parse <Trajectory>(xml);

            // Validate that the Trajectory could be retrieved from the store and the MDMin matches the station that was entered.
            Assert.IsNotNull(result);
            Assert.IsNotNull(result.MDMin);
            Assert.AreEqual(1, result.MDMin.Value);

            var dataObjectGet = dataAdapter.GetGrowingPart(_client.Adapter, uri, uid);

            var trajectoryStationObject = GetTrajectoryStation(dataObjectGet);

            Assert.IsNotNull(trajectoryStationObject);
            Assert.IsNotNull(trajectoryStationObject.MD);
            Assert.IsNotNull(trajectoryStationObject.Incl);
            Assert.IsNotNull(trajectoryStationObject.Azi);
            Assert.AreEqual(trajectoryStation.MD.Value, trajectoryStationObject.MD.Value);
            Assert.AreEqual(trajectoryStation.Incl.Value, trajectoryStationObject.Incl.Value);
            Assert.AreEqual(trajectoryStation.Azi.Value, trajectoryStationObject.Azi.Value);
        }
Esempio n. 10
0
        /// <summary>
        /// Handles the PutObject message of the Store protocol.
        /// </summary>
        /// <param name="header">The message header.</param>
        /// <param name="putObject">The put object message.</param>
        protected override void HandlePutObject(IMessageHeader header, PutObject putObject)
        {
            base.HandlePutObject(header, putObject);

            var uri = this.CreateAndValidateUri(putObject.DataObject.Resource.Uri, header.MessageId);

            if (!uri.IsValid)
            {
                return;
            }
            if (!this.ValidateUriParentHierarchy(uri, header.MessageId))
            {
                return;
            }
            if (!this.ValidateUriObjectType(uri, header.MessageId))
            {
                return;
            }

            try
            {
                var data = putObject.DataObject.GetString();

                if (EtpContentType.Json.EqualsIgnoreCase(uri.ContentType.Format))
                {
                    var objectType = uri.IsRelatedTo(EtpUris.Witsml200) || uri.IsRelatedTo(EtpUris.Eml210)
                        ? ObjectTypes.GetObjectType(uri.ObjectType, uri.Family, OptionsIn.DataVersion.Version200.Value)
                        : ObjectTypes.GetObjectGroupType(uri.ObjectType, uri.Family, uri.Version);

                    var instance = Energistics.Etp.Common.EtpExtensions.Deserialize(objectType, data);
                    data = WitsmlParser.ToXml(instance);
                }

                WitsmlOperationContext.Current.Request = new RequestContext(Functions.PutObject, uri.ObjectType, data, null, null);

                var dataAdapter = Container.Resolve <IEtpDataProvider>(new ObjectName(uri.ObjectType, uri.GetDataSchemaVersion()));
                dataAdapter.Put(putObject.DataObject);

                Acknowledge(header.MessageId);
            }
            catch (ContainerException ex)
            {
                this.UnsupportedObject(ex, putObject.DataObject.Resource.Uri, header.MessageId);
            }
            catch (WitsmlException ex)
            {
                ProtocolException((int)EtpErrorCodes.InvalidObject, $"Invalid object: {ex.Message}; Error code: {(int)ex.ErrorCode}", header.MessageId);
            }
        }
        /// <summary>
        /// Returns the server capabilities object as XML.
        /// </summary>
        /// <returns>A capServers object as an XML string.</returns>
        public string ToXml()
        {
            if (!string.IsNullOrWhiteSpace(_capServerXml))
            {
                return(_capServerXml);
            }

            var capServer = GetCapServer();

            if (capServer != null)
            {
                _capServerXml = WitsmlParser.ToXml(capServer);
            }

            return(_capServerXml);
        }
Esempio n. 12
0
        /// <summary>
        /// Sets the properties of the <see cref="IDataObject"/> instance.
        /// </summary>
        /// <typeparam name="T">The type of entity.</typeparam>
        /// <param name="dataObject">The data object.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="uri">The URI.</param>
        /// <param name="name">The name.</param>
        /// <param name="childCount">The child count.</param>
        /// <param name="lastChanged">The last changed in microseconds.</param>
        public static void SetDataObject <T>(IDataObject dataObject, T entity, EtpUri uri, string name, int childCount = -1, long lastChanged = 0)
        {
            if (entity == null)
            {
                dataObject.SetString(null);
            }
            else if (entity is string)
            {
                dataObject.SetString(entity.ToString());
            }
            else
            {
                var data = EtpContentType.Json.EqualsIgnoreCase(uri.ContentType.Format)
                    ? Energistics.Etp.Common.EtpExtensions.Serialize(entity)
                    : WitsmlParser.ToXml(entity, removeTypePrefix: true);

                dataObject.SetString(data, false);
            }

            double version;
            var    uuid = double.TryParse(uri.Version, out version) && version >= 2.0 ? uri.ObjectId : null;

            if (string.IsNullOrWhiteSpace(uuid))
            {
                uuid = entity.GetPropertyValue <string>(ObjectTypes.Uuid);
            }

            dataObject.Resource = new Resource
            {
                Uri                 = uri,
                Uuid                = uuid ?? string.Empty,
                Name                = name,
                ChildCount          = childCount,
                ContentType         = uri.ContentType,
                ResourceType        = ResourceTypes.DataObject.ToString(),
                CustomData          = new Dictionary <string, string>(),
                LastChanged         = lastChanged,
                ChannelSubscribable = uri.IsChannelSubscribable(),
                ObjectNotifiable    = uri.IsObjectNotifiable()
            };
        }
Esempio n. 13
0
        /// <summary>
        /// Converts a data object collection to XML and optionally converts to a requested version.
        /// </summary>
        /// <param name="request">The GetFromStore request.</param>
        /// <param name="collection">The data object collection.</param>
        /// <returns></returns>
        private string GetXmlOut(WMLS_GetFromStoreRequest request, IEnergisticsCollection collection)
        {
            if (collection == null)
            {
                return(string.Empty);
            }
            EnsureCapServerProviders();

            var    optionsIn = WitsmlOperationContext.Current.OptionsIn;
            string requestedVersion;

            // Attempt transformation if client requested a different version
            if (optionsIn.TryGetValue(OptionsIn.DataVersion.Keyword, out requestedVersion) &&
                _capServerMap.ContainsKey(requestedVersion) &&
                collection.GetVersion() != requestedVersion)
            {
                _log.Debug($"Transforming XMLOut to data schema version {requestedVersion}");
                collection = WitsmlParser.Transform(collection, requestedVersion);
            }

            return(WitsmlParser.ToXml(collection));
        }
Esempio n. 14
0
        /// <summary>
        /// Puts the growing part for a growing object.
        /// </summary>
        /// <param name="etpAdapter">The ETP adapter.</param>
        /// <param name="uri">The growing obejct's URI.</param>
        /// <param name="contentType">Type of the content.</param>
        /// <param name="data">The data.</param>
        public override void PutGrowingPart(IEtpAdapter etpAdapter, EtpUri uri, string contentType, byte[] data)
        {
            var dataObject = etpAdapter.CreateDataObject();

            dataObject.Data = data;

            // Convert byte array to TrajectoryStation
            var trajectoryStationXml = dataObject.GetString();
            var tsDocument           = WitsmlParser.Parse(trajectoryStationXml);
            var trajectoryStation    = WitsmlParser.Parse <TrajectoryStation>(tsDocument.Root);

            // Merge TrajectoryStation into the Trajectory if it is not null
            if (trajectoryStation != null)
            {
                // Get the Trajectory for the uri
                var entity = GetEntity(uri);
                entity.TrajectoryStation = trajectoryStation.AsList();

                var document = WitsmlParser.Parse(WitsmlParser.ToXml(entity));
                var parser   = new WitsmlQueryParser(document.Root, ObjectTypes.GetObjectType <Trajectory>(), null);
                UpdateTrajectoryWithStations(parser, entity, uri, true);
            }
        }
Esempio n. 15
0
        /// <summary>
        /// Gets the channel data records for the specified URI and range.
        /// </summary>
        /// <param name="uri">The uri of the data object</param>
        /// <param name="range">The range of the channel data</param>
        /// <param name="mnemonics">The channel mnemonics</param>
        /// <param name="requestLatestValues">true if only the latest values are requested, false otherwise</param>
        /// <param name="optimizeStart"></param>
        /// <returns></returns>
        public List <List <List <object> > > GetChannelData(EtpUri uri, Range <double?> range, List <string> mnemonics, int?requestLatestValues, bool optimizeStart = false)
        {
            var entity   = GetEntity(uri);
            var stations = GetStations(entity, range.Start, range.End);

            if (requestLatestValues.HasValue)
            {
                stations = stations.AsEnumerable().Reverse()          // Pick from the bottom
                           .Take(requestLatestValues.Value).Reverse() // Revert to original sort
                           .ToList();
            }

            return(stations
                   .Select(x => new List <List <object> >
            {
                new List <object> {
                    x.MD.Value
                },
                new List <object> {
                    WitsmlParser.ToXml(x)
                }
            })
                   .ToList());
        }
Esempio n. 16
0
 private string ToXml(object instance)
 {
     return(WitsmlParser.ToXml(instance));
 }